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)
相关推荐
戒不掉的伤怀13 分钟前
【Navicat 连接MySQL时出现错误1251:客户端不支持服务器请求的身份验证协议;请考虑升级MySQL客户端】
服务器·数据库·mysql
cv高级工程师YKY19 分钟前
服务器 - - QPS与TPS介绍
数据库
nbsaas-boot27 分钟前
高可扩展属性建模设计:架构师的全局思考与落地方案
数据库
爱上语文1 小时前
Redis基础(5):Redis的Java客户端
java·开发语言·数据库·redis·后端
陈敬雷-充电了么-CEO兼CTO1 小时前
推荐算法系统系列>推荐数据仓库集市的ETL数据处理
大数据·数据库·数据仓库·数据挖掘·数据分析·etl·推荐算法
MeshddY2 小时前
(超详细)数据库项目初体验:使用C语言连接数据库完成短地址服务(本地运行版)
c语言·数据库·单片机
wuxinyan1232 小时前
Java面试题033:一文深入了解MySQL(5)
java·数据库·mysql·面试
萧曵 丶2 小时前
Spring @TransactionalEventListener
java·数据库·spring·事务·transactional·异步
胡斌附体2 小时前
mobaxterm终端sqlplus乱码问题解决
数据库·乱码·sqlplus·字符集设置
moon66sun2 小时前
开源项目XYZ.ESB:数据库到数据库(DB->DB)集成
数据库·esb