Mysql DBA 進階營運學習筆記-增刪表欄位更改表名刪除表實戰

來源:互聯網
上載者:User

標籤:utf8   位置   des   tables   營運學習   alter   ant   nta   empty   

9.12 增刪改表欄位

9.12.1 命令文法及預設添加用示範

1.命令文法: alter table 表名 add 欄位 類型 其他

2.測試表資料

mysql> show create table student\G*************************** 1. row ***************************   Table: studentCreate 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=utf81 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: 0mysql> 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: 0mysql> 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: 0mysql> 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 進階營運學習筆記-增刪表欄位更改表名刪除表實戰

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.