+ All Categories
Home > Documents > Lecture 10 DBMS

Lecture 10 DBMS

Date post: 03-Jun-2018
Category:
Upload: bazigh-kiani
View: 231 times
Download: 0 times
Share this document with a friend

of 32

Transcript
  • 8/12/2019 Lecture 10 DBMS

    1/32

    Database Systems

  • 8/12/2019 Lecture 10 DBMS

    2/32

    The Relational Model of Data

    The term relation is basically just a mathematical

    term for a table.

    DBMS products that are based on the relationalmodel of datahave come to dominate the

    database marketplace.

    The introduction of the relational model by E. F.

    Codd in 1969-70 was the most important event

    in the entire history of the database field.

  • 8/12/2019 Lecture 10 DBMS

    3/32

    The Relational Model -

    cont inued C. J. Date was the first to recognize the significance

    of the relational model and has been the mostrecognized author, lecturer, researcher, and

    consultant of the relational technology He is best known for his book An Introduction to

    Database Systems (seventh edition published in2000), which has sold nearly 650,000 copies and is

    used by several hundred colleges and universitiesworldwide.

  • 8/12/2019 Lecture 10 DBMS

    4/32

    Terminology

    S#:S# SNAME:NAME STATUS:STATUS CITY:CITY

    S1

    S2

    S3

    S4

    S5

    Smith

    Jones

    Blake

    Clark

    Adams

    20

    10

    30

    20

    30

    London

    Paris

    Paris

    London

    Athens

    Relatio

    n

    Tuple

    Attributes

    Primary

    Key

    London,

    Tucson,

    Paris, ...

    S# NAME STATUS CITY

    D

    omains

  • 8/12/2019 Lecture 10 DBMS

    5/32

    Relational Data Objects:

    Domains and Relations

    A relationis table

    A tuple corresponds to a row of such a tableand attribute to a column

    The number of tuples is called the

    cardinality and number of attributes arecalleddegree

  • 8/12/2019 Lecture 10 DBMS

    6/32

    Domain

    A domain is a pool of values, from which specific

    attributes of specific relations draw their actual

    values.

    Synonym of data type(Typefor short). Can be system-defined, e.g., text, number, currency,

    yes/no, date/time, hyperlink, autonumber.

    Or user-defined, e.g., POINT.

    For example, the domain labeled S# is the set of alllegal supplier numbers, and the set of S# values

    appearing in relation S at any given time is

    constrained to be some subset of that set.

  • 8/12/2019 Lecture 10 DBMS

    7/32

    Terminologies Used:

    Formal Informal

    Relation table

    Tuple row or record

    Cardinality number of rows

    Attribute column or field

    Degree number of columns

    Domain pool of legal values

  • 8/12/2019 Lecture 10 DBMS

    8/32

    Tables

    Relational database is a collection of tables

    Heading: table name and column names

    Body: rows, occurrences of data

    StdSSN StdLastName StdMajor StdClass StdGPA

    123-45-6789 WELLS IS FR 3.00

    124-56-7890 NORBERT FIN JR 2.70

    234-56-7890 KENDALL ACCT JR 3.50

    Student

  • 8/12/2019 Lecture 10 DBMS

    9/32

    Integrity (Fig. from C.J. Date)

    S# SNAME STATUS CITY

    S1

    S2

    S3

    S4

    S5

    Smith

    Jones

    Blake

    Clark

    Adams

    20

    10

    30

    20

    30

    London

    Paris

    Paris

    London

    Athens

    P# PNAME COLOR WEIGHT

    P1P2

    P3

    P4

    P5

    P6

    NutBolt

    Screw

    Screw

    Cam

    Cog

    RedGreen

    Blue

    Red

    Blue

    Red

    1217

    17

    14

    12

    19

    CITY

    LondonParis

    Rome

    London

    Paris

    London

    S#

    S1

    S1

    S1

    S1

    S1

    S1

    S2

    S2

    S3S4

    S4

    S4

    P#

    P1

    P2

    P3

    P4

    P5

    P6

    P1

    P2

    P2P2

    P4

    P5

    QTY

    300

    200

    400

    200

    100

    100

    300

    400

    200200

    300

    400

    S

    P

    SP

  • 8/12/2019 Lecture 10 DBMS

    10/32

    Integrity

    The termintegrity refers to the accuracyand

    correctness of data in the database.

    The DBMS should automatically enforce

    specified integrity constraints.

    Entity integrity: primary keys

    Each table has column(s) with unique values

    Ensures entities are traceable Referential integrity: foreign keys

    Values of a column in one table match values from a

    source table

    Ensures valid references among tables

  • 8/12/2019 Lecture 10 DBMS

    11/32

    Integrity (Cont)

    The term integrityrefers to the accuracyorcorrectnessof data in the database.

    Integrity constraints guard against accidentaldamage to the database, by ensuring thatauthorized changes to the database do not resultin a loss of data consistency.

    A given database might be subject to any number

    of integrity constraints of arbitrary complexity. The DBMS needs to be informed of such

    constraints, needs to enforce them somehow.

    Basically by rejecting any update that would

    otherwise violate them.

  • 8/12/2019 Lecture 10 DBMS

    12/32

    Integrity (Cont)

    When a new constrain is declared,

    The system must first make sure the database currently

    satisfies it;

    If not, the new constraint is rejected; Otherwise it is accepted and enforced from that point

    forward.

    We can get rid of existing constraints.

  • 8/12/2019 Lecture 10 DBMS

    13/32

    Types of Constraints

    Type (domain) Constraint:

    specifies the legal values for a given type.

    Attribute constraint:

    specifies the legal values for a given attribute.

    Table (relation) constraint:

    specifies the legal values for a given table.

    Multiple attributes may be involved.

    Database constraint:

    specifies the legal values for a given database.

    Multiple tables may be involved.

  • 8/12/2019 Lecture 10 DBMS

    14/32

    Type or Domain Constraints

    Type constraints are the most elementary

    form of integrity constraint.

    A type constraints is an enumeration of the

    legal value of the type, a specification of the

    values that make up the type in question.

    Type constraints are checked immediately.

  • 8/12/2019 Lecture 10 DBMS

    15/32

    Type or Domain Constraints

    (Cont.)

    The checkclause in SQL permits domains to be

    restricted:

    Use checkclause to ensure that an hourly-wage

    domain allows only values greater than a specifiedvalue.

    create domainhourly-wage numeric(5,2)

    constraintvalue-test check(value > = 4.00)

    The domain hourly-wageis declared to be a decimalnumber with 5 digits, 2 of which are after the decimal

    point

    The domain has a constraint that ensures that the

    hourly-wage is greater than 4.00

  • 8/12/2019 Lecture 10 DBMS

    16/32

    Attribute Constraints

    An attribute constraint is just a declaration to theeffect that a specified attribute is of a specified type.

    For example:

    create tableaccount(branch-name char(15),account-number char(10) not null,balance integer,)

    Attribute constraints are part of the definition of theattribute.

    Any attempt to introduce an attribute value into thedatabase that is not a type of the relevant type willsimply rejected.

    Such a situation should never arise.

  • 8/12/2019 Lecture 10 DBMS

    17/32

    Table Constraint

    A table constraint is a constraint on an individual

    table.

    Example:

    Suppliers in London must have status 20.

    Two attributes, CITY and STATUS, of table S areinvolved.

  • 8/12/2019 Lecture 10 DBMS

    18/32

    Database Constraint

    A database constraint is a constraint that

    interrelates two or more distinct tables.

    Example:

    No suppliers with status less than 20 can supply any

    part in a quantity greater than 500.

    Two tables, S and SP, are involved.

  • 8/12/2019 Lecture 10 DBMS

    19/32

    Reference:

    Chapter No 4 of the book by (C. J. Date)

    Home work:

    Read the chapter 4 of the book

  • 8/12/2019 Lecture 10 DBMS

    20/32

    Keys

    The concept of Keysis very important to the

    relational model.

    We'll discuss the following types of keys.

    Candidate Keys

    Primary Keys and Alternative Keys Foreign Keys

  • 8/12/2019 Lecture 10 DBMS

    21/32

  • 8/12/2019 Lecture 10 DBMS

    22/32

    Candidate Keys

    Let R be a table. By definition, at any given time,no two tuples in the value of R are duplicates ofone another.

    Let K be a set of attributes of R. Then K is acandidate key for R if and only if it satisfies:

    (1) Uniqueness: No legal value of R ever contains twodistinct tuples with the same value for K.

    (2) Irreducibility (or minimality):

    No proper subset of K has the uniqueness property.

  • 8/12/2019 Lecture 10 DBMS

    23/32

  • 8/12/2019 Lecture 10 DBMS

    24/32

    Primary and Alternative Keys

    Exactly one candidate key must be chosen as theprimarykey, and the others arealternative keys.

    Primary key and alternative keys are both candidatekeys.

    For S {S#, SNAME, STATUS, CITY},

    if we choose KEY {S#} as the primary key,

    then KEY {SNAME} is an alternative key.

    For SP {S#, P#, QTY},

    We must choose KEY {S#, P#} as the primary key, andthere will be no alternative key.

  • 8/12/2019 Lecture 10 DBMS

    25/32

    Foreign Keys

    A foreign keyis a set of attributes of one table

    R2 whose values are required to match values

    of some candidate keyof some table R1.

    {S#} is a foreign keyof SP that referencesthe

    primary key{S#} of S.

    Any S# value of SP must exist in S.

    Similarly, {P#} is another foreign keyof SP that

    referencestheprimary key{P#} of P.

  • 8/12/2019 Lecture 10 DBMS

    26/32

    Integrity Rules

    Entity integrity No two rows with the same primary key value

    No null values in a primary key

    Referential integrity Foreign keys must match candidate key of source table

    Foreign keys in some cases can be null

    The database must not contain any unmatched foreign keyvalues.

    If B references A, A must exist.

  • 8/12/2019 Lecture 10 DBMS

    27/32

    Referenced Rows

    Referenced row Foreign keys reference rows in the associated primary key

    table

    Each supply row references supplier and part rows.

    Actions on referenced rows Delete a referenced row

    Change the primary key of a referenced row

    Referential integrity should not be violated

  • 8/12/2019 Lecture 10 DBMS

    28/32

    Possible Actions

    Restrict: do not permit action on the

    referenced row

    Cascade: perform action on related rows

    Nullify: only valid if foreign keys accept null

    values

    Default: set foreign keys to a default value

  • 8/12/2019 Lecture 10 DBMS

    29/32

    Referential Actions on Foreign

    Keys

    When we delete a tuple from a table, say S, that

    is referencedby another table, say SP,

    if theprimary key{S#} value of S exists in SP,

    there are several choices of referential actions:

    ON DELETE CASCADE

    The corresponding tuples in SP will be deleted too. ON DELETE RESTRICT

    The deletion of the tuple from S is rejected.

  • 8/12/2019 Lecture 10 DBMS

    30/32

    Referential Actions (Cont.)

    When we update a tuple from a table, say S, thatis referencedby another table, say SP,

    There are similar choices of referential actions: ON UPDATE CASCADE

    ON UPDATE RESTRICT

    There could be other choices besides thesethree. , e.g., ON UPDATE SET DEFAULT.

    ON UPDATE SET NULL.

  • 8/12/2019 Lecture 10 DBMS

    31/32

    Integrity (Fig. from C.J. Date)

    S# SNAME STATUS CITY

    S1

    S2

    S3

    S4

    S5

    Smith

    Jones

    Blake

    Clark

    Adams

    20

    10

    30

    20

    30

    London

    Paris

    Paris

    London

    Athens

    P# PNAME COLOR WEIGHT

    P1

    P2P3

    P4

    P5

    P6

    Nut

    BoltScrew

    Screw

    Cam

    Cog

    Red

    GreenBlue

    Red

    Blue

    Red

    12

    1717

    14

    12

    19

    CITY

    London

    ParisRome

    London

    Paris

    London

    S#

    S1

    S1

    S1

    S1

    S1

    S1

    S2

    S2

    S3

    S4S4

    S4

    P#

    P1

    P2

    P3

    P4

    P5

    P6

    P1

    P2

    P2

    P2P4

    P5

    QTY

    300

    200

    400

    200

    100

    100

    300

    400

    200

    200300

    400

    S

    P

    SP

  • 8/12/2019 Lecture 10 DBMS

    32/32

    SQL Syntax for Actions (Just a

    flavor)

    CREATE TABLE Enrollment

    ( OfferNo INTEGER NOT NULL,

    StdSSN CHAR(11) NOT NULL,

    EnrGrade DECIMAL(3,2),

    CONSTRAINT PKEnrollment PRIMARY KEY(OfferNo,

    StdSSN),

    CONSTRAINT FKOfferNo FOREIGN KEY (OfferNo)REFERENCES Offering

    ON DELETE RESTRICT

    ON UPDATE CASCADE,

    CONSTRAINT FKStdSSN FOREIGN KEY (StdSSN) REFERENCES

    Student

    ON DELETE RESTRICT

    ON UPDATE CASCADE )


Recommended