4B-1 Wei-Pang Yang, Information Management, NDHU Introduction to Database CHAPTER 4B ( 補 ) DB2 and SQL  Overview  Data Definition  Data Manipulation.

Slides:



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

Introduction to Database System Wei-Pang Yang, IM.NDHU, Final Test-1 Example: Banking Database 1. branch 2. customer 客戶 ( 存款戶, 貸款戶 ) 5. account.
Unit 5 The Network Model  5.1 The Network Model  5.2 IDMS.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Chapter 3 An Introduction to Relational Databases.
UNIT 15 Query Optimization Wei-Pang Yang, Information Management, NDHU Contents  15.1 Introduction to Query Optimization  15.2 The Optimization.
Chapter 8 Embedded SQL.
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.
1 Minggu 6, Pertemuan 11 Programmatic SQL Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
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.
DATABASE MANAGEMENT SYSTEM ARCHITECTURE
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.
Unit 2 DB2 and SQL Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview DB2 2.2 Data Definition 2.3 Data Manipulation 2.4.
1-1 Unit 1 Introduction to DBMS (Database Management Systems) Application program End-user DBMS.
Introduction Chapter 1. Reference Book  Database Systems Thomas Connolly, Carolyn Begg, Anne Strachan Addison-Wesley 1999 ISBN:
1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.
Database System Concepts and Architecture Lecture # 3 22 June 2012 National University of Computer and Emerging Sciences.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
Chapter 2 CIS Sungchul Hong
Chapter 2 Database System Architecture. An “architecture” for a database system. A specification of how it will work, what it will “look like.” The “ANSI/SPARC”
6.8 Case Study: E-R for Supplier-and-Parts Database
Database System Concepts and Architecture Lecture # 2 21 June 2012 National University of Computer and Emerging Sciences.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
1-1 Homework 3 Practical Implementation of A Simple Rational Database Management System.
Data Definition After this lecture, you should be able to:
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.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.
Unit 5 The Network Model  5.1 Data Modeling Issues  5.2 The Network Model  5.3 IDMS.
DATABASE MANAGEMENT SYSTEM ARCHITECTURE
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Chapter 8 Embedded SQL.
In this module, we will design a program to work with the PROJECT table, learning to insert a new project into the table, to delete one or more projects.
Index in Database Unit 12 Index in Database 大量資料存取方法之研究 Approaches to Access/Store Large Data 楊維邦 博士 國立東華大學 資訊管理系教授.
Unit 2 DB2 and SQL. 2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview 2.2 Data Definition 2.3 Data Manipulation 2.4 The System.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Data and Applications Security Developments and Directions Dr. Bhavani Thuraisingham The University of Texas at Dallas Lecture #2 Supporting Technologies:
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.
Chapter 4 An Introduction to SQL.
Storage and File Organization
Databases and DBMSs Todd S. Bacastow January 2005.
Lecture 3 : Structured Query Language (SQL)
A Guide to SQL, Seventh Edition
UNIT 11 Query Optimization
Unit 2 DB2 and SQL.
Introduction What is a Database?.
Chapter 2 Database Environment Pearson Education © 2009.
Data, Databases, and DBMSs
Database Application Development
Introduction to Database
Introduction to Database
پايگاه داده ها.
Embedded DB2.
Unit I-2.
Unit 7 Normalization (表格正規化).
Question 1: Basic Concepts (45 %)
Unit 12 Index in Database 大量資料存取方法之研究 Approaches to Access/Store Large Data 楊維邦 博士 國立東華大學 資訊管理系教授.
Chapter 2 Database Environment Pearson Education © 2009.
Chapter 2 Database Environment Pearson Education © 2009.
STRUCTURE OF PRESENTATION :
Unit 12 Index in Database 大量資料存取方法之研究 Approaches to Access/Store Large Data 楊維邦 博士 國立東華大學 資訊管理系教授.
Presentation transcript:

4B-1 Wei-Pang Yang, Information Management, NDHU Introduction to Database CHAPTER 4B ( 補 ) DB2 and SQL  Overview  Data Definition  Data Manipulation  The System Catalog  Embedded SQL

Overview

4B-3 Wei-Pang Yang, Information Management, NDHU Background  Relational Model: proposed by Codd, 1970 Ref: CACM Vol. 13, No.6, "A relational model of data for large shared data banks" System RINGRES DeveloperIBM San Jose Res. Lab UC Berkeley late early 1980 MachineIBM System 370DEC PDP O. S.VM / CMSUNIX Query Language SQLQUEL Language EmbeddedCOBOL or PL/1COBOL, PASCAL, C FORTRAN, BASIC Commercial ProductDB2, SQL / DSCommercial INGRES Distributed OBR*Distributed INGRES OO ExtensionStarburstPOSTGRES

4B-4 Wei-Pang Yang, Information Management, NDHU Relational Databases  Definition: A Relational Database is a database that is perceived by its users as a collection of tables (and nothing but tables). Supplier-and-Parts Database P# PNAME COLOR WEIGHT CITY P1 Nut Red 12 London P2 Bolt Green 17 Paris P3 Screw Blue 17 Rome P4 Screw Red 14 London P5 Cam Blue 12 Paris P6 Cog Red 19 London P S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens S S# P# QTY S1 P1 300 S1 P2 200 S1 P3 400 S1 P4 200 S1 P5 100 S1 P6 100 S2 P1 300 S2 P2 400 S3 P2 200 S4 P2 200 S4 P4 300 S4 P5 400 SP (Hierarchical) IMS

4B-5 Wei-Pang Yang, Information Management, NDHU Relational Databases (cont.) S, P, SP: 3 relations (tables) A row in a relation is called a tuple (record) S, P: entities; SP: relationship primary key: S# in S, P# in P, (S#, P#) in SP atomic: not a set of values, instead of repeating group S# P# S1 { P1, P2, P3, P4, P5, P6 } S2 { P1, P2 }.. atomic Normalization S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens S P# PNAME COLOR WEIGHT CITY P1 Nut Red 12 London P2 Bolt Green 17 Paris P3 Screw Blue 17 Rome P4 Screw Red 14 London P5 Cam Blue 12 Paris P6 Cog Red 19 London P S# P# QTY S1 P1 300 S1 P2 200 S1 P3 400 S1 P4 200 S1 P5 100 S1 P6 100 S2 P1 300 S2 P2 400 S3 P2 200 S4 P2 200 S4 P4 300 S4 P5 400 SP

4B-6 Wei-Pang Yang, Information Management, NDHU Host Language + DSL Host Language + DSL Host Language + DSL Host Language + DSL Host Language + DSL User A1User A2User B1User B2User B3 External # & External View B External/conceptual mapping A Conceptual View External/conceptual mapping B Conceptual/internal mapping Stored database (Internal View) Database management system dictionary (DBMS) e.g. system catalog < DBA Storage structure definition (Internal schema ) Conceptual schema External schema A External schema B (Build and maintain schemas and mappings) & DSL (Data Sub Language) C, Pascal e.g. SQL

4B-7 Wei-Pang Yang, Information Management, NDHU Major System Components: DB2 Source Module Modified Source Module DBRM Object Module Load Module Application Plan PL/I-Compiler Bind Pre-compiler Linkage Editor (Load Module) (Application Plan) Runtime Supervisor Data Manager Buffer Manager (Other) DB PL/I + SQL: EXEC SQL CREATE TABLE S ( S# CHAR(5),......) EXEC SQL SELECT SNAME INTO :SNAME FROM S IF SQLCODE < 0 THEN CALL CREATE(......) CALL SELECT(......) IF SQLCODE < 0 THEN PL/I source parse-tree version of SQL request module a DB Compiler optimized machine codes of SQL statements in system Catalog ' 4 1st time 1. Pre-compiled 2. Bind 3. Runtime Supervisor 4. Data Manager 5. Buffer Manager

4B-8 Wei-Pang Yang, Information Management, NDHU Major System Components: Execution time Runtime Supervisor Application Plan ( for P ) Stored Data Manager If - - CALL - Catalog or Dictionary "data about data" Database "data" Buffer Manager PL / 1 LOAD MODULE P Execution time

4B-9 Wei-Pang Yang, Information Management, NDHU Major System Components: Optimizer SELECT SNAME INTO SNAME FROM S WHERE S# = 'S4' BIND OPTIMIZER BIND OPTIMIZER DBRM Application Plan 1 Application Plan 2 - Plan 1 (without index) : SCAN S if S # = 'S4’ then extract name field go to SCAN S. - Plan 2 (with sorted index) : Binary Search X if X.key = 'S4‘ then Be chosen by Optimizer - Considerations : 1. Which table ? 2. How big ? 3. What index exist ?. X: S#_index s1s1 s2s S#

4B-10 Wei-Pang Yang, Information Management, NDHU Example: A Simple Query Processing Query in SQL : SELECT CUSTOMER. NAME FROM CUSTOMER, INVOICE WHERE REGION = 'N.Y.' AND AMOUNT > AND CUTOMER.C#=INVOICE.C I nternal Form :  (  (S SP) Operator : SCAN C using region index, create C SCAN I using amount index, create I SORT C?and I?on C# JOIN C?and I?on C# EXTRACT name field Calls to Access Method : OPEN SCAN on C with region index GET next tuple. Calls to file system : GET10th to 25th bytes from block #6 of file #5 Language Processor Optimizer Operator Processor Access Method File System database Language Processor Access Method e.g.B-tree; Index; Hashing DBMS

Embedded SQL

4B-12 Wei-Pang Yang, Information Management, NDHU Embedded SQL: Dual-mode  Dual-mode principle: any SQL statement that can be used at terminal (interactive), can also be used in an application program (programmable).  PL/I (Record operations) vs. SQL (Set operations) PLSQL call :

4B-13 Wei-Pang Yang, Information Management, NDHU Embedded SQL: a Fragment  Fragment of a PL/I program with embedded SQL 1 EXEC SQL BEGIN DECLARE SECTION ; 2 DCL SQLSTATE CHAR(5) ; 3 DCL P# CHAR(6) ; 4 DCL WEIGHT FIXED DECIMAL(3) ; 5 EXEC SQL END DECLARE SECTION ; 6 P# = ' P2 ' ; /* for example */ 7 EXEC SQL SELECT P.WEIGHT 8 INTO :WEIGHT 9 FROM P 10 WHERE P. P# = :P# ; 11 IF SQLSTATE = ' ' 12 THEN …. ; /* WEIGHT = retrieved value */ 13 ELSE …. ; /* some exception occurred */

4B-14 Wei-Pang Yang, Information Management, NDHU Embedded SQL: a Fragment (cont.) 1. Embedded SQL statements are prefix by EXEC SQL. 2. Executable statements can appear wherever. (non-executable statements: e.g. DECLARE TABLE, DECLARE CURSOR). 3. SQL statements can reference host variable. (PL/I 變數 ) 4. Any table used should be declared by DECLARE TABLE, because it is used by pre-compiler. 5. SQLSTATE/SQLCODE: feedback information of SQL, stored in SQLCA (SQL Communication Area). SQLSTATE = 0 success > 0 warning < 0 error 6. Host variables must have compatible data type with SQL field. 7. Host variables can have same name as database fields. e.g. City, :City (SQL) (PL/I) SQLCA SQLSTATE or SQLCODE

4B-15 Wei-Pang Yang, Information Management, NDHU Operation: Singleton SELECT  Singleton SELECT: EXEC SQL SELECT STATUS INTO :RANK FROM S WHERE S#=: GIVENS#; If only one record is satisfied: SQLCODE = 0 If no record is satisfied: SQLCODE > 0 If more than one record are satisfied: SQLCODE < 0 How to deal with NULL value? Indicator variable! EXEC SQL SELECT STATUS INTO :RANK :RANKIND FROM S WHERE S#=:GIVENS# RANKIND: an indicator variable, 15-bit signed binary integer. If RANKIND = -1 THEN ……/* Status was NULL */

4B-16 Wei-Pang Yang, Information Management, NDHU Operation: Multiple SELECT  Multiple SELECT: How to handle the cases that more than one record are satisfied? Cursor

4B-17 Wei-Pang Yang, Information Management, NDHU Cursor  A kind of pointer that can be run through a set of records. EXEC SQL DECLARE X CURSOR FOR /*define cursor S*/ SELECT S#, SNAME FROM S WHERE CITY =:Y; EXEC SQL OPEN X; /*activate cursor, execute the query*/ DO for all S records accessible via X; EXEC SQL FETCH X INTO :S#, :SNAME /*advance pt., assign values */ END; EXEC SQL CLOSE X; /*deactivate cursor X*/ e.g. Y = 'London' X S# SNAME define cursor end S1 Smith OPEN: X FETCH: S4 Clark X S1 SmithS4 Clark S1 PL/I S# SNAME Smith var

4B-18 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An Example  Embedded SQL A comprehensive example The program accepts four input values : a part number (GIVENP#), a city name (GIVENCIT), a status increment (GIVENINC), and a status level (GIVENLVL). The program scans all suppliers of the part identified by GIVENP#. For each such supplier, if the supplier city is GIVENCIT, then the status is increased by GIVENINC; otherwise, if the status is less than GIVENLVL, the supplier is deleted, together with all shipments for that supplier. In all cases supplier information is listed on the printer, with an indication of how that particular supplier was handled by the program.

4B-19 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An Example (cont.) SQLEX: PROC OPTIONS (MAIN) ; DCL GIVENP # CHAR(6) ; DCL GIVENCIT CHAR(15) ; DCL GIVENINC FIXED BINARY(15) ; DCL GIVENLVL FIXED BINARY(15) ; DCL S# CHAR(5) ; DCL SNAME CHAR(20) ; DCL STATUS FIXED BINARY(15) ; DCL CITY CHAR(15) ; DCL DISP CHAR(7) ; DCL MORE_SUPPLIERS BIT(1) ; EXEC SQL INCLUDE SQLCA ; /* p.2-41 */ EXEC SQL DECLARE S TABLE ( S# CHAR(5) NOT NULL, SNAME CHAR(20) NOT NULL, STATUS SMALLINT NOT NULL, CITY CHAR(20) NOT NULL ) ; EXEC SQL DECLARE SP TABLE ( S# CHAR(5) NOT NULL, P# CHAR(6) NOT NULL, QTY INTEGER NOT NULL ) ; PL/I Var.

4B-20 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An Example (cont.) EXEC SQL DECLARE Z CURSOR FOR SELECT S#, SNAME, STATUS, CITY FROM S WHERE EXISTS ( SELECT * FROM SP WHERE SP. S# = S. S# AND SP. P# = : GIVENP# ) FOR UPDATE OF STATUS ; EXEC SQL WHENEVER NOT FOUND CONTINUE ; EXEC SQL WHENEVER SQLERROR CONTINUE ; EXEC SQL WHENEVER SQLWARNING CONTINUE ; ON CONDITION ( DBEXCEPTION ) BEGIN ; PUT SKIP LIST ( SQLCA ) ; EXEC SQL ROLLBACK ; GO TO QUIT ; END ;

4B-21 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An Example (cont.) GET LIST ( GIVENP#, GIVENCIT, GIVENINC, GIVENLVL ) ; EXEC SQL OPEN Z ; IF SQLCODE 0 /* 不正常 */ THEN SIGNAL CONDITION ( DBEXCEPTION ) ; MORE_SUPPLIERS = ' 1' B ; DO WHILE ( MORE_SUPPLIERS ) ; EXEC SQL FETCH Z INTO :S#, :SNAME, :STATUS, :CITY ; SELECT ; /* case */ /* a PL/I SELECT, not a SQL SELECT */ WHEN ( SQLCODE = 100 ) /* Not found */ MORE_SUPPLIERS = ' 0 ' B ; WHEN ( SQLCODE 100 & SQLCODE 0 ) /* Warning */ SIGNAL CONDITION ( DBEXCEPTION ) ; 主程式 成功 =0

4B-22 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An Example (cont.) WHEN ( SQLCODE = 0 ) /* success */ DO ; DISP = ' bbbbbbb ' ; /* empty the display buffer */ IF CITY = GIVENCIT THEN DO ; EXEC SQL UPDATE S SET STATUS = STATUS + : GIVENINC; WHERE CURRENT OF Z ; IF SQLCODE 0 THEN SIGNAL CONDITION ( DBEXCEPTION ) ; DISP = ' UPDATED ' ; END ; ELSE IF STATUS < GIVENLVL THEN DO ; EXEC SQL DELETE FROM SP WHERE S# = : S# ;

4B-23 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An Example (cont.) IF SQLCODE 0 & SQLCODE 100 THEN SIGNAL CONDITION ( DBEXCEPTION ); EXEC SQL DELETE FROM S WHERE CURRENT OF Z ; IF SQLCODE 0 THEN SIGNAL CONDITION ( DBEXCEPTION); DISP = 'DELETED ' ; END ; PUT SKIP LIST ( S#, SNAME, STATUS, CITY, DISP ) ; END ; /* WHEN ( SQLCODE = 0 ) */ END ; /* PL/I SELECT */ END ; /* DO WHILE */ EXEC SQL CLOSE Z ; EXEC SQL COMMIT ; /* normal termination */ QUIT: RETURN ; END ; /* SQLEX */