Oracle 实现自增主键
废话不多讲,直接上代码
//1.创建表
Create Table testZcm(
Id Number(2) Not Null Primary Key,
postCode Number(6) Default '0' Not Null,
areaName Varchar2(20) Default '' Not Null
)
//2.创建自动增长序列
Create Sequence zcmSequence Minvalue 1 Increment By 1 Start With 1;
//3.创建触发器
Create Trigger test_seq Before
Insert On testZcm For Each Row When (new.id Is Null)
Begin
Select zcmSequence.Nextval Into :new.id From dual;
End;
//4.测试
Insert Into testzcm (postCode,Areaname)Values(3333,'b');
Insert Into testzcm (postCode,Areaname)Values(234,'qb');
Insert Into testzcm (postCode,Areaname)Values(345,'yu');
Select * From testzcm
————————————————
版权声明:本文为CSDN博主「朝明_张」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhangchaoming123/article/details/51604607