DB2自增長列導入、導出測試
DB2 自增長列測試
1當想將表中一列修改為自動增長時,可用下面命令:
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)
上面命令是在改一表中列的屬性時,在網上找到的很有用。
www.2cto.com
2當修改表中一列自動增長的開始值時,可用下面的命令:
ALTER TABLE <talbe_name> ALTER COLUMN <column name> RESTART WITH 18;
測試:
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))
注:該列中的以及它本身的 IDENTITY 屬性並沒有保證所生成的序列值是唯一的。
但是, PRIMARY KEY 約束保證了表中行的唯一性。
為了確保只將自動生成的值插入標識列,他們指定了 GENERATED ALWAYS 子句。
使用最後一個生成的 order_id 來確定多少數據
選項 NO CACHE 和 ORDER 確保了在系統故障的情況下,不廢棄未使用的標識值。
www.2cto.com
測試1
插入數據
insert into customer_orders_t values (default,current date,12,12,12,10.2,'2')
--成功
insert into customer_orders_t values (1,current date,12,12,12,10.2,'2')
-- 報錯 因為:IDENTITY字段不允許指定值
--解決方案
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')
www.2cto.com
1、命令行取sequence soc.nico_qian的下一個值:
db2 "values next value for soc.nico_qian"
2、命令行重置sequence soc.nico_qian:
db2 "alter sequence soc.nico_qian restart",重置後的值默認為創建SEQUENCE時的MINVALUE
3、命令行以指定值22重置sequence soc.nico_qian:
db2 "alter sequence soc.nico_qian restart with 22"
4、命令行重置表KS.CHECK_CONDITION的IDENTITY字段初始值為20:
db2 "ALTER TABLE KS.CHECK_CONDITION ALTER COLUMN identity_column_name RESTART WITH 20"
5、如果sequence被以命令行的方式重置,那麼用到這個sequence的嵌入式C程序代碼的綁定包
的VALID字段會被修改為N,那麼在下一次這個代碼被調用的時候,DB2會自動重新綁定
此代碼的綁定包,這個動作會給應用程序帶來不可預知的後果,比如:如果這段代碼是在很
頻繁的被用到的時間段內被重新綁定,那麼極易造成死鎖。
同樣的問題會出現在IDENTITY字段上。
www.2cto.com
DB2自增加字段表 導入導出 測試
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))
www.2cto.com
select * from EMPLOYEE;
select * from EMPLOYEE1;
identityignore 忽略自增identitymissing 自動生成自增identityoverride 使用自增
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 全量導出
db2 export to D:\PRODUCTION.ixf of ixf select * from db2admin.EMPLOYEE
--直接插入 報錯 --不允許插入
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 忽略源自增字段 方式插入 --插入正常
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 自動生成目標自增字段方式插入 --插入錯位
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 部分導出
db2 export to D:\PRODUCTION_1.ixf of ixf select FIRSTNAME,LASTNAME,SALARY from db2admin.EMPLOYEE
--部分直接插入 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)
--部分忽略源自增字段直接插入 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 自動生成目標自增字段方式插入 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