Implementation of the DB2 auto increment Field

Source: Internet
Author: User

This is generally used as the identification code and serves as the primary key for defining the table. The generated syntax allows you to customize the policy for generating this value.
Syntax:
Column definition generated {always | by default}
As {identity rules | using your rules}
Delete the table we created last time:
Db2 => drop table nomination
Create another table:
Copy codeThe Code is as follows:
Create table nomination
(
NominationID BIGINT Not Null Primary Key generated always as identity,
Nominee char (6) Not Null,
Nominator char (6) Not Null,
Reason VARCHAR (250 ),
Nomdate date Not Null,
Categoryid INTEGER Not Null,
Check (nominee! = Nominator) not enforced enable query optimization,
Foreign Key CategoryExists (categoryid)
References category (categoryid) on delete restrict
)

Note that the values in the blacklist cannot be explicitly specified using insert or update.
Identity in DB2 also provides multiple policies. For details, you can check the DB2 Manual. The following is an example:
Delete the table we created last time:
Db2 => drop table category
Create a form
Copy codeThe Code is as follows:
Create table category
(
CategoryID INTEGER Primary Key Generated Always as Identity
(Start With 1 Increment by 1 minvalue 0 maxvalue 999999999
No cycle cache 5 no order ),
CateogryName VARCHAR (50) Not Null,
Eligibility VARCHAR (250)
)

All the statements in identity in the black Chinese characters can be found in the DB2 manual. They are all understood by the natural language at a glance.
Sometimes you don't just want to fill in numbers, but you may want to process some letters, so the following example of converting uppercase is for you:
Db2 => alter table category add column
UpperCatName VARCHAR (50) generated always as (upper (CategoryName ))
These are described in the DB2 document.

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.