CHAPTER 3

Degree of relationship
Degree of relationship refers to the number
of participating entities in a relationship.

Dr.Girija Narasimhan

1
Unary Relationship

Subjects may be prerequisites for
other subjects.

Dr.Girija Narasimhan

2
M:N unary relationship: A Subject may have
many other Subjects as prerequisites and each
Subject may be a prerequisite to many other
Subjects

1:M unary relationship:
An Employee may manage many Employees,
but an Employee is managed by only one
Employee

1:1 unary relationship:
A Person may be married to only one Person.

Dr.Girija Narasimhan

3
Binary Relationship
If there are two entities involved in relationship then it is
referred to as binary relationship.

Dr.Girija Narasimhan

4
Ternary Relationship

If there are three entities involved then it is called as ternary relationship and so on.

The University might need to record which
teachers taught which subjects in which
courses.
Dr.Girija Narasimhan

5
The cardinality of a relationship is the number of instances of entity B that can be
associated with entity A. There is a minimum cardinality and a maximum cardinality for
each relationship, with an unspecified maximum cardinality being shown as
N. Cardinality limits are usually derived from the organizations policies or external
constraints.

At the University, each Teacher can teach an
unspecified maximum number of subjects as
long as his/her weekly hours do not exceed 24
(this is an external constraint set by an
industrial award). Teachers may teach 0
subjects if they are involved in non teaching
projects. Therefore, the cardinality limits for
TEACHER are (O,N).

Dr.Girija Narasimhan

The University's policies state that each
Subject is taught by only one teacher,
but it is possible to have Subjects that
have not yet been assigned a
teacher. Therefore, the cardinality limits
for SUBJECT are (0,1).

6
Relation

Relation schema

Employee Table

Relation (R )
Attributes
(A1,A2…An)

Employee (Emp_id,ename,Salary)
Foreign key

Primary key or identifier

Employee (Emp_id,ename,Salary)

Employee (Emp_id,ename,Salary,dept_name)
Foreign key

Composite key

Department(dept_name,ename,Salary)

Over_time (emp_id, dept_name,No_of_hour)

Composite key
Dr.Girija Narasimhan

7
INTEGRITY CONSTRAINTS
DOMAIN
CONSTRAINTS

ENTITY Integrity
CONSTRAINTS

Dr.Girija Narasimhan

Referential
Integrity
CONSTRAINTS

8
DOMAIN CONSTRAINTS

Domain_name

Meaning

Data type

Age

Years of the person

Phone_no

Conduct number

Mark

size

number

3

number

Scored by Student
in Exam

number

Dr.Girija Narasimhan

8

3

Allowable value or range
<=110
Must be 8 digit and
<99999999 and
>90000000

>=0 and <=100

9
ENTITY Integrity CONSTRAINTS
Every relation must have primary key
Every primary key attribute is non- null and unique

Unique means, value of the primary key must
be different not be same.
Null is a value, suppose no other value not assign or apply.
Null is not zero (0) or blank (“ “)

Dr.Girija Narasimhan

10
ENTITY Integrity CONSTRAINTS
Not Unique
Department

Entity integrity not enforced

Not Unique

Same value

Dr.Girija Narasimhan

11
ENTITY Integrity CONSTRAINTS

Null is a value, suppose no other value not assign or apply.

Null is not zero (0) or blank (“ “)
Not Null

Entity integrity not enforced

No null value allowed in the primary key

Dr.Girija Narasimhan

12
Referential Integrity
CONSTRAINTS
• Two tables have relationship or association then use foreign key
Why Referential Integrity constraints?
To maintain Consistency among rows of two relationship.

Rule:
Rule 1: Foreign key must match with primary key of the other
relation.
Rule 2: Otherwise, If it is not matching then Foreign key value must
be “NULL”

Dr.Girija Narasimhan

13
Referential Integrity CONSTRAINTS
EMPLOYEE
Foreign key
Department

EMPLOYEE
Repetition in the foreign
key - allowed

Null value allowed
in the foreign key
Dr.Girija Narasimhan

14
EMPLOYEE

Referential Integrity CONSTRAINTS
Department

Not matching any dept_name in
department table.
So Referential integrity not
enforced

Dr.Girija Narasimhan

15
Referential Integrity CONSTRAINTS
Rule 1: Foreign key must match with primary key of the other relation.

CASCADING DELETE

PROHIBIT DELETE

Dr.Girija Narasimhan

16
Referential Integrity CONSTRAINTS

Dr.Girija Narasimhan

17
CASCADING DELETE
If delete the Primary key table record, automatically the foreign key referencing
table record information also deleted.
For example: Suppose if I delete deptno 20 in DEPT table (master /parent),
automatically in the EMP table empno 6666 and Empno 7329 also deleted
because both are referencing deptno 20.

Dr.Girija Narasimhan

18
PROHIBIT DELETE
Before deleting primary key record, first delete all the foreign key associated
records in referencing table or child table.
For example: First delete EMP table empno 6666 and Empno 7329 and
then delete DEPT table deptno 20

Dr.Girija Narasimhan

19
Rule 2: Otherwise, If it is not matching then Foreign key value must be “NULL”
Place a null value in the Foreign key.
When?
Suppose I want to remove deptno 20 from DEPT TABLE, but I want to retain
or keep the employee 6666 and 7329 in the EMP table , then in the
foreign key attribute DEPTNO column in EMP table instead of 20 I will put
“Null” in both the record 6666 and 7329.

Dr.Girija Narasimhan

20
Mapping ER diagrams to Relations

Why?
Transform Conceptual model to Relational model

Entity type
Regular

Week entity type
Create a new relation

Composite

Binary Relations

Unary Relation

1-1 relation type
1-N relation type
M-N relation type

Multivalued

Dr.Girija Narasimhan

21
Mapping ER diagrams to Relations
Entity type
Regular

Dr.Girija Narasimhan

22
Mapping ER diagrams to Relations
Entity type

Dr.Girija Narasimhan

Composite

23
Mapping ER diagrams to Relations
Entity type

Dr.Girija Narasimhan

Multi valued

24
Mapping Weak Entities

weak entity type does not have an independent existence, but exists only through an
identifying relationship with another entity type called the owner or strong entity.

Dr.Girija Narasimhan

25
Mapping 1-1 Binary Relationship Type
Employee must assign
by only one
Employee may manage one or may
department.
not manage any department

One and only One (Mandatory One)

Zero or One (Optional One)

Using foreign key

Dr.Girija Narasimhan

26
Mapping 1-N Binary Relationship Type

Department have one
or more employee

One or More (Mandatory One)

Employee must work only
one department.

One and only One (Mandatory One)

Dr.Girija Narasimhan

27
Mapping M-N Binary Relationship Type

Employee join one
or more project

Project has one
more employee

One or More (Mandatory One)

One or More (Mandatory One)

Dr.Girija Narasimhan

28
Mapping Unary Relation Type
one employee appointed
as supervisor

Dr.Girija Narasimhan

29

Database relationship

  • 1.
    CHAPTER 3 Degree ofrelationship Degree of relationship refers to the number of participating entities in a relationship. Dr.Girija Narasimhan 1
  • 2.
    Unary Relationship Subjects maybe prerequisites for other subjects. Dr.Girija Narasimhan 2
  • 3.
    M:N unary relationship:A Subject may have many other Subjects as prerequisites and each Subject may be a prerequisite to many other Subjects 1:M unary relationship: An Employee may manage many Employees, but an Employee is managed by only one Employee 1:1 unary relationship: A Person may be married to only one Person. Dr.Girija Narasimhan 3
  • 4.
    Binary Relationship If thereare two entities involved in relationship then it is referred to as binary relationship. Dr.Girija Narasimhan 4
  • 5.
    Ternary Relationship If thereare three entities involved then it is called as ternary relationship and so on. The University might need to record which teachers taught which subjects in which courses. Dr.Girija Narasimhan 5
  • 6.
    The cardinality ofa relationship is the number of instances of entity B that can be associated with entity A. There is a minimum cardinality and a maximum cardinality for each relationship, with an unspecified maximum cardinality being shown as N. Cardinality limits are usually derived from the organizations policies or external constraints. At the University, each Teacher can teach an unspecified maximum number of subjects as long as his/her weekly hours do not exceed 24 (this is an external constraint set by an industrial award). Teachers may teach 0 subjects if they are involved in non teaching projects. Therefore, the cardinality limits for TEACHER are (O,N). Dr.Girija Narasimhan The University's policies state that each Subject is taught by only one teacher, but it is possible to have Subjects that have not yet been assigned a teacher. Therefore, the cardinality limits for SUBJECT are (0,1). 6
  • 7.
    Relation Relation schema Employee Table Relation(R ) Attributes (A1,A2…An) Employee (Emp_id,ename,Salary) Foreign key Primary key or identifier Employee (Emp_id,ename,Salary) Employee (Emp_id,ename,Salary,dept_name) Foreign key Composite key Department(dept_name,ename,Salary) Over_time (emp_id, dept_name,No_of_hour) Composite key Dr.Girija Narasimhan 7
  • 8.
  • 9.
    DOMAIN CONSTRAINTS Domain_name Meaning Data type Age Yearsof the person Phone_no Conduct number Mark size number 3 number Scored by Student in Exam number Dr.Girija Narasimhan 8 3 Allowable value or range <=110 Must be 8 digit and <99999999 and >90000000 >=0 and <=100 9
  • 10.
    ENTITY Integrity CONSTRAINTS Everyrelation must have primary key Every primary key attribute is non- null and unique Unique means, value of the primary key must be different not be same. Null is a value, suppose no other value not assign or apply. Null is not zero (0) or blank (“ “) Dr.Girija Narasimhan 10
  • 11.
    ENTITY Integrity CONSTRAINTS NotUnique Department Entity integrity not enforced Not Unique Same value Dr.Girija Narasimhan 11
  • 12.
    ENTITY Integrity CONSTRAINTS Nullis a value, suppose no other value not assign or apply. Null is not zero (0) or blank (“ “) Not Null Entity integrity not enforced No null value allowed in the primary key Dr.Girija Narasimhan 12
  • 13.
    Referential Integrity CONSTRAINTS • Twotables have relationship or association then use foreign key Why Referential Integrity constraints? To maintain Consistency among rows of two relationship. Rule: Rule 1: Foreign key must match with primary key of the other relation. Rule 2: Otherwise, If it is not matching then Foreign key value must be “NULL” Dr.Girija Narasimhan 13
  • 14.
    Referential Integrity CONSTRAINTS EMPLOYEE Foreignkey Department EMPLOYEE Repetition in the foreign key - allowed Null value allowed in the foreign key Dr.Girija Narasimhan 14
  • 15.
    EMPLOYEE Referential Integrity CONSTRAINTS Department Notmatching any dept_name in department table. So Referential integrity not enforced Dr.Girija Narasimhan 15
  • 16.
    Referential Integrity CONSTRAINTS Rule1: Foreign key must match with primary key of the other relation. CASCADING DELETE PROHIBIT DELETE Dr.Girija Narasimhan 16
  • 17.
  • 18.
    CASCADING DELETE If deletethe Primary key table record, automatically the foreign key referencing table record information also deleted. For example: Suppose if I delete deptno 20 in DEPT table (master /parent), automatically in the EMP table empno 6666 and Empno 7329 also deleted because both are referencing deptno 20. Dr.Girija Narasimhan 18
  • 19.
    PROHIBIT DELETE Before deletingprimary key record, first delete all the foreign key associated records in referencing table or child table. For example: First delete EMP table empno 6666 and Empno 7329 and then delete DEPT table deptno 20 Dr.Girija Narasimhan 19
  • 20.
    Rule 2: Otherwise,If it is not matching then Foreign key value must be “NULL” Place a null value in the Foreign key. When? Suppose I want to remove deptno 20 from DEPT TABLE, but I want to retain or keep the employee 6666 and 7329 in the EMP table , then in the foreign key attribute DEPTNO column in EMP table instead of 20 I will put “Null” in both the record 6666 and 7329. Dr.Girija Narasimhan 20
  • 21.
    Mapping ER diagramsto Relations Why? Transform Conceptual model to Relational model Entity type Regular Week entity type Create a new relation Composite Binary Relations Unary Relation 1-1 relation type 1-N relation type M-N relation type Multivalued Dr.Girija Narasimhan 21
  • 22.
    Mapping ER diagramsto Relations Entity type Regular Dr.Girija Narasimhan 22
  • 23.
    Mapping ER diagramsto Relations Entity type Dr.Girija Narasimhan Composite 23
  • 24.
    Mapping ER diagramsto Relations Entity type Dr.Girija Narasimhan Multi valued 24
  • 25.
    Mapping Weak Entities weakentity type does not have an independent existence, but exists only through an identifying relationship with another entity type called the owner or strong entity. Dr.Girija Narasimhan 25
  • 26.
    Mapping 1-1 BinaryRelationship Type Employee must assign by only one Employee may manage one or may department. not manage any department One and only One (Mandatory One) Zero or One (Optional One) Using foreign key Dr.Girija Narasimhan 26
  • 27.
    Mapping 1-N BinaryRelationship Type Department have one or more employee One or More (Mandatory One) Employee must work only one department. One and only One (Mandatory One) Dr.Girija Narasimhan 27
  • 28.
    Mapping M-N BinaryRelationship Type Employee join one or more project Project has one more employee One or More (Mandatory One) One or More (Mandatory One) Dr.Girija Narasimhan 28
  • 29.
    Mapping Unary RelationType one employee appointed as supervisor Dr.Girija Narasimhan 29