oracle 如何设置主键自增(自增长字段)
oracle有点麻烦,需要使用序列和触发器达到目的。
具体步骤如下:
一、创建数据表
create table AAA_employee(
Id int ,
DeptNo number,
EmpNo number,
Ename varchar2(16),
Job varchar2(32),
Sal float,
HireDate date,
constraint pk_AAA_employee primary key(EmpNo)
);
二、创建员工表自动增长序列
create sequence AAA_employee_autoinc
minvalue 1
maxvalue 9999999999999999999999999999
start with 1
increment by 1
nocache;
三、创建触发器将序列中的值赋给插入AAA_employee表的行
create or replace trigger insert_AAA_employee_autoinc
before insert on AAA_employee
for each row
begin
select AAA_employee_autoinc.nextval into :new.Id from dual;
end insert_AAA_employee_autoinc;
/
四、验证
insert into AAA_employee(DeptNo,EmpNo,Ename,job,sal,hiredate) values(520,5201002,'James','PD',6000,to_date('2012-10-22','yyyy-mm-dd'));
insert into AAA_employee(DeptNo,EmpNo,Ename,job,sal,hiredate) values(521,5211314,'xc','Boss',90000,sysdate);
select * from AAA_employee
REF
https://www.cnblogs.com/remote/p/9974842.html