👻
🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈
👻
tips : 由于MySQL不區分大小寫,為增加可讀性,故本文一律采用小寫
1. 顯示當前資料庫
語法:show databases;
👻tips : 不要忘記加末尾的" ; " ,由于MySQL支持多行輸入,所以一個陳述句的結束需要用 " ; "表示
mysql> show databases;
運行效果如下,則表示正常的運行
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
👻 那我們不小心少輸入一個字母會怎么樣?
mysql> show database;
👻運行效果:
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 'database' at line 1
翻譯過來就是:你當前有一個SQL陳述句錯誤,檢查與MySQL服務器版本對應的手冊,以了解第1行“database”附近使用的正確語法
2. 創建資料庫
👻 簡單語法:create database [ if not exists ] [ 資料庫名 ];
資料庫名有字母(a ~ z)和數字(0~9)和下劃線( _ )組成,開頭必須是字母或者下劃線
mysql> create database if not exists mydatabase;
👻運行效果:
mysql> create database if not exists mydatabase;
Query OK, 1 row affected (0.00 sec)
👻再使用 show databases; 顯示當前的資料庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydatabase |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
資料庫會多出一行 ---- mydatabase,則表明建立資料庫成功
👻tips : if not exists 可以省略不寫
mysql> create database mydatabase1;
Query OK, 1 row affected (0.00 sec)
👻但當資料庫出現重名的時候會報錯,加上 if not exists 則不會報錯,if not exists 的作用是:如果資料庫存在,則不會建立資料庫,如果資料庫不存在,則建立資料庫,
mysql> create database mydatabase;
ERROR 1007 (HY000): Can't create database 'mydatabase'; database exists
👻完整語法:ceate database [if not exists] db_name [create_specification [, create_specification] ...]
create_specification:
[default] character set charset_name [default] collate collation_name
character set : 指定資料庫采用的字符集,默認就行
collate : 指定資料庫字符集的校驗規則
3. 洗掉資料庫
👻語法:drop database if exists [資料庫名]
mysql> drop database mydatabase;
Query OK, 0 rows affected (0.00 sec)
if exists 也可以省略不寫,但如果資料庫中沒有你要洗掉的資料庫時,則會報錯,
mysql> drop database i123;
ERROR 1008 (HY000): Can't drop database 'i123'; database doesn't exist
👻再次使用 show databases; 顯示當前的資料庫,可以發現剛剛建立的mydatabase資料庫已經消失
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
👻在作業中洗掉資料庫是一個危險的操作,請問嘗試!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302747.html
標籤:其他
