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)
相关推荐
Databend1 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术2 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Raymond运维7 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉7 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api