Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization.

Similar presentations


Presentation on theme: "1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization."— Presentation transcript:

1 1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization

2 2 7.1 SQL in a Programming Environment Host Languages: uAny conventional language can be a host language, that is, a language in which SQL calls are embedded. uThe use of a host/SQL combination allows us to do anything computable, yet still get the very-high-level SQL interface to the database.

3 3 7.1.1 Embedded SQL uKey idea: Use a preprocessor to turn SQL statements into procedure calls that fit with the host-language code surrounding. uAll embedded SQL statements begin with EXEC SQL, so the preprocessor can find them easily.

4 4 Always needed Shared Variables uTo connect SQL and the host-language program, the two parts must share some variables. uDeclarations of shared variables are bracketed by: EXEC SQL BEGIN DECLARE SECTION; EXEC SQL END DECLARE SECTION;

5 5 Use of Shared Variables uIn SQL, the shared variables must be preceded by a colon. wThey may be used as constants provided by the host-language program. wThey may get values from SQL statements and pass those values to the host-language program. uIn the host language, shared variables behave like any other variable.

6 6 Example: C Plus SQL ——Insert a new studio void printNetWorth() { EXEC SQL BEGIN DECLARE SECTION; char char studioName[15]; int presNetWorth; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* print request that studio name and address be entered and read response into variables studioName and studioAddr */ EXEC SQL INSERT INTO Studio(name,address) VALUES(:studioName, :studioAddr); }

7 7 Cursor Statements uDeclare a cursor c with: EXEC SQL DECLARE c CURSOR FOR ; uOpen and close cursor c with: EXEC SQL OPEN CURSOR c; EXEC SQL CLOSE CURSOR c; uFetch from c by: EXEC SQL FETCH c INTO ; wMacro NOT FOUND is true if and only if the FETCH fails to find a tuple.

8 8 7.1.2 Dynamic SQL uMost applications use specific queries and modification statements in their interaction with the database. wThus, we can compile the EXEC SQL … statements into specific procedure calls and produce an ordinary host-language program that uses a library. uWhat if the program is something like a generic query interface, that doesn’t know what it needs to do until it runs?

9 9 Two steps for Dynamic SQL 1)Preparing a query: EXEC SQL PREPARE FROM ; 2) Executing a query: EXEC SQL EXECUTE ; w“Prepare” = optimize query. wPrepare once, execute many times.

10 10 Example: A Generic Interface EXEC SQL BEGIN DECLARE SECTION; char query[MAX_LENGTH]; EXEC SQL END DECLARE SECTION; while(1) { /* issue SQL> prompt */ /* read user’s query into array query */ EXEC SQL PREPARE q FROM :query; EXEC SQL EXECUTE q; } q is an SQL variable representing the optimized form of whatever statement is typed into :query

11 11 7.2 Transactions 7.2.1/2 Serializability & Atomicity uDatabase systems are normally being accessed by many users or processes at the same time.(e.g. Airline reservations) wBoth queries and modifications. uUnlike Operating Systems, which support interaction of processes, a DMBS needs to keep processes from troublesome interactions.(e.g. Banking)

12 12 Example: Bad Interaction uYou and your spouse each take $100 from different ATM’s at about the same time. wThe DBMS better make sure one account deduction doesn’t get lost. uCompare: An OS allows two people to edit a document at the same time. If both write, one’s changes get lost.

13 13 7.2.3 Transactions uA 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.

14 14 ACID Transactions uA DBMS is expected to support “ACID transactions,” which are: wAtomic: Either the whole process is done or none is. wConsistent: Database constraints are preserved. wIsolated: It appears to the user as if only one process executes at a time. wDurable: Effects of a process do not get lost if the system crashes.

15 15 Transactions in SQL uSQL supports transactions, often behind the scenes. wEach statement issued at the generic query interface is a transaction by itself. wIn programming interfaces like Embedded SQL or PSM, a transaction begins the first time an SQL statement is executed and ends with the program or an explicit end.

16 16 COMMIT uThe SQL statement COMMIT causes a transaction to complete. wIt’s database modifications are now permanent in the database.

17 17 ROLLBACK uThe SQL statement ROLLBACK also causes the transaction to end, but by aborting. wNo effects on the database. uFailures like division by 0 can also cause rollback, even if the programmer does not request it.

18 18 7.4 Authorization uA file system identifies certain privileges on the objects (files) it manages. wTypically read, write, execute. uA file system identifies certain participants to whom privileges may be granted. wTypically the owner, a group, all users.

19 19 Privileges --- 1 uSQL identifies a more detailed set of privileges on objects (relations) than the typical file system. uNine privileges in all, some of which can be restricted to one column of one relation.

20 20 Privileges --- 2 uSome important privileges on a relation: 1.SELECT = right to query the relation. 2.INSERT = right to insert tuples. wMay apply to only one attribute. 3.DELETE = right to delete tuples. 4.UPDATE = right to update tuples. wMay apply to only one attribute.

21 21 Example: Privileges uFor the statement below: INSERT INTO Studio(name) SELECT DISTINCT studioName FROM Movie WHERE studioName NOT IN (SELECT name FROM Studio); uWe require privileges INSERT on Studio and SELECT on Studio and Movie.

22 22 Authorization ID’s uA user is referred to by authorization ID, typically their name. uThere is an authorization ID PUBLIC. wGranting a privilege to PUBLIC makes it available to any authorization ID.

23 23 Granting Privileges uYou have all possible privileges on the objects, such as relations, that you create. uYou may grant privileges to other users (authorization ID’s), including PUBLIC. uYou may also grant privileges WITH GRANT OPTION, which lets the grantee also grant this privilege.

24 24 The GRANT Statement uTo grant privileges, say: GRANT ON TO ; uIf you want the recipient(s) to be able to pass the privilege(s) to others add: WITH GRANT OPTION

25 25 Example: GRANT uSuppose you are the owner of Studio. You may say: GRANT SELECT, INSERT ON Studio TO kirk,picard;

26 26 Example: Grant Option uSuppose we also grant: GRANT SELECT, INSERT ON Studio TO kirk, picard; uNow, kirk and picard can not only select or insert any attribute of Studio, but can grant to others the privilege SELECT and INSERT on Studio.

27 27 Revoking Privileges uTo revoke privileges, say: REVOKE ON FROM ; uYour grant of these privileges can no longer be used by these users to justify their use of the privilege. wBut they may still have the privilege because they obtained it independently from elsewhere.

28 28 REVOKE Options uWe must append to the REVOKE statement either: 1.CASCADE. Now, any grants made by a revokee are also not in force, no matter how far the privilege was passed. 2.RESTRICT. If the privilege has been passed to others, the REVOKE fails as a warning that something else must be done to “chase the privilege down.”

29 29 Summary uEmbedded SQL: write programs that embed SQL queries in a conventional host language. uDynamic SQL: the host program may create character strings that are interpreted by the SQL system and executed. uTransaction: Atomic, Consistent, Isolated, Durable. uPrivileges: by using GRANT and REVOKE.

30 30 Exercises uRequired reading: 7.2, 7.4, Summary uRecommend reading: 7.1


Download ppt "1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization."

Similar presentations


Ads by Google