《Mysql实例mysql alter table修改表命令整理》要点:
本文介绍了Mysql实例mysql alter table修改表命令整理,希望对您有用。如果有疑问,可以联系我们。
MYSQL入门MYSQL ALTER TABLE命令用于修改表结构,例如添加/修改/删除字段、索引、主键等等,本文章通过实例向大家介绍MYSQL ALTER TABLE语句的使用办法,
MYSQL入门 MySQL ALTER语法如下:
MYSQL入门
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
MYSQL入门 下面来看几个实例:
MYSQL入门1、向表employee中添加Account_Number字段并设置其字段类型为INT
MYSQL入门ALTER TABLE employee ADD COLUMN Account_Number INT
MYSQL入门2、修改表employee中的ID字段为索引
MYSQL入门ALTER TABLE employee ADD INDEX (ID)
MYSQL入门3、修改表employee中的ID字段为主键PRIMARY KEY
MYSQL入门ALTER TABLE employee ADD PRIMARY KEY (ID)
MYSQL入门4、修改表employee中的ID字段为唯一索引UNIQUE
MYSQL入门ALTER TABLE employee ADD UNIQUE (ID)
MYSQL入门5、将employee表中的id字段重命名为salary并设置其数据类型为int
MYSQL入门ALTER TABLE employee CHANGE ID salary INT
MYSQL入门6、删除employee表中的Customer_ID字段
MYSQL入门ALTER TABLE employee DROP Customer_ID
MYSQL入门7、删除employee表中所有主键
MYSQL入门ALTER TABLE employee DROP PRIMARY KEY
MYSQL入门8、删除employee表中字段Customer_ID的索引,只是将Customer_ID的索引取消,不会删除Customer_ID字段.
MYSQL入门ALTER TABLE employee DROP INDEX Customer_ID
MYSQL入门9、修改employee表中First_Name的字段类型为varchar(100)
MYSQL入门ALTER TABLE employee MODIFY First_Name varchar(100)
MYSQL入门10、将表employee重命名为Customer
MYSQL入门ALTER TABLE employee RENAME Customer
MYSQL入门11、多命令写在一起:
MYSQL入门
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;
MYSQL入门感谢阅读此文,希望能赞助到大家,谢谢大家对本站的支持!
欢迎参与《Mysql实例mysql alter table修改表命令整理》讨论,分享您的想法,维易PHP学院为您提供专业教程。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/9885.html