1、方式一:使用序列和触发器
- sql>CREATESEQUENCEtest_sequence2
- 2incrementby1--每次递增1
- 3startwith1--从1开始
- 4nomaxvalue--没有最大值
- 5minvalue1--最小值=1
- 6NOCYCLE;--不循环
- Sequencecreated.
- TABLEtest_create_tab2(
- 2idINT,
- 3valVARCHAR(10),250); line-height:18px"> 4PRIMARYKEY(id)
- 5);
- Tablecreated.
- CREATEORREPLACETRIGGERBeforeTestCreate2Insert
- 2BEFOREINSERTONtest_create_tab2
- 3FOREACHROW
- BEGIN
- 5SELECTtest_sequence2.nextvalINTO:new.idFROMdual;
- 6END;
- 7/
- Triggercreated.
- INTOtest_create_tab2(val)VALUES('NOid');
- 1rowcreated.
- INTOtest_create_tab2(id,val)VALUES(1,'idnouse');
- SELECT*FROMtest_create_tab2;
- IDVAL
- ------------------------------
- 1NOid
- nouse