在mysqk中創(chuàng)建一個(gè)數(shù)據(jù)庫直接使用CREATE DATABASE就行了,選擇數(shù)據(jù)庫使用use databasename而刪除數(shù)據(jù)庫使用drop命令就可以了,下面我來一一詳細(xì)介紹它們的用法.
創(chuàng)建數(shù)據(jù)庫:MySQL的任何事情都是以數(shù)據(jù)庫開始的,數(shù)據(jù)庫我們可以理解為"書架",表則可以理解為"書架上的書",,而表中的數(shù)據(jù)則可以理解為"書中的內(nèi)容",也就是說數(shù)據(jù)庫是容器。當(dāng)我們輸入完用戶名密碼連接到MySQL后,可以使用CREATE DATABASE命令來創(chuàng)建一個(gè)新的MySQL數(shù)據(jù)庫,代碼如下:
- create database xiaoxiaozi;
- /*
- Query OK, 1 row affected (0.06 sec)
- */
這樣就創(chuàng)建了一個(gè)數(shù)據(jù)庫,數(shù)據(jù)庫名為"xiaoxiaozi",在文件系統(tǒng)中,MySQL的數(shù)據(jù)存儲區(qū)將以目錄方式表示MySQL數(shù)據(jù)庫,也就是說其實(shí)數(shù)據(jù)庫在文件系統(tǒng)中的表現(xiàn)為"文件夾",所以說我們在給數(shù)據(jù)庫命名的時(shí)候一定要小心,再小心,其命名規(guī)范與操作系統(tǒng)的紅豆目錄名字的規(guī)范相一致.
例如:在Windows系統(tǒng)中不允許文件和目錄名中有",/,:,*,?,<,>,|"這些字符,在MySQL數(shù)據(jù)庫名字中這些字母會被自動刪除,且數(shù)據(jù)庫的名字不能太長(不能超過64個(gè)字符),一般除非故意搞破壞,沒有人建這么長名字的數(shù)據(jù)庫,也不好記不是,且包含特殊字符的名字或者是全部由數(shù)字或保留字組成的名字必須用反引號包起來,代碼如下:
- create database xiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozi;
- /*
- 數(shù)據(jù)庫名過長
- ERROR 1102 (42000): Incorrect database name 'xiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozi'
- */
- create database 123456;
- /*
- 數(shù)據(jù)庫名為純數(shù)字,需要用反引號包起來
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '123456' at line 1
- */
- create database `123456`;
- /*
- 正確創(chuàng)建數(shù)據(jù)庫名,用反引號將純數(shù)字包起來
- Query OK, 1 row affected (0.00 sec)
- */
且數(shù)據(jù)庫的名字不能相同,如果創(chuàng)建一個(gè)庫名與現(xiàn)有數(shù)據(jù)庫名重復(fù),系統(tǒng)會提示該數(shù)據(jù)庫已經(jīng)存在,創(chuàng)建失敗,代碼如下:
- create database `123456`;
- /*
- 第一次創(chuàng)建,創(chuàng)建成功
- Query OK, 1 row affected (0.01 sec)
- */
- create database `123456`;
- /*
- 第二次創(chuàng)建,創(chuàng)建失敗
- ERROR 1007 (HY000): Can't create database '123456'; database exists
- */
那我們創(chuàng)建數(shù)據(jù)庫的時(shí)候,怎么樣避免庫名已經(jīng)存在這個(gè)錯(cuò)誤信息呢?有兩種方式.
•在創(chuàng)建數(shù)據(jù)庫之前先用 show databases;語句查詢現(xiàn)有數(shù)據(jù)庫名都有哪些,避免建立失敗。
•在創(chuàng)建數(shù)據(jù)庫時(shí),,使用if not exists語句指明,只有當(dāng)數(shù)據(jù)庫不存在時(shí)才創(chuàng)建,代碼如下:
- /*if not exists*/
- create database `123456`;
- /*
- 第一次創(chuàng)建數(shù)據(jù)庫,成功
- Query OK, 1 row affected (0.00 sec)
- */
- create database if not exists `123456`;
- /*
- 如果數(shù)據(jù)庫不存在才創(chuàng)建,執(zhí)行SQL成功,有一個(gè)警告
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- */
- /*show databases示例*/
- show databases;
- /*
- +------------+
- | Database |
- +------------+
- | 123456 |
- | log |
- | manager |
- | mysql |
- | qhcms |
- | test |
- | xiaoxiaozi |
- +------------+
- 7 rows in set (0.00 sec)
- */
選擇需要的數(shù)據(jù)庫,使用USE語句將會選擇一個(gè)數(shù)據(jù)庫,使它成為所有事務(wù)的當(dāng)前數(shù)據(jù)庫,代碼如下:
- use xiaoxiaozi;
- /*
- 提示數(shù)據(jù)庫已改變
- Database changed
- */
同時(shí),我們也可以在查詢表的時(shí)候再告訴MySQL我們這些表是哪些數(shù)據(jù)庫里的表,代碼如下:
- select Host, Db, User From mysql.db;
- /*
- 指明,在mysql數(shù)據(jù)庫的db表中查詢Host,Db,User字段
- +----------------------------+---------+-------+
- | Host | Db | User |
- +----------------------------+---------+-------+
- | % | test | |
- | % | test_% | |
- | 192.168.0.133 | qhcms | cms |
- | m.survivalescaperooms.com | qhcms | cms |
- | localhost | log | log |
- | localhost | manager | mambo |
- | localhost | qhcms | cms |
- +----------------------------+---------+-------+
- 7 rows in set (0.05 sec)
- */
上面這條語句,與如下兩條語句組合的作用是相同的,代碼如下:
- use mysql;
- /*
- 選擇mysql數(shù)據(jù)庫
- Database changed
- */
- select Host, Db, User From db;
- /*
- 在db表里面選擇Host, Db, User字段
- +----------------------------+---------+-------+
- | Host | Db | User |
- +----------------------------+---------+-------+
- | % | test | |
- | % | test_% | |
- | 192.168.0.133 | qhcms | cms |
- | m.survivalescaperooms.com | qhcms | cms |
- | localhost | log | log |
- | localhost | manager | mambo |
- | localhost | qhcms | cms |
- +----------------------------+---------+-------+
- 7 rows in set (0.00 sec)
- */
刪除數(shù)據(jù)庫:這是一個(gè)危險(xiǎn)的動作,如果大家要使用的話,一定要先確認(rèn)這個(gè)數(shù)據(jù)庫是自己的,并且是自己真的想刪的,因?yàn)橐坏﹦h除數(shù)據(jù)庫,是連其內(nèi)部的表數(shù)據(jù)一起刪除的.一旦刪除就真的沒了,所以一定要小心,再小心.
其實(shí)刪除數(shù)據(jù)庫的命令很是簡單 drop database database_name; 不過要刪一個(gè)數(shù)據(jù)庫,如果其不存在的話,系統(tǒng)是會報(bào)錯(cuò)的,這個(gè)時(shí)候我們可以借助if exists來判斷一下數(shù)據(jù)庫是否存在,代碼如下:
- drop database xiaoxiaozi;
- /*
- 第一次刪除xiaoxiaozi數(shù)據(jù)庫
- Query OK, 0 rows affected (0.00 sec)
- */
- drop database xiaoxiaozi;
- /*
- 第二次刪除失敗,因?yàn)榇藭r(shí)該數(shù)據(jù)庫已經(jīng)不存在了
- ERROR 1008 (HY000): Can't drop database 'xiaoxiaozi'; database doesn't exist
- */
- drop database if exists xiaoxiaozi;
- /*
- 刪除的時(shí)候先判斷存不存在,如果不存在執(zhí)行刪除
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- */
新聞熱點(diǎn)
疑難解答
圖片精選