1. 程式人生 > >Mysql DBA 高級運維學習筆記-增刪表字段更改表名刪除表實戰

Mysql DBA 高級運維學習筆記-增刪表字段更改表名刪除表實戰

inno varchar 添加 語句 alter dup mysq def 運維

9.12 增刪改表字段

9.12.1 命令語法及默認添加用演示

1.命令語法: alter table 表名 add 字段 類型 其他

2.測試表數據

mysql> show create table student\G
*************************** 1. row ***************************
   Table: student
Create Table: CREATE TABLE `student` (
  `id` int(4) NOT NULL,
  `name` char(20) NOT NULL,
  `age` tinyint(2) NOT NULL DEFAULT ‘0‘,
  `dept` varchar(16) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type| Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id| int(4)  | NO   | | NULL|   |
| name  | char(20)| NO   | | NULL|   |
| age   | tinyint(2)  | NO   | | 0   |   |
| dept  | varchar(16) | YES  | | NULL|   |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3.實踐

例如在表student中添加字段sex,qq類型分別為char(4),varchar(15)

技術分享圖片

4.執行的命令演示

a.添加性別列,默認語句

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type| Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id| int(4)  | NO   | | NULL|   |
| name  | char(20)| NO   | | NULL|   |
| age   | tinyint(2)  | NO   | | 0   |   |
| dept  | varchar(16) | YES  | | NULL|   |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table student add sex char(4);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type| Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id| int(4)  | NO   | | NULL|   |
| name  | char(20)| NO   | | NULL|   |
| age   | tinyint(2)  | NO   | | 0   |   |
| dept  | varchar(16) | YES  | | NULL|   |
| sex   | char(4) | YES  | | NULL|   |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

提示:默認添加列添加到所有字段結尾

9.12.2 指定添加列在表裏的位置

b.指定添加qq到name列的後面

mysql> alter table student add qq varchar(15) after name;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type| Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id| int(4)  | NO   | | NULL|   |
| name  | char(20)| NO   | | NULL|   |
| qq| varchar(15) | YES  | | NULL|   |
| age   | tinyint(2)  | NO   | | 0   |   |
| dept  | varchar(16) | YES  | | NULL|   |
| sex   | char(4) | YES  | | NULL|   |
+-------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

c.在第一列添加address列

mysql> alter table student add address char(20) first;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type| Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| address | char(20)| YES  | | NULL|   |
| id  | int(4)  | NO   | | NULL|   |
| name| char(20)| NO   | | NULL|   |
| qq  | varchar(15) | YES  | | NULL|   |
| age | tinyint(2)  | NO   | | 0   |   |
| dept| varchar(16) | YES  | | NULL|   |
| sex | char(4) | YES  | | NULL|   |
+---------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

9.13 更改表名

9.13.1 rename法

1.命令語法:rename table 原表名 to 新表名;

列如:表student名字更改為user

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

mysql> rename table student to user;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+---------------+
| Tables_in_zbf |
+---------------+
| user  |
+---------------+
1 row in set (0.01 sec)

9.13.2 Alter方法

mysql> alter table user rename to student;
Query OK, 0 rows affected (0.01 sec)

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

9.14刪除表

命令語法:drop table<表名>

例如:刪除表名為student的表

mysql> drop table student;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
Empty set (0.01 sec)

Mysql DBA 高級運維學習筆記-增刪表字段更改表名刪除表實戰