MySQL表结构修改
2015-11-26 15:34
603 查看
修改表的语法
=========================
增加列[add 列名]
=========================
①alter table 表名 add 列名 列类型 列参数【加的列在表的最后面】
例:alter table test add username char(20) not null default '';
alter table test add birth date not null default '0000-00-00';
②alter table 表名 add 列名 列类型 列参数 after 某列【把新列加在某列后面】
例:alter table test add gender char(1) not null default '' after username;
③alter table 表名 add 列名 列类型 列参数 first【把新列加在最前面】
例:alter table test add pid int not null default 0 first;
=========================
删除列[drop 列名]
=========================
①alter table 表名 drop 列名
例:alter table test drop pid;
=========================
修改列[modife 列名]
=========================
①alter table 表名 modify 列名 新类型 新参数【修改列类型】
例:alter table test modify gender char(4) not null default '';
②alter table 表名 change 旧列名 新列名 新类型 新参数【修改列名和列类型】
例:alter table test change pid uid int unsigned not null default 0;
=========================
查询列
=========================
①desc 表名【查询所有列】
例: desc test;
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId | int(11) | NO | PRI | | |
| dName | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
②show columns from 表名【效果和desc一样】
mysql> show columns from department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId | int(11) | NO | PRI | | |
| dName | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
③show create table 表名【查看表的创建代码】
mysql> show create table department;
CREATE TABLE `department` (
`dId` int(11) NOT NULL,
`dName` varchar(32) DEFAULT NULL,
PRIMARY KEY (`dId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
转载来自:http://www.cnblogs.com/qintangtao/archive/2012/11/17/2775209.html
=========================
增加列[add 列名]
=========================
①alter table 表名 add 列名 列类型 列参数【加的列在表的最后面】
例:alter table test add username char(20) not null default '';
alter table test add birth date not null default '0000-00-00';
②alter table 表名 add 列名 列类型 列参数 after 某列【把新列加在某列后面】
例:alter table test add gender char(1) not null default '' after username;
③alter table 表名 add 列名 列类型 列参数 first【把新列加在最前面】
例:alter table test add pid int not null default 0 first;
=========================
删除列[drop 列名]
=========================
①alter table 表名 drop 列名
例:alter table test drop pid;
=========================
修改列[modife 列名]
=========================
①alter table 表名 modify 列名 新类型 新参数【修改列类型】
例:alter table test modify gender char(4) not null default '';
②alter table 表名 change 旧列名 新列名 新类型 新参数【修改列名和列类型】
例:alter table test change pid uid int unsigned not null default 0;
=========================
查询列
=========================
①desc 表名【查询所有列】
例: desc test;
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId | int(11) | NO | PRI | | |
| dName | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
②show columns from 表名【效果和desc一样】
mysql> show columns from department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId | int(11) | NO | PRI | | |
| dName | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
③show create table 表名【查看表的创建代码】
mysql> show create table department;
CREATE TABLE `department` (
`dId` int(11) NOT NULL,
`dName` varchar(32) DEFAULT NULL,
PRIMARY KEY (`dId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
转载来自:http://www.cnblogs.com/qintangtao/archive/2012/11/17/2775209.html
相关文章推荐
- 编译安装Mysql 5.5.x
- 深入了解mysql
- mysql导入数据load data infile用法
- mysql索引、存储方式对性能的影响
- Mysql分表和分区的区别
- mysql自增
- 怎么查看和修改 MySQL 的最大连接数
- MySQL分区表
- Mysql常用命令
- mysql 回滚,撤销操作,恢复数据
- mysql -权限解决办法
- MySQL中UPDATE语句使用的实例教程
- 一次MySQL的优化之旅
- mysql 查看表结构
- MYSQL 奇怪的幻读问题!!!
- Mysql locate
- 新安装的MySQL必须调整的10项配置(转)
- Mysql的Debug模式实现
- window下免安装版mysql使用
- windows下安装mysql压缩包版[转]