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)
相关推荐
码农郁郁久居人下14 分钟前
Redis的配置与优化
数据库·redis·缓存
MuseLss1 小时前
Mycat搭建分库分表
数据库·mycat
Hsu_kk2 小时前
Redis 主从复制配置教程
数据库·redis·缓存
DieSnowK2 小时前
[Redis][环境配置]详细讲解
数据库·redis·分布式·缓存·环境配置·新手向·详细讲解
程序猿小D2 小时前
第二百三十五节 JPA教程 - JPA Lob列示例
java·数据库·windows·oracle·jdk·jpa
Flerken1012 小时前
数据库语言、SQL语言、数据库系统提供的两种语言
数据库·sql·oracle
掘根2 小时前
【网络】高级IO——poll版本TCP服务器
网络·数据库·sql·网络协议·tcp/ip·mysql·网络安全
消失在人海中2 小时前
oracle 表的外键
数据库·oracle
&木头人&2 小时前
oracle 如何查询表被锁
数据库·oracle
Zd083 小时前
14.其他流(下篇)
java·前端·数据库