Oracle 如何实现自增id
作者:互联网
- 创建一个序列
- 创建一个触发器
- 绑定触发器
-- sql 语句
create table employee(
id int,
DeptNo number,
EmpNo number,
Ename varchar(16),
Job varchar(32),
Sal float,
HireDate date,
constraint pk_employee primary key(EmpNo)
);
-- 2. 创建序列
create sequence employ_autoinc
minvalue 1
maxvalue 999999999999
start with 1
increment by 1
nocache;
-- 3. 创建一个触发器
/*-- Created on 2021/2/8 by HP
create or replace trigger insert_employ_autoinc
before insert on employee
for each row
begin
-- Test statements here
select employ_autonic.nextval into:new.id from dual;
end insert_employ_autoinc;*/
insert into employee(DeptNo,EmpNo,Ename,job,sal,hiredate)
values(520,5201002,'James zhou','PD',6000,to_date('2012-10-22','yyyy-mm-dd'));
select * from employee;
注意 先创建序列 在创建触发器
触发器内容
create or replace trigger insert_employ_autoinc
before insert on employee
for each row
begin
-- Test statements here
select employ_autoinc.nextval into:new.id from dual;
end insert_employ_autoinc;
插入数据
insert into employee(DeptNo,EmpNo,Ename,job,sal,hiredate)
values(520,5201002,'James zhou','PD',6000,to_date('2012-10-22','yyyy-mm-dd'));
insert into employee(DeptNo,EmpNo,Ename,job,sal,hiredate)
values(530,5201003,'kai yang','PD',6000,to_date('2012-10-22','yyyy-mm-dd'));
查询
select * from employee;
删除表
drop table employee;
drop sequence employ_autoinc;
标签:insert,自增,autoinc,EmpNo,employ,employee,Oracle,--,id 来源: https://www.cnblogs.com/yk2763691852/p/14389829.html