1. 程式人生 > >myslq基本語法(3)

myslq基本語法(3)

啟動 表示 管理 ges acid 處理 min 沒有權限 密碼


一.多表連接
當需要的查詢的數據不是來自一張表,二十來自多張表時,就需要用到多表連接查詢
select t1.*,t2.teacher from it_student t1, it_subject t2 where t1.subject = t2.name;

mysql> select t1.*,t2.teacher from it_student t1, it_subject t2 where t1.subject = t2.name;
+----+----------+------+-------+----------+---------+------------+
| id |
name | age | sex | salary | subject | teacher | +----+----------+------+-------+----------+---------+------------+ | 1 | xianqian | 23 | woman | 19000.00 | test | wanglaoshi | | 2 | xiaoming | 26 | man | 15000.30 | python | lilaoshi | | 3 | laowang | 29 | man | 16000.50 | python |
lilaoshi | | 4 | xiaolong | 27 | man | 28000.00 | python | lilaoshi | | 5 | laowen | 30 | man | 12000.00 | test | wanglaoshi | +----+----------+------+-------+----------+---------+------------+ 5 rows in set (0.00 sec)

註意:
1.設置數據庫別名,與列類似,
t1,t2 分別為it_student,it_subject表的別名
2.笛卡兒積:表1的行數 * 表2的行數

3.內連接
缺點:只能顯示匹配的行
表1 inner join 表2 on 條件
select t1.*,t2.teacher from it_student t1 inner join it_subject t2 on t1.subject = t2.name;

mysql> select t1.*,t2.teacher from it_student t1 inner join it_subject t2 on t1.subject = t2.name;
+----+----------+------+-------+----------+---------+------------+
| id | name     | age  | sex   | salary   | subject | teacher    |
+----+----------+------+-------+----------+---------+------------+
|  1 | xianqian |   23 | woman | 19000.00 | test    | wanglaoshi |
|  2 | xiaoming |   26 | man   | 15000.30 | python  | lilaoshi   |
|  3 | laowang  |   29 | man   | 16000.50 | python  | lilaoshi   |
|  4 | xiaolong |   27 | man   | 28000.00 | python  | lilaoshi   |
|  5 | laowen   |   30 | man   | 12000.00 | test    | wanglaoshi |
+----+----------+------+-------+----------+---------+------------+
5 rows in set (0.00 sec)

4.外連接
左外連接(left join):左表中的數據能夠全部顯示
select t1.*,t2.teacher from it_student t1 left join it_subject t2 on t1.subject = t2.name;

mysql> select t1.*,t2.teacher from it_student t1 left join it_subject t2 on t1.subject = t2.name;
+----+----------+------+-------+----------+---------+------------+
| id | name     | age  | sex   | salary   | subject | teacher    |
+----+----------+------+-------+----------+---------+------------+
|  2 | xiaoming |   26 | man   | 15000.30 | python  | lilaoshi   |
|  3 | laowang  |   29 | man   | 16000.50 | python  | lilaoshi   |
|  4 | xiaolong |   27 | man   | 28000.00 | python  | lilaoshi   |
|  1 | xianqian |   23 | woman | 19000.00 | test    | wanglaoshi |
|  5 | laowen   |   30 | man   | 12000.00 | test    | wanglaoshi |
|  6 | laotian  |   27 | man   |     NULL | NULL    | NULL       |
+----+----------+------+-------+----------+---------+------------+
6 rows in set (0.00 sec)

右外連接(right join):右表中的數據能夠全部顯示
select t1.*,t2.teacher from it_student t1 right join it_subject t2 on t1.subject = t2.name;

mysql> select t1.*,t2.teacher from it_student t1 right join it_subject t2 on t1.subject = t2.name;
+------+----------+------+-------+----------+---------+------------+
| id   | name     | age  | sex   | salary   | subject | teacher    |
+------+----------+------+-------+----------+---------+------------+
|    1 | xianqian |   23 | woman | 19000.00 | test    | wanglaoshi |
|    2 | xiaoming |   26 | man   | 15000.30 | python  | lilaoshi   |
|    3 | laowang  |   29 | man   | 16000.50 | python  | lilaoshi   |
|    4 | xiaolong |   27 | man   | 28000.00 | python  | lilaoshi   |
|    5 | laowen   |   30 | man   | 12000.00 | test    | wanglaoshi |
| NULL | NULL     | NULL | NULL  |     NULL | NULL    | houlaoshi  |
+------+----------+------+-------+----------+---------+------------+
6 rows in set (0.00 sec)

二.子查詢
就是查詢語句中嵌套右查詢語句
示例:
select * from it_student where salary=(select max(salary) from it_student);

mysql> select * from it_student where salary=(select max(salary) from it_student);
+----+----------+------+------+----------+---------+
| id | name     | age  | sex  | salary   | subject |
+----+----------+------+------+----------+---------+
|  4 | xiaolong |   27 | man  | 28000.00 | python  |
+----+----------+------+------+----------+---------+
1 row in set (0.00 sec)

select * from it_student where subject in (select name from it_subject where teacher = ‘lilaoshi‘);

mysql> select * from it_student where subject in (select name from it_subject where teacher = lilaoshi);
+----+----------+------+------+----------+---------+
| id | name     | age  | sex  | salary   | subject |
+----+----------+------+------+----------+---------+
|  2 | xiaoming |   26 | man  | 15000.30 | python  |
|  3 | laowang  |   29 | man  | 16000.50 | python  |
|  4 | xiaolong |   27 | man  | 28000.00 | python  |
+----+----------+------+------+----------+---------+
3 rows in set (0.00 sec)

三.事務
1.什麽是事務
就是一系列將要執行或正在執行的操作(sql語句)
2.事務安全,一種保護數據的安全機制,就是保證數據的完整性,一致性,事務執行前和執行後,數據是完整的
3.事務的特性(ACID)
(1)原子性(atomicity)
就是把事務中的一系列操作當作一個整體,要麽全部執行,要麽都不執行
(2)一致性(consistency)
數據從一個狀態轉到另一個狀態,保證每一個狀態數據的完整性
(3)隔離性(isolation)
當有多個事務同時進行時,能夠保證各個事務之間不會相互影響
(4)持久性(durability)
當事務提交後,就會永久保存在數據庫中
4.事務安全機制只在innodb引擎有效,myisam引擎註重查詢快
5.事務處理的步驟
(1)事務的啟動
start transaction;
(2)執行一系列SQL語句
update it_student set ...
update it_student set ...
(3)commit/rollback
提交或回滾,
commit後所有操作都會永久保存到數據庫中
rollback後(2)步驟中執行所有SQL語句都不會寫入數據庫中

事務處理示例:

mysql> select * from it_student where name=xianqian;
+----+----------+------+-------+----------+---------+
| id | name     | age  | sex   | salary   | subject |
+----+----------+------+-------+----------+---------+
|  1 | xianqian |   23 | woman | 19000.00 | test    |
+----+----------+------+-------+----------+---------+
1 row in set (0.00 sec)

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> update it_student set salary=salary+5000 where name=xianqian;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from it_student where name=xianqian;
+----+----------+------+-------+----------+---------+
| id | name     | age  | sex   | salary   | subject |
+----+----------+------+-------+----------+---------+
|  1 | xianqian |   23 | woman | 24000.00 | test    |
+----+----------+------+-------+----------+---------+
1 row in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from it_student where name=xianqian;
+----+----------+------+-------+----------+---------+
| id | name     | age  | sex   | salary   | subject |
+----+----------+------+-------+----------+---------+
|  1 | xianqian |   23 | woman | 19000.00 | test    |
+----+----------+------+-------+----------+---------+
1 row in set (0.00 sec)

四.用戶管理
1.用戶創建
create user ‘用戶名‘@‘主機信息‘ identified by ‘123‘; # 本機可以使用localhost或127.0.0.1
create user ‘tom‘@‘localhost‘ identified by ‘123‘;

mysql> create user tom@localhost identified by 123;
Query OK, 0 rows affected (0.00 sec)

create user ‘tom‘@‘192.168.211.103‘ identified by ‘123‘;

mysql> create user tom@192.168.211.103 identified by 123;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+------------------+
| user | host             |
+------+------------------+
| root | 127.0.0.1        |
| root | 152.136.89.241   |
| tom  | 192.168.211.103  |
| root | 192.168.39.22    |
| root | 36.112.0.68      |
| root | ::1              |
| jack | localhost        |
| root | localhost        |
| tom  | localhost        |
| root | vm\_0\_2\_centos |
+------+------------------+
10 rows in set (0.00 sec)

用戶的主機表現形式

‘user‘@‘localhost‘   表示user只能在本地通過socket登錄數據庫
‘user‘@‘192.168.0.1‘    表示user用戶只能在192.168.0.1登錄數據庫
‘user‘@‘192.168.0.0/24‘    表示user用戶可以在該網絡任意的主機登錄數據庫
‘user‘@‘%‘    表示user用戶可以在所有的機器上登錄數據庫

2.用戶賦權
(1)grant賦權
grant 權限1 [,權限2...] on 數據庫名.數據表名 to ‘用戶名‘@‘主機信息‘;
grant 權限(field1,field2...) on 數據庫名.數據表名 to ‘用戶名‘@‘主機信息‘;
grant select,update(age) on db_itheima.it_student to ‘tom‘@‘localhost‘;
grant all on *.* to ‘tom‘@‘192.168.211.103‘;

mysql> grant all on *.* to tom@192.168.211.103;
Query OK, 0 rows affected (0.00 sec)

賦權完成後建議刷新授權表:flush privileges;
查看權限
show grants; # 查看當前用戶的權限
show grants for ‘tom‘@‘localhost‘;

mysql> show grants for tom@192.168.211.103;
+---------------------------------------------------------------------------------------------------------------------------+
| Grants for tom@192.168.211.103                                                                                            |
+---------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO tom@192.168.211.103 IDENTIFIED BY PASSWORD *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+---------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

註意:
a.*.* 代表所有數據庫的所有表
b.all 代表所有權限;usage 代表沒有權限,只有登錄數據庫,只可以使用test或test_*數據庫
c.grant賦權時加上 with grant option 可以讓被賦權的用戶有權限賦權給其他用戶,但是不能超過它本身具有的最大權限
示例:
grant all select,update,delete on db_itheima.* to ‘tom‘@‘localhost‘;
mysql -u tom -p登錄mysql
grant select ,update on db_itheima.* to ‘jack‘@‘loclahost‘; # jack的最大權限為select,update,delete on db_itheima.*

(2)創建用戶的同時賦權
grant select, update on db_itheima.* to ‘harry‘@‘localhost‘ identified by ‘1234‘;

mysql> grant select, update on db_itheima.* to harry@localhost identified by 1234;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user ;
+-------+------------------+
| user  | host             |
+-------+------------------+
| root  | 127.0.0.1        |
| root  | 152.136.89.241   |
| tom   | 192.168.211.103  |
| root  | 192.168.39.22    |
| root  | 36.112.0.68      |
| root  | ::1              |
| harry | localhost        |
| jack  | localhost        |
| root  | localhost        |
| tom   | localhost        |
| root  | vm\_0\_2\_centos |
+-------+------------------+
11 rows in set (0.00 sec)

(3)權限保存位置

mysql.user   所有mysql用戶的賬號和密碼,以及用戶對全庫全表權限(*.*)
mysql.db    非mysql庫的授權都保存在此(db.*)
mysql.table_priv    某庫某表的授權(db.table)
mysql.columns_priv    某庫某表某列的授權(db.table.col1)
mysql.procs_priv    某庫存儲過程的授權

3.收回權限
用戶有什麽權限就可以回收什麽權限
revoke select on db_itheima.* from ‘harry‘@‘localhost‘;

mysql> show grants for harry@localhost
    -> ;
+--------------------------------------------------------------------------------------------------------------+
| Grants for harry@localhost                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO harry@localhost IDENTIFIED BY PASSWORD *A4B6157319038724E3560894F7F932C8886EBFCF |
| GRANT SELECT, UPDATE ON `db_itheima`.* TO harry@localhost                                                |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> revoke select on db_itheima.* from harry@localhost;
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for harry@localhost;
+--------------------------------------------------------------------------------------------------------------+
| Grants for harry@localhost                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO harry@localhost IDENTIFIED BY PASSWORD *A4B6157319038724E3560894F7F932C8886EBFCF |
| GRANT UPDATE ON `db_itheima`.* TO harry@localhost                                                        |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

4.刪除用戶
drop user ‘用戶名‘@‘主機信息‘;
drop user ‘tom‘@‘localhost‘;
delete from mysql.user where user=‘‘ and host=‘localhost‘ # 刪除本地匿名用戶

mysql> drop user tom@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user where user=tom;
+------+-----------------+
| user | host            |
+------+-----------------+
| tom  | 192.168.211.103 |
+------+-----------------+
1 row in set (0.00 sec)

myslq基本語法(3)