5. Relational Data Model and Relational Database Constraints
5. Relational Data Model and Relational Database Constraints
1
What is a data model?
Data model
An integrated collection of concepts for
describing data, relationships between
data, and constraints on the data used by
an organization.
A model
Is a representation of ‘real world’ objects
and events, and their associations.
It concentrates on the essential, inherent
aspects of an organization and ignores the
accidental properties.
2
A data model can be thought of as
comprising three components:
(1) A structural part, consisting of a set of
rules that define how the database is to be
constructed;
(2) A manipulative part, defining the types
of operations (transactions) that are
allowed on the data (this includes the
operations that are used for updating or
retrieving data and for changing the
structure of the database);
(3) Possibly a set of integrity rules, which
ensures that the data is accurate. 3
Relational Model
Concepts
The relational model represents the
database as a collection of relations
The relational Model of Data is based on
the concept of a Relation
When a relation is thought of as a table of
values, each row in the table represents a
collection of related data values.
The table name and column names are
used to help in interpreting the meaning of
the values in each row
A Relation is a mathematical concept
based on the ideas of sets 4
Relational Model Concepts
For example,
The given table below is called STUDENT because
each row represents facts about a particular student
entity.
5
Informal Definitions
Slide 5- 7
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
Slide 5- 13
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> }
15
Relational Model
• It was proposed by E.F. Codd in 1970 to model data
in the form of relations or tables.
• It represents how data is stored in Relational
Databases.
• A relational database stores data in the form of
relations.
• Each relation has columns and rows which are
formally called attributes and tuples respectively.
• Each tuple in relation is a real-world entity.
16
Relational Model Diagram
― The figure below will help you identify the relation,
attributes, and tuples in a relational model.
― It is an Employee relation and it is having entries of
6 employees (tuples) in it.
17
Terminology
― Relations / Tables – Relations are saved in the table
format. A table has two properties rows and columns.
Rows represent records and columns represent
attributes.
― Attribute: Attributes are the properties which define a
relation.
― Tuple: It is nothing but a single row of a table.
― Degree / arity: The total number of attributes in a
relation.
― Cardinality: Total number of rows present in the Table.
― Domain: Set of possible values for an attribute
― Relation instance: It is a finite set of tuples. They never
have duplicate tuples.
― Relation Schema: It represents the name of the relation
with its attributes. 18
Characteristics of a relation
―Each relation in a database must have
a distinct or unique name.
―Each attribute must have a distinct
name (no two attributes should have
the same name).
―Duplicate tuples must not be present
in a relation.
―Each tuple must have exactly one
data value for an attribute.
―No ordering is required for tuples,
19
RELATIONAL MODEL
CONSTRAINTS
Constraint means a condition.
Therefore database tables or
relations are created based on
constraints or conditions
Constraints are conditions that must
hold on all valid relation states.
20
Relational Integrity Constraints
― They are referred to as conditions that are applied
to the table to ensure the correctness of the data in
the database.
― These constraints are checked before performing
any operation in database. If there is a violation in
any of constrains, operation will fail.
― Thus, integrity constraint is used to guard against
accidental damage to the database.
― They are mostly divided into the following main
categories:
― Key Constraints
― Domain Constraints
― Entity Integrity Constraints
― Referential Integrity Constraints
21
Key Constraints
A Key Constraint is a statement that a certain
minimal subset of the fields of a relation is a unique
identifier for a tuple.
The following are the types of key constraints-
1. Super Key
2. Candidate key.
3. Primary key
4. Secondary or Alternative key
5. unique key
6. Foreign key.
22
Example:
Relation: STUDENT
STUD_NO NAME AGE MOBILE_NO
512 Akshay 20 9876723452
513 Krishna 23 9991165674
546 Vivek 20 7898756543
557 Raghu 21 8987867898
595 Krishna 22 9876543210
Relation: STUDENT-COURSE
STUD_NO COURSE_NO COURSE_NAME
512 C1 DBMS
546 C2 PYTHON
512 C2 PYTHON 23
Super Key
― A single attribute or set of attributes that can be
uniquely identify all attributes of a particular
relation is called supper key.
― Super key contains one or more than one attribute
24
Candidate Key
― The minimal set of attributes which can uniquely
identify a tuple is known as a candidate key. For
example, STUD_NO in STUDENT relation.
― A candidate key can never be NULL or empty and its value should
be unique.
― Candidate keys are selected from are selected from the set of
super keys, the only thing we take care while selecting candidate
key is: It should not have any redundant attributes.
― There can be more than one candidate keys for
table. For example, STUD_NO, as well as
MOBILE_NO both, are candidate keys for relation
STUDENT.
26
Primary Key
― A primary key is being selected from the group of candidate
keys
― Primary Key (cannot have null value) - attribute(s) that uniquely
identify a tuple in a table.
― There can be more than one candidate key in a relation out of
which one can be chosen as a primary key.
― For Example, STUD_NO as well as MOBILE_NO both are
candidate keys for relation STUDENT but STUD_NO can be
chosen as primary key.
Properties of Primary Keys
― A relation can have only one primary key
― Each value in a primary key must be unique
― Primary key cannot contain NULL values
27
Note:
―Super Key is the broadest unique
identifier;
―Candidate Key is a subset of
super key;
―Primary Key is a subset of
candidate key
28
Secondary or Alternative key
―The candidate key other than the primary key
is called an alternate key / Secondary key.
29
Unique Key
Unique key is the same as primary with the
difference being the existence of null value.
Unique key field allows one value as NULL value.
30
Foreign Key
― Foreign Keys are the columns of a table that point to
the primary key of another table
― Key used to create a relationship between two tables
― Here in below tables DeptIDof Department table is
Primary Key where as DeptIDof Employee is an
Foreign key.
― It means it has referred to another table. This concept
is also know as Referential Integrity.
31
Entity integrity constraint
― Entity integrity constraint ensures that the primary key attribute
in a relation, should not accept a null value.
34
Domain Constraint
• Domain constraint defines the domain or set of values
for an attribute.
• It specifies that the value taken by the attribute must
be from its domain only.
• Example: Consider the following Student table
Roll_no Name Age
S001 Akshay 20
S013 Krishna 23
S056 Vivek Z
S054 Raghu 21
35
Discussion Questions
Define the following terms as they apply to the relational model of data:
i. Domain
ii. Attribute
iii. N-tuple
iv. Relation schema
v. Relation state
vi. Degree of a relation
vii. Relational database schema
viii. Relational database state
ix. Why are tuples in a relation not ordered?
x. Why are duplicate tuples not allowed in a relation?
xi. What is the difference between a key and a super key?
xii. Why do we designate one of the candidate keys of a relation to be the
primary key?
xiii. Discuss the characteristics of relations that make them different from
ordinary tables and files.
xiv. Discuss the various reasons that lead to the occurrence of NULL values in
relations.
xv. Discuss the entity integrity and referential integrity constraints. Why each is
considered important?
xvi. Define foreign key. What is this concept used for?
xvii. What is a transaction? How does it differ from an Update operation? 36
Any Queries?
37
Thank you
38