Saturday 5 November 2016

Database Relationships

a) One-to-many and many-to-one relationships: An employee can work in only one department; this relationship is single-valued for employees. On the other hand, one department can have many employees; this relationship is multi-valued for departments. The relationship between employees (single-valued) and departments (multi-valued) is a one-to-many relationship. To define tables for each one-to-many and each many-to-one relationship:

1.    Group all the relationships for which the "many" side of the relationship is the same entity.

2.    Define a single table for all the relationships in the group.


Many-to-One Relationships
Entity
Relationship
Entity
Employees
are assigned to
departments
Employees
work at
jobs
Departments
report to
(administrative) departments

In the third relationship, "departments" is on the "many" side, so a department table, DEPARTMENT, is defined. The following tables show these different relationships.

The EMPLOYEE table:

EMPNO
WORKDEPT
JOB
000010
A00
President
000020
B01
Manager
000120
A00
Clerk
000130
C01
Analyst
000030
C01
Manager
000140
C01
Analyst
000170
D11
Designer

The DEPARTMENT table:

DEPTNO
ADMRDEPT
C01
A00
D01
A00
D11
D01

b) Many-to-many relationships : A relationship that is multi-valued in both directions is a many-to-many relationship. An employee can work on more than one project, and a project can have more than one employee. The questions "What does Dolores Quintana work on?", and "Who works on project IF1000?" both yield multiple answers. A many-to-many relationship can be expressed in a table with a column for each entity ("employees" and "projects"), as shown in the following example. The following table shows how a many-to-many relationship (an employee can work on many projects, and a project can have 7 many employees working on it) is represented. The employee activity (EMP_ACT) table:

EMPNO
PROJNO
000030
IF1000
000030
IF2000
000130
IF1000
000140
IF2000
000250
AD3112

c) One-to-one relationships: One-to-one relationships are single-valued in both directions. A manager manages one department; a department has only one manager. The questions, "Who is the manager of Department C01?", and "What department does Sally Kwan manage?" both have single answers. The relationship can be assigned to either the DEPARTMENT table or the EMPLOYEE table. Because all departments have managers, but not all employees are managers, it is most logical to add the manager to the DEPARTMENT table, as shown in the following example. The following table shows the representation of a one-to-one relationship.

 The DEPARTMENT table:

DEPTNO
MGRNO
A00
000010
B01
000020
D11
000060

Ensure that equal values represent the same entity: You can have more than one table describing the attributes of the same set of entities. For example, the EMPLOYEE table shows the number of the department to which an employee is assigned, and the DEPARTMENT table shows which manager is assigned to each department number. To retrieve both sets of attributes simultaneously, you can join the two tables on the matching columns, as shown in the following example. The values in WORKDEPT and DEPTNO represent the same entity, and represent a join path between the DEPARTMENT and EMPLOYEE tables.

The DEPARTMENT table:

DEPTNO
DEPTNAME
MGRNO
ADMRDEPT
D21
Administration Support
000070
D01

The EMPLOYEE table:

EMPNO
FIRSTNAME
LASTNAME
WORKDEPT
JOB
000250
Daniel
Smith
D21
Clerk


When you retrieve information about an entity from more than one table, ensure that equal values represent the same entity. The connecting columns can have different names (like WORKDEPT and DEPTNO in the previous example), or they can have the same name (like the columns called DEPTNO in the department and project tables). 

No comments:

Post a Comment