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.

Slides:



Advertisements
Similar presentations
Database Languages Chapter 7. The Relational Algebra.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Chapter 3 An Introduction to Relational Databases.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 2 Structured Query Language.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
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.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan SPUFI Hands On Introduction to Embedded SQL DCLGEN.
1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Irwin/McGraw-Hill Copyright © 2000 The McGraw-Hill Companies. All Rights reserved Whitten Bentley DittmanSYSTEMS ANALYSIS AND DESIGN METHODS5th Edition.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
Announcements Read JDBC Project Step 5, due Monday.
Chapter 8 Using SQL in an Application. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 CSC 4480 outline Intro (3-9) Static SQL (10-11)
Chapter 27 The World Wide Web and XML. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.27-2 Topics in this Chapter The Web and the Internet.
4B-1 Wei-Pang Yang, Information Management, NDHU Introduction to Database CHAPTER 4B ( 補 ) DB2 and SQL  Overview  Data Definition  Data Manipulation.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Advanced SQL: Cursors & Stored Procedures
An Introduction to SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 4.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
Database Technology Jing Shen.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Variables and control statements in PL\SQL Chapter 10.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
SQL.. AN OVERVIEW lecture3 1. Overview of SQL 2  Query: allow questions to be asked of the data and display only the information required. It can include.
Chapter 10 Views. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.10-2 Topics in this Chapter What are Views For? View Retrievals View Updates.
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Copyright © 2016 Pearson Education, Inc. Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki Topi CHAPTER 6: INTRODUCTION.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
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.
Fundamental of Database Systems
Chapter 4 An Introduction to SQL.
SQL Environment.
A Guide to SQL, Seventh Edition
Unit 2 DB2 and SQL.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Introduction What is a Database?.
Database Application Development
Introduction to Database
Introduction to Database
Database systems Lecture 3 – SQL + CRUD
Unit I-2.
SQL .. An overview lecture3.
Chapter 8 Advanced SQL.
Presentation transcript:

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 Catalog Views Embedded SQL Cursors Dynamic SQL Call-Level Interface

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-3 History of SQL Developed by IBM in 1970’s Early prototype called System R Current version is SQL:1999 All major database vendors support SQL All support a superset of a subset of SQL Both relvar and relation = table in SQL Tuple = row in SQL Attribute = column in SQL

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-4 Overview of SQL It is the standard language for relational systems, although imperfect Supports data definition (DDL) and data manipulation (DML) DDL: CREATE TABLE, CREATE TYPE, ALTER TABLE DML: SELECT, INSERT, DELETE, UPDATE

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-5 Relational operators in SQL Restrict is implemented by SELECT –Subset of rows –Uses WHERE clause to narrow result –Uses SELECT * to copy entire table Project is also implemented by SELECT –Subset of columns

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-6 Relational operators in SQL Join is also implemented by SELECT –Result is a single table matched by values in the join columns of each source table –Uses WHERE clause to specify source tables –Uses dot operator to qualify like column names

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-7 Update operators in SQL - Insert Insert is implemented by INSERT INTO –Ex,: INSERT INTO TEMP (P#, Weight) SELECT P#, Weight FROM P WHERE COLOR = COLOR (‘Red’); –Inserts part number and weight from P into part number and weight in TEMP for all red parts

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-8 Update operators in SQL - Delete Delete is implemented by DELETE –Ex,: DELETE FROM SP WHERE P# = P# (‘P2’); –Deletes all shipments for part P2

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-9 Update operators in SQL - Update Update is implemented by UPDATE –Ex,: UPDATE S SET STATUS = 2 * STATUS, CITY = ‘Rome’ WHERE CITY = ‘Paris’; –Doubles the status of the Parisian suppliers and moves them to Rome.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-10 The Catalog in SQL Every database has a catalog A catalog has many schemas, one per user Each catalog includes one Information Schema The Information Schema is a collection of all other schemas as views Represents a hypothetical Definition Schema for all users

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-11 The Information Schema - Examples Tables (includes views) Views (does not include tables) Table constraints Referential constraints Column constraints Privileges (Because these objects are themselves tables, they can be queried)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-12 Views CREATE VIEW GOOD_SUPPLIER AS SELECT S#, STATUS, CITY FROM S WHERE STATUS > 15; You can now operate on the view SELECT S#, STATUS FROM GOOD_SUPPLIER WHERE CITY = ‘London’;

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-13 Views - Example Creating a view, and then selecting from the view is equivalent to a select sub-query SELECT GOOD_SUPPLIER.S#, GOOD_SUPPLIER.STATUS FROM (SELECT S#, STATUS, CITY FROM S WHERE STATUS > 15 ) AS GOOD_SUPPLIER; WHERE GOOD_SUPPLIER.CITY = ‘London’;

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-14 Embedded SQL SQL statements can be embedded in a host program C, COBOL, PL/1, Java, and many others Initiated in host via EXEC SQL statement Any SQL statement is valid after EXEC SQL

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-15 Embedded SQL SQL statements after EXEC SQL can reference host variables Declare any such variables in a SQL DECLARE section Then, prefix : (colon) to the variable name when you reference it

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-16 Embedded SQL - Example EXEC SQL BEGIN DECLARE SECTION; DCL SQLSTATE CHAR(5); DCL P# CHAR(6); DCL WEIGHT DECIMAL(5,1); EXEC SQL END DECLARE SECTION; P# = ‘P2’; EXEC SQL SELECT P.WEIGHT INTO :WEIGHT FROM P WHERE P.P# = (:P#);

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-17 Embedded SQL - SQLSTATE IF SQLSTATE = ‘00000’ THEN … ELSE … Every host language must include a host variable called SQLSTATE that returns is execution is ok If no data found then it returns 02000

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-18 Embedded SQL – Loosely coupled with the host Data types between host and tables must be compatible Host variables can have the same name as table columns, but need not SQL retrieval returns sets of rows Host languages handle one row at a time To allow this, SQL sets up a CURSOR to hold returned row sets, so they can be processed one row at a time

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-19 Embedded SQL – Sometimes Cursors are not Needed SELECT, when it returns at most one row INSERT, however many rows DELETE, likewise UPDATE, ditto Example: EXEC SQL UPDATE S SET STATUS = STATUS + :RAISE WHERE CITY = ‘London’;

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-20 Embedded SQL – Cursors A cursor is a buffer holding a row set The buffer is called the “context area” A cursor maintains a pointer to the currently active row, or record, and begins before, yes, before the first row You can process each row, one at a time, and then advance the cursor UPDATE and DELETE are supported when you reference the “CURRENT” row

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-21 Embedded SQL –Cursors A cursor must be declared – here is where you place your SQL statement that will return a set Next OPEN the cursor You may then FETCH the cursor into variables you have declared Each FETCH reads a row into your variables –Often this is accomplished within a loop Your first FETCH reads the first row CLOSE when finished, please

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-22 Dynamic SQL Embedded SQL can be constructed and invoked dynamically DCL SQLSOURCE CHAR VARYING (65000); SQLSOURCE = ‘DELETE FROM SP WHERE QTY < QTY (300)’; EXEC SQL PREPARE SQLPREPPED FROM :SQLSOURCE; EXEC SQL EXECUTE SQLPREPPED;

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-23 SQL Call-Level Interface (CLI) Alternative to Embedded SQL Similar functionality, based in MS ODBC Better than Embedded SQL for two reasons –Can be compiled by the host compiler and distributed shrink wrapped to customers –Greater DBMS independence because they can interface with anyone’s SQL engine