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

Slides:



Advertisements
Similar presentations
1 Lecture 10: Transactions. 2 The Setting uDatabase systems are normally being accessed by many users or processes at the same time. wBoth queries and.
Advertisements

Transactions and Locking Rose-Hulman Institute of Technology Curt Clifton.
UJeffrey D. Ullman uAnfang von: CS145 - Herbst Stanford University uOnline unter: Folien mit weißem Hintergrund wurden.
1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
1 SQL Authorization Privileges Grant and Revoke Grant Diagrams.
1 SQL Authorization Privileges Grant and Revoke Grant Diagrams.
1 Transactions Serializability Isolation Levels Atomicity.
1 Transactions, Views, Indexes Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data.
1 A Closer Look Chapter 2. 2 Underlying Concepts of Databases and Transaction Processing.
Dec 15, 2003Murali Mani Transactions and Security B term 2004: lecture 17.
Cs3431 Transactions, Logging and Security. cs3431 Transactions: What and Why? A set of operations on a database must appear as one “unit”. Example: Consider.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
1 The Big Picture of Databases We are particularly interested in relational databases Data is stored in tables.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
INTRODUCTION TO TRANSACTION PROCESSING CHAPTER 21 (6/E) CHAPTER 17 (5/E)
Agenda Journalling More Embedded SQL. Journalling.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity.
Transactions1 Unit of work on a database. Transactions2 Transactions, concept Logical unit of work on the database –Examples Transfer money from bank.
SCUJoAnne Holliday11–1 Schedule Today: u Transaction concepts. u Read Sections Next u Authorization and security.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
SQL Programming SQL in Application Programs
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
1 SQL Authorization (Chap. 8.7) Privileges Grant and Revoke Grant Diagrams.
1 Transactions Serializability Isolation Levels Atomicity.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Lecture 8: SQL Programming and Transactions Friday, January 24, 2003.
1 Transactions, Views, Indexes Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data This slides are from J. Ullman’s.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: CLI & JDBC & Security Ch Ch. 9.6 – Ch 10.1.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL (modified 11/11/2013)
1 System Aspects of SQL. 2 Binding Styles SQL2 implementations are required to support at least one of the following seven host languages: ADA, C, Cobol,
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
TRANSACTION PROCESSING 1. 2 Why Transactions? uDatabase systems are normally being accessed by many users or processes at the same time. wBoth queries.
Chapter 2 The Big Picture. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2 Databases We are particularly interested in relational databases.
SQL Environment.
Introduction to Database Systems, CS420
A Guide to SQL, Seventh Edition
Databases We are particularly interested in relational databases
Privileges Grant and Revoke Grant Diagrams
Privileges Grant and Revoke Grant Diagrams
Privileges Grant and Revoke Grant Diagrams
Schedule Today Transactions, Authorization. Sections
SQL Authorization Book: A First Course in Database Systems
Transaction Processing
CPSC-310 Database Systems
CPSC-310 Database Systems
Transactions Properties.
Privileges Grant and Revoke Grant Diagrams
Privileges Grant and Revoke Grant Diagrams
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Lecture 17: Systems Aspects of SQL
Lecture 05: SQL Systems Aspects
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

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

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.

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 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 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 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 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.

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 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 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

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 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.

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 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 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 COMMIT uThe SQL statement COMMIT causes a transaction to complete. wIt’s database modifications are now permanent in the database.

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.

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 Privileges 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 Privileges 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 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 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 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 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 Example: GRANT uSuppose you are the owner of Studio. You may say: GRANT SELECT, INSERT ON Studio TO kirk,picard;

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 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 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 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 Exercises uRequired reading: 7.2, 7.4, Summary uRecommend reading: 7.1