Oracle中添加自动编号的序列

1. 创建表T_Test
create table T_Test(id int  ,address char(25), pay int);

2. 创建自增序列
create sequence SEQ_T_Test_ID    //创建名为zc的序列
  increment   by   1    //自增长度为1
  start   with   1     //从1开始计数
  minvalue 1     //最小值为1
  nomaxvalue    //没有最大值
  nocache;      //不设置缓存

3. 为表T_Test创建触发器
create or replace trigger T_Test_id  //将触发器绑定在 id 这一列
before insert
on T_Test
for each row
when(new.id is null)
begin
select SEQ_T_Test_ID.nextval into:new.id from dual;
end;

4. 插入数据
insert into T_Test (address,pay) values('anh3u1i',345);
或者
insert into  T_Test  values(SEQ_T_Test_ID.nextval,'anh3u1i',345);
原文地址:https://www.cnblogs.com/aiwz/p/6153901.html