Introduction to Database

Slides:



Advertisements
Similar presentations
Introduction to Database System Wei-Pang Yang, IM.NDHU, Final Test-1 Example: Banking Database 1. branch 2. customer 客戶 ( 存款戶, 貸款戶 ) 5. account.
Advertisements

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.
Chapter 3 An Introduction to Relational Databases.
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.
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.
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
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”
Database System Concepts and Architecture Lecture # 2 21 June 2012 National University of Computer and Emerging Sciences.
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.
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.
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.
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 1 Introduction Yonsei University 1 st Semester, 2014 Sanghyun Park.
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.
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.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
Chapter 4 An Introduction to SQL.
Storage and File Organization
Databases (CS507) CHAPTER 2.
Databases and DBMSs Todd S. Bacastow January 2005.
Introduction to SQL Programming Techniques
SQL Environment.
Lecture 3 : Structured Query Language (SQL)
A Guide to SQL, Seventh Edition
Chapter 2 Database Environment.
UNIT 11 Query Optimization
Unit 2 DB2 and SQL.
Introduction What is a Database?.
Database System Concepts and Architecture
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Database System Concepts and Architecture.
Chapter 2 Database Environment.
Introduction to DBMS (Database Management Systems)
Chapter 2 Database Environment Pearson Education © 2009.
Chapter 2 Database Environment.
Data, Databases, and DBMSs
DB2.
Database Application Development
Introduction to Database
پايگاه داده ها.
Embedded DB2.
Unit I-2.
Unit 7 Normalization (表格正規化).
STRUCTURE OF PRESENTATION :
Database System Concepts and Architecture
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:

Introduction to Database CHAPTER 4B (補) DB2 and SQL Overview Data Definition Data Manipulation The System Catalog Embedded SQL

Overview

Background Relational Model: proposed by Codd, 1970 Ref: CACM Vol. 13, No.6, "A relational model of data for large shared data banks" System R INGRES Developer IBM San Jose Res. Lab 1974 - 1979 UC Berkeley late 1970 - early 1980 Machine IBM System 370 DEC PDP O. S. VM / CMS UNIX Query Language SQL QUEL Language Embedded COBOL or PL/1 COBOL, PASCAL, C FORTRAN, BASIC Commercial Product DB2, SQL / DS Commercial INGRES Distributed OB R* Distributed INGRES OO Extension Starburst POSTGRES

<e.g.> Supplier-and-Parts Database Relational Databases Definition: A Relational Database is a database that is perceived by its users as a collection of tables (and nothing but tables). <e.g.> Supplier-and-Parts Database (Hierarchical) IMS 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

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 < e.g. > S# P# --------------------------------------------------- S1 { P1, P2, P3, P4, P5, P6 } S2 { P1, P2 } . . 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 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 atomic Normalization

Stored database (Internal View) Host Language + DSL User A1 User A2 User B1 User B2 User B3 External View @ # & B External/conceptual mapping A Conceptual View mapping B Conceptual/internal mapping Stored database (Internal View) Database management system dictionary (DBMS) e.g. system catalog < DBA Storage structure definition (Internal schema) schema External A (Build and maintain schemas and mappings) # @ & DSL (Data Sub Language) C, Pascal e.g. SQL 1 2 3 ... 100

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

Major System Components: Execution time PL / 1 LOAD MODULE P If - - CALL Catalog or Dictionary "data about data" Runtime Supervisor Application Plan ( for P ) Stored Data Manager Buffer Manager Database "data"

Major System Components: Optimizer 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 ? SELECT SNAME INTO SNAME FROM S WHERE S# = 'S4' BIND OPTIMIZER DBRM Application Plan 1 Plan 2 X: S#_index s1 s2 . S#

Example: A Simple Query Processing Language Processor Optimizer Operator Processor Access Method File System database Language Processor Access Method e.g.B-tree; Index; Hashing DBMS Query in SQL: SELECT CUSTOMER. NAME FROM CUSTOMER, INVOICE WHERE REGION = 'N.Y.' AND AMOUNT > 10000 AND CUTOMER.C#=INVOICE.C Internal 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

Embedded SQL

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) PL SQL call :

Embedded SQL: a Fragment <e.g.> 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 = ' 00000 ' 12 THEN …. ; /* WEIGHT = retrieved value */ 13 ELSE …. ; /* some exception occurred */

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. SQLCA SQLSTATE or SQLCODE e.g. City, :City (SQL) (PL/I)

Operation: 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 */

Operation: Multiple SELECT How to handle the cases that more than one record are satisfied? Cursor

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 PL/I S# SNAME Smith var var S1 Smith OPEN: X FETCH: S4 Clark

Embedded SQL: An Example 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.

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 ) ; Back 2-8 PL/I Var.

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 ;

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

Embedded SQL: An Example (cont.) WHEN ( SQLCODE = 0 ) /* success */ DO ; DISP = ' bbbbbbb ' ; /* empty the display buffer */ IF CITY = GIVENCIT THEN 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 DO ; EXEC SQL DELETE FROM SP WHERE S# = : S# ;

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 */