zoukankan      html  css  js  c++  java
  • postgresql事务

    查看更多教程:http://www.gitbook.net/postgresql/2013080567.html

     

    pgsql事务与并发控制

    事务与并发控制

    数据库几大特性:

    ACID

    Atomicity:原子性:一个事务要么全部执行,要么全部不执行

    Consistency :一致性:执行事务的时候,数据库从一个一致的状态变更到另一个状态

     Isolation:隔离性: 确保在并发执行的时候,每个事务感觉不到其他事务在并发的执行

     Durability:持久性:一个事务完成之后,即使数据库发生故障,他对数据库的改变应该永久的保存在数据库中。

     

    并发引起的现象

    • 脏读:一个事务读取了第二个事务的已经修改但是未提交的数据
    • 不可重复读:一个事务第一次读取数据之后,被读取的数据被另一个已提交的事务进行了修改,事务再次读取这些数据时候发现数据已经被另外一个事务所修改,两次查询不一致
    • 幻读:一个事务的两次结果集记录数不一致(特殊的不可重复读)
    1. 查看数据库的隔离级别操作
    • 查看全局事务级别
      select name,setting from pg_settings where name = 'default_transaction_isolation'
    • 修改全局事务隔离级别
      alter system set default_transaction_islation to 'REPEATABLE READ';select pg_reload_conf();select current_setting('transaction_isolation');
    • 查看当前会话的事务隔离级别
      show transaction_isolcation;
    • 设置当前事务的事务隔离级别
      start transaction isolation level READ UNCOMMITED;
    • start TRANSACTION    select xxxx
    • END

     

     

    事物是如何开始的?

    1、每条SQL语句就是一个事物。「自动提交模式」

    2、直到用户执行commit或rollback为止作一个事务。

     

    2.1、事务开始 sql server 、Postgresql 是下面方式:

     

    Begin transaction ;      

          Update student set name=‘teddy’ where id=3 ;

     

         Update student set age=‘13’ where id=3 ;

    Commit ;———>提交处理

     

     

    2.2、事务开始 mysql 是下面方式:start  transaction ;     

    start transaction ;      

          Update student set name=‘teddy’ where id=3 ;

     

         Update student set age=‘13’ where id=3 ;

    Commit ;———>提交处理

     

     

    事务回滚例子:

    begin transaction ;

          Update student set name=‘teddy’ where id=3 ;

     

         Update student set age=‘13’ where id=3 ;

    rollback ;———>取消处理

     

     

     

    创建表:create table student ( student_id integer , name varchar(12),sex varchar(12),age integer);

     

    插入数据:insert into student values(1,'teddy','man',25),(2,’Tony’,’man',27)(3,’lida’,’man',25);

     

    更新数据:Update student set name=‘teddy’ where id=3 ;

     

    查询:select*from student;

     

    删除数据: delete from student where student_id=1;  —————>  只删除数据,表还存在。

     

    删除表:drop table student;——————————————>表和数据都删除了。

     

    删除视图:drop view aa_view_test;

    创建视图:create or replace view 

    aa_view_test          ——>视图名

     (name  , age)          ——>视图里的字段

    as 

    select name ,age from student;

    EXIST OR  NOT EXIST:是否存在

    select name ,age from  student_a as A

    where exist (select *from student_b. as B);

    case用法:

    case  type 

    when ‘a’ then ‘一级’

    when ‘b’ then ‘二级’

    when ‘c’ then ‘三级’

    else ‘其他’ end  as  类型

    对集合的操作:

    Union all  全部数据加起来「包含重复」

    select *from student1

    union all

    select *from student2;

    Intersect-----选取公共部分

    select *from student1

    Intersect

    select *from student2;

    except 记录减法

    select *from student1

    except

    select *from student2;

     

  • 相关阅读:
    kmp dp hdu 3336
    Python之路【第三篇】:Python基础(18)——函数万能参数
    Python之路【第三篇】:Python基础(17)——函数动态参数
    Python之路【第三篇】:Python基础(16)——函数动态参数
    Python之路【第三篇】:Python基础(15)——函数指定参数
    Python之路【第三篇】:Python基础(13)——函数普通参数
    Python之路【第三篇】:Python基础(14)——函数默认参数
    Python之路【第三篇】:Python基础(12)——函数
    Python之路【第三篇】:Python基础(11)——set集合
    Python之路【第三篇】:Python基础(10)——set集合
  • 原文地址:https://www.cnblogs.com/1314520xh/p/10236675.html
Copyright © 2011-2022 走看看