0% found this document useful (0 votes)
4 views41 pages

Chapter4_The Relational Data Model and the Relation Database Constraints

Chapter 3 discusses the relational data model and its associated constraints, including the structure of relations, attributes, and tuples. It covers the types of constraints such as domain, key, and referential integrity constraints that ensure data validity within relational databases. Additionally, it highlights the importance of integrity constraints in maintaining consistency across database states.

Uploaded by

randa19112013
Copyright
© © All Rights Reserved
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)
4 views41 pages

Chapter4_The Relational Data Model and the Relation Database Constraints

Chapter 3 discusses the relational data model and its associated constraints, including the structure of relations, attributes, and tuples. It covers the types of constraints such as domain, key, and referential integrity constraints that ensure data validity within relational databases. Additionally, it highlights the importance of integrity constraints in maintaining consistency across database states.

Uploaded by

randa19112013
Copyright
© © All Rights Reserved
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
You are on page 1/ 41

The relational Data model and

the relational data base


constraints

Chapter 3

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       1
Outline
§ The Relational Data Model and Relational
Database Constraints

§ Relational Model Constraints and Relational


Database Schemas

§ Update Operations, Transactions, and Dealing


with Constraint Violations

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       2
The relational data model and the relational
data base constraints

§ Relational model
§ First commercial implementations available in
early 1980s
§ It has been implemented in a large number of
commercial system
§ Hierarchical and network models
§ Preceded the relational model

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       3
Relational model concepts

§ Represents data as a collection of relations


§ Table of values
§ Row
• Represents a collection of related data values
• Fact that typically corresponds to a real-world entity
or relationship
• Tuple
§ Table name and column names
• Interpret the meaning of the values in each row
attribute
Chapter  3:  The  rela7onal  data  model  and  the  
Fundamentals  of  Database  
rela7onal  constraints       4
Domains, attributes, tuples and relations

§ Domain D
§ Set of atomic values
§ Atomic
§ Each value indivisible
§ Specifying a domain
§ Data type specified for each domain

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       5
Domains, Attributes, Tuples, and
Relations (cont’d.)

§ Relation schema R
§ Denoted by R(A1, A2, ...,An)
§ Made up of a relation name R and a list of
attributes, A1, A2, ..., An
§ Attribute Ai
§ Name of a role played by some domain D in
the relation schema R
§ Degree (or arity) of a relation
§ Number of attributes n of its relation schema
Domains, Attributes, Tuples, and
Relations (cont’d.)

§ Relation (or relation state)


§ Set of n-tuples r = {t1, t2, ..., tm}
§ Each n-tuple t
• Ordered list of n values t =<v1, v2, ..., vn
• Each value vi, 1 ≤ i ≤ n, is an element of dom(Ai) or
is a special NULL value
Domains, Attributes, Tuples, and
Relations (cont’d.)

§ Cardinality
§ Total number of values in domain
§ Current relation state
§ Relation state at a given time
§ Reflects only the valid tuples that represent a
particular state of the real world
§ Attribute names
§ Indicate different roles, or interpretations, for
the domain
Characteristics of Relations

§ Ordering of tuples in a relation


§ Relation defined as a set of tuples
§ Elements have no order among them
§ Ordering of values within a tuple and an
alternative definition of a relation
§ Order of attributes and values is not that
important
§ As long as correspondence between attributes
and values maintained
Characteristics of Relations
Characteristics of Relations

§ Values and NULLs in tuples


§ Each value in a tuple is atomic
§ Flat relational model
• Composite and multivalued attributes not allowed
• First normal form assumption
§ Multivalued attributes
• Must be represented by separate relations
§ Composite attributes
• Represented only by simple component attributes
in basic relational model
Characteristics of Relations

§ NULL values
§ Represent the values of attributes that may be
unknown or may not apply to a tuple
§ Meanings for NULL values
• Value unknown
• Value exists but is not available
• Attribute does not apply to this tuple (also known
as value undefined)
Characteristics of Relations

§ Interpretation (meaning) of a relation


§ Assertion
• Each tuple in the relation is a fact or a particular
instance of the assertion
§ Predicate
• Values in each tuple interpreted as values that
satisfy predicate
Relational model notation

§ Relation schema R of degree n


§ Denoted by R(A1, A2, ..., An)
§ Uppercase letters Q, R, S
§ Denote relation names
§ Lowercase letters q, r, s
§ Denote relation states
§ Letters t, u, v
§ Denote tuples
Relational model notation

§ Name of a relation schema: STUDENT


§ Indicates the current set of tuples in that
relation
§ Notation: STUDENT(Name, Ssn, ...)
§ Refers only to relation schema
§ Attribute A can be qualified with the
relation name R to which it belongs
§ Using the dot notation R.A
Relational model constraints

§ Constraints
§ Restrictions on the actual values in a database
state
§ Derived from the rules in the mini-world that the
database represents

§ Inherent model-based constraints or implicit


constraints
§ Inherent in the data model

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       16
Relational model constraints

§ Inherent model-based constraints or implicit


constraints
§ Schema-based constraints or explicit
constraints
§ Can be directly expressed in schemas of the
data model
§ Application-based or semantic constraints or
business rules
§ Cannot be directly expressed in schemas
§ Expressed and enforced by application program

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       17
Domain constraints

§ Typically include:
§ Numeric data types for integers and real
numbers
§ Characters
§ Booleans
§ Fixed-length strings
§ Variable-length strings
§ Date, time, timestamp
§ Money
§ Other special data types

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       18
Key constraints

§ No two tuples can have the same combination


of values for all their attributes.
§ Superkey
§ No two distinct tuples in any state r of R can have
the same value for SK
§ Key
§ Superkey of R
§ Removing any attribute A from K leaves a set of
attributes K that is not a superkey of R any more
Fundamentals  of  Database  
19
Key constraints

§ Candidate key
§ Relation schema may have more than one
candidate key

§ Primary key of the relation


§ Designated among candidate keys
§ Underline attribute

§ Other candidate keys are designated as unique


keys

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       20
Key constraints

§ Key satisfies two properties:


§ Two distinct tuples in any state of
relation cannot have identical values for
(all) attributes in key
§ Minimal superkey
• Cannot remove any attributes and still have
uniqueness constraint in above condition
hold

Fundamentals  of  Database  


21
Key constraints

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       22
Relational Databases and Relational Database schemas

§ Relational database schema S


§ Set of relation schemas S = {R1, R2, ..., Rm}
§ Set of integrity constraints IC

§ Relational database state


§ Set of relation states DB = {r1, r2, ..., rm}
§ Each ri is a state of Ri and such that the ri relation states
satisfy integrity constraints specified in IC

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       23
Relational Databases and Relational Database schemas

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       24
Relational Databases and Relational Database schemas

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       25
Relational Databases and Relational Database schemas

§ Invalid state
§ Does not obey all the integrity constraints
§ Valid state
§ Satisfies all the constraints in the defined set of
integrity constraints IC

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       26
Integrity, Referential Integrity, and foreign keys

§ Entity integrity constraint


§ No primary key value can be NULL
§ Referential integrity constraint
§ Specified between two relations
§ Maintains consistency among tuples in two
relations

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       27
Integrity, Referential Integrity, and foreign keys

§ Foreign key rules:


§ The attributes in FK have the same domain(s)
as the primary key attributes PK
§ Value of FK in a tuple t1 of the current state
r1(R1) either occurs as a value of PK for some
tuple t2 in the current state r2(R2) or is NULL

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       28
Integrity, Referential Integrity, and foreign keys

The conditions for a foreign key, given below, specify a


referential integrity constraint between the two relation
schemas R1 and R2. A set of attributes FK in relation schema
R1 is a foreign key of R1 that references relation R2 if it
satisfies the following rules:
1. The attributes in FK have the same domain(s) as the
primary key attributes PK of R2; the attributes FK are said
to reference or refer to the relation R2.
2. A value of FK in a tuple t1 of the current state r1(R1) either
occurs as a value of PK for some tuple t2 in the current
state r2(R2) or is NULL. In the former case, we have t1[FK]
= t2[PK], and we say that the tuple t1 references or refers
to the tuple t2.
Chapter  3:  The  rela7onal  data  model  and  the  
Fundamentals  of  Database  
rela7onal  constraints       29
Integrity, Referential Integrity, and foreign keys

§ Diagrammatically display referential integrity constraints


§ Directed arc from each foreign key to the relation it
references
§ All integrity constraints should be specified on relational
database schema

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       30
Integrity, Referential Integrity, and foreign keys

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       31
Other types of constraints

§ Semantic integrity constraints


§ May have to be specified and enforced on a
relational database
§ Use triggers and assertions
§ More common to check for these types of
constraints within the application programs

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       32
Other types of constraints

§ Functional dependency constraint


§ Establishes a functional relationship among two
sets of attributes X and Y
§ Value of X determines a unique value of Y
§ State constraints
§ Define the constraints that a valid state of the
database must satisfy
§ Transition constraints
§ Define to deal with state changes in the database

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       33
Update operations, Transactions, and dealing with
constraint violations
§ Operations of the relational model can be
categorized into retrievals and updates
§ Basic operations that change the states of
relations in the database:
§ Insert
§ Delete
§ Update (or Modify)

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       34
The insertion operation

§ Provides a list of attribute values for a new tuple t


that is to be inserted into a relation R
§ Can violate any of the four types of constraints
§ If an insertion violates one or more constraints
§ Default option is to reject the insertion

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       35
The insertion operation… An example:

Operation:
Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, NULL, ‘1960-04-05’, ‘6357
Windy Lane, Katy, TX’, F, 28000, NULL, 4> into EMPLOYEE.
Result: This insertion violates the entity integrity constraint
(NULL for the primary key Ssn), so it is rejected.

Operation:
Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’, ‘6357
Windy Lane, Katy, TX’, F, 28000, ‘987654321’, 4> into
EMPLOYEE.
Result: This insertion violates the key constraint because
another tuple with the same Ssn value already exists in the
EMPLOYEE relation, and so it is rejected.
36
The insertion operation… An example:

Operation:
Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’,
‘6357 Windswept, Katy, TX’, F, 28000, ‘987654321’, 7> into
EMPLOYEE.
Result: This insertion violates the referential integrity constraint
specified on Dno in EMPLOYEE because no corresponding
referenced tuple exists in DEPARTMENT with Dnumber = 7.

Operation:
Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’,
‘6357 Windy Lane, Katy, TX’, F, 28000, NULL, 4> into
EMPLOYEE.
Result: This insertion satisfies all constraints, so it is
acceptable.
37
The delete operation

§ Can violate only referential integrity


§ If tuple being deleted is referenced by foreign keys from
other tuples
§ Restrict
• Reject the deletion
§ Cascade
• Propagate the deletion by deleting tuples that
reference the tuple that is being deleted
§ Set null or set default
• Modify the referencing attribute values that cause the
violation
Chapter  3:  The  rela7onal  data  model  and  the  
Fundamentals  of  Database  
rela7onal  constraints       38
The delete operation…. An example:

Operation:
Delete the WORKS_ON tuple with Essn = ‘999887777’ and Pno = 10.
Result: This deletion is acceptable and deletes exactly one tuple.

Operation:
Delete the EMPLOYEE tuple with Ssn = ‘999887777’.
Result: This deletion is not acceptable, because there are tuples in
WORKS_ON that refer to this tuple. Hence, if the tuple in EMPLOYEE
is deleted, referential integrity violations will result.

Operation:
Delete the EMPLOYEE tuple with Ssn = ‘333445555’.
Result: This deletion will result in even worse referential integrity
violations, because the tuple involved is referenced by tuples from
the EMPLOYEE, DEPARTMENT,WORKS_ON, and DEPENDENT relations.

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       39
The update operation

§ Necessary to specify a condition on attributes of


relation
§ Select the tuple (or tuples) to be modified
§ If attribute not part of a primary key nor of a foreign
key
§ Usually causes no problems
§ Updating a primary/foreign key
§ Similar issues as with Insert/Delete

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       40
The update operation … An example

Chapter  3:  The  rela7onal  data  model  and  the  


Fundamentals  of  Database  
rela7onal  constraints       41

You might also like