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陳述句
MariaDB [XX_DATA]> SELECT * FROM xx;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | cwt | 18 |
| 2 | tom | 20 |
| 3 | xx | 25 |
| 4 | lisi | 20 |
+----+------+-----+
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)
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)
示例
- 創建一個資料庫,并創建一張表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 INTO student VALUES (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);
Query OK, 11 rows affected (0.001 sec)
Records: 11 Duplicates: 0 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 | 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)
- 查詢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/183663.html
標籤:其他
上一篇:PL/SQL入門,非常詳細的筆記
下一篇:Oracle表空間
