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

Slides:



Advertisements
Similar presentations
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives u How to use the SQL programming language u How to use SQL cursors u How to create.
Advertisements

Copyright © 2004 Pearson Education, Inc.. Chapter 9 More SQL: Assertions, Views, and Programming Techniques.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 9 Introduction to SQL Programming Techniques.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Objective: To access a database from an application program (as opposed to interactive interfaces) Why? An interactive interface is convenient but not.
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.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL: Assertions, Views, and Programming Techniques by Pinar Senkul resources: mostly froom.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
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.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Chapter 9 MORE SQL: Assertions, Views, and Programming Techniques Copyright © 2004 Pearson Education, Inc.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
LIS651 lecture 7 PHP mySQL Thomas Krichel
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Announcements Read JDBC Project Step 5, due Monday.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 1 DATADABASE PROGRAMMING 2Chapter 13 from our text.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
CS 405G: Introduction to Database Systems Database programming.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
1 CS 430 Database Theory Winter 2005 Lecture 17: Objects, XML, and DBMSs.
Dr Gordon Russell, Napier University Unit Embedded SQL - V3.0 1 Embedded SQL Unit 5.1.
Oracle Data Integrator Procedures, Advanced Workflows.
Advanced SQL: Cursors & Stored Procedures
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
1 CS 430 Database Theory Winter 2005 Lecture 2: General Concepts.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 MORE SQL: Assertions, Views, and Programming Techniques Copyright © 2004 Pearson Education, Inc.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 7 SQL HUANG XUEHUA. Chapter Objectives Specification of more general constraints via assertions SQL facilities for defining views (virtual.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
10/3/2017.
CSE202 Database Management Systems
Introduction to SQL Programming Techniques
SQL Environment.
A Guide to SQL, Seventh Edition
Views, Stored Procedures, Functions, and Triggers
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Subject Name: DATABASE MANAGEMENT SYSTEM Subject Code: 10CS54
CPSC-310 Database Systems
Database Application Development
DB Programming: SQL Programming Techniques
Introduction to SQL Programming Techniques
Unit I-2.
Chapter 8 Advanced SQL.
Presentation transcript:

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

2 Additional Topics Views Programmatic Interface

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 Defining a View CREATE VIEW view_name(col1, …) AS SELECT … ; Column names are optional  Column names are taken from SELECT if not specified

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 = ‘ ’;

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 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 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 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 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 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 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 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 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 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 Application Programming Interface API for accessing the database  Standard: SQL/CLI (Call Level Interface) Standardized version of ODBC  DBMS Specific DBMS specific API

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 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 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 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 ODBC Examples C/ODBC  Figures 9.11, 9.12 from book Java/JDBC  Figures 9.13, 9.14 from book

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