mysql 列的删除增加与修改

语法:
alter table 表名 add 新列名 列类型 列属性....默认在最后面
alter table 表名 add 新列名 列类型 列属性....after 指定列名(将会出现在指定列的后面)
alter table 表名 drop column 列名 删除某列
alter table 表名 change 原列名   新列名 新属性 (修改表名和属性)
alter table 表名 modify 列名 属性(只能修改列的属性,不能修改列的名称)
rename table 原表名 to 新表名(给表重命名)
drop table 表名 (删除某张表)

#查看boy表中的数据.
mysql> select * from boy;
+-----+--------+
| hid | bname  |
+-----+--------+
|   1 | lisi   |
|   2 | 王五   |
|   3 | 赵六   |
|   5 | 海燕   |
+-----+--------+
4 rows in set (0.00 sec)

#在表boy中添加新的列“height”.
mysql> alter table boy add height tinyint unsigned not null default 0;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc boy;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| hid    | int(11)             | NO   |     | NULL    |       |
| bname  | varchar(100)        | NO   |     | NULL    |       |
| height | tinyint(3) unsigned | NO   |     | 0       |       |
+--------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

#在表boy中添加新的列“birth”.
mysql> alter table boy add birth date not null default 0;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc boy;
+--------+---------------------+------+-----+------------+-------+
| Field  | Type                | Null | Key | Default    | Extra |
+--------+---------------------+------+-----+------------+-------+
| hid    | int(11)             | NO   |     | NULL       |       |
| bname  | varchar(100)        | NO   |     | NULL       |       |
| height | tinyint(3) unsigned | NO   |     | 0          |       |
| birth  | date                | NO   |     | 0000-00-00 |       |
+--------+---------------------+------+-----+------------+-------+
4 rows in set (0.00 sec)

#删除"birth"列.
mysql> alter table boy drop column birth;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

#查看表结构.
mysql> desc boy;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| hid    | int(11)             | NO   |     | NULL    |       |
| bname  | varchar(100)        | NO   |     | NULL    |       |
| height | tinyint(3) unsigned | NO   |     | 0       |       |
+--------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

#新增的列"birth"在列bname的后面,指定在某些列的后面.
mysql> alter table boy add birth date not null default 0 after bname;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc boy;
+--------+---------------------+------+-----+------------+-------+
| Field  | Type                | Null | Key | Default    | Extra |
+--------+---------------------+------+-----+------------+-------+
| hid    | int(11)             | NO   |     | NULL       |       |
| bname  | varchar(100)        | NO   |     | NULL       |       |
| birth  | date                | NO   |     | 0000-00-00 |       |
| height | tinyint(3) unsigned | NO   |     | 0          |       |
+--------+---------------------+------+-----+------------+-------+
4 rows in set (0.00 sec)

#修改表中某一列的属性或者名称.
mysql> alter table boy change height tizhong smallint;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc boy;
+---------+--------------+------+-----+------------+-------+
| Field   | Type         | Null | Key | Default    | Extra |
+---------+--------------+------+-----+------------+-------+
| hid     | int(11)      | NO   |     | NULL       |       |
| bname   | varchar(100) | NO   |     | NULL       |       |
| birth   | date         | NO   |     | 0000-00-00 |       |
| tizhong | smallint(6)  | YES  |     | NULL       |       |
+---------+--------------+------+-----+------------+-------+
4 rows in set (0.00 sec)

#使用change只修改列的名称,发现后面不接属性不行,会报错.
mysql> alter table boy change tizhong weihgt;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> alter table boy change tizhong weihgt tinyint;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

#使用change只修改表名,后面属性照原来的写即可.
mysql> desc boy;
+--------+--------------+------+-----+------------+-------+
| Field  | Type         | Null | Key | Default    | Extra |
+--------+--------------+------+-----+------------+-------+
| hid    | int(11)      | NO   |     | NULL       |       |
| bname  | varchar(100) | NO   |     | NULL       |       |
| birth  | date         | NO   |     | 0000-00-00 |       |
| weihgt | tinyint(4)   | YES  |     | NULL       |       |
+--------+--------------+------+-----+------------+-------+
4 rows in set (0.00 sec)
mysql> alter table boy change weihgt weight tinyint;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc boy;
+--------+--------------+------+-----+------------+-------+
| Field  | Type         | Null | Key | Default    | Extra |
+--------+--------------+------+-----+------------+-------+
| hid    | int(11)      | NO   |     | NULL       |       |
| bname  | varchar(100) | NO   |     | NULL       |       |
| birth  | date         | NO   |     | 0000-00-00 |       |
| weight | tinyint(4)   | YES  |     | NULL       |       |
+--------+--------------+------+-----+------------+-------+
4 rows in set (0.00 sec)

#用change只是修改列的属性,列名写相同的即可,属性改为新的属性.
mysql> desc boy;
+--------+--------------+------+-----+------------+-------+
| Field  | Type         | Null | Key | Default    | Extra |
+--------+--------------+------+-----+------------+-------+
| hid    | int(11)      | NO   |     | NULL       |       |
| bname  | varchar(100) | NO   |     | NULL       |       |
| birth  | date         | NO   |     | 0000-00-00 |       |
| weight | tinyint(4)   | YES  |     | NULL       |       |
+--------+--------------+------+-----+------------+-------+
4 rows in set (0.00 sec)
mysql> alter table boy change weight weight smallint;
Query OK, 4 rows affected (0.07 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc boy;
+--------+--------------+------+-----+------------+-------+
| Field  | Type         | Null | Key | Default    | Extra |
+--------+--------------+------+-----+------------+-------+
| hid    | int(11)      | NO   |     | NULL       |       |
| bname  | varchar(100) | NO   |     | NULL       |       |
| birth  | date         | NO   |     | 0000-00-00 |       |
| weight | smallint(6)  | YES  |     | NULL       |       |
+--------+--------------+------+-----+------------+-------+
4 rows in set (0.00 sec)

#只修改列的属性,列名相同不写可以不?发现不写会报错,必须相同.
mysql> alter table boy change weight tinyint;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tinyint' at line 1

#使用modify也是可以修改列的属性的,但是不能修改列名.
mysql> alter table boy modify tizhong tinyint;
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc boy;
+---------+--------------+------+-----+------------+-------+
| Field   | Type         | Null | Key | Default    | Extra |
+---------+--------------+------+-----+------------+-------+
| hid     | int(11)      | NO   |     | NULL       |       |
| bname   | varchar(100) | NO   |     | NULL       |       |
| birth   | date         | NO   |     | 0000-00-00 |       |
| tizhong | tinyint(4)   | YES  |     | NULL       |       |
+---------+--------------+------+-----+------------+-------+
4 rows in set (0.00 sec)

#修改表名rename....to..
mysql> rename table girl to tgirl;
Query OK, 0 rows affected (0.29 sec)
#查看表,已经修改为tgirl。
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| boy            |
| course         |
| sc             |
| student        |
| t1             |
| t10            |
| t11            |
| t12            |
| t13            |
| t14            |
| t15            |
| t2             |
| t3             |
| t4             |
| t5             |
| t6             |
| t7             |
| t8             |
| t9             |
| tgirl          |
+----------------+
20 rows in set (0.00 sec)

#直接删除某张表.(drop table 表名)
mysql> drop table t1;
Query OK, 0 rows affected (0.02 sec)
#查看表t1已经不存在.
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| boy            |
| course         |
| sc             |
| student        |
| t10            |
| t11            |
| t12            |
| t13            |
| t14            |
| t15            |
| t2             |
| t3             |
| t4             |
| t5             |
| t6             |
| t7             |
| t8             |
| t9             |
| tgirl          |
+----------------+
19 rows in set (0.00 sec)


猜你喜欢

转载自blog.51cto.com/215687833/2350945