MySQL基本操作{記錄1}{檢視,選擇,建立,修改,刪除}
阿新 • • 發佈:2022-03-23
1,啟動MySQL
終端輸入
mysql -u root -p
然後輸入密碼,啟動成功
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
2,基本操作
2.1檢視MySQL下的所有資料庫
SHOW DATABASES;
2.2選擇要操作的資料庫:use 資料庫名;
比如我們要使用資料庫work
use work;
mysql> use work; Reading table information for completion of table and column names You can turn off this feature toget a quicker startup with -A Database changed
此後我們的所有操作都針對work
2.3建立資料庫:使用create命令
CREATE DATABASE 資料庫名;
例如我們要建立一個名為work的資料庫
CREATE DATABASE work;
如果work資料庫不存在則建立,如果存在則不會建立
work已存在,顯示如下資訊
mysql> CREATE DATABASE work; ERROR 1007 (HY000): Can't create database 'work'; database exists
2.4檢視某個資料庫 使用LIKE從句,他用於匹配指定的資料庫名稱
SHOW DATABASES LIKE '資料庫名';
例如我們要檢視資料庫work
SHOW DATABASES LIKE 'work';
mysql> SHOW DATABASES LIKE 'work'; +-----------------+ | Database (work) | +-----------------+ | work | +-----------------+ 1 row in set (0.00 sec)
2.4檢視某資料庫的資訊(定義宣告)
SHOW CREATE DATABASE 資料庫名;
檢視work的資訊(定義宣告)
SHOW CREATE DATABASE work;
mysql> SHOW CREATE DATABASE work; +----------+--------------------------------------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------------------------------------------------------------------------+ | work | CREATE DATABASE `work` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ | +----------+--------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
2.5修改資料庫
ALTER DATABASE 資料庫名
終端下:
mysql> ALTER DATABASE 資料庫名 -> DEFAULT CHARACTER SET 字符集名 -> DEFAULT COLLATE 校對規則名;
對work資料庫進行修改
檢視:
SHOW CREATE DATABASE work; +----------+--------------------------------------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------------------------------------------------------------------------+ | work | CREATE DATABASE `work` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ | +----------+--------------------------------------------------------------------------------------------------------------------------------+
修改:
mysql> ALTER DATABASE work -> DEFAULT CHARACTER SET gb2312 -> DEFAULT COLLATE gb2312_chinese_ci; Query OK, 1 row affected (0.02 sec)
再次檢視:修改成功
mysql> SHOW CREATE DATABASE work; +----------+----------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------------------------------------------+ | work | CREATE DATABASE `work` /*!40100 DEFAULT CHARACTER SET gb2312 */ /*!80016 DEFAULT ENCRYPTION='N' */ | +----------+----------------------------------------------------------------------------------------------------+
2.6刪除資料庫,使用DORP命令(謹慎使用)不能刪除MySQL安裝時自己建立的資料庫
命令格式:
DROP DATABASE 資料庫名;
例:刪除work_tb1
DROP DATABASE work_tb1;
mysql> DROP DATABASE work_tb1; Query OK, 0 rows affected (0.02 sec)