DB2 auto-increment column import and export test

https://blog.csdn.net/wfq826qfw/article/details/8443670

1 When you want to modify a column in the table to grow automatically, you can use the following command:
Alter table <table name> alter column <column name> set not null
Alter table <table name> alter column <column name> set generated always as identity ( start with 1, increment by 1)
The above command is very useful to find on the Internet when changing the attributes of a column in a table.

2 When modifying the auto-increment start value of a column in a table, use the following command:
ALTER TABLE <talbe_name> ALTER COLUMN <column name> RESTART WITH 18;

 

test:

CREATE TABLE customer_orders_t (
   order_id  INT NOT NULL  GENERATED ALWAYS AS IDENTITY
      (START WITH 1
        INCREMENT BY 1
        MINVALUE 1
        NO MAXVALUE
       NO CYCLE
       NO CACHE
       ORDER),
   order_date         DATE NOT NULL,
   cust_id           INT NOT NULL,
   product_id         INT NOT NULL,
   quantity           INT NOT NULL,
   price              DECIMAL(10,2)         NOT NULL,
   status             CHAR(9)         NOT NULL,
   PRIMARY KEY (order_date, order_id))


Note: The IDENTITY property in this column and on itself does not guarantee that the resulting sequence value is unique.
However, the PRIMARY KEY constraint guarantees the uniqueness of rows in the table.
To ensure that only auto-generated values ​​are inserted into the identity column, they specify the GENERATED ALWAYS clause.
Using the last generated order_id to determine how many data
options NO CACHE and ORDER ensures that unused identity values ​​are not discarded in the event of a system failure.

 

Test 1

 insert data

insert into customer_orders_t values (default,current date,12,12,12,10.2,'2')

--success


insert into customer_orders_t values (1,current date,12,12,12,10.2,'2')

-- Error because: IDENTITY field does not allow specifying values

​​-- solution
 ALTER TABLE customer_orders_t
        ALTER COLUMN order_id
        SET GENERATED BY DEFAULT


--创建orders_seq对象
 CREATE SEQUENCE orders_seq
        AS INT
        START WITH 1
        INCREMENT BY 1
        MINVALUE 1
        NO MAXVALUE
   NO CYCLE
   NO CACHE
   ORDER


--插入数据
INSERT INTO customer_orders_t VALUES (NEXT VALUE FOR orders_seq, CURRENT DATE,12,12,12,10.2,'2')

 

1. The command line takes the next value of sequence soc.nico_qian:
 db2 "values ​​next value for soc.nico_qian"

2. The command line resets the sequence soc.nico_qian:
 db2 "alter sequence soc.nico_qian restart", the reset value defaults to the MINVALUE when the SEQUENCE was created

3. The command line resets the sequence soc.nico_qian with the specified value of 22:
 db2 "alter sequence soc.nico_qian restart with 22"

4. The command line resets the initial value of the IDENTITY field of the table KS.CHECK_CONDITION to 20:
 db2 "ALTER TABLE KS.CHECK_CONDITION ALTER COLUMN identity_column_name RESTART WITH 20"

5. If the sequence is reset by the command line,
   the VALID field of the binding package of the embedded C program code that uses this sequence will be modified to N, then the next time the code is called, DB2 will automatically Rebinding
   The binding package of this code, this action will bring unpredictable consequences to the application. For example, if this code is
   rebinding during a very frequently used time period, it is very easy to cause deadlock.
   The same problem occurs with the IDENTITY field.

 

 

DB2 self-increasing field table import and export test

 CREATE TABLE EMPLOYEE ( SERIALNUMBER BIGINT NOT NULL
   GENERATED ALWAYS AS IDENTITY
   (START WITH 1, INCREMENT BY 1),
   FIRSTNAME CHAR(64),
   LASTNAME CHAR(64),
   SALARY   DECIMAL(10, 2),
   PRIMARY KEY (SERIALNUMBER))


 CREATE TABLE EMPLOYEE1( SERIALNUMBER BIGINT NOT NULL
   GENERATED ALWAYS AS IDENTITY
   (START WITH 1, INCREMENT BY 1),
   FIRSTNAME CHAR(64),
   LASTNAME CHAR(64),
   SALARY   DECIMAL(10, 2),
   PRIMARY KEY (SERIALNUMBER))


select * from EMPLOYEE;
select * from EMPLOYEE1;


identityignore ignore auto-increment identitymissing auto-generate auto-increment identityoverride use auto-increment

insert into db2admin.EMPLOYEE(FIRSTNAME,LASTNAME,SALARY)values ( 'A','AA',1);

insert into db2admin.EMPLOYEE(FIRSTNAME,LASTNAME,SALARY)values ( 'B','BB',2);

insert into db2admin.EMPLOYEE(FIRSTNAME,LASTNAME,SALARY)values ( 'C','CC',3);

--TEST full export
db2 export to D:\PRODUCTION.ixf of ixf select * from db2admin.EMPLOYEE --Direct
insert error--Not allowed to insert
db2 load CLIENT from D:\PRODUCTION.ixf of ixf replace into db2admin.EMPLOYEE1 NONRECOVERABLE
db2 import from D:\PRODUCTION.ixf of ixf insert into db2admin.EMPLOYEE1
--identityignore ignores the insert from the incremental field method -- insert normal
db2 load CLIENT from D:\PRODUCTION.ixf of ixf modified by identityignore replace into db2admin.EMPLOYEE1 NONRECOVERABLE
db2 import from D:\PRODUCTION.ixf of ixf modified by identityignore commitcount 1000 insert into db2admin.EMPLOYEE1
--identitymissing Automatically generate target self-increment field insert --insert misplaced
db2 load CLIENT from D:\PRODUCTION.ixf of ixf modified by identitymissing replace into db2admin.EMPLOYEE1 NONRECOVERABLE
db2 import from D:\PRODUCTION.ixf of ixf  modified by identitymissing    commitcount 1000  insert into db2admin.EMPLOYEE1
--identityoverride 使用源自增字段方式插入   OK
db2 load CLIENT from D:\PRODUCTION.ixf of ixf  modified by identityoverride  replace into  db2admin.EMPLOYEE1  NONRECOVERABLE


--TEST partial export
db2 export to D:\PRODUCTION_1.ixf of ixf select FIRSTNAME,LASTNAME,SALARY from db2admin.EMPLOYEE --partial
insert OK
db2 load CLIENT from D:\PRODUCTION_1.ixf of ixf replace into db2admin.EMPLOYEE1( FIRSTNAME,LASTNAME,SALARY) NONRECOVERABLE
db2 import from D:\PRODUCTION_1.ixf of ixf insert into db2admin.EMPLOYEE1( FIRSTNAME,LASTNAME,SALARY)
-- Partially ignore the source from the added fields and insert directly OK
db2 load CLIENT from D:\PRODUCTION_1. ixf of ixf modified by identityignore replace into db2admin.EMPLOYEE1( FIRSTNAME,LASTNAME,SALARY) NONRECOVERABLE
db2 import from D:\PRODUCTION_1.ixf of ixf modified by identityignore commitcount 1000 insert into db2admin.EMPLOYEE1( FIRSTNAME,LASTNAME,SALARY)
--identitymissing Automatically generate target auto-increment field and insert OK
db2 load CLIENT from D:\PRODUCTION_1.ixf of ixf  modified by identitymissing  replace into  db2admin.EMPLOYEE1( FIRSTNAME,LASTNAME,SALARY) NONRECOVERABLE
db2 import from D:\PRODUCTION_1.ixf of ixf  modified by identitymissing    commitcount 1000  insert into db2admin.EMPLOYEE1( FIRSTNAME,LASTNAME,SALARY)
--identityoverride 自动生成自增方式插入   --缺少字段错误
db2 load CLIENT from D:\PRODUCTION_1.ixf of ixf  modified by identityoverride  replace into  db2admin.EMPLOYEE1( FIRSTNAME,LASTNAME,SALARY) NONRECOVERABLE



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325273489&siteId=291194637