1.修改表名:
alter table table_oldname rename [to] table_newname;
mysql> alter table example0 rename user;
Query OK, 0 rows affected (0.00 sec)mysql
2.修改字段數據類型:
alter table table_name modify attribute data_type;
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| stu_name | varchar(20) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)sql
mysql> alter table user modify stu_name varchar(30);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0ide
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| stu_name | varchar(30) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)it
3.修改字段名:
alter table table_name chane old_attribute new_attribute new_data_type;
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| stu_name | varchar(30) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)table
mysql> alter table user change stu_name name varchar(30);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0class
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)數據類型
增長字段
alter table table_name add attribute_1 data_thype [constraint] [first|after attribute_2]
在不指名位置的狀況下,默認字段增長在最後一列:
mysql> alter table user add age int(4) not null;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0數據
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)tab
在表的第一列增長字段:
mysql> alter table user add num int(8) first;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0di
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int(8) | YES | | NULL | |
| stu_id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
在表的指定位置後面增長字段:
mysql> alter table user add address varchar(30) not null after stu_id;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int(8) | YES | | NULL | |
| stu_id | int(11) | NO | PRI | NULL | |
| address | varchar(30) | NO | | NULL | |
| name | varchar(30) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
刪除字段
alter table table_name drop attribute;
mysql> alter table user drop address;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int(8) | YES | | NULL | |
| stu_id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
修改字段的排列位置
alter table table_name modify attribute_1 data_type first|after attribute_2
修改字段到第一個位置:
mysql> alter table user modify name varchar(30) first;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(30) | YES | | NULL | |
| num | int(8) | YES | | NULL | |
| stu_id | int(11) | NO | PRI | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
修改字段到指定位置:
mysql> alter table user modify sex boolean after name;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| num | int(8) | YES | | NULL | |
| stu_id | int(11) | NO | PRI | NULL | |
| age | int(4) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
更改表的存儲引擎 alter table table_name engine=engine_name;