You are on page 1of 30

IFN 554

Databases
Tutorial 3: Data Modelling

School of Information Systems


Faculty of Science
IFN 554: Databases
School of Information Systems
Outline

Part 1: Lecture 3 Summary & Normalisation

Part 2: SQL

Part 3: Assessment Task 2

IFN 554: Databases


School of Information Systems
Part 1 – Lecture 3 Summary and Normalisation

IFN 554: Databases


School of Information Systems
Activity Revision
Normalisation is to organize tables in a manner that reduces redundancy and
dependency of data.

A table is in 1NF: if and only if all underlying simple domains contain atomic values only.
A table is in 2NF: It should satisfy the rule that a non-key attribute must be a fact about
the whole key, not part of the key.
A table is in 3NF: It should avoid that a non-key attribute provides a fact about another
non-key attribute (i.e., non-transitive dependency).

IFN 554: Databases


School of Information Systems
Activity 1
A table is in 1NF: if and only if all underlying simple domains contain atomic values only.

IFN 554: Databases


School of Information Systems
Activity 1
A table is in 2NF: It should satisfy the rule that a non-key attribute must be a fact about
the whole key, not part of the key.
 “Key”: Single attribute or composition of multiple attributes for identifying each record
( “uniqueness constraint” in ORM model)
 “Whole key”: The combination of multiple values (e.g., Student_ID and Unit)

IFN 554: Databases


School of Information Systems
Activity 1
Q1. The following is 1NF table, make it 2NF table.
Table: TempStaffAllocation (Not2NF)

Whole Key Non- Key

staffNo branchNo branchAddress name Position hoursPerWeek


S4555 B002 City Centre Plaza, Seattle WA 98122 Ellen Layman Assistant 16
S4555 B004 16 - 14th Avenue, Seattle WA 98128 Ellen Layman Assistant 9
S4612 B002 City Centre Plaza, Seattle WA 98122 Dave Sinclair Assistant 14
S4612 B004 16 - 14th Avenue, Seattle WA 98128 Dave Sinclair Assistant 10
Composite Primary Key

IFN 554: Databases


School of Information Systems
TempStaff(staffNo, name, Position)

Activity 1 Branch(branchNo, branchAddress)


TempStaffAllocation(staffNo, branchNo, hoursPerWeek)
Q1. The following is 1NF table, make it 2NF table.
Table: TempStaff (2NF) Table: Branch (2NF)

staffNo name Position branchNo branchAddress


S4555 Ellen Layman Assistant B002 City Centre Plaza, Seattle WA 98122
S4555 Ellen Layman Assistant B004 16 - 14th Avenue, Seattle WA 98128
S4612 Dave Sinclair Assistant B002 City Centre Plaza, Seattle WA 98122
S4612 Dave Sinclair Assistant B004 16 - 14th Avenue, Seattle WA 98128
Primary Key Table: TempStaffAllocation (2NF) Primary Key

staffNo branchNo hoursPerWeek


S4555 B002 16

Becomes a Foreign Key S4555 B004 9


S4612 B002 14
S4612 B004 10 Becomes a Foreign Key
Composite Primary Key

IFN 554: Databases


School of Information Systems
Activity 1 - Normalisation
A table is in 2NF: It should satisfy the rule that a non-key attribute must be a fact
about the whole key, not part of the key.
 “Key”: Single attribute or composition of multiple attributes for identifying each record (
“uniqueness constraint” in ORM model)
 “Whole key”: The combination of multiple values (e.g. StaffNo and BranchNr)

Q2. The following is 1NF table, make it 2NF table.

Customer Date Item Company City


Tom Hanks 03/03/2020 HDD IBM New York
Tom Hanks 04/03/2020 Memory IBM New York
Julia Roberts 01/02/2020 Software MS Washington
Will Smith 10/01/2020 CD MS Washington

IFN 554: Databases


School of Information Systems
Activity 1 - Normalisation
Customer Date Item Company City
Tom Hanks 03/03/2020 HDD IBM New York
Tom Hanks 04/03/2020 Memory IBM New York
Julia Roberts 01/02/2020 Software MS Washington
Will Smith 10/01/2020 CD MS Washington

Whole Key Non-Key

1. Is Item the fact about the whole key?


2. Is Company the fact about the whole key?
3. Is City is the fact about the whole key?

Hint! If the answer is No, split the table into two tables!

IFN 554: Databases


School of Information Systems
Activity 1 - Normalisation
Q2. The following is 1NF table, make it 2NF table.
Whole Key Non-Key

Customer Date Item Company City


Tom Hanks 03/03/2020 HDD IBM New York
Tom Hanks 04/03/2020 Memory IBM New York
Julia Roberts 01/02/2020 Software MS Washington
Will Smith 10/01/2020 CD MS Washington

Becomes a Foreign Key


Customer Date Item Customer Company City
Tom Hanks 03/03/2020 HDD Tom Hanks IBM New York
Tom Hanks 04/03/2020 Memory Julia Roberts MS Washington
Julia Roberts 01/02/2020 Software Will Smith MS Washington
Will Smith 10/01/2020 CD Primary Key
Composite Primary Key

IFN 554: Databases


School of Information Systems
Activity 1 - Normalisation
A table is in 3NF: It should avoid that a non-key attribute provides a fact about
another non-key attribute (i.e., non-transitive dependency).
-> The non-key attributes should be mutually independent!

Q3. There are 2NF tables, make them 3NF.

ID Name Account_No Bank_Code_No Bank


1 Robin Williams 1223 B1 Commonwealth
2 Julia Roberts 2345 B2 Westpac
3 Will Smith 5678 B1 Commonwealth
4 Lady Gaga 1111 B3 Bank of Queensland

IFN 554: Databases


School of Information Systems
Activity 1 - Normalisation
Q3. There are 2NF tables, make them 3NF.
ID Name Account_No Bank_Code_No Bank
1 Robin Williams 1223 B1 Commonwealth
2 Julia Roberts 2345 B2 Westpac
3 Will Smith 5678 B1 Commonwealth
4 Lady Gaga 1111 B3 Bank of Queensland
Becomes a Foreign Key

ID Name Account_No Bank_Code_No Bank_Code_No Bank


1 Robin Williams 1223 B1 B1 Commonwealth
2 Julia Roberts 2345 B2 B2 Westpac
3 Will Smith 5678 B1 B1 Commonwealth
4 Lady Gaga 1111 B3 B3 Bank of Queensland
Primary Key Primary Key

IFN 554: Databases


School of Information Systems
Activity 1 - Normalisation
Q4. There are 2NF tables, make them 3NF.
Customer Date Item Customer Company City
Tom Hanks 03/03/2020 HDD Tom Hanks IBM New York
Tom Hanks 04/03/2020 Memory Julia Roberts MS Washington
Julia Roberts 01/02/2020 Software Will Smith MS Washington
Will Smith 10/01/2020 CD Primary Key
Composite Primary Key Non-Key
Customer also Foreign key Non-Key

Only 1 non-key attribute – no further action required 2 non-key attribute – consider the following questions

Tip. If “Non key” in a table comprises two or more columns,


consider the following questions.

IFN 554: Databases


School of Information Systems
Activity 1 - Normalisation
Customer Date Item Customer Company City
Tom Hanks 03/03/2020 HDD Tom Hanks IBM New York
Tom Hanks 04/03/2020 Memory Julia Roberts MS Washington
Julia Roberts 01/02/2020 Software Will Smith MS Washington
Will Smith 10/01/2020 CD Primary Key
Non-Key
1. Is Company the fact about City (Non-key)? No

2. Is City the fact about Company (Non-Key)? Yes


Hint! If any answer is Yes, split the table into two tables!
Becomes a Foreign Key
Customer Company Company City
Tom Hanks IBM IBM New York
Primary Key
Julia Roberts MS MS Washington
Primary Key
Will Smith MS
IFN 554: Databases
School of Information Systems
Part 2 - SQL
IFN 554: Databases
School of Information Systems
Activity 2 – Install MySQL
Follow instructions given in canvas
Go to: https://www.mysql.com/downloads/

Download database file Dreamhome.db from Week 3 module on canvas


https://www.youtube.com/watch?v=u96rVINbAUI

IFN 554: Databases


School of Information Systems
Activity 2 – Install MySQL
1 3
1

2 4

https://www.youtube.com/watch?v=u96rVINbAUI

IFN 554: Databases


School of Information Systems
Activity 2 – Import the data file

1 3&4

IFN 554: Databases


School of Information Systems
Activity 3 – SQL: Create Tables
Task: Create two table including all attributes and constraints
Registration (clientNo, branchNo, staffNo, dateJoined)

Client (clientNo, fname, lname, telNo, preType, maxRent)

Registration Registration
clientNo branchNo staffNo dateJoined
• The combination of clientNo and branchNo is the primary key for
CR76 B005 SL41 2004-04-02 Registration table
CR56 B003 SG37 2004-04-11 • There are three (3) foreign keys:
• Client (clientNo)
CR74 B003 SG37 2004-11-16 • Staff (staffNo)
CR62 B007 SA9 2004-03-07 • Branch(branchNo)
* Branch and Staff tables already exist in the Dream Homes database
Client
clientNo fname lname telNo preType maxRent
Client
CR56 Aline Stewart 0414-848-1825 Flat 350
• clientNo is the primary key
CR62 Mary Tregear 0224-196720 Flat 600
• maxRent in the table is decimal type
CR74 Mike Ritchie 01475-392178 House 750
CR76 John Kay 0207-774-5632 Flat 425

IFN 554: Databases


School of Information Systems
Activity 3 – SQL: Create Tables
Task: Create two table including all attributes and constraints
Q1. Identify tables and table names (relations).
• Table 1: Registration
• Table 2: Client
• Foreign key in Registration table indicates a relationship between both

Q2. Identify columns and their data types (attributes and domains)​.

• Table Registration: ClientNo (TEXT), BranchNo (TEXT), StaffNo (TEXT), DateJoined (TEXT)
• Table Client: ClientNo (TEXT), fname (TEXT), lname (TEXT), telNo (TEXT), preType (TEXT),
maxRent (REAL)

In MySQL, data type: TEXT (String), INTEGER (Numeric- integer) and REAL (Numeric- decimal).
Boolean is not supported (instead, integers 0 (false) and 1 (true) can be stored).

IFN 554: Databases


School of Information Systems
Activity 4 – SQL: Create Tables
Q3. Identify constraints (NOT NULL, Primary key, Foreign Keys)​.

From the schema, we can identify primary key and foreign key.

Registration

• The combination of clientNo and branchNo is the primary key for


Registration (clientNo, branchNo, staffNo, dateJoined) Registration table
• There are three (3) foreign keys:
• Client (clientNo)
• Staff (staffNo)
Client (clientNo, fname, lname, telNo, preType, maxRent) • Branch(branchNo)
* Branch and Staff tables already exist in the Dream Homes database

Client

• clientNo is the primary key


• maxRent in the table is decimal type

IFN 554: Databases


School of Information Systems
Activity 4 – SQL Revision: Create Tables
To create a table, use the following SQL command for Creating Table in MySQL
** Commands are in black and variable input is noted in Green

Create table TableName (


ColumnName1 Text Not Null, Important little things ‘,’ at the end of lines
SQL
Code
ColumnName2 Text,
primary key (ColumnName1, ColumnName2, …),
foreign key (ColumnName) references Reference TableName (ColumnName)
); Important little things ‘;’ at the end of command

IFN 554: Databases


School of Information Systems
Activity 4 – SQL Revision: Alter Tables
You can make some changes to an existing table by ALTER TABLE command.

The command is used:​


• To add a new column to a table: ​

SQL ALTER TABLE table_name (e.g., Client)


Code
ADD new column_name (e.g., age) datatype (e.g., REAL)

• To rename the name of table: ​

SQL ALTER TABLE table_name (e.g., Client)


Code
RENAME TO new table_name (e.g., client_old)​
MySQL DROP TABLE documentation: https://dev.mysql.com/doc/refman/8.0/en/drop-table.html

IFN 554: Databases


School of Information Systems
Activity 4 – SQL Revision: Delete Tables
You can delete a table by DROP TABLE command as follow.

DROP TABLE table_name [RESTRICT | CASCADE] (e.g., DROP TABLE


SQL
Code
Movie RESTRICT);

• Restrict option:
To prevent the parent table from being deleted if any other tables
refer to it (foreign key).

• Cascade option:
To always delete the parent table together with all the tables
where they are referred (foreign key). ​

MySQL DROP TABLE documentation: https://dev.mysql.com/doc/refman/8.0/en/drop-table.html

IFN 554: Databases


School of Information Systems
Activity 4 – SQL: Create Tables
Q4. Create table Registration in DB Browser
CREATE TABLE Registration(
clientNo VARCHAR(10),
branchNo VARCHAR(10),
staffNo VARCHAR(10),
dateJoined VARCHAR(20),
PRIMARY KEY(clientNo, branchNo),
FOREIGN KEY (clientNo) REFERENCES Client (clientNo) ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (branchno) REFERENCES Branch (branchno) ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (staffNo) REFERENCES Staff (staffNo) ON DELETE SET NULL ON UPDATE CASCADE
);
On delete Set Null on Update Cascade refers to the behaviour of the table on delete

IFN 554: Databases


School of Information Systems
Activity 4 – SQL: Create Tables
Q5. Create table Client in DB Browser
CREATE TABLE Client (
clientNo VARCHAR(10) NOT NULL,
fName TEXT NOT NULL,
lName TEXT NOT NULL,
telNo TEXT,
preType TEXT,
maxRet REAL, -- decimal number
PRIMARY KEY(clientNo)
);

IFN 554: Databases


School of Information Systems
Activity 4 – SQL: Create Tables
Q6. Add a new column (age, REAL type) to Client table.

ALTER TABLE Client ADD new_column REAL

SELECT * FROM Client

Q7. Rename the name of Client table to Client_old.


ALTER TABLE Client RENAME TO Client_old

SELECT * FROM Client_old

Q8. Delete table ”Viewing”

DROP TABLE IF EXISTS Viewing

IFN 554: Databases


School of Information Systems
Activity 4 – SQL: Create Tables
Q9. Delete table “Client_old”

DROP TABLE Client_old

Q10. Delete table “Registration”

DROP TABLE IF EXISTS Registration

IFN 554: Databases


School of Information Systems
IFN 554: Databases
School of Information Systems

You might also like