本文介紹建立資料庫指令及顯示建立語句指令
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 test2\G
*************************** 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)