Oracle创建表,id为自增序列

来源:互联网 发布:英雄联盟淘宝网 编辑:程序博客网 时间:2024/05/21 19:46

1.创建表

create table t_user (

 id number(10) constraint pk_id primary key,
 name varchar2(20) not null,
 phone_Number varchar2(20) constraint unique_phone_number unique,
 email_Address varchar2(200) constraint email_not_null not null,
 home_Address varchar2(200) constraint home_addr_not_null not null
)
#查看约束
select * from user_constraints;


2.创建序列

create sequence t_user_id_seq start with 1 increment by 1;
#查看序列
select * from user_sequences;


3.创建触发器

create or replace trigger t_user_trigger
before insert on t_user
for each row
when(new.id is null)
begin
  select t_user_id_seq.nextval into:NEW.ID from dual;
end;
#查看触发器
select * from user_triggers;


#测试

insert into t_user(name,phone_number,email_address,home_address) values('zcl','13800138000','hy@gmail.com','北京')
commit;

select * from t_user;
0 0