Chapter 4 An Introduction to SQL.

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

Basic SQL Introduction Presented by: Madhuri Bhogadi.
Copyright © 2004 Pearson Education, Inc.. Chapter 9 More SQL: Assertions, Views, and Programming Techniques.
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.
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.
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.
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.
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.
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)
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.
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.
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.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
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.
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.
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.
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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.
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.
Fundamental of Database Systems
Introduction to SQL Programming Techniques
SQL Environment.
Lecture 3 : Structured Query Language (SQL)
A Guide to SQL, Seventh Edition
Chapter 10 Views.
PGT(CS) ,KV JHAGRAKHAND
Unit 2 DB2 and SQL.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Introduction What is a Database?.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Database System Concepts and Architecture.
CPSC-310 Database Systems
Database Application Development
Introduction to Database
Introduction to Database
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
variables and control statements in PL\SQL
Database Processing: David M. Kroenke’s Chapter Seven:
Database systems Lecture 3 – SQL + CRUD
Unit I-2.
SQL .. An overview lecture3.
Lecture 17: Systems Aspects of SQL
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
DATABASE Purpose of database
Presentation transcript:

Chapter 4 An Introduction to SQL

SQL: History and Overview The Catalog Views Embedded SQL Cursors 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.

Early prototype called System R Current version is SQL:1999 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.

It is the standard language for relational systems, although imperfect 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.

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.

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.

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.

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.

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.

Every database has a catalog A catalog has many schemas, one per user 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.

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.

CREATE VIEW GOOD_SUPPLIER AS SELECT S#, STATUS, CITY FROM S 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.

SELECT GOOD_SUPPLIER .S#, GOOD_SUPPLIER .STATUS 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.

SQL statements can be embedded in a host program 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.

SQL statements after EXEC SQL can reference host variables 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.

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.

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

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.

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.

A cursor is a buffer holding a row set 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.

You may then FETCH the cursor into variables you have declared 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.

Embedded SQL can be constructed and invoked dynamically 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.

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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.