Oracle主键自增设置

 --创建表
 CREATE TABLE tb_demo
 (
     id INT NOT NULL,
     key1 VARCHAR2(40) NULL,
     key2 VARCHAR2(40) NULL
 );

--设置主键
alter table tb_demo add constraint tb_demo_pk primary key (id);

--新建序列
 create sequence tb_demo_id
 minvalue 1
 nomaxvalue 
 increment by 1 
 start with 1
 nocache;
 
 
 --新建触发器
 create or replace trigger tb_demo_tg_insertId
 before insert on tb_demo for each row 
 begin
   select tb_demo_id.Nextval into:new.id from dual;
 end;
 
 --插入数据
 insert into tb_demo (key1, key2)
 values ('key1', 'key2');
 insert into tb_demo (key1, key2)
 values ('key11', 'key22');
 
 --查询表
 select * from tb_demo;


 --查询当前序列值
 select tb_demo_id.currval from dual;

你可能感兴趣的:(Oracle,数据库,oracle,数据库)