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;
                   与其他两种锁相比,排他锁是限制性最强的表锁;
                   他仅允许其他用户查询数据;
                   不允许执行插入、删除和更新操作;
                   在同一时间仅允许一位用户在表上放置排他锁;

原文地址:https://www.cnblogs.com/zhangqs008/p/2341258.html