Mariadb基礎
關系型資料庫介紹
資料結構模型
- 資料結構模型主要有:
- 層次模型
- 網狀模型
- 關系模型
- 二維關系:row(行),column(列)
- 資料庫管理系統:DBMS(DataBase Management System)
- 關系型資料庫管理系統:RDBMS(Relational DataBase Management System)
注: 資料庫管理系統是由DateBase服務+DateBase+table+row、column組成,這些東西可以存在多個,而不是只能存在一個
RDBMS專業名詞
-
常見的關系型資料庫管理系統
- MySQL:MySQL,MariaDB,Percona-Server
- PostgreSQL:簡稱為pgsql
- Oracle
- MSSQL:Windows平臺的資料庫
-
SQL:Structure Query Language,結構化查詢語言
-
約束:constraint,向資料表提供的資料要遵守的限制
- 主鍵約束:一個或多個欄位的組合,填入的資料必須能在本表中唯一標識本行,且必須提供資料,不能為空(NOT NULL),
- 一個表中只能存在一個
- 惟一鍵約束:一個或多個欄位的組合,填入的資料必須能在本表中唯一標識本行,允許為空(NULL)
- 一個表可以存在多個
- 外鍵約束:一個表中的某欄位可填入資料取決于另一個表的主鍵已有的資料
- 檢查性約束
- 主鍵約束:一個或多個欄位的組合,填入的資料必須能在本表中唯一標識本行,且必須提供資料,不能為空(NOT NULL),
-
索引:將表中的一個或多個欄位中的資料復制一份另存,并且這些資料需要按特定次序排序存盤
關系型資料庫的常見組件
- 資料庫:database
- 表:table,由row和列column組成
- 索引:index
- 視圖:view
- 用戶:user
- 權限:privilege
- 存盤程序:procedure
- 存盤函式:function
- 觸發器:trigger
- 事件調度器:event scheduler
SQL陳述句
- SQL陳述句分為三類
- DDL:Data Defination Language,資料定義語言
- DML:Data Manipulation Language,資料操縱語言
- DCL:Data Control Language,資料控制語言
| SQL陳述句型別 | 對應操作 |
|---|---|
| DDL | CREATE:創建 DROP:洗掉 ALTER:修改 |
| DML | INSERT:向表中插入資料 DELETE:洗掉表中資料 UPDATE:更新表中資料 SELECT:查詢表中資料 |
| DCL | GRANT:授權 REVOKE:移除授權 |
MariaDB安裝與配置
MariaDB安裝
- MariaDB和MySQL操作語法是通用的,但是現在MySQL是收費的,所以這里我們安裝MariaDB
- MariaDB安裝的方式有三種:
- 源代碼:編譯安裝
- 二進制格式的程式包:展開至特定路徑,并經過簡單配置后即可使用
- 程式包管理器管理的程式包
//在Redhat 8中本地源中已經存在mariadb安裝包,所以可以直接使用yum命令安裝
[root@server ~]# yum -y install mariadb mariadb-common mariadb-devel mariadb-server
- 在Redhat 7 中要安裝MySQL
#配置mysql的yum源
wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm \
http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm
#安裝mysql5.7
yum -y install mysql-community-server mysql-community-client \
mysql-community-common mysql-community-devel
Mariadb配置
//啟動資料庫
[root@server ~]# systemctl enable --now mariadb
//確保3306埠已經處于監聽狀態
[root@server ~]# ss -antl|grep 3306
LISTEN 0 80 *:3306 *:*
//登錄mariadb資料庫
[root@server ~]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
//修改Mariadb登錄密碼
MariaDB [(none)]> set password = password('redhat123+');
//重新使用密碼登,以下兩種方法都可以登錄
[root@server ~]# mysql -uroot -p'redhat123+'
[root@server ~]# mysql -uroot -p
Enter password:
Mariadb的程式組成
- 客戶端
- mysql:CLI互動式客戶端程式
- mysql_secure_installation:安全初始化
- mysqldump:mysql備份工具
- mysqladmin:mysql備份工具
- 服務端
- Mariadb
Mariadb工具使用
-
語法:
mysql [OPTIONS] [database] -
常用選項:
- -uUSERNAME #指定用戶名,默認為root
- -hHOST #指定服務器主機,默認為localhost,推薦使用ip地址
- -pPASSWORD #指定用戶的密碼
[root@server ~]# mysql -uroot -p'redhat123+' -h127.0.0.1 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.3.17-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>- -P# #指定資料庫監聽的埠,這里的#需用實際的埠號代替,如-P3307
- -V #查看當前使用的mysql版本
[root@server ~]# mysql -V mysql Ver 15.1 Distrib 10.3.17-MariaDB, for Linux (x86_64) using readline 5.1- -e #不登錄mysql執行sql陳述句后退出,常用于腳本
[root@server ~]# mysql -uroot -p'redhat123+' -e 'SHOW DATABASES;' +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+
服務端監聽的兩種socket地址
| socket | 說明 |
|---|---|
| IP socket | 默認監聽在tcp的3306埠,支持遠程通信 |
| Unix socket | 監聽在sock檔案上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock) 僅支持本地通信 server地址只能是:localhost,127.0.0.1 |
Mariadb資料庫操作
DDL操作
資料庫操作
- 創建資料庫
//語法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS CWT; #如果CWT資料庫不存在則創建CWT資料庫,這樣做的好處是,如果要創建的資料庫存在也不會報錯
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| CWT |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
- 洗掉資料庫
//語法:DROP DATABASE [IF EXISTS] 'DB_NAME';
MariaDB [(none)]> DROP DATABASE IF EXISTS CWT; #如果CWT資料庫存在則洗掉它,這樣做的好處是,如果要洗掉的資料庫不存在也不會報錯
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
表操作
- 創建表
//語法:CREATE TABLE table_name (col1 datatype 修飾符,col2 datatype 修飾符) ENGINE='存盤引擎型別';
MariaDB [(none)]> CREATE DATABASE XX_DATA;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| XX_DATA |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
//進入資料庫
MariaDB [(none)]> USE XX_DATA
Database changed
MariaDB [XX_DATA]> SHOW TABLES;
//創建表
MariaDB [XX_DATA]> CREATE TABLE xx(id int not null,name varchar(10),age tinyint);
Query OK, 0 rows affected (0.004 sec)
//查看表
MariaDB [XX_DATA]> SHOW TABLES;
+-------------------+
| Tables_in_XX_DATA |
+-------------------+
| xx |
+-------------------+
1 row in set (0.001 sec)
- 洗掉表
MariaDB [XX_DATA]> DROP TABLE xx;
Query OK, 0 rows affected (0.003 sec)
MariaDB [XX_DATA]> SHOW TABLES;
Empty set (0.000 sec)
用戶操作
- mysql用戶帳號由兩部分組成,如
'USERNAME'@'HOST',表示此USERNAME只能從此HOST上遠程登錄,HOST的值可以為:- IP地址,如:192.168.86.132
- 通配符
- %:匹配任意長度的任意字符,常用于設定允許從任何主機登錄
- _:匹配任意單個字符
注: HOST應該為客戶端的IP地址
//創建用于登錄資料庫的用戶
語法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
MariaDB [(none)]> CREATE USER 'tom'@'192.168.86.132' IDENTIFIED BY 'redhat456+';
Query OK, 0 rows affected (0.000 sec)
//使用新創建的用戶和密碼在客戶端上登錄
[root@client ~]# mysql -utom -p'redhat456+' -h192.168.86.129
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
//洗掉資料庫用戶
語法:DROP USER 'username'@'host';
MariaDB [(none)]> DROP USER 'tom'@'192.168.86.132';
Query OK, 0 rows affected (0.000 sec)
查看命令SHOW
- 查看支持的所有字符集(支持的語言)
MariaDB [(none)]> SHOW CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
...
- 查看當前資料庫支持的所有存盤引擎
MariaDB [(none)]> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| MRG_MyISAM | YES | Collection of identical MyISAM tables ...
//上面那種方法輸出的內容比較難以閱讀,所以推薦使用以下這種方式
MariaDB [(none)]> SHOW ENGINES\G
*************************** 1. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 2. row ***************************
Engine: MRG_MyISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
...
- 查看資料庫資訊
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| XX_DATA |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.116 sec)
//不進入某資料庫而列出其包含的所有表
MariaDB [(none)]> SHOW TABLES FROM XX_DATA;
Empty set (0.000 sec)
- 查看表結構
//語法:DESC [db_name.]table_name;
MariaDB [(none)]> DESC XX_DATA.xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
//如果已經進入的資料庫,則只需要DESC xx;就能查看表結構了
- 查看某表的創建命令
//語法:CREATE TABLE table_name;
MariaDB [XX_DATA]> SHOW CREATE TABLE xx;
...
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| xx | CREATE TABLE `xx` (
`id` int(11) NOT NULL,
`name` varchar(15) DEFAULT NULL,
`age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
...
- 查看某表的狀態
//語法:SHOW TABLE STATUS LIKE 'table_name'\G
MariaDB [XX_DATA]> SHOW TABLE STATUS LIKE 'xx'\G #支持通配符,如:SHOW TABLE STATUS LIKE 'xx%'\G
*************************** 1. row ***************************
Name: xx
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
...
- 添加欄位
MariaDB [XX_DATA]> DESC xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.000 sec)
MariaDB [XX_DATA]> ALTER TABLE xx ADD score float;
Query OK, 0 rows affected (0.002 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [XX_DATA]> DESC xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.000 sec)
- 洗掉欄位
MariaDB [XX_DATA]> DESC xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.000 sec)
MariaDB [XX_DATA]> ALTER TABLE xx DROP score;
Query OK, 0 rows affected (0.134 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [XX_DATA]> DESC xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
- 修改欄位
MariaDB [XX_DATA]> DESC xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
MariaDB [XX_DATA]> ALTER TABLE xx MODIFY age tinyint not null;
Query OK, 0 rows affected (0.007 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [XX_DATA]> DESC xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.000 sec)
獲取幫助
- 獲取命令使用幫助
//語法:HELP keyword;
MariaDB [XX_DATA]> HELP CREATE TABLE
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
...
DML操作
- DML操作包括增(INSERT)、刪(DELETE)、改(UPDATE)、查(SELECT),均屬針對表的操作,
INSERT陳述句
//語法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
MariaDB [XX_DATA]> DESC xx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(15) | YES | | NULL | |
| age | tinyint(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
//插入一條資料
MariaDB [XX_DATA]> INSERT INTO xx VALUE(1,'cwt',18);
Query OK, 1 row affected (0.001 sec)
//插入多條資料
MariaDB [XX_DATA]> INSERT INTO xx VALUES (2,'tom',20),(3,'xx',25),(4,'lisi',20);
Query OK, 3 rows affected (0.001 sec)
Records: 3 Duplicates: 0 Warnings: 0
SELECT陳述句
- 欄位column表示法
| 表示符 | 含義 |
|---|---|
| * | 所有欄位 |
| as | 欄位別名 |
- 條件判斷陳述句WHERE
| 操作型別 | 常用運算子 |
|---|---|
| 運算子 | >,<,>=,<=,=,!= BETWEEN column# AND column#:在兩個欄位之間 LIKE:模糊匹配 RLIKE:基于正則運算式進行模式匹配 IS NOT NULL:非空 IS NULL:空 |
| 條件邏輯操作 | AND:與 OR:或 NOT:非 |
- ORDER BY:排序,默認為升序(ASC)
| ORDER BY 陳述句 | 意義 |
|---|---|
| ORDER BY ‘column_name’ | 根據column_name進行升序排序 |
| ORDER BY ‘column_name’ DESC | 根據column_name進行降序排序 |
| ORDER BY ’column_name’ LIMIT 2 | 根據column_name進行升序排序,并只取前2個結果 |
| ORDER BY ‘column_name’ LIMIT 1,2 | 根據column_name進行升序排序,并且略過第1個結果取后面的2個結果 |
//語法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
//查看表中的所有資料
MariaDB [cwt]> SELECT * FROM student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhaangshan | 20 |
| 7 | lisi | 50 |
| 8 | chenshou | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
//查看表中指定的欄位的資料
MariaDB [cwt]> SELECT name FROM student;
+-------------+
| name |
+-------------+
| tom |
| jerry |
| wangqing |
| sean |
| zhangshan |
| zhaangshan |
| lisi |
| chenshou |
| wangwu |
| qiuyi |
| qiuxiaotian |
+-------------+
//定義別名
MariaDB [cwt]> SELECT id AS a,name AS b,age AS c FROM student;
+----+-------------+------+
| a | b | c |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
...
//查找年齡大于20的資料
MariaDB [cwt]> SELECT * FROM student WHERE age > 20;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+-----------+------+
//查找年齡在20到30歲之間的資料
MariaDB [cwt]> SELECT * FROM student WHERE age BETWEEN 20 AND 30;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhaangshan | 20 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
//查找名字以n結尾的資料
MariaDB [cwt]> SELECT * FROM student WHERE name LIKE '%n';
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhaangshan | 20 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
//使用正則運算式進行匹配
MariaDB [cwt]> SELECT * FROM student WHERE name RLIKE '^z.*';
+----+------------+------+
| id | name | age |
+----+------------+------+
| 5 | zhangshan | 26 |
| 6 | zhaangshan | 20 |
+----+------------+------+
//匹配年齡為空的資料
MariaDB [cwt]> SELECT * FROM student WHERE age is NULL;
+----+------+------+
| id | name | age |
+----+------+------+
| 12 | cwt | NULL |
+----+------+------+
//以age欄位進行升序排序,去除age為NULL的行
MariaDB [cwt]> SELECT * FROM student WHERE age is NOT NULL ORDER BY age;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 8 | chenshou | 10 |
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
| 6 | zhaangshan | 20 |
...
//以age欄位進行降序排序,去除age為NULL的行
MariaDB [cwt]> SELECT * FROM student WHERE age is NOT NULL ORDER BY age DESC;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 9 | wangwu | 100 |
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 3 | wangqing | 25 |
...
//以age欄位進行排序,并取前3個結果
MariaDB [cwt]> SELECT * FROM student WHERE age is NOT NULL ORDER BY age LIMIT 2;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 8 | chenshou | 10 |
| 10 | qiuyi | 15 |
//以age欄位進行排序,并忽略結果的第一個資料取后面的兩個資料
MariaDB [cwt]> SELECT * FROM student WHERE age is NOT NULL ORDER BY age LIMIT 1,2;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
GROUP BY陳述句
- 將結果集中的資料行根據選擇列的值進行邏輯分組,以便能匯總表內容的子集,實作對每個組而不是對整個結果集進行整合
//語法GROUP BY {col_name | expr | position} [ASC|DESC]
MariaDB [cwt]> SELECT * FROM info;
+----+-----------+------------+--------+
| id | name | department | salary |
+----+-----------+------------+--------+
| 1 | zhangshan | sales | 5000 |
| 2 | wangwu | dev | 20000 |
| 3 | lisi | sales | 15000 |
| 4 | cwt | dev | 15000 |
| 5 | tom | operation | 25000 |
| 6 | jerry | operation | 20000 |
+----+-----------+------------+--------+
//取出表中每個部門中工資最高的人
MariaDB [cwt]> SELECT name,department,MAX(salary) as salary FROM info GROUP BY department;
+-----------+------------+--------+
| name | department | salary |
+-----------+------------+--------+
| wangwu | dev | 20000 |
| tom | operation | 25000 |
| zhangshan | sales | 15000 |
+-----------+------------+--------+
UPDATE陳述句
//語法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
MariaDB [XX_DATA]> SELECT * FROM xx;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | cwt | 18 |
| 2 | tom | 20 |
| 3 | xx | 25 |
| 4 | lisi | 20 |
+----+------+-----+
4 rows in set (0.000 sec)
MariaDB [XX_DATA]> UPDATE xx SET age = 21 where name = 'cwt';
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [XX_DATA]> SELECT * FROM xx;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | cwt | 21 |
| 2 | tom | 20 |
| 3 | xx | 25 |
| 4 | lisi | 20 |
+----+------+-----+
4 rows in set (0.000 sec)
DELETE陳述句
//語法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
MariaDB [XX_DATA]> SELECT * FROM xx;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | cwt | 21 |
| 2 | tom | 20 |
| 3 | xx | 25 |
| 4 | lisi | 20 |
+----+------+-----+
4 rows in set (0.000 sec)
//洗掉表中的某一條記錄
MariaDB [XX_DATA]> DELETE FROM xx where name = 'xx';
Query OK, 1 row affected (0.002 sec)
MariaDB [XX_DATA]> SELECT * FROM xx;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | cwt | 21 |
| 2 | tom | 20 |
| 4 | lisi | 20 |
+----+------+-----+
3 rows in set (0.000 sec)
//洗掉整張表的內容,但會保留表本身
MariaDB [XX_DATA]> DELETE FROM xx;
Query OK, 3 rows affected (0.001 sec)
MariaDB [XX_DATA]> SELECT * FROM xx;
Empty set (0.000 sec)
MariaDB [XX_DATA]> SHOW TABLES;
+-------------------+
| Tables_in_XX_DATA |
+-------------------+
| xx |
+-------------------+
1 row in set (0.000 sec)
TRUNCATE陳述句
- TRUNCATE與DELETE的區別:
| 陳述句 | 特點 |
|---|---|
| DELETE | DELETE洗掉表內容時僅洗掉內容,但會保留表結構 DELETE陳述句每次洗掉一行,并在事務日志中為所洗掉的每行記錄一項 可以通過回滾事務日志恢復資料 非常占用空間 |
| TRUNCATE | 洗掉表中所有資料,且無法恢復 表結構、約束和索引等保持不變,新添加的行計數值重置為初始值 執行速度比DELETE快,且使用的系統和事務日志資源少 通過釋放存盤表資料所用的資料頁來洗掉資料,并且只在事務日志中記錄頁的釋放 對于有外鍵約束參考的表,不能使用TRUNCATE TABLE洗掉資料 不能用于加入了索引視圖的表 |
語法:TRUNCATE table_name;
MariaDB [cwt]> DESC student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
MariaDB [cwt]> SELECT * FROM student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshou | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
MariaDB [cwt]> TRUNCATE student;
Query OK, 0 rows affected (0.307 sec)
MariaDB [cwt]> INSERT student(name,age) VALUE('cwt',20);
Query OK, 1 row affected (0.001 sec)
MariaDB [cwt]> SELECT * FROM student; #如果使用DELETE洗掉表的內容,則重新插入資料時新添加的行計數值不會重置為初始值,也就是說id的值為12
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | cwt | 20 |
+----+------+------+
1 row in set (0.000 sec)
DCL操作
創建授權GRANT
- 權限型別(priv_type)
| 權限型別 | 作用 |
|---|---|
| ALL | 所有權限 |
| SELECT | 讀取內容的權限 |
| INSERT | 插入內容的權限 |
| UPDATE | 更新內容的權限 |
| DELETE | 洗掉內容的權限 |
- 指定要操作的物件db_name.table_name
| 表示方式 | 意義 |
|---|---|
| *.* | 所有庫的所有表 |
| db_name | 指定庫的所有表 |
| db_name.table_name | 指定庫的指定表 |
注: WITH GRANT OPTION:被授權的用戶可將自己的權限副本轉贈給其他用戶(復制自己的權限給另外一個用戶),不建議使用,
//語法:GRANT priv_type,... ON [object_type] db_name.table_name TO 'username'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| XX_DATA |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
//授權tom用戶在192.168.86.132上遠程登錄資料庫,且對XX_DATA資料庫有SELECT權限
MariaDB [(none)]> GRANT SELECT ON XX_DATA.* TO 'tom'@'192.168.86.132' IDENTIFIED BY 'redhat456+';
Query OK, 0 rows affected (0.000 sec)
//重讀授權表
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
//遠程登錄資料庫,并測驗權限是否正確設定
[root@client ~]# mysql -utom -p'redhat456+' -h192.168.86.129
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| XX_DATA |
| information_schema |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> USE XX_DATA
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
MariaDB [XX_DATA]> CREATE TABLE cwt(id int not null);
ERROR 1142 (42000): CREATE command denied to user 'tom'@'192.168.86.132' for table 'cwt'
查看授權
- 查看當前登錄用戶的授權資訊
MariaDB [(none)]> SHOW GRANTS;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*AACA4ED26B7AFC0A081A787B8C607B4262252DE5' WITH GRANT OPTION |
| GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION
...
- 查看指定用戶的授權資訊
MariaDB [(none)]> SHOW GRANTS FOR 'tom'@'192.168.86.132';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for tom@192.168.86.132 |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.86.132' IDENTIFIED BY PASSWORD '*0AF6C9DF2CBB59D18182D5C4AFD2E069F93F7DA3' |
| GRANT SELECT ON `XX_DATA`.* TO 'tom'@'192.168.86.132'
...
取消授權REVOKE
//語法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
MariaDB [(none)]> REVOKE SELECT ON XX_DATA.* FROM 'tom'@'192.168.86.132';
Query OK, 0 rows affected (0.000 sec)
//重讀授權表
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> SHOW GRANTS FOR tom@192.168.86.132;
+-----------------------------------------------------------------------------------------------------------------+
| Grants for tom@192.168.86.132 |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.86.132' IDENTIFIED BY PASSWORD '*0AF6C9DF2CBB59D18182D5C4AFD2E069F93F7DA3' |
- mysql服務行程啟動時會讀取mysql庫中的所有授權表至記憶體中:
- GRANT或REVOKE等執行權限操作會保存于表中,mysql的服務行程會自動重讀授權表,并更新至記憶體中
- 對于不能夠或不能及時重讀授權表的命令,可手動讓mysql的服務行程重讀授權表
//重讀授權表
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
內連接、左連接以及右連接
- 示例表說明:
MariaDB [cwt]> SELECT * FROM info;
+----+-----------+------------+--------+
| id | name | department | salary |
+----+-----------+------------+--------+
| 1 | zhangshan | sales | 5000 |
| 2 | wangwu | dev | 20000 |
| 3 | lisi | sales | 15000 |
| 4 | cwt | dev | 15000 |
| 5 | tom | operation | 25000 |
| 6 | jerry | operation | 20000 |
+----+-----------+------------+--------+
6 rows in set (0.000 sec)
MariaDB [cwt]> SELECT * FROM age_info;
+----+------+
| id | age |
+----+------+
| 1 | 30 |
| 2 | 20 |
| 3 | 45 |
| 4 | 30 |
| 5 | 25 |
| 6 | 23 |
| 7 | 28 |
| 8 | 30 |
| 9 | 25 |
| 10 | 23 |
| 11 | 28 |
| 12 | 30 |
+----+------+
12 rows in set (0.000 sec)
MariaDB [cwt]> SELECT * FROM job_number;
+----+----+
| id | jn |
+----+----+
| 1 | 1 |
| 2 | 3 |
| 3 | 5 |
| 4 | 6 |
| 5 | 9 |
+----+----+
內連接
- 組合兩個表中的記錄,回傳關聯欄位相符的記錄,也就是回傳兩張表中都包含的內容
MariaDB [cwt]> SELECT * FROM info as a INNER JOIN age_info as b ON a.id = b.id INNER JOIN job_number as c ON a.id = c.jn;
+----+-----------+------------+--------+----+------+----+----+
| id | name | department | salary | id | age | id | jn |
+----+-----------+------------+--------+----+------+----+----+
| 1 | zhangshan | sales | 5000 | 1 | 30 | 1 | 1 |
| 3 | lisi | sales | 15000 | 3 | 45 | 2 | 3 |
| 5 | tom | operation | 25000 | 5 | 25 | 3 | 5 |
| 6 | jerry | operation | 20000 | 6 | 23 | 4 | 6 |
+----+-----------+------------+--------+----+------+----+----+
左連接
- left join 是left outer join的簡寫,它的全稱是左外連接,是外連接中的一種, 左(外)連接,左表(a_table)的記錄將會全部表示出來,而右表(b_table)只會顯示符合搜索條件的記錄,右表記錄不足的地方均為NULL,
MariaDB [cwt]> SELECT * FROM info as a LEFT JOIN age_info as b ON a.id = b.id LEFT JOIN job_number as c ON a.id = c.jn;
+----+-----------+------------+--------+------+------+------+------+
| id | name | department | salary | id | age | id | jn |
+----+-----------+------------+--------+------+------+------+------+
| 1 | zhangshan | sales | 5000 | 1 | 30 | 1 | 1 |
| 3 | lisi | sales | 15000 | 3 | 45 | 2 | 3 |
| 5 | tom | operation | 25000 | 5 | 25 | 3 | 5 |
| 6 | jerry | operation | 20000 | 6 | 23 | 4 | 6 |
| 2 | wangwu | dev | 20000 | 2 | 20 | NULL | NULL |
| 4 | cwt | dev | 15000 | 4 | 30 | NULL | NULL |
+----+-----------+------------+--------+------+------+------+------+
右連接
- right join是right outer join的簡寫,它的全稱是右外連接,是外連接中的一種,與左(外)連接相反,右(外)連接,左表(a_table)只會顯示符合搜索條件的記錄,而右表(b_table)的記錄將會全部表示出來,左表記錄不足的地方均為NULL,
MariaDB [cwt]> SELECT * FROM info as a RIGHT JOIN age_info as b ON a.id = b.id RIGHT JOIN job_number as c ON a.id = c.jn;
+------+-----------+------------+--------+------+------+----+----+
| id | name | department | salary | id | age | id | jn |
+------+-----------+------------+--------+------+------+----+----+
| 1 | zhangshan | sales | 5000 | 1 | 30 | 1 | 1 |
| 3 | lisi | sales | 15000 | 3 | 45 | 2 | 3 |
| 5 | tom | operation | 25000 | 5 | 25 | 3 | 5 |
| 6 | jerry | operation | 20000 | 6 | 23 | 4 | 6 |
| NULL | NULL | NULL | NULL | NULL | NULL | 5 | 9 |
+------+-----------+------------+--------+------+------+----+----+
- 查詢多張表,組合結果
MariaDB [cwt]> SELECT * FROM info;
+----+-----------+------------+--------+
| id | name | department | salary |
+----+-----------+------------+--------+
| 1 | zhangshan | sales | 5000 |
| 2 | wangwu | dev | 20000 |
| 3 | lisi | sales | 15000 |
| 4 | cwt | dev | 15000 |
| 5 | tom | operation | 25000 |
| 6 | jerry | operation | 20000 |
+----+-----------+------------+--------+
6 rows in set (0.000 sec)
MariaDB [cwt]> SELECT * FROM age_info;
+----+------+
| id | age |
+----+------+
| 1 | 30 |
| 2 | 20 |
| 3 | 45 |
| 4 | 30 |
| 5 | 25 |
| 6 | 23 |
| 7 | 28 |
| 8 | 30 |
+----+------+
MariaDB [cwt]> SELECT * FROM info,age_info WHERE info.name = 'cwt' AND info.id = age_info.id;
+----+------+------------+--------+----+------+
| id | name | department | salary | id | age |
+----+------+------------+--------+----+------+
| 4 | cwt | dev | 15000 | 4 | 30 |
+----+------+------------+--------+----+------+
1 row in set (0.001 sec)
//指定欄位
MariaDB [cwt]> SELECT info.name,age_info.age FROM info,age_info WHERE info.name = 'cwt' AND info.id = age_info.id;
+------+------+
| name | age |
+------+------+
| cwt | 30 |
+------+------+
示例
- 創建一個資料庫,并創建一張表student,該表包含三個欄位(id,name,age),表結構如下:
mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
- 決議
MariaDB [(none)]> CREATE DATABASE cwt;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| cwt |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> USE cwt;
Database changed
MariaDB [cwt]> CREATE TABLE student(id int PRIMARY KEY auto_increment NOT NULL,name VARCHAR(100) NOT NULL,age tinyint);
Query OK, 0 rows affected (0.326 sec)
MariaDB [cwt]> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.003 sec)
- 查看下該新建的表有無內容(用select陳述句)
MariaDB [cwt]> SELECT * FROM student;
Empty set (0.000 sec)
- 在新建的student表中插入資料(用insert陳述句),結果應如下所示:
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
- 決議
MariaDB [cwt]> insert student(name,age) values('tom',20),('jerry',23),('wangqing',25),('sean',28),('zhangshan',26),('zhangshan',20),('lisi',NULL),('chenshou',10),('wangqu',3),('qiuyi',15),('qiuxiaotian',20);
MariaDB [cwt]> SELECT * FROM student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshou | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.000 sec)
- 修改lisi的年齡為50
MariaDB [cwt]> UPDATE student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [cwt]> SELECT * FROM student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | 50 |
| 8 | chenshou | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.000 sec)
- 以age欄位降序排序
MariaDB [cwt]> SELECT * FROM student ORDER BY age DESC;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 29 | lisi | 50 |
| 26 | sean | 28 |
| 27 | zhangshan | 26 |
| 25 | wangqing | 25 |
| 24 | jerry | 23 |
| 23 | tom | 20 |
| 28 | zhangshan | 20 |
| 33 | qiuxiaotian | 20 |
| 32 | qiuyi | 15 |
| 30 | chenshou | 10 |
| 31 | wangqu | 3 |
+----+-------------+------+
- 查詢student表中年齡最小的3位同學跳過前2位
MariaDB [cwt]> SELECT * FROM student ORDER BY age LIMIT 2,3;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 32 | qiuyi | 15 |
| 23 | tom | 20 |
| 33 | qiuxiaotian | 20 |
+----+-------------+------+
- 查詢student表中年齡最大的4位同學
MariaDB [cwt]> SELECT * FROM student ORDER BY age DESC LIMIT 4;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 29 | lisi | 50 |
| 26 | sean | 28 |
| 27 | zhangshan | 26 |
| 25 | wangqing | 25 |
+----+-----------+------+
- 查詢student表中名字叫zhangshan的記錄
MariaDB [cwt]> SELECT * FROM student WHERE name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
+----+-----------+------+
2 rows in set (0.000 sec)
- 查詢student表中名字叫zhangshan且年齡大于20歲的記錄
MariaDB [cwt]> SELECT * FROM student WHERE name = 'zhangshan' AND age > 20;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
+----+-----------+------+
1 row in set (0.000 sec)
- 查詢student表中年齡在23到30之間的記錄
MariaDB [cwt]> SELECT * FROM student WHERE age BETWEEN 23 and 30;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
+----+-----------+------+
4 rows in set (0.000 sec)
- 修改wangwu的年齡為100
MariaDB [cwt]> UPDATE student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [cwt]> SELECT * FROM student WHERE name = 'wangwu';
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.000 sec)
- 洗掉student中名字叫zhangshan且年齡小于等于20的記錄
MariaDB [cwt]> DELETE FROM student WHERE name = 'zhangshan' AND age <= 20;
Query OK, 1 row affected (0.001 sec)
MariaDB [cwt]> SELECT * FROM student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshou | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.000 sec)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/186506.html
標籤:其他
上一篇:Oracle drop表后恢復表和資料。Oracle資料型別VARCHAR2(4000)改為CLOB
下一篇:MySQL鎖的基本介紹
