文章詳情頁(yè)
DB2 自動(dòng)遞增字段實(shí)現(xiàn)方法
瀏覽:263日期:2023-03-24 09:22:32
使用這個(gè)一般是用作識(shí)別碼的,當(dāng)做定義表格的主鍵。generated語(yǔ)法則可以自定義你想怎么產(chǎn)生這個(gè)值的策略。
語(yǔ)法如下:
column definition generated {always | by default}
as {identity identity rules | using your rules}
我們先刪掉上次我們建立的表格:
db2 => drop table nomination
然后再創(chuàng)建一個(gè)表格:
復(fù)制代碼 代碼如下:
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
)
注意黑體字,以后我們就不能使用insert或者update來顯式的指定它的值了。
而DB2中的identity也提供了多種策略,具體的可以去查DB2手冊(cè),我們舉例如下:
我們先刪掉上次我們建立的表格:
db2 => drop table category
然后建立表單
復(fù)制代碼 代碼如下:
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)
)
黑體字中identity中的語(yǔ)句你都能在DB2的手冊(cè)中查到,都是自然語(yǔ)言一看就懂了。
有時(shí)候你并不只想去做數(shù)字的填充,你可能還想處理一些字母,那么下邊這個(gè)轉(zhuǎn)換大寫的例子就是給你的:
db2 => alter table category add column
UpperCatName VARCHAR(50) generated always as (upper(CategoryName))
關(guān)于這些在DB2的文檔里都有具體說明。
語(yǔ)法如下:
column definition generated {always | by default}
as {identity identity rules | using your rules}
我們先刪掉上次我們建立的表格:
db2 => drop table nomination
然后再創(chuàng)建一個(gè)表格:
復(fù)制代碼 代碼如下:
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
)
注意黑體字,以后我們就不能使用insert或者update來顯式的指定它的值了。
而DB2中的identity也提供了多種策略,具體的可以去查DB2手冊(cè),我們舉例如下:
我們先刪掉上次我們建立的表格:
db2 => drop table category
然后建立表單
復(fù)制代碼 代碼如下:
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)
)
黑體字中identity中的語(yǔ)句你都能在DB2的手冊(cè)中查到,都是自然語(yǔ)言一看就懂了。
有時(shí)候你并不只想去做數(shù)字的填充,你可能還想處理一些字母,那么下邊這個(gè)轉(zhuǎn)換大寫的例子就是給你的:
db2 => alter table category add column
UpperCatName VARCHAR(50) generated always as (upper(CategoryName))
關(guān)于這些在DB2的文檔里都有具體說明。
標(biāo)簽:
DB2
相關(guān)文章:
1. 用 SQL 查詢 DB2 XML 數(shù)據(jù)(1)2. DB2數(shù)據(jù)庫(kù)為單個(gè)會(huì)話鎖定技巧3. 實(shí)例講解DB2數(shù)據(jù)庫(kù)中SELECT語(yǔ)句高級(jí)用法4. 選擇適合自己的DB2 9.5客戶機(jī)連通性選項(xiàng)5. 使用DB2look重新創(chuàng)建優(yōu)化器訪問計(jì)劃(8)6. 創(chuàng)建一個(gè)空的IBM DB2 ECO數(shù)據(jù)庫(kù)7. 應(yīng)用經(jīng)驗(yàn):關(guān)于IBM DB2數(shù)據(jù)庫(kù)的注意事項(xiàng)8. DB2 DBA避免性能災(zāi)難并獲得高性能的技巧9. DB2中創(chuàng)建一個(gè)漢字拼音首字母的SQL函數(shù)(1)10. 解決DB2數(shù)據(jù)庫(kù)備份參數(shù)修改后的報(bào)錯(cuò)問題
排行榜
