MySQL基础教程5——DDL—表操作

157 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第30天,点击查看活动详情

MySQL基础教程5——DDL—表操作

在进行表操作前一定要使用表存在的数据库!

表操作——增加

创建表

使用create table 表名( 字段一 字段一类型 comment 字段一注释, 字段二 字段二类型 comment 字段二注释 ); comment 表注释

注意: 最后一个字段结尾不用加,,comment 注释可以不加。

mysql> create table user(
    -> name char(5) comment '姓名',
    -> id int
    -> ); comment `用户表`
Query OK, 0 rows affected (0.03 sec)

表操作——查询

查询当前数据库所有表

使用show tables;

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

可以看到刚刚创建的user表

查询表结构

使用desc 表名;

mysql> desc user;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name  | char(5) | YES  |     | NULL    |       |
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

可以查询创建表的结构

查询指定表的建表语句

使用show create table 表名;

mysql> show create table user;
+-------+----------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                           |
+-------+----------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `name` char(5) DEFAULT NULL COMMENT '姓名',
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)

可以看到创建表时自己输入的语句以及一些默认的语句。

表操作——修改

增加新字段

使用alter table 表名 add 字段名 类型 [comment ];

mysql> alter table user add age int;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

使用desc 表名;查询一下age字段是否添加成功。

mysql> desc user;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name  | char(5) | YES  |     | NULL    |       |
| id    | int(11) | YES  |     | NULL    |       |
| age   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

age字段已成功添加。

修改数据类型

使用alter table 表名 modify 字段名 新数据类型(长度);

mysql> alter table user modify name varchar(20);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0=

使用desc 表名;查看name字段的type是否被修改。

mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| id    | int(11)     | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

name字段已近成功被修改为varchar类型。

修改字段名及字段类型

使用alter table 表名 change 旧字段名 新字段名 类型(长度) [comment ];

mysql> alter table user change id xh int;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

使用desc 表名;查询id字段是否被修改为xh

mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| xh    | int(11)     | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

修改表名

使用alter table 旧表名 rename to 新表名;

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

使用show tables;查询一下user是否被修改为users

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

表名已被成功修改。

表操作——删除

删除字段

使用alter table 表名 drop 字段名;

mysql> alter table users drop xh;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

使用desc 表名;查看xh字段是否被删除。

mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

xh字段已被删除。

删除表

使用drop table 表名;

mysql> drop table users;
Query OK, 0 rows affected (0.02 sec)

使用show tables;查询users是否已被删除。

mysql> show tables;
Empty set (0.00 sec)

可以看到现在该数据库里已经没有表了。

在删除表时若无该表会报错,可以使用drop table if exists 表名;判断若有则删除。

格式化表

使用truncate table 表名;

mysql> truncate table user;
Query OK, 0 rows affected (0.05 sec)

格式化和删除的区别

删除表是整张表都删除了,格式化表只是删除表中所有数据。

(点击进入专栏查看详细教程)