How to modify a column of auto-increment primary keys in a DB2 table

Source: Internet
Author: User

Method 1 for modifying a column in a DB2 table as an auto-increment primary key) to change a column in the table to auto-increment, run the following command:

Alter table <table name> alter column <column name> set generated always as identity (start with 1,increment by 1) 

 

The above command is useful online when you change the attributes of a column in a table. 2) When you modify the starting value of an auto-increment column in a table, run the following command:
ALTER TABLE <talbe_name> ALTER COLUMN <column name> RESTART WITH 18; 

 

DB2 can use generated always as identity to specify a field as a self-increasing field. For example, this indicates that id is automatically a self-increasing field, which increases by 1 each time from 1. You can also use generated to calculate the field value through other fields, for example;
Create table user (name varchar (30), n1 integer, n2 integer, id integer generated always as (n1 + n2) Example: create table strategy (strategy_id decimal (17) generated always as identity (start with 1, increment by 1) primary key not null, strategy_name varchar (200), area_code decimal (6, 0); generated always as identity (start with 1, increment by 1)

 

Auto-increment starts from 1, and 1 is added each time. the biggest difference between the DB2 Identity field and sequence: the Identity field and sequence field is that identity can only be used in one table, and identity cannot be shared among multiple tables; sequence can share the serial number generator in multiple tables in the same database by naming them to generate digital sequences. in DB2, two methods can be used to automatically generate a digital sequence: define columns with the IDENTITY attribute. Create a SEQUENCE object. When you use the IDENTITY attribute to define a table column, each time a row is inserted into the table, a value is automatically generated for the column. The second method that enables DB2 to automatically generate a SEQUENCE of numbers is to create a SEQUENCE object. You can use a sequence expression to reference a sequence object. Sequence expressions can appear in most positions where expressions can appear. Sequence expressions can specify whether the value to be returned is a new value or a previously generated value. If the sequence reference is for the next value, a value is automatically generated for the sequence and returned as the result of the sequence expression. For example, if a sequence named orders_seq has been created, the sequence expression returns the next value generated for the sequence: next value for orders_seq if the sequence reference is FOR the previous value, the VALUE generated FOR the sequence in the previous SQL statement is returned as the result of the sequence expression. This sequence expression returns the previous value generated by the sequence: previous value for orders_seq Note: When DB2 UDB introduced the sequence, it once supported the non-SQL standard syntax NEXTVAL instead of NEXT VALUE, and PREVVAL instead of previous value. These variants continue to be supported. Select between the marker and the sequence. Although both the marker and the sequence are used to generate values, you may choose one to replace the other according to special circumstances. The identification column is useful in the following situations: there is only one column of values to be automatically generated in the Table. Each row requires independent values and the auto generator is used to generate the table's primary key. The process of generating new values is closely related to the table insertion Operation, sequence objects are useful no matter how an insert operation occurs: to store values generated from a sequence to multiple tables, each table has multiple columns that need to be automatically generated. (You may use the same sequence or multiple sequences to generate multiple values for each row. values) the process of generating a new value is irrelevant to any table reference and is different from the sequence object. The identification column is defined on the table, so some restrictions are required. Each table can have only one identification column. When creating a column as the identification column, the exact numeric data type must be used for this column. Because the identity attribute generates a value for the column, which is similar to the DEFAULT clause, you cannot specify the DEFAULT clause when defining the identity column. The identifier column is implicitly defined as not null.

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.