DBMS Constraints

download DBMS Constraints

of 23

Transcript of DBMS Constraints

  • 7/29/2019 DBMS Constraints

    1/23

  • 7/29/2019 DBMS Constraints

    2/23

    Chapter 5

    The Relational Data Model andRelational Database Constraints

    Copyright 2004 Pearson Education, Inc.

  • 7/29/2019 DBMS Constraints

    3/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Chapter Outline

    hapter 5-3

    Relational Model Concepts

    Relational Model Constraints and Relational

    Database Schemas

    Update Operations and Dealing with ConstraintViolations

  • 7/29/2019 DBMS Constraints

    4/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Relational Model Concepts

    hapter 5-4

    The relational Model of Data is based on theconcept of a Relation.

    Each relation resembles a table of values Each row in the table represents a collection

    of related data values Each row in the table represents a fact tht

    typically corresponds to a real world entity orrelationship

    A Row is called a tuple

    A column header is called a attribute Table is called a relationA Relation is a mathematical concept based

    on the ideas of sets.

  • 7/29/2019 DBMS Constraints

    5/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    FORMAL DEFINITIONS

    hapter 5-5

    The Schema of a Relation: R(A1, A2, .....An), is madeup of relation name R and list of attributes A1, A2..An

    Relation schema Ris defined overattributesA1, A2,.....An

    For Example -

    CUSTOMER (Cust-id, Cust-name, Address,Phone#)

    **Above example is a relation of degree 4

    Here, CUSTOMER is a relation defined over the fourattributes Cust-id, Cust-name, Address, Phone#, eachof which has a domain or a set of valid values. Forexample, the domain ofCust-id is 6 digit numbers.

  • 7/29/2019 DBMS Constraints

    6/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    FORMAL DEFINITIONS

    hapter 5-6

    A tuple is an ordered set of values Each value is derived from an appropriate domain.

    Each row in the CUSTOMER table may be referred to

    as a tuple in the table and would consist of four values.

    is a tuple belonging to the CUSTOMER relation.

    A relation may be regarded as a set of tuples(rows).

    Columns in a table are also called attributes of the

    relation.

  • 7/29/2019 DBMS Constraints

    7/23Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    FORMAL DEFINITIONS

    hapter 5-7

    A domain has a logical definition:

    A domain may have a data-type or a format defined forit.

    USA_phone_numbers are the set of 10 digit phonenumbers valid in the U.S.

    The USA_phone_numbers may have a format: (ddd)-ddd-dddd where each d is a decimal digit.

    E.g., Dates have various formats such as monthname,date, year or yyyy-mm-dd, or dd mm,yyyy etc.

  • 7/29/2019 DBMS Constraints

    8/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    FORMAL DEFINITIONS

    hapter 5-8

    The relation is formed over the cartesian product of the sets;each set has values from a domain

    Cartesian product specifies all possible combinations ofvalues from underlying domain

    For example, attribute Cust-name is defined over the domainof strings of 25 characters. The role these strings play in theCUSTOMER relation is that of the name of customers.

    R: schema of the relation

    r of R: a specific "value" or population of R.

    R is also called the intension of a relation

    r is also called the extension of a relation

  • 7/29/2019 DBMS Constraints

    9/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    FORMAL DEFINITIONS

    hapter 5-9

    Let S1 = {0,1} Let S2 = {a,b,c}

    Let R S1 X S2

    Then for example: r(R) = { , , }

    is one possible state or population orextension r of the relation R, defined over

    domains S1 and S2. It has three tuples.

  • 7/29/2019 DBMS Constraints

    10/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    DEFINITION SUMMARYInformal Terms Formal Terms

    Table Relation

    Column Attribute/Domain

    Row Tuple

    Values in a column Domain

    Table Definition Schema of a Relation

    Populated Table Extension

    apter 5-10

  • 7/29/2019 DBMS Constraints

    11/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Example - Figure 5.1

    hapter 5-11

  • 7/29/2019 DBMS Constraints

    12/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    CHARACTERISTICS OF RELATIONS

    Ordering of tuples in a relation r(R): A relation is defined as aset of tuples. Mathematically elements of a set have no orderamong them hence tuples in a relation do not have any particularorder.

    Ordering of attributes in a relation schema R (and of valueswithin each tuple): We will consider the attributes in R(A1, A2, ...,

    An) and the values in t= to be ordered.

    (However, a more general alternative definition of relation doesnot require this ordering).

    Values in a tuple: All values are considered atomic(indivisible). A special null value is used to represent values that

    are unknown or inapplicable to certain tuples.

  • 7/29/2019 DBMS Constraints

    13/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Relational Integrity Constraints

    apter 5-13

    Constraints are conditions that must hold on allvalid relation instances. The main types of

    constraints are:

    1. Domain constraints

    2. Key constraints

    3. Entity integrity constraints

    4. Referential integrity constraints

  • 7/29/2019 DBMS Constraints

    14/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Domain Constraints

    apter 5-14

    Domain constraints specify tht the value of eachattribute A must be an atomic value from the

    domain dom(A)

    The data types associated with domains typically

    include integer, float and character also date, time

    and money data types.

    Eg- roll no 1 to 50 cant have 60 roll no

  • 7/29/2019 DBMS Constraints

    15/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Key Constraints Superkey of R: A set of attributes SK of R such that no

    two tuples in any valid relation instance r(R) will havethe same value for SK. That is, for any distinct tuples t1and t2 in r(R), t1[SK] t2[SK].

    Key of R: A "minimal" superkey; that is, a superkey Ksuch that removal of any attribute from K results in a set

    of attributes that is not a superkey.Example: The CAR relation schema:CAR(State, Reg#, SerialNo, Make, Model, Year)has two keys Key1 = {State, Reg#}, Key2 = {SerialNo}, which are

    also superkeys. {SerialNo, Make} is a superkey but not akey.

    If a relation has several candidate keys, one is chosenarbitrarily to be the primary key. The primary keyattributes are underlined.

    Eg House front door and backdoor key.

    Eg- pan no, emp id. To calculate slary we need emp id

    not pan no

  • 7/29/2019 DBMS Constraints

    16/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Key Constraints

    apter 5-16

    5.4

  • 7/29/2019 DBMS Constraints

    17/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Entity Integrity

    apter 5-17

    Entity Integrity: The primary key attributes of a tuplecan never be null

    t[PK] null for any tuple t in r(R) Note: Other attributes of R may be similarly

    constrained to disallow null values, even though theyare not members of the primary key.

  • 7/29/2019 DBMS Constraints

    18/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Referential Integrity

    apter 5-18

    A constraint involving two relations (the previousconstraints involve a single relation).

    Used to specify a relationship among tuples in tworelations: the referencing relation and the referencedrelation.

    Tuples in the referencing relation R1 have attributes FK(called foreign key attributes) that reference the primarykey attributes PK of the referenced relation R2. A tuple t1 inR1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].

    In any given relation a set of attributes is called a foreign iffollowing rules hold

    The attribute in the foreign key has to be the same as theprimary key in other relation

    For every tuple in the referencing relation like dpt theattributes in its foreign key refer to existing tuples like eachdpt shd hv a manager who exist in the database or it shd

    be null

  • 7/29/2019 DBMS Constraints

    19/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Referential IntegrityConstraint

    apter 5-19

    Statement of the constraintThe value in the foreign key column (or columns)

    FK of the the referencing relation R1 can beeither:

    (1) a value of an existing primary key valueof the corresponding primary key PK in thereferenced relation R2,, or..

    (2) a null.

    In case (2), the FK in R1 should not be a part ofits own primary key.

  • 7/29/2019 DBMS Constraints

    20/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

    Other Types of Constraints

    apter 5-20

    Semantic Integrity Constraints:

    - based on application semantics and cannot beexpressed by the model per se

    -E.g., the max. no. of hours per employee for allprojects he or she works on is 56 hrs per week

    - A constraint specification languagemay have tobe used to express these

    - SQL-99 allows triggers and ASSERTIONS toallow for some of these

  • 7/29/2019 DBMS Constraints

    21/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Editionapter 5-21

    5.5

  • 7/29/2019 DBMS Constraints

    22/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Editionapter 5-22

    5.6

  • 7/29/2019 DBMS Constraints

    23/23

    Elmasri/Navathe, Fundamentals of Database Systems, Fourth Editionapter 5-23

    5.7