/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

Oracle主键自增

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;

posted @ 2020-07-13 10:10  一品堂.技术学习笔记  阅读(299)  评论(0编辑  收藏  举报