0% found this document useful (0 votes)
20 views36 pages

Week 14 Revision

Uploaded by

beldron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views36 pages

Week 14 Revision

Uploaded by

beldron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Week 14

KT24503 Database
Important Dates

Project 2 Submission (10%)


01 • 28 January Friday 11.55pm

02 Final Exam (40%)


• 13 February Monday 9-12pm
Final Exam Topics Included:
• The Relational Database Model
• Entity Relationship Modeling
• Normalization
• SQL
• Transaction and Concurrency Control

• All structured questions


• Consists of 4 questions (30-25-20-25)
-Relational Schema
-Logical database model
-relational model

01
Revision
ER Diagram to Relational Model Conversion

Relational Model represents how data is


stored in database in the form of table.
2.Entity set with multi valued attribute:

Employee (EmpID, Name)


3.Entity set with Composite attribute:
4 . Entity with Derived Attribute

Employee (Emp_No, Ename, DOjoin)


5. 1:M (One–to–many) Relationship

CarPlate FoodReadyTime
DeliveryNo
Name PickUpAddress

1 M
RIDER Makes DELIVERY
RiderNo
DeliveryAddress

Relational Schema
RIDER (RiderNo, Name, CarPlate)
DELIVERY (DeliveryNo, PickUpAddress, DeliveryAddress, RiderNo, FoodReadyTime)

Foreign Key
6. M:N (many to many) Relationship:
7. 1:1 (one to one) Relationship:
5 .Weak Entity

Relational Schema
Officer (Loan_number, Amount)
Payment (Loan_number, Payment_number, Date)
STEP 1: Write the
relational schema
for all the entities

Employee (EmployeeID, FN, LN, DOB)


EmployeeContactNo ( EmployeeID, ContactNo)
Project (ProjectID, ProjectName)
Company (CompanyName, Location)
Salary (SalaryID, ModeOfPayment, DateOfSalary)
STEP 2: Write the
relational schema
for all the
relationships:

Employee (EmployeeID, FN, LN, DOB, Company_Name, DateOfJoin, SalaryID, ProjectID, StartDate)
EmployeeContactNo ( EmployeeID, ContactNo)
Project (ProjectID, ProjectName)
Company (CompanyName, Location)
Salary (SalaryID, ModeOfPayment, DateOfSalary)
02
SQL
ERD for HR System
Q1 Answer:

SELECT first_name, last_name,


Write a query in SQL to display the hire_date, salary, department_id
first name and last name, hire date, FROM employees
salary, and department number WHERE last_name NOT LIKE ‘%M%'
for those employees whose last ORDER BY department_id ASC;
name does not containing the
letter M and make the result set
in ascending order by department
number.
Q2 Answer:

Write a query in SQL to


SELECT CONCAT(first_name,last_name)
display the full name AS Fullname ,job_id,hire_date
(first and last), job id FROM employees
and date of hire for WHERE
hire_date BETWEEN '1996-05-05’
those employees who
AND '1997-07-05'
was hired during
March 5th, 1996 and
July 5th, 1997.
Q3 Answer:

SELECT CONCAT(first_name,last_name)
AS Fullname,salary,department_id
Write a query in SQL to display the FROM employees
full name (first and last name), WHERE manager_id IS NULL
salary, and department number
for those employees who is not
working under a manager.
Q4 Answer:

Write a query in SQL to display SELECT * FROM employees


WHERE YEAR(hire_date)=1997
all the information of AND MONTH(hire_date) IN (9,10)
employees who have been
hired in the month of
September and October,
1997.
Q5 Answer:

Write a query in SQL to display SELECT job_id, count(employee_id,


sum(salary, max(salary)-min(salar
job ID, number of employees,
FROM employees GROUP BY job_id
sum of salary, and
difference between highest salary
and lowest salary for a job
Q6 Answer:

Write a query in SQL to


SELECT country_id, count(city)
display the country ID FROM locations
and number of cities GROUP BY country_id
in that country we
have.
Q7 Answer:

Write a query in SQL to display SELECT manager_id,count(employee_id)


FROM employees
the manager ID and number GROUP BY manager_id
of employees managed by
the manager.
Q8 Answer:

Select CONCAT (e.first_name,' ',


Write a query in SQL to display the e.last_name) AS FULLNAME ,
employees full name (first name count(d.employee_id) AS NoDEPENDENTS
and last name) who have from employees e, dependents d
WHERE d.employee_id = e.employee_id
dependents and number of group by e.employee_id
dependents they have and make order by fullname;
the result set in ascending order by
full name.
Q9 Answer:
SELECT e.first_name, e.last_name,
d.department_name, e.salary
Write a query in SQL to display FROM employees e, departments d
the first name, last name, WHERE e.department_id = d.department_id
AND e.salary >8000;
and department name and
salary for employees who OR
earn more than 8000. SELECT e.first_name, e.last_name,
d.department_name, e.salary
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
AND e.salary >8000;
Q10 Answer:

Write a query in SQL to display the


SELECT e.first_name, e.last_name,
first name, last name, d.department_id, d.department_name
department number and FROM employees e
department name, for all JOIN departments d
ON e.department_id = d.department_id
employees for departments 8 or AND d.department_id IN (8,4)
4 and display the result ordered ORDER BY e.last_name
by last name.
Q11 Answer:

SELECT e.first_name, e.last_name,


Write a query in SQL to display d.department_name, l.city,
those employees who contain a l.state_province
FROM employees e
letter s to their first name and JOIN departments d
also display their last name, ON e.department_id = d.department_id
JOIN locations l
department name, city, and ON d.location_id = l.location_id
state province. WHERE e.first_name LIKE '%s%’;
Q12 Answer:

Write a query in SQL to display SELECT CONCAT (e1.first_name, ' ',


the full name (first name, e1.last_name) AS "employee_name",
CONCAT (e2.first_name, ' ',
last name) of all employees e2.last_name) AS "manager_name"
including the full name (first FROM employees e1
JOIN employees e2
name, last name) of their ON e1.manager_id = e2.employee_id;
manager.
Q13 Answer:

SELECT d.department_name,
Write a query in SQL to display l.city,
l.state_province
the department name, city, FROM departments d
and state province for each JOIN locations l
ON d.location_id = l.location_id;
department.
Q14 Answer:

Write a query in SQL to SELECT d.department_name,


display department name CONCAT(e2.first_name, ' ', e2.last_name) AS
MANAGER_NAME
and the full name (first FROM departments d
JOIN employees e1
and last name) of the ON d.department_id = e1.department_id
manager. JOIN employees e2
ON e2.manager_id = e1.employee_id;
Q15 Answer:

SELECT CONCAT(e.first_name, '


Write a query in SQL to display the ', e.last_name) AS full_name, e.salary
full name (first and last name), FROM employees e
JOIN departments d
and salary of those employees ON e.department_id = d.department_id
who working in any JOIN locations l
ON d.location_id = l.location_id
department located in London. WHERE l.city ='London'
Q16 Answer:

SELECT d.department_name,
Write a query in SQL to display COUNT(e.employee_id) AS
the department name and num_employees
FROM departments d
number of employees in each
JOIN employees e
of the department. ON d.department_id =
e.department_id
GROUP BY d.department_name;
03
Normalization
Steps in normalization

3rd normal form is


generally considered
sufficient

34
Need to know the steps:
1. Identify Candidate Keys??
(staffNo, patNo, appointment) staffNo dentistName patNo patName appointment surgeryRoomNo

S1011 Felix Leong P100 Nina Evans 13-Jan-17 10.00 S15


S1011 Felix Leong P105 Raymond Choo 13-Jan-17 12.00 S15
2. Find functional dependencies?
S1024 Chrissy Chin P108 Natasha Isak 13-Jan-17 10.00 S10
○ Partial dependencies S1024 Chrissy Chin P108 Natasha Isak 15-Jan-17 14.00 S10
DENTIST (staffNo, dentistName); S1032 Meliana Santi P105 Raymond Choo 15-Jan-17 16.30 S15

PATIENT (patNo, patName) S1032 Meliana Santi P110 Scarlett Tan 16-Jan-17 18.00 S13

○ Full dependencies
APPOINTMENT (staffNo, patNo, appointment, surgeryRoomNo)

-3NF – Remove transitive dependencies


- Referential Integrity is a rule that states that every value of a foreign key must match a
value of an existing primary key
- The staffNo in APPOINTMENT must exist in staffNo in DENTIST
- The patNo in APPOINTMENT must exist in patNo in PATIENT
UNF
● Normalize the table until 3NF Order (OrderNo, Date, CustNo, CustName, CustAddress,
City, Country, ProductNo, Description, Quantity, UnitPrice)

Step 1: Identify Candidate Keys and remove multivalued


attributes
CK is (OrderNo, CustNo, ProductNo)

Step 2: 1NF
Customer (CustNo, CustName, CustAddress, City,
Country)- > partial dependencies
Product (ProductNo, Description, UnitPrice) - > partial
dependencies
2NF/3NF – remove transitive dependencies
Order ( OrderNo, CustNo, ProductNo ,Date, Quantity)
Customer (CustNo, CustName, CustAddress,Country)- full dependencies
Country ( countryId, countryName, cityID)
City (cityID, cityName)
Product (ProductNo, Description, UnitPrice)
Order ( OrderNo, CustNo, ProductNo ,Date, Quantity)
- full dependencies

You might also like