2 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
ALTER TABLE table
{ [ ALTER COLUMN column_name
{ new_data_type [ ( precision [ , scale ] ) ]
[ COLLATE < collation_name > ]
[ NULL | NOT NULL ]
| {ADD | DROP } ROWGUIDCOL }
]
| ADD
{ [ < column_definition > ]
| column_name AS computed_column_expression
} [ ,...n ]
| [ WITH CHECK | WITH NOCHECK ] ADD
{ < table_constraint > } [ ,...n ]
| DROP
{ [ CONSTRAINT ] constraint_name
| COLUMN column } [ ,...n ]
| { CHECK | NOCHECK } CONSTRAINT
{ ALL | constraint_name [ ,...n ] }
| { ENABLE | DISABLE } TRIGGER
{ ALL | trigger_name [ ,...n ] }
}
< column_definition > ::=
{ column_name data_type }
[ [ DEFAULT constant_expression ] [ WITH VALUES ]
| [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ]
]
[ ROWGUIDCOL ]
[ COLLATE < collation_name > ]
[ < column_constraint > ] [ ...n ]
< column_constraint > ::=
[ CONSTRAINT constraint_name ]
{ [ NULL | NOT NULL ]
| [ { PRIMARY KEY | UNIQUE }
[ CLUSTERED | NONCLUSTERED ]
[ WITH FILLFACTOR = fillfactor ]
[ ON { filegroup | DEFAULT } ]
]
| [ [ FOREIGN KEY ]
REFERENCES ref_table [ ( ref_column ) ]
[ ON DELETE { CASCADE | NO ACTION } ]
[ ON UPDATE { CASCADE | NO ACTION } ]
[ NOT FOR REPLICATION ]
]
| CHECK [ NOT FOR REPLICATION ]
( logical_expression )
}
< table_constraint > ::=
[ CONSTRAINT constraint_name ]
{ [ { PRIMARY KEY | UNIQUE }
[ CLUSTERED | NONCLUSTERED ]
{ ( column [ ,...n ] ) }
[ WITH FILLFACTOR = fillfactor ]
[ ON { filegroup | DEFAULT } ]
]
| FOREIGN KEY
[ ( column [ ,...n ] ) ]
REFERENCES ref_table [ ( ref_column [ ,...n ] ) ]
[ ON DELETE { CASCADE | NO ACTION } ]
[ ON UPDATE { CASCADE | NO ACTION } ]
[ NOT FOR REPLICATION ]
| DEFAULT constant_expression
[ FOR column ] [ WITH VALUES ]
| CHECK [ NOT FOR REPLICATION ]
( search_conditions )
}

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
在表格被建立在資料庫(kù)中后,我們常常會(huì)發(fā)現(xiàn),這個(gè)表格的結(jié)構(gòu)需要有所改變。常見的改變?nèi)缦拢?br/>? 加一個(gè)欄位
? 刪去一個(gè)欄位
? 改變欄位名稱
? 改變欄位的資料種類
以上列出的改變并不是所有可能的改變。ALTER TABLE 也可以被用來作其他的改變,例如改變主鍵定義。ALTER TABLE 的語法如下:
ALTER TABLE "table_name" [改變方式]
[改變方式] 的詳細(xì)寫法會(huì)依我們想要達(dá)到的目標(biāo)而有所不同。再以上列出的改變中,[改變方式] 如下:
? 加一個(gè)欄位: ADD "欄位 1" "欄位 1 資料種類"
? 刪去一個(gè)欄位: DROP "欄位 1"
? 改變欄位名稱: CHANGE "原本欄位名" "新欄位名" "新欄位名資料種類"
? 改變欄位的資料種類: MODIFY "欄位 1" "新資料種類"
例如,往表中加入一列:
ALTER table customer add Gender char(1)
下面是把表中列Address改為Addr
ALTER table customer change Address Addr char(50)
下面是把上面那個(gè)Addr的類型變?yōu)閏har(30)
ALTER table customer modify Addr char(30)
這是刪除一列
ALTER table customer drop Gender
- 2 回答
- 0 關(guān)注
- 388 瀏覽
添加回答
舉報(bào)