DB2 自動遞增字段實現(xiàn)方法
更新時間:2009年11月30日 20:27:23 作者:
DB2提供了當有一行插入的時候自動在某一列添加值的功能,可以使用所謂identity rules,簡單點的比如某個數(shù)值的遞增填入該列中,當然也有很復(fù)雜的。
使用這個一般是用作識別碼的,當做定義表格的主鍵。generated語法則可以自定義你想怎么產(chǎn)生這個值的策略。
語法如下:
column definition generated {always | by default}
as {identity identity rules | using your rules}
我們先刪掉上次我們建立的表格:
db2 => drop table nomination
然后再創(chuàng)建一個表格:
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手冊,我們舉例如下:
我們先刪掉上次我們建立的表格:
db2 => drop table category
然后建立表單
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中的語句你都能在DB2的手冊中查到,都是自然語言一看就懂了。
有時候你并不只想去做數(shù)字的填充,你可能還想處理一些字母,那么下邊這個轉(zhuǎn)換大寫的例子就是給你的:
db2 => alter table category add column
UpperCatName VARCHAR(50) generated always as (upper(CategoryName))
關(guān)于這些在DB2的文檔里都有具體說明。
語法如下:
column definition generated {always | by default}
as {identity identity rules | using your rules}
我們先刪掉上次我們建立的表格:
db2 => drop table nomination
然后再創(chuàng)建一個表格:
復(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手冊,我們舉例如下:
我們先刪掉上次我們建立的表格:
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中的語句你都能在DB2的手冊中查到,都是自然語言一看就懂了。
有時候你并不只想去做數(shù)字的填充,你可能還想處理一些字母,那么下邊這個轉(zhuǎn)換大寫的例子就是給你的:
db2 => alter table category add column
UpperCatName VARCHAR(50) generated always as (upper(CategoryName))
關(guān)于這些在DB2的文檔里都有具體說明。
您可能感興趣的文章:
- DB2 常用命令小結(jié)
- DB2比較常用與實用sql語句總結(jié)
- db2 導(dǎo)入導(dǎo)出單個表的操作詳解
- DB2 日期和時間的函數(shù)應(yīng)用說明
- DB2 數(shù)據(jù)庫創(chuàng)建、表的ixf文件導(dǎo)出導(dǎo)入示例
- DB2 常用命令速查(備忘)
- DB2 SELECT語句高級用法
- DB2如何查看當前用戶模式及切換用戶
- CentOS下DB2數(shù)據(jù)庫安裝過程詳解
- DB2新手使用的一些小筆記:新建實例、數(shù)據(jù)庫路徑不存在、客戶端連接 .
- 比較SQL Server與Oracle、DB2
- DB2數(shù)據(jù)庫的備份和恢復(fù)
- Python連接DB2數(shù)據(jù)庫
相關(guān)文章
常見數(shù)據(jù)庫系統(tǒng)比較 DB2數(shù)據(jù)庫
常見數(shù)據(jù)庫系統(tǒng)比較 DB2數(shù)據(jù)庫...2007-03-03解決db2事務(wù)日志已滿及日志磁盤空間已滿問題辦法詳解
本文主要講解了解決db2事務(wù)日志已滿及日志磁盤空間已滿的問題,DB2總的可用活動日志的最大空間是有限制的,當達到限制之后,就會發(fā)生日志滿的問題2018-03-03