1. 语法:create database 库名
for example:
mysql> create database oldboy;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy |
| performance_schema |
| test |
+--------------------+
mysql> help show
mysql> show create database oldboy;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| oldboy | CREATE DATABASE `oldboy` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
mysql> show create database oldboy \G;
*************************** 1. row ***************************
Database: oldboy
Create Database: CREATE DATABASE `oldboy` /*!40100 DEFAULT CHARACTER SET utf8 */
-------------------------------------------------------------------------------------
上面的例子字符集是utf8,下面创建一个gbk字符集
mysql> create database oldboy_gbk default character set gbk collate gbk_chinese_ci;
Query OK, 1 row affected (0.00 sec) // collate gbk_chinese_ci 是字符集的校队规则
mysql> show create database oldboy_gbk;
+------------+--------------------------------------------------------------------+
| Database | Create Database |
+------------+--------------------------------------------------------------------+
| oldboy_gbk | CREATE DATABASE `oldboy_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ |
+------------+--------------------------------------------------------------------+
1 row in set (0.00 sec)
-------------------------------------------------------------------------------------------
创建一个utf8的数据库
mysql> create database oldboy_utf8 character set utf8 collate utf8_general_ci;
mysql> show create database oldboy_utf8;
+-------------+----------------------------------------------------------------------+
| Database | Create Database |
+-------------+----------------------------------------------------------------------+
| oldboy_utf8 | CREATE DATABASE `oldboy_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+-------------+----------------------------------------------------------------------+
提示:如果编译的时候指定了特定的字符集,那么创建对应的数据库就不需要字符集了。
企业里怎么创建数据库呢?
1.根据开发的程序确定字符集(建议utf8)
2.编译时候指定字符集,例如:
--DDEFAULT_CHARSET=utf8 \
--DDEFAULT_COLLATION=utf8_general_ci \
然后建库的时候默认创建即可,create database oldboy;
3.编译的时候没有指定字符集或者指定了和程序不同的字符集,如何解决?
指定字符集创建数据库即可。
数据库要支持创建的字符集