MySQL Lecture 2-Database Operations

MySQL Lecture 2-Database Operations

A database can be a container dedicated to storing data objects, including: tables, views, triggers, stored procedures, etc., where tables are the most basic data objects, and all data is stored in tables. Before creating any data objects, create a database. Then create the various objects needed in the database.

One, create a database

Use the CREATE DATABASE statement to create a database, the syntax format is as follows:

CREATE DATABASE [IF NOT EXISTS] <数据库名>
[[DEFAULT] CHARACTER SET <字符集名>] [[DEFAULT] COLLATE <校对规则名>];

说明:
(1[]中的内容是可选的。
(2<数据库名>:要创建数据库的名称。
(3IF NOT EXISTS:只有该数据库不存在时才能执行创建操作,可以避免因为数据库已经存在而引起错误。
(4[DEFAULT] CHARACTER SET:指定数据库的默认字符集。字符集是用来定义 MySQL 存储字符串的方式。该选项可以省略,如果省略就采用配置文件中指定的字符集。MySQL 不允许在同一个系统中创建两个相同名称的数据库。
(5[DEFAULT] COLLATE:指定字符集的默认校对规则。校对规则用来定义比较字符串的方式,以解决排序和字符分组的问题。该选项可以省略,如果省略就采用配置文件中指定的校对规则。

1. Check MySQL's default character set and collation rules

mysql> show variables like '%char%';   --查看系统默认的字符集
+--------------------------+--------------------------------------+
| Variable_name            | Value                                |
+--------------------------+--------------------------------------+
| character_set_client     | utf8                                 |
| character_set_connection | utf8                                 |
| character_set_database   | utf8mb4                              |
| character_set_filesystem | binary                               |
| character_set_results    | utf8                                 |
| character_set_server     | utf8mb4                              |
| character_set_system     | utf8                                 |
| character_sets_dir       | /usr/local/mysql-5.7/share/charsets/ |
+--------------------------+--------------------------------------+
8 rows in set (0.26 sec)

mysql> show variables like '%COLL%';  --查看系统默认的校对规则
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8_general_ci    |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.03 sec)

2. Omit the character set and collation rules when creating the database

mysql> create database mydb;
Query OK, 1 row affected (0.21 sec)

You can use the show create database command to view the complete database creation information, the command is as follows:

mysql> show create database mydb;
+----------+------------------------------------------------------------------+
| Database | Create Database                                                  |
+----------+------------------------------------------------------------------+
| mydb     | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.01 sec)

--由于创建数据库时没有指定字符集,则使用 character_set_database 参数对应的字符集。

3. Specify the character set and collation rules when creating the database

mysql> create database mydb2 character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.11 sec)

mysql> show create database mydb2;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| mydb2    | CREATE DATABASE `mydb2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)

Two, view the existing database in the system

Use the show databases command to view the existing databases in the system, the command is as follows:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#:q!       |
| hist               |
| mydb               |
| mydb2              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
8 rows in set (0.03 sec)

Three, modify the database

Modifying the database is mainly to modify the character set and collation rules of the database. Use the alter database command. The meaning of each option is the same as that of create database. The syntax is as follows:

ALTER DATABASE [数据库名]
[ DEFAULT ] CHARACTER SET <字符集名> |
[ DEFAULT ] COLLATE <校对规则名>

The following changes the character set of the mydb database to gbk, and the collation rules to gbk_chinese_ci.

mysql> alter database mydb character set gbk collate gbk_chinese_ci;
Query OK, 1 row affected (0.01 sec)

mysql> show create database mydb;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| mydb     | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

Fourth, delete the database

To delete the database, use the drop database command, the syntax is as follows:

drop database 数据库名;

The following command deletes the database mydb:

mysql> drop database mydb;
Query OK, 0 rows affected (0.18 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#:q!       |
| hist               |
| mydb2              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

Five, select the database

Since objects such as tables and views must be included in a certain database, you must select the database where the object is located before creating the object. The syntax is as follows:

use 数据库名;

1. Select the database hist and view the tables contained in it

mysql> use hist;
Database changed

mysql> show tables;   --hist 数据库包含一张表。
+----------------+
| Tables_in_hist |
+----------------+
| dept           |
+----------------+
1 row in set (0.00 sec)

2. Select the database mydb2 to view the tables contained in it

mysql> use mydb2;
Database changed

mysql> show tables;  --mydb2 数据库中没有创建表。
Empty set (0.00 sec)

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/108651969