0% found this document useful (0 votes)
26 views61 pages

Lec 11 (Extra) - Relational Model

The document discusses relational database constraints. It describes how relational databases store data in tables consisting of rows and columns. Constraints ensure data integrity by restricting what values can be inserted into columns. There are several types of constraints including domain constraints to enforce valid data types, key constraints to enforce unique primary keys, and integrity constraints to enforce valid foreign key references between tables. Maintaining constraints is important for ensuring the accuracy and consistency of data in the database.
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)
26 views61 pages

Lec 11 (Extra) - Relational Model

The document discusses relational database constraints. It describes how relational databases store data in tables consisting of rows and columns. Constraints ensure data integrity by restricting what values can be inserted into columns. There are several types of constraints including domain constraints to enforce valid data types, key constraints to enforce unique primary keys, and integrity constraints to enforce valid foreign key references between tables. Maintaining constraints is important for ensuring the accuracy and consistency of data in the database.
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/ 61

The Relational Data Model and Relational Database

Constraints
WHAT WE ARE GOING TO LEARN?
 The Relational Data Model and Relational Database
Constraints
 Relational Model Constraints and Relational Database
Schemas
 Update Operations, Transactions,
and Dealing with Constraint Violations
THE RELATIONAL DATA MODEL AND
RELATIONAL DATABASE
CONSTRAINTS
 Relational model
 The model was first proposed by Dr. E.F. Codd
 First commercial implementations available in early
1980s
 Has been implemented in a large number of
commercial system
 Hierarchical and network models
 Preceded the relational model
RELATIONAL MODEL CONCEPTS
 In relational data model, the data is stored in the form of
tables (i.e. rows ad columns). These tables are called
relations.
 The user of the relational database system may insert
new tuples, delete tuples, and modify tuples
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
 Relation: A (named) set of tuples all of the same form (i.e.,
having the same set of attributes)

 Relation schema R
 used for describing (the structure of) a relation
 It depicts the attributes of the table A 1, A2, ...,An
 It
consists of a relation name R and set of attributes A1, A2, ...,An
 Denoted by R( A1, A2, ...,An)

 Example:
 CUSTOMER(CustomerID,CustonerName,Status)

 Degree (or arity) of a relation


 Number of attributes in a relation schema
DOMAIN
 It is a data type that describes the types of values that can appear in
each column
 Atomic-Each value indivisible
 Specifying a domain -data type specified for each domain
 Set of atomic values
 USA_phone_number: string of digits of length ten
 SSN: string of digits of length nine
 Name: string of characters beginning with an upper case letter
 GPA: a real number between 0.0 and 4.0
 Sex: a member of the set { female, male }
 Dept_Code: a member of the set { CMPS, MATH, ENGL, PHYS, PSYC, ... }
ATTRIBUTES

 Attribute Ai
 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

CUSTOMER (CID, Cname, Caddress, Cphone)


TUPLE
 In a relation a row is called a tuple.
 A tuple is an ordered set of values (enclosed in angled
brackets ‘< … >’). Each value is derived from an
appropriate domain.
 Each n-tuple is an ordered list of n values t=(v1,v2,….,vn)
where each value vi 1<= i<=n.
CUSTOMER (CID, Cname, Caddress, Cphone)

 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)


 Cardinality
 Number of rows in any relation is known as its cardinality

 Current relation state


 Relation state at a given time
 Reflects only the valid tuples that represent a particular state of the real
world

4 Microsoft Active

12
RELATION
• Relational Database schema: A collection of relations
S={R1,R2,…,Rm}, each one consistent with its specified
relational schema.

• A Relational Database Instance is a set of relation


instances DB = {r1,...,rn} such that each ri in an instance
of Ri satisfying the constraints
DEFINITION SUMMARY

Informal Terms Formal Terms

Table Relation
Column Attribute/Domain
Row Tuple
Values in a column Domain
Table Definition Schema of Relation
Populated Table Extension

17
CHARACTERISTICS OF RELATIONS

 Ordering of tuples in a relation


 Ordering of values within a tuple

 Values and NULLs in tuples

 No two tuples are identical in a relation

 Interpretation of a relation
CHARACTERISTICS OF RELATIONS
 Ordering of tuples in a relation
 Relation defined as a set of tuples and set has no particular order
among its elements, hence, tuples in a relation have no specific
order.
Ordering of values within a tuple
•Order of attributes and values is not that important
•As long as correspondence between attributes and values maintained

•a tuple can be considered as a set of (<attribute>, <value>) pairs, where each pair gives

the value of the mapping from an attribute Ai to a value vi from dom(Ai)


CUSTOMER( Cno, Cname, Caddress, Cphone)

t = <( Cno, 123),(Cname,’Rita’),(Caddress,’Vasco’),(Cphone,234567)>


t = < (Cname,’Rita’),( Cno, 123),(Cphone,234567),(Caddress,’Vasco’)>

t = <123,’Rita’,’Vasco’,234567>
t = <’Rita’,’Vasco’,123,234567>
 Values and NULLs in tuples
 Each value in a tuple is atomic
 A relation does not allow composite and multivalued attributes.
 Multivalued attributes
• Must be represented by separate relations

Cno Cphone 13 111111


13 111111, 222222, 333333 13 222222
 Composite attributes 13 333333
• Represented only by simple component attributes in basic relational model

• Student(Roll_No, Name)

Student(Roll_No, FirstName, LastName)


12co01 Rita Kumar
12co02 Seema Mehta
 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 (missing)

 Attribute does not apply to this tuple (also known as value undefined)

CustomerID Name City Age Gender Termination Date

NULL
2 Bob New York 55 M
NULL
3 Susan Mumbai NULL F
NULL
NULL
4 Amazon Mapusa 10
 No two tuples are identical in a relation:
 A relation is a set of tuples.
 Thus a table that contains duplicate tuples is not a relation
and cannot be stored in a relational database.
 Therefore, each tuple in a relation must be uniquely
identified by its contents.

NO
12co01 Rita Kumar
12co01 Rita Kumar
 Interpretation of a relation:
 A relation can be used to interpret facts about entities as
a type of assertion. A relation can also used to interpret
facts about relationships
 The first tuple in Figure asserts the fact that there is a STUDENT
whose Name is Benjamin Bayer, Ssn is 305-61-2435, Age is 19,
and so on.
BOOK relation
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 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
 n-tuple t in a relation r(R)
 Denoted by t = <v1, v2, ..., vn>

 vi is the value corresponding to attribute Ai


 Component values of tuples:
 t[Ai] and t.Ai refer to the value vi in t for attribute Ai
RELATIONAL MODEL CONSTRAINTS
 Constraints
 Database constraints are restrictions on the contents of the
database or on database operations. It is a condition specified
on a database schema that restricts the data to be inserted in
an instance of the database.
 Need of Constraints :
 Constraints in the database provide a way to guarantee that :
 the values of individual columns are valid.
 in a table, rows have a valid primary key or unique key values.

 in a dependent table, rows have valid foreign key values that

reference rows in a parent table.


DOMAIN CONSTRAINTS
 Each table has certain set of columns and each column allows a same type
of data, based on its data type.
 The column does not accept values of any other data type.
 Domain constraints are user defined data type and we can define them like
this:
 Domain Constraint = data type + Constraints (NOT NULL / UNIQUE /
PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT).
 Consider the example below –
KEY CONSTRAINTS
 An attribute that can uniquely identify a tuple in a
relation is called the key of the table.
 All the values of primary key must be unique. .
INTEGRITY CONSTRAINT

 Entity integrity constraint


 Entity integrity constraint specifies that no attribute of
primary key must contain a null value in any relation. This is
because the presence of null value in the primary key violates
the uniqueness property
 Consider the example below-
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
 Diagrammatically display referential integrity constraints
 Directed arc from each foreign key to the relation it references

 This rule states that if a foreign key in Table 1 refers to the Primary Key of
Table 2, then every value of the Foreign Key in Table 1 must be null or be
available in Table 2. For example,

Table 2 - referenced relation


Table 1- referencing relation
REFERENTIAL INTEGRITY AND FOREIGN KEYS
 Referential integrity constraints work on the concept of Foreign Keys.
 A foreign key is a key attribute of a relation that can be referred in other
relation.
 This rule states that if a foreign key in Table 1 refers to the Primary Key of
Table 2, then every value of the Foreign Key in Table 1 must be null or be
available in Table 2. For example,

Table 2 - referenced relation


Table 1- referencing relation
REFERENTIAL INTEGRITY AND FOREIGN KEYS
 Then the following properties must be hold :
 Records cannot be inserted into a Foreign table if corresponding
records in the master table do not exist.
 Records of the master table or Primary Table cannot be deleted or

updated if corresponding records in the detail table actually exist.


OTHER TYPES OF CONSTRAINTS
 Tuple Uniqueness Constraints
A relation is defined as a set of tuples. All tuples or all rows
in a relation must be unique or distinct. Suppose if in a
relation, tuple uniqueness constraint is applied, then all the
rows of that table must be unique i.e. it does not contain the
duplicate values.
 For example,
Constraints on values for Relation.Tuple
CHECK applies to entire tuples=> Specified separately in table definition
Example:

CREATE TABLE Campus(location char(25),


enrollment integer,
rank integer,
CHECK(enrollment >= 10,000 OR rank > 5)
 Single Value Constraints –
Single value constraints refers that each attribute of an
entity set has a single value. If the value of an attribute is
missing in a tuple, then we can fill it with a “null” value.
The null value for a attribute will specify that either the
value is not known or the value is not applicable.
Consider the below example-
 Semantic integrity
 Ensures that data entered into a row reflects an allowable value for that row.
The value must be within the domain, or allowable set of values, for that
column.
 For example, the quantity column of the items table permits only numbers. If
a value outside the domain can be entered into a column, the semantic
integrity of the data is violated.
 Other examples
UPDATE OPERATIONS, TRANSACTIONS, AND
DEALING WITH CONSTRAINT VIOLATIONS

 The basic Database operations performed on a relation are –


 Insert Operation – The insert operation is used to insert a new
tuple(s) on relations.
 Delete Operation – The delete operation is used to delete the
tuple(s).
 Update or Modify Operation – The update operation is used to
change/update the values of some attributes in existing tuples.
 Whenever these above operations are performed, the integrity constraints should not
be violated. Before describing operations, let us understand the following figure.

 Consider two existing relations named EMPLOYEE and DEPARTMENT.


 Some Basic Points about the Figure –

 Here, ENO is a Primary Key and


DNO is a Foreign Key in
EMPLOYEE relation.
 Table that contain candidate key
is called referenced relation and
 The table containing foreign key
is called referencing relation.
 So, the relation DEPARTMENT is
a referenced relation and
 The relation EMPLOYEE is a
referencing relation.
THE INSERT OPERATION :
 Insertion in a Referenced Relation (DEPARTMENT)
 If we insert a tuple in the referenced relation , then it causes no
violation.
 For example, Insertion of <13, ‘Karnal’> in DEPARTMENT relation is
allowed because insertion of this tuple causes no violation.
INSERTION IN A REFERENCING RELATION -(EMPLOYEE)

 The insert operation in a referencing relation can violate any of


four types of constraints –
 Insertion of the value
<5, ‘Pooja’, 10, 11> into EMPLOYEE table having domain
constraints as the employee age cannot be less than 18 years?
INSERTION IN A REFERENCING RELATION -(EMPLOYEE)

 Domain Constraints –
Insertion of the value
<5, ‘Pooja’, 10, 11> into EMPLOYEE table is not allowed,
Because this insertion violates the domain constraints as the employee
age cannot be less than 18 years.
 Key Constraints –
 Insertion of the value <3, ‘Anuja’, 19, 11> into EMPLOYEE
table?
 Key Constraints –
 Insertion of the value <3, ‘Anuja’, 19, 11> into EMPLOYEE
table is not allowed, Because this insertion violates the key
constraints as an employee with ENO(Primary Key) 3 already
exists.
 Entity Constraints –
 Insertion
of the value <NULL, ‘Kavya’, 21, 10> into
EMPLOYEE table?
 Entity Constraints –
 Insertion of the value <NULL, ‘Kavya’, 21, 10> into
EMPLOYEE table is not allowed, Because this insertion
violates the Entity Integrity constraints as the primary
key(ENO) cannot contain a null value.
 Referential Integrity Constraints –
 Insertion of the value <6, ‘Ajit’, 19, 16> into EMPLOYEE table ?
 Referential Integrity Constraints –
 Insertion of the value <6, ‘Ajit’, 19, 16> into EMPLOYEE table is
not allowed, Because this insertion violates the Referential Integrity
constraints as there is no row or tuple with DNO=16 exists in
DEPARTMENT relation.
DATABASE OPERATIONS AND DEALING
WITH CONSTRAINT VIOLATIONS- THE
DELETE OPERATION

 The Delete Operation :


 Consider two existing relations named EMPLOYEE and DEPARTMENT.

 The Delete Operation violates only referential Integrity Constraints or


Integrity Rule 2.
 Deletion in a Referencing Relation (EMPLOYEE
relation)
 Ifwe delete a tuple from the referencing relation, then it
causes no violation.
 For example, Deletion of <3, ‘Somvir’, 22, 10> from
EMPLOYEE relation is allowed because deletion of this
tuple causes no violation.
 Deletion in a Referenced Relation (DEPARTMENT relation)
 There are three options available if a deletion causes violation
i) Reject the Deletion – (ON DELETE NO ACTION)
It prevents deleting a parent when there are children. It is the
the Default Constraint.

For example, Deletion of <10, ‘Rohtak’> from DEPARTMENT


relation is not allowed because deletion of this tuple will violate referential
integrity as in the EMPLOYEE relation, the tuple with DNO=10 causes
problem. And therefore, reject the deletion.
2) Cascade Deletion – (ON DELETE CASCADE)
If deletion causes integrity violation, then delete from both the table i.e. if the
tuples are deleted from the referenced table, then the tuple will also be deleted
from the referencing relation that is being deleted.
For example, Deletion of <10, ‘Rohtak’> from DEPARTMENT relation will
delete the following tuples in EMPLOYEE relation :
<1, ‘Ankit’, 19, 10>
<3, ‘Somvir’, 22, 10>
<4, ‘Sourabh’, 19, 10>
3) Modify the referencing Attributes – (ON DELETE SET NULL)
sets null value or some valid value in the foreign key field for corresponding deleting
referenced value. i.e. changing the referencing attribute values that cause the violation
either null or another valid value.
If there is no restriction or constraint applied for putting the NULL value in the
referencing relation – then allow to delete from referenced relation otherwise prohibited.
For example, Deletion of <10, ‘Rohtak’> from DEPARTMENT relation will delete the
following tuples in EMPLOYEE relation :
<1, ‘Ankit’, 19, 10>
<3, ‘Somvir’, 22, 10>
<4, ‘Sourabh’, 19, 10>
and null values will be filled
in the EMPLOYEE relation
on that place. So the relations
will look like this :
UPDATE OPERATIONS AND DEALING WITH CONSTRAINT
VIOLATIONS –
 The Update Operation :
 Consider two existing relationsnamed EMPLOYEE and DEPARTMENT.
1) Updation in a referencing relation –
updation of referencing attribute may causes referential integrity
violation. The Updation restricted if causes violation.
For Example

If there is no violation,
then updation will
be allowed.
2) Updation in a referenced relation –
There are again three options available if an updation causes violation –
Reject the updation – (ON UPDATE NO ACTION) –
It prevents updating a parent when there are children. It is the the Default
Constraint. For example,
 B) Cascade Updation – (ON UPDATE CASCADE)
 If updation causes integrity violation, then update in both the table i.e. if the tuples
are updated from the referenced table, then the tuple will also be updates from the
referencing relation that is being updated.
c)Modify the referencing Attributes – (ON UPDATE SET NULL
sets null value or some valid value in the foreign key field for corresponding updating
referenced value. i.e. changing/updating the referencing attribute values that cause the
violation either null or another valid value.
If there is no restriction or constraint applied for putting the NULL value in the
referencing relation – then allow to update from referenced relation otherwise prohibited.

You might also like