0% found this document useful (0 votes)
29 views237 pages

Join The Club: C207 - Database Systems 2012

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 PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views237 pages

Join The Club: C207 - Database Systems 2012

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 PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 237

Join the Club

C207 – Database Systems


2012
What is a database?
A database is a structured collection of “stuff”

Why do we need databases?


 To store and organise our collections of “stuff” (data)
 To avoid losing or duplicating our “stuff” (data)
 Make changes to our “stuff” (data) easily
What does a database look like?
 A list on a piece of paper
 A spreadsheet
 A table stored in a special type of management system (more
later)

Page 2, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
What is a structured collection of data?
A structured collection of data consists of columns
and rows.
What is a column?
 A column is an attribute. For example, if we were storing data
about you, then some of your attributes are name, age and
diploma. These attributes become columns in our collection.
What is a row?
 A row is a set of attributes. For example, your name is Greta,
you are 18 and you are in DIT. Your friend’s name is Bette, she is
18 and she is in DIDM. These two examples (you and your
friend) illustrate rows (or records).

Page 3, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
How do we organise rows and columns?
Column

Greta Garbo 18 DIT


Bette Davis 18 DIDM
Joan Crawford 18 DIDM

Row Ingrid Bergman 18 DIDM


Katharine Hepburn 19 DIT
Lucille Ball 17 DBA
Judy Garland 18 DBA
Ella Raines 18 DBA

The intersection of a row and a column is known as a field or cell.


A field or cell contains a single piece of data. The highlighted cell
tells us that Ingrid Bergman is 18 years old. The collection of all
rows and all columns is called a table.

Page 4, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
Today's questions
How many students from your class are in each
diploma?
 If we store all students in our database, we can simply count
them to answer this question.
How many students live in west coast area?
 To answer this question, we need to do a few things. First we
need to classify students’ addresses into North, South, East and
West. We then need to store that classification with the
student. Finally we can count the number of students in a
given area.

Page 5, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
Today's questions
What is the average age of the class?
 To answer this question we need to store EITHER the age of
each student, or more accurately the birth date of each
student. Note: If we store the age we can calculate the average
age NOW; however if we store the birth date we can calculate the
average age FOREVER. This means the data we store in our
database can influence the usefulness of that database.
What are the top interest groups that your
classmates have joined or intend to join?
 To answer this question we need a list of interest groups you
have joined or intend to join – then we can count the number
as before.

Page 6, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
What we would store

Page 7, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
Additional questions
What happens if two students have exactly the same
name?
 With the database we have just suggested, we could tell the two
students apart by dob. But what happens if the dob is also the same?
 This situation is COMMON
 The solution is to introduce a key or unique identifier. For example we
could record NRIC or Student Number in each row.
Will your group still function if it is used to store
information not just for your class; but all students in RP /
all polytechnics?
 We would need to store additional fields for each record (additional
columns in our table). We would need new columns to store the school
and the polytechnic in which the student is enrolled.
Page 8, Database Systems
School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
What happens when the volume increases?
A file is fine for storing the information for a class,
but what happens when we need to store
information for 100’s of classes?
 We could store in a spreadsheet. We could use a different sheet
for each class.
 However, sometimes we may exceed the limits of even a
spreadsheet. For example, what if we want to record census
information for everyone in Singapore?
 To manage higher data volumes a particular piece of software
helps us. This software is known as a Database Management
System (DBMS).
 The remainder of this course will concentrate on DBMS

Page 9, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
What we introduced this week
Table
 A table is a structured way of storing our data (or, a place to put our “stuff”).
A table consists of one or more rows and one or more columns. A table may
be represented in a piece of paper, a spreadsheet or as a structure stored in a
DBMS.
Row (record)
 A row is a collection of attributes that describe our “stuff”. A row (or record)
is generally shown running across the page.
Column (attribute)
 A column describes a single attribute of a row. A column (or attribute) is
generally shown running down the page.
Field (cell)
 A field or cell is the intersection of a row and a column, and holds a single
attribute for a single record.
Page 10, Database Systems
School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
Overview of a database structure

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

Page 11, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
Next week …
Introduction to MySQL (a database management
system)
Introduction to SQL (structured query language)

Learning the basics of querying a database. Week


two will lay a foundation that will be expanded in
future weeks – don’t miss it.

Page 12, Database Systems


School of Infocomm P01 – Join the Club
Republic Polytechnic © 2012
The Fig Leaf
C207 – Database Systems
2012
What is MySQL?
MySQL is a relational database management
system (RDBMS).
MySQL is the most popular open-source relational
database available today.
MySQL drives many of the world’s popular web
sites

Page 14, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Anatomy of a simple SQL statement
SELECT <column list>
FROM <table list>
SELECT
 This keyword that instructs SQL to retrieve records; as opposed
to updating or deleting them. The <column list> contains a
comma separated list of columns (attributes) of interest.
FROM
 This keyword precedes comma separated list tables. If a
column is used in the <column list> then the table containing
that column must be present in the <table list>.

Page 15, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Selecting every column from a table
SELECT *
FROM <table>
*
 This special character means ‘all columns’.
 Use this special character only when querying the database
interactively.
 It is to be used with CAUTION and is not recommended for use
in any application (e.g., PHP Web Applications in Semester 2)
 You cannot select the columns in a different order
 If your database table changes, your code may ‘break’

Page 16, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Adding a conditional
SELECT <column list>
FROM <table list>
WHERE <where clause>
WHERE
 This keyword that instructs SQL to first filter the records before
returning them. A WHERE clause has two main functions.
 Restricting the number of rows selected by a query to a subset of
the total rows
 Joining one or more tables together on a common ‘key’

Page 17, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Adding more than one conditional
SELECT <column list>
FROM <table list>
WHERE <where clause section>
AND <where clause section>
AND
 The AND keyword is used when there are multiple filters. For
example, if you wanted to write a query to return all books
over you would use an AND statement. E.g., All books
published by Apress AND price exceeds $80.00
 AND is very common when joining one or more tables together.

Page 18, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Adding more than one conditional
SELECT <column list>
FROM <table list>
WHERE <where clause section>
OR <where clause section>
OR
 This keyword is used as a conjunction when there are multiple
alternatives. For example, the phrase “English OR Mandarin”
indicates books that are English as well as books that are
written in Mandarin.
 Do not confuse the keyword with the AND keyword. Books
written in “English AND Mandarin” has a different meaning
Page 19, Database Systems
School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Operator precedence
As you have learned previously it is sometimes
difficult to tell which operations are performed first.
The order of the operations does make a difference
to how your results are filtered (the number of
rows returned)
If in doubt use parenthesis!

Page 20, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Operator precedence
SELECT * FROM book, publisher
WHERE book.publisher_id = publisher.publisher_id
AND (language = 'English' OR language = 'Mandarin')
AND price > 70;

SELECT * FROM book, publisher


WHERE book.publisher_id = publisher.publisher_id
AND language = 'English' OR language = 'Mandarin'
AND price > 70;

The first query returns 14 rows, the second returns


29rows. Can you see why?

Page 21, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Finding similar things
SELECT <column list>
FROM <table list>
WHERE <column> LIKE <pattern>
LIKE
 This keyword that instructs SQL return all records where the
column value matches the <pattern>.
 Create the pattern using the one or more wildcard characters
“%”
 LIKE and “%” always occur together

Page 22, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Finding similar things
SELECT title
FROM book
WHERE title LIKE '%Pro%';

SELECT title
FROM book
WHERE title LIKE 'Pro%';

The first query returns 9 rows, the second returns 3


rows. Can you see why?

Page 23, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Removing records
DELETE
FROM <table list>
WHERE <where clause>
DELETE
 Delete removes a row (or rows) from a database. Note that there is
no column list as there was in the SELECT statement.
DELETE - WHERE
 If you use the DELETE keyword without a WHERE clause then you will
remove each and every record from your database. Use DELETE with
great caution.
 It is recommended to always use a WHERE clause when using a
DELETE unless you are certain you want to clear the entire table.
Page 24, Database Systems
School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
Q20. Write your own SQL statement to retrieve all
books where the price is less than $50.00 (10 rows)

SELECT *
FROM book
WHERE price < 50;

Page 25, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
Q21. Write your own SQL statement to retrieve all
books where the language is Mandarin (3 rows)

SELECT *
FROM book
WHERE language = 'Mandarin';

Page 26, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
Q23. Write your own SQL statement to retrieve only
the title and price of all books where the quantity
available is less than three, and the price exceeds
$100.00 (7 rows)

SELECT title, price


FROM book
WHERE quantity_available < 3
AND price > 100;

Page 27, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
Q24. Research for yourself the SQL keyword LIKE.
Use what you have learned so far, together with
the keyword LIKE to retrieve the title and price of
all books on Java. (4 rows)

SELECT title, price


FROM book
WHERE title LIKE '%Java%';

Page 28, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
Q26/27. Write your own SQL statement to retrieve
the title, language and publisher name for all books
published by Apress, and the price exceeds $80.00
(3 rows)

SELECT title, language,


publisher_name
FROM book, publisher
WHERE book.publisher_id =
publisher.publisher_id
AND publisher_name = 'Apress'
AND price > 80;
Page 29, Database Systems
School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
Q28/29. Write your own SQL statement to retrieve all
attributes of the book and the publisher where the
language is either English or Mandarin only and the
price is greater than $70.00 (14 rows)

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';

Page 31, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Eve's queries
What titles do I have in stock?

SELECT title
FROM book;

How can you avoid duplication of names? For


example 'Children and Emotion: The Development of
Psychological Understanding'

Page 32, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Eve's queries
How many copies of each title do I have?

SELECT title, quantity_available


FROM book
ORDER BY title;

Since the list is now ordered it becomes much


simpler to count the number of distinct titles. In
future weeks we will show how SQL can answer
this question directly.

Page 33, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Eve's queries
Can I find a title’s publisher without having to
locate a physical copy?

SELECT publisher_name
FROM book b, publisher p
WHERE b.publisher_id =
p.publisher_id
AND title LIKE '%Java%';

Page 34, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Introduced this week
SELECT
FROM
*
WHERE
AND
OR
( )
LIKE
%
DELETE
<
>
``
ORDER BY
DESC
Page 35, Database Systems
School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Next Week

Larger schemas and more on


the SQL SELECT statement

Page 36, Database Systems


School of Infocomm P02 – The Fig Leaf
Republic Polytechnic © 2012
Orders @ The Fig Leaf
C207 – Database Systems
2012
Primary keys and foreign keys
 Most tables you will see will have a primary key. A primary
key uniquely identifies a particular row. A primary key cannot
be duplicated.
 If we use the primary key from table A in table B, then it is
known as a primary key in table A, but it is known as a
foreign key in table B.
 Foreign keys are generally not unique.

 The primary key/foreign key pair form a relationship


between the two (or more) tables that give relational
databases their name.

Page 38, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
 Write an SQL query to select the author’s first and last
names, and all book titles written by that author.
 What does TOGETHER mean in the primary key for
school_book_order?
Table Primary Key Foreign Key
author author_id  
 book isbn  publisher_id AND author_id 
 publisher  publisher_id  
 school_book_order  school_id AND isbn TOGETHER   school_id AND isbn
 school  school_id  

Page 39, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
ERD lines
 The lines between two tables in an ERD depict the type of
relationship that exists between two tables.
 A dotted line means that the primary key from one table is
used as a foreign key in a second table.

Page 40, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
ERD lines
 A solid line means that the primary key from one table is used
as part of the primary key in another table.
 The “crows foot” represents the direction of the relationship.

 Relationships can be “read”. In the example given below we


read the relationship as “a school creates one or more
school_book_orders” and going the other way, “a
school_book_order belongs to one and only one school”

Page 41, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers
 Q11. Write a query that shows the authors first and last name,
the title and price of the book, and the publisher’s name.

SELECT a.first_name, a.last_name, b.title,


b.price, p.publisher_name
FROM author a, book b, publisher p
WHERE a.author_id = b.author_id
AND p.publisher_id = b.publisher_id;

Page 42, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Compound primary keys
 A compound key is “slightly” different to a normal
primary key/foreign key pair.
 A compound key uses the combination at least two
foreign keys to create a primary key in another table. We
will learn much more about the different type of keys
(natural, surrogate, compound & composite) later in the
course.
 To query using a compound primary key proceed as if it
was a foreign key.

Page 43, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Primary Keys: Important Note
This note is in large bold type, because it is a very
common mistake made by students.

Regardless of the number of primary key


columns in a table,
EACH TABLE HAS ONLY
ONE
PRIMARY KEY
Page 44, Database Systems
School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers continued
 Q13. Write a select statement that returns the title and
language of a book, together with the names of the school
that ordered that book, and the number ordered and
number outstanding.

SELECT b.title, b.language, s.school_name,


sbo.number_ordered,
sbo.number_outstanding
FROM book b,
school s,
school_book_order sbo
WHERE b.isbn = sbo.isbn
AND s.school_id = sbo.school_id;

Page 45, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers continued
 You can change the way a column title is displayed in SQL by
using the keyword AS. Using that keyword write an SQL
statement to produce the exact report you see below.

Page 46, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers continued
SELECT a.last_name AS Author,
b.title AS Title,
sbo.number_ordered AS 'No. Ordered'
FROM author a,
book b,
school_book_order sbo
WHERE a.author_id = b.author_id
AND b.isbn = sbo.isbn

Page 47, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Worksheet answers continued
 Refine the query so that only books ordered more than three
times appear, with the highest number of orders shown first
and the lowest number of orders shown last.

SELECT a.first_name AS Author,


b.title AS Title,
sbo.number_ordered AS 'No. Ordered'
FROM author a,
book b,
school_book_order sbo
WHERE a.author_id = b.author_id
AND b.isbn = sbo.isbn
AND sbo.number_ordered > 3
ORDER BY sbo.number_ordered DESC
Page 48, Database Systems
School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Problem statement answers
Which schools have previously ordered more than
one book from Eve’s Fig Leaf bookshop?

SELECT DISTINCT s.school_name


FROM school s, school_book_order sbo
WHERE s.school_id = sbo.school_id
AND number_ordered > 1;

Page 49, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Problem statement answers
Who publishes the author Paul Harris?

SELECT DISTINCT p.publisher_name,


p.publisher_address
FROM publisher p, book b, author a
WHERE p.publisher_id = b.publisher_id
AND b.author_id = a.author_id
AND a.first_name = 'Paul'
AND a.last_name = 'Harris';

Page 50, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Problem statement answers
How many books by Chris Fry were published in
English?

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';

Page 51, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Introduced this week

Multiple table selects


Multiple foreign keys
Compound keys

Page 52, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Next Week

Inserting data into the database

Modifying existing data

Creating tables and keys

Four and five table joins

Page 53, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
Contacts @ The Fig Leaf
C207 – Database Systems
2012
Learning Journey
P01 RDBMS, schemas, tables, rows, columns & fields

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

Page 55, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Four table join
 Which schools are reading books by Paul Harris?

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';

Page 56, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Five table join
 Report of schools, book details and number of outstanding orders.

SELECT s.school_name as School,


b.isbn as 'I.S.B.N',
b.title as 'Title',
p.publisher_name as Publisher,
a.last_name as Author,
sbo.number_outstanding 'No. Outstanding'
FROM author a, book b, publisher p,
school_book_order sbo, school s
WHERE a.author_id = b.author_id
AND p.publisher_id = b.publisher_id
AND b.isbn = sbo.isbn
AND sbo.school_id = s.school_id
AND sbo.number_outstanding > 0
ORDER BY sbo.number_outstanding DESC,s.school_name;
Page 57, Database Systems
School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Anatomy of a CREATE TABLE statement

CREATE TABLE <table name>


(<col 1> <col 1 type>(<precision>) <constraint>,
<col 2> <col 2 type>(<precision>) <constraint>,

PRIMARY KEY <primary key>
CONSTRAINT <foreign key name>
FOREIGN KEY <column name>
REFERENCES <table>(<col>)
)
ENGINE = <db engine>

Page 58, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Data Types in MySQL
Each column definition in a create table statement
requires a column name, data type and optionally a
not null constraint.

In this course we will generally use the most


common data -types, although many others exist.

 INT(n)
 VARCHAR(n)
 DECIMAL(n,m)
 DATETIME

Page 59, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Common Data-types
Data-type Comments
INT(n) Integer up to n characters long. Precision may be omitted. (-
2147483648 to 2147483647)
VARCHAR(n) String up to n characters long, variable length, not padded
DECIMAL(n,m) Decimal numbers. n represents the total length, m denotes the
fractional component. Hence FLOAT(5,2) can hold a maximum
value of 999.99
DATETIME Date with time component (e.g., 2011-09-05 23:59:26)
Insert using YYYY-MM-DD HH:MI:SS format
CHAR(n) Character up to n characters long, fixed length, padded
DATE Date without time component. (e.g., 2011-09-05 00:00:00)
Insert using YYYY-MM-DD format
TIME Time with no date component ('-838:59:59' to '838:59:59‘)
retrieved in 'HH:MM:SS' format
BOOLEAN Boolean Value. 1 – True, 0 – False. Synonym for TINYINT(1).

Page 60, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Sample CREATE TABLE statement
CREATE TABLE IF NOT EXISTS `contact` (
`contact_id` INT(4) NOT NULL ,
`last_name` VARCHAR(45) NOT NULL ,
`first_name` VARCHAR(45) NOT NULL ,
`email` VARCHAR(150) NULL ,
`phone` VARCHAR(10) NULL ,
`school_id` INT(4) NOT NULL ,
PRIMARY KEY (`contact_id`) ,
CONSTRAINT `fk_contact_school`
FOREIGN KEY (`school_id` )
REFERENCES `school` (`school_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

Page 61, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Sample CREATE TABLE statement
CREATE TABLE IF NOT EXISTS `contact` (
`contact_id` INT(4) NOT NULL ,
`last_name` VARCHAR(45) NOT NULL ,
`first_name` VARCHAR(45) NOT NULL ,
`email` VARCHAR(150) NULL ,
`phone` VARCHAR(10) NULL ,
`school_id` INT(4) NOT NULL ,
PRIMARY KEY (`contact_id`) ,
CONSTRAINT `fk_contact_school`
FOREIGN KEY (`school_id` )
REFERENCES `school` (`school_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

Page 62, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Insert statement
INSERT INTO contact
(contact_id,
last_name,
first_name,
email,
phone,
school_id)
VALUES
(1,
'Goh',
'Ivan',
'igoh@bendemeerhs.edu.sg',
'66767676',
4);
Page 63, Database Systems
School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Commit
 Commit simply means save.

 MySQL Query browser automatically saves your changes


without you requiring to commit explicitly.
 When using query browser on a default XQAMPP MySQL
installation, you do not need to issue commit statements.
 The script turns off and on autocommit using the following
statements:
 SET AUTOCOMMIT=0;
 COMMIT;

Page 64, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Running the same insert twice
MySQL prevents you from creating a row with the
same primary key more than once.
For this reason when you run your insert statement
for a second time you will receive the message:
Duplicate entry ‘1’ for key ‘PRIMARY’ (ref: WS,Q6)
WS Q8 asks: does the key you assigned to each
‘contact’ in your database matter? Since contact_id
has no real meaning it is called a ‘surrogate key’. It
doesn’t matter what the number is, or even if there
are gaps in the numbers. All that matters is that the
values are unique.
Page 65, Database Systems
School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
An update statement

UPDATE contact
SET email = 'resources@krss.edu.sg'
WHERE school_id = 2;

Page 66, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
The delete statement

DELETE
FROM contact
WHERE contact_id = 7;

Page 67, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Q11 answer
SELECT DISTINCT c.last_name AS 'Family Name',
c.first_name AS 'Given Name',
c.phone AS 'Phone'
FROM publisher p,
book b,
school_book_order sbo,
school s,
contact c
WHERE p.publisher_id = b.publisher_id
AND b.isbn = sbo.isbn
AND sbo.school_id = s.school_id
AND s.school_id = c.school_id
AND p.publisher_name = 'Wiley'
ORDER BY c.last_name

Page 68, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Introduced this week

Four and five table joins


AS keyword to rename columns in reports
CREATE TABLE statement
INSERT statement
UPDATE statement
DELETE statement

Page 69, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Next Week

We take a break from SQL …

Beginning database design


We will revisit SQL again in later weeks 

Page 70, Database Systems


School of Infocomm P04 – Contacts @ The Fig Leaf
Republic Polytechnic © 2012
Tina’s Problem
C207 – Database Systems
2012
Learning Journey …

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

Page 72, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
What's redundancy?
 In database terms, redundancy is storing data that is not
required. Such data is redundant or unnecessary. Redundant
data is not always duplicated data.

 For example, in Tina’s database it is necessary to re-enter


data if a student takes several modules, or joins several
interest groups (IG)

 In Tina’s database, it is difficult to count accurately how


many students are in a respective diploma program or IG

Page 73, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
What is a data anomaly?
 A data anomaly is an error (mistake). Data anomalies may be
created when we insert new data, update existing data or
delete data using a database that is not correctly normalised.

 Update anomaly: if the school decides to change a diploma


name, how many records need to be updated?

 Insert anomaly: If there is a new diploma in the school, can it


be added before students are enrolled in it?

 Delete anomaly: if there is only one DSM student record in


the table and it is accidently deleted, what will happen to the
DSM program?

Page 74, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Review of Primary Key Types
 Natural Key – has meaning outside the system.
Single Column  Examples include NRIC, Licence Plate Number, Area Code
Simple  Surrogate Key – has no meaning outside the
Primary Keys system,
 Previous examples include author_id and publisher_id
 Compound Key – is a combination of other
primary keys and no other column
 Example: the combination of isbn and school_id forms the key to
school_book_order
Multi-Column  Composite Key – Usually formed by adding
Primary additional columns to an existing key to get
Keys
uniqueness.
 Example: We start with last_name as a key to a person table, but
we eventually find two people with the same name. So we add
birthdate to the primary key to make it unique again. The final
primary key is the combination of last_name and birthdate.

Page 75, Database Systems


School of Infocomm P03 – Orders @ The Fig Leaf
Republic Polytechnic © 2012
What is normalisation?
 Normalisation is a technique for organizing data in relational
databases tables to minimize the duplication of data

 Why do we need Normalisation?


 To eliminate the data redundancy in database
 To avoid insert, update, and delete anomalies

Page 76, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
First normal form (1NF)
Table must not contain repeating groups of data
Table must have a primary key

One
student
may take
several
modules
every
semester

There is no single column that uniquely identifies a


particular record

Page 77, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
First normal form (1NF)
Table must not contain repeating groups of data
Table must have a primary key

Page 78, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Second normal form (2NF)
 Table must be in 1NF
 Non-key columns must not have partial dependencies on the
compound/composite primary key (no partial dependencies)
Compound/composite primary key

module_name only depends classroom depends on


on the module_code, but both module_code
not student_id and student_id
Page 79, Database Systems
School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Second normal form (2NF)
 Table must be in 1NF
 Non-key columns must not have partial dependencies on the
compound/composite primary key (no partial dependencies)
COMPOUND/COMPOSITE
primary key

Page 80, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Second normal form (2NF) – IG
 If you used the IG, you will have noticed a second partial
dependency that also needs to be normalised.

FIRST NORMAL FORM SECOND NORMAL FORM

Page 81, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Third normal form (3NF)
Table must be in 2NF
Table must not contain transitive dependencies
(columns that are functionally dependent on other non-key columns)

Diploma name relies on


diploma code. Diploma
code relies on student_id
Page 82, Database Systems
School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Third normal form (3NF)
Table must be in 2NF
Table must not contain columns that are
functionally dependent on other non-key columns

Page 83, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
The final solution
student student_ig
 student_id  student_id
 name  ig_id
 email  date_joined
 diploma_code
student_module
interest_group  student_id
 ig_id  module_code
 ig_name  classroom
 ig_supervisor
diploma_master
module_master  diploma_code
 module_code  diploma_name
 module_name
Page 84, Database Systems
School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Introduced this week

Normalisation
Compound keys
Bottom-up design by
1NF
2NF
3NF

Page 85, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
Next Week

Top-down design

Page 86, Database Systems


School of Infocomm P05 – Tina’s Problem
Republic Polytechnic © 2012
The RP Market
C207 – Database Systems
2012
Learning Journey …

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

Page 88, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
List all possible attributes ...

• Order Number • Description


• Ordered by • Price
• Ordered on • Item total price
• Delivery to • Delivery Charge
• Delivery address • GST
• Postal code • Total purchase price
• Item ID
• Description
• Quantity
• Unit Price
• Price
• Status

Page 89, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Clarify attribute names ...

• Order Number • Purchase Description


• (Ordered by) Member Name • Purchase Price
• Ordered on • Item total price
• Delivery name • Delivery Charge Percent
• Delivery address • GST Percent
• Delivery Postal code • Total purchase price
• Item ID
• Item Description
• Ordered Item Quantity
• Item Unit Price
• Item Price
• Ordered Item Status

Page 90, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Remove calculated attributes ...

• Order Number • Purchase Description


• Member Name • Purchase Price
• Ordered on • Item total price
• Delivery name • Delivery Charge Percent
• Delivery address • GST Percent
• Delivery Postal code • Total purchase price
• Item ID
• Item Description
• Ordered Item Quantity
• Item Unit Price
• Item Price
• Ordered Item Status

Page 91, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Logically group attributes ...

Member table Item table


• Member Name • Item ID
• Item Description
Order table • Item Unit Price
• Order Number
• Ordered on Charge table
• Charge Name
Delivery table • Charge Percent
• Delivery name
• Delivery address Unknown
• Delivery Postal code •Ordered Item Quantity
•Ordered Item Status

Page 92, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Choose Suitable Primary Keys ...

Member table Item table


• Member ID • Item ID
• Member Name • Item Description
• Item Unit Price
Order table
• Order Number Charge table
• Ordered on • Charge ID
• Charge Name
Delivery table • Charge Percent
• Delivery ID
• Delivery name Unknown
• Delivery address •Ordered Item Quantity
• Delivery Postal code •Ordered Item Status

Page 93, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Look for Relationships (1:m)
 Examine the candidate tables carefully,
Order table
looking for relationships. Revisiting the • Order Number
original documentation will help. • Ordered by
• Ordered on
 If you look carefully, you will notice that • Delivery ID (FK)
one order can have only one delivery
location but one delivery location may Delivery table
received many orders. The relationship • Delivery ID
between order and delivery is therefore • Delivery name
1:m • Delivery address
• Delivery Postal code
 In this case you take the key from the
“one” (the delivery table) and put it in
the “many” (the order table) as a
foreign key.
Page 94, Database Systems
School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Look for Relationships (m:n)
You may also notice that for every
Order table
order, there are multiple items. • Order Number
However, an item may be ordered not • Ordered by
just to one order, but to many. • Ordered on

Therefore the relationship between New Table

item and order is many to many (m:n) Ordered Item table


• Order Number
• Item ID
This m:n relationship can be resolved
• Ordered Item Quantity
through a resolving table. • Ordered Item Status
We now have a table in which to place
Item table
the Ordered Item Quantity and Ordered • Item ID
Item Status – columns that previously • Item Description
didn’t fit in any of the existing tables. • Item Unit Price

Page 95, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Continuing on …
 You will also discover another 1:m relationship between the
member and order tables and another m:n relationship
between the order and charge tables. The resolving table
for this m:n relationship does not have additional attribute.

 However please note, the solution presented in the next


slide is only one possible implementation. We could have
used different keys to give a different design.

 There are often many different database designs for a given


problem. Try and choose the best.

Page 96, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
ERD for The RP Market

Page 97, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Important - Run checks for Normalisation
 First normal form (1NF)

 Every table has a unique primary key


 No repeating groups
 Second normal form (2NF)

 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 98, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Bottom up?
 It is important to note that we could have started with a
single large table (as we did last week) and applied 1NF, 2NF
and 3NF in order.
 Doing so would have yielded very similar (perhaps identical)
result.
 What’s the best way to design a database?

 Identify all your attributes and try to group them logically.


Generally speaking, a good grouping of attributes will
identify good candidate table.
 Check your design satisfies 1NF, 2NF and 3NF. Modify
where necessary.
 Identify and add in additional tables or columns.
Page 99, Database Systems
School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
What we introduced this week

Converting documentation into a database

Top down database design

Normalisation checking

Page 100, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Next week …

Using MySQL Workbench


data modelling

Page 101, Database Systems


School of Infocomm P06 – The RP Market
Republic Polytechnic © 2012
Hollywood
C207 – Database Systems
2012
Learning Journey …

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
P09 Sakila II – SQL Revisited

Page 103, Database Systems


School of Infocomm P06 – The Dutch East India Company
Republic Polytechnic © 2012
Our Solution

Page 104, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
Models

 Given a business model, we can represent the data


requirements in a model.

 The model we use is called the ER diagram (or ERD)

 ERDs can greatly enhance communication among all


interested parties, such as business customers, DB
designers, application architects, and system users.

Page 105, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
Entities

 Entities are usually nouns, the thing about which we want to


collect data.

 Examples of entities may include students, books, calendar


entries, bank accounts

 Entities are shown as ‘boxes’ on an ERD.

Page 106, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
Attributes

 Attributes of an entity are usually nouns, to describe the


entity

 One or more attributes are used to define a primary key

 For example, in the movie entity, movie_id, title, category,


release year, length_in_min, ranting, and language are
attributes that describe the movie. The movie_id is the
primary key.

Page 107, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
Relationships

 Relationships show how entities associate or relate with


each other

 Many to many relationships are shown as a resolving table


(also known as intersect table or join table)

 An example of a resolving table was shown in the diagram


between movie and loan. A movie can be loaned to many
friends and a friend can loan many movies. The relationship
is m:n, and is resolved by creation of the loan table

Page 108, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
Resolving tables

 The resolving table may contain new columns besides the


keys, for example: The returned_date column in the loan
table.

 It is very common to find attributes that can be added to


resolving tables. It is a good idea to spend some time
considering such additional columns.

 Rarely is a table such as collaboration created where no


additional columns are present. In this case the
collaboration column exists simply for implementation of
the n:m relationship between studios and movies.

Page 109, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
An alternative
 Why didn’t we create a loan_id in the loan table, and use
movie_id and friend_id as normal foreign keys?

 Answer – we could have done so ...

 Can you see any advantages or disadvantages of this


approach?

NEW

OLD

Page 110, Database Systems


School of Infocomm P07 - Hollywood
Republic Polytechnic © 2012
What we introduced this week

Creation of a database from a textual description.

Resolving tables to handle m:n relationships

MySQL Workbench

Page 111, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
Next Week

Sakila

(More SQL)

Page 112, Database Systems


School of Infocomm P07 – Hollywood
Republic Polytechnic © 2012
Sakila I
C207 – Database Systems
2012
Learning Journey …

P05 Normalisation: bottom-up

P06 Normalisation: top-down


P07 From Functional Requirements to an ERD
P08 Sakila I – SQL Revisited
P09 Revision Week (OCL)
P10 Sakila II – SQL Revisited

Page 114, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
Aggregated functions
What is an aggregate function?

 An aggregated function is simply a function


that helps us to summarise large amounts
of data

 The idea of aggregated functions is to


reduce the programming effort, and make
your life easier ...

 Aggregated functions take column names


as parameters.

Page 115, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
MySQL aggregated functions

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

Page 116, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
How do we use aggregate functions?
 Aggregate functions can be used on their own

SELECT COUNT(*)
FROM film;

 Or they may be used in conjunction with other columns.


When used with other columns in the SELECT clause,
aggregated functions MUST be used with the GROUP BY
clause

SELECT rental_duration, COUNT(*)


FROM film
WHERE title like `%LOVE%`
GROUP BY rental_duration;
Page 117, Database Systems
School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
More examples of aggregated functions
 How would we find if the film’s rating affects
the replacement cost?

SELECT rating, AVG(replacement_cost)


FROM film
GROUP BY rating;

 How do we find which film group has the


highest total replacement cost?

SELECT rating, SUM(replacement_cost)


FROM film
GROUP BY rating
ORDER BY SUM(replacement_cost) DESC;

Page 118, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
In or out?
 The keyword IN, like nearly all SQL statements, has it’s roots
in set theory.

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)

 Here we can see that American movies are completely


contained by (or IN) a larger set of all movies.

Page 119, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
How do I define a set?
Sets that lie within other sets can be defined in two
main ways.

Defined by explicit membership

SELECT title, rating


FROM film
WHERE rating IN (`G`, `PG`, `M`);

Page 120, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
And how else can we define a set?
We can also define as set by using NESTED SQL
(also known as a SUB QUERY)
 A sub query is simply a query that we use within an
INSERT, UPDATE or DELETE statement

 The sub query first defines the set upon which the outer
statement operates

Page 121, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
A sub-query (nested query) example …
 The worksheet example asked you to
construct a reasonably easy query to select
the customer ID for all French customers ...

SELECT c.customer_id
FROM customer c, address a
,city ct ,country co
WHERE ...
AND co.country = 'France';

 This query may now be used in other


statements by including it as a sub-query (or
nested query).
Page 122, Database Systems
School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
How do we use a nested query?
We can use it to determine the rental date for all
French customers using the IN keyword.
SELECT rental_date
FROM rental
WHERE customer_id IN (
SELECT c.customer_id
FROM customer c, address a, city ct,
country co
WHERE ...
AND co.country = 'France'
);

Page 123, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
PROBLEM SOLUTIONS

Page 124, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
Problem statement solution 1
SELECT act.actor_id,
last_name,
first_name,
count(film_id) AS 'number_films'
FROM actor act,
film_actor fac
WHERE act.actor_id = fac.actor_id
GROUP BY act.actor_id, last_name, first_name
ORDER BY count(film_id) DESC;

Page 125, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
Problem statement solution 2
SELECT ctr.country,cty.city,
SUM(pmt.amount)
FROM country ctr,
city cty,
address adr,
customer cst,
payment pmt
WHERE ctr.country_id = cty.country_id
AND cty.city_id = adr.city_id
AND adr.address_id = cst.address_id
AND cst.customer_id = pmt.customer_id
GROUP BY ctr.country, cty.city
ORDER BY SUM(pmt.amount) DESC;

Page 126, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
Problem statement solution 3
SELECT rental_date, return_date
FROM rental
WHERE customer_id IN (
SELECT c.customer_id
FROM customer c, address a, city ct, country co
WHERE c.address_id = a.address_id
AND a.city_id = ct.city_id
AND co.country_id = ct.country_id
AND co.country = 'China'
)
ORDER BY rental_date DESC;

Page 127, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
What we introduced this week

COUNT(*)
Other aggregated functions
IN keyword
!
Nested Queries (Sub Queries)
GROUP BY

Page 128, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
Next Week

OFF-CAMPUS LEARNING
(OCL)

Revision

Page 129, Database Systems


School of Infocomm P08 – Sakila
Republic Polytechnic © 2012
Henry’s Helper
C207 – Database Systems
2012
ERD Interpretation
Q1) It is important to specify the data precision.
Data-type Comments
INT(n) Integer up to n characters long. Precision may be omitted. (-
2147483648 to 2147483647)
VARCHAR(n) String up to n characters long, variable length, not padded
DECIMAL(n,m) Decimal numbers. n represents the total length, m denotes the
fractional component. Hence DECIMAL(5,2) can hold a
maximum value of 999.99

Q2 ) All primary key must be unique. There must


be one primary key for each table.
Q3) A Compound/Composite Primary Key
consists of two or more attributes. The
uniqueness is for all attributes combined.
Page 131, Database Systems
School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
ERD Interpretation
Q4)Types of relationship
 One-to-one (1:1)
 One-to-many (1:m)
 Many-to-one (m:1)
 Many-to-many (m:n)

Q7) “Reading” relationship.


 All relationship is to be read with two sentences.
 “one product has one or more receipts”
 “one receipt has one or more products”

Q8) Data constraints


 NOT NULL
 NULL
 AUTO INCREMENT
Page 132, Database Systems
School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
ERD Interpretation
Q6) Types of primary key
 Natural Primary Key – has meaning outside the system
Single Column
 Surrogate Primary Key – has no meaning outside the Simple
system Primary Keys
 Compound Primary Key – is a combination of other
primary keys and no other column Multi-Column
 Composite Primary Key – Usually formed by adding Primary
additional columns to an existing key to get Keys
uniqueness
Q14) A Composite Primary Key can be
obtained by adding an attribute to the
Compound Primary Key.
 E.g. Compound key(receipt_no, product_code) +
quantity  Composite key (receipt_no,
product_code, quantity)
Page 133, Database Systems
School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Revision of Aggregated Functions

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;

Q: Can COUNT(c.name) be used instead of COUNT(*)?

Page 134, Database Systems


School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Revision of Aggregated Functions

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;

Q:What is the purpose of the formula


‘(sale_price - item_discount)*quantity’?

Page 135, Database Systems


School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Revision of Aggregated Functions

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;

Page 136, Database Systems


School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Revision of Sub-queries
Question SQL
20 SELECT product_code
FROM sale_item
WHERE receipt_no = 50909;

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);

Page 137, Database Systems


School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Report 2 Query

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;

Page 138, Database Systems


School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Completing the Schema

Page 139, Database Systems


School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Checking for Third Normal Form (Normalisation)

 First normal form (1NF)

 Eliminate calculated values


 Every table has a unique primary key
 No repeating groups
 Second normal form (2NF)

 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

Page 141, Database Systems


School of Infocomm P09 – Henry’s Helper
Republic Polytechnic © 2012
Sakila II
C207 – Database Systems
2012
Learning journey …

P06 From Functional Requirements to an ERD

P08 Sakila – SQL Revisited


P09 Revision Week - OCL
P10 Sakila II – SQL Revisited
P11 Transaction Processing
P12 Managing Multiple Users

Page 143, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
What is a view?
 A view is simply a convenient method of storing queries that
examine your data.

 Views often replace sub-queries when the sub-query is


complex, or when it is used often.

 You (generally) cannot insert, update or delete data in a


view.

Page 144, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Anatomy of a view
Syntax:
CREATE VIEW <view name>
AS <select statement>;

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;

Page 145, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
How does a view work?
Now, rather than selecting from a large query we
can now simply select from the view
SELECT * FROM actor_and_film;

Page 146, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
What else can be done with a view?
Views can be used to simplify complex queries

First, create a view for the Chinese Customers:

CREATE VIEW chinese_customer AS (


SELECT c.customer_id
FROM customer c, address a, city ct, country co
WHERE c.address_id = a.address_id
AND a.city_id = ct.city_id
AND co.country_id = ct.country_id
AND co.country = 'China'
);

Page 147, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
A view can simplify complex queries
Then use the view to replace a nested query
SELECT COUNT(*)
FROM rental
WHERE customer IN (
SELECT c.customer_id
FROM customer c, address a, city ct, country co
WHERE c.address_id = a.address_id
AND a.city_id = ct.city_id
AND co.country_id = ct.country_id
AND co.country = 'China'
)

SELECT COUNT(*)
FROM rental
WHERE customer_id IN (SELECT * FROM chinese_customer)

Page 148, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Group by with ‘having’
The HAVING clause enables us to be more selective
when using aggregated functions.

In particular the HAVING clause operates on the


aggregated function and limits the rows returned
by the query to a smaller set.

Page 149, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Having continued …
Let’s consider how many actors
names begin with a letter after ‘I’.
We write the following query and
obtain the result set shown below:
SELECT
SUBSTR(first_name,1,1) AS
initial,
COUNT(*)
FROM actor
WHERE SUBSTR(first_name,1,1) > 'I'
GROUP BY initial;

Page 150, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Having continued …
Now we only want to consider names where the
number of occurrences of the initial is greater than
10. We simply modify the query as follows and
obtain our result.

SELECT SUBSTR(first_name,1,1) initial, COUNT(*)


FROM actor
WHERE SUBSTR(first_name,1,1) > 'I'
GROUP BY initial
HAVING COUNT(*) > 10

Page 151, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Comparison functions
Function Description
= Equal to operator
>= Greater than or equal to operator
> Greater than operator
<= Less than or equal to operator
< Less than operator
!=, <> Not equal to operator
IS Test again a BOOLEAN value
IS NULL Test again a NULL value
LIKE Simple pattern match
SOUNDS LIKE Compare sounds

... and there are more String comparison functions

Page 152, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Date and time functions
Function Description
DATE(expr)(v4.1.1) Extract the date part of a date or datetime expression.
e.g., DATE(‘2008-06-20 01:02:03’) returns ‘2008-06-20’

DAYOFMONTH(date) Return the day of the month (1-31).


e.g., DAYOFMONTH(‘2008-06-20’) returns 20
DAYOFWEEK(date) Return the weekday index for date (1= Sunday,
2=Monday, …, 7=Saturday)

NOW() Return the current date and time as a value in ‘YYYY-MM-


DD HH:SS:MM’ format if it is used in a string context

HOUR(time) Extract the hour. The range of the return value is 0 to 23


MINUTE(time) Return the minute for the time, in the range 0 to 59
DATE_FORMAT(date, Formats the date value according to the format string
format)
... and there are more MySQL date and time functions

Page 153, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
String manipulation functions
Function Description
CONCAT(str1, str2,…) Return concatenated string. Can accept many parameters.
e.g., CONCAT(‘My’, ‘SQL’) returns ‘MySQL’
INSTR(str, substr) Return the index of the first occurrence of substring.
e.g., INSTR(‘database’, ‘a’) returns 2
LOWER(str) Return the argument in lowercase.
e.g., LOWER(‘MySQL’) returns ‘mysql’
REPLACE(str, Replace occurrences of a specified string. REPLACE() performs a case-
from_str, to_str) sensitive match when searching for from_str.
e.g., REPLACE(‘wWw.mysql.com’, ‘w’, ‘m’) returns ‘mWm.mysql.com’
SUBSTR(str, pos,[len]) Return the substring as specified.
e.g., SUBSTR(‘MySQL’, 3, 2) returns ‘SQ’
UPPER(str) Convert to uppercase.
e.g., UPPER(‘MySQL’) returns ‘MYSQL’
LENGTH(str) Returns the length of the string.
e.g., LENGTH(‘MySQL’) returns 5
… and there are more MySQL String functions

Page 154, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Using string functions
String functions are often used in database
applications

SQL string functions that operate on string


positions. The first position is 1, not 0 (unlike Java)

Example: Return the first 50 characters of the film


description of all movies

SELECT SUBSTR(description,1,50)
FROM film

Page 155, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solutions to Problem Statement

Page 156, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Tip for Writing Complex SQL
Add a little bit of SQL – Test – Repeat
(remember Lather – Rinse – Repeat)

Example – For Problem Statement Q1.

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;

Page 160, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solution 2 – Add, Test, Repeat.
3. Next we added in the remainder of the last name

SELECT SUBSTR(last_name,1,1),
SUBSTR(last_name,2,LENGTH(last_name) -1)
FROM actor;

Page 161, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solution 2 – Add, Test, Repeat.
4. The next step was to UPPER and LOWER the various bits

SELECT UPPER(SUBSTR(last_name,1,1)),
LOWER(SUBSTR(last_name,2,LENGTH(last_name) -1))
FROM actor;

Page 162, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solution 2 – Add, Test, Repeat.
5. The next step was to concatenate the two pieces together.

SELECT CONCAT(
UPPER(SUBSTR(last_name,1,1)),
LOWER(SUBSTR(last_name,2,LENGTH(last_name) -1))
)
FROM actor;

Page 163, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solution 2 – Add, Test, Repeat.
6. Now copy the relevant part, paste it, and make the necessary changes for the first
name. Note the order changed – but it doesn’t matter – we’ll fix that next.

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;

Page 164, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solution 2 – Add, Test, Repeat.
7. Finally add in the column aliases and the ORDER BY clause and we’re done!

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;

Page 165, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Solution 3
Follow the same add-test-repeat cycle to build up the SQL for the final report.

SELECT f.title AS "Title",


date_format(
r.return_date,
'%W, %D %M %Y') AS "Returned Date"
FROM film f,
inventory i,
rental r
WHERE f.film_id = i.film_id
AND i.inventory_id = r.inventory_id
ORDER BY r.return_date DESC;

Page 166, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
Next Week

MySQL running on a server


(not locally in your laptop)

Page 167, Database Systems


School of Infocomm P10 – Sakila II
Republic Polytechnic © 2012
The Novice
C207 – Database Systems
2012
Learning Journey

P08 Sakila – SQL Revisited

P09 Revision Week


P10 Sakila II – SQL Revisited
P11 MySQL – Transaction Processing
P12 MySQL – Managing Multiple Users
P13 MySQL – Procedures and Functions

Page 169, Database Systems


School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
What was today’s problem all about?

Today’s problem examined transaction processing

A database transaction begins when the user first logs on, or


when an they execute a COMMIT or ROLLBACK statement.

Following the logon, commit or rollback, one or more SQL


statements are executed.

The transaction concludes when the user logs off, or when


they execute a second COMMIT or ROLLBACK statement.

System crashes, loss of communication or unexpected log-


offs usually result in an automatic ROLLBACK.
Page 170, Database Systems
School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
What is the purpose of a transaction?
 A transaction ensures that
 ALL CHANGES COMMITTED or ...
 ALL CHANGES ARE ROLLED BACK

 In the banking example we showed that if


money is transferred TO one account it
must be transferred FROM another
account and both changes COMMITTED at
the same time.

 If we place money in one account without


removing the same amount from the other
account, then our transaction is broken.
Worse still, we invalidate account balances.
Page 171, Database Systems
School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
The problem’s cause
The cancellation of Adam’s existing (C-
reserve) ticket is not in the same transaction
as the issue of his new ticket (B-reserve)

When Adam is logged off due to inactivity,


the database saves all changes (commit)
rather than reverting all changes (rollback)

This commit results in the cancellation of


Adam’s existing ticket, without the issuance
of a new one. At this point Adam can neither
use his existing ticket, nor upgrade to a new
higher-value ticket.
Page 172, Database Systems
School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
Solution 1.
 The security logout could issue a ROLLBACK rather than a
COMMIT statement.
 Adam’s new B-reserve ticket would be cancelled
 Adam’s existing C-reserve ticket would remain valid
 Adam can then use his C-reserve ticket, or begin the
upgrade process again

ROLLBACK

Page 173, Database Systems


School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
Solution 2.
The current business processes can be altered so that the upgrade
payment, the cancellation of the old ticket and issuance of a new
ticket all occur within the same transaction boundary.

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.

 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.

 Isolation - No operation (or user) outside of the


transaction can see the data in an intermediate state.

 Durability- Once the user has been notified of a


transaction success it is persisted (permanently
saved) and cannot be rolled back.
Page 175, Database Systems
School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
What do others see when you update a row?
Other users reading the same table
see the database as it was PRIOR to
us beginning the transaction (the
old, but committed data)

The user performing the transaction


always views the data in it’s current
state

Page 176, Database Systems


School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
Completing the transaction
When we COMMIT or ROLLBACK the
transaction is completed.

If we committed our transaction then


all other users see the new data that
has just been committed.

If we rolled back then all other users


see the data unchanged by our
transaction.

Page 177, Database Systems


School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
The BIG idea
Be VERY careful how you define your transactions
Ensure that actions are grouped together into the
smallest possible transaction
Ensure that all actions in a transaction are either
ALL COMMITTED or ALL ROLLED BACK

Page 178, Database Systems


School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
What we introduced this week

MySQL in server
Central Database Server
Rollback and Commit
Transaction processing
MySQL procedures
ACID
Readers and Writers

Page 179, Database Systems


School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
Next Week

Monitoring activity
within databases

Page 180, Database Systems


School of Infocomm P11 – The Novice
Republic Polytechnic © 2012
Hacked!
C207 – Database Systems
2012
Learning Journey

P09 Revision Week

P10 Sakila II – SQL Revisited


P11 Transaction Processing
P12 Managing Multiple Users
P13 Procedures and Functions
P14 Correcting DB Schemas that don’t work

Page 182, Database Systems


School of Infocomm
Republic Polytechnic © 2012
GRANT
GRANT statement gives privileges to users on the
usage of the server, database, tables and columns.

Types of commonly used privileges:


 CREATE, ALTER, DROP
 SELECT, UPDATE, DELETE
 GRANT OPTION
 ALL PRIVILEGES

The REVOKE statement removes the privileges.

Page 183, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Anatomy of GRANT
Syntax:
GRANT <PRIVILIEGES> ON <table name>
TO <user name> [WITH GRANT OPTION];

REVOKE <PRIVILIEGES> ON <table name>


FROM <user name>;

Example:
GRANT SELECT, INSERT, UPDATE
ON department
TO 'ru_fasttrack'@'%';

Page 184, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Readers and writers …
A READER is a user that is attempting to
read rows in a table. This user will only
see currently committed data

A WRITER is a user that is attempting to


update, insert or delete data. This user
will be the only one to see the changes in
the middle of their own transaction. When
the writer commits the changes other
users (readers) can then view the new
data.

Page 185, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Read consistency
 People that want to READ data are unaware
that some data is being modified. BUT, they
don’t need to wait for the WRITERS to finish
their transaction.

 WRITERS can go ahead and alter the data.


Writers don’t need to wait for readers, even if
they are viewing/editing the same data.

 WRITERS only wait for other WRITERS if they


are both attempting to write the same data.

Page 186, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Updates at the same time
You are checking in on Singapore
Airlines using their on-line check-in
system. You see the following
diagram.

You decide you’d like to sit in seat


A33, and click to book.

2 seconds ahead of you some other


person books that seat, but you
still see it as available. What
happens?
Page 187, Database Systems
School of Infocomm
Republic Polytechnic © 2012
You lose
When you clicked, then internally you became a
WRITER, and unfortunately another WRITER began
their transaction earlier.

When this happens the application will inform you that


you were unsuccessful. It will then refresh the page,
show you the new seating layout and ask you to choose
again.
The database is kept consistent ... But you lose your
seat.

What would have happened if you had booked seat


A34? Both updates could have been performed at the
same time.
Page 188, Database Systems
School of Infocomm
Republic Polytechnic © 2012
CIA

Database
Security

Source: National Security


Telecommunications and Information
Systems Security Committee’s
Confidentiality, Integrity and Availability
(C.I.A.) Triangle
(NSTISSC)

Page 189, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Confidentiality
 Importance of understanding your data
• Need to determine the level of control and security
• Need to determine individual users accessibility to the data

High More Few

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

Page 190, Database Systems


School of Infocomm
Republic Polytechnic © 2012
More on confidentiality
Information is available only to those people (users)
that have authorised access
Authorised access has many levels, not just at the
database. Applications and networks may require
passwords and even make use of more
sophisticated mechanisms (SMS or key generators)
to ensure that when access is granted, is authorised.
Is it important? Very! Imagine what would happen
to a bank’s reputation if it’s systems were hacked
and people’s account balances were posted on the
internet.
Page 191, Database Systems
School of Infocomm
Republic Polytechnic © 2012
More on confidentiality
What mechanisms does a database use to enforce
confidentiality?
Passwords. The simplest ideas are often the best. Enforcing
a password that complies to certain criteria and which must
be changed periodically is very important.
Data Hiding (based on security tables) As we have seen
today we can ‘hide’ data from one person, whilst displaying
it to another.
Role based security (e.g., if you are granted a privileged role
you will be able to see and alter data that is not available to
others whom have not been granted that role)

Page 192, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Integrity
 Ensuring data is not incorrectly modified either maliciously or
by accident.
 What mechanisms exist to support integrity?

• There’s actually a few ways. One method is a journal (or


history) table. Every time data is modified or deleted then
an entry is written to this journal table.

• The journal table can only seen only by privileged users,


and other users do not actually even know the history is
being kept.

• If there is a problem, we can always see who modified the


data, when they modified it, and what they modified.
Page 193, Database Systems
School of Infocomm
Republic Polytechnic © 2012
Integrity continued
• Another way to preserve the integrity of a database is by
implementing structured backup plan.

• After backups are taken it is important that they are


checked and stored safely (possibly off-site).

• Periodically the backups must be tested to ensure that


they will work in the event of a failure.

• Many databases (including Oracle and MySQL) implement


a logging system that can be used to ‘roll forward’ from
the last backup to the exact time of failure.

Page 194, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Availability
 Availability is often discussed in terms of a
percentage. For example, Amazon database must
be 99.99% available.

 Available means that people and applications that


need the data can access that data in a timely
manner.

 Database Administrators (DBA) personnel are


responsible for maintaining the database in an
available state.

Page 195, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Availability continued
Availability Approximate Downtime per year
Percentage
99.999% 5 minutes
99.99% 50 minutes
99.95% 4.5 hours
99.9% 9.0 hours
99.8% 18 hours
99.5% 45 hours
99% 88 hours
98% 7.3 days (top 50 retailers 2008)

Page 196, Database Systems


School of Infocomm
Republic Polytechnic © 2012
The cost of downtime
 Amazon

The answer is simple: about $33.96 in revenue per second (or a


modest $122,260.27 an hour), based on Amazon.com’s $10.71
billion dollar revenues in 2007. Which translates into a lost
profit, based on annual profit figures of $190 million, of $6.02
per second (or $21,689.50 an hour).
Our Expectation: Actual figure may be less than $6.02 per second since many customers would not be lost, but
would merely delay their purchase. However, the cost of downtime is still obviously quite real and very
important to on-line businesses.

Page 197, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Availability continued
 Other measures taken to ensure availability
 RAID (Redundant Array of Independent Disks)
 Database Mirroring
 Failover Clustering
 Mirrored Backup Media

Page 198, Database Systems


School of Infocomm
Republic Polytechnic © 2012
What we introduced this week

The CIA model


Data Hiding
Managing database users
Triggers
Grants &
Audit Trails

Page 199, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Next Week

Procedures, Triggers & Functions


Using MySQL Server

Page 200, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Tabloid Writer
C207 – Database Systems
2012
Learning Journey

P10 Revision Week

P11 Transaction Processing


P12 Managing Multiple Users
P13 Procedures and Functions
P14 Correcting DB Schemas that don’t work
P15 Specification to DB. The journey is completed.

Page 202, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
Creating an audit trail
Create a table exactly the same as the table you
want to audit, but add the modified_by and
modified_date columns.

CREATE TABLE `story_jn`(


jn_id INT(3)NOT NULL AUTO_INCREMENT,
story_name VARCHAR(50) NOT NULL, story_text
VARCHAR(2000) NOT NULL, complete_flag VARCHAR(1)
NOT NULL, created_by VARCHAR(50) NOT NULL,
created_date DATETIME NOT NULL, modified_by
VARCHAR(50) NOT NULL, modified_date DATETIME
NOT NULL, PRIMARY KEY (jn_id)
);

Page 203, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
Creating the audit trail
Create trigger/s to capture the before image and
copy it to the audit table. This trigger fires
whenever an update or delete event occurs on the
base table.

Page 204, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
The trigger
CREATE TRIGGER `before_update_story` CREATE TRIGGER `before_delete_story`
BEFORE UPDATE ON story BEFORE DELETE ON story
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
INSERT INTO story_jn ( INSERT INTO story_jn (
story_name, story_name,
story_text, story_text,
complete_flag, complete_flag,
created_by, created_by,
created_date, created_date,
modified_by, modified_by,
modified_date) modified_date)
VALUES ( VALUES (
OLD.story_name, OLD.story_name,
OLD.story_text, OLD.story_text,
OLD.complete_flag, OLD.complete_flag,
OLD.created_by, OLD.created_by,
OLD.created_date, OLD.created_date,
USER(), USER(),
NOW() NOW()
); );
END$$ END$$

Page 205, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
The procedure
 The problem required the creation of a
procedure to update the complete flag
on a selected story.

 The procedure takes as input parameters


the story name, and the flag (Y/N)

 The procedure performs a simple


update.

 Procedures are often used to handle


very complex and/or repetitive functions

Page 206, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
The procedure

CREATE PROCEDURE update_complete_flag


(IN param_story_name VARCHAR(50),
IN param_complete_flag VARCHAR(1))

BEGIN
UPDATE story
SET complete_flag = param_complete_flag
WHERE story_name = param_story_name;

END$$

Page 207, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
Create the view
You were asked to create a view that enabled the
users to see the CURRENT view and the
HISTORICAL view of all stories.

You need to join together the story, and the


story_jn tables BUT the two tables are NOT related.
You could NOT use keys!

How can we show the results of two unrelated


tables?

Page 208, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
UNION ALL
One way is to use a union, to join both tables (sets)
together.

CREATE VIEW all_story_and_updates


AS
SELECT story_name, story_text,
complete_flag, created_by,
DATE_FORMAT(created_date, '%D %M %Y %T')
AS 'Modify Date'
FROM story
UNION ALL
SELECT story_name, story_text,
complete_flag, modified_by,
DATE_FORMAT(modified_date, '%D %M %Y %T')
AS 'Modify Date'
FROM story_jn
ORDER BY story_name , 'Modify Date' DESC;

Page 209, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
Functions
 Functions are similar to procedures, but return a value

 Some of functions include

 Masking very complex processing by returning a single


variable

 Performing database manipulations, and then notifying


the user of success or failure

 Hiding underlying data structures

Page 210, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
Return from a function

CREATE FUNCTION update_complete_flag_f


(param_story_name VARCHAR(50),
param_complete_flag VARCHAR(1))
RETURNS VARCHAR(20)

BEGIN

UPDATE story
SET complete_flag = param_complete_flag
WHERE story_name = param_story_name;

RETURN "update complete";

END$$

Page 211, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
Finally…
There are many database objects that provide useful
functionality. Database objects are NOT limited simply to
tables and views

We have considered TABLES, VIEWS, TRIGGERS,


PROCEDURES and FUNCTIONS in this course

Other objects exist, e.g., SEQUENCES, ROLES, PACKAGES,


SYNONYMS and many others.

Objects do vary in their implementation from database to


database, but the concepts remain the same. We have seen
these objects using MySQL, but other databases have similar
objects.
Page 212, Database Systems
School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
What we introduced this week

 Creating a procedure

 Creating a trigger

 Creating a function

 Granting EXECUTE functionality

 Running stored procedures and


functions

Page 213, Database Systems


School of Infocomm P13 – Tabloid Writer
Republic Polytechnic © 2012
Next Week

Database Validation

Page 214, Database Systems


School of Infocomm
Republic Polytechnic © 2012
EPL
C207 – Database Systems
2012
Learning Journey

P11 MySQL – Transaction Processing

P12 MySQL – Managing Multiple Users


P13 MySQL – Procedures and Functions
P14 Correcting DB Schemas that don’t
work
P15 Specification to DB. The journey is completed.

Page 216, Database Systems


School of Infocomm
Republic Polytechnic © 2012
What happens when we enter the clubs?
INSERT INTO club
(club_id,
club_name,
home_colours,
away_colours)
VALUES
(3,
'Middlesbrough',
'Red with White Bar',
'Blue Black Stripes');

Page 217, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Correction

Increase the precision on the club_name column from 10 to 20.

Page 218, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Enter values for the stadiums
 There’s no place for the stadium name!
 Also, overall rank is a VARCHAR field, when it’s recording a
numeric value!
 We can’t tell which club calls the stadium home. It must be
changed as shown below.

Page 219, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Enter Chelsea’s Last Seven Results
 Attempting to enter only seven results out of eight
produces an error because all eight results are mandatory.
The following solution will work.

Page 220, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Adding the 8th result
 Method 1.
 7th record becomes 8th record etc ...

UPDATE last_eight
SET result8 = result7,
result7 = result6,
result6 = result5,
result5 = result4,
result4 = result3,
result3 = result2,
result2 = result1,
result1 = 'W'
WHERE club_id = 2;

Page 221, Database Systems


School of Infocomm
Republic Polytechnic © 2012
An alternative approach.
Remove the last_eight table completely!
Derive the information from the games table.

We can calculate the wins for a team (let’s say


Arsenal) as follows

SELECT game_id, 'W'


FROM game
WHERE (home_club_id = 1 AND home_club_points >
away_club_points)
OR (away_club_id = 1 AND away_club_points >
home_club_points)

Page 222, Database Systems


School of Infocomm
Republic Polytechnic © 2012
An alternative approach.
We can calculate the losses for Arsenal in a similar
fashion

SELECT game_id, ‘L'


FROM game
WHERE (home_club_id = 1 AND home_club_points <
away_club_points)
OR (away_club_id = 1 AND away_club_points <
home_club_points)

Page 223, Database Systems


School of Infocomm
Republic Polytechnic © 2012
And we can combine these queries
The following query will give us a current view of
last 8 games played by Arsenal
No problem if less than (or more than) 8 games have been played

SELECT game_id, 'W'


FROM game
WHERE (home_club_id = 1 AND home_club_points > away_club_points)
OR (away_club_id = 1 AND away_club_points > home_club_points)
UNION
SELECT game_id, 'L'
FROM game
WHERE (home_club_id = 1 AND home_club_points < away_club_points)
OR (away_club_id = 1 AND away_club_points < home_club_points)
UNION
SELECT game_id, 'D'
FROM game
WHERE (home_club_id = 1 AND home_club_points = away_club_points)
OR (away_club_id = 1 AND away_club_points = home_club_points)
ORDER BY game_id DESC
LIMIT 8;

Page 224, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Seating capacity ... adding a column
The easiest way to record seating capacity is to add
a column, insert the data in Workbench and
regenerate the schema script.
Alternatively, you could update the ERD model to
ensure it’s kept current first, then run the following
SQL statement

ALTER TABLE stadium


ADD COLUMN seating_capacity INT(7) NOT NULL;

It’s easy to then add the capacity and write the


required SQL statement.
Page 225, Database Systems
School of Infocomm
Republic Polytechnic © 2012
Adding match data
 To record the date of a match, we need a column to hold
that data
 To record a future match both home_club_points and
away_club_points columns must be made optional
 In general order your columns so that the mandatory
columns appear first (design principle)

Page 226, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Multiple player positions
This problem is more difficult ...
The primary key for position is player_id and
game_id (COMPOUND primary key)
We can record the UNIQUE combination of player
and game
We CANNOT record multiple positions because if a
player has the same position in the same game then
the PRIMARY KEY will be the duplicated
The PRIMARY KEY can never contain duplicates!

Conclusion: The KEY needs to change!

Page 227, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Possible solution ...
We could add an ID field as the primary key and remove the compound primary
key (player_id + game_id). The position_id is now known as a SURROGATE primary
key.

A new table is created to store the position description, for example goalkeeper.

Why is it best not to place the position description in the game_player_position


table? Hint: Normalisation rule

Page 228, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Recording multiple statistic records
 The solution chosen here was to include a year (stored as an
integer) into the primary key, creating a COMPOSITE key.
Multiple statistic records can then be accessed (by year)
 Question: Why was a statistics_id NOT created, as it was for
the position table?

Page 229, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Our final solution

Page 230, Database Systems


School of Infocomm
Republic Polytechnic © 2012
What’s the difference?
 What does the ‘o’ represent on the relationship near club?
 How do you “read” these relationships?

Page 231, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Covered this week

Database Validation
Compound v. Composite Keys
UNION in SQL

Page 232, Database Systems


School of Infocomm
Republic Polytechnic © 2012
Next Week

Database Design

Page 233, Database Systems


School of Infocomm
Republic Polytechnic © 2012
ERIS
C207 – Database Systems
2012
Learning Journey

P12 MySQL – Managing Multiple Users

P13 MySQL – Procedures and Functions


P14 Correcting DB Schemas that don’t work
P15 Specification to DB

Page 235, Database Systems


School of Infocomm P15 – ERIS
Republic Polytechnic © 2012
Our Solution

Page 236, Database Systems


School of Infocomm P15 – ERIS
Republic Polytechnic © 2012
What we introduced this week

Practical application of
previous learning in C207

Page 237, Database Systems


School of Infocomm P15 – ERIS
Republic Polytechnic © 2012

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy