清華大學(xué)數(shù)據(jù)庫原理課件第三章_第1頁
清華大學(xué)數(shù)據(jù)庫原理課件第三章_第2頁
清華大學(xué)數(shù)據(jù)庫原理課件第三章_第3頁
清華大學(xué)數(shù)據(jù)庫原理課件第三章_第4頁
清華大學(xué)數(shù)據(jù)庫原理課件第三章_第5頁
已閱讀5頁,還剩61頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

數(shù)據(jù)庫原理王建民清華大學(xué)軟件學(xué)院2003年/秋

Chapter3

RelationalModel2/4/2023863項目答辯—清華英泰/武漢天喻Entity-RelationshipModelRequirementforawarehouseWhatwillbestoredinthewarehouse?ADBMSindependentdatabasemodelDatabaseDesignPhasesRequirementsCollection&AnalysisRequirementRequirementCollectionMethodsConceptualDesignE-RModelLogicalDesignPhysicalDesignApplicationDesignRelationalModelRelationalDBSDBMSHPLs,Tools,OSRequirementSpecificationConceptualDatabaseModelLogicalModelsPerformanceRequestsFinalSchemaApplicationRequirementRequirementSpecificationApplicationProgramsEnterpriseSchemaDatabaseSchemaConceptualModeltoLogicalModelReductionofanE-RSchematoTablesAdatabasewhichconformstoanE-RdiagramcanberepresentedbyacollectionoftablesTabularrepresentationisthebasisoftherelationalmodelConvertinganE-RdiagramtoatableformatisthebasisforderivingarelationaldatabasedesignfromanE-RdiagramRelationalModelRelationalDataStructureRelationRelationalOperations(QueryLanguages)RelationalAlgebraRelationalCalculusIntegrityConstraintsEntityIntegrityReferentialIntegrityUser-definedIntegrityMainContentRelationandRelationSchemaQueryLanguageRelationalAlgebraARelationisaTableAttributesTuplesDomainRelationDomain

thesetofpermittedvalueswiththesametypegivensetsD1,D2,….Dn,arelationrisasubsetofD1xD2x…xDnThusarelationisasetofn-tuples(a1,a2,…,an)where

ai

Di

niscalledarity(ordegree)ExampleofRelation

customer-name={Jones,Smith,Curry,Lindsay}

customer-street={Main,North,Park}

customer-city={Harrison,Rye,Pittsfield}

Thenr={(Jones,Main,Harrison),

(Smith,North,Rye),

(Curry,North,Rye),

(Lindsay,Park,Pittsfield)}

isarelationovercustomer-namexcustomer-streetxcustomer-cityTuple&TupleVariableAnelementofrelationrisatuple,representedbyarowinatableAtuplevariabletstandsforatuplet[customer-name]denotesthevalueoftonthecustomer-nameattributet[1]denotesvaluethefirstattributeoft(viz.positionalnotation)ArelationisasetoftupleswiththesametypeTuplesareUnordered

OrderoftuplesisirrelevantTuplesmaybestoredinanarbitraryorderContent-basedlocation

E.g.accountrelationwithunorderedtuplesIsanytablearelation?Attributevaluesare(normally)requiredtobeatomic,thatis,indivisibleE.g.multi-valuedattributevaluesarenotatomicE.positeattributevaluesarenotatomicNullValueThespecialvaluenull(空值)

isamemberofeverydomainmeans‘unknown’or‘doesnotexist’ThenullvaluecausescomplicationsinthedefinitionofmanyoperationsWemustdealwithitconsistentlyRelationSchemaDefinition:R(A,D,dom,F)TableheaderR(A1,A2,…,An)isarelationschema E.g.Customer-schema

(security-no,

customer-name, customer-street, customer-city)r(R)isarelation

instanceofrelationschemaR E.g. customer(Customer-schema)SuperkeyLetKRKisa

superkey

ofRifvaluesforKaresufficienttoidentifyauniquetupleofeachpossiblerelationr(R)

Example:{customer-name,customer-street}and

{customer-name}

arebothsuperkeysofCustomer,ifnotwocustomerscanpossiblyhavethesamenameCandidateKey&PrimaryKeyKisacandidatekeyifKisminimal{customer-name}isacandidatekeyforCustomer,sinceitisasuperkey(assumingnotwocustomerscanpossiblyhavethesamename),andnosubsetofitisasuperkeyAlthoughseveralcandidatekeysmayexist,oneofthecandidatekeysisselectedtobetheprimarykeyTheattributesoftheprimarykeyiscalledprimeattributes(主屬性)ExampleofKeysJonesSmithCurryLindsaycustomer-nameMainNorthNorthParkcustomer-streetHarrisonRyeRyePittsfieldcustomer-citycustomertuples11011111111110111111121101111111311011111114Security#PrimaryKeyCandidateKeySuperKeyForeignKeysBorrowerReferencingrelationForeignkeysReferencedrelationCustomerLoanRelationalDatabaseAdatabaseschemaconsistsofasetofrelationschemasAdatabaseinstanceconsistsofasetofrelationsInrelationaldatabase,informationaboutanenterpriseisbrokenupintoparts,witheachrelationstoringonepartoftheinformation

E.g.

account:storesinformationaboutaccounts

depositor:storesinformationaboutwhichcustomer ownswhichaccountcustomer:storesinformationaboutcustomersQualityofRelationSchemaStoringallinformationasasinglerelationsuchas

bank(account-number,balance,customer-name,..)

resultsintoomuchrepetitionofinformationtheneedfortoomanynullvaluesNormalizationtheory(Chapter7)dealswithhowtodesignrelationalschemasE-RDiagramofaBankingEnterpriseSchemaDiagramfortheBankingEnterpriseBasicConceptsDomain (域),Relation(關(guān)系),Attribute(屬性),

Tuple(元組)Superkey(碼),CandidateKey(候選碼),PrimaryKey(主碼),ForeignKey(外碼)RelationSchema(關(guān)系模式),

Relationinstance(關(guān)系實例),

RelationalDatabase(關(guān)系數(shù)據(jù)庫)MainContentRelationandRelationSchemaQueryLanguageRelationalAlgebraQueryLanguageAlanguageinwhichauserspecifytherequestofinformationfromthedatabaseProceduralLanguageAsequenceofoperationsRelationalalgebraNon-ProceduralLanguageDescriptionofthedesiredinformationRelationalcalculusMainContentRelationandRelationSchemaQueryLanguageRelationalAlgebraWhatisAlgebra?MathematicalsystemconsistingofOperands

variablesorvaluesfromwhichnewvaluescanbeconstructedOperators

symbolsdenotingproceduresthatconstructnewvaluesfromgivenvaluesWhatisRelationalAlgebra?AnalgebrawhoseoperandsarerelationsorvariablesthatrepresentrelationsOperatorsaredesignedtodothemostcommonoperationsthatweneedtodowithrelationsinadatabaseTheoperatorstakeoneormorerelationsasinputsandgiveanewrelationasaresultTheresultisanalgebrathatcanbeusedasaquerylanguageforrelationsBasicOperatorsSixbasicoperatorsunionsetdifferenceCartesianproductSelectProjectRenameUnion–ExampleRelationsr,s:

rs:AB121AB23rsAB1213UnionOperationNotation:r

sDefinedas

r

s={t|trorts}Forr

stobevalid r,

smusthavethesamearity

(samenumberofattributes) Theattributedomainsmustbe

compatible(e.g.,2ndcolumnofrdealswiththesametypeofvaluesasdoesthe2ndcolumnofs)SetDifference–ExampleRelationsr,s:r–s:AB121AB23rsAB11SetDifferenceOperationNotationr–sDefinedas:

r–s={t|t

r

andts}

SetdifferencesmustbetakenbetweencompatiblerelationsrandsmusthavethesamearityattributedomainsofrandsmustbecompatibleCartesian-Product-ExampleRelationsr,s:rx

s:AB12AB11112222CD1019201010102010EaabbaabbCD10102010EaabbrsCartesian-ProductOperationNotationrxsDefinedas:

rxs={tq|trandqs}Assumethatattributesofr(R)ands(S)aredisjoint.(Thatis,RS=)Ifattributesofr(R)ands(S)arenotdisjoint,thenrenamingmustbeusedSelect–ExampleRelationrABCDA=B^D>5

(r)ABCD123710SelectOperationNotation:

p(r)piscalledtheselectionpredicate(選擇謂詞)Definedas:

p(r)={t|tr

andp(t)} Wherepisaformulainpropositionalcalculus(命題邏輯)

consistingoftermsconnectedby

(and),(or),(not)Eachtermisoneof

<attribute>op<attribute>or<constant>whereopisoneof:=,,>,,<,Project–ExampleRelationr:ABC102030401112AC1112=AC112A,C(r)ProjectOperationNotation:

A1,A2,…,

Ak(r) whereA1,A2areattributenamesandrisarelationTheresultisdefinedastherelationofkcolumnsobtainedbyerasingthecolumnsthatarenotlistedDuplicaterowsremovedfromresult,sincerelationsaresetsRenameExampleAllowsustoname,andthereforetoreferto,theresultsofrelational-algebraexpressionsLetEisarelationalalgebraexpressions

X(E)returntheresultrelationofEasXrelationWhentheCartesianproductofarelationwithitselfisdesired,AllowsustorefertoarelationbymorethanonenameLetr1isrelationontherelationschemaR1(son,father)

r1.father,r2.son(

r1.son=r2.father(r1X

r2(r1)))RenameOperationNotation:

x(E) returnstheresultofexpressionEunderthenameX Ifarelational-algebraexpressionEhasarityn,then

x

(A1,A2,…,An)

(E) returnstheresultofexpressionEunderthenameX,andwiththeattributesrenamedtoA1,A2,….,AnCompositionofOperationsCanbuildexpressionsusingmultipleoperationsExample:A=C(rxs)rxsA=C(rxs)AB11112222CD

1019201010102010EaabbaabbABCDE122102020aabFormalDefinitionAbasicexpressionintherelationalalgebraconsistsofeitheroneofthefollowing:ArelationinthedatabaseAconstantrelationFormalDefinition(c1)LetE1andE2berelational-algebraexpressions;thefollowingareallrelational-algebraexpressions:(并集)E1

E2(差集)

E1-E2(笛卡爾積)E1xE2(選擇)p(E1),PisapredicateonattributesinE1(投影)s(E1),

SisalistconsistingofsomeoftheattributesinE1(換名)

x

(E1),xisthenewnamefortheresultofE1BankingExampleBranch 營業(yè)所

(branch-name,branch-city,assets)Customer 客戶

(customer-name,customer-street,customer-only)Account 存款明細(xì) (account-number,branch-name,balance)loan 貸款明細(xì) (loan-number,branch-name,amount)depositor 存款賬戶 (customer-name,account-number)borrower 貸款賬戶 (customer-name,loan-number)ExampleQueries1、Findallloansofover$1200(找出所有貸款額超過1200$的貸款)amount>1200(loan)2、Findtheloannumberforeachloanofanamountgreaterthan$1200(找出所有貸款額超過1200$的貸款號)

loan-number(amount>1200(loan))ExampleQueries3、Findthenamesofallcustomerswhohavealoan,anaccount,orboth,fromthebank(找出貸款或存款的客戶名)customer-name(borrower)customer-name(depositor)4、Findthenamesofallcustomerswhohavealoanandanaccountatbank(找出既有貸款又有存款的客戶名)

customer-name(borrower)customer-name(depositor)customer-name(borrower)—(customer-name(borrower)—customer-name(depositor))ExampleQueries5、FindthenamesofallcustomerswhohavealoanatthePerryridgebranchcustomer-name(branch-name=“Perryridge”(borrower.loan-number=loan.loan-number(borrowerXloan)))6、FindthenamesofallcustomerswhohavealoanatthePerryridgebranchbutdonothaveanaccountatanybranchofthebankcustomer-name(branch-name=“Perryridge”(borrower.loan-number=loan.loan-number(borrowerxloan)))

–customer-name(depositor)ExampleQueries7、FindthenamesofallcustomerswhohavealoanatthePerryridgebranch(找出所有在Perryidge營業(yè)所貸款的用戶名)Query1

customer-name(branch-name=“Perryridge”(borrower.loan-number=loan.loan-number(borrowerxloan)))Query2customer-name(loan.loan-number=borrower.loan-number ((branch-name=“Perryridge”(loan))xborrower))AdditionalOperationsWedefineadditionaloperationsthatdonotaddanypowertotherelationalalgebra,butthatsimplifycommonqueriesSetintersectionNaturaljoinDivisionAssignmentSet-Intersection-ExampleRelationr,s:rsAB121AB23rsAB2Set-IntersectionOperationNotation:r

sDefinedas:r

s={t|t

r

and

t

s}Assume:r,shavethesamearity

attributesofrandsarecompatibleNote:r

s=r-(r-s)NaturalJoin–ExampleRelationsr,s:AB12412CDaababB13123DaaabbErAB11112CDaaaabEsrsNatural-JoinOperationNotation:rsLetrandsberelationsonschemasRandSrespectively.TheresultisarelationonschemaR

Swhichisobtainedbyconsideringeachpairoftuples

trfromrandtsfroms.IftrandtshavethesamevalueoneachoftheattributesinR

S,atuple

tisaddedtotheresult,wherethasthesamevalueastronrthasthesamevalueastsonsNatural-JoinOperation(c1)Example:

R=(A,B,C,D)

S=(E,B,D)Resultschema=(A,B,C,D,E)r

sisdefinedas: r.A,r.B,r.C,r.D,s.E(r.B=s.Br.D=s.D(rxs))Letr(R)ands(S)havenocommonattributes,thenrs=?Division–ExampleRelationsr,s:r

s:AB12AB123111342rsDivisionOperationNotation:Suitedtoqueriesthatincludethephrase“forall”.LetrandsberelationsonschemasRandSrespectivelywhereR=(A1,…,Am,B1,…,Bn)S=(B1,…,Bn)TheresultofrsisarelationonschemaR–S=(A1,…,Am)

rs={t|tR-S(r)us(tur)}r

s

AnotherDivisionExampleABaaaaaaaaCDaabababbE11113111Relationsr,s:r

s:DabE11ABaaCrsABaaaaaCR-S(r)DefinitionwithBasicOperationsPropertyLetq=r

sThenqisthelargestrelationsatisfyingqxs

rDefinitionintermsofthebasicalgebraoperation

Letr(R)ands(S)berelations,andletS

R

rs=R-S(r)–R-S((R-S

(r)xs)–R-S,S(r))

ToseewhyR-S,S(r)simplyreordersattributesofr

R-S(R-S

(r)xs)–R-S,S(r))givesthosetuplestin

R-S

(r)suchthatforsometupleus,tu

r.InverseOperationofXRelationsr,s:rx

s:AB11112222CD101

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論