【SQL】(MySQL)DDL——表TABLE

扶醉桌前 提交于 2020-02-17 18:43:30

创建表

语法:CRATE TABLE [IF NOT EXISTS] 表名(
   列名 列类型
   列名 列类型
   ···  ···
   列名 列类型
   );
输入语句:

mysql> CREATE TABLE tb_stu(
    -> number CHAR(11),
    -> name VARCHAR(50),
    -> age INT,
    -> gender VARCHAR(10)
    -> );

运行结果:

Query OK, 0 rows affected (0.38 sec)

显示表

语法:SHOW TABLES;
输入语句:

mysql> SHOW TABLES;

运行结果:

+-----------------+
| Tables_in_mydb1 |
+-----------------+
| tb_stu          |
+-----------------+
1 row in set (0.34 sec)

显示当前数据库中所有表的名字。

查询表

语法:DESC 表名;
输入语句:

mysql> DESC tb_stu;

运行结果:

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| number | char(11)    | YES  |     | NULL    |       |
| name   | varchar(50) | YES  |     | NULL    |       |
| age    | int(11)     | YES  |     | NULL    |       |
| gender | varchar(10) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.39 sec)

删除表

语法:DROP TABLE 表名;
输入语句:

mysql> DROP TABLE tb_stu;

运行结果:

Query OK, 0 rows affected (0.00 sec)

修改表

原表:

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| number | char(11)    | YES  |     | NULL    |       |
| name   | varchar(50) | YES  |     | NULL    |       |
| age    | int(11)     | YES  |     | NULL    |       |
| gender | varchar(10) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

1.添加列

语法:ALTER TABLE 表名
   ADD(
   列名 列类型
   列名 列类型
   ···  ···
   列名 列类型
   );
输入语句:

mysql> ALTER TABLE tb_stu
    -> ADD(
    -> education VARCHAR(50)
    -> );

运行结果:

Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

查看当前表:

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| number    | char(11)    | YES  |     | NULL    |       |
| name      | varchar(50) | YES  |     | NULL    |       |
| age       | int(11)     | YES  |     | NULL    |       |
| gender    | varchar(10) | YES  |     | NULL    |       |
| education | varchar(50) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

2.修改列

语法:ALTER TABLE 表名
   MODIFY 列名 列类型;
输入语句:

mysql> ALTER TABLE tb_stu
    -> MODIFY education VARCHAR(100);

运行结果:

Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

查看当前表:

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| number    | char(11)     | YES  |     | NULL    |       |
| name      | varchar(50)  | YES  |     | NULL    |       |
| age       | int(11)      | YES  |     | NULL    |       |
| gender    | varchar(10)  | YES  |     | NULL    |       |
| education | varchar(100) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

3.删除列

语法:ALTER TABLE 表名
   DROP 列名;
输入语句:

mysql> ALTER TABLE tb_stu
    -> DROP education;

运行结果:

Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

查看当前表:

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| number | char(11)    | YES  |     | NULL    |       |
| name   | varchar(50) | YES  |     | NULL    |       |
| age    | int(11)     | YES  |     | NULL    |       |
| gender | varchar(10) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

4.修改表名

原表名:

+-----------------+
| Tables_in_mydb1 |
+-----------------+
| tb_stu          |
+-----------------+

语法:ALTER TABLE 原表名
   RENAME TO 新表名;
输入语句:

mysql> ALTER TABLE tb_stu
    -> RENAME TO tb_student;

运行结果:

Query OK, 0 rows affected (0.01 sec)

新表名:

+-----------------+
| Tables_in_mydb1 |
+-----------------+
| tb_student      |
+-----------------+
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!