2 回答
TA贡献1824条经验 获得超5个赞
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贡献1856条经验 获得超17个赞
在表格被建立在资料库中后,我们常常会发现,这个表格的结构需要有所改变。常见的改变如下:
• 加一个栏位
• 删去一个栏位
• 改变栏位名称
• 改变栏位的资料种类
以上列出的改变并不是所有可能的改变。ALTER TABLE 也可以被用来作其他的改变,例如改变主键定义。ALTER TABLE 的语法如下:
ALTER TABLE "table_name" [改变方式]
[改变方式] 的详细写法会依我们想要达到的目标而有所不同。再以上列出的改变中,[改变方式] 如下:
• 加一个栏位: ADD "栏位 1" "栏位 1 资料种类"
• 删去一个栏位: DROP "栏位 1"
• 改变栏位名称: CHANGE "原本栏位名" "新栏位名" "新栏位名资料种类"
• 改变栏位的资料种类: MODIFY "栏位 1" "新资料种类"
例如,往表中加入一列:
ALTER table customer add Gender char(1)
下面是把表中列Address改为Addr
ALTER table customer change Address Addr char(50)
下面是把上面那个Addr的类型变为char(30)
ALTER table customer modify Addr char(30)
这是删除一列
ALTER table customer drop Gender
- 2 回答
- 0 关注
- 330 浏览
添加回答
举报