数据库管理


使用HELP指令可以查看帮助信息。

                    mysql> HELP SHOW DATABASES;
                    Name: 'SHOW DATABASES'
                    Description:
                    Syntax:
                    SHOW {DATABASES | SCHEMAS}
                        [LIKE 'pattern' | WHERE expr]
                

数据库查询

使用SHOW DATABASES查询数据库,还可使用SHOW DATABASES LIKE 语句过滤数据库查询:

                    SHOW DATABASES [LIKE '<数据库名>'];
                
                    mysql> SHOW DATABASES;
                    +--------------------+
                    | Database           |
                    +--------------------+
                    | gxp                |
                    | information_schema |
                    | mysql              |
                    | performance_schema |
                    | sys                |
                    +--------------------+
                    5 rows in set (0.00 sec)
                
                    mysql> SHOW DATABASES LIKE "infor%";
                    +--------------------+
                    | Database (infor%)  |
                    +--------------------+
                    | information_schema |
                    +--------------------+
                    1 row in set (0.00 sec)
                

创建数据库

使用CREATE DATABASE创建数据库,IF NOT EXISTS是可选项,加上它会先判断数据库是否存在,如不存在才创建。

                    CREATE DATABASE [IF NOT EXISTS] <数据库名称>;
                
                    mysql> CREATE DATABASE IF NOT EXISTS school;
                    Query OK, 1 row affected, 1 warning (0.00 sec)

                    mysql>
                

删除数据库

使用DROP DATABASE删除数据库

                    DROP DATABASES <数据库名称>;
                
                    mysql> DROP DATABASE school;
                    Query OK, 1 row affected (0.04 sec)

                    mysql>
                

查看数据库创建信息

使用SHOW CREATE DATABASE可以查看数据库创建信息。

                    SHOW CREATE DATABASE <数据库名称>;
                
                    mysql> SHOW CREATE DATABASE school;
                    +----------+----------------------------------------------------------------------------------------------------------------------------------+
                    | Database | Create Database                                                                                                                  |
                    +----------+----------------------------------------------------------------------------------------------------------------------------------+
                    | school   | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
                    +----------+----------------------------------------------------------------------------------------------------------------------------------+
                    1 row in set (0.00 sec)
                

修改数据库

使用ALTER DATABASE修改数据库

                    ALTER DATABASE <数据库名称> <修改选项>;
                
                    mysql>  ALTER DATABASE school READ ONLY = 1;
                    Query OK, 1 row affected (0.02 sec)

                    mysql> CREATE TABLE school.student ( id bigint(20) primary key not null auto_increment,name varchar(20));
                    ERROR 3989 (HY000): Schema 'school' is in read only mode.
                

上述指令将数据库school设置为只读模式,创建新的数据表出错。

使用数据库

使用USE将数据库设置为默认数据库。

                    USE <数据库名称>;
                
                    mysql> USE school;
                    Database changed
                    mysql>
                

创建表

使用CREATE TABLE创建表

                    CREATE TABLE [IF NOT EXISTS] <表名> (<栏位定义1>,<栏位定义2>,...);
                
                    mysql> CREATE TABLE IF NOT EXISTS student (id bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT, name varchar(50) NOT NULL, age int(11) default 0);
                    Query OK, 0 rows affected, 2 warnings (0.06 sec)
                

创建数据表student,其栏位信息如下表:

栏位定义
idbigint、不为空、主键、自增
namevarchar、最大长度50、不为空
ageint、默认值为0

查看表结构

使用DESC查看数据表结构。

                    DESC <表名>;
                
                    mysql> DESC student;
                    +-------+-------------+------+-----+---------+----------------+
                    | Field | Type        | Null | Key | Default | Extra          |
                    +-------+-------------+------+-----+---------+----------------+
                    | id    | bigint      | NO   | PRI | NULL    | auto_increment |
                    | name  | varchar(50) | NO   |     | NULL    |                |
                    | age   | int         | YES  |     | 0       |                |
                    +-------+-------------+------+-----+---------+----------------+
                    3 rows in set (0.00 sec)
                

查看数据表创建

使用SHOW CREATE TABLE 查看数据表创建语句。

                    SHOW CREATE TABLE <表名>;
                
                    mysql> SHOW CREATE TABLE student;
                    +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
                    | Table   | Create Table
                                                                                    |
                    +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
                    | student | CREATE TABLE `student` (
                    `id` bigint NOT NULL AUTO_INCREMENT,
                    `name` varchar(50) NOT NULL,
                    `age` int DEFAULT '0',
                    PRIMARY KEY (`id`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
                    +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
                    1 row in set (0.00 sec)
                

添加栏位

使用ALTER TABLE student ADD COLUMN,添加数据表栏位。

                    ALTER TABLE student ADD COLUMN <栏位定义1>, ADD COLUMN <栏位定义2>,... ;
                
                    mysql> ALTER TABLE student ADD COLUMN classId int NOT NULL,ADD Column gender VARCHAR(10) NOT NULL;
                    Query OK, 0 rows affected (0.05 sec)
                    Records: 0  Duplicates: 0  Warnings: 0
                

查询添加结果:

                    mysql> DESC student;
                    +---------+-------------+------+-----+---------+----------------+
                    | Field   | Type        | Null | Key | Default | Extra          |
                    +---------+-------------+------+-----+---------+----------------+
                    | id      | bigint      | NO   | PRI | NULL    | auto_increment |
                    | name    | varchar(50) | NO   |     | NULL    |                |
                    | age     | int         | YES  |     | 0       |                |
                    | classId | int         | NO   |     | NULL    |                |
                    | gender  | varchar(10) | NO   |     | NULL    |                |
                    +---------+-------------+------+-----+---------+----------------+
                    5 rows in set (0.00 sec)
                

修改栏位

使用ALTER TABLE ... MODIFY COLUMN,修改数据表栏位。

                    ALTER TABLE <表名> MODIFY COLUMN <栏位定义1>,MODIFY COLUMN <栏位定义2>,... ;
                
                    mysql> ALTER TABLE student MODIFY COLUMN gender VARCHAR(8) NOT NULL DEFAULT '';
                    Query OK, 0 rows affected (0.17 sec)
                    Records: 0  Duplicates: 0  Warnings: 0
                

查看修改结果:

                    mysql> DESC student;
                    +---------+-------------+------+-----+---------+----------------+
                    | Field   | Type        | Null | Key | Default | Extra          |
                    +---------+-------------+------+-----+---------+----------------+
                    | id      | bigint      | NO   | PRI | NULL    | auto_increment |
                    | name    | varchar(50) | NO   |     | NULL    |                |
                    | age     | int         | YES  |     | 0       |                |
                    | classId | int         | NO   |     | NULL    |                |
                    | gender  | varchar(8)  | NO   |     |         |                |
                    +---------+-------------+------+-----+---------+----------------+
                    5 rows in set (0.00 sec)
                

删除栏位

使用ALTER TABLE ... DROP COLUMN,删除数据表栏位。

                    ALTER TABLE <表名> DROP COLUMN <栏位1>,DROP COLUMN <栏位2>,... ;
                
                    mysql> ALTER TABLE student DROP COLUMN gender;
                    Query OK, 0 rows affected (0.05 sec)
                    Records: 0  Duplicates: 0  Warnings: 0
                

查看修改结果:

                    mysql> DESC student;
                    +---------+-------------+------+-----+---------+----------------+
                    | Field   | Type        | Null | Key | Default | Extra          |
                    +---------+-------------+------+-----+---------+----------------+
                    | id      | bigint      | NO   | PRI | NULL    | auto_increment |
                    | name    | varchar(50) | NO   |     | NULL    |                |
                    | age     | int         | YES  |     | 0       |                |
                    | classId | int         | NO   |     | NULL    |                |
                    +---------+-------------+------+-----+---------+----------------+
                    4 rows in set (0.01 sec)
                

删除数据表

使用DROP TABLE,删除数据表。

                    ROP TABLE <表名>;
                
                    mysql> DROP TABLE student;
                    Query OK, 0 rows affected (0.05 sec)
                

创建索引

使用CREATE INDEX 创建索引。

                    CREATE INDEX <索引名称> [USING BTREE | HASH ] ON <表名>(<栏位>);
                
                    mysql> CREATE INDEX nameIndex USING BTREE ON student(name);
                    Query OK, 0 rows affected, 1 warning (0.05 sec)
                    Records: 0  Duplicates: 0  Warnings: 1
                

查看索引

使用SHOW INDEX FROM 查看索引。

                    mysql> SHOW INDEX FROM student\G;
                    *************************** 1. row ***************************
                    ...
                    *************************** 2. row ***************************
                            Table: student
                    Non_unique: 1
                        Key_name: nameIndex
                    Seq_in_index: 1
                    Column_name: name
                        Collation: A
                    Cardinality: 0
                        Sub_part: NULL
                        Packed: NULL
                            Null:
                    Index_type: BTREE
                        Comment:
                    Index_comment:
                        Visible: YES
                    Expression: NULL
                    2 rows in set (0.01 sec)
                

删除索引

使用DROP INDEX 删除索引。

                    DROP INDEX <索引名称> ON <表名>;
                
                    mysql> DROP INDEX nameIndex ON student;
                    Query OK, 0 rows affected (0.04 sec)
                    Records: 0  Duplicates: 0  Warnings: 0