0% found this document useful (0 votes)
38 views46 pages

DBMS CST 204 Relational Model Costraints & Constraints Violation

The document provides an overview of the relational data model and relational database constraints. It defines key concepts such as relations, tuples, domains, schemas, and keys. It also describes integrity constraints including domain constraints, entity integrity constraints, and referential integrity constraints. Foreign keys are introduced as a way to specify relationships between relations and maintain referential integrity.

Uploaded by

Dev Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views46 pages

DBMS CST 204 Relational Model Costraints & Constraints Violation

The document provides an overview of the relational data model and relational database constraints. It defines key concepts such as relations, tuples, domains, schemas, and keys. It also describes integrity constraints including domain constraints, entity integrity constraints, and referential integrity constraints. Foreign keys are introduced as a way to specify relationships between relations and maintain referential integrity.

Uploaded by

Dev Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

Module-2

The Relational Data Model and Relational


Database Constraints
Module 2: Relational Model
• Structure of Relational Databases - Integrity
Constraints, Synthesizing ER diagram to relational
schema
• Introduction to Relational Algebra - select, project,
cartesian product operations, join - Equi-join, natural
join. query examples,
• introduction to Structured Query Language (SQL),
Data Definition Language (DDL), Table definitions and
operations – CREATE, DROP, ALTER, INSERT, DELETE,
UPDATE.
Relational Model Concepts
• Represents data as a collection of relations
• Informally, a relation looks like a table of values
• Table of values
– Row
• Represents a collection of related data values
• Fact that typically corresponds to a real-world entity or
relationship
– In the formal model, rows are called tuples
– Table name and column names
• To interpret the meaning of the values in each row-
attribute
Relational Model Concepts (cont’d.)
Informal Definitions
• Key of a Relation:
– Each row has a value of a data item (or set of items) that
uniquely identifies that row in the table
• Called the key
– In the STUDENT table, SSN is the key

– Sometimes row-ids or sequential numbers are assigned


as keys to identify the rows in a table
• Called artificial key or surrogate key
Formal Definitions - Schema
• The Schema (or description) of a Relation:
– Denoted by R(A1, A2, .....An)
– R is the name of the relation
– The attributes of the relation are A1, A2, ..., An
• Example:
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
– CUSTOMER is the relation name
– Defined over the four attributes: Cust-id, Cust-name, Address,
Phone#
• Each attribute has a domain or a set of valid values.
– For example, the domain of Cust-id is 6 digit numbers.
– Indicated by dom(Cust_id)
Formal Definitions - Tuple
• A tuple is an ordered set of values (enclosed in angled
brackets ‘< … >’)
• Each value is derived from an appropriate domain.

• A row in the CUSTOMER relation is a 4-tuple and would


consist of four values, for example:
– <632895, "John Smith", "101 Main St. Atlanta, GA 30332",
"(404) 894-2000">
– This is called a 4-tuple as it has 4 values
– A tuple (row) in the CUSTOMER relation.

• A relation is a set of such tuples (rows)


Formal Definitions - Domain
• A domain is a set of atomic values.

• A domain has a logical definition:


– Example: “USA_phone_numbers” are the set of 10 digit
phone numbers valid in the U.S.

• A domain also has a data-type or a format defined for it.


– The USA_phone_numbers may have a format: (ddd)ddd-
dddd where each d is a decimal digit.
– Dates have various formats such as year, month, date
formatted as yyyy-mm-dd, or as dd mm,yyyy etc.
Formal Definitions - Domain

• The attribute name designates the role played by a domain in a


relation:
– Used to interpret the meaning of the data elements
corresponding to that attribute
– Example: The domain Date may be used to define two
attributes named “Invoice-date” and “Payment-date” with
different meanings

– The degree(or arity) of a relation is the number of attributes


n of its relation schema.
Formal Definitions
• Formally,
– Given R(A1, A2, .........., An)
– r(R)  dom (A1) X dom (A2) X ....X dom(An)

• R(A1, A2, …, An) is the schema of the relation


• R is the name of the relation
• A1, A2, …, An are the attributes of the relation
• r(R): a specific state (or "value" or “population”) of relation R
– this is a set of tuples (rows)
– r(R) = {t1, t2, …, tn} where each ti is an n-tuple
Formal Definitions - State
• The relation state is a subset of the Cartesian product of the
domains of its attributes
– each domain contains the set of all possible values the
attribute can take.

• Example: attribute Cust-name is defined over the domain of


character strings of maximum length 25
– dom(Cust-name) is varchar(25)

• The role these strings play in the CUSTOMER relation is


that of the name of a customer.
Formal Definitions - Example
• Let R(A1, A2) be a relation schema:
– Let dom(A1) = {0,1}
– Let dom(A2) = {a,b,c}
• Then: dom(A1) X dom(A2) is all possible combinations:
{<0,a> , <0,b> , <0,c>, <1,a>, <1,b>, <1,c> }
• The relation state r(R)  dom(A1) X dom(A2)
• For example: r(R) could be {<0,a> , <0,b> , <1,c> }
– this is one possible state (or “population” or “extension”) r of
the relation R, defined over A1 and A2.

• Cardinality
– Total number of values in domain indicated by |D|
– Total no.of tuples in the cartesian product
Definition Summary
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Domain
Values
Row Tuple

Table Definition Schema of a Relation


Populated Table State of the Relation
Characteristics of Relations

1. Ordering of tuples in a relation


– Relation defined as a set of tuples
– tuples have no particular order
2. Ordering of values within a tuple and an alternative
definition of a relation
– n-tuple is an ordered list of n values.
– A tuple can also be considered as a set of (<attribute>,<value>)
pairs.
– Order of attributes and values is not that important
– As long as correspondence between attributes and
values maintained
Characteristics of Relations
Characteristics of Relations

3. Values and NULLs in tuples


– Each value in a tuple is atomic
– Flat relational model
• Composite and multivalued attributes not allowed
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)
Relational Model Notation
• Relation schema R of degree n : R(A1,A2,…An)

• Uppercase letters Q,R,S denotes relation names

• Lowercase letters q,r,s denote relation states

• Letters t,u,v denote tuples.

• Attribute name can be represented with relation name as


R.A- eg: STUDENT.name
Relational Model Notation
• An n-tuple t in a relation state r(R) is denoted by t=<v 1,v2,
…vn> where vi is the value corresponding to attribute A i

• Value in a tuple for attribute Ai can be indicated with t[Ai]


or t.Ai

• Eg: t=<‘Dick Davidson’,422-11-2320, NULL,’3452 Eglin


Road’,(817)749-1253,25,3.53> from STUDENT

• t[Name]=<‘Dick Davidson’>

• t[SSN,Gpa,Age]=< 422-11-2320,3.53,25>
Relational model constraints and
relational database schemas
Relational Constraints or Schema based Constraints or
Explicit Constraints

• Relational Integrity constraints in DBMS are


referred to conditions which must be present
for a valid relation.
1.Domain Constraints
2.Key Constraints and Constraints on NULL
Values
3.Entity Integrity Constraints
4.Referential Integrity Constraints
Domain Constraints
• Specify that the value of each attribute A must be atomic
from the domain dom(A)

• Domain 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
Key Constraints and Constraints on
NULL Values
• No two tuples can have the same combination
of values for all their attributes.
• All tuples in a relation must be distinct
• Superkey
– No two distinct tuples in any state r of R can have the
same value for SK
• Key
– Is a Superkey of R with additional property that on
removing any attribute A from K leaves a set of
attributes K that is not a superkey of R any more
Key Constraints and Constraints on
NULL Values (cont’d.)
• 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
Key Constraints and Constraints on
NULL Values (cont’d.)
• Candidate key
– Relation schema may have more than one key
• Primary key of the relation
– Designated among candidate keys
– PK is Underlined in the schema.
• Other candidate keys are designated as
unique keys
Key Constraints and Constraints on
NULL Values (cont’d.)
Integrity, Referential Integrity,
and Foreign Keys
• Entity integrity constraint
– States that no primary key value can be NULL
• Referential integrity constraint
– Specified between two relations
– Maintains consistency among tuples in two
relations
– It states that a tuple in one relation that refers to
another relation must refer to an existing tuple in
that relation .
Integrity, Referential Integrity,
and Foreign Keys (cont’d.)
• 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.
Integrity, Referential Integrity,
and Foreign Keys (cont’d.)
• 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
Other Types of Constraints (cont’d.)
• 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
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)
The Insert 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
Possible violations for each operation
• INSERT may violate any of the constraints:
– Domain constraint:
• if one of the attribute values provided for the new tuple is not of
the specified attribute domain
– Key constraint:
• if the value of a key attribute in the new tuple already exists in
another tuple in the relation
– Referential integrity:
• if a foreign key value in the new tuple references a primary key
value that does not exist in the referenced relation
– Entity integrity:
• if the primary key value is null in the new tuple
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
Possible violations for each operation
• DELETE may violate only referential integrity:
– If the primary key value of the tuple being deleted is referenced
from other tuples in the database
• Can be remedied by several actions: RESTRICT, CASCADE, SET NULL
(see Chapter 8 for more details)
– RESTRICT option: reject the deletion
– CASCADE option: propagate the new primary key value into the
foreign keys of the referencing tuples
– SET NULL option: set the foreign keys of the referencing tuples to
NULL
– One of the above options must be specified during database
design for each foreign key constraint
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
Possible violations for each operation
• UPDATE may violate domain constraint and NOT NULL
constraint on an attribute being modified
• Any of the other constraints may also be violated, depending
on the attribute being updated:
– Updating the primary key (PK):
• Similar to a DELETE followed by an INSERT
• Need to specify similar options to DELETE
– Updating a foreign key (FK):
• May violate referential integrity
– Updating an ordinary attribute (neither PK nor FK):
• Can only violate domain constraints
In-Class Exercise
(Taken from Exercise 5.15)
Consider the following relations for a database that keeps track of student
enrollment in courses and the books adopted for each course:
STUDENT(SSN, Name, Major, Bdate)
COURSE(Course#, Cname, Dept)
ENROLL(SSN, Course#, Quarter, Grade)
BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
TEXT(Book_ISBN, Book_Title, Publisher, Author)
Draw a relational schema diagram specifying the foreign keys for this
schema.

You might also like