ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 一、 创建数据库 ### 1.创建数据库语法:`create database [if not exists] db_name [create_specification];` 基本语法创建数据库 mytest,例: ```sql mysql> create database mytest; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mytest | | performance_schema | | school | | school_01 | | shop | | sys | +--------------------+ 8 rows in set (0.00 sec) ``` #### a、可选项 `if not exists` 创建数据库时若加上选项 `if not exists` ,如果这个数据库**没有存在**就顺利创建: ```sql mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | school_01 | | shop | | sys | +--------------------+ 7 rows in set (0.00 sec) mysql> create database if not exists test2; Query OK, 1 row affected (0.00 sec) ``` 创建数据库时若加上选项 `if not exists` ,如果**已经存在**这个数据库就不会创建,并给出一个警告 warning: ```sql mysql> create database if not exists test2; Query OK, 1 row affected, 1 warning (0.00 sec) ``` 查看给出的警告: ```sql mysql> show warnings; +-------+------+------------------------------------------------+ | Level | Code | Message | +-------+------+------------------------------------------------+ | Note | 1007 | Can't create database 'test2'; database exists | +-------+------+------------------------------------------------+ 1 row in set (0.00 sec) ``` 创建数据库时若不加选项 `if not exists` ,如果这个数据库没有存在就顺利创建;如果已经存在这个数据库就会创建失败,并报错提示: ```sql mysql> create database test3; Query OK, 1 row affected (0.00 sec) mysql> create database test3; ERROR 1007 (HY000): Can't create database 'test3'; database exists ``` #### b、可选项 `create_specification` 数据库创建选项 create_specification 有两个待设置的内容: 1. `character set`:字符集,定义了字符以及字符的编码。常见的如 latin1、gbk、utf8、utf8mb4 等。 2. `collate`:字符校对规则,字符序,定义了字符的比较规则。它们以其相关的字符集名开始,通常包括一个语言名,并且以 `_ci`(大小写不敏感)、`_cs`(大小写敏感)或 `_bin`(二元法,二进制编码)结束。建议使用 bin 类型,相对更准确。 例,创建数据库 mytest 时设置 character set : ```sql mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | school_01 | | shop | | sys | +--------------------+ 7 rows in set (0.00 sec) mysql> create database mytest character set utf8; Query OK, 1 row affected (0.00 sec) mysql> show create database mytest; +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | mytest | CREATE DATABASE `mytest` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) ``` 例,创建数据库 test2时设置 character set 和 collate : ```sql mysql> create database test2 character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec) mysql> show create database test2; +----------+---------------------------------------------------------------------------------+ | Database | Create Database | +----------+---------------------------------------------------------------------------------+ | test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */ | +----------+---------------------------------------------------------------------------------+ 1 row in set (0.00 sec) ``` ## 二、修改数据库选项 **基本语法为:** `alter database db_name [alter_specification] ` 例如,修改数据库 test2 的字符集为 utf8 : ```sql mysql> show create database test2; +----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET latin1 */ | +----------+------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter database test2 character set utf8; Query OK, 1 row affected (0.00 sec) mysql> show create database test2; +----------+----------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------+ | test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+----------------------------------------------------------------+ 1 row in set (0.00 sec) ``` 例如,修改数据库 test2 的字符集为 gbk 和字符序为 gbk _bin : ```sql mysql> alter database test2 character set gbk collate gbk_bin; Query OK, 1 row affected (0.00 sec) mysql> show create database test2; +----------+-------------------------------------------------------------------------------+ | Database | Create Database | +----------+-------------------------------------------------------------------------------+ | test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET gbk COLLATE gbk_bin */ | +----------+-------------------------------------------------------------------------------+ 1 row in set (0.00 sec) ``` ## 三、删除数据库 ### 1.删除数据库语法:`drop database db_name;` 例如,删除数据库 mytest : ```sql mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mytest | | performance_schema | | school | | school_01 | | shop | | sys | +--------------------+ 8 rows in set (0.00 sec) mysql> drop database mytest; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | school_01 | | shop | | sys | +--------------------+ 7 rows in set (0.00 sec) ```