Oracle主键自增

1、创建table

1 CREATE TABLE demo6
2 (
3     id INT NOT NULL,
4     key1 VARCHAR2(40) NULL,
5     key2 VARCHAR2(40) NULL
6 );

2、设置主键

1 alter table demo6 add constraint demo6_pk primary key (id);

3、新建序列

1 create sequence demo6_id
2 minvalue 1
3 nomaxvalue 
4 increment by 1 
5 start with 1
6 nocache;

4、新建触发器

1 create or replace trigger demo6_tg_insertId
2 before insert on demo6 for each row 
3 begin
4   select demo6_id.Nextval into:new.id from dual;
5 end;

5、插入数据

1 insert into demo6 (key1, key2)
2 values ('key1', 'key2');
3 insert into demo6 (key1, key2)
4 values ('key11', 'key22');

6、查询table

1 select * from demo6;

 7、查询当前序列值

1 select demo6_id.currval from dual;

参考文章:http://www.cnblogs.com/dshore123/p/8267240.html


原文地址:https://www.cnblogs.com/xiaostudy/p/10062491.html