0% found this document useful (0 votes)
73 views

(M7S2-POWERPOINT) - Transaction Control Commands

This document provides an overview of transaction control commands in SQL. It discusses transactions, transaction control statements like COMMIT, ROLLBACK, SAVEPOINT, and FOR UPDATE clause. It also covers topics like read consistency, locking, and isolation levels. Examples are provided to illustrate concepts like rolling back to a savepoint in a transaction, locking rows for update, and ensuring read consistency across transactions. Key transaction concepts like atomicity, consistency, isolation, and durability are also summarized.

Uploaded by

Akademiko Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

(M7S2-POWERPOINT) - Transaction Control Commands

This document provides an overview of transaction control commands in SQL. It discusses transactions, transaction control statements like COMMIT, ROLLBACK, SAVEPOINT, and FOR UPDATE clause. It also covers topics like read consistency, locking, and isolation levels. Examples are provided to illustrate concepts like rolling back to a savepoint in a transaction, locking rows for update, and ensuring read consistency across transactions. Key transaction concepts like atomicity, consistency, isolation, and durability are also summarized.

Uploaded by

Akademiko Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

MODULE 7 SUBTOPIC 2

Transaction Control Commands

■At the end of the chapter, the learner should be able to:
• Write single table queries using SQL SELECT command
• Understand the function of transaction control statements
A database transaction consists of one of the
following:
• DML statements that constitute one consistent change to
the data
• One DDL statement
• One data control language (DCL) statement
Time COMMIT

Transaction

DELETE

SAVEPOINT A

INSERT

UPDATE

SAVEPOINT B

INSERT
ROLLBACK ROLLBACK ROLLBACK
to SAVEPOINT B to SAVEPOINT A
• Create a marker in the current transaction by using the SAVEPOINT
statement.
• Roll back to that marker by using the ROLLBACK TO SAVEPOINT
statement.
UPDATE...
SAVEPOINT update_done;

INSERT...
ROLLBACK TO update_done;
• Make the changes:
DELETE FROM employees
WHERE employee_id = 99999;

INSERT INTO departments


VALUES (290, 'Corporate Tax', NULL, 1700);

• Commit the changes:


COMMIT;
Discard all pending changes by using the ROLLBACK statement:
• Data changes are undone.
• Previous state of the data is restored.
• Locks on the affected rows are released.

DELETE FROM copy_emp;


ROLLBACK ;
DELETE FROM test;
25,000 rows deleted.

ROLLBACK;
Rollback complete.

DELETE FROM test WHERE id = 100;


1 row deleted.

SELECT * FROM test WHERE id = 100;


No rows selected.

COMMIT;
Commit complete.
• Read consistency guarantees a consistent view of the data at all times.
• Changes made by one user do not conflict with the changes made by
another user.
• Read consistency ensures that, on the same data:
✓Readers do not wait for writers
✓Writers do not wait for readers
✓Writers wait for writers
User A

UPDATE employees Data


SET salary = 7000 blocks
WHERE last_name = 'Grant';

Undo
segments

Changed
SELECT * and unchanged
FROM userA.employees; Read- data
consistent
image Before
change
User B (“old” data)
• Locks the rows in the EMPLOYEES table where job_id is SA_REP.

SELECT employee_id, salary, commission_pct, job_id


FROM employees
WHERE job_id = 'SA_REP'
FOR UPDATE
ORDER BY employee_id;

• Lock is released only when you issue a ROLLBACK or a COMMIT.


• If the SELECT statement attempts to lock a row that is locked by
another user, then the database waits until the row is available, and
then returns the results of the SELECT statement.
• You can use the FOR UPDATE clause in a SELECT statement against multiple
tables.
SELECT e.employee_id, e.salary, e.commission_pct
FROM employees e JOIN departments d
USING (department_id)
WHERE job_id = 'ST_CLERK‘
AND location_id = 1500
FOR UPDATE
ORDER BY e.employee_id;

• Rows from both the EMPLOYEES and DEPARTMENTS tables are locked.
• Use FOR UPDATE OF column_name to qualify the column you intend to
change, then only the rows from that specific table are locked.
Projection Selection

Table 1 Table 1

Join

Table 1 Table 2
Used for queries on single or multiple tables
Clauses of the SELECT statement:
• SELECT
• List the columns (and expressions) to be returned from the query
• FROM
• Indicate the table(s) or view(s) from which data will be obtained
• WHERE
• Indicate the conditions under which a row will be included in the result
• GROUP BY
• Indicate categorization of results
• HAVING
• Indicate the conditions under which a category (group) will be included
• ORDER BY
• Sorts the result according to specified criteria
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
Find products with standard price less than $275

Table 6-3: Comparison Operators in SQL


Alias is an alternative column or table name
SELECT CUST.CUSTOMERNAME AS NAME,
CUST.CUSTOMERADDRESS
FROM CUSTOMER_V CUST
WHERE NAME = ‘Home Furnishings’;
•Using the COUNT aggregate function to find totals

SELECT COUNT(*) FROM ORDERLINE_T


WHERE ORDERID = 1004;

Note: With aggregate functions you can’t have single-valued


columns included in the SELECT clause, unless they are included in
the GROUP BY clause.
AND, OR, and NOT Operators for customizing conditions in WHERE
clause

Note: The LIKE operator allows you to compare strings using wildcards. For
example, the % wildcard in ‘%Desk’ indicates that all strings that have any
number of characters preceding the word “Desk” will be allowed.
Figure 6-8 Boolean query A without use of parentheses

By default,
processing order of
Boolean operators is
NOT, then AND, then
OR
With parentheses…these override the normal precedence of Boolean
operators

With parentheses, you can override normal precedence rules. In


this case parentheses make the OR take place before the AND.
Figure 6-9 Boolean query B with use of parentheses
Sort the results first by STATE, and within a state by the CUSTOMER NAME

Note: The IN operator in this example allows you to include rows whose
CustomerState value is either FL, TX, CA, or HI. It is more efficient
than separate OR conditions.

Database Systems 1
For use with aggregate functions
• Scalar aggregate: single value returned from SQL query with aggregate function
• Vector aggregate: multiple values returned from SQL query with aggregate function (via GROUP BY)

• You can use single-value fields with aggregate functions if they are included in the GROUP BY clause

Database Systems 1
For use with GROUP BY

Like a WHERE clause, but it operates on groups (categories), not on


individual rows. Here, only those groups with total numbers greater than 1
will be included in final result.

Database Systems 1
EMPLOYEES table
Feature Simple Views Complex Views
Number of tables One One or more
Contain functions No Yes
Contain groups of No Yes
data
DML operations Yes Not always
through a view
Views provide users controlled access to tables
Base Table–table containing the raw data
Dynamic View
• A “virtual table” created dynamically upon request by a user
• No data actually stored; instead data from base table made available to user
• Based on SQL SELECT statement on base tables or other views
Materialized View
• Copy or replication of data
• Data actually stored
• Must be refreshed periodically to match corresponding base tables

Database Systems 1
• You embed a subquery in the CREATE VIEW statement:

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view


[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];

• The subquery can contain complex SELECT syntax.

Database Systems 1
• View has a name.
• View is based on a SELECT statement.
• CHECK_OPTION works only for updateable views and
prevents updates that would create rows not included in the
view.
• Simplify query commands
• Assist with data security (but don't rely on views for security, there are
more important security measures)
• Enhance programming productivity
• Contain most current base table data
• Use little storage space
• Provide customized view for user
• Establish physical data independence

Database Systems 1
• Use processing time each time view is referenced
• May or may not be directly updateable

Database Systems 1
In this lesson, you should have learned how to use the following
statements:

Function Description
INSERT Adds a new row to the table
UPDATE Modifies existing rows in the table
DELETE Removes existing rows from the table
MERGE Combines of Insert and Update in one statement
COMMIT Makes all pending changes permanent
SAVEPOINT Is used to roll back to the savepoint marker
ROLLBACK Discards all pending data changes
FOR UPDATE clause Locks rows identified by the SELECT query
in SELECT
END OF MODULE 7
• Taylor, A. G. (2019). SQL for dummies (9th ed.). Hoboken, NJ: For
Dummies.
• Harrington, J. (2016). Relational Database Design and Implementation
(4th Edition). Morgan Kaufmann
• Juric, N., Vrbsky, S., Nestorov, S. (2016). Database Systems: Introduction
to Databases and Data Warehouses. Prospect Press
• Kroenke, D. M., & Auer, D. J. (2016). Database Concepts. Pearson.
• Sullivan, D. (2015). NoSQL for Mere Mortals (1st ed.). Boston: Addison-
Wesley.
• Hoffer, J., Ramesh, V., Topi, H. (2013). Modern Database Management 11th
Edition, Prentice Hall.

Introduction to Programming

You might also like