mysql alter table 修改表命令详细介绍
MySQLALTER语法如下:
ALTER[IGNORE]TABLEtbl_namealter_spec[,alter_spec...] alter_specification: ADD[COLUMN]create_definition[FIRST|AFTERcolumn_name] orADDINDEX[index_name](index_col_name,...) orADDPRIMARYKEY(index_col_name,...) orADDUNIQUE[index_name](index_col_name,...) orALTER[COLUMN]col_name{SETDEFAULTliteral|DROPDEFAULT} orCHANGE[COLUMN]old_col_namecreate_definition orMODIFY[COLUMN]create_definition orDROP[COLUMN]col_name orDROPPRIMARYKEY orDROPINDEXindex_name orRENAME[AS]new_tbl_name ortable_options
下面来看几个实例:
1、向表employee中添加Account_Number字段并设置其字段类型为INT
ALTERTABLEemployeeADDCOLUMNAccount_NumberINT
2、修改表employee中的ID字段为索引
ALTERTABLEemployeeADDINDEX(ID)
3、修改表employee中的ID字段为主键PRIMARYKEY
ALTERTABLEemployeeADDPRIMARYKEY(ID)
4、修改表employee中的ID字段为唯一索引UNIQUE
ALTERTABLEemployeeADDUNIQUE(ID)
5、将employee表中的id字段重命名为salary并设置其数据类型为int
ALTERTABLEemployeeCHANGEIDsalaryINT
6、删除employee表中的Customer_ID字段
ALTERTABLEemployeeDROPCustomer_ID
7、删除employee表中所有主键
ALTERTABLEemployeeDROPPRIMARYKEY
8、删除employee表中字段Customer_ID的索引,只是将Customer_ID的索引取消,不会删除Customer_ID字段。
ALTERTABLEemployeeDROPINDEXCustomer_ID
9、修改employee表中First_Name的字段类型为varchar(100)
ALTERTABLEemployeeMODIFYFirst_Namevarchar(100)
10、将表employee重命名为Customer
ALTERTABLEemployeeRENAMECustomer
11、多命令写在一起:
mysql>ALTERTABLEBooks ->ADDPRIMARYKEY(BookID), ->ADDCONSTRAINTfk_1FOREIGNKEY(PubID)REFERENCESPublishers(PubID), ->ADDCOLUMNFormatENUM('paperback','hardcover')NOTNULLAFTERBookName;
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!