MySQL ALTER语法如下:

ALTER [IGNORE] TABLE tbl_name alter_spec [, alter_spec ...] 
alter_specification: 
ADD [COLUMN] create_definition [FIRST | AFTER column_name ] 
or ADD INDEX [index_name] (index_col_name,...) 
or ADD PRIMARY KEY (index_col_name,...) 
or ADD UNIQUE [index_name] (index_col_name,...) 
or ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 
or CHANGE [COLUMN] old_col_name create_definition 
or MODIFY [COLUMN] create_definition 
or DROP [COLUMN] col_name 
or DROP PRIMARY KEY 
or DROP INDEX index_name 
or RENAME [AS] new_tbl_name 
or table_options 
 

下面来看几个实例:

1、向表employee中添加Account_Number字段并设置其字段类型为INT

ALTER TABLE employee ADD COLUMN Account_Number INT

2、修改表employee中的ID字段为索引

ALTER TABLE employee ADD INDEX (ID) 

3、修改表employee中的ID字段为主键PRIMARY KEY

ALTER TABLE employee ADD PRIMARY KEY (ID)

4、修改表employee中的ID字段为唯一索引UNIQUE

ALTER TABLE employee ADD UNIQUE (ID)

5、将employee表中的id字段重命名为salary并设置其数据类型为int

ALTER TABLE employee CHANGE ID salary INT

6、删除employee表中的Customer_ID字段

ALTER TABLE employee DROP Customer_ID

7、删除employee表中所有主键

ALTER TABLE employee DROP PRIMARY KEY

8、删除employee表中字段Customer_ID的索引,只是将Customer_ID的索引取消,不会删除Customer_ID字段。

ALTER TABLE employee DROP INDEX Customer_ID

9、修改employee表中First_Name的字段类型为varchar(100)

ALTER TABLE employee MODIFY First_Name varchar(100) 

10、将表employee重命名为Customer

ALTER TABLE employee RENAME Customer 

11、多命令写在一起:

mysql> ALTER TABLE Books
  -> ADD PRIMARY KEY (BookID),
  -> ADD CONSTRAINT fk_1 FOREIGN KEY (PubID) REFERENCES Publishers (PubID),
  -> ADD COLUMN Format ENUM('paperback', 'hardcover') NOT NULL AFTER BookName;

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com