Professional Documents
Culture Documents
Assig2 CH 5
Assig2 CH 5
Questions:
Projects Working_Hours
P_ID P_Name P_Location E_ID P_ID Hours
1 Project A Malaysia 1 2 80
2 Project B Nigeria 2 1 100
1 1 100
2 1 50
1. Create a database for the above entities. Note: Use a meaningful name in naming the database.
Page 1 of 11
Page 2 of 11
2. Create the tables above. You need to identify a Primary Key for each table.
3. Insert the records above into the tables that you have created in Question 2.
Page 3 of 11
Page 4 of 11
Page 5 of 11
4. Specify the Foreign Key for each table (If any) with the referential integrity functions: on update cascade and
on delete restrict.
Page 6 of 11
5. Modify your table definition using the Alter command to add an extra column to the Employees table called
dateEmployed to represent the date that the employee started working for the company. Use the describe
command to view the structure of the table Employees.
Page 7 of 11
d for the company
6. View the contents of your Employees table by typing ‘select * from Employees’. Update the second row in your
Employees table by changing the name to ‘Issa Tairov. View the contents of the table again and check that the
name has changed.
UPDATE syntax.
1. UPDATE table-name
2. SET column-name1 = value1,
1. UPDATE table-name
2. SET column-name1 = value1,
4. WHERE condition
DELETE syntax.
1. DELETE table-name
1. DELETE table-name
2. WHERE condition
9. Modify your table definition using the Alter command to rename the table Working_Hours to
Project_Duration
10. Modify your table definition using the Alter command to change the column name Hours to P_Hours.
11. Delete employee’s record that has no salary. Hint: You can use is null function.
Page 8 of 11
12. Modify your table definition using the Alter command to drop the l_name column.
14. Update the Projects table, add another project record with the following values:
Project Y Indonesia
15. Modify your table definition using the Alter command to increase the size of D_Name to 55.
This result does not include E13 because of the null value in the commission column. To
ensure that the row with the null value is included, we need to look at the individual fields.
By adding commission and salary for employee E13, the result will be a null value. The
solution is shown below.
Relationships
Relationships are the glue that holds the tables together. They are used to connect related
information between tables.
Relationship strength is based on how the primary key of a related entity is defined. A weak,
or non-identifying, relationship exists if the primary key of the related entity does not contain
a primary key component of the parent entity. Company database examples include:
Customer(CustID, CustName)
Order(OrderID, CustID, Date)
A strong, or identifying, relationship exists when the primary key of the related entity
contains the primary key component of the parent entity. Examples include:
Types of Relationships
A one to many (1:M) relationship should be the norm in any relational database design and
is found in all relational database environments. For example, one department has many
employees. Figure 8.7 shows the relationship of one of these employees to the department.
Page 9 of 11
A light blue diamond in the middle connected on either side to a blue rectangle. The
rectangle on the left says EMPLOYEE and is connected with a line to five yellow ovals with
the words Birthdate, Name, Address, Salary, EID. The diamond is also connected to a blue
rectangle on its right with the word DEPARTMENT and that is connected with lines to three
yellow ovals with the words Name, Office, DeptID.
Figure 8.7. Example of a one to many relationship.
One to one (1:1) relationship
A one to one (1:1) relationship is the relationship of one entity to only one other entity, and
vice versa. It should be rare in any relational database design. In fact, it could indicate that
two entities actually belong in the same table.
An example from the COMPANY database is one employee is associated with one spouse,
and one spouse is associated with one employee.
Many to many (M:N) relationships
Figure 8.8 shows another another aspect of the M:N relationship where an employee has
different start dates for different projects. Therefore, we need a JOIN table that contains the
EID, Code and StartDate.
image
Figure 8.8. Example where employee has different start dates for different projects.
A unary relationship, also called recursive, is one in which a relationship exists between
occurrences of the same entity set. In this relationship, the primary and foreign keys are the
same, but they represent two entities with different roles. See Figure 8.9 for an example.
Page 10 of 11
For some entities in a unary relationship, a separate column can be created that refers to
the primary key of the same entity set.
image
Figure 8.9. Example of a unary relationship.
Ternary Relationships
Refer to Figure 8.10 for an example of mapping a ternary relationship type. Note n-ary
means multiple tables in a relationship. (Remember, N = many.)
For each n-ary (> 2) relationship, create a new relation to represent the relationship.
The primary key of the new relation is a combination of the primary keys of the
participating entities that hold the N (many) side.
In most cases of an n-ary relationship, all the participating entities hold a many side.
Page 11 of 11