Oracle创建表,id为自增序列

1.创建表

create table my_table (

 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 my_table_seq start with 1 increment by 1;
#查看序列
select * from user_sequences;

#删除序列

 DROP SEQUENCE my_table_seq;

3.创建触发器

create or replace trigger bi_my_table 
before insert on my_table
for each row
when(new.id is null)
begin
  select my_table_seq.nextval into:NEW.ID from dual;
end;

或者

CREATE OR REPLACE TRIGGER  "bi_my_table" 
  before insert on "my_table"              
  for each row 
begin  
  if :new."ID" is null then
    select "my_table_seq".nextval into :new."ID" from sys.dual;
  end if;
end;
ALTER TRIGGER  "bi_my_table" ENABLE

#查看触发器
select * from user_triggers;

#删除触发器

DROP TRIGGER "bi_my_table" ;

#测试

insert into my_table(name,phone_number,email_address,home_address) values('zcl','13800138000','youremail@gmail.com','guangzhou')
commit;

select * from t_user;

转载于:https://my.oschina.net/youfen/blog/1924489

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值