Operating database
A database is an organized and shareable collection of data stored in a computer for a long time.
First, display the database
SHOW DATABASES;
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
Copy the code
2. Create a database
Creating a database is to allocate a space in the database system for storing corresponding data. It is the basis for table operations and database management.
CREATE DATABASE Specifies the DATABASE name
Two databases have been added — Examole and Practice
mysql> CREATE DATABASE practice;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE examole;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| examole |
| mysql |
| performance_schema |
| practice |
| sys |
+--------------------+
6 rows in set (0.00 sec)
Copy the code
Query OK represents a successful creation, modification, or deletion.
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,create_specification] ...] create_specification: [DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
Copy the code
Description:
- Uppercase indicates the keyword
- [] is optional
- CHARACTER SET: Specifies the CHARACTER SET used by the database
- COLLATE: specifies the verification rule for the database character set
* When we create a database without specifying character set and validation rules:
- The system uses the default character set utF8
- The verification rule is UTF8_ general_ CI
For example:
mysql> CREATE DATABASE IF NOT EXISTS example CHARACTER SET utf8mb4;
Query OK, 1 row affected (0.00 sec)
Copy the code
If the system does not have a database named example, create a database named example.
Create a database using the UTF8MB4 character set. Note: MySQL utF8 encoding is not true UTF8, does not contain some complex Chinese characters. MySQL’s true UTF8 uses UTF8MB4. We recommend that you use UTF8MB4
Three, use the database
Use database name;
mysql> USE example;
Database changed
mysql> SHOW TABLES;
Empty set (0.00 sec)
Copy the code
Delete the database
Deleting a database means deleting an existing database from the database system. After deleting the database, the original space will be reclaimed, will delete all tables and all data in the database! carefully
DROP DATABASES Indicates the database name
mysql> DROP DATABASE examole;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| example |
| mysql |
| performance_schema |
| practice |
| sys |
+--------------------+
6 rows in set (0.00 sec)
Copy the code
DROP DATABASE [IF EXISTS] db_name;