码迷,mamicode.com
首页 > 数据库 > 详细

Mysql DBA 高级运维学习笔记-增删表字段更改表名删除表实战

时间:2018-01-28 13:51:00      阅读:221      评论:0      收藏:0      [点我收藏+]

标签: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: 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 高级运维学习笔记-增删表字段更改表名删除表实战

标签:utf8   位置   des   tables   运维学习   alter   ant   nta   empty   

原文地址:http://blog.51cto.com/10642812/2065961

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!