DB2列的修改及限制
DB2列的修改方法未必人人都會(huì),下面對(duì)DB2列的修改及限制作出詳細(xì)的介紹,如果您對(duì)DB2列的修改方法感興趣的話,不妨一看。
1.修改長(zhǎng)度
alter table tab_name alter c1 set data type varchar(20)
2.設(shè)置為非空
alter table tab_name alter c1 set not null
create table tab_name (id interger not null)
3.設(shè)置默認(rèn)值
create table tab_name (id integer,name varchar(10) with default 'none')
4.為列創(chuàng)建序列
使用關(guān)鍵字 generated always as identity
create table tab_name (id integer generated always as identity (start with 1,increment by 1))
5.唯一性約束
使用關(guān)鍵字primary key
create table tab_name (id int not null primary key)
使用關(guān)鍵字unique
alter table tab_name add constraint unique (id)
create unique index idx_name on tab_name (id)
6.檢查性約束
使用關(guān)鍵字check
ALTER TABLE BOOKS ADD BOOKTYPE CHAR(1) CHECK (BOOKTYPE IN ('F','N') )
7.參照約束
使用關(guān)鍵字references
CREATE TABLE AUTHORS (AUTHORID INTEGER NOT NULL PRIMARY KEY,
LNAME VARCHAR(100),
FNAME VARCHAR(100))
CREATE TABLE BOOKS (BOOKID INTEGER NOT NULL PRIMARY KEY,
BOOKNAME VARCHAR(100),
ISBN CHAR(10),
AUTHORID INTEGER REFERENCES AUTHORS)
【編輯推薦】