Mysql數(shù)據(jù)庫操作
創(chuàng)建數(shù)據(jù)庫
類別 | 詳細(xì)解示 |
---|---|
基本語法 | CREATE DATABASE 數(shù)據(jù)庫名; |
示例 | CREATE DATABASE PHP; |
示例說明 | 創(chuàng)建一個(gè)數(shù)庫,數(shù)據(jù)庫的名字為PHP |
示例:
mysql> CREATE DATABASE PHP;
?Query OK, 1 row affected (0.00 sec)
“Query OK” 表示上面的命令執(zhí)行成功,所有的 DDL 和 DML(不包 括 SELECT)操作執(zhí)行成功后都顯示“Query OK”,這里理解為執(zhí)行成功就可以了;“1 row affected” 表示操作只影響了數(shù)據(jù)庫中一行的記錄,“0.00 sec”則記錄了操作執(zhí)行的時(shí)間。
如果已經(jīng)存在這個(gè)數(shù)據(jù)庫,系統(tǒng)會(huì)?示:
mysql> CREATE DATABASE PHP;
?ERROR 1007 (HY000): Can't create database 'PHP'; database exists
查看數(shù)據(jù)庫
基本語法:
類別 | 詳細(xì)解示 |
---|---|
基本語法 | show databases; |
示例說明 | 顯示當(dāng)前服務(wù)器的所有數(shù)據(jù)庫 |
注意:
show是指顯示
database 是指數(shù)據(jù)庫
databases 是數(shù)據(jù)庫的復(fù)數(shù)形式,指全部數(shù)據(jù)庫。
示例:
mysql> SHOW DATABASES;
?+--------------------+
?| Database ? ? ? ? ? |
?+--------------------+
?| information_schema |
?| mysql ? ? ? ? ? ? ?|
?| performance_schema |
?| user ? ? ? ? ? ? ? |
?+--------------------+
?4 rows in set (0.00 sec)
選中數(shù)據(jù)庫
基本語法:
類別 | 詳細(xì)解示 |
---|---|
基本語法 | use 庫名; |
示例 | use PHP |
示例說明 | 使用數(shù)據(jù)庫PHP |
注意:
use 是指使用;
庫名 是存在當(dāng)前數(shù)據(jù)庫系統(tǒng)中的具體的數(shù)據(jù)庫的名稱;
示例:
mysql> use PHP;
?Database changed
這樣就進(jìn)入到了 PHP 數(shù)據(jù)庫中了。當(dāng)然你可以使用 use 語句隨時(shí)切換要操作的數(shù)據(jù)庫,剛剛選中了PHP ,現(xiàn)在我們切換到mysql內(nèi)容的 mysql 數(shù)據(jù)庫看看:
mysql> use mysql;
?Reading table information for completion of table and column names
?You can turn off this feature to get a quicker startup with -A
?Database changed
出現(xiàn) ” Database changed“ 表示切換成功。然后,看看 mysql數(shù)據(jù)庫里面有什么內(nèi)容(和查看當(dāng)前數(shù)據(jù)庫服務(wù)器數(shù)據(jù)庫一樣使用 show 語句)
查看數(shù)據(jù)庫中的表
進(jìn)入到庫后我們可以看這個(gè)庫里面有多少個(gè)數(shù)據(jù)表。
類別 | 詳細(xì)解示 |
---|---|
基本語法 | show tables; |
示例說明 | 顯示當(dāng)前數(shù)據(jù)庫下所有的表 |
使用use 進(jìn)入到某個(gè)數(shù)據(jù)庫后可以使用show tables
示例,查看當(dāng)前數(shù)據(jù)庫的表:
mysql> show tables;
?+---------------------------+
?| Tables_in_mysql ? ? ? ? ? |
?+---------------------------+
?| columns_priv ? ? ? ? ? ? ?|
?| db ? ? ? ? ? ? ? ? ? ? ? ?|
?| event ? ? ? ? ? ? ? ? ? ? |
?| func ? ? ? ? ? ? ? ? ? ? ?|
?| general_log ? ? ? ? ? ? ? |
?| help_category ? ? ? ? ? ? |
?| help_keyword ? ? ? ? ? ? ?|
?| help_relation ? ? ? ? ? ? |
?| help_topic ? ? ? ? ? ? ? ?|
?| innodb_index_stats ? ? ? ?|
?| innodb_table_stats ? ? ? ?|
?| ndb_binlog_index ? ? ? ? ?|
?| plugin ? ? ? ? ? ? ? ? ? ?|
?| proc ? ? ? ? ? ? ? ? ? ? ?|
?| procs_priv ? ? ? ? ? ? ? ?|
?| proxies_priv ? ? ? ? ? ? ?|
?| servers ? ? ? ? ? ? ? ? ? |
?| slave_master_info ? ? ? ? |
?| slave_relay_log_info ? ? ?|
?| slave_worker_info ? ? ? ? |
?| slow_log ? ? ? ? ? ? ? ? ?|
?| tables_priv ? ? ? ? ? ? ? |
?| time_zone ? ? ? ? ? ? ? ? |
?| time_zone_leap_second ? ? |
?| time_zone_name ? ? ? ? ? ?|
?| time_zone_transition ? ? ?|
?| time_zone_transition_type |
?| user ? ? ? ? ? ? ? ? ? ? ?|
?+---------------------------+
?28 rows in set (0.00 sec)
這些表里面的內(nèi)容是關(guān)系數(shù)據(jù)庫服務(wù)器相關(guān)的用戶、權(quán)限、數(shù)據(jù)庫狀態(tài)、設(shè)置等相關(guān)的信息數(shù)據(jù)。
刪除數(shù)據(jù)庫
類別 | 詳細(xì)解示 |
---|---|
基本語法 | DROP DATABASE 庫名; |
示例 | DROP DATABASE? PHP; |
示例說明 | 刪除一個(gè)數(shù)庫,數(shù)據(jù)庫的名字為liwenkai |
注意:
drop 是漢語可以翻譯為指掉下來,不要了的意思
database 是指庫
庫名 是指要?jiǎng)h掉的庫的名稱
示例:
mysql> DROP DATABASE? PHP;
?Query OK, 0 rows affected (0.01 sec)
【切記】注:數(shù)據(jù)庫刪除后,下面的所有數(shù)據(jù)都會(huì)全部刪除,所以刪除前一定要慎重并做好相應(yīng)的備份。(若重要數(shù)據(jù)未備份,而實(shí)際中產(chǎn)生的數(shù)據(jù)風(fēng)險(xiǎn)與本書無關(guān)。)