用上文的user1表,查看表结构:
| id | smallint(5) unsigned | NO | PRI | NULL | auto
_increment |
| username | varchar(20) | NO | | NULL | |
| p_id | smallint(5) unsigned | YES | MUL | NULL | |
+----------+----------------------
+------+-----
+---------+----------------+
3 rows in set (0.00 sec)
添加一个默认值为10的非空字段
mysql> ALTER TABLE user1 ADD age SMALLINT UNSIGNED NOT NULL DEFAULT 10;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
| id | smallint(5) unsigned | NO | PRI | NULL | auto
_increment |
| username | varchar(20) | NO | | NULL | |
| p_id | smallint(5) unsigned | YES | MUL | NULL | |
4 rows in set (0.00 sec)
在某列后添加字段:AFTER(FIRST添加到最前面)
mysql> ALTER TABLE user1 ADD password VARCHAR(32) NOT NULL AFTER username;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
| id | smallint(5) unsigned | NO | PRI | NULL | auto
_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| p_id | smallint(5) unsigned | YES | MUL | NULL | |
5 rows in set (0.00 sec)
添加多列:
mysql> ALTER TABLE user1 ADD (aaa VARCHAR(32), bbb VARCHAR(32),ccc VARCHAR(32));
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
| sax | varchar(32) | NO | | NULL | |
| id | smallint(5) unsigned | NO | PRI | NULL | auto
_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| p_id | smallint(5) unsigned | YES | MUL | NULL | |
| age | smallint(5) unsigned | NO | | 10 | |
| aaa | varchar(32) | YES | | NULL | |
| bbb | varchar(32) | YES | | NULL | |
9 rows in set (0.00 sec)
删除列aaa:
mysql> ALTER TABLE user1 DROP aaa;
Query OK, 0 rows affected (0.45 sec)
Records: 0 Duplicates: 0 Warnings: 0
| sax | varchar(32) | NO | | NULL | |
| id | smallint(5) unsigned | NO | PRI | NULL | auto
_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| p_id | smallint(5) unsigned | YES | MUL | NULL | |
| age | smallint(5) unsigned | NO | | 10 | |
| bbb | varchar(32) | YES | | NULL | |
8 rows in set (0.00 sec)
同时删除两列:bbb,ccc
mysql> ALTER TABLE user1 DROP bbb,DROP ccc;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
| sax | varchar(32) | NO | | NULL | |
| id | smallint(5) unsigned | NO | PRI | NULL | auto
_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| p_id | smallint(5) unsigned | YES | MUL | NULL | |
6 rows in set (0.00 sec)
删除主键约束:(必须是没有自增长的主键,自增长要依附于主键删不了,key_test符合)
mysql> ALTER TABLE user1 DROP PRIMARY KEY;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
| id | smallint(5) unsigned | NO | PRI | NULL | |
2 rows in set (0.00 sec)
mysql> ALTER TABLE key
_test DROP PRIMARY KEY;
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
| id | smallint(5) unsigned | NO | | NULL | |
2 rows in set (0.00 sec)
删除唯一约束
| unique
_test | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| unique_test | 0 | username | 1 | username | A | 2 | NULL | NULL | YES | BTREE | | |
+-------------+------------
+----------+--------------
+-------------+-----------
+-------------+----------
+--------+------
+------------+---------
+---------------+
mysql> ALTER TABLE unique
_test DROP INDEX username;
Query OK, 0 rows affected (0.39 sec)
Records: 0 Duplicates: 0 Warnings: 0
1 row in set (0.00 sec)
删除外键约束:注意外键约束名:user1_ibfk_1
| Table | Create Table
| user1 | CREATE TABLE
`user1` (
`sax` varchar(32) NOT NULL,
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(32) NOT NULL,
`p_id` smallint(5) unsigned DEFAULT NULL,
`age` smallint(5) unsigned NOT NULL DEFAULT '10',
PRIMARY KEY (`id`),
KEY `p_id` (`p_id`),
CONSTRAINT `user1_ibfk_1` FOREIGN KEY (`p_id`) REFERENCES `province` (`id`) ON DELETE CASCADE
1 row in set (0.41 sec)
mysql> ALTER TABLE user1 DROP FOREIGN KEY user1
_ibfk_1;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
----------------------------------------------------------------------
| Table | Create Table
+-------+-------------------------------------------------------------
----------------------------------------------------------------------
| user1 | CREATE TABLE
`user1` (
`sax` varchar(32) NOT NULL,
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(32) NOT NULL,
`p_id` smallint(5) unsigned DEFAULT NULL,
`age` smallint(5) unsigned NOT NULL DEFAULT '10',
PRIMARY KEY (`id`),
KEY `p_id` (`p_id`)
----------------------------------------------------------------------
1 row in set (0.00 sec)
外键去除后,索引p_id就没有用了,也可以去除:
mysql> ALTER TABLE user1 DROP INDEX p
_id;
Query OK, 0 rows affected (0.39 sec)
Records: 0 Duplicates: 0 Warnings: 0
-----------------------------------------------------------------
| Table | Create Table
+-------+--------------------------------------------------------
-----------------------------------------------------------------
| user1 | CREATE TABLE
`user1` (
`sax` varchar(32) NOT NULL,
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(32) NOT NULL,
`p_id` smallint(5) unsigned DEFAULT NULL,
`age` smallint(5) unsigned NOT NULL DEFAULT '10',
PRIMARY KEY (`id`)
-----------------------------------------------------------------
1 row in set (0.00 sec)
修改列的位置:想把user1中的id调到最上面
| sax | varchar(32) | NO | | NULL | |
| id | smallint(5) unsigned | NO | PRI | NULL | auto
_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| p_id | smallint(5) unsigned | YES | | NULL | |
6 rows in set (0.00 sec)
mysql> ALTER TABLE user1 MODIFY id SMALLINT UNSIGNED NOT NULL FIRST;
Query OK, 2 rows affected (0.44 sec)
Records: 2 Duplicates: 0 Warnings: 0
| id | smallint(5) unsigned | NO | PRI | NULL | |
| sax | varchar(32) | NO | | NULL | |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| p
_id | smallint(5) unsigned | YES | | NULL | |
| age | smallint(5) unsigned | NO | | 10 | |
+----------+----------------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
修改列信息:将sax改为gender更好一点,再把长度改短点
mysql> ALTER TABLE user1 CHANGE sax gender VARCHAR(4) NOT NULL;
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
| id | smallint(5) unsigned | NO | PRI | NULL | |
| gender | varchar(4) | NO | | NULL | |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| p
_id | smallint(5) unsigned | YES | | NULL | |
| age | smallint(5) unsigned | NO | | 10 | |
+----------+----------------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
表名user1不抢眼,改个名字吧
mysql> ALTER TABLE user1 RENAME 阿姆斯特朗回旋加速喷气式阿姆斯特朗炮;
Query OK, 0 rows affected (0.01 sec)
| 阿姆斯特朗回旋加速喷气式阿姆斯特朗炮 |
| default_ |
| key
_test |
| little |
| null_test |
| province |
| unique
_id |
| unique_test |
9 rows in set (0.00 sec)
不行,太装X,用另一种方法改个低调的
mysql> RENAME TABLE 阿姆斯特朗回旋加速喷气式阿姆斯特朗炮 TO toly;
Query OK, 0 rows affected (0.01 sec)
| default_ |
| key
_test |
| little |
| null_test |
| province |
| toly |
| unique
_id |
| unique_test |
9 rows in set (0.00 sec)
转载于:https://www.cnblogs.com/toly-top/p/9782023.html