Join The Club: C207 - Database Systems 2012
Join The Club: C207 - Database Systems 2012
DBMS
Schema 1 Schema 3
Table 1
Table 3
Column 1 Column 2
Schema • Row 1 • Row 1
2 Table 2 • Row 2 • Row 2
SELECT title
FROM book
WHERE title LIKE 'Pro%';
SELECT *
FROM book
WHERE price < 50;
SELECT *
FROM book
WHERE language = 'Mandarin';
SELECT *
FROM book b, publisher p
WHERE b.publisher_id = p.publisher_id
AND (b.language = 'English' OR
b.language = 'Mandarin')
AND b.price > 70
ORDER BY b.price DESC;
Page 30, Database Systems
School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
Q30. Remove the book with ISBN equal to 978-0-
07-124738-2
DELETE
FROM book
WHERE isbn = '978-0-07-124738-2';
SELECT title
FROM book;
SELECT publisher_name
FROM book b, publisher p
WHERE b.publisher_id =
p.publisher_id
AND title LIKE '%Java%';
SELECT b.title
FROM author a,
book b
WHERE a.author_id = b.author_id
AND a.last_name = 'Fry'
AND a.first_name = 'Chris'
AND b.language = 'English';
SELECT s.school_name
FROM author a, book b,
school s, school_book_order sbo
WHERE a.author_id = b.author_id
AND b.isbn = sbo.isbn
AND sbo.school_id = s.school_id
AND a.first_name = 'Paul'
AND a.last_name = 'Harris';
INT(n)
VARCHAR(n)
DECIMAL(n,m)
DATETIME
UPDATE contact
SET email = 'resources@krss.edu.sg'
WHERE school_id = 2;
DELETE
FROM contact
WHERE contact_id = 7;
P02 SELECT, FROM, *, WHERE, AND, OR, ( ), LIKE, %, DELETE, <, >, '',
ORDER BY, DESC, aliases
P03 Keys (primary, foreign, compound), selecting from multiple
tables, AS, DISTINCT
P04 Four and five table joins, CREATE TABLE,
INSERT, UPDATE
P05 Normalisation: bottom-up
P06 Normalisation: top-down
P07 From Functional Requirements to an ERD
One
student
may take
several
modules
every
semester
Normalisation
Compound keys
Bottom-up design by
1NF
2NF
3NF
Top-down design
P03 Keys (primary, foreign, compound), selecting from multiple tables, AS, DISTINCT
P04 Four and five table joins, CREATE TABLE, INSERT, UPDATE
P05 Normalisation: bottom-up
P06 Normalisation: top-down
P07 From Functional Requirements to an ERD
P08 Sakila – SQL Revisited
Must be in 1NF
No partial dependencies (applies only to compound keys)
Third normal form (3NF)
Must be in 2NF
No transitive dependencies (column relies on column that
is defined by primary key)
Normalisation checking
P04 Four and five table joins, CREATE TABLE, INSERT, UPDATE
NEW
OLD
MySQL Workbench
Sakila
(More SQL)
Function Description
AVG () Returns the average value
COUNT () Returns the number of values
MAX () Returns the maximum value
MIN () Returns the minimum value
STD () Returns the population standard deviation
SUM () Returns the sum of the values
VARIANCE () Returns the population standard variance
... and there are more
SELECT COUNT(*)
FROM film;
ALL MOVIES
AMERICAN
Hitchhikers guide to the Galaxy (UK)
The Vanishing (ITALY)
MOVIES
America Dreaming (USA)
LOUD (Germany) Jackass 7 (USA)
Sink the Bismarck (UK)
The sub query first defines the set upon which the outer
statement operates
SELECT c.customer_id
FROM customer c, address a
,city ct ,country co
WHERE ...
AND co.country = 'France';
COUNT(*)
Other aggregated functions
IN keyword
!
Nested Queries (Sub Queries)
GROUP BY
OFF-CAMPUS LEARNING
(OCL)
Revision
Question SQL
16 SELECT c.name AS "Category",
COUNT(*) AS "Number of Product“
FROM category c, product p
WHERE c.category_id = p.category_id
GROUP BY c.name;
Question SQL
17 SELECT r.receipt_no AS "Receipt Number",
description AS Description,
sale_price AS "Unit Selling Price",
quantity AS "Quantity Ordered",
item_discount AS "Item Discount",
(sale_price-item_discount)*quantity
FROM product p, sale_item s, receipt r
WHERE p.product_code = s.product_code
AND r.receipt_no = s.receipt_no
AND r.receipt_no = 50909;
Question SQL
18 SELECT r.receipt_no AS "Receipt Number",
SUM((sale_price-item_discount)*quantity)
FROM product p, sale_item s, receipt r
WHERE p.product_code = s.product_code
AND r.receipt_no = s.receipt_no
GROUP BY r.receipt_no;
Question SQL
19 SELECT r.receipt_no AS "Receipt Number",
SUM(((sale_price-item_discount)-cost_price)*quantity)
FROM product p, sale_item s, receipt r
WHERE p.product_code = s.product_code
AND r.receipt_no = s.receipt_no
GROUP BY r.receipt_no;
Question SQL
21 SELECT description AS Description,
sale_price AS "Unit Selling Price"
FROM product p
WHERE p.product_code IN (SELECT product_code
FROM sale_item
WHERE receipt_no = 50909);
Question SQL
23 SELECT description, quantity, item_discount,
r.receipt_no, sale_date, receipt_discount
FROM product p, sale_item si, receipt r
WHERE p.product_code = si.product_code
AND si.receipt_no = r.receipt_no;
Must be in 1NF
No partial dependencies (applies only to compound keys)
Third normal form (3NF)
Must be in 2NF
No transitive dependencies (column relies on column that is
defined by primary key) Page 140, Database Systems
School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Next Week
SAKILA II
Example:
CREATE VIEW actor_and_film
AS SELECT a.last_name, a.first_name, f.title
FROM actor a,
film_actor fa,
film f
WHERE a.actor_id = fa.actor_id
AND fa.film_id = f.film_id;
SELECT COUNT(*)
FROM rental
WHERE customer_id IN (SELECT * FROM chinese_customer)
SELECT SUBSTR(description,1,50)
FROM film
1. Add the columns you need, the tables you need and the join conditions first
(remember three tables require two join conditions). TEST it. Fix any bugs before
continuing.
SELECT a.actor_id,
a.first_name,
a.last_name,
f.release_year,
FROM actor a,
film_actor fa,
film f
WHERE a.actor_id = fa.actor_id
AND fa.film_id = f.film_id
Page 157, Database Systems
School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Tip for Writing Complex SQL
2. Add in the filter (release_year = 2006). TEST it. Add in the aggregated
function. Remember aggregated functions require a GROUP BY. Group by all
non-aggregated columns. TEST it. Fix any bugs before continuing.
SELECT a.actor_id,
a.first_name,
a.last_name,
f.release_year,
COUNT(*) number_films
FROM actor a,
film_actor fa,
film f
WHERE a.actor_id = fa.actor_id
AND fa.film_id = f.film_id
AND release_year = 2006
GROUP BY a.actor_id, a.first_name,
a.last_name, f.release_year;
Page 158, Database Systems
School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Tip for Writing Complex SQL
3. Finally, add in the HAVING and ORDER by clauses. TEST it. Fix any bugs.
SELECT a.actor_id,
a.first_name,
a.last_name,
f.release_year,
COUNT(*) AS number_films
FROM actor a,
film_actor fa,
film f
WHERE a.actor_id = fa.actor_id
AND fa.film_id = f.film_id
AND release_year = 2006
GROUP BY a.actor_id, a.first_name,
a.last_name, f.release_year
HAVING COUNT(*) > 10
ORDER BY number_films DESC;
Page 159, Database Systems
School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solution 2 – Add, Test, Repeat.
1. To build this query we started with finding the first letter of the last name
SELECT SUBSTR(last_name,1,1)
FROM actor;
2. Next we worked out the length of the last name, without the initial
SELECT SUBSTR(last_name,1,1),
LENGTH(last_name) -1
FROM actor;
SELECT SUBSTR(last_name,1,1),
SUBSTR(last_name,2,LENGTH(last_name) -1)
FROM actor;
SELECT UPPER(SUBSTR(last_name,1,1)),
LOWER(SUBSTR(last_name,2,LENGTH(last_name) -1))
FROM actor;
SELECT CONCAT(
UPPER(SUBSTR(last_name,1,1)),
LOWER(SUBSTR(last_name,2,LENGTH(last_name) -1))
)
FROM actor;
SELECT CONCAT(
UPPER(SUBSTR(last_name,1,1)),
LOWER(SUBSTR(last_name,2,LENGTH(last_name) -1))
),
CONCAT(
UPPER(SUBSTR(first_name,1,1)),
LOWER(SUBSTR(first_name,2,LENGTH(first_name) -1))
)
FROM actor;
SELECT CONCAT(
UPPER(SUBSTR(last_name,1,1)),
LOWER(SUBSTR(last_name,2,LENGTH(last_name) -1))
) AS "Last",
CONCAT(
UPPER(SUBSTR(first_name,1,1)),
LOWER(SUBSTR(first_name,2,LENGTH(first_name) -1))
) AS "First"
FROM actor
ORDER BY Last;
ROLLBACK
If any part of the procedure fails, then all changes are
automatically rolled back. This solution is preferred.
Transaction Boundary
Page 174, Database Systems
School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
Introducing ACID
Atomicity - Guarantee that changes made during a
transaction are either all committed, or all rolled
back.
MySQL in server
Central Database Server
Rollback and Commit
Transaction processing
MySQL procedures
ACID
Readers and Writers
Monitoring activity
within databases
Example:
GRANT SELECT, INSERT, UPDATE
ON department
TO 'ru_fasttrack'@'%';
Database
Security
Ac p
Top Secret
c e eo
s s p le
Information
ib
Secrecy
Confidential
ilit
l
ro
yb
nt
Co
y
Restricted
Unclassified
Low Less Many
BEGIN
UPDATE story
SET complete_flag = param_complete_flag
WHERE story_name = param_story_name;
END$$
BEGIN
UPDATE story
SET complete_flag = param_complete_flag
WHERE story_name = param_story_name;
END$$
Creating a procedure
Creating a trigger
Creating a function
Database Validation
UPDATE last_eight
SET result8 = result7,
result7 = result6,
result6 = result5,
result5 = result4,
result4 = result3,
result3 = result2,
result2 = result1,
result1 = 'W'
WHERE club_id = 2;
A new table is created to store the position description, for example goalkeeper.
Database Validation
Compound v. Composite Keys
UNION in SQL
Database Design
Practical application of
previous learning in C207