Use of the DB2 auto-incrementing field IDENTITY

Source: Internet
Author: User
Tags erro

How should I use the DB2 auto-incrementing field IDENTITY? I believe this is a problem that many people have mentioned. The following describes the usage of the DB2 auto-incrementing field IDENTITY for your reference.

A. Generated always
The value is generated by DB2 and cannot be assigned directly.

 
 
  1. Example:  
  2. Create table t1  
  3. (id int generated always as identity (start with 100 increment by 1),description char(10));  
  4.       Commit;  
  5.       Insert into t1 values (default,’a1’);                         //insert100 a1  
  6.    Insert into t1(description) values (’a1’);                    //insert101 a1  
  7.       Insert into t1 values (200,’a1’);                             //erro  
  8.       Commit;  
  9.       Insert into t1(description) values (’a1’);                    //insert102 a1  
  10.       Rollback;  
  11.       Insert into t1(description) values (’a1’);                    //insert103 a1  
  12.       Commit;  
  13.       Select * from t1;  
  14.       100       a1  
  15.       101       a1  
  16.       103       a1 

B. Generated by default
Values can be generated by DB2 or assigned directly by the customer. However, DB2 cannot guarantee that the provided values are unique.

 
 
  1. Example:
  2. Create table t1
  3. (Id int generated by default as identity (start with 100 increment by 1), description char (10) in userspace1
  4. Commit;
  5. Insert into t1 values (default, 'a1'); // insert100 a1
  6. Insert into t1 (description) values ('a1'); // insert101 a1
  7. Insert into t1 values (200, 'a1'); // insert200 a1
  8. Insert into t1 values (102, 'a1'); // insert102 a1
  9. Commit;
  10. Insert into t1 (description) values ('a1'); // erro, so the current auto-increment value is 102, and the database already has a 102 primary key value.
  11. Insert into t1 (description) values ('a1'); // insert103 a1
  12. Commit;
  13. Select * from t1;
  14. 100 a1
  15. 101 a1
  16. 102 a1
  17. 103 a1
  18. 200 a1

The usage of the DB2 auto-incrementing field IDENTITY is described above.

Introduction to DB2 datetime Data Types

Six common DB2 command lines

Four precautions for DB2 Data Import

DB2 Management page size limit

Policy Selection for DB2 environment variable Management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.