Unit 2 DB2 and SQL 1-1. 2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit 2 2.1 Overview DB2 2.2 Data Definition 2.3 Data Manipulation 2.4.

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.
Chapter 3 An Introduction to Relational Databases.
FCU, Department of ECE, IC Design Research Lab. TEL: # 4945 Pre-SIm , Post-Sim.
Software Engineering for Digital Home 單元 2 :軟體處理程序與需求分析 2-3 需求工程處理程序 Presenter: Away.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/3 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH3.5 ~ CH /10/31.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
第 15 章 Servlet 程式設計 Java 2 程式設計入門與應用. 2 目錄 15-1 Servlet 的簡介 15-1 Servlet 的簡介 15-2 建立 Servlet 程式 15-2 建立 Servlet 程式 溫故知新 自我突破習題.
國立聯合大學 資訊管理學系 陳士杰老師 MySQL5 視覺化資料庫管理工具 - MySQL Adminstrator.
1.1 電腦的特性 電腦能夠快速處理資料:電腦可在一秒內處理數百萬個 基本運算,這是人腦所不能做到的。原本人腦一天的工 作量,交給電腦可能僅需幾分鐘的時間就處理完畢。 電腦能夠快速處理資料:電腦可在一秒內處理數百萬個 基本運算,這是人腦所不能做到的。原本人腦一天的工 作量,交給電腦可能僅需幾分鐘的時間就處理完畢。
程式語言實習 - JAVA Class1 1. 介紹 Java 編寫、編譯、執行 2. 請同學完成作業二.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
第 5 章 深入 Response 物件 製作. 網頁的轉向與強制輸出 - 讓網頁轉彎的 Redirect 敘述 運用 Response 物件的 Redirect 方法,將瀏覽器顯 示的網頁,導向至其他網頁,語法如下: Response.Redirect 網頁路徑與名稱 此網頁路徑與名稱  若是導向到同一台.
最新計算機概論 第 5 章 系統程式. 5-1 系統程式的類型 作業系統 (OS) : 介於電腦硬體與 應用軟體之間的 程式,除了提供 執行應用軟體的 環境,還負責分 配系統資源。
1 MySQL 資料庫的使用 網頁資料庫的基礎 網頁資料庫的基礎 下載與安裝 MySQL 下載與安裝 MySQL 啟動 MySQL 伺服器 啟動 MySQL 伺服器 Web 介面的 MySQL 管理程式 Web 介面的 MySQL 管理程式 MySQL 建立資料庫 MySQL 建立資料庫 在 MySQL.
North Point Government Primary PM School 北角官立下午小學 應用 ‘ 基本能力學生評估 ’ 及 ‘ 網上學與教支援系統 ’ 經驗分享.
1-6 動畫的文件屬性 舞台是動畫實際播放的畫面, 所以舞台的大 小與長寬比例對將來動畫的呈現有很大的 影響。 Flash 預設的舞台尺寸是 550 Pixels × 400 Pixels, 背景為白色, 如果要更改舞台大小與 背景色, 請執行『修改 / 文件』命令, 開啟文 件屬性 (Document.
校外使用圖書館購置之資料庫 龍華大學圖書館. 讀者遠端認證 (RPA) 設定說明  透過圖書館架設完成的 RPA (Remote Patron Authentication) 讀者遠端認證代理主 機系統,讀者於校外或院外 可直接連線使 用本館所提供的資料庫。  若非使用本館電子資料,請勿設定此代理.
從此處輸入帳號密碼登入到管理頁面. 點選進到檔案管理 點選「上傳檔案」上傳資料 點選瀏覽選擇電腦裡的檔案 可選擇公開或不公開 為平台上的資料夾 此處為檔案分類,可顯示在展示頁面上,若要參加 MY EG 競賽,做品一律上傳到 “ 98 MY EG Contest ” 點選此處確定上傳檔案.
資料結構實習-一 參數傳遞.
6-2 認識元件庫與內建元件庫 Flash 的元件庫分兩種, 一種是每個動畫專 屬的元件庫 (Library) ;另一種則是內建元 件庫 (Common Libraries), 兩者皆可透過 『視窗』功能表來開啟, 以下即為您說明。
資料庫實作教學.
2010 MCML introduction 製作日期: 2010/9/10 製作人 : 胡名霞.
概念性產品企劃書 呂學儒 李政翰.
Concepts of Database Management Sixth Edition
無線通訊網路 Mac 層 TDM 通訊模式的操作與效能研究 專題生 : 林書弘、蔡逸祥、毛建翔、王政 華 指導教授 : 黃依賢.
組合語言與系統程式 軟體使用教學. 軟體下載及安裝 下載 到課程網頁下載 tw/~jiun/ 安裝 解壓縮後將 System Software 資料夾放在 D 槽 (C 槽版本放在 C 之下 )
幼兒行為觀察與記錄 第八章 事件取樣法.
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.
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.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
6.8 Case Study: E-R for Supplier-and-Parts Database
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
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.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
Wei-Pang Yang, Information Management, NDHU Normalization Unit 7 Normalization ( 表格正規化 ) 7-1.
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:
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Introduction to Database System Wei-Pang Yang, IM.NDHU, Final Test-1 Example: Banking Database 1. branch 2. customer 客戶 ( 存款戶, 貸款戶 ) 5. account.
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.
Internet Technology Laboratory Department of Computer and Communication Kun Shan University  官方網站:
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.
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.
網站開發架構與工具 張耀仁. Motivation Content  HTML, CSS, JavaScript, SQL  Ruby on Rails  Reference.
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.
生物資訊程式語言應用 Part 4 MySQL.
Unit 2 DB2 and SQL.
Introduction to Database
Introduction to Database
پايگاه داده ها.
Φροντιστήριο SQL (από το βιβλίο του Date)
Unit 7 Normalization (表格正規化).
資料庫管理作業(一).
STRUCTURE OF PRESENTATION :
Question 1: Basic Concepts (40 %)
Question 1: Basic Concepts (45 %)
MySQL安裝&操作教學.
STRUCTURE OF PRESENTATION :
Presentation transcript:

Unit 2 DB2 and SQL 1-1

2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview DB2 2.2 Data Definition 2.3 Data Manipulation 2.4 The System Catalog 2.5 Embedded SQL 2.6 實作範例 Unit 2 DB2 and SQL

2.1 Overview DB2 Unit 2 DB2 and SQL 1-3

2-4 Wei-Pang Yang, Information Management, NDHU Background  Relational Model: proposed by Codd, 1970 Ref: A Relational Model of Data for Large Shared Data Banks, CACM, 13(6): , June 1970.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 Unit 2 DB2 and SQL

2-5 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 Model) IMS vs. Unit 2 DB2 and SQL

2-6 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 Unit 2 DB2 and SQL

2-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:(P.2- 46):(46) 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 Unit 2 DB2 and SQL

2-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 (Ref. p. 2-34) Unit 2 DB2 and SQL

2-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 (Ref. p. 2-34) (Ref. p. 2-34) - Considerations : 1. Which table ? 2. How big ? 3. What index exist ?. X: S#_index s1s1 s2s S# Unit 2 DB2 and SQL

2.2 Data Definition Unit 2 DB2 and SQL 1-10

2-11 Wei-Pang Yang, Information Management, NDHU DDL of SQL  Base Table Create Table Drop Table Alter Table  Index Create Index Clustering Index Drop Index  View Create View Drop View Unit 2 DB2 and SQL

2-12 Wei-Pang Yang, Information Management, NDHU Base Tables  A named table S# SNAME STATUS CITY S S# SNAME STATUS CITY DISC S CREATE TABLE S (S# CHAR (5) NOT NULL, SNAME CHAR (20), STATUS SMALLINT, CITY CHAR (15));  Data can be entered by (1) INSERT statement (2) DB2 load utility ALTER TABLE S ADD DISC SMALLINT ; DROP S ; - Description of S is removed from system catalog. - All index and views defined on S are automatically dropped. Unit 2 DB2 and SQL

2-13 Wei-Pang Yang, Information Management, NDHU Base Tables (cont.)  FOREIGN KEY FOREIGN KEY (column-commalist ) REFERENCES base-table [(column-commalist )] [ON DELETE option] [ON UPDATE option]  CHECK CHECK (conditional-expression) : CREATE TABLE SP ( S# S# NOT NULL, P# P# NOT NULL, QTY QTY NOT NULL, PRIMARY KEY ( S#, P# ), FOREIGN KEY ( S# ) REFERENCES S ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY ( P# ) REFERENCES P ON DELETE CASCADE ON UPDATE CASCADE, CHECK ( QTY >0 AND QTY <5001 ) ) ; SP S# P# QTY S S# S 1 Smith ….. S 1 P 1 …. (Ref. p. 3-9) (Ref. p , Integrity Rule) Unit 2 DB2 and SQL

2-14 Wei-Pang Yang, Information Management, NDHU Indexes X : CREATE INDEX X ON SP ( P# ASC, S# ASC); : CREATE UNIQUE INDEX XS ON SP (S#, P#) - enforced that no two tuples have the same index field. : DROP INDEX X; - Definition of X is removed from Catalog. SP Unit 2 DB2 and SQL

2-15 Wei-Pang Yang, Information Management, NDHU Y PNAME#Pointer Nut P Clustering index  Logical sequence  Physical sequence X P# PNAME.... P# Pointer P1 Nut. P1 P2.. P2 P logical sequence : P1 < P2 < P3 <... physical sequence : P1 P2 P3 P1 P3 P2 Clustered page1page2 CREATE INDEX X ON P ( P#) CLUSTER; Note: A given base table can have at most one cluster index. Unit 2 DB2 and SQL

2-16 Wei-Pang Yang, Information Management, NDHU Views SQL View V1 View V2 Data Set D1 Data Set D2 Data Set D3 Data Set D4 Base Table B1 Base Table B2 Base Table B3 Base Table B4 Virtual table (doesn't really exist ) No stored file Definition of view is stored in system catalog A base table may be stored in several files A file may contain several base tables A view may be derived from several base tables A base table may derive several views Unit 2 DB2 and SQL

2-17 Wei-Pang Yang, Information Management, NDHU Views: Example SELECT S#, SNAME, STATUS FROM S WHERE CITY = ' LONDON' ? AS LONDON-SUPPLIERS view name view definition in catalog CREATE VIEW LONDON-SUPPLIERS S# SNAME STATUS S1 Smith 20 S4 Clark 20 S1 Smith 20 S4 Clark 20 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 Unit 2 DB2 and SQL

2-18 Wei-Pang Yang, Information Management, NDHU Views: Example (cont.) Can be used as base table: e.g. S, P, SP SELECT * FROM LONDON-SUPPLIERS WHERE STATUS < 50 SELECT S#, SNAME, STATUS FROM S WHERE STATUS < 50 AND CITY = ' LONDON' converted by Bind (ref. p. 2-7) Unit 2 DB2 and SQL

2-19 Wei-Pang Yang, Information Management, NDHU Views: Advantages  Advantages of views : (1) Provide logical data independence : Logical data independence (e.g. Relation): users and user programs are not dependent on logical structure of the database. Two aspects of logical data independence : Growth and restructuring. (v.s. Physical data independence (e.g. B-tree): users and user programs are not dependent on physical structure of the stored database.) (2) Allow same data to be seen by different users in different ways. (3) User perception is simplified. (4) Automatic security is provided for hidden data View 1 base T 1 base T 2 1->1->1->... Logical Structure Physical Structure (storage) View 2 Physical data independence Logical data independence Unit 2 DB2 and SQL

2.3 Data Manipulation Unit 2 DB2 and SQL 1-20

2-21 Wei-Pang Yang, Information Management, NDHU DML of SQL  Retrieval Operation  SELECT  Update Operation  UPDATE  DELETE  INSERT  Expressions  Table Expressions  Operations on View Unit 2 DB2 and SQL

2-22 Wei-Pang Yang, Information Management, NDHU Retrieval Operations  Get color and city for "non-Paris" parts with weight greater than ten. SELECT P.COLOR, P.CITY FROM P WHERE P.CITY 'Paris' AND P.WEIGHT > 10; DISTINCT SELECT DISTINCT P.COLOR, P.CITY FROM P WHERE P.CITY 'Paris' AND P.WEIGHT > 10; ORDER BY SELECT DISTINCT P.COLOR, P.CITY FROM P WHERE P.CITY 'Paris' AND P.WEIGHT > 10 ORDER BY CITY DESC; 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 Unit 2 DB2 and SQL

2-23 Wei-Pang Yang, Information Management, NDHU Retrieval Operations (cont.)  For all parts, get the part number and the weight of that part in grams. SELECT P.P#, P.WEIGHT * 454 AS GMWT FROM P ; – If the AS MWT specification had been omitted, the corresponding result column would effectively have been unnamed.  Get full details of all suppliers. SELECT * – or "SELECT S.*" (i.e., the "*" can be qualified ) FROM S ;  Get the total number of suppliers. SELECT COUNT ( * ) AS N FROM S ; Unit 2 DB2 and SQL 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

2-24 Wei-Pang Yang, Information Management, NDHU Retrieval Operations (cont.)  Get the maximum and minimum quantity for part P2. SELECT MAX (SP.QTY) AS MAXQ, MIN (SP.QTY) AS MINQ FROM SP WHERE SP. P# = 'P2';  For each part supplied, get the part number and the total shipment quantity. SELECT SP.P#, SUM (SP.QTY) AS TOTQTY FROM SP GROUP BY SP.P#; Unit 2 DB2 and SQL 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

2-25 Wei-Pang Yang, Information Management, NDHU Retrieval Operations (cont.)  Get part numbers for all parts supplied by more than one supplier. SELECT SP.P# FROM SP GROUP BY SP.P# HAVING COUNT ( SP. S# ) > 1;  Get supplier names for suppliers who supply part P2. SELECT DISTINCT S.SNAME FROM S WHERE S. S# IN ( SELECT SP. S# FROM SP WHERE SP.P# = 'P2‘); Unit 2 DB2 and SQL 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

2-26 Wei-Pang Yang, Information Management, NDHU Update Operations  Single-row INSERT. INSERT INTO P(P#, PNAME, COLOR, WEIGHT, CITY) VALUES ('P8', 'Sprocket', 'Pink', 14, 'Nice');  Multi-row INSERT. INSERT INTO TEMP (S#, CITY) SELECT S.S#, S.CITY FROM S WHERE S.STATUS > 15 ;  Multi-row UPDATE. UPDATE P SET COLOR = 'Yellow', WEIGHT = P.WEIGHT + 5 WHERE P.CITY = 'Paris'; Unit 2 DB2 and SQL 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

2-27 Wei-Pang Yang, Information Management, NDHU Update Operations (cont.)  Multi-row UPDATE. UPDATE P SET CITY = ( SELECT S.CITY FROM S WHERE S.S# = 'S5') WHERE P.COLOR = ' Red '  Multi-row DELETE. DELETE FROM SP WHERE 'London‘ = (SELECT S.CITY FROM S WHERE S.S# = SP.S#); Unit 2 DB2 and SQL 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

2-28 Wei-Pang Yang, Information Management, NDHU Table Expressions  The SELECT, FROM, WHERE, GROUP BY, and HAVING clause.  A Comprehensive Example SELECT P. P#, 'Weight in grams = ' AS TEXT1, P.WEIGHT * 454 AS GMWT, P.COLOR, 'Max quantity = ' AS TEXT2, MAX (SP.QTY) AS MQY FROM P, SP WHERE P.P# = SP.P# AND ( P.COLOR = 'Red‘ OR P.COLOR = ' Blue') AND SP.QTY > 200 GROUP BY P. P#, P.WEIGHT, P.COLOR HAVING SUM (SP.QTY) > 350; Unit 2 DB2 and SQL

2-29 Wei-Pang Yang, Information Management, NDHU DML operations on View  Retrieval (SELECT): no problem  Update (INSERT, DELETE, UPDATE): ? (1) Column Subset: theoretically updateable if contains primary key. : CREAT VIEW S#_CITY AS SELECT S#, CITY FROM S; S INSERT INTO S#_CITY VALUES ('S6', 'Rome'); S# S1 S2 S3 S4 S5 SNAME Smith Jones Blake Clark Adams Base Table STATUS CITY London Paris London Athens S# S1 S2 S3 S4 S5 CITY London Paris London Athens S#_CITY View V1 View B1 Base Table B2 S# S1 S2 S3 S4 S5 S6 SNAME Smith Jones Blake Clark Adams Null STATUS Null CITY London Paris London Athens Rome S

2-30 Wei-Pang Yang, Information Management, NDHU DML on View: Column Subset without key CREATE VIEW STATUS_CITY AS SELECT STATUS, CITY FROM S; S# S1 S2 S3 S4 S5 SNAME Smith Jones Blake Clark Adams STATUS CITY London Paris London Athens S STATUS CITY London Paris Athens STATUS_CITY INSERT INTO STATUS_CITY VALUES (30, 'Rome') Primary key cannot be null !! S# S1 S2 S3 S4 S5 Null SNAME Smith Jones Blake Clark Adams Null STATUS CITY London Paris London Athens Rome S Unit 2 DB2 and SQL

2-31 Wei-Pang Yang, Information Management, NDHU DML on View: Row Subset and Join (2) Row Subset: updateable ! (3) Join: some are not updateable.  CREATE VIEW COLOCATED ( S#, SNAME, S.CITY, P#, PNAME, P.CITY) AS SELECT S#, SNAME, S.C ITY, P#, PNAME, P.CITY FORM S, P WHERE S.CITY=P.CITY;  If issued the following command: UPDATE COLOCATED SET S.CITY = 'Athens‘ WHERE S.CITY ='London'  Then S.CITY  P.CITY Violate the definition of the view!! S# S1 S2 S3 S4 S# S1 S2 S3 S4 SNAME Smith Jones Blake Clark SNAME Smith Jones Blake Clark S.CITY London Paris London S.CITY London Paris London P# P1 P4 P6 P2 P5 P2 P5 P1 P4 P6 P# P1 P4 P6 P2 P5 P2 P5 P1 P4 P6 PNAME Nut Screw Cog Bolt Cam Bolt Cam Nut Screw Cog PNAME Nut Screw Cog Bolt Cam Bolt Cam Nut Screw Cog P.CITY London Paris London P.CITY London Paris London COLOCATED S P SP n* mnm 11 Unit 2 DB2 and SQL

2-32 Wei-Pang Yang, Information Management, NDHU DML on View: Statistical Summary (4) Statistical Summary : not updateable. : CREATE VIEW PQ( P#, TOTQTY ) AS SELECT P#, SUM( QTY ) FROM SP GROUP BY P# ; No stored data item for the field " TOTQTY " P# TOTQTY P1 600 P PQ SP P# TOTQTY P1 600 View Base 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 Unit 2 DB2 and SQL

2.4 The System Catalog 1-33

2-34 Wei-Pang Yang, Information Management, NDHU System Catalog: Concept  The Catalog Structure SYSTABLES SYSCOLUMNS SYSINDEXES  System Catalog: "Data about data“ (Ref. p. 2-8) (Ref. p. 2-8) i.e. information of base tables, view, indexes, users, and access privileges that are of interest to the system itself. -Optimizer: use index info. to choose access method. (Ref. p. 2-9) (Ref. p. 2-9) -Authorization subsystem: use access privilege to grant or deny user requests.  Querying the catalog: by SQL DML cc record creator... S 4 5 Yang P 5 6 Yang SP 3 12 Yang T1 T2... u 1 R R u 2 W R u n O W access control matrix Unit 2 DB2 and SQL

2-35 Wei-Pang Yang, Information Management, NDHU Updating the Catalog  Cannot use the SQL update, delete and insert, because it would be too easy to destroy information!  It is data definition statements (i.e. CREATE, DROP, ALTER ) that perform such updates. CREATE = INSERT into catalog DROP = DELETE from catalog ALTER = UPDATE catalog Unit 2 DB2 and SQL

2-36 Wei-Pang Yang, Information Management, NDHU Updating the Catalog: Example  : CREATE TABLE S (S# CHAR(5) Not Null, SNAME CHAR(20), STATUS SMALLINT, CITY CHAR(5); SYSTABLE NAME CREATOR... S SYSCOLUMNS NAME TBNAME... S# S SNAME S CITY S STATUS S... Unit 2 DB2 and SQL

2-37 Wei-Pang Yang, Information Management, NDHU Updating the Catalog: COMMENT  Catalog also includes entries for catalog tables. SYSTABLE NAME CREATOR... REMARK SYSTABLE SYSIBM SYSCOLUMN SYSIBM S Yang... Supplier P Yang... Part SP Yang  The only statement that updates catalog: COMMENT : COMMENT ON TABLE S IS Supplier Unit 2 DB2 and SQL

2.5 Embedded SQL Unit 2 DB2 and SQL 1-38

2-39 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 : Unit 2 DB2 and SQL

2-40 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 */ Unit 2 DB2 and SQL

2-41 Wei-Pang Yang, Information Management, NDHU 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 var., :City) 4. Any table used should be declared by DECLARE TABLE, it is used by pre-compiler. Unit 2 DB2 and SQL

2-42 Wei-Pang Yang, Information Management, NDHU 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 Unit 2 DB2 and SQL

2-43 Wei-Pang Yang, Information Management, NDHU Operations  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 Unit 2 DB2 and SQL 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

2-44 Wei-Pang Yang, Information Management, NDHU Operation: Multiple SELECT  Multiple SELECT: How to handle the cases that more than one record are satisfied? Cursor Unit 2 DB2 and SQL 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 EXEC SQL SELECT STATUS FROM S WHERE CITY=: GIVENCIT;

2-45 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 S1 Smith OPEN: X FETCH: S4 Clark X S1 SmithS4 Clark S1 PL/I S# SNAME Smith var open cursor close cursor Unit 2 DB2 and SQL 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

2-46 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An 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. Unit 2 DB2 and SQL

2-47 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 ) ; Back 2-7Back 2-7 PL/I Var. Unit 2 DB2 and SQL

2-48 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# ) i.e. P2 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 ; Unit 2 DB2 and SQL

2-49 Wei-Pang Yang, Information Management, NDHU Embedded SQL: An Example (cont.) GET LIST ( GIVENP#, GIVENCIT, GIVENINC, GIVENLVL ) ; EXEC SQL OPEN Z ; IF SQLCODE 0 /* abnormal */ 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 ) ; Main SQLCODE =0 Unit 2 DB2 and SQL

2-50 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# ; Unit 2 DB2 and SQL

2-51 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 */ Unit 2 DB2 and SQL

2.6 實作及範例 Unit 2 DB2 and SQL 1-52

2-53 Wei-Pang Yang, Information Management, NDHU Program Exercise 1 : Using DBMS (EX1:usingSQL)  EX (p ) Using the suppliers-parts-projects database, write a program with embedded SQL statements to list all supplier rows, in supplier number order. Each supplier row should be immediately followed in the listing by all project rows for projects supplied by that supplier, in project number order. create database selection update query catalog. embedded SQL (program) Unit 2 DB2 and SQL

2-54 Wei-Pang Yang, Information Management, NDHU 下載安裝 WampSever( 含 MySQL)  建立使用環境 : 下載安裝 WampSever WampServer 是一個整合了 Apache ( 網站伺服器 ) 、 MySQL ( 資料庫系統 ) 、 和 PHP 的套件,即安裝一次 WampServer ,就同時安裝完成上述軟體。 WampServer 我們就是資料庫網站開發人員 可在自己的桌面電腦上編寫和測試程式,無須檔案上傳到伺服器, 可以節省很多時間; 程式錯誤不會破壞伺服器,若有差錯,也只是在自已的電腦的某一 個資料夾,絕不會影響伺服器上的其他系統。  點選影音檔 Ex 1 : 下載安裝 WampSever ( 含 MySQL) [ 助教說明 ]  點選影音檔 Ex 1 : 下載安裝 WampSever ( 含 MySQL) [ 吳嘉峰同學 範例說明 ] Unit 2 DB2 and SQL

2-55 Wei-Pang Yang, Information Management, NDHU  透過 phpMyAdmin 操作 MySQL phpMyAdmin 是一個以 PHP 為基礎,以 Web-Base 方式架構在網站主機上的 MySQL 的資料庫管理工具,讓管理者可用 Web 介面管理 MySQL 資料庫。 由此 Web 介面可以簡易方式輸入 SQL 語法並顯示結果。 phpMyaAdmin 跟其他 PHP 程式一樣在網頁伺服器上執行,可以在任何地方使用 這些程式產生的 HTML 頁面,也就是可以遠端管理 MySQL 資料庫,即可以方便 的建立、修改、刪除資料庫及資料表。  參考說明與範例 點選影音檔 Ex1: 透過 phpMyAdmin 操作 MySQL [ 助教說明 ] 點選影音檔 Ex1: 透過 phpMyAdmin 操作 MySQL [ 吳嘉峰同學範例說明 ] 參考點選更多的範例說明 Unit 2 DB2 and SQL 透過 phpMyAdmin 操作 MySQL