查看數(shù)據(jù)庫
使用show databases;
可查看所有的數(shù)據(jù)庫。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.03 sec)
使用show create database 數(shù)據(jù)庫名;
可查看某一數(shù)據(jù)庫的創(chuàng)建信息约啊。
mysql> show create database mysql;
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| mysql | CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
使用select database();
可查看當前所在的數(shù)據(jù)庫庸娱。
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
使用show variables like 'character%';
可查看數(shù)據(jù)庫編碼集邻辉。
mysql> show variables like 'character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)
創(chuàng)建數(shù)據(jù)庫
使用create database 數(shù)據(jù)庫名 charset 字符編碼;
可創(chuàng)建數(shù)據(jù)庫。
命名規(guī)則:
- 數(shù)字、字母残邀、下劃線,
@
、#
芥挣、$
驱闷。 - 庫名區(qū)分字母大小寫。
- 不能使用特殊字符和mysql關鍵字空免,不能單獨使用數(shù)字空另,最長128位。
mysql> create database students charset utf8;
Query OK, 1 row affected, 1 warning (0.05 sec)
切換數(shù)據(jù)庫
使用use 數(shù)據(jù)庫名;
可進入指定的數(shù)據(jù)庫蹋砚。
mysql> use students;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| students |
+------------+
1 row in set (0.00 sec)
修改數(shù)據(jù)庫
使用alter database 數(shù)據(jù)庫名 charset 字符編碼;
mysql> show create database students;
+----------+------------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------------------------------------------+
| students | CREATE DATABASE `students` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter database students charset=gbk;
Query OK, 1 row affected (0.01 sec)
mysql> show create database students;
+----------+-----------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------------------------------------------+
| students | CREATE DATABASE `students` /*!40100 DEFAULT CHARACTER SET gbk */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
刪除數(shù)據(jù)庫
使用drop database 數(shù)據(jù)庫名;
可對數(shù)據(jù)庫進行刪除操作扼菠。
為了防止刪除不存在的數(shù)據(jù)庫報錯可以使用if
判斷要執(zhí)行刪除的數(shù)據(jù)庫是否存在,drop database if exists 數(shù)據(jù)庫名;
mysql> drop database if exists students;
Query OK, 0 rows affected (0.09 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
系統(tǒng)庫
MySQL
在初始化后會創(chuàng)建一些系統(tǒng)庫坝咐,不同的系統(tǒng)庫有不同的用處循榆。
information_schema
: 虛擬庫,不占用磁盤空間墨坚,存儲的是數(shù)據(jù)庫啟動后的一些參數(shù)秧饮,如用戶表信息、列信息泽篮、權限信息盗尸、字符信息等。
performance_schema
: MySQL5.5
開始新增一個數(shù)據(jù)庫:主要用于收集數(shù)據(jù)庫服務器性能參數(shù)帽撑,記錄處理查詢請求時發(fā)生的各種事件泼各、鎖等現(xiàn)象。
mysql
: 授權庫油狂,主要存儲系統(tǒng)用戶的權限信息
sys
:系統(tǒng)庫使用基礎環(huán)境历恐。