4.數據庫鎖及表分區
1.數據庫權限授權的語句:
grant select,update,insert,delete on test(表名) to xiaoshan(用戶名);
2.被授權用戶訪問表;注意,一定要加上授權用戶的用戶名
select * from scott.test;
update test set ename='xiaoshan'
where empno=9000;
3.行級鎖:
①行被排他鎖定;
②在某行的鎖被釋放之前,其他用戶不能修改此行;
③使用commit或rollback命令釋放鎖;
4.行級鎖的獲取:
方式①:使用insert,update語句時,自動獲取行級鎖;
方式②:select...for update子句,在表的一行或多行上放置排他鎖,
用於防止其他用戶更新該行。可以執行除更新之外的其他操作;
(鎖定限定行:查詢出行,並鎖定之);
select * from test for update of ename;鎖定指定列(ename)
select * from test for update wait 10;指定等待時間;
5.表級鎖:
①保護表的數據;
②在多個用戶同時訪問數據時,確保數據的完整性;
③可以設置為三種模式:共享,共享更新和排他鎖;
共享鎖:lock table test in share mode;
任何用戶都可以上鎖;
僅允許其他用戶進行查詢操作;不能插入、更新、刪除;
多個用戶可以同時在同一表中放置共享鎖;
【scott>】lock table test in share mode;
【小山>】update scott.test set ename='xiaoshan' where empno=7196; --這時將出現假死狀態;
【scott>】commit;
【小山>】“已更新 1 行”;
共享更新鎖:lock table test in share update mode;
鎖定要更新的行;
允許其他用戶同時查詢、插入、更新未鎖定的行;
在select語句中使用“for update”子句。可以強製使用共享更新鎖;
允許多個用戶同時鎖定表的不同行;
排他鎖:lock table 表名 in exclusive mode;
與其他兩種鎖相比,排他鎖是限製性最強的表鎖;
他僅允許其他用戶查詢數據;
不允許執行插入、刪除和更新操作;
在同一時間僅允許一位用戶在表上放置排他鎖;
最後更新:2017-04-02 03:42:39