Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS640 Information Systems Dr Deepak B Phatak Subrao Nilekani Chair Professor Kanwal Rekhi Building, Department of CSE IIT Bombay Session 7, SQL DML and.

Similar presentations


Presentation on theme: "CS640 Information Systems Dr Deepak B Phatak Subrao Nilekani Chair Professor Kanwal Rekhi Building, Department of CSE IIT Bombay Session 7, SQL DML and."— Presentation transcript:

1 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

2 Dr. Deepak B Phatak2 IIT BOMBAY CS634-Session 7, SQL-transactions Session overview Views Data manipulation Changes in database contents Transactions Course Project

3 Dr. Deepak B Phatak3 IIT BOMBAY CS634-Session 7, SQL-transactions View definition To create a view, we use: Create view v as ; is any legal expression The view name is represented by ‘v’

4 Dr. Deepak B Phatak4 IIT BOMBAY CS634-Session 7, SQL-transactions Sample view Create view h8students as Select (sroll, sname, sroom, scpi) From student Where shostel = 8;

5 Dr. Deepak B Phatak5 IIT BOMBAY CS634-Session 7, SQL-transactions Sample query a view Select sname From h8students Where sroom > 300 And scpi > 9.00;

6 Dr. Deepak B Phatak6 IIT BOMBAY CS634-Session 7, SQL-transactions Sample query a view 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

7 Dr. Deepak B Phatak7 IIT BOMBAY CS634-Session 7, SQL-transactions Derived relation 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) );

8 Dr. Deepak B Phatak8 IIT BOMBAY CS634-Session 7, SQL-transactions Modification of values Database tables can be modified by any one of the following Insert Update Delete

9 Dr. Deepak B Phatak9 IIT BOMBAY CS634-Session 7, SQL-transactions Insertion of rows 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);

10 Dr. Deepak B Phatak10 IIT BOMBAY CS634-Session 7, SQL-transactions Another insertion example Insert into reg Select sroll, ‘CS771’ From reg where ccode = ‘CS634’ And sroll in (select sroll from student Where scpi > 9.5);

11 Dr. Deepak B Phatak11 IIT BOMBAY CS634-Session 7, SQL-transactions Modification of values Change the grades of all CS634 students to ‘AA’ Update reg Set grade = ‘AA’ Where ccode = ‘CS634’;

12 Dr. Deepak B Phatak12 IIT BOMBAY CS634-Session 7, SQL-transactions Modification of values Increase faculty salaries by 50% Update faculty Set salary = salary * 1.50;

13 Dr. Deepak B Phatak13 IIT BOMBAY CS634-Session 7, SQL-transactions Deletion 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’);

14 Dr. Deepak B Phatak14 IIT BOMBAY CS634-Session 7, SQL-transactions Another example of deletion - Remove all students of Hostel 8 Delete From Student Where shostel = 8; - Fire all faculty Delete from Faculty;

15 Dr. Deepak B Phatak15 IIT BOMBAY CS634-Session 7, SQL-transactions Authorization 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

16 Dr. Deepak B Phatak16 IIT BOMBAY CS634-Session 7, SQL-transactions Insertion into views Modification of values in a view actually means modification of values in the base table (s) Modifications permitted only in simple views

17 Dr. Deepak B Phatak17 IIT BOMBAY CS634-Session 7, SQL-transactions Schema modifications 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?

18 Dr. Deepak B Phatak18 IIT BOMBAY CS634-Session 7, SQL-transactions Authorization 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

19 Dr. Deepak B Phatak19 IIT BOMBAY CS634-Session 7, SQL-transactions SQL transactions Transaction concept ACID properties SQL features supporting transactions Special cases Long transactions Distributed transactions Replication

20 Dr. Deepak B Phatak20 IIT BOMBAY CS634-Session 7, SQL-transactions Transaction concept 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

21 Dr. Deepak B Phatak21 IIT BOMBAY CS634-Session 7, SQL-transactions Transaction concept (contd.) 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.

22 Dr. Deepak B Phatak22 IIT BOMBAY CS634-Session 7, SQL-transactions Transaction concept (contd.) 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

23 Dr. Deepak B Phatak23 IIT BOMBAY CS634-Session 7, SQL-transactions Example of fund transfer 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)

24 Dr. Deepak B Phatak24 IIT BOMBAY CS634-Session 7, SQL-transactions Example of fund transfer 1.Read(A) 2.A := A – 50 3.Write(A) 4.Read(b) 5.B := B + 50 6.Write(B) Update Accounts Set Bal = Bal - 50 Where acode = ‘A’; Update Accounts Set Bal = Bal + 50 Where acode = ‘B’;

25 Dr. Deepak B Phatak25 IIT BOMBAY CS634-Session 7, SQL-transactions ACID properties 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.

26 Dr. Deepak B Phatak26 IIT BOMBAY CS634-Session 7, SQL-transactions Atomicity and consistency 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.

27 Dr. Deepak B Phatak27 IIT BOMBAY CS634-Session 7, SQL-transactions Acid properties (contd.) Isolation. Although multiple transactions may execute concurrently, each transaction must be unaware of other concurrently executing transactions.

28 Dr. Deepak B Phatak28 IIT BOMBAY CS634-Session 7, SQL-transactions Isolation Intermediate transaction results must be hidden from other concurrently executed transactions. That is, for every pair of transactions t i and t j, it appears to t i that, either t j finished execution before t i started, or t j started execution after t i finished.

29 Dr. Deepak B Phatak29 IIT BOMBAY CS634-Session 7, SQL-transactions Possible wrong effects of concurrency 1. Read(A) 4. A := A – 50 6. Write(A) 7. Read(B) 8. B := B + 50 9. Write(B) 2. Read (A) 5. Write (A) Assume Deepak has Rs 14500 in ‘A’ T1 ( Deepak ) T2 (Pratibha) 3. A := A - 14500

30 Dr. Deepak B Phatak30 IIT BOMBAY CS634-Session 7, SQL-transactions Isolation 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

31 Dr. Deepak B Phatak31 IIT BOMBAY CS634-Session 7, SQL-transactions Isolation 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.

32 Dr. Deepak B Phatak32 IIT BOMBAY CS634-Session 7, SQL-transactions Acid properties (contd.) Durability. After A transaction completes successfully, the changes it has made to the database persist, even if there are system failures.

33 Dr. Deepak B Phatak33 IIT BOMBAY CS634-Session 7, SQL-transactions Durability 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.

34 Dr. Deepak B Phatak34 IIT BOMBAY CS634-Session 7, SQL-transactions SQL transactions Normal syntax Special prescription

35 Dr. Deepak B Phatak35 IIT BOMBAY CS634-Session 7, SQL-transactions SQL transactions (contd.) 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

36 Dr. Deepak B Phatak36 IIT BOMBAY CS634-Session 7, SQL-transactions SQL transactions 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.

37 Dr. Deepak B Phatak37 IIT BOMBAY CS634-Session 7, SQL-transactions SQL transactions 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

38 Dr. Deepak B Phatak38 IIT BOMBAY CS634-Session 7, SQL-transactions SQL transactions (contd.) Another option in SQL, enclose statements within begin atomic … … end;

39 Dr. Deepak B Phatak39 IIT BOMBAY CS634-Session 7, SQL-transactions Course project 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

40 Dr. Deepak B Phatak40 IIT BOMBAY CS634-Session 7, SQL-transactions Activities Each group will give its choice by January 30 Project allocation, and formation of the teams will be announced after the tutorial on 31 st 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


Download ppt "CS640 Information Systems Dr Deepak B Phatak Subrao Nilekani Chair Professor Kanwal Rekhi Building, Department of CSE IIT Bombay Session 7, SQL DML and."

Similar presentations


Ads by Google