29
Degree of relationship refers to the number of participating entities in a relationship. Degree of relationship 1 CHAPTER 3 Dr.Girija Narasimhan

Database relationship

  • Upload
    hct

  • View
    190

  • Download
    1

Embed Size (px)

DESCRIPTION

database, degree of relationship, entity, attribute, binary, unary, tenary, cascade delete, prohibit delete, null value, referential integrity, entity integrity constraints, domain constraints

Citation preview

Page 1: Database relationship

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

Degree of relationship

1

CHAPTER 3

Dr.Girija Narasimhan

Page 2: Database relationship

Subjects may be prerequisites for other subjects.

Unary Relationship

2 Dr.Girija Narasimhan

Page 3: Database relationship

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.

3 Dr.Girija Narasimhan

Page 4: Database relationship

Binary Relationship

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

4 Dr.Girija Narasimhan

Page 5: Database relationship

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.

5 Dr.Girija Narasimhan

Page 6: Database relationship

:

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.

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

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

6 Dr.Girija Narasimhan

Page 7: Database relationship

:

7

Relation

Relation schema

Employee Table

Employee (Emp_id,ename,Salary)

Primary key or identifier Employee (Emp_id,ename,Salary)

Foreign key

Employee (Emp_id,ename,Salary,dept_name)

Department(dept_name,ename,Salary)

Foreign key

Composite key Over_time (emp_id, dept_name,No_of_hour)

Composite key

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

Dr.Girija Narasimhan

Page 8: Database relationship

8

DOMAIN CONSTRAINTS

INTEGRITY CONSTRAINTS

ENTITY Integrity CONSTRAINTS

Referential Integrity CONSTRAINTS

Dr.Girija Narasimhan

Page 9: Database relationship

9

DOMAIN CONSTRAINTS

Domain_name Meaning Data type size Allowable value or range

Age Years of the person number 3 <=110

Phone_no Conduct number number 8 Must be 8 digit and <99999999 and >90000000

Mark Scored by Student in Exam

number 3 >=0 and <=100

Dr.Girija Narasimhan

Page 10: Database relationship

10

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

Page 11: Database relationship

ENTITY Integrity CONSTRAINTS

Department

Not Unique

Not Unique

Same value

Entity integrity not enforced

11 Dr.Girija Narasimhan

Page 12: Database relationship

12

ENTITY Integrity CONSTRAINTS

Null is a value, suppose no other value not assign or apply. Null is not zero (0) or blank (“ “)

Not Null

No null value allowed in the primary key

Entity integrity not enforced

Dr.Girija Narasimhan

Page 13: Database relationship

13

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

Page 14: Database relationship

14

Department

EMPLOYEE

EMPLOYEE

Repetition in the foreign key - allowed

Null value allowed in the foreign key

Referential Integrity CONSTRAINTS

Foreign key

Dr.Girija Narasimhan

Page 15: Database relationship

15

Department

EMPLOYEE Referential Integrity CONSTRAINTS

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

Dr.Girija Narasimhan

Page 16: Database relationship

16

Rule 1: Foreign key must match with primary key of the other relation.

CASCADING DELETE PROHIBIT DELETE

Dr.Girija Narasimhan

Referential Integrity CONSTRAINTS

Page 17: Database relationship

:

17

Referential Integrity CONSTRAINTS

Dr.Girija Narasimhan

Page 18: Database relationship

18

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

Page 19: Database relationship

19

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

Page 20: Database relationship

:

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

Page 21: Database relationship

21

Mapping ER diagrams to Relations

Transform Conceptual model to Relational model

Why?

Entity type Week entity type Binary Relations

Regular Composite Multivalued

Create a new relation 1-1 relation type 1-N relation type M-N relation type

Dr.Girija Narasimhan

Unary Relation

Page 22: Database relationship

:

22

Mapping ER diagrams to Relations

Entity type

Regular

Dr.Girija Narasimhan

Page 23: Database relationship

:

23

Mapping ER diagrams to Relations

Entity type Composite

Dr.Girija Narasimhan

Page 24: Database relationship

:

24

Mapping ER diagrams to Relations

Entity type Multi valued

Dr.Girija Narasimhan

Page 25: Database relationship

25

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

Page 26: Database relationship

26

Mapping 1-1 Binary Relationship Type

Using foreign key

One and only One (Mandatory One) Zero or One (Optional One)

Employee may manage one or may not manage any department

Dr.Girija Narasimhan

Employee must assign by only one department.

Page 27: Database relationship

27

Mapping 1-N Binary Relationship Type

One or More (Mandatory One) One and only One (Mandatory One)

Dr.Girija Narasimhan

Employee must work only one department.

Department have one or more employee

Page 28: Database relationship

28

Mapping M-N Binary Relationship Type

One or More (Mandatory One) One or More (Mandatory One)

Dr.Girija Narasimhan

Employee join one or more project

Project has one more employee

Page 29: Database relationship

29

Mapping Unary Relation Type

Dr.Girija Narasimhan

one employee appointed as supervisor