Mysql8.0 数据库表的基本操作

1,创建表

CREATE TABLE 表名称(

字段名1 数据类型,

字段名2 数据类型,

字段名3 数据类型,

)

bash 复制代码
mysql> create table tb_student(
    -> sid INT,
    -> sname VARCHAR(20)
    -> );
Query OK, 0 rows affected (1.19 sec)

mysql> create table tb_teacher(
    -> tid INT,
    -> tname VARCHAR(20)
    -> );
Query OK, 0 rows affected (1.19 sec)

2,查看表

SHOW TABLES;

bash 复制代码
mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| tb_student       |
| tb_teacher       |
+------------------+
2 rows in set (0.00 sec)

使用show tables;前必须先use 数据库名称,选择具体的数据库。

如果不选择数据库,那么使用如下语句:

SHOW TABLES FROM 数据库名称;

bash 复制代码
mysql> show tables from test01;
+------------------+
| Tables_in_test01 |
+------------------+
| tb_student       |
| tb_teacher       |
+------------------+
2 rows in set (0.00 sec)

3,查看表结构

DESCRIBE 表名称;简写 DESC 表名称;

或者

SHOW CREATE TABLE 表名称;

3.1 DESC 表名称
bash 复制代码
mysql> desc tb_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int         | YES  |     | NULL    |       |
| sname | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
3.2 SHOW CREATE TABLE 表名称
bash 复制代码
mysql> show create table tb_student\G
*************************** 1. row ***************************
       Table: tb_student
Create Table: CREATE TABLE `tb_student` (
  `sid` int DEFAULT NULL,
  `sname` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

这种方式查看数据库表定义不只是字段,还有字符集、字符集校对规则和存储引擎(例如,InnoDB)等信息。

4,修改表

4.1 增加字段

ALTER TABLE 表名称 ADD COLUMN 新增字段名 数据类型;

bash 复制代码
mysql> alter table tb_student add column score INT;
Query OK, 0 rows affected (0.65 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int         | YES  |     | NULL    |       |
| sname | varchar(20) | YES  |     | NULL    |       |
| score | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

默认在所有字段末尾添加新字段。 可以通过AFTER和FIRST指定添加位置。

ALTER TABLE 表名称 ADD COLUMN 新增字段名 数据类型 AFTER 字段名;

ALTER TABLE 表名称 ADD COLUMN 新增字段名 数据类型 FIRST;

bash 复制代码
mysql> desc tb_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int         | YES  |     | NULL    |       |
| sname | varchar(20) | YES  |     | NULL    |       |
| score | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table tb_student add column age INT AFTER sname;
Query OK, 0 rows affected (0.80 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int         | YES  |     | NULL    |       |
| sname | varchar(20) | YES  |     | NULL    |       |
| age   | int         | YES  |     | NULL    |       |
| score | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
4.2 删除字段

ALTER TABLE 表名称 DROP COLUMN 字段名;

bash 复制代码
mysql> desc tb_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int         | YES  |     | NULL    |       |
| sname | varchar(20) | YES  |     | NULL    |       |
| age   | int         | YES  |     | NULL    |       |
| score | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table tb_student drop column age;
Query OK, 0 rows affected (0.88 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int         | YES  |     | NULL    |       |
| sname | varchar(20) | YES  |     | NULL    |       |
| score | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.3 修改字段名、数据类型、位置等

修改字段名称可以通过CHANGE关键字实现,而修改字段数据类型、位置等属性则需要通过MODIFY关键字实现。

修改字段名称:

ALTER TABLE 表名称 CHANGE COLUMN 字段名 新字段名 数据类型;

bash 复制代码
mysql> alter table tb_student change column score chinese INT;
Query OK, 0 rows affected (0.79 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sid     | int         | YES  |     | NULL    |       |
| sname   | varchar(20) | YES  |     | NULL    |       |
| chinese | int         | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

修改字段类型:

ALTER TABLE 表名称 MODIFY COLUMN 字段名 新数据类型;

ALTER TABLE 表名称 MODIFY COLUMN 字段名 数据类型 AFTER 字段名;

ALTER TABLE 表名称 MODIFY COLUMN 字段名 数据类型 FIRST;

bash 复制代码
mysql> alter table tb_student modify column sname VARCHAR(30);
Query OK, 0 rows affected (0.26 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sid     | int         | YES  |     | NULL    |       |
| sname   | varchar(30) | YES  |     | NULL    |       |
| chinese | int         | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.4 修改表名称

ALTER TABLE 表名称 RENAME TO 新表名称;

RENAME TABLE 表名称 TO 新表名称;

bash 复制代码
mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| tb_student       |
| tb_teacher       |
+------------------+
2 rows in set (0.00 sec)

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

mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| student          |
| tb_teacher       |
+------------------+
2 rows in set (0.00 sec)
4.5 修改表的编码方式

ALTER TABLE 表名称 CHARACTER SET 字符集名称 COLLATE 字符集对应的校对规则;

bash 复制代码
mysql> show create table tb_student\G
*************************** 1. row ***************************
       Table: tb_student
Create Table: CREATE TABLE `tb_student` (
  `sid` int DEFAULT NULL,
  `sname` varchar(30) DEFAULT NULL,
  `chinese` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

mysql> alter table tb_student character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected, 2 warnings (0.34 sec)
Records: 0  Duplicates: 0  Warnings: 2

mysql> show create table tb_student\G
*************************** 1. row ***************************
       Table: tb_student
Create Table: CREATE TABLE `tb_student` (
  `sid` int DEFAULT NULL,
  `sname` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  `chinese` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
1 row in set (0.00 sec)

5,删除表

DROP TABLE 表名称;

bash 复制代码
mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| tb_student       |
| tb_teacher       |
+------------------+
2 rows in set (0.00 sec)

mysql> drop table tb_teacher;
Query OK, 0 rows affected (2.60 sec)

mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| tb_student       |
+------------------+
1 row in set (0.00 sec)
相关推荐
哭哭啼37 分钟前
Redis环境部署(主从模式、哨兵模式、集群模式)
数据库·redis·缓存
咕噜Yuki06091 小时前
OCP证书如何下载?
数据库·ocp·证书查询
冬瓜3121 小时前
linux-c 使用c语言操作sqlite3数据库-1
数据库·sqlite
夜色呦1 小时前
现代电商解决方案:Spring Boot框架实践
数据库·spring boot·后端
WangYaolove13142 小时前
请解释Python中的装饰器是什么?如何使用它们?
linux·数据库·python
我是黄大仙2 小时前
利用飞书多维表格自动发布版本
运维·服务器·数据库·飞书
曾经的三心草2 小时前
Mysql之约束与事件
android·数据库·mysql·事件·约束
WuMingf_2 小时前
redis
数据库·redis
张某布响丸辣2 小时前
SQL中的时间类型:深入解析与应用
java·数据库·sql·mysql·oracle