課程資源數(shù)據(jù)庫系統(tǒng)課件chorsql_第1頁
課程資源數(shù)據(jù)庫系統(tǒng)課件chorsql_第2頁
課程資源數(shù)據(jù)庫系統(tǒng)課件chorsql_第3頁
課程資源數(shù)據(jù)庫系統(tǒng)課件chorsql_第4頁
課程資源數(shù)據(jù)庫系統(tǒng)課件chorsql_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余82頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

Chapter4

Object-RelationalSQL(Oracle)2023/2/20DatabasePrinciples&Programming2eidenamepositiondependentsdep_namedep_agee001Smith,JohnAgentMichaelJ.9SusanR.7e002Andrews,DavidSuperintendentDavidM.Jr.10e003Jones,FranklinAgentAndrewK.11MarkW.9LouisaM.4Figure4.1AnemployeesTablewithaMulti-ValueddependentsAttributes2023/2/20DatabasePrinciples&Programming3Figure1.1bObject-RelationalStudentEnrollment

Databasesidnameclasstelephoneenrollmentlnamefnamecnomajor1JonesAllan2555-1234101No108Yes2SmithJohn3555-4321105No3BrownHarry2555-1122101Yes108No5WhiteEdward3555-3344102No105No2023/2/20DatabasePrinciples&Programming4ContentObjectTypesTheREFObjectReferenceCollectionTypesnestedtablesandvarraysPL/SQLProcedures,UDFs,andMethodsOthers2023/2/20DatabasePrinciples&Programming51.ObjectTypes1.1定義新的數(shù)據(jù)類型(對(duì)象類型)1.2對(duì)象類型的使用方法1.3對(duì)象值的創(chuàng)建、查詢與更新1.4對(duì)象的引用類型2023/2/20DatabasePrinciples&Programming61.ObjectTypes1.1定義新的數(shù)據(jù)類型(對(duì)象類型)1.2對(duì)象類型的使用方法1.3對(duì)象值的創(chuàng)建、查詢與更新1.4對(duì)象的引用類型2023/2/20DatabasePrinciples&Programming71.1定義新的數(shù)據(jù)類型對(duì)象類型的創(chuàng)建與刪除CREATETYPEtypenameASOBJECT(attrnamedatatype,......);這些數(shù)據(jù)類型一經(jīng)定義,便以持久形式保存在數(shù)據(jù)庫系統(tǒng)中,用戶可以象使用系統(tǒng)內(nèi)置的數(shù)據(jù)類型一樣使用這些復(fù)雜的數(shù)據(jù)類型,以此來擴(kuò)充系統(tǒng)的數(shù)據(jù)類型。DROPTYPEtypename;2023/2/20DatabasePrinciples&Programming81.1定義新的數(shù)據(jù)類型Example4.2.1:創(chuàng)建一個(gè)’姓名’(name_t)類型CREATETYPE

name_t

ASOBJECT(lnamevarchar(30),fnamevarchar(30),michar(1));2023/2/20DatabasePrinciples&Programming91.1定義新的數(shù)據(jù)類型例如:創(chuàng)建一個(gè)‘文檔’類型CREATETYPEDocumentASOBJECT(namevarchar(50),authorvarchar(30),dateDate);CREATETABLEcustomers(cidchar(4)notnull,cnamevarchar(13),cityvarchar(20),discntreal,PRIMARYKEY(cid));創(chuàng)建‘表’的命令2023/2/20DatabasePrinciples&Programming101.ObjectTypes1.1定義新的數(shù)據(jù)類型(對(duì)象類型)1.2對(duì)象類型的使用方法1.3對(duì)象值的創(chuàng)建、查詢與更新1.4對(duì)象的引用類型2023/2/20DatabasePrinciples&Programming111.2對(duì)象類型的使用1)使用所創(chuàng)建的對(duì)象類型來創(chuàng)建新類型用于定義新類型中的屬性(類型的嵌套定義)2)使用所創(chuàng)建的對(duì)象類型來創(chuàng)建新的表用于定義表中的屬性如同使用普通數(shù)據(jù)類型一樣,可以用對(duì)象類型來定義表或?qū)ο箢愋椭械膶傩?)使用對(duì)象數(shù)據(jù)類型來直接創(chuàng)建一張表新創(chuàng)建的‘表’的結(jié)構(gòu)與對(duì)象類型的結(jié)構(gòu)相同可以在創(chuàng)建的‘表’中增加完整性約束定義2023/2/20DatabasePrinciples&Programming121&2)使用已有的對(duì)象類型來創(chuàng)建新的類型或表CREATETYPEperson_tASOBJECT(ssnoint,pnamename_t,ageint);Example4.2.3:創(chuàng)建新的數(shù)據(jù)類型2023/2/20DatabasePrinciples&Programming13類型的嵌套定義關(guān)系1&2)使用已有的對(duì)象類型來創(chuàng)建新的類型或表CREATETYPEperson_tASOBJECT(ssnoint,pnamename_t,ageint);Exp.4.2.3:用類型name_t創(chuàng)建新的類型person_tCREATETYPE

name_t

ASOBJECT(lnamevarchar(30),fnamevarchar(30),michar(1)

);2023/2/20DatabasePrinciples&Programming14CREATETABLEteachers(tidint,tnamename_t,roomint);Example4.2.2:定義表中的屬性1&2)使用已有的對(duì)象類型來創(chuàng)建新的類型或表2023/2/20DatabasePrinciples&Programming153)使用對(duì)象數(shù)據(jù)類型來直接創(chuàng)建一張表

CREATETABLEtablenameOFtypename{(constraint-define)};Example4.2.4CREATETABLEpeopleOFperson_t(PRIMARYKEY(ssno));2023/2/20DatabasePrinciples&Programming16ssnopnameagelnamefnamemi123550123MarchJacquelineE23245882134DelaneyPatrickX59023894455SanchezJoseF30Figure4.2ORACLEObjectTablepeopleofExample4.2.4AttributeswithintablepeopleAttributeswithinpname2023/2/20DatabasePrinciples&Programming171.ObjectTypes1.1定義新的數(shù)據(jù)類型(對(duì)象類型)1.2對(duì)象類型的使用方法1.3對(duì)象值的創(chuàng)建、查詢與更新1.4對(duì)象的引用類型2023/2/20DatabasePrinciples&Programming181.3對(duì)象值的創(chuàng)建、查詢與更新1.3.1對(duì)象值的創(chuàng)建構(gòu)造函數(shù)1.3.2對(duì)象值的查詢1.3.3對(duì)象值的更新2023/2/20DatabasePrinciples&Programming191.3.1對(duì)象值的創(chuàng)建兩個(gè)重要的函數(shù)對(duì)象構(gòu)造函數(shù)typename(argument,……)返回對(duì)象取值的函數(shù)value(…)例:name_t(‘Einstein’,‘Albert’,‘E’)

2023/2/20DatabasePrinciples&Programming201.3.1對(duì)象值的創(chuàng)建使用對(duì)象類型name_t來定義teachers表中的屬性tname當(dāng)我們使用插入操作在表teachers中插入新的元組時(shí),會(huì)自動(dòng)生成類型為name_t的對(duì)象值

Insertintoteachersvalues(1234,name_t(‘Einstein’,‘Albert’,‘E’),120);對(duì)象構(gòu)造函數(shù)2023/2/20DatabasePrinciples&Programming211.3.2對(duì)象值的查詢例1:查詢位于123號(hào)房間的教師的編號(hào)(普通查詢)Selectt.tidFromteacherstWheret.room=123CREATETYPE

name_t

ASOBJECT(lnamevarchar(30),fnamevarchar(30),michar(1));CREATETABLEteachers(tidint,tnamename_t,roomint);2023/2/20DatabasePrinciples&Programming221.3.2對(duì)象值的查詢例2:查詢位于123號(hào)房間的教師的編號(hào)和姓名(firstname&lastname)Selectt.tid,t.tname.fname,t.tname.lnameFromteacherstWheret.room=123CREATETYPE

name_t

ASOBJECT(lnamevarchar(30),fnamevarchar(30),michar(1));CREATETABLEteachers(tidint,tnamename_t,roomint);2023/2/20DatabasePrinciples&Programming231.3.3對(duì)象值的更新可以修改整個(gè)對(duì)象值當(dāng)表中的一個(gè)屬性的值域是對(duì)象類型時(shí),可以用‘對(duì)象’值直接對(duì)該屬性進(jìn)行賦值如果一張‘表’是基于‘對(duì)象類型’創(chuàng)建的,那么可以用‘對(duì)象’值直接修改整個(gè)元組可以修改對(duì)象中成員屬性的值2023/2/20DatabasePrinciples&Programming24例1:修改元組中的對(duì)象屬性值updatepeoplepsetp.pname=

name_t('Gould','Ben',null)wheressno=321341223;pname是類型為name_t的屬性CREATETYPEperson_t

ASOBJECT(ssnoint,pnamename_t,ageint);CREATETABLEpeople

OFperson_t(PRIMARYKEY(ssno));2023/2/20DatabasePrinciples&Programming25CREATETYPEperson_t

ASOBJECT(ssnoint,pnamename_t,ageint);CREATETABLEpeople

OFperson_t(PRIMARYKEY(ssno));例2:修改某個(gè)對(duì)象屬性值的成員屬性u(píng)pdatepeoplepsetp.pname.mi=‘C’wheressno=321341223;2023/2/20DatabasePrinciples&Programming26例3:修改整個(gè)元組updatepeoplepsetp=person_t(332341223,name_t('Gould','Glen','A'),55)wheressno=321341223;CREATETYPEperson_t

ASOBJECT(ssnoint,pnamename_t,ageint);CREATETABLEpeople

OFperson_t(PRIMARYKEY(ssno));people是一個(gè)用對(duì)象類型person_t直接創(chuàng)建的表2023/2/20DatabasePrinciples&Programming271.ObjectTypes1.1定義新的數(shù)據(jù)類型(對(duì)象類型)1.2對(duì)象類型的使用方法1.3對(duì)象值的創(chuàng)建、查詢與更新1.4對(duì)象的引用類型2023/2/20DatabasePrinciples&Programming28DefinitionoftheREFObjectReference對(duì)象的引用類型:REF<objecttype>是指向某個(gè)元組對(duì)象的指針類型可用于實(shí)現(xiàn)對(duì)象類型之間的嵌套引用在使用含有REF類型的對(duì)象類型(ObjectType)來創(chuàng)建關(guān)系表時(shí),必須使用Scopefor子句來限制REF屬性的取值范圍。2023/2/20DatabasePrinciples&Programming291.4對(duì)象的引用類型1.4.1定義類型之間的引用關(guān)系1.4.2創(chuàng)建含有引用類型的關(guān)系表1.4.3引用關(guān)系查詢1.4.4函數(shù)與謂詞1.4.5類型的循環(huán)嵌套定義1.4.6其它約束1.4.1定義類型之間的引用關(guān)系定義方法先定義一個(gè)對(duì)象類型X然后再定義一個(gè)對(duì)象類型Y,在類型Y中含有對(duì)類型X的引用屬性(REF屬性),從而構(gòu)成了類型X與類型Y之間的引用關(guān)系。其中:X

被稱為‘基本對(duì)象類型’(被引用類型)Y

被稱為‘引用類型’2023/2/20DatabasePrinciples&Programming302023/2/20DatabasePrinciples&Programming311.4.1定義類型之間的引用關(guān)系定義基本的對(duì)象類型:customer_t,agents_t,products_tcreatetypecustomer_tasobject(cidchar(4),cnamevarchar(13),cityvarchar(20),discntreal);2023/2/20DatabasePrinciples&Programming321.4.1定義類型之間的引用關(guān)系createtypeagent_tasobject(aidchar(3),anamevarchar(13),cityvarchar(20),percentsmallint);2023/2/20DatabasePrinciples&Programming331.4.1定義類型之間的引用關(guān)系createtypeproduct_tasobject(pidchar(3),pnamevarchar(13),cityvarchar(20),quantityinteger,pricedoubleprecision);2023/2/20DatabasePrinciples&Programming341.4.1定義類型之間的引用關(guān)系定義類型之間的引用關(guān)系createtypeorder_tasobject(ordnoint,monthchar(3),cidchar(4),aidchar(3),pidchar(3),qtyint,dollarsdoubleprecision,ordcustrefcustomer_t,ordagentrefagent_t,ordprodrefproduct_t);新增三個(gè)REF屬性,分別引用(指向)三個(gè)不同的元組對(duì)象(RowObject)1.4.2創(chuàng)建含有引用類型的關(guān)系表基本方法如下:先使用基本對(duì)象類型創(chuàng)建相應(yīng)的基本關(guān)系表再使用含有REF屬性的引用類型創(chuàng)建對(duì)應(yīng)的關(guān)系表2023/2/20DatabasePrinciples&Programming352023/2/20DatabasePrinciples&Programming361.4.2創(chuàng)建含有引用類型的關(guān)系表使用前面定義的對(duì)象類型創(chuàng)建基本關(guān)系表createtablecustomersofcustomer_t(primarykey(cid));createtableproductsofproduct_t(primarykey(pid));createtableagentsofagent_t(primarykey(aid));2023/2/20DatabasePrinciples&Programming371.4.2創(chuàng)建含有引用類型的關(guān)系表創(chuàng)建含有引用類型的關(guān)系表createtableordersoforder_t(primarykey(ordno),scopefor(ordcust)iscustomers,scopefor(ordagent)isagents,scopefor(ordprod)isproducts);Scopefor子句用于限制REF屬性的取值范圍為什么要有Scopefor子句?2023/2/20DatabasePrinciples&Programming38createtableordersoforder_t(……,scopefor(ordcust)iscustomers,scopefor(ordagent)isagents,scopefor(ordprod)isproducts);createtypecustomer_t

asobject(……);createtypeagent_t

asobject(……);createtypeproduct_t

asobject(……);createtypeorder_t

asobject(……ordcustrefcustomer_t,ordagentrefagent_t,ordprodrefproduct_t);createtablecustomersofcustomer_t(……);createtableproductsofproduct_t(……);createtableagentsofagent_t(……);2023/2/20DatabasePrinciples&Programming391.4.3引用關(guān)系查詢根據(jù)元組之間的REF引用關(guān)系進(jìn)行查詢selectdistinctame,o.ordagent.anamefromorderso selecto.ordno,amefromordersowhereo.dollars>200.002023/2/20DatabasePrinciples&Programming401.4.3引用關(guān)系查詢Example4.2.11:findpidvaluesofproductsthathavebeenorderedbyatleasttwocustomers.selectdistinctx1.pidfromordersx1,ordersx2wherex1.pid=x2.pidandx1.ordcust<x2.ordcustselectdistinctx1.pidfromordersx1,ordersx2wherex1.pid=x2.pidandx1.cid<x2.cidordcust承擔(dān)起了cid所擔(dān)負(fù)的責(zé)任!2023/2/20DatabasePrinciples&Programming411.4.4函數(shù)與謂詞兩個(gè)函數(shù)獲取對(duì)象(元組)的引用指針:REF(……)返回引用指針?biāo)赶驅(qū)ο蟮闹?DEREF(……)兩個(gè)謂詞ISDANGLINGISNULL2023/2/20DatabasePrinciples&Programming421.4.4函數(shù)與謂詞函數(shù)REF(…):返回對(duì)象的引用指針selectamefromcustomerscwherenotexists(select*fromordersxwherex.ordcust=ref(c)andx.aid='a05‘);Example4.2.12:retrieveallcustomernameswherethecustomerdoesnotplaceanorderthroughagenta05.2023/2/20DatabasePrinciples&Programming431.4.4函數(shù)與謂詞Example4.2.13selectc.cidfromcustomerscwherenotexists(select*fromagentsawherea.city='NewYork'andnotexists(select*fromordersxwherex.ordcust=ref(c)andx.ordagent=ref(a)));2023/2/20DatabasePrinciples&Programming441.4.4函數(shù)與謂詞DEREF函數(shù)檢索整個(gè)被引用對(duì)象,而不是僅僅獲得該對(duì)象的引用指針selectvalue(p),deref(p.partner)frompolice_officerspcreatetypepolice_officer_tasobject(pol_personperson_t,badge_numberinteger,

partnerrefpolice_officer_t);2023/2/20DatabasePrinciples&Programming451.4.4函數(shù)與謂詞有關(guān)REF屬性的判定謂詞:ISDANGLING用于判斷所引用的元組對(duì)象是否存在如果所引用的元組對(duì)象不存在,那么該謂詞返回邏輯真(TRUE),否則返回邏輯假(FALSE)。該謂詞主要用于檢查那些錯(cuò)誤的對(duì)象引用指針。2023/2/20DatabasePrinciples&Programming461.4.4函數(shù)與謂詞example4.2.14selecto.cidfromordersowhereo.ordcustISDANGLING;selecto.cidfromordersowhereo.ordcust<>(selectref(c)fromcustomerscwherec.cid=o.cid)等價(jià)于:2023/2/20DatabasePrinciples&Programming471.4.4函數(shù)與謂詞也可以使用ISNULL謂詞來查找取值為空(NULL)的REF屬性但是,‘isdangling’不等于‘isnull’對(duì)象引用指針的使用規(guī)則AdanglingREFisnon-nullbutuseless.Ifo.ordcustisnullordangling,thenameisnull.2023/2/20DatabasePrinciples&Programming481.4.5類型的循環(huán)嵌套定義對(duì)象類型(objecttype)不能嵌套定義,但REF

關(guān)系可以實(shí)現(xiàn)嵌套引用。createtypepolice_officer_tasobject(pol_personperson_t,badge_numberinteger,partnerrefpolice_officer_t);2023/2/20DatabasePrinciples&Programming49createtypepolice_officer_tasobject(pol_personperson_t,badge_numberinteger,

partnerref

police_officer_t

);可以使用對(duì)象類型police_officer_t

來創(chuàng)建表createtablepolice_officersofpolice_officer_t(primarykey(badge_number),scopefor(partner)ispolice_officers);2023/2/20DatabasePrinciples&Programming50在表police_officers上的對(duì)象查詢的例子Retrievethelastnamesofallpoliceofficerswhohavepartnersoversixtyyearsofage.selectp.pol_person.pname.lnamefrompolice_officerspwherep.partner.pol_person.age>60;createtypepolice_officer_tasobject(pol_personperson_t,badge_numberinteger,

partnerref

police_officer_t

);createtablepolice_officersofpolice_officer_t(……);2023/2/20DatabasePrinciples&Programming511.4.6其它約束有關(guān)REF定義的其它約束(REFDependencies)1)兩張表之間的相互REF關(guān)系的定義2)兩個(gè)具有相互REF關(guān)系的表/類型的刪除3)REF屬性數(shù)據(jù)的加載2023/2/20DatabasePrinciples&Programming521.4.6其它約束1)兩張表之間的相互REF關(guān)系的定義首先,定義兩個(gè)具有相互REF關(guān)系的對(duì)象類型(createtype……)部分創(chuàng)建(partiallycreate)第一個(gè)對(duì)象類型(只給出類型名,沒有類型的詳細(xì)定義)詳細(xì)定義第二個(gè)對(duì)象類型(包含對(duì)第一個(gè)類型的引用屬性)再詳細(xì)定義第一個(gè)對(duì)象類型再用創(chuàng)建好的對(duì)象類型創(chuàng)建關(guān)系表2023/2/20DatabasePrinciples&Programming531.4.6其它約束2)兩個(gè)具有相互REF關(guān)系的表/類型的刪除在刪除類型(droptype)之前需要先刪除表(droptable)在刪除類型(droptype)時(shí)需要采用強(qiáng)制刪除的方式DROPTYPEtypenameFORCE;2023/2/20DatabasePrinciples&Programming541.4.6其它約束3)REF屬性數(shù)據(jù)的加載方法一:先不管REF屬性的賦值(先置為NULL),然后再使用UPDATE操作修改REF屬性上的取值updateordersosetordcust=(selectref(c)fromcustomerscwherec.cid=o.cid),ordagent=(selectref(a)fromagentsawherea.aid=o.aid),ordprod=(selectref(p)fromproductspwherep.pid=o.pid);2023/2/20DatabasePrinciples&Programming551.4.6其它約束3)REF屬性數(shù)據(jù)的加載方法二:使用帶有子查詢的插入操作insertintopolice_officersselectvalue(p),1000,ref(p0)frompeoplep,police_officersp0wherep.ssno=033224445andp0.badge_number=990;2023/2/20DatabasePrinciples&Programming562.CollectionTypesCollectiontypesallowustoputmultiplevalues(collectionsofvalues)inacolumnofanindividualrow.Oracle2.1TableTypes(NestedTables)2.2ArrayTypesContainingitemsallofthesametype–theelementtype2023/2/20DatabasePrinciples&Programming572.1TableTypesandNestedTables使用tabletype來定義表中的屬性可以實(shí)現(xiàn)多值屬性的功能CREATETYPEdependents_tASTABLEOFperson_t;創(chuàng)建一個(gè)新的表類型(tabletype)2023/2/20DatabasePrinciples&Programming582.1TableTypesandNestedTables使用類型dependents_t來定義表employees中的屬性并形成一個(gè)嵌套表(nestedtable)定義。createtableemployees(eidint,epersonperson_t,

dependentsdependents_t,primarykey(eid))nestedtabledependentsstoreasdependents_tab;2023/2/20DatabasePrinciples&Programming59執(zhí)行上述的建表命令將在數(shù)據(jù)庫中創(chuàng)建兩個(gè)關(guān)系表:employee:存放職工記錄dependents_tab:存放職工的所有家屬信息(記錄),被稱為‘嵌套表’在一條建表命令中可以定義多個(gè)nestedtable每一個(gè)table-type屬性,都需要有一個(gè)對(duì)應(yīng)的nestedtablecreatetableemployees(eidint,epersonperson_t,

dependentsdependents_t,primarykey(eid))nestedtabledependentsstoreasdependents_tab;2023/2/20DatabasePrinciples&Programming602.1TableTypesandNestedTablesNestedtable的訪問Example:Retrievethenestedtableofalldependentsofemployee101.selectdependentsfromemployeeswhereeid=101;2023/2/20DatabasePrinciples&Programming612.1TableTypesandNestedTablesNestedtable的訪問Example:Retrievetheeidsofemployeeswithmorethansixdependents.

selecteidfromemployeesewhere6<(selectcount(*)fromtable(e.dependents));轉(zhuǎn)換函數(shù):table(…)2023/2/20DatabasePrinciples&Programming62Nestedtable的訪問:

table(…)的使用

Selectcount(*)From(selecte.dependents

fromemployeesewheree.eid=101);錯(cuò)誤的使用方法:Selectcount(*)Fromtable(selecte.dependents

fromemployeese

wheree.eid=101);正確的使用方法:2023/2/20DatabasePrinciples&Programming632.1TableTypesandNestedTablesOracle數(shù)據(jù)庫沒有提供nestedtable的相等比較運(yùn)算。可以使用IN操作符來實(shí)現(xiàn)某些需要通過nestedtable進(jìn)行的查詢功能2023/2/20DatabasePrinciples&Programming642.1TableTypesandNestedTablesExample:Listeidsofemployees(thereshouldbeonlyone)withadependenthavingSocialSecurityNumber3451112222selecteidfromemployeesewhere3451112222in

(selectd.ssnofromtable(e.dependents)d);通過子查詢得到職工e的所有家屬的SSN編號(hào)的集合2023/2/20DatabasePrinciples&Programming652.1TableTypesandNestedTablesOracle提供了單個(gè)對(duì)象的相等比較功能Example:retrieveeidswithdependentsthathavenamegivenbyname_t(‘Lukas’,‘David’,‘E’)selecteidfromemployeesewherename_t(‘Lukas’,‘David’,‘E’)in

(selectd.pnamefromtable(e.dependents)d);2023/2/20DatabasePrinciples&Programming662.1TableTypesandNestedTablesOracle不支持直接對(duì)嵌套表屬性的統(tǒng)計(jì)查詢功能,即下述的統(tǒng)計(jì)查詢操作是錯(cuò)誤的:selectcount(e.dependents)fromemployeesewheree.eid=101;2023/2/20DatabasePrinciples&Programming672.1TableTypesandNestedTables表與其自身的嵌套表的聯(lián)接查詢Example:displayallemployeeidentifiersandtheirdependents’ssnovaluesselecte.eid,d.ssnofromemployeese,table(e.dependents)d2023/2/20DatabasePrinciples&Programming682.1TableTypesandNestedTables如果希望同時(shí)列出那些沒有dependents職工的信息,那么可以使用outerjoin來實(shí)現(xiàn)這樣的查詢。selecte.eid,d.ssnofromemployeese,table(e.dependents)(+)d2023/2/20DatabasePrinciples&Programming692.1TableTypesandNestedTablesNestedCursors(嵌套游標(biāo))可以使查詢結(jié)果的顯示更清楚明了selecte.eid,d.ssnoasdep_ssofromemployeese,table(e.dependents)dwhered.age<16;例如:普通的查詢操作2023/2/20DatabasePrinciples&Programming70使用nestedcursor的查詢操作selecte.eid,cursor

(

selectd.ssnoasdep_ssnofromtable(e.dependents)dwhered.age<16)dep_tabfromemployees

e;selecte.eid,d.ssnoasdep_ssofromemployeese,table(e.dependents)dwhered.age<16;普通的查詢操作2023/2/20DatabasePrinciples&Programming71可以使用nestedcursor來實(shí)現(xiàn)對(duì)嵌套表屬性的統(tǒng)計(jì)功能selecteid,

cursor(selectcount(*)fromable(e.dependents))fromemployeese;2023/2/20DatabasePrinciples&Programming72也可以使用下面的兩種方式實(shí)現(xiàn)上述的統(tǒng)計(jì)查詢功能selecteid,(selectcount(*)fromtable(e.dependents))fromemployeese;selecteid,count(*)fromemployeese,table(e.dependents)groupbyeid;2023/2/20DatabasePrinciples&Programming73selecteid,cursor(selectcount(*)fromtable(e.dependents))fromemployeese;selecteid,(selectcount(*)fromtable(e.dependents))fromemployeese;selecteid,count(*)fromemployeese,table(e.dependents)groupbyeid;Q1:Q2:Q3:2023/2/20DatabasePrinciples&Programming742.2ArrayTypesArrayTypesforVARRAYscreatetypeextensions_tasvarray(4)ofint;Typename: extensions_tElementtype: intMaximumnumber: 42023/2/20DatabasePrinciples&Programming752.2ArrayTypes使用ArrayTypes定義表中的屬性createtablephonebook(phpersonperson_t,extensionsextensions_t);可以使用table(…)將一個(gè)VARRAY屬性轉(zhuǎn)換成一張嵌套表。2023/2/20DatabasePrinciples&Programming762.2ArrayTypesNestedtable與VARRAY的比較NestedtableVARRAY成員的排列次序無序有序成員的最大數(shù)目沒有限制確定的值成員的存儲(chǔ)組織單獨(dú)的存儲(chǔ)表直接存儲(chǔ)在表中2023/2/20DatabasePrinciples&Programming772.2ArrayTypesNestedtable與VARRAY的訪問模式比較可以對(duì)嵌套表屬性執(zhí)行insert操作,或通過update操作修改其成員的取值但對(duì)于VARRAY屬性則不能執(zhí)行上述的插入或修改操作,只能通過update語句修改整個(gè)VARRAY屬性的取值2023/2/20DatabasePrinciples&Programming783.PL/SQLProcedures,UDFs,andMethodsProgramblockinPL/SQLdeclare--localvariableiinteger;totalinteger:=0;beginforiin1..100looptotal:=total+i;endloop;insertintoresult(rvalue)values(total);end;2023/2/20DatabasePrinciples&Programming793.PL/SQLProcedures,UDFs,andMethodsCreatefunctioninPL/SQLcreatefunctionsum_n(ninteger)returnintegerisiinteger;totalinteger:=0;beginforiin1..nlooptotal:=total+i;endloop;returntotal;--returnresulttoSQLor

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論