msyql> create table customer_tbl ( id INT, name VARCHAR(20),age INT );
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW COLUMNS FROM customer_tbl;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
1.在表中添加一列
语法格式
ALTER TABLE table_name
ADD new_column_name column_definition
[ FIRST | AFTER column_name ];
参数说明:
示例代码
ALTER TABLE customer_tbl
ADD sex varchar(10) NOT NULL
AFTER name;
mysql> desc customer_tbl;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
2.在表中添加多列
语法格式
ALTER TABLE table_name
ADD new_column_name column_definition
[ FIRST | AFTER column_name ],
ADD new_column_name column_definition
[ FIRST | AFTER column_name ],
…
;
示例代码
ALTER TABLE customer_tbl
ADD address varchar(100) NOT NULL
AFTER name,
ADD salary int(100) NOT NULL
AFTER age ;
mysql> ALTER TABLE customer_tbl
-> ADD address varchar(100) NOT NULL
-> AFTER name,
-> ADD salary int(100) NOT NULL
-> AFTER age ;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc customer_tbl;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(100) | NO | | NULL | |
| sex | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | int(100) | NO | | NULL | |
+---------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
3.修改表中的列
语法格式
ALTER TABLE table_name
MODIFY column_name column_definition
[ FIRST | AFTER column_name ];
示例代码
ALTER TABLE customer_tbl
MODIFY name varchar(50) NULL;
mysql> ALTER TABLE customer_tbl
-> MODIFY name varchar(50) NULL;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc customer_tbl;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| address | varchar(100) | NO | | NULL | |
| sex | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | int(100) | NO | | NULL | |
+---------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> ALTER TABLE customer_tbl
-> MODIFY salary int(100) NOT NULL DEFAULT 1000;
mysql> ALTER TABLE customer_tbl
ALTER salary SET DEFAULT 1000;
mysql> ALTER TABLE customer_tbl
ALTER salary DROP DEFAULT;
mysql> ALTER TABLE customer_tbl ENGINE = MYISAM;
4.删除表中的列
语法格式
ALTER TABLE table_name
DROP COLUMN column_name;
示例代码
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE customer_tbl
DROP COLUMN address;
mysql> ALTER TABLE customer_tbl
-> DROP COLUMN address;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc customer_tbl;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | int(100) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
5.重命名表中的列
语法格式
ALTER TABLE table_name
CHANGE COLUMN old_name new_name
column_definition
[ FIRST | AFTER column_name ]
示例代码
ALTER TABLE customer_tbl
CHANGE COLUMN name title
varchar(20) NOT NULL;
mysql> ALTER TABLE customer_tbl
-> CHANGE COLUMN name title
-> varchar(20) NOT NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc customer_tbl;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| title | varchar(20) | NO | | NULL | |
| sex | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | int(100) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
6.重命名表
语法格式
ALTER TABLE TABLE_NAME
RENAME TO new_table_name;
示例代码
ALTER TABLE customer_tbl
RENAME TO customer_table;
mysql> ALTER TABLE customer_tbl
-> RENAME TO customer_table;
Query OK, 0 rows affected (0.00 sec)
mysql> desc customer_tbl;
ERROR 1146 (42S02): Table 'RUNOON.customer_tbl' doesn't exist
mysql> desc customer_table;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| title | varchar(20) | NO | | NULL | |
| sex | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | int(100) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
长按二维码,查阅更多教程
发表评论