0% found this document useful (0 votes)
61 views8 pages

Clinic 3 Answer

The document contains questions about SQL statements, ERD diagrams, database normalization, and database transactions. For SQL questions, it provides examples of INSERT, UPDATE, DELETE, and SELECT statements to add, modify, and retrieve data from the SAKILA sample database. The ERD section defines identifying and non-identifying relationships and the different types of primary keys. The normalization section explains the three normal forms and provides an example of normalizing a table. The transactions section defines the ACID properties and CIA triad and asks questions about COMMIT and ROLLBACK commands.

Uploaded by

hamzah
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)
61 views8 pages

Clinic 3 Answer

The document contains questions about SQL statements, ERD diagrams, database normalization, and database transactions. For SQL questions, it provides examples of INSERT, UPDATE, DELETE, and SELECT statements to add, modify, and retrieve data from the SAKILA sample database. The ERD section defines identifying and non-identifying relationships and the different types of primary keys. The normalization section explains the three normal forms and provides an example of normalizing a table. The transactions section defines the ACID properties and CIA triad and asks questions about COMMIT and ROLLBACK commands.

Uploaded by

hamzah
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/ 8

C207 UT3 Clinic Questions

Section A – SQL Statements


Use the SAKILA sample database to answer the questions in this section.

1) Write the SQL statement to insert a new customer. The details of the customer is as follows:
a. customer_id: 1005
b. first_name: Elle
c. last_name: Low
d. address_id: 103
e. active: TRUE
f. create_date: current date and time

INSERT INTO customer


(customer_id, first_name, last_name, address_id, active, create_date)
VALUES
(1005, ‘Elle’, ‘Low’, 103, true, NOW());

2) Write the SQL Statement to update this new customer’s active status to FALSE.

UPDATE customer
SET active = false
WHERE customer_id = 1005;

3) Write the SQL Statement to delete away this new customer.

DELETE
FROM customer
WHERE customer_id = 1005;

4) Write the SQL statement to display the number of rental of each country.

SELECT country, COUNT(*)


FROM country c, city ci, address a, customer cu, rental r
WHERE c.country_id = ci.country_id
AND ci.city_id = a.city_id
AND a.address_id = cu.address_id
AND cu.customer_id = r.customer_id
GROUP BY country;
5) Write the SQL statement to display the number of films rented out for each month.

SELECT MONTH(rental_date), COUNT(*)


FROM rental
GROUP BY MONTH(rental_date);

6) Write the SQL statement to display the output below.

The output counts the number of occurrence of the customers based on the first letter of
their last name. Filter the count to display only the letters with more than 50 occurrences
with the highest occurrence displayed on the top.

SELECT SUBSTR(last_name, 1, 1) AS ‘First Letter of Last Name’, COUNT(*)


FROM customer
GROUP BY SUBSTR(last_name, 1, 1)
HAVING COUNT(*) > 50
ORDER BY COUNT(*) DESC;

7) Write the SQL statement to display the output below.

Each film can only be loaned for one week (7 days). The output counts the number of late
return of each customer. [hint: datediff(return_date,rental_date) > 7]
Display the customer with the highest number of late return on the top.

SELECT CONCAT(UPPER(first_name), UPPER(last_name)) AS ‘customer name’,


COUNT(*) AS ‘number of late return’
FROM customer c, rental r
WHERE c.customer_id = r.customer_id
AND datediff(return_date,rental_date) > 7
GROUP BY ‘customer name’
ORDER BY ‘number of late return’ DESC;
8) Write the SQL statement to create a VIEW that selects the staff from FRANCE.
Using the newly created view, write a nested sub-query to display the total payment made
by the staff from FRANCE.

CREATE VIEW france_staff AS


( SELECT staff_id
FROM country co, city ci, address ad, staff s
WHERE co.country_id = ci.country_id
AND ci.city_id = ad.city_id
AND as.address_id = s.address_id
AND co.country = ‘FRANCE’ );

SELECT s.first_name, s.last_name, SUM(p.amount)


FROM staff s, payment p
WHERE s.staff_id = p.staff_id
AND staff_id IN (SELECT staff_id FROM france_staff);

Section B - ERD
1) Which of the followings can be found in an ERD?
a. Tables and attributes
b. Relationships
c. Test Data
d. Constraints

2) What is the difference between identifying and non-identifying relationship.


a. Identifying relationship: A solid line means that the primary key from one table is
used as part of the primary key in another table.
b. Non-identifying relationship: A dotted line means that the primary key from one
table is used as a foreign key in a second table.

3) If two tables have many-to-many relationship, what table is needed to represent the
relationship? What type of primary can be used in this table?
a. Resolving table
b. COMPOUND primary key / SURROGATE primary key

4) List down the 4 types of primary keys and their characteristics.


a. Natural Primary Key – has meaning outside the system
b. Surrogate Primary Key – has no meaning outside the system
c. Compound Primary Key – is a combination of other primary keys and no other
column
d. Composite Primary Key – Usually formed by adding additional columns to an existing
key to get uniqueness
5) One student is assigned to one project. One project is assigned to one or more students.
Describe the Primary/Foreign keys pair which represents the one-to-many relationship.
a. The relationship between “student” and “project” is many-to-one. The ‘one’ side is
at the “project” and the ‘many’ side is at the “student”.
i. NOTE: to use “one-to-many” or “many-to-one” is based on which table is
mentioned FIRST.
b. The primary key of “project” (one side) will be the foreign key at the “student”
(many side).

student project
Section C – Normalisation
1) State the rules of the three normal forms.
a. First normal form (1NF)
i. Every table has a unique primary key
ii. Remove derived columns
iii. No repeating groups
b. Second normal form (2NF)
i. Must be in 1NF
ii. No partial dependencies (applies only to compound keys)
c. Third normal form (3NF)
i. Must be in 2NF
ii. No transitive dependencies (column relies on column that is defined by
primary key)

2) Normalise the following table.

Question
No Question Student ID Name Diploma Diploma Name Answer
1 Which 120001 John DBA Diploma in Business Application E-Canteen
canteen do
you like Diploma in Business Information Food
best? 120002 Kelly DBIS System Haven
120003 Loki DBA Diploma in Business Application E-Canteen
120004 Mary DBA Diploma in Business Application E-Canteen
Diploma in Information Food
120005 Norman DIT Technology Haven
Diploma in Information
120006 Osman DIT Technology Kou Fu
2 Is the food 120001 John DBA Diploma in Business Application Yes
expensive? Diploma in Business Information
120002 Kelly DBIS System No
120003 Loki DBA Diploma in Business Application No
120004 Mary DBA Diploma in Business Application Yes
Diploma in Information
120005 Norman DIT Technology No
Diploma in Information
120006 Osman DIT Technology Yes

1NF
“question”  question_no, question
“answer”  question_no, student_id, name, diploma, diploma_name, answer

2NF
“question”  question_no, question
“answer”  question_no, student_id, answer
“student”  student_id, name, diploma, diploma_code
3NF
“question”  question_no, question
“answer”  question_no, student_id, answer
“student”  student_id, name, diploma_code
“diploma”  diploma_code, diploma_name

OR

3NF
“question”  question_no, question
“answer”  question_no, student_id, answer_id
“answer”  answer_id, answer
“student”  student_id, name, diploma_code
“diploma”  diploma_code, diploma_name

Section D – Transactions/ ACID / CIA


1) List down the definitions of ACID and CIA.
a. Atomicity - Guarantee that changes made during a transaction are either all
committed, or all rolled back.
b. Consistency - Ensures that a transaction does not break the integrity constraints of a
database. If integrity constraints are broken, then all changes must be rolled back.
c. Isolation - No operation (or user) outside of the transaction can see the data in an
intermediate state.
d. Durability- Once the user has been notified of a transaction success it is persisted
(permanently saved) and cannot be rolled back.

e. Confidentiality - Information is available only to those people (users) that have


authorized access
f. Integrity - Ensuring data is not incorrectly modified either maliciously or by accident.
g. Availability - that people and applications that need the data can access that data in
a timely manner.

2) Which one of the following is the least likely to be viewed as a transaction?


a. Retrieving a customer’s latest purchase
b. Transferring $1000.00 from a customer’s saving account into his checking account
c. Reserving a seat on a plane
d. Reserving a hotel room

3) What happen when a “COMMIT” command is given?

Changes made to the database, since the transaction began, are persisted.
4) What happen when a “ROLLBACK” command is given?

Changes made to the database, since the transaction began, are un-done.

5) The COMMIT and ROLLBACK commands ensure which component of ACID and CIA?

Atomicity, Consistency, and Integrity.

6) John withdrew $100 at an ATM machine. The next day, he discovered that instead of $100,
$110 was deducted from his account. Which component of ACID is violated?

Consistency is violated.

7) John went to the bank to enquiry on the incorrect deduction. The bank manager told him
that the bank’s ATM system was being manipulated to deduct 10% more. Which component
of CIA has the system failed?

The Integrity of the system had failed.


Section E – PROCEDURES / TRIGGERS / FUNCTIONS

1) Whenever 3 numbers (<1000) are inserted into the ‘cal_average’ table, a trigger will be fired
to calculate and store the average. Fill in the blanks below to complete the function and
trigger.

CREATE FUNCTION `calculate_average`


( para_num1 INT(3),
para_num2 INT(3),
para_num3 INT(3) )

RETURNS DECIMAL(6,3)
BEGIN
RETURN ( (para_num1 + para_num2 + para_num3) / 3.0);
END$$

CREATE TRIGGER `before_insert_average`


BEFORE INSERT ON `cal_average`
FOR EACH ROW
BEGIN

SET NEW.average = calculate_average (NEW.num1,


NEW.num2, NEW.num3);
END$$

You might also like