当前位置:Gxlcms > 数据库问题 > Oracle主键自增

Oracle主键自增

时间:2021-07-01 10:21:17 帮助过:3人阅读

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;

技术分享图片

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


 

Oracle主键自增

标签:constrain   htm   primary   span   cache   rom   dem   end   sele   

人气教程排行