Oracle PL/SQL/Table/Transaction Rollback Commit

Материал из SQL эксперт
Перейти к: навигация, поиск

alter rollback segment SEGMENT_NAME online

    
SQL> alter rollback segment SEGMENT_NAME online;
SQL>
SQL>
SQL> commit;
SQL>
SQL>



atomicity: roll back and commit

   
SQL>
SQL>
SQL> create table t ( x number(1) );
Table created.
SQL> /
SQL>  insert into t values ( 1 );
1 row created.
SQL> insert into t values ( 2 );
1 row created.
SQL> /
1 row created.
SQL> rollback;
Rollback complete.
SQL> select * from t;
no rows selected
SQL> insert into t values ( 1 );
1 row created.
SQL> insert into t values ( 2 );
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t;
         X
----------
         1
         2
SQL> /
         X
----------
         1
         2
SQL>
SQL> drop table t;
Table dropped.



Rollback a delete

  

SQL>
SQL> -- create demo table
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
  3    First_Name         VARCHAR2(10 BYTE),
  4    Last_Name          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York",  "Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York",  "Manager")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
  3  /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /
ID   FIRST_NAME LAST_NAME  START_DAT END_DATE      SALARY CITY       DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01   Jason      Martin     25-JUL-96 25-JUL-06    1234.56 Toronto    Programmer
02   Alison     Mathews    21-MAR-76 21-FEB-86    6661.78 Vancouver  Tester
03   James      Smith      12-DEC-78 15-MAR-90    6544.78 Vancouver  Tester
04   Celia      Rice       24-OCT-82 21-APR-99    2344.78 Vancouver  Manager
05   Robert     Black      15-JAN-84 08-AUG-98    2334.78 Vancouver  Tester
06   Linda      Green      30-JUL-87 04-JAN-96    4322.78 New York   Tester
07   David      Larry      31-DEC-90 12-FEB-98    7897.78 New York   Manager
08   James      Cat        17-SEP-96 15-APR-02    1232.78 Vancouver  Tester
8 rows selected.
SQL>
SQL> delete from Employee where id = "01";
1 row deleted.
SQL>
SQL> select id, last_name from Employee where id = "01";
no rows selected
SQL>
SQL> rollback;
Rollback complete.
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /
Table dropped.
SQL>
SQL>
SQL>



Rollback to savepoint in exception handler

    
SQL>
SQL> create table myTable2 ( cnt int );
Table created.
SQL>
SQL> insert into myTable2 values ( 0 );
1 row created.
SQL>
SQL> create table t ( x int check ( x>0 ) );
Table created.
SQL>
SQL> create trigger t_trigger before insert or delete on t for each row
  2  begin
  3     if ( inserting ) then
  4          update myTable2 set cnt = cnt +1;
  5     else
  6          update myTable2 set cnt = cnt -1;
  7     end if;
  8     dbms_output.put_line( "fired and updated "  || sql%rowcount || " rows" );
  9  end;
 10  /
Trigger created.
SQL>
SQL> create or replace procedure p
  2  as
  3  begin
  4          insert into t values ( 1 );
  5          insert into t values (-1 );
  6  end;
  7  /
Procedure created.
SQL> select * from t;
no rows selected
SQL> select * from myTable2;
       CNT
----------
         0
SQL> begin
  2          p;
  3  end;
  4  /
fired and updated 1 rows
fired and updated 1 rows
begin
*
ERROR at line 1:
ORA-02290: check constraint (sqle.SYS_C0010265) violated
ORA-06512: at "sqle.P", line 5
ORA-06512: at line 2

SQL> select * from t;
no rows selected
SQL> select * from myTable2;
       CNT
----------
         0
SQL>
SQL>
SQL> begin
  2      p;
  3  exception
  4      when others then null;
  5  end;
  6  /
fired and updated 1 rows
fired and updated 1 rows
PL/SQL procedure successfully completed.
SQL> select * from t;
         X
----------
         1
SQL> select * from myTable2;
       CNT
----------
         1
SQL>
SQL> rollback;
Rollback complete.
SQL>
SQL> begin
  2      savepoint sp;
  3      p;
  4  exception
  5      when others then
  6          rollback to sp;
  7  end;
  8  /
fired and updated 1 rows
fired and updated 1 rows
PL/SQL procedure successfully completed.
SQL> select * from t;
no rows selected
SQL> select * from myTable2;
       CNT
----------
         0
SQL>
SQL>
SQL>
SQL> drop table t;
Table dropped.
SQL> drop table myTable2;
Table dropped.



SET TRANSACTION READ WRITE

    
SQL> CREATE TABLE project (
  2    pro_id              NUMBER(4),
  3    pro_name            VARCHAR2(40),
  4    budget          NUMBER(9,2),
  5    CONSTRAINT project_pk   PRIMARY KEY (pro_id)
  6  );
Table created.
SQL>
SQL>
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1001, "A",12345);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1002, "ERP",23456);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1003, "SQL",34567);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1004, "CRM",45678);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1005, "VPN",56789);
1 row created.
SQL>
SQL>
SQL> CREATE TABLE server_usage (
  2    pro_id                   NUMBER(4),
  3    emp_id                  NUMBER,
  4    time_log_date                DATE,
  5    hours_logged                 NUMBER(8,2),
  6    dollars_charged              NUMBER(8,2),
  7    CONSTRAINT server_usage_pk  PRIMARY KEY (pro_id, emp_id, time_log_date)
  8  );
Table created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1001,101,to_date("4-Apr-2004","dd-mon-yyyy"),1123,222);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1002,102,to_date("4-Apr-2005","dd-mon-yyyy"),1124,223);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1003,103,to_date("4-Apr-2006","dd-mon-yyyy"),1125,224);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1004,104,to_date("4-Apr-2007","dd-mon-yyyy"),1126,225);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1005,105,to_date("4-Apr-2008","dd-mon-yyyy"),1127,226);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1001,106,to_date("4-Apr-2009","dd-mon-yyyy"),1128,227);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1002,107,to_date("4-Apr-2010","dd-mon-yyyy"),1129,228);
1 row created.
SQL> SET ECHO ON
SQL> 
SQL> SET TRANSACTION READ WRITE;
SQL>
SQL> 
SQL> INSERT INTO project
  2     SELECT 2008, pro_name, budget FROM project
  3     WHERE pro_id = 1002;
1 row created.
SQL>
SQL> 
SQL> UPDATE server_usage
  2  SET pro_id = 2008
  3  WHERE pro_id = 1002;
2 rows updated.
SQL>
SQL> 
SQL> ROLLBACK;
Rollback complete.
SQL>
SQL>
SQL> drop table project;
Table dropped.
SQL> drop table server_usage;
Table dropped.



set transaction use rollback segment SEGMENT_NAME

    
SQL> set transaction use rollback segment SEGMENT_NAME
  2  /



show autocommit

    
SQL>
SQL> set  autocommit on
SQL> show autocommit
autocommit IMMEDIATE
SQL> set  autocommit 42
SQL> show autocommit
AUTOCOMMIT ON for every 42 DML statements
SQL> set  autocommit off
SQL>
SQL>



TRANSACTION CONTROL

   
SQL>
SQL> CREATE TABLE purchase (
  2       product_name  VARCHAR2(25),
  3       quantity      NUMBER(4,2),
  4       purchase_date DATE,
  5       salesperson   VARCHAR2(3)
  6       );
Table created.
SQL>
SQL>
SQL> INSERT INTO purchase VALUES ("Small Widget", 1, "14-JUL-03", "CA");
1 row created.
SQL> INSERT INTO purchase VALUES ("Medium Widget", 75, "14-JUL-03", "BB");
1 row created.
SQL>
SQL>
SQL> SELECT * FROM purchase;
PRODUCT_NAME                QUANTITY PURCHASE_ SAL
------------------------- ---------- --------- ---
Small Widget                       1 14-JUL-03 CA
Medium Widget                     75 14-JUL-03 BB
SQL>
SQL> ROLLBACK;
Rollback complete.
SQL>
SQL> SELECT * FROM purchase;
no rows selected
SQL>
SQL>
SQL> INSERT INTO purchase VALUES ("Small Widget", 1, "14-JUL-03", "CA");
1 row created.
SQL> SAVEPOINT a;
Savepoint created.
SQL> INSERT INTO purchase VALUES ("Medium Widget", 75, "14-JUL-03", "BB");
1 row created.
SQL> SAVEPOINT sp_2;
Savepoint created.
SQL> INSERT INTO purchase VALUES ("Product Number", 2, "14-JUL-03", "GA");
1 row created.
SQL> SAVEPOINT third;
Savepoint created.
SQL> INSERT INTO purchase VALUES ("Small Widget", 8, "15-JUL-03", "GA");
1 row created.
SQL> SAVEPOINT final_sp;
Savepoint created.
SQL> INSERT INTO purchase VALUES ("Medium Widget", 20, "15-JUL-03", "LB");
1 row created.
SQL> SELECT * FROM purchase;
PRODUCT_NAME                QUANTITY PURCHASE_ SAL
------------------------- ---------- --------- ---
Small Widget                       1 14-JUL-03 CA
Medium Widget                     75 14-JUL-03 BB
Product Number                     2 14-JUL-03 GA
Small Widget                       8 15-JUL-03 GA
Medium Widget                     20 15-JUL-03 LB
SQL> ROLLBACK TO final_sp;
Rollback complete.
SQL> SELECT * FROM purchase;
PRODUCT_NAME                QUANTITY PURCHASE_ SAL
------------------------- ---------- --------- ---
Small Widget                       1 14-JUL-03 CA
Medium Widget                     75 14-JUL-03 BB
Product Number                     2 14-JUL-03 GA
Small Widget                       8 15-JUL-03 GA
SQL> ROLLBACK TO third;
Rollback complete.
SQL> SELECT * FROM purchase;
PRODUCT_NAME                QUANTITY PURCHASE_ SAL
------------------------- ---------- --------- ---
Small Widget                       1 14-JUL-03 CA
Medium Widget                     75 14-JUL-03 BB
Product Number                     2 14-JUL-03 GA
SQL> ROLLBACK TO sp_2;
Rollback complete.
SQL> SELECT * FROM purchase;
PRODUCT_NAME                QUANTITY PURCHASE_ SAL
------------------------- ---------- --------- ---
Small Widget                       1 14-JUL-03 CA
Medium Widget                     75 14-JUL-03 BB
SQL> ROLLBACK TO a;
Rollback complete.
SQL> SELECT * FROM purchase;
PRODUCT_NAME                QUANTITY PURCHASE_ SAL
------------------------- ---------- --------- ---
Small Widget                       1 14-JUL-03 CA
SQL> ROLLBACK;
Rollback complete.
SQL> SELECT * FROM purchase;
no rows selected
SQL>
SQL>
SQL> drop table purchase;
Table dropped.



Transaction Rollback and commit

  

SQL>
SQL> -- rollback and commit
SQL>
SQL> create table t ( x number(1) );
Table created.
SQL>
SQL>
SQL> insert into t values ( 1 );
1 row created.
SQL> insert into t values ( 2 );
1 row created.
SQL>
SQL> rollback;
Rollback complete.
SQL> select * from t;
no rows selected
SQL> insert into t values ( 1 );
1 row created.
SQL> insert into t values ( 2 );
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t;
         X
----------
         1
         2
SQL>
SQL> drop table t;
Table dropped.
SQL>
SQL>
SQL>



Transactions and Error Handling

    
SQL>
SQL> CREATE TABLE emp (
  2     empID INT NOT NULL PRIMARY KEY,
  3     Name      VARCHAR(50) NOT NULL);
Table created.
SQL> INSERT INTO emp (empID,Name) VALUES (1,"Tom");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (2,"Jack");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (3,"Mary");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (4,"Bill");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (5,"Cat");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (6,"Victor");
1 row created.
SQL> BEGIN
  2
  3     INSERT INTO emp (empID, Name) VALUES (101, "sqle");
  4     INSERT INTO emp (empID, Name) VALUES (102, "Claire");
  5
  6     SAVEPOINT BeforeAddingAnne;
  7     INSERT INTO emp (empID, Name) VALUES (103, "Anne");
  8     ROLLBACK TO BeforeAddingAnne;
  9
 10     COMMIT;
 11
 12  EXCEPTION
 13     WHEN OTHERS
 14        THEN ROLLBACK;
 15  END;
 16  /
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL> drop table emp;
Table dropped.



Transactions and Savepoints

    
SQL>
SQL>
SQL> CREATE TABLE emp (
  2     empID INT NOT NULL PRIMARY KEY,
  3     Name      VARCHAR(50) NOT NULL);
Table created.
SQL> INSERT INTO emp (empID,Name) VALUES (1,"Tom");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (2,"Jack");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (3,"Mary");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (4,"Bill");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (5,"Cat");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (6,"Victor");
1 row created.
SQL>
SQL> SAVEPOINT BeforeAddingBademp;
Savepoint created.
SQL> INSERT INTO emp (empID, Name) VALUES (99, "Bad emp");
1 row created.
SQL> ROLLBACK TO BeforeAddingBademp;
Rollback complete.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
SQL>
SQL>
SQL> drop table emp;
Table dropped.