System Aspects of SQL
Binding Styles <ul><li>SQL2 implementations are required to support at least one of the following seven host languages: AD...
Embedded SQL Approach  <ul><li>Embed SQL statements directly into the text of host language program </li></ul><ul><ul><li>...
Impedance Mismatch <ul><li>Data model of SQL differs from the models of programming languages </li></ul><ul><ul><li>SQL us...
The SQL/host language interface <ul><li>Transfer of data bet. DBMS and host-language program is through  shared  variables...
The DECLARE section <ul><li>EXEC SQL BEGIN DECLARE SECTION; Declaration of shared variables goes here … EXEC SQL END DECLA...
Using shared variables <ul><li>Any SQL statement that does not return a result can be embedded (e.g. delete- and update-, ...
Single-row select statements <ul><li>select-from-where queries are not directly embeddable due to impedance mismatch </li>...
Example of single-row select statements <ul><ul><li>void printNetWorth() {  /* read the name of a studio and print the  ne...
Cursor <ul><li>To create and use a cursor </li></ul><ul><ul><li>Cursor declaration EXEC SQL DECLARE <cursor_name> CURSOR F...
Cursor Example <ul><ul><ul><li>#define NO_MORE_TUPLES !(strcmp(SQLSTATE, “02000”)) void worthRanges() { int i, digits, cou...
Modifications by cursor <ul><ul><li>void changeWorth() { exec sql begin declare section;   int worth; char sqlstate[6];  e...
Cursor options <ul><li>SQL2 offers a variety of options for cursors </li></ul><ul><li>Summary: </li></ul><ul><ul><li>the o...
Ordering tuples for fetching <ul><li>To specify an order, use  ORDER BY  </li></ul><ul><ul><li>Exec sql declare movieStarC...
What is correct behavior of  myCursor? <ul><ul><li>int sumyear = 0; exec sql declare myCursor cursor for  select year from...
Cursor sensitivity <ul><li>There are three types of cursors </li></ul><ul><ul><li>Sensitive cursor </li></ul></ul><ul><ul>...
Read-only cursor <ul><li>if you do not have an intention to update relation through cursor, then declare it with “FOR READ...
Scrolling cursors <ul><li>When there are other orders in which tuples may be fetched, declare the cursor with SCROLL </li>...
Scrolling cursor example <ul><ul><li>exec sql declare execCursor SCROLL cursor for </li></ul></ul><ul><ul><li>select netWo...
Dynamic SQL <ul><li>For some applications, it is much more convenient to construct parts of the SQL query dynamically (i.e...
PREPARE statements <ul><li>Steps in dynamic SQL </li></ul><ul><ul><li>Generate the necessary SQL statements dynamically (i...
EXECUTE statements <ul><li>EXECUTE statement </li></ul><ul><ul><li>Executes the prepared statement actually </li></ul></ul...
Dynamic SQL example <ul><ul><li>void readQuery() { </li></ul></ul><ul><ul><li>exec sql begin declare section; </li></ul></...
SQL Descriptor Area (1) <ul><li>An memory area that is used to transfer the input parameter or output parameter when the d...
SQL Descriptor Area (2) <ul><li>relevant SQL statement </li></ul><ul><ul><li>ALLOCATE DESCRIPTOR </li></ul></ul><ul><ul><l...
Transaction <ul><li>a sequence of database operations that have ACID properties </li></ul><ul><li>syntax in ESQL/C </li></...
Properties of a transaction: ACID <ul><li>atomicity </li></ul><ul><ul><li>either all actions in a transaction occur succes...
Problems with nonserializable execution <ul><li>There are three cases in which nonserializable execution causes problems, ...
Transaction in ESQL/C
Serial vs. serializable <ul><li>A complete history  H  is  serial  if, for every two transaction T i  and T j  that appear...
Why concurrency control? <ul><li>To provide a transaction with ACID properties </li></ul><ul><ul><li>give each transaction...
Practice and theory <ul><li>Surprisingly, most systems do not provide true isolation! </li></ul><ul><li>A compromise betwe...
Isolation degree
Isolation theorem <ul><li>Isolation theorem: if a transaction observes the degree-1,2,3 lock protocol, then any legal hist...
Transaction isolation in SQL2 <ul><li>SET TRANSACTION [READ ONLY | READ WRITE]  ISOLATION LEVEL … </li></ul><ul><ul><li>re...
Difference of isolation <ul><li>Transaction approach </li></ul><ul><ul><li>Find an available seat and reserve it by settin...
SQL environment  <ul><li>SQL environment </li></ul><ul><ul><li>A particular instance of a particular SQL DBMS with the col...
Schema <ul><li>A named collection of “descriptors” within a catalog </li></ul><ul><li>SQL objects (such as base tables, vi...
Create schema statement <ul><li>CREATE SCHEMA [schema_name]  [AUTHORIZATION user] [DEFAULT CHARACTER SET character-set] [s...
Example of schema creation <ul><ul><li>CREATE SCHEMA MovieSchema </li></ul></ul><ul><ul><li>CREATE DOMAIN CertDomain …  </...
Drop schema statement <ul><li>Not necessary to declare the schema all at once </li></ul><ul><ul><li>One can modify or add ...
Catalog, cluster <ul><li>Cluster </li></ul><ul><ul><li>A collection of catalogs </li></ul></ul><ul><ul><li>Maximum scope o...
Clients and servers in the SQL environment <ul><li>Within an SQL environment are two special kinds of processes: </li></ul...
SQL-Connection <ul><li>In order to perform any database operation, the SQL-agent must first cause the SQL-client to establ...
CONNECT statements <ul><li>Connection manipulation </li></ul><ul><ul><li>CONNECT statement </li></ul></ul><ul><ul><li>SET ...
DISCONNECT statement <ul><li>SQL2 allows several connections to be opened by the user, but only one can be active at any t...
Session <ul><li>The SQL operations that are performed while a connection is active form a session </li></ul><ul><li>the se...
Session-related statements <ul><li>session manipulation </li></ul><ul><ul><li>SET SESSION AUTHORIZATION statement </li></u...
Security and user authorization in SQL2 <ul><li>SQL2 postulates authorization ID </li></ul><ul><li>A special ID  PUBLIC  w...
Privileges <ul><li>Six types of privileges </li></ul><ul><ul><li>SELECT, INSERT, DELETE, UPDATE, REFERENCES, USAGE </li></...
Privilege example <ul><ul><li>Insert into Studio(name)  select distinct studioName </li></ul></ul><ul><ul><li>from Movie <...
Creating privileges <ul><li>SQL elements such as schemas or modules has an owner </li></ul><ul><li>There are three points ...
The privilege-checking process <ul><li>Any SQL operation has two parties </li></ul><ul><ul><li>the database elements upon ...
Granting privileges <ul><li>SQL2 provides a GRANT statement to allow one user to give a privilege to another. </li></ul><u...
Example of Grant statements <ul><ul><li>Movie (title, year, length, inColor, studioName, producerC#) Studio (name, address...
Grant diagrams <ul><ul><li>“*” indicates the grant option </li></ul></ul><ul><ul><li>“**” indicates ownership of the datab...
Revoking privileges  <ul><li>A granted privilege can be revoked at any time </li></ul><ul><ul><li>REVOKE <privilege list> ...
Example of revoking privileges <ul><ul><li>Revoke select, insert on Studio from picard cascade; </li></ul></ul><ul><ul><li...
Example U INSERT on R ** V INSERT(A) on R V INSERT on R After step (2) U INSERT on R ** V INSERT(A) on R After step (3)
Another example U p ** V p * W p U p ** V p After step (2) After step (3)
Upcoming SlideShare
Loading in …5
×

System Aspects of SQL 2

526 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
526
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

System Aspects of SQL 2

  1. 1. System Aspects of SQL
  2. 2. Binding Styles <ul><li>SQL2 implementations are required to support at least one of the following seven host languages: ADA, C, Cobol, Fortran, M (Mumps), Pascal, PL/1 </li></ul><ul><li>Also, required to support at least one of the following binding-styles </li></ul><ul><ul><li>direct SQL (Interactive SQL) </li></ul></ul><ul><ul><ul><li>refers to direct execution of SQL statements from an interactive terminal </li></ul></ul></ul><ul><ul><li>Module </li></ul></ul><ul><ul><ul><li>Example procedure delete_part(SQLSTATE, :PNO_PARAM CHAR(6)); Delete from P where P.PNO= :PNO_PARAM; This procedure can be invoked from host programming as below … PNO_ARG = ‘p2’; CALL DELETE_PART(retcode, PNO_ARG); … </li></ul></ul></ul><ul><ul><ul><li>Acts as an external procedure that is invoked by host program </li></ul></ul></ul><ul><ul><li>Embedded SQL (ESQL) </li></ul></ul>
  3. 3. Embedded SQL Approach <ul><li>Embed SQL statements directly into the text of host language program </li></ul><ul><ul><li>ESQL/C, ESQL/Cobol, etc. </li></ul></ul><ul><li>Mostly commonly used </li></ul><ul><li>Embedded SQL statements are prefixed by EXEC SQL </li></ul>Host language Embedded SQL + Preprocessor Host language Function calls + Host language compiler Host language program SQL library
  4. 4. Impedance Mismatch <ul><li>Data model of SQL differs from the models of programming languages </li></ul><ul><ul><li>SQL uses the relational data model </li></ul></ul><ul><ul><li>C or other program language use a data model with integers, reals, etc., and does not support a notion of set and bag </li></ul></ul><ul><ul><li>a mechanism must be devised to allow the development of programs that use both SQL and another language </li></ul></ul><ul><li>What about a single-language approach ??? </li></ul><ul><li>Real database programming requires both SQL and a conventional language ( host language ) </li></ul>
  5. 5. The SQL/host language interface <ul><li>Transfer of data bet. DBMS and host-language program is through shared variables of the host language </li></ul><ul><ul><li>All shared variables are prefixed by a colon when they are referred to by an SQL statement </li></ul></ul><ul><ul><li>they appear without the colon in host-language statements </li></ul></ul><ul><li>Exception handling </li></ul><ul><ul><li>After execution of a SQL statement, certain status code is placed in special parameters </li></ul></ul><ul><ul><li>Two state codes </li></ul></ul><ul><ul><ul><li>SQLCODE of integer type: SQL1 </li></ul></ul></ul><ul><ul><ul><li>SQLSTATE of CHAR(5): SQL2 </li></ul></ul></ul><ul><ul><li>SQLSTATE </li></ul></ul><ul><ul><ul><li>class code: first two characters </li></ul></ul></ul><ul><ul><ul><li>subclass code: the rest three characters </li></ul></ul></ul><ul><ul><ul><li>example: 00000 (successful execution), 02000 (no tuple found) </li></ul></ul></ul>
  6. 6. The DECLARE section <ul><li>EXEC SQL BEGIN DECLARE SECTION; Declaration of shared variables goes here … EXEC SQL END DECLARE SECTION; </li></ul><ul><li>Example </li></ul><ul><ul><li>exec sql begin declare section; </li></ul></ul><ul><ul><li>char studioName[15], studioAddr[50]; </li></ul></ul><ul><ul><li>char sqlstate[6]; // Note, it is of 6 chars </li></ul></ul><ul><ul><li>exec sql end declare section; </li></ul></ul>
  7. 7. Using shared variables <ul><li>Any SQL statement that does not return a result can be embedded (e.g. delete- and update-, etc) </li></ul><ul><ul><li>void getStudio() { </li></ul></ul><ul><ul><li> exec sql begin declare section; </li></ul></ul><ul><ul><li>char studioName[15], studioAddr[50]; </li></ul></ul><ul><ul><li>char sqlstate[6]; </li></ul></ul><ul><ul><li>exec sql end declare section; </li></ul></ul><ul><ul><li>/* print request that studio name and address be entered and </li></ul></ul><ul><ul><li> read response into variables studioName and studioAddr */ </li></ul></ul><ul><ul><li>exec sql insert into Studio(name, address) </li></ul></ul><ul><ul><li>values (:studioName, :studioAddr); } </li></ul></ul>
  8. 8. Single-row select statements <ul><li>select-from-where queries are not directly embeddable due to impedance mismatch </li></ul><ul><ul><li>Use shared variable for a query that produces a single tuple ( single-row select ) </li></ul></ul><ul><ul><li>Use cursor, otherwise </li></ul></ul><ul><li>Single-row select </li></ul><ul><ul><li>SELECT <attribute-name(s)> INTO <shared variable(s)> FROM <table-name> WHERE <condition> </li></ul></ul><ul><ul><li>If result of the query is either no tuple or more than one tuple, no assignment and appropriate code is written in SQLSTATE </li></ul></ul>
  9. 9. Example of single-row select statements <ul><ul><li>void printNetWorth() { /* read the name of a studio and print the netWorth of the studio’s president */ exec sql begin declare section; </li></ul></ul><ul><ul><li>char studioName[15]; </li></ul></ul><ul><ul><li>int presNetWorth; </li></ul></ul><ul><ul><li>char sqlstate[6]; </li></ul></ul><ul><ul><li>exec sql end declare section; </li></ul></ul><ul><ul><li>/* Receive studio name be entered. Read response into studioName*/ </li></ul></ul><ul><ul><li>exec sql select netWorth into :presNetWorth </li></ul></ul><ul><ul><li>from Studio, MovieExec </li></ul></ul><ul><ul><li>where presC# = cert# and Studio.name = :studioName; </li></ul></ul><ul><ul><li>/* Check SQLSTATE. If all 0’s, print the value of presNetWorth*/ } </li></ul></ul>
  10. 10. Cursor <ul><li>To create and use a cursor </li></ul><ul><ul><li>Cursor declaration EXEC SQL DECLARE <cursor_name> CURSOR FOR <query> </li></ul></ul><ul><ul><ul><li><query> denotes a relation name or a select-from-where expression, whose value is a relation </li></ul></ul></ul><ul><ul><li>EXEC SQL OPEN <cursor_name> </li></ul></ul><ul><ul><li>One or more uses of a fetch statement. EXEC SQL FETCH FROM <cursor_name> INTO <list of variables> </li></ul></ul><ul><ul><li>EXEC SQL CLOSE <cursor_name> </li></ul></ul>
  11. 11. Cursor Example <ul><ul><ul><li>#define NO_MORE_TUPLES !(strcmp(SQLSTATE, “02000”)) void worthRanges() { int i, digits, counts[15]; exec sql begin declare section; int worth; char sqlstate[6]; exec sql end declare section; exec sql declare execCursor cursor for select netWorth from MovieExec; for (i = 0; i < 15; i++) counts[i] = 0; exec sql open execCursor; while (1) {exec sql fetch from execCursor into :worth; if (NO_MORE_TUPLES) break; digits = 1; while (worth /= 10) > 0) digits++; if (digits <= 14) counts[digits]++; } exec sql close execCursor; for (i = 0; i < 15; i++) printf(“digits = %d: number of execs = %d ”, i, counts[i]); } </li></ul></ul></ul>
  12. 12. Modifications by cursor <ul><ul><li>void changeWorth() { exec sql begin declare section; int worth; char sqlstate[6]; exec sql end declare section; exec sql declare execCursor cursor for select netWorth from MovieExec; exec sql open execCursor; while (1) { exec sql fetch from execCursor into :worth; if (NO_MORE_TUPLES) break; if (worth < 1000) exec sql delete from MovieExec where current of execCursor; else exec sql update MovieExec set netWorth = 2 * netWorth where current of execCursor; } exec sql close execCursor; } </li></ul></ul>
  13. 13. Cursor options <ul><li>SQL2 offers a variety of options for cursors </li></ul><ul><li>Summary: </li></ul><ul><ul><li>the order in which tuples are fetched from the relation can be specified </li></ul></ul><ul><ul><li>the effect of changes to the relation that the cursor ranges over can be limited </li></ul></ul><ul><ul><li>the motion of the cursor through the list of tuples can be varied </li></ul></ul>
  14. 14. Ordering tuples for fetching <ul><li>To specify an order, use ORDER BY </li></ul><ul><ul><li>Exec sql declare movieStarCursor cursor for </li></ul></ul><ul><ul><li>select title, year, studio, starName </li></ul></ul><ul><ul><li>from Movie, StarsIn </li></ul></ul><ul><ul><li>where title = movieTitle and year = movieYear </li></ul></ul><ul><ul><li>order by year, title; </li></ul></ul>
  15. 15. What is correct behavior of myCursor? <ul><ul><li>int sumyear = 0; exec sql declare myCursor cursor for select year from Movie where length > 100; exec sql open myCursor; loop: exec sql fetch from myCursor into :year; If no data found, go to finished; sumyear = sumyear + year; exec sql delete from Movie where inColor = ‘F’; goto loop; finished: exec sql close myCursor; compute something … ; </li></ul></ul>
  16. 16. Cursor sensitivity <ul><li>There are three types of cursors </li></ul><ul><ul><li>Sensitive cursor </li></ul></ul><ul><ul><li>Insensitive cursor </li></ul></ul><ul><ul><li>Indeterminant cursor </li></ul></ul><ul><li>Insensitive cursor guarantees that the rows seen by the cursor will not be affected by other statement executed as part of the same transaction </li></ul><ul><ul><li>Use keyword INSENSITIVE EXEC SQL DECLARE myCursor INSENSITIVE CURSOR FOR … </li></ul></ul><ul><ul><li>Only read-only cursor can be insensitive </li></ul></ul><ul><ul><li>One way to implement insensitive cursor is to make a temporary table when the cursor is opened </li></ul></ul><ul><ul><li>An absence of INSENSITIVE doesn’t mean that it is sensitive (in SQL2) </li></ul></ul><ul><li>Sensitive cursor will be a part of SQL3 </li></ul>
  17. 17. Read-only cursor <ul><li>if you do not have an intention to update relation through cursor, then declare it with “FOR READ ONLY” </li></ul><ul><ul><li>An attempt to execute an UPDATE or DELETE through read-only cursor would cause an error </li></ul></ul><ul><ul><li>Exec sql declare movieStarCursor cursor for </li></ul></ul><ul><ul><li>select title, year, studio, starName </li></ul></ul><ul><ul><li>from Movie, StarsIn </li></ul></ul><ul><ul><li>where title = movieTitle and year = movieYear </li></ul></ul><ul><ul><li>order by year, title FOR READ ONLY; </li></ul></ul>
  18. 18. Scrolling cursors <ul><li>When there are other orders in which tuples may be fetched, declare the cursor with SCROLL </li></ul><ul><ul><li>Put the keyword SCROLL before the keyword CURSOR </li></ul></ul><ul><ul><li>in a FETCH statement, follow the keyword FETCH by one of several options </li></ul></ul><ul><ul><ul><li>NEXT or PRIOR. </li></ul></ul></ul><ul><ul><ul><li>FIRST or LAST </li></ul></ul></ul><ul><ul><ul><li>RELATIVE followed by a positive or negative integer </li></ul></ul></ul><ul><ul><ul><li>ABSOLUTE followed by a positive or negative integer </li></ul></ul></ul>
  19. 19. Scrolling cursor example <ul><ul><li>exec sql declare execCursor SCROLL cursor for </li></ul></ul><ul><ul><li>select netWorth from MovieExec; exec sql open execCursor; </li></ul></ul><ul><ul><li>exec sql fetch LAST from execCursor into :worth; </li></ul></ul><ul><ul><li>while(1) { </li></ul></ul><ul><ul><li>/* same as before form */ </li></ul></ul><ul><ul><li>exec sql fetch prior from execCursor into :worth; } … </li></ul></ul><ul><ul><li>exec sql fetch ABSOLUTE -3 from execCursor; </li></ul></ul><ul><ul><li>exec sql fetch RELATIVE 4 from execCursor; </li></ul></ul>
  20. 20. Dynamic SQL <ul><li>For some applications, it is much more convenient to construct parts of the SQL query dynamically (i.e. at run time) and then to bind and execute them dynamically </li></ul><ul><ul><li>Such statements are not known at compile time, hence cannot be handled by an SQL preprocessor or a host-language compiler </li></ul></ul><ul><li>Possible applications </li></ul><ul><ul><li>ad hoc query utilities </li></ul></ul><ul><ul><li>natural language interface </li></ul></ul><ul><ul><li>on-line applications </li></ul></ul><ul><ul><li>database design tools </li></ul></ul>
  21. 21. PREPARE statements <ul><li>Steps in dynamic SQL </li></ul><ul><ul><li>Generate the necessary SQL statements dynamically (i.e. at run time, on an “as-needed” basis) </li></ul></ul><ul><ul><li>Compile (prepare) such statements dynamically by invoking the SQL compiler at run time </li></ul></ul><ul><ul><li>Execute the newly compiled statements when necessary </li></ul></ul><ul><li>PREPARE statement </li></ul><ul><ul><li>Takes the source statement and prepares (i.e. compiles) it to produce an executable version </li></ul></ul><ul><ul><li>May have placeholders </li></ul></ul><ul><ul><li>Two modes: local, global </li></ul></ul><ul><ul><ul><li>local: belong to the module containing the PREPARE statement </li></ul></ul></ul><ul><ul><ul><li>global: belongs to SQL-session module </li></ul></ul></ul>
  22. 22. EXECUTE statements <ul><li>EXECUTE statement </li></ul><ul><ul><li>Executes the prepared statement actually </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>SQLSOURCE = ‘DELETE FROM SP WHERE QTY > ?’; </li></ul></ul></ul><ul><ul><ul><li>EXEC SQL PREPARE X FROM :SQLSOURCE; </li></ul></ul></ul><ul><ul><ul><li>ARG = 100; </li></ul></ul></ul><ul><ul><ul><li>EXEC SQL EXECUTE X USING :ARG; </li></ul></ul></ul><ul><li>EXECUTE IMMEDIATE statement </li></ul><ul><ul><li>Combines PREPARE and EXECUTE </li></ul></ul><ul><ul><li>Doesn’t contain any placeholder </li></ul></ul><ul><ul><li>Used when it is executed only once </li></ul></ul>
  23. 23. Dynamic SQL example <ul><ul><li>void readQuery() { </li></ul></ul><ul><ul><li>exec sql begin declare section; </li></ul></ul><ul><ul><li>char *query; </li></ul></ul><ul><ul><li>exec sql end declare section; </li></ul></ul><ul><ul><li>/* prompt user for a query, allocate space (e.g., use malloc) and make shared variable :query point to the first character of the query */ </li></ul></ul><ul><ul><li>exec sql prepare SQLquery from :query; </li></ul></ul><ul><ul><li>exec sql execute SQLquery; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  24. 24. SQL Descriptor Area (1) <ul><li>An memory area that is used to transfer the input parameter or output parameter when the dynamic SQL statement is executed </li></ul><ul><li>Generally speaking, SQLDA describes </li></ul><ul><ul><li>a column of the table that results from executing some prepared statement </li></ul></ul><ul><ul><li>an argument to replace some placeholder in some prepared statement </li></ul></ul><ul><li>Standard SQLDA is encapsulated; the internal structure is not visible to the user, and is not explicitly specified in the standard </li></ul>
  25. 25. SQL Descriptor Area (2) <ul><li>relevant SQL statement </li></ul><ul><ul><li>ALLOCATE DESCRIPTOR </li></ul></ul><ul><ul><li>DEALLOCATE DESCRIPTOR </li></ul></ul><ul><ul><li>DESCRIBE statement </li></ul></ul><ul><ul><ul><li>to place descriptor information for the prepared statement into some specified SQL descriptor area </li></ul></ul></ul><ul><ul><ul><li>GET DESCRIPTOR statement </li></ul></ul></ul><ul><ul><ul><li>SET DESCRIPTOR statement </li></ul></ul></ul>
  26. 26. Transaction <ul><li>a sequence of database operations that have ACID properties </li></ul><ul><li>syntax in ESQL/C </li></ul><ul><ul><li>start: most (but not all) SQL statements </li></ul></ul><ul><ul><li>not transaction-initiating statements </li></ul></ul><ul><ul><ul><li>CONNECT, DISCONNECT stmt </li></ul></ul></ul><ul><ul><ul><li>SET stmt </li></ul></ul></ul><ul><ul><ul><li>COMMIT, ROLLBACK </li></ul></ul></ul><ul><ul><ul><li>DECLARE stmts </li></ul></ul></ul><ul><ul><ul><li>GET DIAGNOSTICS </li></ul></ul></ul><ul><ul><li>end: COMMIT WORK, ROLLBACK WORK </li></ul></ul><ul><li>COMMIT </li></ul><ul><ul><li>indicates successful end of a transaction </li></ul></ul><ul><ul><li>all effects must be made permanently </li></ul></ul><ul><li>ROLLBACK </li></ul><ul><ul><li>indicates abnormal termination of a transaction </li></ul></ul><ul><ul><li>all effects must be obliterated </li></ul></ul>
  27. 27. Properties of a transaction: ACID <ul><li>atomicity </li></ul><ul><ul><li>either all actions in a transaction occur successfully or nothing has happened (all-or-nothing property) </li></ul></ul><ul><li>consistency </li></ul><ul><ul><li>assumes that any successful transaction commits only legal result </li></ul></ul><ul><ul><li>a transaction is a correct transformation of the state, i.e. from one valid state to another valid state </li></ul></ul><ul><li>isolation </li></ul><ul><ul><li>events within a transaction must be hidden from other transaction running concurrently </li></ul></ul><ul><ul><li>the actions carried out by a transaction against a shared database cannot become visible to other transitions until the transaction commits </li></ul></ul><ul><li>durability </li></ul><ul><ul><li>once a transaction has completed and has committed, the system must guarantee that these results survive any subsequent failures </li></ul></ul>
  28. 28. Problems with nonserializable execution <ul><li>There are three cases in which nonserializable execution causes problems, concurrency anomalies </li></ul><ul><li>dirty read </li></ul><ul><ul><li>T2 has seen dirty data </li></ul></ul><ul><li>lost update </li></ul><ul><ul><li>Effect (i.e. update) </li></ul></ul><ul><ul><li>of T2 is lost </li></ul></ul><ul><li>unrepeatable read </li></ul>
  29. 29. Transaction in ESQL/C
  30. 30. Serial vs. serializable <ul><li>A complete history H is serial if, for every two transaction T i and T j that appear in H , either all operations of T i appear before all operations of T j or vice versa </li></ul><ul><ul><li>there is no interleaving of operations of different transactions </li></ul></ul><ul><li>A history H is serializabl e if its committed projection is equivalent to a serial history </li></ul>
  31. 31. Why concurrency control? <ul><li>To provide a transaction with ACID properties </li></ul><ul><ul><li>give each transaction the impression “there are no concurrent updates” </li></ul></ul><ul><ul><li>hide concurrency anomalies </li></ul></ul><ul><li>Goal </li></ul><ul><ul><li>Generate serializable executions only </li></ul></ul><ul><li>Concurrency control techniques </li></ul><ul><ul><li>Locking </li></ul></ul><ul><ul><li>Timestamps </li></ul></ul><ul><ul><li>Multiversioning </li></ul></ul><ul><ul><li>Optimistic approach </li></ul></ul>
  32. 32. Practice and theory <ul><li>Surprisingly, most systems do not provide true isolation! </li></ul><ul><li>A compromise between correctness and performance </li></ul><ul><li>Degree of isolation </li></ul><ul><ul><li>degree 0: a degree-0 transaction does not overwrite another transaction’s dirty data if the other transaction is degree-1 or greater </li></ul></ul><ul><ul><li>degree 1: a degree-1 transaction has no lost updates </li></ul></ul><ul><ul><li>degree 2: a degree-2 transaction has no lost updates and no dirty reads </li></ul></ul><ul><ul><li>degree 3: a degree-3 transaction has no lost updates, no dirty reads, and repeatable read </li></ul></ul><ul><li>Most SQL system defaults to degree-2 and allow forms of degree-1 or degree-3 </li></ul>
  33. 33. Isolation degree
  34. 34. Isolation theorem <ul><li>Isolation theorem: if a transaction observes the degree-1,2,3 lock protocol, then any legal history will give that transaction degree-1,2,3 isolation respectively as long as other transactions are at least degree-1 </li></ul><ul><li>Implication: each user can select the isolation degree appropriate to his transaction without causing other concurrent transactions to malfunction with inconsistent inputs, as long as everybody runs at degree-1or higher </li></ul><ul><li>degree-1: </li></ul><ul><ul><li>read dirty data! </li></ul></ul><ul><ul><li>Usually for read-only transactions (e.g. UPDATE STATISTICS stmt) </li></ul></ul>
  35. 35. Transaction isolation in SQL2 <ul><li>SET TRANSACTION [READ ONLY | READ WRITE] ISOLATION LEVEL … </li></ul><ul><ul><li>read uncommitted: degree-1 </li></ul></ul><ul><ul><li>read committed: degree-2 </li></ul></ul><ul><ul><li>repeatable read: degree-2.99 (degree-3 except phantom) </li></ul></ul><ul><ul><li>serializable: degree-3 </li></ul></ul><ul><li>Default values </li></ul><ul><ul><li>READ WRITE </li></ul></ul><ul><ul><li>SERIALIZABLE </li></ul></ul><ul><ul><li>In case of READ UNCOMMITTED, READ ONLY </li></ul></ul>
  36. 36. Difference of isolation <ul><li>Transaction approach </li></ul><ul><ul><li>Find an available seat and reserve it by setting occupied to 1 If none, abort </li></ul></ul><ul><ul><li>Ask the customer for approval. If so, commit. If not, release the seat by setting occupied to 0 and repeat step 1 to get another seat </li></ul></ul><ul><li>Read Uncommitted </li></ul><ul><ul><li>A transaction may be notified that all seats are booked up, but seats will be released later. </li></ul></ul><ul><li>Read Committed </li></ul><ul><ul><li>It may see a different set of available seats each time it queries, as other transactions successfully book seats or cancel seats in parallel </li></ul></ul><ul><li>Repeatable read </li></ul><ul><ul><li>A seat that is available on the first query at step 1 will remain available at subsequent queries </li></ul></ul><ul><ul><li>If new seats are created during execution, a subsequent query for available seats may also retrieve the new seats </li></ul></ul>
  37. 37. SQL environment <ul><li>SQL environment </li></ul><ul><ul><li>A particular instance of a particular SQL DBMS with the collection of all databases and the collection of all users and programs </li></ul></ul>Schema Schema Catalog Environment = Installation of DBMS Catalog Catalog Cluster = maximum scope of a DB operation
  38. 38. Schema <ul><li>A named collection of “descriptors” within a catalog </li></ul><ul><li>SQL objects (such as base tables, views, integrity constraints, etc.) are always created within the context of some particular schema </li></ul><ul><ul><li>domain descriptor </li></ul></ul><ul><ul><li>base table descriptor </li></ul></ul><ul><ul><li>view descriptor </li></ul></ul><ul><ul><li>constraint descriptor </li></ul></ul><ul><ul><li>privilege descriptor </li></ul></ul><ul><ul><li>character set descriptor </li></ul></ul><ul><ul><li>collation descriptor </li></ul></ul><ul><ul><li>translation descriptor </li></ul></ul><ul><li>CREATE/DROP/SET SCHEMA statement </li></ul>
  39. 39. Create schema statement <ul><li>CREATE SCHEMA [schema_name] [AUTHORIZATION user] [DEFAULT CHARACTER SET character-set] [schema-element-list]; </li></ul><ul><ul><li>[AUTHORIZATION user] : denotes owner of new schema </li></ul></ul><ul><ul><li>The possible “schema elements’ are: </li></ul></ul><ul><ul><ul><li>domain definitions </li></ul></ul></ul><ul><ul><ul><li>base table definitions </li></ul></ul></ul><ul><ul><ul><li>view definitions </li></ul></ul></ul><ul><ul><ul><li>grant statements </li></ul></ul></ul><ul><ul><ul><li>constraint definitions </li></ul></ul></ul><ul><ul><ul><li>character set definitions </li></ul></ul></ul><ul><ul><ul><li>collation definitions </li></ul></ul></ul><ul><ul><ul><li>translation definitions </li></ul></ul></ul>
  40. 40. Example of schema creation <ul><ul><li>CREATE SCHEMA MovieSchema </li></ul></ul><ul><ul><li>CREATE DOMAIN CertDomain … </li></ul></ul><ul><ul><li>other domain declarations </li></ul></ul><ul><ul><li>CREATE TABLE MovieStar … </li></ul></ul><ul><ul><li>create-table statements for the four other tables </li></ul></ul><ul><ul><li>CREATE VIEW MovieProd … </li></ul></ul><ul><ul><li>other view declarations </li></ul></ul><ul><ul><li>CREATE ASSERTION RichPres ... </li></ul></ul>
  41. 41. Drop schema statement <ul><li>Not necessary to declare the schema all at once </li></ul><ul><ul><li>One can modify or add to a schema using the appropriate CREATE, DROP, ALTER statement </li></ul></ul><ul><li>One problem is that the SQL system needs to know to which schema the new table belongs </li></ul><ul><li>To change the “current” schema, use SET SCHEMA statement </li></ul><ul><ul><li>SET SCHMA <schema-name>; </li></ul></ul><ul><li>To drop schema </li></ul><ul><ul><li>DROP SCHEMA schema {RESTRICT | CASCADE}; </li></ul></ul><ul><ul><ul><li>RESTRICT: the operation will fail unless the specified schema is empty (i.e., contains no descriptor) </li></ul></ul></ul><ul><ul><ul><li>CASCADE: the operation will succeed, and drop not only the specified schema per se but also all objects contained in that schema </li></ul></ul></ul>
  42. 42. Catalog, cluster <ul><li>Cluster </li></ul><ul><ul><li>A collection of catalogs </li></ul></ul><ul><ul><li>Maximum scope over which a query can be issued </li></ul></ul><ul><ul><ul><li>SQL operations are not allowed to span clusters </li></ul></ul></ul><ul><ul><li>Each SQL-session has exactly one associated cluster </li></ul></ul><ul><ul><li>SQL2 is not very precise about what a cluster is </li></ul></ul><ul><li>Catalog </li></ul><ul><ul><li>A named collection of schema within an SQL environment </li></ul></ul><ul><ul><li>Each catalog contains a special schema INFORMATION_SCHEMA that contains information about all the schemas in the catalog </li></ul></ul><ul><ul><li>No explicit mechanism for creating and dropping catalogs in SQL2, i.e. implementation-dependent </li></ul></ul><ul><ul><ul><li>CREATE CATALOG <catalog name> </li></ul></ul></ul><ul><ul><li>SET CATALOG <catalog name> </li></ul></ul>
  43. 43. Clients and servers in the SQL environment <ul><li>Within an SQL environment are two special kinds of processes: </li></ul><ul><ul><li>SQL server: supports operations on the database elements </li></ul></ul><ul><ul><li>SQL client: allows a user to connect to a server </li></ul></ul><ul><li>A module is the SQL2 term for an application program </li></ul><ul><li>An execution of a module is called an SQL agent </li></ul>SQL-agent Module SQL-client SQL-server Connection Session Environment
  44. 44. SQL-Connection <ul><li>In order to perform any database operation, the SQL-agent must first cause the SQL-client to establish an SQL-connection to some SQL-server </li></ul><ul><ul><li>by executing an (implicit or explicit) CONNECT statement </li></ul></ul><ul><li>SQL-connection also initiates an SQL-session over SQL-connection </li></ul><ul><li>Each session has a current catalog and a current schema within that catalog </li></ul><ul><li>SQL-agent can establish any number of SQL-connections, and thus initiate any number of SQL-sessions </li></ul>
  45. 45. CONNECT statements <ul><li>Connection manipulation </li></ul><ul><ul><li>CONNECT statement </li></ul></ul><ul><ul><li>SET CONNECTION statement </li></ul></ul><ul><ul><li>DISCONNECT statement </li></ul></ul><ul><li>CONNECT statement </li></ul><ul><ul><li>CONNECT TO {DEFAULT | server_name [AS connection_name] [USER string3]}; </li></ul></ul><ul><ul><li>DEFAULT: an SQL-connection is established to a ‘”default SQL-server” </li></ul></ul>
  46. 46. DISCONNECT statement <ul><li>SQL2 allows several connections to be opened by the user, but only one can be active at any time </li></ul><ul><li>To switch among connections </li></ul><ul><ul><li>SET CONNECTION {DEFAULT | string}; </li></ul></ul><ul><li>To drop the connection, </li></ul><ul><ul><li>DISCONNECT {DEFAULT | CURRENT | ALL | string} </li></ul></ul><ul><ul><ul><li>DEFAULT : the default SQL-connection is terminated </li></ul></ul></ul><ul><ul><ul><li>CURRENT : the current SQL-connection is terminated </li></ul></ul></ul><ul><ul><ul><li>ALL : the current SQL-connection (if any) and all dormant SQL-connections (if any) are terminated </li></ul></ul></ul>
  47. 47. Session <ul><li>The SQL operations that are performed while a connection is active form a session </li></ul><ul><li>the session is coextensive with the connection that created it </li></ul><ul><ul><li>An SQL-session takes place in when the necessary SQL-connection is established and terminates when the last SQL-statement or the DISCONNECT statement is executed </li></ul></ul><ul><li>Each session has a number of associated default values </li></ul><ul><ul><li>a default authorization identifier </li></ul></ul><ul><ul><li>a default catalog name </li></ul></ul><ul><ul><li>a default schema name </li></ul></ul><ul><ul><li>a default time zone </li></ul></ul><ul><ul><li>a default character set name </li></ul></ul>
  48. 48. Session-related statements <ul><li>session manipulation </li></ul><ul><ul><li>SET SESSION AUTHORIZATION statement </li></ul></ul><ul><ul><li>SET CATALOG statement </li></ul></ul><ul><ul><li>SET SCHEMA statement </li></ul></ul><ul><ul><li>SET TIME ZONE statement </li></ul></ul><ul><ul><li>SET NAMES statement </li></ul></ul>
  49. 49. Security and user authorization in SQL2 <ul><li>SQL2 postulates authorization ID </li></ul><ul><li>A special ID PUBLIC which includes any user </li></ul><ul><li>authorization vs. authentication </li></ul><ul><li>SQL objects to be protected </li></ul><ul><ul><li>table, column, view, domain, character set, collation, translation </li></ul></ul>
  50. 50. Privileges <ul><li>Six types of privileges </li></ul><ul><ul><li>SELECT, INSERT, DELETE, UPDATE, REFERENCES, USAGE </li></ul></ul><ul><ul><li>The REFERENCES privilege is the right to refer to the relation in an integrity constraints. </li></ul></ul><ul><ul><li>The USAGE privilege on a domain, or on several other kind of schema elements other than relations and assertions, is the right to use that element in one’s own declarations </li></ul></ul><ul><ul><ul><li>domain, character, collation, translation </li></ul></ul></ul><ul><ul><li>The INSERT, UPDATE and REFERENCES privileges may also be given a single attribute as an argument. </li></ul></ul>
  51. 51. Privilege example <ul><ul><li>Insert into Studio(name) select distinct studioName </li></ul></ul><ul><ul><li>from Movie </li></ul></ul><ul><ul><li>where studioName not in </li></ul></ul><ul><ul><li>(select name </li></ul></ul><ul><ul><li> from Studio); </li></ul></ul><ul><ul><li>the INSERT privilege on either Studio or attribute name of the Studio(name) </li></ul></ul><ul><ul><li>the SELECT privileges on Movie and Studio </li></ul></ul>
  52. 52. Creating privileges <ul><li>SQL elements such as schemas or modules has an owner </li></ul><ul><li>There are three points at which ownership is established in SQL2 </li></ul><ul><ul><li>when a schema is created </li></ul></ul><ul><ul><li>When a session is initiated by a CONNECT statement </li></ul></ul><ul><ul><ul><li>connect to Startfleet-sql-server AS conn1 user kirk; </li></ul></ul></ul><ul><ul><li>when a module is created </li></ul></ul><ul><ul><ul><li>authorization picard; </li></ul></ul></ul>
  53. 53. The privilege-checking process <ul><li>Any SQL operation has two parties </li></ul><ul><ul><li>the database elements upon which the operation is performed </li></ul></ul><ul><ul><li>the agent that causes the operation </li></ul></ul><ul><li>The privileges available to the agent derive from a particular authorization ID called the current authorization ID </li></ul><ul><ul><li>The module authorization ID if the module has an authorization ID </li></ul></ul><ul><ul><li>The session authorization ID if not </li></ul></ul><ul><li>SQL operation may be executed only if the current authorization ID possesses all the privileges needed to carry out the operation </li></ul>
  54. 54. Granting privileges <ul><li>SQL2 provides a GRANT statement to allow one user to give a privilege to another. </li></ul><ul><li>GRANT <privilege list> ON <database element> TO <user list> [WITH GRANT OPTION]; </li></ul><ul><ul><li><privilege list> </li></ul></ul><ul><ul><ul><li>SELECT, INSERT(name), etc. </li></ul></ul></ul><ul><ul><ul><li>ALL PRIVILEGES </li></ul></ul></ul><ul><ul><li><user list> </li></ul></ul><ul><ul><ul><li>list of one or more authorization IDs </li></ul></ul></ul><ul><ul><li>WITH GRAND OPTION </li></ul></ul>
  55. 55. Example of Grant statements <ul><ul><li>Movie (title, year, length, inColor, studioName, producerC#) Studio (name, address, presC#) </li></ul></ul><ul><ul><li>User “Janeway” is the owner </li></ul></ul><ul><ul><ul><li>grant select, insert on Studio to kirk, picard with grant option; </li></ul></ul></ul><ul><ul><ul><li>grant select on Movie to kirk, picard with grant option </li></ul></ul></ul><ul><ul><li>“ picard” grants to user “sisko” </li></ul></ul><ul><ul><ul><li>grant select, insert on Studio to sisko; </li></ul></ul></ul><ul><ul><ul><li>grant select on Movie to sisko; </li></ul></ul></ul><ul><ul><li>“ kirk” grants to “sisko” </li></ul></ul><ul><ul><ul><li>grant select, insert(name) on Studio to sisko; </li></ul></ul></ul><ul><ul><ul><li>grant select on Movie to sisko; </li></ul></ul></ul>
  56. 56. Grant diagrams <ul><ul><li>“*” indicates the grant option </li></ul></ul><ul><ul><li>“**” indicates ownership of the database element </li></ul></ul>Janeway SELECT on Movie ** Janeway INSERT on Movie ** Janeway INSERT on Studio ** Janeway SELECT on Studio ** Kirk INSERT on Studio * Kirk SELECT on Studio * Kirk SELECT on Movie * Sisko INSERT (name) on Studio Sisko INSERT on Studio Sisko SELECT on Movie Sisko SELECT on Studio Picard SELECT on Movie * Picard SELECT on Studio * Picard INSERT on Studio *
  57. 57. Revoking privileges <ul><li>A granted privilege can be revoked at any time </li></ul><ul><ul><li>REVOKE <privilege list> ON <database element> FROM <user list> [CASCADE | RESTRICT]; </li></ul></ul><ul><ul><li>CASCADE: </li></ul></ul><ul><ul><li>RESTRICT: </li></ul></ul><ul><ul><ul><li>the revoke statement cannot be executed if the cascading rule described in the previous item would result in the revoking of any privileges due to the revoked privileges having been passed on to others </li></ul></ul></ul><ul><ul><li>REVOKE GRANT OPTION FOR instead of REVOKE </li></ul></ul><ul><ul><ul><li>the privileges themselves remain </li></ul></ul></ul><ul><ul><ul><li>the option to grant them to others is removed. </li></ul></ul></ul>
  58. 58. Example of revoking privileges <ul><ul><li>Revoke select, insert on Studio from picard cascade; </li></ul></ul><ul><ul><li>Revoke select on Movie from picard cascade; </li></ul></ul>Janeway SELECT on Movie ** Janeway INSERT on Movie ** Janeway INSERT on Studio ** Janeway SELECT on Studio ** Kirk INSERT on Studio * Kirk SELECT on Studio * Kirk SELECT on Movie * Sisko INSERT (name) on Studio Sisko INSERT on Studio Sisko SELECT on Movie Sisko SELECT on Studio
  59. 59. Example U INSERT on R ** V INSERT(A) on R V INSERT on R After step (2) U INSERT on R ** V INSERT(A) on R After step (3)
  60. 60. Another example U p ** V p * W p U p ** V p After step (2) After step (3)

×