ischanc/cs475/fall2000/ch1_tr.pdf · 2000. 10. 16. · key constrain t a sup er key for a relation...

25

Upload: others

Post on 18-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

  • RelationalModel

    �Arelationschemeisa�nitesequenceofuniqueattributenames.For

    example,

    EMPLOYEES

    =

    (EMPID,

    ENAME,

    ADDRESS,

    SALARY)

    isarelationschemewithfourattributenames.

    �Adomainisasetofvalues.Witheachattributename,A,adomain,

    dom(A),isassociated.Thisdomainincludesaspecialvaluecallednull.

    Forexample,dom(EMPID)couldbethesetofallpossibleintegers

    between1000and9999andthespecialnullvalue.

    �GivenarelationschemeR

    =

    A1,

    ...,

    An,arelationronthe

    schemeRisde�nedasany�nitesubsetoftheCartesianproduct

    dom(A1)

    x

    ...

    x

    dom(An).

    1

  • �AssumingappropriatedomainsfortheEMPLOYEESrelationscheme,a

    samplerelationunderthisschemecouldbe

    {

    (1111,'Jones','111

    Ash

    St.',20000),

    (2222,'Smith','123

    Elm

    St.',25000),

    (3333,'Brown','234

    Oak

    St.',30000)

    }

    �Eachoftheelementsofarelationisalsoreferredtoasatuple.

    �Arelationaldatabasescheme,D,isa�nitesetofrelationschemes,

    fR1,

    ...,

    Rmg.

    �ArelationaldatabaseonschemeDisasetofrelations

    fr1,

    ...

    rmg

    whereeachriisarelationonthecorrespondingschemeRi.

    2

  • KeyConstraint

    �AsuperkeyforarelationschemeRisanysubset,KofRthatsatis�es

    thepropertythatineveryvalidrelationundertheschemeR,itisnot

    possibletohavetwodi�erenttupleswiththesamevaluesunderK.

    �AcandidatekeyforRisanykeyforRsuchthatnoneofitsproper

    subsetsisalsoakey.

    �TheprimarykeyforarelationschemeRisoneofthecandidatekeys

    chosenbythedesignerofthedatabase.

    �Theprimarykeyattributesarerequiredtosatisfythenot

    null

    constraint,i.e.,notuplecanhaveanullvalueundertheprimarykey

    attributes.

    3

  • ReferentialIntegrity/ForeignKeyConstraint

    �Duringthedesignofarelationaldatabase,thedesignermaycreatea

    relationschemeRwhichincludestheprimarykeyattributesofanother

    relationscheme,sayS.

    �Insuchasituation,thereferentialintegrityconstraintspeci�esthe

    conditionthatthevaluesthatappearundertheprimarykeyattributes

    inanyvalidrelationunderschemeRm

    u

    st

    alsoappearintherelation

    underschemeS.

    �TheattributesintheschemeRthatcorrespondtotheprimarykey

    attributesofschemeScollectivelyarereferredtoasaforeignkeyin

    schemeR.

    �Unliketheprimarykeyattributes,theforeignkeyattributesdonot

    havetosatisfythenot

    nullconstraint.

    4

  • NotNullconstraint

    Thisconstraintspeci�estheconditionthattuplevaluesundercertain

    attributes(speci�edtobenotnull)cannotbenull.

    Thisconditionisusuallyalwaysimposedontheprimarykeyattributes.

    InOracle,primarykeyattributesareautomaticallyconstrainedtobe

    notnull.

    Otherattributesmayalsobeconstrainedtobenotnulliftheneed

    arises.

    5

  • Gradebookdatabase

    CATALOG(CNO,ctitle)

    STUDENTS(SID,fname,lname,minit)

    COURSES(TERM,LINENO,cno,a,b,c,d)

    COMPONENTS(TERM,LINENO,COMPNAME,maxpoints,weight)

    ENROLLS(SID,TERM,LINENO)

    SCORES(SID,TERM,LINENO,COMPNAME,points)

    6

  • catalog

    CNO

    CTITLE

    csc226IntroductiontoProgrammingI

    csc227IntroductiontoProgrammingII

    csc343AssemblyProgramming

    csc481AutomataandFormalLanguages

    csc498IntroductiontoDatabaseSystems

    csc880DeductiveDatabasesandLogicProgramming

    7

  • students

    SID

    FNAME

    LNAME

    MINIT

    1111Nandita

    RajshekharK

    2222Sydney

    Corn

    A

    3333Susan

    Williams

    B

    4444Naveen

    RajshekharB

    5555Elad

    Yam

    G

    6666Lincoln

    Herring

    F

    8

  • courses

    TERM

    LINENO

    CNO

    A

    B

    C

    D

    f96

    1031

    csc22690806550

    f96

    1032

    csc22690806550

    sp97

    1031

    csc22790806550

    components

    TERM

    LINENO

    COMPNAME

    MAXPOINTSWEIGHT

    f96

    1031

    exam1

    100

    30

    f96

    1031

    quizzes

    80

    20

    f96

    1031

    �nal

    100

    50

    f96

    1032

    programs

    400

    40

    f96

    1032

    midterm

    100

    20

    f96

    1032

    �nal

    100

    40

    sp97

    1031

    paper

    100

    50

    sp97

    1031

    project

    100

    50

    9

  • enrolls

    SID

    TERM

    LINENO

    1111f96

    1031

    2222f96

    1031

    4444f96

    1031

    1111f96

    1032

    2222f96

    1032

    3333f96

    1032

    5555sp97

    1031

    6666sp97

    1031

    10

  • scores

    SID

    TERM

    LINENO

    COMPNAME

    POINTS

    1111f96

    1031

    exam1

    90

    1111f96

    1031

    quizzes

    75

    1111f96

    1031

    �nal

    95

    2222f96

    1031

    exam1

    70

    2222f96

    1031

    quizzes

    40

    2222f96

    1031

    �nal

    82

    4444f96

    1031

    exam1

    83

    4444f96

    1031

    quizzes

    71

    4444f96

    1031

    �nal

    74

    11

  • Mailorderdatabase

    EMPLOYEES(ENO,ename,zip,hdate)

    PARTS(PNO,pname,qoh,price,level)

    CUSTOMERS(CNO,cname,street,zip,phone)

    ORDERS(ONO,CNO,ENO,received,shipped)

    ODETAILS(ONO,PNO,qty)

    ZIPCODES(ZIP,city)

    12

  • employees

    ENO

    ENAME

    ZIP

    HDATE

    1000

    Jones

    6722612-DEC-95

    1001

    Smith

    6060601-JAN-92

    1002

    Brown

    5030201-SEP-94

    parts

    PNO

    PNAME

    QOH

    PRICE

    LEVEL

    10506LandBeforeTimeI

    200

    19.99

    20

    10507LandBeforeTimeII

    156

    19.99

    20

    10508LandBeforeTimeIII

    190

    19.99

    20

    10509LandBeforeTimeIV

    60

    19.99

    20

    10601SleepingBeauty

    300

    24.99

    20

    10701WhenHarryMetSally120

    19.99

    30

    10800DirtyHarry

    140

    14.99

    30

    10900Dr.Zhivago

    100

    24.99

    30

    13

  • customers

    CNO

    CNAME

    STREET

    ZIP

    PHONE

    1111

    Charles

    123MainSt.

    67226316-636-5555

    2222

    Bertram

    237AshAvenue67226316-689-5555

    3333

    Barbara

    111InwoodSt.

    60606316-111-1234

    orders

    ONO

    CNO

    ENO

    RECEIVED

    SHIPPED

    1020

    1111

    1000

    10-DEC-94

    12-DEC-94

    1021

    1111

    1000

    12-JAN-95

    15-JAN-95

    1022

    2222

    1001

    13-FEB-95

    20-FEB-95

    1023

    3333

    1000

    20-JUN-97

    null

    14

  • odetails

    ONO

    PNO

    QTY

    1020

    105061

    1020

    105071

    1020

    105082

    1020

    105093

    1021

    106014

    1022

    106011

    1022

    107011

    1023

    108001

    1023

    109001

    zipcodes

    ZIP

    CITY

    67226Wichita

    60606FortDodge

    50302KansasCity

    54444Columbia

    66002Liberal

    61111FortHays

    15

  • RelationalAlgebra-Set-theoreticoperations

    Tworelationsareunion-compatibleiftheyhavethesamenumber

    ofattributesandthedomainsofthecorrespondingattributesinthe

    tworelationsarethesame.

    Considertworelationsr

    ands

    thatareunion-compatible.

    Union:r

    [s

    =ftjt

    2r

    ort

    2sg.

    Di�erence:r

    �s

    =ftjt

    2r

    andt

    62sg

    Intersection:r

    \s

    =ftjt

    2r

    andt

    2sg

    �CartesianProduct:Letr

    ands

    beanytworelations.

    r

    �s

    =ft1:t2jt12r

    andt22sg,

    where,t1:t2istheconcatenationoftuplest1andt2toformalarger

    tuple.

    16

  • Example:Set-theoreticOperators

    r

    A

    B

    a

    b

    a

    c

    b

    d

    s

    A

    B

    a

    c

    a

    e

    r

    [s

    A

    B

    a

    b

    a

    c

    b

    d

    a

    e

    r

    �s

    A

    B

    a

    b

    b

    d

    r

    �s

    r.A

    r.B

    s.A

    s.B

    a

    b

    a

    c

    a

    b

    a

    e

    a

    c

    a

    c

    a

    c

    a

    e

    b

    d

    a

    c

    b

    d

    a

    e

    r

    \s

    A

    B

    a

    c

    17

  • Relation-theoreticoperations

    Rename:Therenameoperatortakesasinputarelationandreturnsthe

    samerelationasoutput,butunderadi�erentname.Thesymbolic

    notationfortherenameoperatoris�s (r),whereristheinputrelation

    andsisthenewname.

    Select:Symbolically,theselectoperatoriswrittenas�F(r),whereFisthe

    selectioncriterionandristheinputrelationandisde�nedasfollows:

    �F(r)=ftjt2randtsatis�esFg.

    Project:Symbolically,theprojectoperatoriswrittenas�A(r),whereAis

    asub-listoftheattributesofr,andisde�nedasfollows:

    �A(r)=ft[A]jt2rg

    wheret[A]isatupleconstructedfromtbykeepingthevaluesthat

    correspondtotheattributesinAanddiscardingothervalues.

    18

  • Relation-theoreticoperations-Continued

    NaturalJoin:Symbolically,thenaturaljoiniswrittenasr./s,wherer

    isarelationonschemeRandsisarelationonschemeS,andis

    de�nedasfollows:

    r./s=ftj(9u2r)(9v2s)(t[R]=uandt[S]=v)g

    Division:Symbolically,thedivisionoperationiswrittenasr�sandis

    de�nedasfollows:

    r�s=ftj(8u2s)(t:u2r)g

    wheret:uistheconcatenationoftupletwithtupleu.

    19

  • Example:Relation-theoreticoperations

    r

    A

    C

    D

    a

    c

    d

    a

    e

    f

    a

    g

    h

    b

    c

    d

    b

    g

    h

    c

    c

    d

    c

    e

    f

    s

    C

    D

    c

    d

    e

    f

    t

    B

    C

    D

    b

    c

    d

    b

    e

    f

    �A=0b0

    orC=0c0(r)

    A

    C

    D

    a

    c

    d

    b

    c

    d

    b

    g

    h

    c

    c

    d

    �A(r)

    Aabc

    r

    ./

    t

    A

    C

    D

    B

    a

    c

    d

    b

    a

    e

    f

    b

    b

    c

    d

    b

    c

    c

    d

    b

    c

    e

    f

    b

    r

    s

    Aac

    20

  • BasicOperations

    Basicsetofoperations:rename,select,project,cartesianproduct,

    union,di�erence.Otheroperationscanbeexpressedintermsofthese

    six.

    Intersection:

    r\

    s=r�

    (r�

    s)

    NaturalJoin:

    r./s=�R\

    S(�F(r�

    s))

    whereF

    isaselectionconditionwhichindicatesthatthetuple

    valuesunderthecommonattributesofrandsareequal.

    Division:

    r�

    s=�R�

    S(r)�

    �R�

    S((�R�

    S(r)�

    s)�

    r)

    Eventhoughrelationschemesarede�nedassequences,theyaretreated

    assetsintheseequalitiesforsimplicity.

    21

  • Queryingusingrelationalalgebra

    Gradebookdatabasequeries

    Q1GetthenamesofstudentsenrolledintheAssemblyProgramming

    classinthef96term.

    t1:=�

    CTITLE=0AssemblyProgramming

    0(catalog)

    t2:=�TERM=0f960(courses)

    t3:=t1./t2./enrolls./students

    result:=�FNAME;LNAME;MINIT(t3)

    Q2GettheSIDvaluesofstudentswhodidnotenrollinanyclassduring

    thef96term.

    �SID(students)�

    �SID(�TERM=0f960(enrolls))

    22

  • Gradebookdatabasequeriescontinued

    Q3GettheSIDvaluesofstudentswhohaveenrolledincsc226and

    csc227,

    t1:=�SID(enrolls./�CNO=0csc2260(courses))

    t2:=�SID(enrolls./�CNO=0csc2270(courses))

    result:=t1\

    t2

    Q4GettheSIDvaluesofstudentswhohaveenrolledincsc226orcsc227,

    t1:=�SID(enrolls./�CNO=0csc2260(courses))

    t2:=�SID(enrolls./�CNO=0csc2270(courses))

    result:=t1[

    t2

    Q5GettheSIDvaluesofstudentswhohaveenrolledinallthecoursesin

    thecatalog.

    �SID;CNO(courses./enrolls)�

    �CNO(catalog)

    23

  • Mailorderdatabasequeries

    Q6Getpartnamesofpartsthatcostlessthan20.00.

    �PNAME(�PRICE<2

    0

    :0

    0 (parts))

    Q7GetpairsofCNOvaluesofcustomerswhohavethethesamezipcode.

    t1:=�c1 (customers)�

    �c2 (customers)

    t2:=�c1

    :ZIP=

    c2

    :ZIP

    andc1

    :CNO<c2

    :CNO(t1)

    result:=�c1

    :CNO;c2

    :CNO(t2)

    Q8Getthenamesofcustomerswhohaveorderedpartsfromemployees

    livinginWichita.

    t1:=�ENO(employees./�CITY=

    0Wichita0(zipcodes))

    result:=�CNAME(customers./orders./t1)

    24

  • Mailorderdatabasequeriescontinued

    Q9GetCNOvaluesofcustomerswhohaveorderedpartsonlyfrom

    employeeslivinginWichita.

    t1:=�ENO(employees./�CITY6=0Wichita0(zipcodes))

    result:=�CNO(orders)�

    �CNO(orders./t1))

    Q10GetCNOvaluesofcustomerswhohaveorderedpartsfromall

    employeeslivinginWichita.

    t1:=�ENO(employees./�CITY=

    0Wichita0(zipcodes))

    result:=�CNO;ENO(orders)�

    t1

    25