一、新增字段
1、查看表结构
mysql> desc office;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| city | varchar(50) | NO | | | |
| address | varchar(50) | YES | | NULL | |
| country | varchar(50) | NO | | | |
| postal_code | varchar(15) | YES | UNI | NULL | |
+-------------+-------------+------+-----+---------+----------------+
2、新增字段 create_time 记录创建时间
ALTER TABLE `office` ADD COLUMN create_time DATETIME COMMENT '记录创建时间';
3、修改表的字段名称 create_time 为 create_at
ALTER TABLE `office` CHANGE COLUMN `create_time` create_at DATETIME COMMENT '记录创建时间';
4、修改 create_at 字段属性为 int(11)
ALTER TABLE `office` MODIFY COLUMN `create_at` INT(11) NOT NULL DEFAULT 0 COMMENT '记录创建时间';
5、删除字段 create_at
ALTER TABLE `office` DROP COLUMN `create_at`;
6、修改表 office 名称为 offices
ALTER TABLE `office` RENAME TO offices;
查看表结构
1、DESCRIBE tablename 和 DESC tablename 一样。
DESCRIBE `office`;
DESC `office`;
mysql> DESCRIBE `office`;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| city | varchar(50) | NO | | | |
| address | varchar(50) | YES | | NULL | |
| country | varchar(50) | NO | | | |
| postal_code | varchar(15) | YES | UNI | NULL | |
+-------------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql>
2、SHOW CREATE TABLE `office`\G
mysql> SHOW CREATE TABLE `office`\G
*************************** 1. row ***************************
Table: office
Create Table: CREATE TABLE `office` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '办公室表',
`city` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '所在城市',
`address` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '地址',
`country` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '国家名称',
`postal_code` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '邮政编码',
PRIMARY KEY (`id`),
UNIQUE KEY `postal_code` (`postal_code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)
mysql>