当前位置: 移动技术网 > IT编程>数据库>Oracle > ORACLE数据库创建表、自增主键、外键相关语法讲解

ORACLE数据库创建表、自增主键、外键相关语法讲解

2018年12月09日  | 移动技术网IT编程  | 我要评论

n1116,徐州双胞胎饲料,芒果娱乐网

一 创建表结构:

drop table base_list;

create table base_list (

“data_list_id” number not null ,

“base_data_id” number not null ,

“base_data_list” varchar2(100 byte) not null ,

“flag” number default 0 not null

)

logging

nocompress

nocache

二 创建序列:

create sequence seq_data_list_id– seq_data_list_id 自动增长列

increment by 1 – 每次加几个

start with 1 – 从1开始计数

nomaxvalue – 不设置最大值

nocycle – 一直累加,不循环

nocache

三 创建触发器:

create or replace trigger “bsqam1”.”t_data_list_id” before insert on “bsqam1”.”base_list” referencing old as “old” new as “new” for each row

begin

—在新增之前将自增的主键字段的值赋值为sequence的nextval

select seq_data_list_id.nextval into :new.data_list_id from dual;

end;

四 创建外键:

–使用alter table创建外键

alter table certificate

add constraint f_user_id

foreign key (user_id)

references tuser(user_id);

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网