CPSC 421 Database Management Systems. Lecture 10: Embedded SQL

Similar documents
ITCS Implementation. Jing Yang 2010 Fall. Class 14: Introduction to SQL Programming Techniques (Ch13) Outline

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CPSC 421 Database Management Systems. Lecture 11: Storage and File Organization

Chapter 13 Introduction to SQL Programming Techniques

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

Lecture 26 10/28/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

CMPT 354 Database Systems I

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

Announcements. SQL: Part IV. Transactions. Summary of SQL features covered so far. Fine prints. SQL transactions. Reading assignments for this week

SQL STORED ROUTINES. CS121: Relational Databases Fall 2017 Lecture 9

BIS Database Management Systems.

MIS Database Systems.

HP NonStop Structured Query Language (SQL)

SQL: Programming Midterm in class next Thursday (October 5)

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

CPSC 421 Database Management Systems. Lecture 19: Physical Database Design Concurrency Control and Recovery

Non-interactive SQL. EECS Introduction to Database Management Systems

Introduction to Data Management. Lecture #2 (Big Picture, Cont.)

Database Application Development

Database Application Development

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

Chapter 9 SQL in a server environment

Database Connectivity using PHP Some Points to Remember:

Data! CS 133: Databases. Goals for Today. So, what is a database? What is a database anyway? From the textbook:

First lecture of this chapter is in slides (PPT file)

Database Applications

CSC 3300 Homework 3 Security & Languages

Bonus Content. Glossary

Data Base Concepts. Course Guide 2

DATABASES SQL INFOTEK SOLUTIONS TEAM

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Outline. We ll learn: Faloutsos CMU SCS

Database System Concepts and Architecture

AN INTRODUCTION TO WEB PROGRAMMING. Dr. Hossein Hakimzadeh Department of Computer and Information Sciences Indiana University South Bend, IN

PHP Development - Introduction

SQL Data Definition Language: Create and Change the Database Ray Lockwood

COMP519: Web Programming Autumn 2015

John Edgar 2

Database Processing. Fundamentals, Design, and Implementation. Global Edition

Working with Databases and Java

Schedule. Feb. 12 (T) Advising Day. No class. Reminder: Midterm is Feb. 14 (TH) Today: Feb. 7 (TH) Feb. 21 (TH) Feb. 19 (T)

Chapter 9 SQL in a server environment

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

CS108 Lecture 19: The Python DBAPI

L6 Application Programming. Thibault Sellam Fall 2018

Database Applications. SQL/PSM Embedded SQL JDBC

CSE 132A. Database Systems Principles

Embedded SQL. Introduction

Embedded SQL. Davood Rafiei

Development Technologies. Agenda: phpmyadmin 2/20/2016. phpmyadmin MySQLi. Before you can put your data into a table, that table should exist.

CMPT 354: Database System I. Lecture 2. Relational Model

Testing with MySQL embedded

Chapter 1: Introduction

Introduction to Oracle Objects

The Relational Model Constraints and SQL DDL

2017 Politecnico di Torino 1

PostgreSQL Database and C++ Interface (and Midterm Topics) ECE 650 Systems Programming & Engineering Duke University, Spring 2018

DATABASE MANAGEMENT SYSTEMS. UNIT I Introduction to Database Systems

Database Systems. Sven Helmer. Database Systems p. 1/567

2017 Politecnico di Torino 1

Outline. CS 235: Introduction to Databases. DB Application Programming. Interface Solutions. Basic PSM Form. Persistent Stored Modules

COSC 304 Introduction to Database Systems. Database Introduction. Dr. Ramon Lawrence University of British Columbia Okanagan

CS 377 Database Systems. Li Xiong Department of Mathematics and Computer Science Emory University

Lab 6-1: MySQL Server

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

Database Application Development

EMBEDDED SQL Part 1: Static Statements

SQL: Programming. Introduction to Databases CompSci 316 Fall 2017

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

Oracle Database 12c: Program with PL/SQL Duration: 5 Days Method: Instructor-Led

EMBEDDED SQL. SE 3DB3 Fall 2016 MICHAEL LIUT DEPARTMENT OF COMPUTING AND SOFTWARE MCMASTER UNIVERSITY

Database Management Systems

CS 564: DATABASE MANAGEMENT SYSTEMS. Spring 2018

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

More loops. Control structures / flow control. while loops. Loops / Iteration / doing things over and over and over and over...

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

1.8 Database and data Data Definition Language (DDL) and Data Manipulation Language (DML)

Chapter 1 Introduction

MySQL for Developers. Duration: 5 Days

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

SQL in Programming Languages Read chapter 5 of Atzeni et al. BD: Modelli e Linguaggi di Interrogazione and section 8.

DATABASE SYSTEMS. Database programming in a web environment. Database System Course, 2016

Transbase R PHP Module

MySQL for Developers. Duration: 5 Days

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications

CS634 Architecture of Database Systems Spring Elizabeth (Betty) O Neil University of Massachusetts at Boston

The URL of the whole system is:

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

Introduction to Data Management. Lecture #2 (Big Picture, Cont.) Instructor: Chen Li

Contents I Introduction 1 Introduction to PL/SQL iii

ECE 650 Systems Programming & Engineering. Spring 2018

Java EE Architecture, Part Three. Java EE architecture, part three 1(57)

PHP Reference. To access MySQL manually, run the following command on the machine, called Sources, where MySQL and PhP have been installed:

Introduction. Example Databases

Transcription:

CPSC 421 Database Management Systems Lecture 10: Embedded SQL * Some material adapted from R. Ramakrishnan, L. Delcambre, and B. Ludaescher Today s Agenda Quiz Project Part 2 Embedded SQL DDL and DML Notes: Next week we do not have class on Thursday Tuesday is a midterm review please bring questions! The following Tuesday is the Midterm CPSC 421, 2009 2 1

Basic Database Architecture Web Forms Application Front Ends SQL Interface SQL Commands DBMS Plan Executor Operator Evaluator Parser Optimizer Query Evaluation Engine Transaction Manager Lock Manager Concurrency Control File and Access Methods Buffer Manager Disk Space Manager Recovery Manager Index Files Data Files CPSC 421, 2009 System Catalog 3 Embedded SQL The majority of SQL is generated from software applications through Embedded SQL Embedded SQL allows data from a DBMS to be accessed from within a regular software program Embedded SQL programs can Convert/modify data before it is presented to users Control what data is visible to users Generate SQL dynamically based on user preferences CPSC 421, 2009 4 2

Embedded SQL SQL commands can be called from within a host language such as C/C++,.NET, PHP, Java, Typically provided through a set of libraries or modules Includes some type of statements to connect to the right database SQL statements can refer to host variables the regular variables in your program (to pass parameters to queries) Because SQL (and the relational model) are often very different than the host language The use of SQL in the language may not be natural This means that it can be difficult to integrate records/relations with the data structures and constructs provided by the language This is often referred to as the impedance mismatch problem CPSC 421, 2009 5 Embedded SQL SQL query results are often large (multi-) sets of records Most languages cannot efficiently (or practically) represent or hold typical query results To address this, SQL supports a mechanism called a cursor for accessing query results Cursors are somewhat similar to standard IO stream APIs As well as iterators CPSC 421, 2009 6 3

Cursors Cursors can be declared on a relation or query statement (which generates a relation) Like an IO stream (or iterator) we can open a cursor (repeatedly) fetch records from a cursor move the cursor to a new location determine if all tuples have been retrieved modify or delete a tuple pointed to by a cursor CPSC 421, 2009 7 Cursors Capabilities vary from one DBMS to another Move forward only Move forward or backward one row at a time Move to arbitrary locations What are some advantages/disadvantages? Cursor placement A cursor is placed BEFORE the first row of the result Special end of result value (like EOF) is used to denote when past the last row CPSC 421, 2009 8 4

Embedded SQL implementations We will look at two different approaches for Embedded SQL A traditional approach used for C (e.g., with Oracle) Using SQL in a scripting language (PHP) An API-based approach for C (for mysql) CPSC 421, 2009 9 Example Schema Suppose we want to track products and categories for a retailer Products(ProductID : int, CategoryID : int, ProductName : string, UnitPrice : currency) Categories(CategoryID : int, CategoryName : string) Products.CategoryID REFERENCES Categories.CategoryID BTW, are these tables normalized, i.e., in BCNF? CPSC 421, 2009 10 5

Embedded SQL in C void ListProducts(short max) { char SQLSTATE[6]; EXEC SQL BEGIN DECLARE SECTION char product_name[20]; float product_price; short max_price = max; EXEC SQL END DECLARE SECTION Special preprocessing commands SQLSTATE holds the return value from the DBMS EXEC SQL denotes embedded SQL section DECLARE SECTION binds variables into SQL max is a parameter of this procedure that is used to initialized max_price CPSC 421, 2009 11 Embedded SQL in C (cont.) EXEC SQL DECLARE pinfo CURSOR FOR SELECT P.ProductName, P.UnitPrice FROM Products P, Categories C WHERE C.CategoryName = Beverages AND P.UnitPrice < :max_price AND P.CategoryID = C.CategoryID ORDER BY P.UnitPrice; DECLARE pinfo CURSOR defines a name for the cursor max_price (which was set to the max function argument) is a variable CPSC 421, 2009 12 6

Embedded SQL in C } (cont.) EXEC SQL OPEN pinfo; EXEC SQL FETCH pinfo INTO :product_name, :product_price; while(strcmp(sqlstate, 02000 )!= 0) { prinj( %s costs %f each\n, product_name, product_price); EXEC SQL FETCH pinfo INTO :product_name, :product_price; } EXEC SQL CLOSE pinfo; OPEN pinfo opens the query result we are interested in product_name and product_price are our previously defined variables FETCH pinfo INTO assigns data into our variables and advances the cursor to the next row CLOSE pinfo releases the resources associated with the cursor CPSC 421, 2009 13 Embedded SQL in C In this approach, the SQL preprocessing commands (EXEC SQL) are compiled in a separate step: A special program is called before calling the C compiler that converts these into low-level library calls This approach makes the SQL statements feel like tightly integrated into the language In some ways, it makes it look like C has new SQL constructs One disadvantage is that it complicates the compilation process, making it harder to track down errors CPSC 421, 2009 14 7

Embedded SQL in PHP Another (common) approach is to use a library (i.e., predefined set of function calls) PHP uses this approach along with many other languages (including Java) Each DBMS provides a different library (e.g., we will look at the MySQL one; PostgreSQL has its own, etc.) CPSC 421, 2009 15 A Quick Note on PHP PHP is a general-purpose scripting language it is similar to other (procedural) scripting languages like Perl, tcsh, bash, python, etc. it is primarily used as a scripting language for web development since it can be easily embedded with HTML and provides useful functions for building web applications this makes it easy to create web applications that require access to back-end databases there are lots of resources, tutorials, and examples for PHP on the web CPSC 421, 2009 16 8

Embedded SQL in PHP First we have to connect to a database <?php $host = localhost ; $user = bowers ; $pw = secret ; $db = products ; mysql_connect($host, $user, $pw) or die(mysql_error()); mysql_select_db($db);?> All PHP scripts are enclosed in <?php?> CPSC 421, 2009 17 Embedded SQL in PHP Now that we are connected, we can query the DB connect to db <html> <body> <h2>product names and prices</h2> <?php $sql_stmt = SELECT P.ProductName, P.UnitPrice FROM Products P, Categories C The $results variable is a handle to a cursor WHERE P.CategoryName = Beverages AND P.CategoryID = C.CategoryID ORDER BY P.UnitPrice ; $results = mysql_query($sql_stmt) or die( Invalid query:. mysql_error()); CPSC 421, 2009 18 9

Embedded SQL in PHP And retrieve and display the results of the query while($row = mysql_fetch_row($results)) { list($product_name, $unit_price) = $row; print $product_name, $unit_price <br/> ; } mysql_close();?> </body> </html> The $results variable is a handle to a cursor CPSC 421, 2009 19 Embedded SQL in PHP A number of helper functions are provided # get the number of rows of a result $num_rows = mysql_num_rows($result); print The query returned. $num_rows. results\n ; # list the databases (for the current connecvon) $databases = mysql_list_dbs(); $dbs = mysql_num_rows($databases); for($i = 0; $i < $dbs; $i++) print mysql_db_name($databases, $i). \n ; # list the tables $tables = mysql_list_tables($db); $num_tables = mysql_num_rows($tables); for($i = 0; $i < $num_tables; $i++) print mysql_tablename($tables, $i). \n ; # establish mulvple connecvons $connect1 = mysql_connect($hostname1, $user1, $pw1); $connect2 = mysql_connect($hostname2, $user2, $pw2); CPSC 421, 2009 20 10

Embedded SQL in PHP For more info on PHP http://www.php.net http://www.w3schools.com/php/ http://www.php.net/mysql/ Many other language bindings besides MySQL Warning: I haven t tried using PHP w/ MySQL on ada! Both are installed though CPSC 421, 2009 21 Embedded MySQL in C MySQL provides a C API A different approach than using special preprocessing macros as before Warning: I haven t tried this on ada Connecting to MySQL #include /usr/include/mysql/mysql.h int main() { MYSQL * mysql; MYSQL_RES * result; MYSQL_ROW row; Port, Socket, Client Flag mysql_init(mysql); mysql_real_connect(mysql, host, user, password, db, 0, NULL, 0); CPSC 421, 2009 22 11

Embedded MySQL in C Querying a MySQL database } mysql_query(mysql, SELECT * FROM ); result = mysql_store_result(mysql); // create cursor while(row = mysql_fetch_row(result)) { print results as row[0], row[1], } mysql_free_result(result); mysql_close(mysql); return 0; CPSC 421, 2009 23 Embedded MySQL in C Compiling gcc prog.c I/usr/include/mysql L/usr/lib/mysql lmysqlclient 1m 1z The basic idea is to compile with the specific mysql libraries as opposed to using the precompiler CPSC 421, 2009 24 12

Other Embedded SQL Solutions ODBC Open Database Connectivity Older standard, proposed by Microso but driven by the database community A number of vendors (Oracle, etc.) make ODBC drivers available JDBC Java Database Connecvvity Similar to ODBC but for Java Also, many vendors provide drivers Provides a single Java API for accessing any database that supports JDBC This differs than, e.g., PHP, where each vendor has a different set of API calls (pg_connect, etc.) CPSC 421, 2009 25 Database Language Commands DDL Data Definition Language Schema-level commands DML Data Manipulation Language Row-level commands CPSC 421, 2009 26 13

DDL Data Definition Language Create, edit, or delete database objects Tables Stored Procedures Data Types NOT ROWS! Drop table: DROP TABLE Patient; Create table: CREATE TABLE Patient ( ); CPSC 421, 2009 27 DDL Data Definition Language Alter table: ALTER TABLE Patient * Plus any of the following: [ADD COLUMN] [ALTER COLUMN] [DROP COLUMN] [ADD CONSTRAINT] [DROP CONSTRAINT] CPSC 421, 2009 28 14

DML Data Manipulation Language Inserting, updating, or deleting rows Deleting rows: DELETE FROM Patient WHERE FirstName LIKE B% ; Note: This will potentially delete multiple patients! CPSC 421, 2009 29 DML Data Manipulation Language Inserting rows: INSERT INTO Patient (ID, FirstName, LastName) VALUES (4, Sue, Smith ), (6, John, Jones ); INSERT INTO Patient (ID, FirstName, DateOfBirth) SELECT ID, Fname, DOB FROM Transfers WHERE Status = 1; This will insert a patient for each row returned from the query CPSC 421, 2009 30 15

DML Data Manipulation Language Updating rows: UPDATE TABLE Patient SET FirstName = Bob, DateOfBirth = AddDays(DateOfBirth, 1) WHERE ID = 555; This will change the FirstName and DateOfBirth for the patient with ID 555. CPSC 421, 2009 31 For Thursday Reading Ch 6: Intro, 6.1 Be sure to understand: Basic concepts of embedded SQL Basic idea of cursors ( we will talk a bit more about this when we get to query operator implementation) Homework Part 2 of the project due next Thursday Homework 4 (most likely) assigned on Tuesday but due after the midterm CPSC 421, 2009 32 16