Relational Model
Rajeev Srivastava
ToC
• Relation
• Tuples
• Cardinality
• Attribute
• Degree/Arity
• Domains
• Relation vs. Table
2
• Candidate Key
• Super Key
• Primary Key
• Composite Key
• Foreign Key
• Integrity Rules
RAJEEVS@CDAC.IN
Relation
In Relational Model data is modelled in form of Relations represented by tabular
structure.
Consider the relation EMPLOYEE represented by the following table:
3
EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode
RAJEEVS@CDAC.IN
Tuples in a Relation
A relation is a set of tuples; each row here is a tuple :
4
EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode
1
2
3
4
5
7
6
RAJEEVS@CDAC.IN
Cardinality of a Relation
No of Tuples in a Relation at a point in time.
Cardinality = 7
5
EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode
1
2
3
4
5
7
6
RAJEEVS@CDAC.IN
Attribute in a Relation
An attribute represents a quality/information about an entity.
A tuple consists of Attribute values.
6
EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode
RAJEEVS@CDAC.IN
Degree (=Arity) of a Relation
A degree or arity of a Relation is the number of attributes in it.
Degree = 8
7
EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode
RAJEEVS@CDAC.IN
Domains
Each attribute has a domain associated with it.
Attribute values in a relation are restricted to the values from its domain.
8
EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode
DEPT
ACCO
PURC
COUR
DESIG
PE
TO
STO
RAJEEVS@CDAC.IN
Consider the Employee relation defined as :
create table EMPLOYEE(
EmpCode integer(4),
Name char(30),
Desig char(4),
Grade integer(4),
JoinDate date,
Basic integer(7),
Gender char(1),
DeptCode char(4) )
9RAJEEVS@CDAC.IN
Domains of Attributes of Employee Relation :
EmpCode set of all 4-digit numbers
Name set of all 30-alpha characters
Desig set of all designation codes
Grade set of all grade values
JoinDate set of all dates (in a given range)
Basic set of all possible values for basic
Gender set {'M','F‘, ‘T’}
DeptCode set of all dept codes
10RAJEEVS@CDAC.IN
A Relation may be represented as a Table where
11
Relation Table
Tuple Row/Record
Attribute Column
Degree/Arity No of Columns in the table
Cardinality No or Rows in the table
Domain Pool of acceptable values for a
column
Primary Key Unique Identifier
RAJEEVS@CDAC.IN
But, a Relation is not a Table, because :
• A table has an inherent order for rows; there is no concept of order for tuples
in a relation.
• A relation must have a primary key; a table need not have an identifier.
• The tuples in a relation must be unique; there is no such restriction for tables
12RAJEEVS@CDAC.IN
Relation : Observations
• A Relation is a set of tuples.
• A Relation is time-variant.
• A Relation cannot have duplicate tuples.
• Tuples in a Relation are unordered.
• Values are Atomic.
• Attribute values are of same kind.
• A Relation is a subset of the Cartesian Product of a set of domains.
13RAJEEVS@CDAC.IN
Candidate Key & Super Key
A set of attributes is said to be Candidate Key if and only if it satisfies the
following time-independent properties:
Uniqueness property: No two distinct tuples have the same value for the key.
Minimality property: None of the attributes of the key can be discarded from
the key without destroying the uniqueness property.
A Super Key is a Non-Minimal Candidate Key. Its a set of one or more columns in
a table for which no two rows can have the exact same values.
An Alternate Key is a candidate key that is not the primary key.
14RAJEEVS@CDAC.IN
Candidate Key?
create table EMPLOYEE(
EmpCode integer(4),
Name char(30),
Desig char(4),
Grade integer(4),
JoinDate date,
Basic integer(7),
Gender char(1),
DeptCode char(4) )
15RAJEEVS@CDAC.IN
Candidate Key?
create table EMPLOYEE (
EmpCode integer(4),
Name char(30),
Desig char(4),
Grade integer(4),
JoinDate date,
Basic integer(7),
Gender char(1),
DeptCode char(4),
Email char(100),
MobileNo char(16) )
16RAJEEVS@CDAC.IN
Primary Key
is a candidate key that have following two qualities -
• Uniquely identifies a tuple in a relation
• Must NOT be NULL
*Should be selected from candidate keys such that it never/rarely changes.
17RAJEEVS@CDAC.IN
Primary Key?
create table EMPLOYEE (
EmpCode integer(4),
Name char(30),
Desig char(4),
Grade integer(4),
JoinDate date,
Basic integer(7),
Gender char(1),
DeptCode char(4),
Email char(100),
MobileNo char(16) )
18RAJEEVS@CDAC.IN
Composite Key
• A candidate key with two or more attributes that uniquely identifies the tuple in a
Relation.
• Also called as compound key
Composite Primary Key
• A primary key which is a composite key is called as Composite Primary Key.
19RAJEEVS@CDAC.IN
Can we have more than one primary key
in a table?
No. We can not.
People who are new to RDBMS are often get confused with the restriction of single Primary Key
in a table and the concept of Composite Key. To clarify the same -
A table can have only one Primary Key.
The Primary Key can be defined on a single column or more than one columns. If the Primary
Key is defined using more that one columns, it is known as a Composite Key (or Composite
Primary Key).
Therefore, a Composite Key in a table does not mean that there are more than one Primary Keys
in the table. Instead, a Composite Key uses more than one columns to define a (Single) Primary
Key.
RAJEEVS@CDAC.IN 20
Foreign Key
• A Foreign Key is a set of attributes in one relation whose values are required to match
one of the values of the primary key of the same or different relation.
• There can be more than one foreign key in a given relation.
21RAJEEVS@CDAC.IN
Identify a relation in any system/business, define its
Attributes, Domain for each attribute and find out Primary,
Key, Foreign Keys, Candidate Keys, Super Key in the relation.
Foreign Key(s)?
create table EMPLOYEE(
EmpCode integer(4),
Name char(30),
Desig char(4),
Grade integer(4),
JoinDate date,
Basic integer(7)),
Gender char(1),
DeptCode char(4) )
create table DEPT(
DeptCode char(4),
DeptName char(30),
Location char(10) )
22RAJEEVS@CDAC.IN
Integrity Rules
Entity Integrity: Implemented through Primary Key
“No Attribute participating in the primary key of a relation may accept null
values”
Guarantees that each tuple will have a unique identity.
Referential Integrity: Implemented through Foreign Key
“Values of the foreign key (a) must be either null, or (b) if non-null, must match
with the primary key value of some tuple of the `parent’ relation. The reference
can be to the same relation”
*Foreign Key is also know as Reference/Referential key.
23RAJEEVS@CDAC.IN
Relational Database Operations
• SELECTION: This selects some or all of the records in a table.
Through WHERE Clause
• PROJECTION: This limits columns from a table.
Through SELECT Clause
• UNION
• INTERSECTION
• DIFFERENCE
• JOIN
• CARTESIAN PRODUCT
RAJEEVS@CDAC.IN 24

Relational Model - An Introduction

  • 1.
  • 2.
    ToC • Relation • Tuples •Cardinality • Attribute • Degree/Arity • Domains • Relation vs. Table 2 • Candidate Key • Super Key • Primary Key • Composite Key • Foreign Key • Integrity Rules [email protected]
  • 3.
    Relation In Relational Modeldata is modelled in form of Relations represented by tabular structure. Consider the relation EMPLOYEE represented by the following table: 3 EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode [email protected]
  • 4.
    Tuples in aRelation A relation is a set of tuples; each row here is a tuple : 4 EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode 1 2 3 4 5 7 6 [email protected]
  • 5.
    Cardinality of aRelation No of Tuples in a Relation at a point in time. Cardinality = 7 5 EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode 1 2 3 4 5 7 6 [email protected]
  • 6.
    Attribute in aRelation An attribute represents a quality/information about an entity. A tuple consists of Attribute values. 6 EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode [email protected]
  • 7.
    Degree (=Arity) ofa Relation A degree or arity of a Relation is the number of attributes in it. Degree = 8 7 EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode [email protected]
  • 8.
    Domains Each attribute hasa domain associated with it. Attribute values in a relation are restricted to the values from its domain. 8 EmpCode Name Desig Grade JoinDate BasicSalary Gender DeptCode DEPT ACCO PURC COUR DESIG PE TO STO [email protected]
  • 9.
    Consider the Employeerelation defined as : create table EMPLOYEE( EmpCode integer(4), Name char(30), Desig char(4), Grade integer(4), JoinDate date, Basic integer(7), Gender char(1), DeptCode char(4) ) [email protected]
  • 10.
    Domains of Attributesof Employee Relation : EmpCode set of all 4-digit numbers Name set of all 30-alpha characters Desig set of all designation codes Grade set of all grade values JoinDate set of all dates (in a given range) Basic set of all possible values for basic Gender set {'M','F‘, ‘T’} DeptCode set of all dept codes [email protected]
  • 11.
    A Relation maybe represented as a Table where 11 Relation Table Tuple Row/Record Attribute Column Degree/Arity No of Columns in the table Cardinality No or Rows in the table Domain Pool of acceptable values for a column Primary Key Unique Identifier [email protected]
  • 12.
    But, a Relationis not a Table, because : • A table has an inherent order for rows; there is no concept of order for tuples in a relation. • A relation must have a primary key; a table need not have an identifier. • The tuples in a relation must be unique; there is no such restriction for tables [email protected]
  • 13.
    Relation : Observations •A Relation is a set of tuples. • A Relation is time-variant. • A Relation cannot have duplicate tuples. • Tuples in a Relation are unordered. • Values are Atomic. • Attribute values are of same kind. • A Relation is a subset of the Cartesian Product of a set of domains. [email protected]
  • 14.
    Candidate Key &Super Key A set of attributes is said to be Candidate Key if and only if it satisfies the following time-independent properties: Uniqueness property: No two distinct tuples have the same value for the key. Minimality property: None of the attributes of the key can be discarded from the key without destroying the uniqueness property. A Super Key is a Non-Minimal Candidate Key. Its a set of one or more columns in a table for which no two rows can have the exact same values. An Alternate Key is a candidate key that is not the primary key. [email protected]
  • 15.
    Candidate Key? create tableEMPLOYEE( EmpCode integer(4), Name char(30), Desig char(4), Grade integer(4), JoinDate date, Basic integer(7), Gender char(1), DeptCode char(4) ) [email protected]
  • 16.
    Candidate Key? create tableEMPLOYEE ( EmpCode integer(4), Name char(30), Desig char(4), Grade integer(4), JoinDate date, Basic integer(7), Gender char(1), DeptCode char(4), Email char(100), MobileNo char(16) ) [email protected]
  • 17.
    Primary Key is acandidate key that have following two qualities - • Uniquely identifies a tuple in a relation • Must NOT be NULL *Should be selected from candidate keys such that it never/rarely changes. [email protected]
  • 18.
    Primary Key? create tableEMPLOYEE ( EmpCode integer(4), Name char(30), Desig char(4), Grade integer(4), JoinDate date, Basic integer(7), Gender char(1), DeptCode char(4), Email char(100), MobileNo char(16) ) [email protected]
  • 19.
    Composite Key • Acandidate key with two or more attributes that uniquely identifies the tuple in a Relation. • Also called as compound key Composite Primary Key • A primary key which is a composite key is called as Composite Primary Key. [email protected]
  • 20.
    Can we havemore than one primary key in a table? No. We can not. People who are new to RDBMS are often get confused with the restriction of single Primary Key in a table and the concept of Composite Key. To clarify the same - A table can have only one Primary Key. The Primary Key can be defined on a single column or more than one columns. If the Primary Key is defined using more that one columns, it is known as a Composite Key (or Composite Primary Key). Therefore, a Composite Key in a table does not mean that there are more than one Primary Keys in the table. Instead, a Composite Key uses more than one columns to define a (Single) Primary Key. [email protected] 20
  • 21.
    Foreign Key • AForeign Key is a set of attributes in one relation whose values are required to match one of the values of the primary key of the same or different relation. • There can be more than one foreign key in a given relation. [email protected] Identify a relation in any system/business, define its Attributes, Domain for each attribute and find out Primary, Key, Foreign Keys, Candidate Keys, Super Key in the relation.
  • 22.
    Foreign Key(s)? create tableEMPLOYEE( EmpCode integer(4), Name char(30), Desig char(4), Grade integer(4), JoinDate date, Basic integer(7)), Gender char(1), DeptCode char(4) ) create table DEPT( DeptCode char(4), DeptName char(30), Location char(10) ) [email protected]
  • 23.
    Integrity Rules Entity Integrity:Implemented through Primary Key “No Attribute participating in the primary key of a relation may accept null values” Guarantees that each tuple will have a unique identity. Referential Integrity: Implemented through Foreign Key “Values of the foreign key (a) must be either null, or (b) if non-null, must match with the primary key value of some tuple of the `parent’ relation. The reference can be to the same relation” *Foreign Key is also know as Reference/Referential key. [email protected]
  • 24.
    Relational Database Operations •SELECTION: This selects some or all of the records in a table. Through WHERE Clause • PROJECTION: This limits columns from a table. Through SELECT Clause • UNION • INTERSECTION • DIFFERENCE • JOIN • CARTESIAN PRODUCT [email protected] 24