Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.

Similar presentations


Presentation on theme: "1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics."— Presentation transcript:

1 1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics

2 2 Additional Topics Views Programmatic Interface

3 3 Views Views are Virtual tables They can be referenced in SELECT statements just like another table They can be referenced in INSERT, UPDATE, and DELETE when that makes sense Why Views?  Information hiding, etc. etc.  Integration with Privileges E.g. have SELECT privilege on view, not underlying table MySQL: Views 5.0 and later

4 4 Defining a View CREATE VIEW view_name(col1, …) AS SELECT … ; Column names are optional  Column names are taken from SELECT if not specified

5 5 View Update Problematic  Not all views are updateable Aggregate columns Joins  Simple Example: CREATE VIEW Emp_Dept AS SELECT SSN, Dno, DName FROM Employee JOIN Department ON Dno=Dnumber; UPDATE Emp_Dept SET DName = ‘New Research’ WHERE SSN = ‘333445555’;

6 6 More View Update Safe (all DBMSs support)  A View which is a Row and Column subset of a single table is updateable INSERTS require default values for invisible column Check Option  CREATE VIEW … WITH CHECK OPTION;  For updateable views  All updates will appear in the view Good practice: Specify for views you intend to be updateable DBMS will tell you if view is not updateable

7 7 How are Views Implemented? Query Modification:  Modify a query on the view to be a query on the underlying tables  Simple query on a complex view becomes a complex query View Materialization  Maintain a table with the contents of the view  Mapping database updates to changes in the view is a problem Some DBMSs allow the DBA to choose a strategy

8 8 Application Interface Interactive SQL:  Type SQL and have it directly executed  Good for: Database Administration, one time and/or ad hoc queries, shell scripts  Not good for applications Application Interface Techniques  Application Programming Interface  Embedded SQL  Database Programming Language

9 9 Impedance Mismatch Problem with host languages and SQL:  Mapping needed between host language types and SQL data types  Databases have Rows, Tables or Row Sets Host languages have their own types of composite data types  Often would like to map between host language objects and rows in tables in the DBMS Dealing with update can get tricky Bottom line  None of these problems is close to unsolvable  Code is required to deal with all of these problems

10 10 Application Interface Concepts Connection  A connection to the database  Multiple SQL statements can be processed using a connection  Host, username, password, etc. specified when connection is established  First step in interacting with the database is to establish a connection

11 11 Application Interface Concepts Cursor  SELECT can return multiple rows. How do I handle this in my host language?  Solution: Create a Cursor Reflects the current status of the query Allows iteration over the results of the query FETCH fetches the next row from the Cursor  Some systems allow backward iteration and seeking on a cursor

12 12 Application Interface Concepts Single Statement SELECT  When a SELECT will return a single row, directly FETCH the results of the select in a single statement Shared variables  Variables that are shared between the database interface (SQL) and the programming language  Alternatively, bindings between variables in SQL statements and variables in the programming language

13 13 Application Interface Concepts “Dynamic” SQL  SQL statements that are created on the fly by the program  PREPARE A statement is prepared prior to being executed Can be used to save overhead of repeated parsing  IMMEDIATE Execute the statement without prior preparation SQLCODE, SQLSTATE  Variables used to communicate errors, exceptions, state between DBMS interface and program

14 14 Embedded SQL SQL statements are embedded into the host programming language Precompiler or preprocessor used to convert the program into standard language  Statements replaced by appropriate functions calls See examples:  C: Figures 9.2, 9.3, 9.4, 9.5 in text  Java: SQL/J: Figures 9.6, 9.7, 9.8, 9.9, 9.10

15 15 Embedded SQL Why?  Integrated syntax for including SQL in application programs Why not?  Integrated? Syntax for including SQL in application programs  Support is spotty Preprocessor is a mini-compiler

16 16 Application Programming Interface API for accessing the database  Standard: SQL/CLI (Call Level Interface) Standardized version of ODBC  DBMS Specific DBMS specific API

17 17 Application Programming Interface SQL statements represented as character strings  Some form of variable substitution needed  Warning: May have to escape strings Make function calls as appropriate  Follow same model as Embedded SQL: Establish a connection Open a cursor Execute statement Fetch results

18 18 Example MySQL/Python dno = raw_input('Enter a Department Number... ').strip(); connection = MySQLdb.connect(host = ‘localhost' db='example', user='example', passwd='example'); try: cursor = connection.cursor() cursor.execute( ''' select ssn, fname, minit, lname, salary from employee where DNO = %(dno)s''', {'dno' : dno}) rows = cursor.fetchall() for row in rows: print 'SSN: %s, Name: %s %s %s, Salary: %s' % row cursor.close() finally: connection.close()

19 19 Open Database Connectivity (ODBC) Originated by Microsoft  Standardized as the SQL/CLI Connects to Data Sources rather than a specific database  Initialization parameters (.INI) describe data source Can manage multiple data sources Picture courtesy MySQL Documentation

20 20 Java Database Connectivity (JDBC) Same idea as ODBC, specialized for Java  Some changes to interface to adapt to Java style OO programming and strong typing Provides same capabilities as ODBC

21 21 ODBC Examples C/ODBC  Figures 9.11, 9.12 from book Java/JDBC  Figures 9.13, 9.14 from book

22 22 Database Programming Language Standard form: SQL/PSM (Persistent Stored Modules) Full scale programming language  Conditions, loops, exceptions, procedures and functions  SQL statements  SQL Data Types (e.g. Cursors) Used to support triggers inside DBMS May or may not have API support for calling PSM programs Short example: Figure 9.15 in text


Download ppt "1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics."

Similar presentations


Ads by Google