Query the database:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| test |
+--------------------+
6 rows in set (0.03 sec)
The query creation database command and its default character set:
mysql> show create database school;
+----------+--------------------------------------------------------------------
--------------------------------+
| Database | Create Database
|
+----------+--------------------------------------------------------------------
--------------------------------+
| school | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!
80016 DEFAULT ENCRYPTION='N' */ |
+----------+--------------------------------------------------------------------
--------------------------------+
1 row in set (0.00 sec)
Create the database:
mysql> create database data_view;
Query OK, 1 row affected (0.41 sec)
An existing database cannot be created repeatedly, and an error will be reported:
mysql> create database data_view;
ERROR 1007 (HY000): Can't create database 'data_view'; database exists
Using judgment to determine whether there is a database will not report an error:
If you do n’t create it, if you do n’t, you will create it.
mysql> create database if not exists data_view;
Query OK, 1 row affected, 1 warning (0.23 sec)
Specify the character set when creating the database:
For example: gbk
mysql> create database if not exists data_test character set gbk;
Query OK, 1 row affected (0.38 sec)
At this point, check that the character set of the newly created database has become gbk:
mysql> show create database data_test;
+-----------+-------------------------------------------------------------------
-----------------------------------+
| Database | Create Database
|
+-----------+-------------------------------------------------------------------
-----------------------------------+
| data_test | CREATE DATABASE `data_test` /*!40100 DEFAULT CHARACTER SET gbk */
/*!80016 DEFAULT ENCRYPTION='N' */ |
+-----------+-------------------------------------------------------------------
-----------------------------------+
1 row in set (0.00 sec)
Modify the database character set:
mysql> alter database data_test character set utf8;
Query OK, 1 row affected, 1 warning (0.43 sec)
Delete the database (dangerous way):
mysql> drop database if exists data_test;
Query OK, 0 rows affected (0.32 sec)
Check if there is still this database
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| data_view |
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| test |
+--------------------+
7 rows in set (0.00 sec)
Use of the database:
Check the current database before using it:
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
null indicates that the database is not currently used;
Use the database
mysql> use data_view;
Database changed
Check out the currently used database:
mysql> select database();
+------------+
| database() |
+------------+
| data_view |
+------------+
1 row in set (0.00 sec)