數(shù)據(jù)庫操作
6.1 數(shù)據(jù)庫操作
創(chuàng)建數(shù)據(jù)庫
示例:
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í)行的時間。
如果已經(jīng)存在這個數(shù)據(jù)庫,系統(tǒng)會?示:
mysql> create database liwenkai; ERROR 1007 (HY000): Can't create database 'liwenkai'; database exists
查看數(shù)據(jù)庫
注意:
show是指顯示
database 是指數(shù)據(jù)庫
databases 是數(shù)據(jù)庫的復(fù)數(shù)形式,指全部數(shù)據(jù)庫。
示例:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | php | | test | +--------------------+ 5 rows in set (0.00 sec)
選中數(shù)據(jù)庫
使用數(shù)據(jù)庫liwenkai
注意:
use 是指使用;
庫名 是存在當(dāng)前數(shù)據(jù)庫系統(tǒng)中的具體的數(shù)據(jù)庫的名稱;
示例:
mysql> use php; Database changed
這樣就進(jìn)入到了 php 數(shù)據(jù)庫中了。當(dāng)然你可以使用 use 語句隨時切換要操作的數(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)入到庫后我們可以看這個庫里面有多少個數(shù)據(jù)表。
使用use 進(jìn)入到某個數(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ù)庫,數(shù)據(jù)庫的名字為php
注意:
drop 是漢語可以翻譯為指掉下來,不要了的意思
database 是指庫
庫名 是指要刪掉的庫的名稱
示例:
mysql> drop database liwenkai; Query OK, 0 rows affected (0.01 sec)
【切記】注:數(shù)據(jù)庫刪除后,下面的所有數(shù)據(jù)都會全部刪除,所以刪除前一定要慎重并做好相應(yīng)的備份。