Lesson 2: Avoiding phantom rows

 Using snapshot isolation to avoid phantom rows

You can use the snapshot isolation level to maintain consistency at the same level as isolation level at 3, without any sort of blocking. The Sales Manager's statement is not blocked, and the Accountant does not see a phantom row.

If you have not done so, follow steps 1 through 4 of the phantom rows tutorial, which describes how to start two instances of Interactive SQL. See Tutorial: Understanding phantom rows.

  1. Enable snapshot isolation for the Accountant by executing the following statements.

    SET OPTION PUBLIC. allow_snapshot_isolation = 'On';
    SET TEMPORARY OPTION isolation_level = 'snapshot';
  2. In the Accountant window, enter the following statement to list all the departments.

    SELECT * FROM Departments
    ORDER BY DepartmentID;
    DepartmentID DepartmentName DepartmentHeadID
    100 R & D 501
    200 Sales 902
    300 Finance 1293
    400 Marketing 1576
    500 Shipping 703
  3. The Sales Manager decides to set up a new department to focus on the foreign market. Philip Chin, who has EmployeeID 129, heads the new department.

    INSERT INTO Departments
       ( DepartmentID, DepartmentName, DepartmentHeadID )
       VALUES( 600, 'Foreign Sales', 129 );
    COMMIT;

    The final statement creates the new entry for the new department. It appears as a new row at the bottom of the table in the Sales Manager's window.

    SELECT * FROM Departments
    ORDER BY DepartmentID;
    DepartmentID DepartmentName DepartmentHeadID
    100 R & D 501
    200 Sales 902
    300 Finance 1293
    400 Marketing 1576
    500 Shipping 703
    600 Foreign Sales 129
  4. The Accountant can execute his query again, and does not see the new row because the transaction has not ended.

    SELECT *
    FROM Departments
    ORDER BY DepartmentID;
    DepartmentID DepartmentName DepartmentHeadID
    100 R & D 501
    200 Sales 902
    300 Finance 1293
    400 Marketing 1576
    500 Shipping 703
  5. The Sales Manager would like to add a second department to handle sales initiative aimed at large corporate partners. Execute the following statement in the Sales Manager's window.

    INSERT INTO Departments
     ( DepartmentID, DepartmentName, DepartmentHeadID )
       VALUES( 700, 'Major Account Sales', 902 );

    The Sales Manager's change is not blocked because the Accountant is using snapshot isolation.

  6. The Accountant must end his snapshot transaction to see the changes the Sales Manager committed to the database.

    COMMIT;
       SELECT * FROM Departments
       ORDER BY DepartmentID;

    Now the Accountant sees the Foreign Sales department, but not the Major Account Sales department.

    DepartmentID DepartmentName DepartmentHeadID
    100 R & D 501
    200 Sales 902
    300 Finance 1293
    400 Marketing 1576
    500 Shipping 703
    600 Foreign Sales 129
  7. To avoid changing the SQL Anywhere sample database, you should roll back the incomplete transaction that inserts the Major Account Sales department row and use a second transaction to delete the Foreign Sales department.

    1. Execute the following statement in the Sales Manager's window to rollback the last, incomplete transaction:

      ROLLBACK;
    2. Also in the Sales Manager's window, execute the following two statements to delete the row that you inserted earlier and commit this operation.

      DELETE FROM Departments
      WHERE DepartmentID = 600;
      COMMIT;