數(shù)據(jù)庫可以看作是一個(gè)專門存儲(chǔ)數(shù)據(jù)對(duì)象的容器,這里的數(shù)據(jù)對(duì)象包括表、視圖、觸發(fā)器、存儲(chǔ)過程等,其中表是最基本的數(shù)據(jù)對(duì)象。在?MySQL?數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)對(duì)象之前,先要?jiǎng)?chuàng)建好數(shù)據(jù)庫。
一、?CREATE DATABASE? ?創(chuàng)建數(shù)據(jù)庫
語法格式如下:
CREATE DATABASE [IF NOT EXISTS] <數(shù)據(jù)庫名>
[[DEFAULT] CHARACTER SET <字符集名>] [[DEFAULT] COLLATE <校對(duì)規(guī)則名>];
[ ]中的內(nèi)容是可選的。語法說明如下:
<數(shù)據(jù)庫名>:創(chuàng)建數(shù)據(jù)庫的名稱。MySQL 的數(shù)據(jù)存儲(chǔ)區(qū)將以目錄方式表示 MySQL 數(shù)據(jù)庫,因此數(shù)據(jù)庫名稱必須符合操作系統(tǒng)的文件夾命名規(guī)則,注意在 MySQL 中不區(qū)分大小寫。
IF NOT EXISTS:在創(chuàng)建數(shù)據(jù)庫之前進(jìn)行判斷,只有該數(shù)據(jù)庫目前尚不存在時(shí)才能執(zhí)行操作。此選項(xiàng)可以用來避免數(shù)據(jù)庫已經(jīng)存在而重復(fù)創(chuàng)建的錯(cuò)誤。
[DEFAULT] CHARACTER SET:指定數(shù)據(jù)庫的默認(rèn)字符集。
[DEFAULT] COLLATE:指定字符集的默認(rèn)校對(duì)規(guī)則。
MySQL 的字符集(CHARACTER)和校對(duì)規(guī)則(COLLATION)兩個(gè)不同的概念:字符集是用來定義 MySQL 存儲(chǔ)字符串的方式,校對(duì)規(guī)則定義了比較字符串的方式,解決排序和字符分組的問題。
字符集和校對(duì)規(guī)則是一對(duì)多的關(guān)系,每個(gè)字符集至少對(duì)應(yīng)一個(gè)校對(duì)規(guī)則,MySQL 支持 39 種字符集的將近 200 種校對(duì)規(guī)則。
實(shí)例1:最簡(jiǎn)單的創(chuàng)建 MySQL 數(shù)據(jù)庫的語句
在 MySQL 中創(chuàng)建一個(gè)名為 test_db 的數(shù)據(jù)庫。在 MySQL 命令行客戶端輸入 SQL 語句CREATE DATABASE test_db;即可創(chuàng)建一個(gè)數(shù)據(jù)庫,輸入的 SQL 語句與執(zhí)行結(jié)果如下。
mysql> CREATE DATABASE test_db;
Query OK, 1 row affected (0.12 sec)
若再次輸入上述語句,則系統(tǒng)會(huì)給出錯(cuò)誤提示信息,如下所示:
mysql> CREATE DATABASE test_db;
ERROR 1007 (HY000): Can't create database 'test_db'; database exists
MySQL 不允許在同一系統(tǒng)創(chuàng)建兩個(gè)相同名稱的數(shù)據(jù)庫。
如果加上IF NOT EXISTS從句,則可以避免類似錯(cuò)誤,如下所示:
mysql> CREATE DATABASE?IF NOT EXISTS test_db;
Query OK, 1 row affected (0.12 sec)
實(shí)例2:創(chuàng)建 MySQL 數(shù)據(jù)庫時(shí)指定字符集和校對(duì)規(guī)則
使用 MySQL 命令行工具創(chuàng)建一個(gè)測(cè)試數(shù)據(jù)庫,命名為 test_db_char,指定其默認(rèn)字符集為 utf8,默認(rèn)校對(duì)規(guī)則為 utf8_chinese_ci(簡(jiǎn)體中文,不區(qū)分大小寫),輸入的 SQL 語句與執(zhí)行結(jié)果如下所示:
mysql> CREATE DATABASE?IF NOT EXISTS test_db_char
-> DEFAULT CHARACTER SET utf8
-> DEFAULT COLLATE utf8_chinese_ci;
Query OK, 1 row affected (0.03 sec)
這時(shí),可以使用SHOW CREATE DATABASE查看 test_db_char 數(shù)據(jù)庫的定義聲明,發(fā)現(xiàn)該數(shù)據(jù)庫的指定字符集為 utf8,運(yùn)行結(jié)果如下所示:
mysql> SHOW CREATE DATABASE test_db_char;
+--------------+-----------------------------------------------------+
| Database? ? | Create Database? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+--------------+-----------------------------------------------------+
| test_db_char | CREATE DATABASE `test_db_char` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+--------------+-----------------------------------------------------+
1 row in set (0.05 sec)
為防止字符混亂的情況發(fā)生,MySQL 有時(shí)需要在創(chuàng)建數(shù)據(jù)庫時(shí)明確指定字符集;在中國大陸地區(qū),常用的字符集有 utf8 和 gbk。
utf8 能夠存儲(chǔ)全球的所有字符,在任何國家都可以使用,默認(rèn)的校對(duì)規(guī)則為?utf8_general_ci,對(duì)于中文可以使用?utf8_general_ci。
gbk 只能存儲(chǔ)漢語涉及到的字符,不具有全球通用性,默認(rèn)的校對(duì)規(guī)則為?gbk_chinese_ci。
二、?SHOW DATABASES? ?查看或顯示當(dāng)前用戶權(quán)限范圍以內(nèi)的數(shù)據(jù)庫
查看數(shù)據(jù)庫的語法格式為:
SHOW DATABASES [LIKE '數(shù)據(jù)庫名'];
語法說明如下:
LIKE 從句是可選項(xiàng),用于匹配指定的數(shù)據(jù)庫名稱。LIKE 從句可以部分匹配,也可以完全匹配。
數(shù)據(jù)庫名由單引號(hào)' '包圍。
實(shí)例1:查看所有數(shù)據(jù)庫
列出當(dāng)前用戶可查看的所有數(shù)據(jù)庫:
mysql> SHOW DATABASES;
+--------------------+
| Database? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| sakila? ? ? ? ? ? |
| sys? ? ? ? ? ? ? ? |
| world? ? ? ? ? ? ? |
+--------------------+
6 row in set (0.22 sec)
實(shí)例2:創(chuàng)建并查看數(shù)據(jù)庫
先創(chuàng)建一個(gè)名為 test_db 的數(shù)據(jù)庫:
mysql> CREATE DATABASE test_db;
Query OK, 1 row affected (0.12 sec)
再使用 SHOW DATABASES 語句顯示權(quán)限范圍內(nèi)的所有數(shù)據(jù)庫名,如下所示:
mysql> SHOW DATABASES;
+--------------------+
| Database? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| sakila? ? ? ? ? ? |
| sys? ? ? ? ? ? ? ? |
| test_db? ? ? ? ? ? |
| world? ? ? ? ? ? ? |
+--------------------+
7 row in set (0.22 sec)
你看,剛才創(chuàng)建的數(shù)據(jù)庫已經(jīng)被顯示出來了。
實(shí)例3:使用 LIKE 從句
先創(chuàng)建三個(gè)數(shù)據(jù)庫,名字分別為 test_db、db_test、db_test_db。
1) 使用 LIKE 從句,查看與 test_db 完全匹配的數(shù)據(jù)庫:
mysql> SHOW DATABASES LIKE 'test_db';
+--------------------+
| Database (test_db) |
+--------------------+
| test_db? ? ? ? ? ? |
+--------------------+
1 row in set (0.03 sec)
2) 使用 LIKE 從句,查看名字中包含 test 的數(shù)據(jù)庫:
mysql> SHOW DATABASES LIKE '%test%';
+--------------------+
| Database (%test%)? |
+--------------------+
| db_test? ? ? ? ? ? |
+--------------------+
| db_test_db? ? ? ? |
+--------------------+
| test_db? ? ? ? ? ? |
+--------------------+
3 row in set (0.03 sec)
3) 使用 LIKE 從句,查看名字以 db 開頭的數(shù)據(jù)庫:
mysql> SHOW DATABASES LIKE 'db%';
+----------------+
| Database (db%) |
+----------------+
| db_test? ? ? ? |
+----------------+
| db_test_db? ? |
+----------------+
2 row in set (0.03 sec)
4) 使用 LIKE 從句,查看名字以 db 結(jié)尾的數(shù)據(jù)庫:
mysql> SHOW DATABASES LIKE '%db';
+----------------+
| Database (%db) |
+----------------+
| db_test_db? ? |
+----------------+
| test_db? ? ? ? |
+----------------+
2 row in set (0.03 sec)
三、 ALTER DATABASE 或 ALTER SCHEMA? ?修改數(shù)據(jù)庫的相關(guān)參數(shù)
修改數(shù)據(jù)庫的語法格式為:
ALTER DATABASE [數(shù)據(jù)庫名] { [ DEFAULT ] CHARACTER SET <字符集名> |
[ DEFAULT ] COLLATE <校對(duì)規(guī)則名>}
語法說明如下:
ALTER DATABASE 用于更改數(shù)據(jù)庫的全局特性。這些特性存儲(chǔ)在數(shù)據(jù)庫目錄的 db.opt 文件中。
使用 ALTER DATABASE 需要獲得數(shù)據(jù)庫 ALTER 權(quán)限。
數(shù)據(jù)庫名稱可以忽略,此時(shí)語句對(duì)應(yīng)于默認(rèn)數(shù)據(jù)庫。
CHARACTER SET 子句用于更改默認(rèn)的數(shù)據(jù)庫字符集。
修改數(shù)據(jù)庫的字符集
查看 test_db 數(shù)據(jù)庫的定義聲明的執(zhí)行結(jié)果如下所示:
mysql> SHOW CREATE DATABASE test_db;
+----------+--------------------------------------------------------+
| Database | Create Database? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+----------+--------------------------------------------------------+
| test_db? | CREATE DATABASE `test_db` /*!40100 DEFAULT CHARACTER SET utf8 */|
+----------+--------------------------------------------------------+
1 row in set (0.05 sec)
【例 1】使用命令行工具將數(shù)據(jù)庫 test_db 的指定字符集修改為 gb2312,默認(rèn)校對(duì)規(guī)則修改為 utf8_unicode_ci,輸入 SQL 語句與執(zhí)行結(jié)果如下所示:
mysql> CREATE DATABASE test_db
? ? -> DEFAULT CHARACTER SET gb2312
? ? -> DEFAULT COLLATE gb2312_chinese_ci;
mysql> SHOW CREATE DATABASE test_db;
+----------+--------------------------------------------------------+
| Database | Create Database? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+----------+--------------------------------------------------------+
| test_db? | CREATE DATABASE `test_db` /*!40100 DEFAULT CHARACTER SET gb2312 */|
+----------+--------------------------------------------------------+
1 row in set (0.00 sec)
四、?DROP DATABASE?或?DROP SCHEMA? ?刪除數(shù)據(jù)庫的相關(guān)參數(shù)
語法格式為:
DROP DATABASE [ IF EXISTS ] <數(shù)據(jù)庫名>
語法說明如下:
<數(shù)據(jù)庫名>:指定要?jiǎng)h除的數(shù)據(jù)庫名。
IF EXISTS:用于防止當(dāng)數(shù)據(jù)庫不存在時(shí)發(fā)生錯(cuò)誤。
DROP DATABASE:刪除數(shù)據(jù)庫中的所有表格并同時(shí)刪除數(shù)據(jù)庫。使用此語句時(shí)要非常小心,以免錯(cuò)誤刪除。如果要使用 DROP DATABASE,需要獲得數(shù)據(jù)庫 DROP 權(quán)限。
注意:MySQL 安裝后,系統(tǒng)會(huì)自動(dòng)創(chuàng)建名為 information_schema 和 mysql 的兩個(gè)系統(tǒng)數(shù)據(jù)庫,系統(tǒng)數(shù)據(jù)庫存放一些和數(shù)據(jù)庫相關(guān)的信息,如果刪除了這兩個(gè)數(shù)據(jù)庫,MySQL 將不能正常工作。
MySQL刪除數(shù)據(jù)庫實(shí)例
下面在 MySQL 中創(chuàng)建一個(gè)測(cè)試數(shù)據(jù)庫 test_db_del。
mysql> CREATE DATABASE test_db_del;
Query OK, 1 row affected (0.08 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| sakila? ? ? ? ? ? |
| sys? ? ? ? ? ? ? ? |
| test_db? ? ? ? ? ? |
| test_db_char? ? ? |
| test_db_del? ? ? ? |
| world? ? ? ? ? ? ? |
+--------------------+
9 rows in set (0.00 sec)
使用命令行工具將數(shù)據(jù)庫 test_db_del 從數(shù)據(jù)庫列表中刪除,輸入的 SQL 語句與執(zhí)行結(jié)果如下所示:
mysql> DROP DATABASE test_db_del;
Query OK, 0 rows affected (0.57 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database? ? ? ? ? |
+--------------------+
| information_schema |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| sakila? ? ? ? ? ? |
| sys? ? ? ? ? ? ? ? |
| test_db? ? ? ? ? ? |
| test_db_char? ? ? |
| world? ? ? ? ? ? ? |
+--------------------+
8 rows in set (0.00 sec)
此時(shí)數(shù)據(jù)庫 test_db_del 不存在。再次執(zhí)行相同的命令,直接使用 DROP DATABASE test_db_del,系統(tǒng)會(huì)報(bào)錯(cuò),如下所示:
mysql> DROP DATABASE test_db_del;
ERROR 1008 (HY000): Can't drop database 'test_db_del'; database doesn't exist
如果使用IF EXISTS從句,可以防止系統(tǒng)報(bào)此類錯(cuò)誤,如下所示:
mysql> DROP DATABASE IF EXISTS test_db_del;
Query OK, 0 rows affected, 1 warning (0.00 sec)
五、USE? ?完成一個(gè)數(shù)據(jù)庫到另一個(gè)數(shù)據(jù)庫的跳轉(zhuǎn)。
當(dāng)用 CREATE DATABASE 語句創(chuàng)建數(shù)據(jù)庫之后,該數(shù)據(jù)庫不會(huì)自動(dòng)成為當(dāng)前數(shù)據(jù)庫,需要用 USE 來指定當(dāng)前數(shù)據(jù)庫。其語法格式為:
USE <數(shù)據(jù)庫名>
該語句可以通知 MySQL 把<數(shù)據(jù)庫名>所指示的數(shù)據(jù)庫作為當(dāng)前數(shù)據(jù)庫。該數(shù)據(jù)庫保持為默認(rèn)數(shù)據(jù)庫,直到語段的結(jié)尾,或者直到遇見一個(gè)不同的 USE 語句。
只有使用 USE 語句來指定某個(gè)數(shù)據(jù)庫作為當(dāng)前數(shù)據(jù)庫之后,才能對(duì)該數(shù)據(jù)庫及其存儲(chǔ)的數(shù)據(jù)對(duì)象執(zhí)行操作。
【實(shí)例】使用命令行工具將數(shù)據(jù)庫 test_db 設(shè)置為默認(rèn)數(shù)據(jù)庫,輸入的 SQL 語句與執(zhí)行結(jié)果如下所示:
mysql> USE test_db;
Database changed