DB2自增字段IDENTITY应该如何使用呢?相信这是很多人都提到过的问题,下面就为您详细介绍DB2自增字段IDENTITY的用法,供您参考。
A.Generated always
值由DB2生成,客户不能直接赋值
Example:
Create table t1
(id int generated always as identity (start with 100 increment by 1),description char(10));
Commit;
Insert into t1 values (default,’a1’); //insert100 a1
Insert into t1(description) values (’a1’); //insert101 a1
Insert into t1 values (200,’a1’); //erro
Commit;
Insert into t1(description) values (’a1’); //insert102 a1
Rollback;
Insert into t1(description) values (’a1’); //insert103 a1
Commit;
Select * from t1;
100 a1
101 a1
103 a1
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
B.Generated by default
值可由DB2生成,也可以准许客户直接赋值,不过DB2不能保证提供的值唯一。
Example:
Create table t1
(id int generated by default as identity (start with 100 increment by 1),description char(10)) in userspace1
Commit;
Insert into t1 values (default,’a1’); //insert100 a1
Insert into t1(description) values (’a1’); //insert101 a1
Insert into t1 values (200,’a1’); // insert200 a1
Insert into t1 values (102,’a1’); // insert102 a1
Commit;
Insert into t1(description) values (’a1’); //erro ,因此自增的当前值为102,数据库中已经存在102的主键值了
Insert into t1(description) values (’a1’); //insert103 a1
Commit;
Select * from t1;
100 a1
101 a1
102 a1
103 a1
200 a1
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
以上DB2自增字段IDENTITY的用法介绍。
【编辑推荐】