Download transaction - Ekalavya

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Access wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Oracle Database wikipedia , lookup

Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

SQL wikipedia , lookup

Commitment ordering wikipedia , lookup

PL/SQL wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Serializability wikipedia , lookup

Concurrency control wikipedia , lookup

Transcript
CS640
Information Systems
Dr Deepak B Phatak
Subrao Nilekani Chair Professor
Kanwal Rekhi Building, Department of CSE
IIT Bombay
Session 7, SQL DML and transactions
IIT BOMBAY
Session overview
IIT BOMBAY
• Views
• Data manipulation
• Changes in database contents
• Transactions
• Course Project
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
2
View definition
IIT BOMBAY
• To create a view, we use:
Create view v
as <query expression>;
• <Query expression> is any legal
expression
• The view name is represented by ‘v’
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
3
Sample view
IIT BOMBAY
Create view h8students as
Select (sroll, sname, sroom, scpi)
From student
Where shostel = 8;
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
4
Sample query a view
IIT BOMBAY
Select sname
From h8students
Where sroom > 300
And scpi > 9.00;
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
5
Sample query a view
IIT BOMBAY
Select sname
From h8students
Where sroom > 300
And scpi > 9.00;
The create view statement only creates the
schema of the view, It is materialized only
when a query is executed
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
6
Derived relation
IIT BOMBAY
• Temporary views created as a
result of a query
Select sh, cpiavg
from (select shostel, avg(scpi)
From student
Group by shostel
As result (sh, cpiavg)
);
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
7
Modification of values
IIT BOMBAY
• Database tables can be modified by
any one of the following
• Insert
• Update
• Delete
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
8
Insertion of rows
IIT BOMBAY
• Insert record of a new student joining
the institute
Insert into student
Values (‘02D01234’, ‘alekh’, 02, 123,);
Insert into student (sroll, shostel)
Values (‘01007052’, 08);
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
9
Another insertion example
IIT BOMBAY
Insert into reg
Select sroll, ‘CS771’
From reg
where ccode = ‘CS634’
And sroll in (select sroll from student
Where scpi > 9.5);
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
10
Modification of values
IIT BOMBAY
• Change the grades of all CS634
students to ‘AA’
Update reg
Set grade = ‘AA’
Where ccode = ‘CS634’;
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
11
Modification of values
IIT BOMBAY
• Increase faculty salaries by 50%
Update faculty
Set salary = salary * 1.50;
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
12
Deletion
IIT BOMBAY
• Delete registration records of ‘CS634’
for students with CPI < 6.5
Delete from reg
Where sroll in
(select sroll from student natural join reg
Where scpi < 6.5 and ccode = ‘CS634’);
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
13
Another example of deletion
IIT BOMBAY
- Remove all students of Hostel 8
Delete From Student
Where shostel = 8;
- Fire all faculty
Delete from Faculty;
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
14
Authorization
IIT BOMBAY
• It is obvious that the powerful SQL
features for data manipulation may
be misused to corrupt the contents
of our database
• Data Base Administrator (DBA) has
full rights of access
• Other users are given selective
permissions using ‘Grant’ statement.
• These can be revoked later
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
15
Insertion into views
IIT BOMBAY
• Modification of values in a view
actually means modification of
values in the base table (s)
• Modifications permitted only in
simple views
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
16
Schema modifications
IIT BOMBAY
• Data changes occur regularly
• Schema changes are infrequent
• Must be handled with great care
• Alter table command
• When the table structure is changed,
what happens to values of existing and
new columns?
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
17
Authorization
IIT BOMBAY
• It is obvious that the powerful SQL
features for data manipulation may
be misused to corrupt the contents
of our database
• Data Base Administrator (DBA) has
full rights of access
• Other users are given select
permissions using ‘Grant’ statement,
which can be revoked later
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
18
SQL transactions
IIT BOMBAY
•
•
•
•
Transaction concept
ACID properties
SQL features supporting transactions
Special cases
• Long transactions
• Distributed transactions
• Replication
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
19
Transaction concept
IIT BOMBAY
• A transaction is a unit of program
execution that accesses and possibly
updates various data items.
• A transaction is normally expected to
support ACID properties to preserve
data integrity
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
20
Transaction concept (contd.)
IIT BOMBAY
• A transaction must see a consistent
database. It must also leave behind a
consistent database
• During transaction execution the
database may be inconsistent.
• When the transaction is
committed, the database must be
consistent.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
21
Transaction concept (contd.)
IIT BOMBAY
• Two main issues to deal with:
• Failures of various kinds
• hardware failures
• system crashes
• Concurrent execution of multiple
transactions
• Different transactions trying to
read/update same rows/columns
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
22
Example of fund transfer
IIT BOMBAY
•
Transaction to transfer Rs. 50
from account A to account B:
1. Read(A)
2. A := A – 50
3. Write(A)
4. Read(B)
5. B := B + 50
6. Write(B)
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
23
Example of fund transfer
IIT BOMBAY
1. Read(A)
2. A := A – 50
3. Write(A)
Update Accounts
Set Bal = Bal - 50
Where acode = ‘A’;
4. Read(b)
5. B := B + 50
6. Write(B)
Update Accounts
Set Bal = Bal + 50
Where acode = ‘B’;
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
24
ACID properties
IIT BOMBAY
• Atomicity. Either all operations of
the transaction are properly reflected
in the database or none are.
• Consistency. Execution of a
transaction in isolation preserves the
consistency of the database.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
25
Atomicity and consistency
IIT BOMBAY
• Atomicity requirement — if the
transaction fails after step 3 and
before step 6, the system should
ensure that its updates are not
reflected in the database, else an
inconsistency will result.
• Consistency requirement – the sum of
a and b is unchanged by the execution
of the transaction.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
26
Acid properties (contd.)
IIT BOMBAY
• Isolation. Although multiple
transactions may execute
concurrently, each transaction
must be unaware of other
concurrently executing
transactions.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
27
Isolation
IIT BOMBAY
• Intermediate transaction results
must be hidden from other
concurrently executed transactions.
• That is, for every pair of
transactions ti and tj, it appears to
ti that, either tj finished execution
before ti started, or tj started
execution after ti finished.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
28
Possible wrong effects of concurrency
IIT BOMBAY
Assume Deepak has Rs 14500 in ‘A’
T1 (Deepak)
1. Read(A)
T2 (Pratibha)
2. Read (A)
3. A := A - 14500
4. A := A – 50
6.
7.
8.
9.
Write(A)
Read(B)
B := B + 50
Write(B)
Dr. Deepak B Phatak
5. Write (A)
CS634-Session 7, SQL-transactions
29
Isolation
IIT BOMBAY
• If, during the transaction T1, another
transaction T2 reads the value of ‘A’
and attempts to modify it
• Wrong balance for ‘A’ will result at
the end of these transactions
• Transaction may be illegal
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
30
Isolation
IIT BOMBAY
• Isolation requirement — if another
transaction is allowed to access the
partially updated database, it will see
an inconsistent database (the sum A +
B will be less than what it should be)
• Can be ensured trivially by running
transactions serially, that is one after
the other. However, executing
multiple transactions concurrently has
significant benefits.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
31
Acid properties (contd.)
IIT BOMBAY
• Durability. After A transaction
completes successfully, the changes it
has made to the database persist,
even if there are system failures.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
32
Durability
IIT BOMBAY
• Durability requirement — once the user
has been notified that the transaction
has completed (i.e., The transfer of
the Rs. 50 has taken place), the
updates to the database by the
transaction must persist despite
failures.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
33
SQL transactions
IIT BOMBAY
• Normal syntax
• Special prescription
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
34
SQL transactions (contd.)
IIT BOMBAY
• In most database systems, each SQL
statement that executes successfully is
automatically committed.
• Each transaction would then consist of
only a single statement
• Automatic commit can usually be
turned off, allowing multi-statement
transactions, but how to do so depends
on the database system
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
35
SQL transactions
IIT BOMBAY
• A transaction is A sequence of queries
and update statements executed as A
single unit
• Transactions are started implicitly
and terminated by one of
• Commit work: makes all updates
of the transaction permanent in
the database
• Rollback work: undoes all updates
performed by the transaction.
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
36
SQL transactions
IIT BOMBAY
• If any step of a transaction fails, all
work done by the transaction can be
undone by rollback work.
• Rollback of incomplete transactions is
done automatically, in case of system
failures
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
37
SQL transactions (contd.)
IIT BOMBAY
• Another option in SQL, enclose
statements within
begin atomic
…
…
end;
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
38
Course project
IIT BOMBAY
• Campus Activity Information System
• Hostel activities
• Sports activities
• Cultural activities
• Academic activities
• Educational
• Research and Development
• Events, for example
• Mood Indigo
• Techfest
• Ecell Activities
• Other activities
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
39
Activities
IIT BOMBAY
• Each group will give its choice by
January 30
• Project allocation, and formation of the
teams will be announced after the
tutorial on 31st January
• Submission Schedule for project work
• System Requirement Specification
• Draft SRS due on 5 February
• Final SRS due on 15 February
• Mid Semester Examination??
• Monday 19 February, 1700 Hrs
Dr. Deepak B Phatak
CS634-Session 7, SQL-transactions
40