Practical Issues of Database Application
Practical Issues of Database Application
Practical Issues of Database Application
database application
1. Transaction in SQL
2. Indexes in SQL & Query optimization
3. Views
User 1 finds a
time seat empty
User2 finds a seat
empty
User1 sets seat
22A occupied
User2 sets seat
22A occupied
Serializability
Transaction is a group of operations that need to
be performed together
A certain transaction must be serializable with
respect to other transactions, that is, the
transactions run serially – one at a time, no
overlap
Atomicity
A certain combinations of database operations
need to be done atomically, that is, either they
are all done or neither is done
Atomicity
Example: Transfer $500 from the account number
3209 to account number 3208 by two steps
◦ (1) Subtract $500 from account number 3209
◦ (2) Add $500 to account number 3208
What happen if there is a failure after step (1) but
before step (2)?
Atomicity
$500
ACC: 3209
$500
ACC: 3208
Transactions
Transaction is a collection of one or more
operations on the database that must be
executed atomically
That is, either all operations are performed or
none are
In SQL, each statement is a transaction by itself
SQL allows to group several statements into a
single transaction
Transactions
Transaction begins by SQL command START
TRANSACTION
Two ways to end a transaction
The SQL statement COMMIT causes the
transaction to end successfully
The SQL statement ROLLBACK causes the
transaction to abort, or terminate
unsuccessfully
ACID properties of Transaction
Atomicity
Consistency
Isolation
Durability
ACID properties of Transaction
Atomicity: a transaction is an atomic unit of
processing; it should either be performed in its
entirety or not performed at all.
◦ At the end of the transaction, either all statements
of the transaction is successful or all statements of
the transaction fail.
◦ If a partial transaction is written to the disk then the
Atomic property is violated
Consistency: a transaction should be consistency
preserving, meaning that if it is completely executed
from beginning to end without interference from other
transactions, it should take the database from one
consistent state to another.
ACID properties of Transaction
Isolation: a transaction should appear as though
it is being executed in isolation from other
transactions, even though many transactions are
executing concurrently. That is the execution of a
transaction should not be interfered with by any
other transactions executing concurrently.
Virtual views
◦ Do not exist physically
◦ Defined by an expression like a query
◦ Can be queried and can even be modified
Views
A view just a relation, but we store a definition rather
than a set of tuples
DATA
USER VIEW
TABLES
SELECT *
FROM (SELECT *
FROM tblEmployee
WHERE depNum=1
) ed1, tblDependent d
WHERE ed1.empSSN=d.empSSN
Renaming Attributes
Example 3:
◦ Create view for all employees of Department
number 1, including: SSN, Fullname, Age,
Salary, Sex
Modifying Views
With updatable views, the modification is
translated into an equivalent modification on a
base table
The modification can be done to the base table
View Removal
As we know, Employee_Dep1 is associated to
tplEmployee relation
DROP VIEW Employee_Dep1;
◦ Delete the definition of the view
◦ Does not effect on tplEmployee relation
DROP TABLE tplEmployee;
◦ Delete tplEmployee relation
◦ Make the view Employee_Dep1 unusable
Updatable Views
We can modify views that are defined by selecting
some attributes from one relation R, and
◦ Sub query started by SELECT, not SELECT
DISTINCT
◦ The WHERE clause must not involve R in a sub
query
◦ The FROM clause can only consist of one
occurrence of R and no other relation
◦ The list in the SELECT clause must include
enough attributes that for every tuple inserted
into the view, we can fill the other attributes out
with NULL values or the proper default
Updatable Views
Example 4:
◦ Create view from table Employee
◦ Do changes on Employee and review created
view
◦ Do changes on created view and review
Employee
Update on table effects on view
Update on view effects on table with
unexpected result
Update on view raises error on table
Query optimization
In practice:
1. Define the requirements: Who? What? Where? When?
Why?