Mysql基础之 ALTER命令

ALTER命令

  作用:当我们修改数据库的列属性、列名称、表名等,要使用ALTER命令

  教程:

  1、首先是我们创建一个数据库以及一张表

mysql> create table exercise(
    -> id int,
    -> name varchar(15)
    -> );

    1.1、查看表结构

mysql> SHOW COLUMnS FROM exercise;  或者使用  DESC erercise;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(15) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

  删除、添加、修改表字段

  1、删除一个字段,比如id

mysql> ALTER TABLE exercise drop id;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESC exercise;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(15) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

  2、添加字段

    2.1、添加一个字段,比如id_new

mysql> ALTER TABLE exercise ADD COLUMN id_new int;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESC exercise;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name   | varchar(15) | YES  |     | NULL    |       |
| id_new | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

    2.2、将新添加的字段设置为第一列

mysql> ALTER TABLE exercise ADD COLUMN sex varchar(1) FIRST;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESC exercise;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sex    | varchar(1)  | YES  |     | NULL    |       |
| name   | varchar(15) | YES  |     | NULL    |       |
| id_new | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

 

    2.3、在某个列之后添加新的字段,比如在name列后添加

mysql> ALTER TABLE exercise ADD COLUMN address char(50) AFTER name;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESC exercise;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sex     | varchar(1)  | YES  |     | NULL    |       |
| name    | varchar(15) | YES  |     | NULL    |       |
| address | char(50)    | YES  |     | NULL    |       |
| id_new  | int(11)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

  总结:添加字段,只有FIRST和AFTER关键字两个

  3、修改一个字段属性及名称

    3.1、修改列属性

      使用modify修改列属性

mysql> ALTER TABLE exercise modify id_new int(4);  #将原来的int(11)修改为int(4)
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESC exercise;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name   | varchar(15) | YES  |     | NULL    |       |
| id_new | int(4)      | YES  |     | NULL    |       | 
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

      使用change修改列属性

mysql> ALTER TABLE exercise CHANGE id_new id_new int(8);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESC exercise;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name   | varchar(15) | YES  |     | NULL    |       |
| id_new | int(8)      | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

    总结:modify和change的区别:修改列属性时,使用change比较麻烦,要写两次列名称,而使用modify只需要写一次列名称就可以了。

    3.2、修改列名字

mysql> ALTER TABLE exercise CHANGE id_new id_new_new;
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 exercise CHANGE id_new id_new_new int(8);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC exercise;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
 +------------+-------------+------+-----+---------+-------+
 | name | varchar(15) | YES | | NULL | |
 | id_new_new | int(8) | YES | | NULL | |
 +------------+-------------+------+-----+---------+-------+
 2 rows in set (0.00 sec)

    总结:修改列名称需要使用CHANGE,而MODIFY不能修改列的名称。如果要同时修改名称和属性还是CHANGE。使用CHANGE后面要跟上旧列、新列的名称,以及列属性。因此修改名称前先查看列属性。

    一句话:字段名称CHANGE;字段属性MODIFY。即名称又属性还是CHANGE

  4、修改表名称

mysql> ALTER TABLE exercise rename exercise_new;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| exercise_new   |
+----------------+
1 row in set (0.00 sec)

  5、ALTER 修改字段的默认值

  我们往往在建表的时候给某个列的属性上面设置默认值,比如“DEFAULT NULL”等等。如何修改默认值呢?首先来查看所有列的默认值

mysql> SHOW COLUMNS FROM exercise;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sex     | varchar(1)  | YES  |     | NULL    |       |
| name    | varchar(15) | YES  |     | NULL    |       |
| address | char(50)    | YES  |     | NULL    |       |
| id_new  | int(11)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

  基本所有的列默认值DEFAULT都是NULL,现在我们指定“sex”列默认值是“M”

mysql> ALTER TABLE exercise ALTER sex SET DEFAULT 'M';
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

  查看一下:

mysql> SHOW COLUMNS FROM exercise;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sex     | varchar(1)  | YES  |     | M       |       |
| name    | varchar(15) | YES  |     | NULL    |       |
| address | char(50)    | YES  |     | NULL    |       |
| id_new  | int(11)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

   尝试删除表中一个列的默认值

mysql> ALTER TABLE exercise ALTER sex drop DEFAULT;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

   再来查看一下:

mysql> SHOW COLUMNS FROM exercise;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sex     | varchar(1)  | YES  |     | NULL    |       |
| name    | varchar(15) | YES  |     | NULL    |       |
| address | char(50)    | YES  |     | NULL    |       |
| id_new  | int(11)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
posted @ 2018-10-11 14:40  峰哥ge  阅读(3332)  评论(0编辑  收藏  举报