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

1,创建表

CREATE TABLE 表名称(

字段名1 数据类型,

字段名2 数据类型,

字段名3 数据类型,

)

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;

mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| tb_student       |
| tb_teacher       |
+------------------+
2 rows in set (0.00 sec)

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

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

SHOW TABLES FROM 数据库名称;

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 表名称
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 表名称
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 新增字段名 数据类型;

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;

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 字段名;

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 字段名 新字段名 数据类型;

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;

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 新表名称;

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 字符集对应的校对规则;

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 表名称;

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)

你可能感兴趣的:(数据库)