Lec 11 (Extra) - Relational Model
Lec 11 (Extra) - Relational Model
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
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)
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
4 Microsoft Active
12
RELATION
• Relational Database schema: A collection of relations
S={R1,R2,…,Rm}, each one consistent with its specified
relational schema.
Table Relation
Column Attribute/Domain
Row Tuple
Values in a column Domain
Table Definition Schema of Relation
Populated Table Extension
17
CHARACTERISTICS OF RELATIONS
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
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
• Student(Roll_No, Name)
Attribute does not apply to this tuple (also known as value undefined)
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
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>
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,
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
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.