[mysql] 常用命令四

① 查看表的创建细节

 mysql>show create table employee;

 user  | CREATE TABLE `user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(30) NOT NULL,
 `password` varchar(32) NOT NULL,
 `age` int(11) NOT NULL,
 `sex` varchar(2) DEFAULT '男'?,
 `birthday` date DEFAULT NULL,
 PRIMARY KEY (`id`)
 ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=gbk

② 表名的修改
mysql> rename table userclob to userclob1;
Query OK, 0 rows affected (0.15 sec)

③ 在表现有列的基础上增加一列。
alter table 表名add 列名 字段数据类型;

mysql> alter table userclob add image blob;
Query OK, 7 rows affected (0.27 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> desc userclob;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(30) | NO   |     | NULL    |                |
| note  | longtext    | YES  |     | NULL    |                |
| image | blob        | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

④ 修改name列,使其长度为60。

mysql> alter table userclob modify name varchar(60);
Query OK, 7 rows affected (0.42 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> desc userclob;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(60) | YES  |     | NULL    |                |
| note  | longtext    | YES  |     | NULL    |                |
| image | blob        | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

⑤ 删除image列
mysql> alter table userclob drop image;
Query OK, 7 rows affected (0.30 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> desc userclob;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(60) | YES  |     | NULL    |                |
| note  | longtext    | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

⑥ 列名的修改
mysql> desc userclob;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(60) | YES  |     | NULL    |                |
| note  | longtext    | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> alter table userclob change id userid int;
Query OK, 7 rows affected (0.30 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> desc userclob;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| userid | int(11)     | NO   | PRI | 0       |       |
| name   | varchar(60) | YES  |     | NULL    |       |
| note   | longtext    | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

⑦ 使用truncate删除表中记录。
truncate table employee;

posted @ 2012-11-16 16:21  Aviva_ye  阅读(148)  评论(0编辑  收藏  举报