MySQL相关信息之数据库操作(三)

时间:2021-07-11 03:27:53

1、创建数据库

CREATE  {DATABASE  |  SCHEMA}  [IF NOT EXISTS]  DB_NAME

[DEFAULT]  CHARACTER SET [=] charset_name

{}为必选项,[]为可选项

root@localhost test>CREATE DATABASE t1;
Query OK, 1 row affected (0.03 sec)

2、查看存在的所有的数据库

root@localhost test>SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| t1 |
| test |
+--------------------+
5 rows in set (0.00 sec)

root@localhost test>

3、查看数据库的编码方式

root@localhost test>SHOW CREATE DATABASE t1;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| t1 | CREATE DATABASE `t1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)

4、数据库编码格式设定为gbk

root@localhost test>CREATE DATABASE IF NOT EXISTS t2 CHARACTER SET gbk;
Query OK, 1 row affected (0.00 sec)

root@localhost test>SHOW CREATE DATABASE t2;
+----------+------------------------------------------------------------
| Database | Create Database
+----------+------------------------------------------------------------
| t2 | CREATE DATABASE `t2` /*!40100 DEFAULT CHARACTER SET gbk */
+----------+------------------------------------------------------------
1 row in set (0.00 sec)

5、修改数据库

ALTER {DATABASE  |  SCHEMA} [db_name]

[default] CHARACTER SET [=] charset_name

把数据库的编码格式由gbk修改为utf8

root@localhost test>ALTER DATABASE t2 CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

root@localhost test>SHOW CREATE DATABASE t2;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| t2 | CREATE DATABASE `t2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)

6、删除数据库

DROP {DATABASE  |  SCHEMA} [IF EXISTS] db_name

 

root@localhost test>DROP DATABASE t1;
Query OK, 0 rows affected (0.05 sec)

root@localhost test>SHOW DATABASES;  #查看警告
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| t2 |
| test |
+--------------------+
5 rows in set (0.00 sec)

root@localhost test>DROP DATABASE t1;
ERROR 1008 (HY000): Can't drop database 't1'; database doesn't exist
root@localhost test>DROP DATABASE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

root@localhost test>SHOW WARNINGS;
+-------+------+--------------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------------+
| Note | 1008 | Can't drop database 't1'; database doesn't exist |
+-------+------+--------------------------------------------------+
1 row in set (0.00 sec)