0% found this document useful (0 votes)
64 views96 pages

Relational Model

This document discusses the relational model in database systems. It describes the basic structure of relations, attributes, relation schemas, relation instances, keys such as candidate keys and foreign keys. It also covers relational algebra operations like select, project, union, set difference and more.

Uploaded by

nidhidarklord
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views96 pages

Relational Model

This document discusses the relational model in database systems. It describes the basic structure of relations, attributes, relation schemas, relation instances, keys such as candidate keys and foreign keys. It also covers relational algebra operations like select, project, union, set difference and more.

Uploaded by

nidhidarklord
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter2:RelationalModel

DatabaseSystemConcepts,5thEd.
Silberschatz,KorthandSudarshan [Link]

Chapter2:RelationalModel
s StructureofRelationalDatabases s FundamentalRelationalAlgebraOperations s AdditionalRelationalAlgebraOperations s ExtendedRelationalAlgebraOperations s NullValues s ModificationoftheDatabase

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ExampleofaRelation

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

BasicStructure
s Formally,givensetsD1,D2,.Dnarelationrisasubsetof

D1xD2xxDn Thus,arelationisasetofntuples(a1,a2,,an)whereeachaiDi
s Example:If
q

customer_name={Jones,Smith,Curry,Lindsay,} /*Setofallcustomernames*/ customer_street={Main,North,Park,}/*setofallstreetnames*/ customer_city={Harrison,Rye,Pittsfield,}/*setofallcitynames*/ (Smith,North,Rye), (Curry,North,Rye), (Lindsay,Park,Pittsfield)} isarelationover customer_namexcustomer_streetxcustomer_city

q q

Thenr={(Jones,Main,Harrison),

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

AttributeTypes
s Eachattributeofarelationhasaname s Thesetofallowedvaluesforeachattributeiscalledthedomainofthe

attribute
q

s Attributevaluesare(normally)requiredtobeatomic;thatis,indivisible

[Link], butcannotbeasetofaccountnumbers

s Domainissaidtobeatomicifallitsmembersareatomic s Thespecialvaluenullisamemberofeverydomain s Thenullvaluecausescomplicationsinthedefinitionofmanyoperations


q

Weshallignoretheeffectofnullvaluesinourmainpresentationand considertheireffectlater

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

RelationSchema
s A1,A2,,Anareattributes s R=(A1,A2,,An)isarelationschema

Example: Customer_schema=(customer_name,customer_street,customer_city)
s r(R)denotesarelationrontherelationschemaR

Example: customer(Customer_schema)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

RelationInstance
s Thecurrentvalues(relationinstance)ofarelationarespecifiedbya

table

s Anelementtofrisatuple,representedbyarowinatable

customer_name customer_street Jones Smith Curry Lindsay Main North North Park customer

customer_city Harrison Rye Rye Pittsfield

attributes (orcolumns)

tuples (orrows)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

RelationsareUnordered
sOrderoftuplesisirrelevant(tuplesmaybestoredinanarbitraryorder) sExample:accountrelationwithunorderedtuples

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Database
s s

Adatabaseconsistsofmultiplerelations Informationaboutanenterpriseisbrokenupintoparts,witheachrelation storingonepartoftheinformation account:storesinformationaboutaccounts depositor:storesinformationaboutwhichcustomer ownswhichaccount customer:storesinformationaboutcustomers

Storingallinformationasasinglerelationsuchas bank(account_number,balance,customer_name,..) resultsin


q

repetitionofinformation

e.g.,iftwocustomersownanaccount(Whatgetsrepeated?) e.g.,torepresentacustomerwithoutanaccount

theneedfornullvalues

Normalizationtheory(Chapter7)dealswithhowtodesignrelationalschemas
2.<number> Silberschatz,KorthandSudarshan

DatabaseSystemConcepts5thEdition,June15,2005

ThecustomerRelation

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ThedepositorRelation

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Keys
s LetKR s KisasuperkeyofRifvaluesforKaresufficienttoidentifyauniquetupleof

eachpossiblerelationr(R)
q

bypossiblerwemeanarelationrthatcouldexistintheenterprisewe aremodeling. Example:{customer_name,customer_street}and {customer_name} arebothsuperkeysofCustomer,ifnotwocustomerscanpossiblyhave thesamename

Inreallife,anattributesuchascustomer_idwouldbeusedinsteadof customer_nametouniquelyidentifycustomers,butweomitittokeep ourexamplessmall,andinsteadassumecustomernamesareunique.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Keys(Cont.)
s KisacandidatekeyifKisminimal

Example:{customer_name}isacandidatekeyforCustomer,sinceit isasuperkeyandnosubsetofitisasuperkey. identifyingtupleswithinarelation


q

s Primarykey:acandidatekeychosenastheprincipalmeansof

Shouldchooseanattributewhosevaluenever,orveryrarely, changes. [Link],butmaychange

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ForeignKeys
s Arelationschemamayhaveanattributethatcorrespondstotheprimary

[Link].
q q

E.g.customer_nameandaccount_numberattributesofdepositorare foreignkeystocustomerandaccountrespectively. Onlyvaluesoccurringintheprimarykeyattributeofthereferenced relationmayoccurintheforeignkeyattributeofthereferencing relation.

s Schemadiagram

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

QueryLanguages
s Languageinwhichuserrequestsinformationfromthedatabase. s Categoriesoflanguages
q q

Procedural Nonprocedural,ordeclarative Relationalalgebra Tuplerelationalcalculus Domainrelationalcalculus

s Purelanguages:
q q q

s Purelanguagesformunderlyingbasisofquerylanguagesthatpeople

use.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

RelationalAlgebra
s Procedurallanguage s Sixbasicoperators
q q q q q q

select: project: union: setdifference: Cartesianproduct:x rename:

s Theoperatorstakeoneortworelationsasinputsandproduceanew

relationasaresult.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

SelectOperationExample
s Relationr

C 1 5 12

D 7 7 3

23 10

A=B^D>5(r)

C 1

D 7

23 10

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

SelectOperation
s s s

Notation:p(r) piscalledtheselectionpredicate Definedas: p(r)={t|trandp(t)} Wherepisaformulainpropositionalcalculusconsistingofterms connectedby:(and),(or),(not) Eachtermisoneof: <attribute> op <attribute>or<constant>

whereopisoneof:=,,>,.<.
s

Exampleofselection:

branch_name=Perryridge(account)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ProjectOperationExample
s Relationr:

B 10 20 30 40

C 1 1 1 2

A,C(r)

C 1 1 1 2 =

C 1 1 2

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ProjectOperation
s Notation:

A ,A ,,A (r )
1 2 k

whereA1,A2areattributenamesandrisarelationname.
s Theresultisdefinedastherelationofkcolumnsobtainedbyerasing

thecolumnsthatarenotlisted

s Duplicaterowsremovedfromresult,sincerelationsaresets s Example:Toeliminatethebranch_nameattributeofaccount

account_number,balance(account)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

UnionOperationExample
s Relationsr,s:

B 1 2 r 1

B 2 3 s

A
s rs:

B 1 2 1 3

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

UnionOperation
s Notation:rs s Definedas:

rs={t|trorts}
s Forrstobevalid.

1.r,smusthavethesamearity(samenumberofattributes) [Link](example:2ndcolumn ofrdealswiththesametypeofvaluesasdoesthe2nd columnofs)


s Example:tofindallcustomerswitheitheranaccountoraloan

customer_name(depositor)customer_name(borrower)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

SetDifferenceOperationExample
s Relationsr,s:

B 1 2 1 r

B 2 3 s

s rs:

B 1 1

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

SetDifferenceOperation
s Notationrs s Definedas:

rs={t|trandts}
s Setdifferencesmustbetakenbetweencompatible

relations.
q q

randsmusthavethesamearity attributedomainsofrandsmustbecompatible

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

CartesianProductOperationExample
s Relationsr,s: A B 1 r 2 C D 10 10 20 10 s A B 1 1 1 1 2 2 2 2 C D 10 10 20 10 10 10 20 10 E a a b b a a b b
Silberschatz,KorthandSudarshan

E a a b b


s rxs:


DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

CartesianProductOperation
s Notationrxs s Definedas:

rxs={tq|trandqs}
s Assumethatattributesofr(R)ands(S)aredisjoint.(Thatis,RS=). s Ifattributesofr(R)ands(S)arenotdisjoint,thenrenamingmustbe

used.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

CompositionofOperations
s Canbuildexpressionsusingmultipleoperations s Example:A=C(rxs) s rxs

B 1 1 1 1 2 2 2 2 B 1 2 2

D 10 10 20 10 10 10 20 10 D

E a a b b a a b b E a a b
2.<number> Silberschatz,KorthandSudarshan


s A=C(rxs)

10 10 20

DatabaseSystemConcepts5thEdition,June15,2005

RenameOperation
s Allowsustoname,andthereforetoreferto,theresultsofrelational

algebraexpressions.

s Allowsustorefertoarelationbymorethanonename. s Example:

x(E)
returnstheexpressionEunderthenameX

s IfarelationalalgebraexpressionEhasarityn,then

x ( A1 , A2 ,..., An )

(E )

returnstheresultofexpressionEunderthenameX,andwiththe attributesrenamedtoA1,A2,.,An.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

BankingExample
branch(branch_name,branch_city,assets) customer(customer_name,customer_street,customer_city) account(account_number,branch_name,balance) loan(loan_number,branch_name,amount) depositor(customer_name,account_number) borrower(customer_name,loan_number)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ExampleQueries
s Findallloansofover$1200

amount>1200(loan)

s Findtheloannumberforeachloanofanamountgreaterthan

$1200

loan_number(amount>1200(loan))
s Findthenamesofallcustomerswhohavealoan,anaccount,orboth,

fromthebank

customer_name(borrower)customer_name(depositor)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ExampleQueries
s FindthenamesofallcustomerswhohavealoanatthePerryridge

branch.

customer_name(branch_name=Perryridge (borrower.loan_number=loan.loan_number(borrowerxloan)))
sFindthenamesofallcustomerswhohavealoanatthe

Perryridgebranchbutdonothaveanaccountatanybranchof thebank.

customer_name(branch_name=Perryridge (borrower.loan_number=loan.loan_number(borrowerxloan))) customer_name(depositor)


DatabaseSystemConcepts5thEdition,June15,2005 2.<number> Silberschatz,KorthandSudarshan

ExampleQueries
s FindthenamesofallcustomerswhohavealoanatthePerryridgebranch.

Query1

customer_name(branch_name=Perryridge(

borrower.loan_number=loan.loan_number(borrowerxloan)))
q

Query2

customer_name(loan.loan_number=borrower.loan_number(

(branch_name=Perryridge(loan))xborrower))

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ExampleQueries
s

Findthelargestaccountbalance
q

Strategy:

Findthosebalancesthatarenotthelargest Renameaccountrelationasdsothatwecancompareeach accountbalancewithallothers

Usesetdifferencetofindthoseaccountbalancesthatwerenotfound intheearlierstep.

Thequeryis:

balance(account)[Link] ([Link]<[Link](accountxd(account)))

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

FormalDefinition
s Abasicexpressionintherelationalalgebraconsistsofeitheroneofthe

following:
q q

Arelationinthedatabase Aconstantrelation

s LetE1andE2berelationalalgebraexpressions;thefollowingareall

relationalalgebraexpressions:
q q q q q

E1E2 E1E2 E1xE2

p(E1),PisapredicateonattributesinE1
s(E1),SisalistconsistingofsomeoftheattributesinE1

x 1 DatabaseSystemConcepts5thEdition,June15,2005

(E ),xisthenewnamefortheresultofE1 2.<number>

Silberschatz,KorthandSudarshan

AdditionalOperations
Wedefineadditionaloperationsthatdonotaddanypowertothe relationalalgebra,butthatsimplifycommonqueries.
s Setintersection s Naturaljoin s Division s Assignment

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

SetIntersectionOperation
s Notation:rs s Definedas: s rs={t|trandts} s Assume:
q q

r,shavethesamearity attributesofrandsarecompatible

s Note:rs=r(rs)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

SetIntersectionOperationExample
s Relationr,s:

AB r 1 2 1

AB s 2 3

s rs

AB 2

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

NaturalJoinOperation
sNotation:rs s LetrandsberelationsonschemasRandSrespectively.
q q

Then,rsisarelationonschemaRSobtainedasfollows: Considereachpairoftuplestrfromrandtsfroms. IftrandtshavethesamevalueoneachoftheattributesinRS,adda tuplettotheresult,where

thasthesamevalueastronr thasthesamevalueastsons

s Example:

R=(A,B,C,D) S=(E,B,D)
q q

Resultschema=(A,B,C,D,E) rsisdefinedas:
2.<number> Silberschatz,KorthandSudarshan

DatabaseSystemConcepts5thEdition,June15,2005

NaturalJoinOperationExample
s Relationsr,s:

B 1 2 4 1 2

D a a b a b

B 1 3 1 2 3

D a a a b b s


s rs

B 1 1 1 1 2

D a a a a b


DatabaseSystemConcepts5thEdition,June15,2005


Silberschatz,KorthandSudarshan

2.<number>

DivisionOperation
s Notation: s Suitedtoqueriesthatincludethephraseforall. s LetrandsberelationsonschemasRandSrespectively

rs

where
q q

R=(A1,,Am,B1,,Bn) S=(B1,,Bn)

Theresultofrsisarelationonschema RS=(A1,,Am) rs={t|tRS(r)us(tur)} Wheretumeanstheconcatenationoftuplestanduto produceasingletuple

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

DivisionOperationExample
s Relationsr,s: A B 1 2 3 1 1 1 3 4 6 1 2 r B 1 2 s

s rs:


DatabaseSystemConcepts5thEdition,June15,2005 2.<number> Silberschatz,KorthandSudarshan

AnotherDivisionExample
s Relationsr,s: A B a a a a a a a a C D a a b a b a b b E 1 1 1 1 3 1 1 1 D a b E 1 1


s rs:

r
A

B a a

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

DivisionOperation(Cont.)
s

Property
q q

Letq=rs Thenqisthelargestrelationsatisfyingqxsr

Definitionintermsofthebasicalgebraoperation Letr(R)ands(S)berelations,andletSR rs=RS(r)RS((RS(r)xs)RS,S(r)) Toseewhy


q

RS,S(r)simplyreordersattributesofr RS(RS(r)xs)RS,S(r))givesthosetuplestin RS(r)suchthatforsometupleus,tur.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

AssignmentOperation
s

Theassignmentoperation()providesaconvenientwaytoexpress complexqueries.
q

Writequeryasasequentialprogramconsistingof

aseriesofassignments followedbyanexpressionwhosevalueisdisplayedasaresultof thequery.

Assignmentmustalwaysbemadetoatemporaryrelationvariable. temp1RS(r) temp2RS((temp1xs)RS,S(r)) result=temp1temp2 Theresulttotherightoftheisassignedtotherelationvariableon theleftofthe. Mayusevariableinsubsequentexpressions.

Example:Writersas

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

BankExampleQueries
s Findthenamesofallcustomerswhohavealoanandanaccountat

bank.

customer_name(borrower)customer_name(depositor)

s Findthenameofallcustomerswhohavealoanatthebankandthe

loanamount

customer_name,loan_number,amount(borrowerloan)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

BankExampleQueries
s FindallcustomerswhohaveanaccountfromatleasttheDowntown

andtheUptownbranches. q Query1

customer_name(branch_name=Downtown(depositoraccount)) customer_name(branch_name=Uptown(depositoraccount))
q

Query2 customer_name,branch_name(depositoraccount) temp(branch_name)({(Downtown),(Uptown)})

NotethatQuery2usesaconstantrelation.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

BankExampleQueries
s Findallcustomerswhohaveanaccountatallbrancheslocatedin

Brooklyncity.

customer_name,branch_name(depositoraccount) branch_name(branch_city=Brooklyn(branch))

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ExtendedRelationalAlgebraOperations
s GeneralizedProjection s AggregateFunctions s OuterJoin

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

GeneralizedProjection
s Extendstheprojectionoperationbyallowingarithmeticfunctionstobe

usedintheprojectionlist.

F ,F ,..., F (E )
1 2 n

s Eisanyrelationalalgebraexpression s EachofF1,F2,,Fnarearearithmeticexpressionsinvolvingconstants

andattributesintheschemaofE.

s Givenrelationcredit_info(customer_name,limit,credit_balance),find

howmuchmoreeachpersoncanspend:

customer_name,limitcredit_balance(credit_info)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

AggregateFunctionsandOperations
s Aggregationfunctiontakesacollectionofvaluesandreturnsasingle

valueasaresult.

avg:averagevalue min:minimumvalue max:maximumvalue sum:sumofvalues count:numberofvalues


s Aggregateoperationinrelationalalgebra
G1,G2 ,,Gn

F ( A ),F ( A ,,F ( A ) (E )
1 1 2 2 n n

Eisanyrelationalalgebraexpression
q q q

G1,G2,Gnisalistofattributesonwhichtogroup(canbeempty) EachFiisanaggregatefunction EachAiisanattributename

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

AggregateOperationExample
s Relationr:

C 7 7 3 10

s gsum(c)(r)

sum(c) 27

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

AggregateOperationExample
s Relationaccountgroupedbybranchname:

branch_name account_number Perryridge Perryridge Brighton Brighton Redwood A102 A201 A217 A215 A222

balance 400 900 750 750 700

branch_nameg sum(balance)(account)

branch_name Perryridge Brighton Redwood

sum(balance) 1300 1500 700

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

AggregateFunctions(Cont.)
s Resultofaggregationdoesnothaveaname
q q

Canuserenameoperationtogiveitaname Forconvenience,wepermitrenamingaspartofaggregate operation


branch_nameg sum(balance)assum_balance(account)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

OuterJoin
s Anextensionofthejoinoperationthatavoidslossofinformation. s Computesthejoinandthenaddstuplesformonerelationthatdoesnot

matchtuplesintheotherrelationtotheresultofthejoin.
q
q

s Usesnullvalues:

nullsignifiesthatthevalueisunknownordoesnotexist
Allcomparisonsinvolvingnullare(roughlyspeaking)falseby definition.

Weshallstudyprecisemeaningofcomparisonswithnullslater

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

OuterJoinExample
s Relationloan

loan_number branch_name L170 L230 L260


s Relationborrower

amount 3000 4000 1700

Downtown Redwood Perryridge

customer_name loan_number Jones Smith Hayes L170 L230 L155

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

OuterJoinExample
s Join

loanborrower loan_number L170 L230


sLeftOuterJoin

branch_name Downtown Redwood

amount customer_name 3000 4000 Jones Smith

loanborrower loan_number L170 L230 L260 branch_name Downtown Redwood Perryridge amount customer_name 3000 4000 1700 Jones Smith null

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

OuterJoinExample
sRightOuterJoin

loanborrower loan_number L170 L230 L155


sFullOuterJoin

branch_name Downtown Redwood null

amount customer_name 3000 4000 null Jones Smith Hayes

loanborrower loan_number L170 L230 L260 L155 branch_name Downtown Redwood Perryridge null amount customer_name 3000 4000 1700 null Jones Smith null Hayes

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

NullValues
s Itispossiblefortuplestohaveanullvalue,denotedbynull,forsome

oftheirattributes

s nullsignifiesanunknownvalueorthatavaluedoesnotexist. s Theresultofanyarithmeticexpressioninvolvingnullisnull. s Aggregatefunctionssimplyignorenullvalues(asinSQL) s Forduplicateeliminationandgrouping,nullistreatedlikeanyother

value,andtwonullsareassumedtobethesame(asinSQL)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

NullValues
s Comparisonswithnullvaluesreturnthespecialtruthvalue:unknown
q

Iffalsewasusedinsteadofunknown,thennot(A<5) wouldnotbeequivalenttoA>=5 OR:(unknownortrue)=true, (unknownorfalse)=unknown (unknownorunknown)=unknown AND:(trueandunknown)=unknown, (falseandunknown)=false, (unknownandunknown)=unknown NOT:(notunknown)=unknown InSQLPisunknownevaluatestotrueifpredicatePevaluatesto unknown

s Threevaluedlogicusingthetruthvalueunknown:
q

q q

s Resultofselectpredicateistreatedasfalseifitevaluatestounknown

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

ModificationoftheDatabase
s Thecontentofthedatabasemaybemodifiedusingthefollowing

operations:
q q q

Deletion Insertion Updating

s Alltheseoperationsareexpressedusingtheassignment

operator.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Deletion
s Adeleterequestisexpressedsimilarlytoaquery,except

insteadofdisplayingtuplestotheuser,theselectedtuplesare removedfromthedatabase. particularattributes

s Candeleteonlywholetuples;cannotdeletevaluesononly s Adeletionisexpressedinrelationalalgebraby:

rrE whererisarelationandEisarelationalalgebraquery.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

DeletionExamples
s DeleteallaccountrecordsinthePerryridgebranch.

accountaccount branch_name=Perryridge(account)
sDeleteallloanrecordswithamountintherangeof0to50

loanloan amount 0 andamount50(loan)


sDeleteallaccountsatbrancheslocatedinNeedham.

r1 branch_city=Needham(accountbranch) r2account_number,branch_name,balance(r1) r3customer_name,account_number(r2depositor) accountaccountr2 depositordepositorr3


DatabaseSystemConcepts5thEdition,June15,2005 2.<number> Silberschatz,KorthandSudarshan

Insertion
s Toinsertdataintoarelation,weeither:
q q

specifyatupletobeinserted writeaquerywhoseresultisasetoftuplestobeinserted rrE

s inrelationalalgebra,aninsertionisexpressedby:

whererisarelationandEisarelationalalgebraexpression.
s TheinsertionofasingletupleisexpressedbylettingEbeaconstant

relationcontainingonetuple.

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

InsertionExamples
s InsertinformationinthedatabasespecifyingthatSmithhas$1200in

accountA973atthePerryridgebranch.

accountaccount{(A973,Perryridge,1200)} depositordepositor{(Smith,A973)}
sProvideasagiftforallloancustomersinthePerryridge

branch,a$[Link] astheaccountnumberforthenewsavingsaccount.

r1(branch_name=Perryridge(borrowerloan)) accountaccountloan_number,branch_name,200(r1) depositordepositorcustomer_name,loan_number(r1)

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Updating
s Amechanismtochangeavalueinatuplewithoutchargingallvaluesin

thetuple

s Usethegeneralizedprojectionoperatortodothistask

r F1,F2 ,,Fl , ( r )
s EachF iseither i
q q

theIthattributeofr,iftheIthattributeisnotupdated,or, iftheattributeistobeupdatedFiisanexpression,involvingonly constantsandtheattributesofr,whichgivesthenewvalueforthe attribute

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

UpdateExamples
s Makeinterestpaymentsbyincreasingallbalancesby5percent.

accountaccount_number,branch_name,balance*1.05(account)

sPayallaccountswithbalancesover$10,0006percentinterest

andpayallothers5percent

accountaccount_number,branch_name,balance*1.06(BAL>10000(account))

account_number,branch_name,balance*1.05(BAL10000(account))

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

EndofChapter2

DatabaseSystemConcepts,5thEd.
Silberschatz,KorthandSudarshan [Link]

[Link]

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.6:Theloanrelation

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.7:Theborrowerrelation

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Resultofbranch_name=Perryridge(loan) Resultof

Figure2.9

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.10: Loannumberandtheamountoftheloan

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.11:Namesofallcustomerswho haveeitheranaccountoranloan

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.12: Customerswithanaccountbutnoloan

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.13:Resultofborrower|X|loan

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.14

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.15

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.16

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.17 Largestaccountbalanceinthebank

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.18:Customerswholiveonthe samestreetandinthesamecityas Smith

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.19:Customerswithbothan accountandaloanatthebank

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.20

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.21

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.22

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.23

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.24:Thecredit_inforelation

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.25

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.26:Thept_worksrelation

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.27 Thept_worksrelationafterregrouping

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.28

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.29

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.30 Theemployeeandft_worksrelations

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.31

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.32

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.33

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

Figure2.34

DatabaseSystemConcepts5thEdition,June15,2005

2.<number>

Silberschatz,KorthandSudarshan

You might also like