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)
上面命令是在改一表中列的屬性時,在網上找到的很有用。
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 確保了在系統故障的情況下,不廢棄未使用的標識值。
測試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')
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欄位上。
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))
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