mysql alter table 修改表命令详细介绍
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;
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Android 自定义可拖拽View界面渲染刷新后不会自动回到起始位置
下一篇: Laravel框架运行出错提示RuntimeException No application encryption key has been specified.解决方法
推荐阅读