1.创建数据库语句
create database语句是在MySQL实例上创建一个指定名称的数据库。
create schema语句的语义和create database是一样的。
2.语法解析
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
(1)用给定的名称创建数据库。如果用这句话,你需要创建数据库的权限。创建模式是一个创建数据库的同义词。
(2)如果数据库存在且未指定是否存在,则会出现错误。
(3)在一个活跃的锁表语句的会话中不允许创建数据库。
(4)create_specification选项指定创建数据库的属性,并存储在数据库目录的db.opt文件中。
- charcter set属性指定数据库的默认字符集;
- collate属性指定此数据库的默认排序规则。
[root@localhost course]# ls
course.frm course.ibd db.opt dept.frm dept.ibd students.frm students.ibd teacher_backup.frm teacher_backup.ibd teacher.frm teacher.ibd
[root@localhost course]# cat db.opt
default-character-set=latin1
default-collation=latin1_swedish_ci
(5)MySQL中的数据库被实现为一个目录,该目录包含与数据库中的表相对应的文件。因为当数据库初始化时是没有表的,创建数据库语句只在MySQL数据目录下创建一个目录的和db.opt文件。
(6)如果在数据库目录下手动创建目录(使用mkdir),服务器认为它是数据库目录,并显示在数据库里。
(7)你也可以使用mysqladmin程序创建数据库。
3.实践演示
mysql> create database test2;
Query OK, 1 row affected (0.00 sec)
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> create database test3 character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show create database test3;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| test3 | CREATE DATABASE `test3` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> create database test3;
ERROR 1007 (HY000): Can't create database 'test3'; database exists
mysql> create database if not exists test3;
Query OK, 1 row affected, 1 warning (0.00 sec)
4.创建数据库的另一种方式
[root@localhost data]# pwd
/data1/mysql/data
[root@localhost data]# mkdir test1
[root@localhost data]# chown mysql:mysql -R test1
[root@localhost data]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 11
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| a1 |
| a2 |
| a3 |
| course |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
9 rows in set (0.01 sec)
5.查看数据库创建语句
SHOW CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
显示创建命名数据库的创建数据库语句。
如果显示语句包含"IF NOT EXISTS"子句,输出也包含这样一个子句。"SHOW CREATE SCHEMA"是"SHOW CREATE DATABASE"的同义词。
mysql> show create database test2G
*************************** 1. row ***************************
Database: test2
Create Database: CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)
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> show create schema test2;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)