MySql表结构修改

来源:互联网 发布:亿玛创新网络 编辑:程序博客网 时间:2024/06/08 22:51

1.增加列【add 列名】

(1)在表的最后追加一列:

语法:alter table 表名 add 列名 列类型 列参数 ;
例如:alter table test add name char(30)not null default ‘Tom’;

(2)在某列后面加一列

语法:alter table 表名 add 列名 列类型 列参数 after 某列;
例子:alter table test add age int(4)not null after name;

(3)把某一列加在最前面

语法:alter table 表名 add 列名 列类型 列参数 first;
例子:alter table test add ID int(11) not null first;

**

2.删除列【drop 列名】

语法:alter table 表名 drop 列名;
例子:alter table test drop name;

3.修改列 【modify 列名】

(1)修改列类型

语法:alter table 表名 modify 列名 新类型 新参数;
例子:alter table test modify ID tinyint not null;

(2)修改列名和列类型

语法:alter table 表名 change旧列名 新列名 新参数;
例子:alter table test change name username char(20) not null;

4.查询列

(1)查询所有列(其实就是表结构)

语法:desc 表名;
例子:desc test;

语法:show columns from 表名;
例子:show columns from test;

(2)查询创建表的语句

语法:show create table 表名;
例子:show create table test;

原创粉丝点击