版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、4B-1Wei-Pang Yang, Information Management, NDHUIntroduction to DatabaseCHAPTER 4B (補補)DB2 and SQLqOverviewq Data Definitionq Data Manipulationq The System Catalogq Embedded SQL1-2Overview4B-3Wei-Pang Yang, Information Management, NDHUBackgroundRelational Model: proposed by Codd, 1970 Ref: CACM Vol.
2、13, No.6, A relational model of data for large shared data banks4B-4Wei-Pang Yang, Information Management, NDHURelational DatabasesDefinition: A Relational Database is a database that is perceived by its users as a collection of tables (and nothing but tables). Supplier-and-Parts DatabaseP# PNAME CO
3、LOR WEIGHT CITYP1 Nut Red 12 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London PS# SNAME STATUS CITYS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens S S# P# QTY S1 P1 300 S1 P2 200 S1 P3 400 S1 P4 2
4、00S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400SP(Hierarchical)IMS4B-5Wei-Pang Yang, Information Management, NDHURelational Databases (cont.)S, P, SP: 3 relations (tables)A row in a relation is called a tuple (record)S, P: entities; SP: relationshipprimary key: S# in S, P#
5、 in P, (S#, P#) in SPatomic: not a set of values, instead of repeating group S# P# - S1 P1, P2, P3, P4, P5, P6 S2 P1, P2 . . . . . .atomicNormalizationS# SNAME STATUS CITYS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens SP# PNAME COLOR WEIGHT CITYP1 Nut Red 12
6、 LondonP2 Bolt Green 17 ParisP3 Screw Blue 17 RomeP4 Screw Red 14 LondonP5 Cam Blue 12 ParisP6 Cog Red 19 London P S# P# QTY S1 P1 300 S1 P2 200 S1 P3 400 S1 P4 200S1 P5 100S1 P6 100S2 P1 300S2 P2 400S3 P2 200S4 P2 200S4 P4 300S4 P5 400SP4B-6Wei-Pang Yang, Information Management, NDHUHost Language+
7、DSLHost Language+ DSLHost Language+ DSLHost Language+ DSLHost Language+ DSLUser A1User A2User B1User B2User B3External View # &External ViewBExternal/conceptualmapping AConceptualViewExternal/conceptualmapping BConceptual/internalmappingStored database (Internal View)Databasemanagementsystem diction
8、ary(DBMS) e.g. system catalogDBAStoragestructuredefinition(Internalschema)ConceptualschemaExternalschemaAExternalschemaB(Build andmaintainschemasandmappings)#& DSL (Data Sub Language)C, Pascale.g. SQLSQL123123.1004B-7Wei-Pang Yang, Information Management, NDHUMajor System Components: DB2SourceModule
9、ModifiedSourceModuleDBRMObjectModuleLoadModuleApplicationPlanPL/I-CompilerBindPre-compilerLinkageEditor(Load Module)(Application Plan)Runtime SupervisorData ManagerBuffer Manager(Other)DBPL/I + SQL:EXEC SQL CREATE TABLE S ( S# CHAR(5), .)EXEC SQL SELECT SNAME INTO :SNAME FROM SIF SQLCODE 0 THEN .CAL
10、L CREATE( .)CALL SELECT( .)IF SQLCODE 10000 AND CUTOMER.C#=INVOICE.CInternal 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 fieldCalls to Access Method: OPEN SCAN on C with region index GET next tup
11、le . . .Calls to : GET10th to 25th bytes from block #6 of Language ProcessorOptimizerOperator ProcessorAccess MethodFile System databaseLanguageProcessorAccessMethode.g.B-tree; Index; Hashing DBMS1-11Embedded SQL4B-12Wei-Pang Yang, Information Management, NDHUEmbedded SQL: Dual-mode Dual-mode princi
12、ple: 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)PLSQLcall : 4B-13Wei-Pang Yang, Information Management, NDHUEmbedded SQL: a Fragment Fragment of a PL/I program with embedded
13、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 = r
14、etrieved value */ 13 ELSE . ; /* some exception occurred */ 4B-14Wei-Pang Yang, Information Management, NDHUEmbedded 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).
15、3. SQL statements can reference host variable. (PL/I變數(shù))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 0If more than one record are sa
16、tisfied: SQLCODE 0How 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-16Wei-Pang Yang, Information Management, NDHUOperation: Mul
17、tiple SELECTMultiple SELECT:How to handle the cases that more than one record are satisfied? Cursor4B-17Wei-Pang Yang, Information Management, NDHUCursorA 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 =:
18、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 XS#SNAMEdefine cursorendS1 SmithOPEN:XFETCH:S4 ClarkXS1 SmithS4 ClarkS1PL/IS
19、#SNAMESmithvarvar4B-18Wei-Pang Yang, Information Management, NDHUEmbedded SQL: An ExampleEmbedded 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 s
20、uppliers 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
21、 listed on the printer, with an indication of how that particular supplier was handled by the program. 4B-19Wei-Pang Yang, Information Management, NDHUEmbedded SQL: An Example (cont.)SQLEX: PROC OPTIONS (MAIN) ; DCL GIVENP # CHAR(6) ; DCL GIVENCIT CHAR(15) ; DCL GIVENINC FIXED BINARY(15) ; DCL GIVEN
22、LVL 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
23、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-20Wei-Pang Yang, Information Management, NDHUEmbedded SQL: An Example (cont.) EXEC SQL DECLARE Z CURSOR FOR SELECT S#, SNAME, STATUS, CITY FROM S WHERE EXISTS ( SELECT * FR
24、OM 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-21We
25、i-Pang Yang, Information Management, NDHUEmbedded 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 ) ;主程式成功=04B-22Wei-Pang Yang, Information Management,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度房屋買賣合同土地使用權(quán)變更范本3篇
- 2025版航空貨運客戶滿意度提升合同3篇
- 2025年度電子商務(wù)平臺銷售合同重要性分析
- 二零二五年度應(yīng)急預(yù)案制定與演練合同3篇
- 課程設(shè)計論文選題思路
- 二零二五年度數(shù)據(jù)中心機房監(jiān)控系統(tǒng)隔音降噪施工合同
- 自動專業(yè) 課程設(shè)計
- 二零二五年度教育機構(gòu)勞動合同規(guī)范標(biāo)準(zhǔn)3篇
- 線上藝術(shù)創(chuàng)作課程設(shè)計
- 瑜伽小班課程設(shè)計圖
- 新人教版一年級數(shù)學(xué)下冊全冊導(dǎo)學(xué)案
- 2025年中考語文復(fù)習(xí)之現(xiàn)代文閱讀:非連續(xù)性文本閱讀(10題)
- GB/T 9755-2024合成樹脂乳液墻面涂料
- 商業(yè)咨詢報告范文模板
- 2024年度軟件定制開發(fā)合同(ERP系統(tǒng))3篇
- 家族族譜模板
- 家譜修編倡議書范文
- 高中體育與健康人教版全一冊 形意強身功 課件
- 高中語文《勸學(xué)》課件三套
- 人教版一年級數(shù)學(xué)上冊-教材分析
- 【企業(yè)盈利能力探析的國內(nèi)外文獻綜述2400字】
評論
0/150
提交評論