CS411 Database Systems Kazuhiro Minami 07: SQL System Aspects.

Slides:



Advertisements
Similar presentations
1 Database-Connection Libraries Call-Level Interface Java Database Connectivity PHP.
Advertisements

Chapter 9 SQL in a Server Environment Call-Level Interface Java Database Connectivity PHP.
1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
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.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
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 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
CSCI 6962: Server-side Design and Programming
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Internet and Web Application Development Revision.
INTERNET APPLICATION DEVELOPMENT For More visit:
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
CSE470 Software Engineering Fall Database Access through Java.
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.
1 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
CS 405G: Introduction to Database Systems Database programming.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Database Application Development SQL In Application Code : 1. Embedded SQL 2. Database API’s 3. SQLJ.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
CITA 310 Section 7 Installing and Testing a Programming Environment (Textbook Chapter 7)
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Database Connectivity and Server-Side Scripting Chapter 12.
SQL Programming SQL in Application Programs
Basics of JDBC Session 14.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Chapter 7: Constraints and Triggers Foreign Keys Local and Global Constraints Triggers 1.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: CLI & JDBC & Security Ch Ch. 9.6 – Ch 10.1.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL in the real world 1. The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7.
SQL Environment.
Introduction to Database Systems, CS420
CS320 Web and Internet Programming SQL and MySQL
CPSC-310 Database Systems
ISC440: Web Programming 2 Server-side Scripting PHP 3
CPSC-310 Database Systems
CS3220 Web and Internet Programming SQL and MySQL
Database-Connection Libraries
CS3220 Web and Internet Programming SQL and MySQL
Presentation transcript:

CS411 Database Systems Kazuhiro Minami 07: SQL System Aspects

Announcements Midterm exam on Oct. 13 next Tuesday –3:30pm – 4:45pm –Bring your UIUC photo ID –Topics: E/R modeling Relational schema design RA SQL 2

T/F Questions on SQL Constraints 1.If we declare that an attribute A is UNIQUE, we cannot have two tuples where the attribute A has a NULL value. 2.Insertion in SQL supports the bag semantics. 3.There is no way to define a primary key containing multiple attributes in SQL. 4.If we execute an UPDATE-SET-WHERE statement, every time the DBMS finds a tuple satisfying the WHERE conditions, the values of that tuple are updated immediately. 5.Every foreign key must be a primary key of the relation. 6.It is possible to enforce a foreign-key constraint by defining a tuple-based CHECK.

More T/F Questions 1.It is possible to convert a SQL statement with GROUP BY clause into an equivalent single SQL statement without GROUP BY clause by using subqueries. CREATE ASSERTION NoExpensiveBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price) )); CREATE ASSERTION NoExpensiveBars CHECK ( NOT EXISTS ( SELECT bar FROM Sells GROUP BY bar HAVING 5.00 < AVG(price) )); SELECT Distinct bar FROM Sells s1 WHERE 5.00 < ( SELECT AVG(price) FROM Sells s2 WHERE s1. bar = s2.bar) Sells(bar, beer, price)

More T/F Questions 1.It is possible to convert a SQL statement with subqueries into an equivalent single SQL statement without using subquries.

6 System Aspects of SQL (Chapter 9: Four more ways to make SQL calls from outside the DBMS) Call-Level Interface PHP Java Database Connectivity Stored procedures Embedded SQL

Connecting SQL to the Host Language Outside –API Approach: Vendor specific libraries[80’s- ] –MySQL API for PHP Open interfaces [90’s - ] –JDBC, ODBC Embedded SQL [70’s- ] Embedded SQL for C/C++. Not widely used. Inside –Stored procedures/functions: [80’s- ]

SQL/Host Language Interface (CLI) Function call SQL Library Host language program SQL stmt DBMS Prepare SQL statements from user inputs Fetch rows from the cursor TableCursor while(1){ } Impedance mismatch problem

The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database Server Forms & Buttons Your business logic is executed here Display query result Client & Server Interaction

MySQL + PHP

What is PHP? Stands for Hypertext Preprocessor A server-side scripting language PHP scripts are executed on the server Supports many databases (MysQL, Infomix, Oracle, etc.) 11

What is a PHP file? Contain text, HTML tags and scripts PHP files are returned to a browser as plain HTML Have a file extension of “.php” 12

Steps for writing a DB application 1. SSH to a csil Linux machine (e.g., csil-linux-ts1) 2. Login to MySQL server % mysql -h csil-projects.cs.uiuc.edu -u netid –p 3. Choose a database mysql > use ; 4. Create a table “hello” mysql > CREATE TABLE hello (varchar(20)); 5. Insert a tuple mysql > INSERT INTO hello VALUES (‘Hello World!’); 6. Quit MySQL mysql > quit 13 Set up a table

Steps for writing a DB application 1. Go to the directory ~/csil-projects/public_html % cd csil-projects/public_html 2. Write hello_world.php 3. Open a web brower 14 Write a PHP program

hello_world.php <?php$host = 'csil-projects.cs.uiuc.edu';$user = 'minami'; $password = ’password';$link = mysql_connect($host, $user, $password) or die ('Could not connect: '. mysql_error()); mysql_select_db('minami_db') or die ('Could not select database ');$query = 'SELECT * FROM hello';$result = mysql_query($query);while ($row = mysql_fetch_array($result)) { echo ”$row[message] ”;}mysql_free_result($result);mysql_close($link);?> 15

PHP Basics All PHP code exist inside HTML text <?php PHP code goes here ?> Variables – Untyped and need not be declared – Begins with ‘$’ Strings – Surrounded by either single or double quotes $host = 'csil-projects.cs.uiuc.edu’; $x = ‘A host is $host.’ $x = “A message is $host.” – Concatination of strings 'Could not connect: '. mysql_error() 16

PHP Basics (Cont.) Arrays – Ordinary arrays $a = array(30, 20, 10, 0) with $a[0] equal to 30,$a[1] equal to 20 and so on – Associative arrays $seasons = array(‘spring’ => ‘warm’, ‘summer’ => ‘hot’, ‘fall’ => ‘warm’, ‘winter’ => ‘cold’); Then, seasons[‘summer’] has the value ‘hot’. 17

Creating a Database Connection Before you can access data in a database, you must create a connection to the database Syntax: mysql_connect(servername, username, password); Example: <?php $con = mysql_connect("localhost",”user",”pwd"); if (!$con) { die('Could not connect: '. mysql_error()); }// some code ?> 18

Executing SQL Statements Choose a database mysql_select_db('minami_db') or die ('Could not select database '); Execute a SQL statement $query = 'SELECT * FROM hello'; $result = mysql_query($query); 19

Cursor Operations: Fetching results Use the mysql_fetch_array() function to return the first row from the recordset as an array. Each call to mysql_fetch_array() returns the next row in the recordset. The while loop loops through all the records in the recordset. To refer to the value of “message” attribute, we use the PHP $row variable ($row[message]). while ($row = mysql_fetch_array($result)) { echo ”$row[message] ”; } 20

Loading Data From a file 1.Get data by crawling on the web 2.Parse and format data with Perl, awk, or other languages 3.Execute a mysql LOAD command below mysql > LOAD DATA LOCAL INFILE ‘importfile.csv' INTO TABLE table_name FIELDS TERMINATED BY ',' 21

Insert Data From a Form Into a Database When a user clicks the submit button in the HTML form, the form data is sent to "insert.php". ISBN: Title: 22

Insert Data From a Form Into a Database The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables. $book = $_POST["bname"]; $isbn = $_POST["isbn"]; $sql = "INSERT INTO book(isbn, name) VALUES ($isbn, '$book')"; mysql_query($sql)) 23

JDBC

25 All these methods follow the basic PHP paradigm 1.Connect to a DB server. 2.Say what database you want to use. 3.Assemble a string containing an SQL statement. 4.Get the DBMS to prepare a plan for executing the statement. 5.Execute the statement. 6.Extract the results into variables in the local programming language.

JDBC Java Database Connectivity (JDBC) is a library similar to SQL/CLI, but with Java as the host language. JDBC/CLI differences are often related to the object-oriented style of Java, but there are other differences. The brainchild of a former UIUC undergrad

Connections A connection object is obtained from the environment in a somewhat implementation- dependent way. We’ll start by assuming we have myCon, a connection object.

Statements JDBC provides two classes: 1.Statement = an object that can accept a string that is an SQL statement and can execute such a string. 2.PreparedStatement = an object that has an associated SQL statement ready to execute.

Creating Statements The Connection class has methods to create Statements and PreparedStatements. Statement stat1 = myCon.createStatement(); PreparedStatement stat2 = myCon.createStatement( “SELECT beer, price FROM Sells” + “WHERE bar = ‘Joe’’s Bar’” ); Java trick: + concatenates strings. createStatement with no argument returns a Statement; with one argument it returns a PreparedStatement.

Executing SQL Statements JDBC distinguishes queries from modifications, which it calls “updates.” Statement and PreparedStatement each have methods executeQuery and executeUpdate. –For Statements, these methods have one argument: the query or modification to be executed. –For PreparedStatements: no argument.

Example: Update stat1 is a Statement. We can use it to insert a tuple as: stat1.executeUpdate( “INSERT INTO Sells” + “VALUES(‘Brass Rail’, ‘Bud’, 3.00)” );

Example: Query stat2 is a PreparedStatement holding the query “SELECT beer, price FROM Sells WHERE bar = ‘Joe’’s Bar’”. executeQuery returns an object of class ResultSet --- we’ll examine it later. The query: ResultSet Menu = stat2.executeQuery();

Accessing the ResultSet An object of type ResultSet is something like a cursor. Method Next() advances the “cursor” to the next tuple. –The first time Next() is applied, it gets the first tuple. –If there are no more tuples, Next() returns the value FALSE.

Accessing Components of Tuples When a ResultSet is referring to a tuple, we can get the components of that tuple by applying certain methods to the ResultSet. Method getX (i ), where X is some type, and i is the component number, returns the value of that component. –The value must have type X.

Example: Accessing Components Menu is the ResultSet for the query “SELECT beer, price FROM Sells WHERE bar = ‘Joe’’s Bar’”. Access the beer and price from each tuple by: while ( Menu.Next() ) { theBeer = Menu.getString(1); thePrice = Menu.getFloat(2); /* do something with theBeer and thePrice */ }