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 - 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
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
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.sql from Week 3 module on canvas
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).
Q2. Identify columns and their data types (attributes and domains).
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,
ColumnName2 Text,
primary key (ColumnName1, ColumnName2, …),
foreign key (ColumnName) references Reference TableName (ColumnName)
);
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:
ALTER TABLE table_name (e.g., Client)
ADD new column_name (e.g., age) datatype (e.g., REAL)
• To rename the name of table:
ALTER TABLE table_name (e.g., Client)
RENAME TO new table_name (e.g., client_old)
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
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).
IFN 554: Databases
School of Information Systems
Activity 4 – SQL: Create Tables
Q4. Create table Registration in DB Browser
Q5. Create table Client in DB Browser
Q6. Add a new column (age, REAL type) to Client table
Q7. Rename the name of Client table to Client_old
Q8. Delete table ”Viewing”
Q9. Delete table “Client_old”
Q10. Delete table “Registration”
IFN 554: Databases
School of Information Systems