Database Coursework (2)
Database Coursework (2)
TABLE OF CONTENTS
TABLE OF CONTENTS.......................................................................................................................1
Part 1....................................................................................................................................................... 3
QUESTION A................................................................................................................................... 4
1. Functional/Non-Functional Requirements..............................................................................4
1.1 Functional Requirements................................................................................................4
1.2 Non-Functional Requirements........................................................................................5
2. Business Case..........................................................................................................................6
2.1 System Purposes.............................................................................................................6
2.2 Key Users and Their Roles.............................................................................................6
3. Main Entities, Relationships................................................................................................... 6
3.1 Entities............................................................................................................................6
3.2 Relationships.................................................................................................................. 6
4. Business Scenarios.................................................................................................................7
User Registration and Role Assignment...............................................................................7
Form Submission and Processing.........................................................................................7
Room Assignment and Stay Management........................................................................... 8
Payment Handling................................................................................................................ 8
Feedback Collection............................................................................................................. 8
Role-Based Access and Permissions (through Rights)........................................................ 8
QUESTION B....................................................................................................................................9
1. Conceptual ER Diagram......................................................................................................... 9
2. Specify Keys, Relationships, Attributes (Logical ERD provided)......................................... 9
2.1 Constraints (Primary/Foreign Key)................................................................................ 9
2.2 Relationships (Updated)............................................................................................... 10
2.3 Attributes...................................................................................................................... 11
2.4 Logical ER Diagram.....................................................................................................13
3. Physical ER Diagram............................................................................................................ 13
3.1Note of Explanation.......................................................................................................14
3.1.1 Constraints (NULL / NOT NULL)..................................................................... 14
User Profile............................................................................................................14
User Role...............................................................................................................15
Rights.....................................................................................................................15
User Account.........................................................................................................15
Next of Kin............................................................................................................16
Source....................................................................................................................16
Form...................................................................................................................... 17
Payment................................................................................................................. 17
Room..................................................................................................................... 17
Stay........................................................................................................................18
Accommodation.................................................................................................... 19
Feedback................................................................................................................19
3.1.2 Data Types.......................................................................................................... 19
1
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
QUESTION C..................................................................................................................................20
Normalization Process (1NF->3NF).........................................................................................21
1. First Normal Form (1NF)......................................................................................................21
2. Second Normal Form (2NF)................................................................................................. 21
3. Third Normal Form (3NF).................................................................................................... 21
QUESTION D................................................................................................................................. 21
QUESTION E..................................................................................................................................21
QUESTION F.................................................................................................................................. 21
Part 2..................................................................................................................................................... 22
QUESTION A................................................................................................................................. 22
● Query:.................................................................................................................................... 22
● Output:................................................................................................................................... 23
QUESTION B..................................................................................................................................25
● Query:.................................................................................................................................... 25
● Output:................................................................................................................................... 25
QUESTION C..................................................................................................................................27
● Query:.................................................................................................................................... 28
● Output:................................................................................................................................... 28
QUESTION D................................................................................................................................. 29
● Query:.................................................................................................................................... 30
● Output:................................................................................................................................... 30
QUESTION E..................................................................................................................................31
● Query:.................................................................................................................................... 31
● Output:................................................................................................................................... 32
QUESTION F.................................................................................................................................. 32
● Query:.................................................................................................................................... 33
● Output:................................................................................................................................... 33
QUESTION G................................................................................................................................. 34
● Query:.................................................................................................................................... 34
● Output:................................................................................................................................... 35
QUESTION H................................................................................................................................. 35
● Query:.................................................................................................................................... 36
● Output:................................................................................................................................... 36
2
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
Part 1
(Design & Implementation of Database System)
3
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
NOTE:
QUESTION A.
Provide proof of requirements elicitations to get some of the pertinent requirements. Clearly
demonstrate the scenario analysis and define the database design's scenario entities.
1. Functional/Non-Functional Requirements
+ Users must register, log in, and manage accounts via User Account
+ Each User Account is linked to a User Profile and a User Role (Client or
Staff)
- Form Submission & Processing
- Payment Processing
4
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
+ Payments are recorded for each stay
+ Staff manage forms and rooms but cannot access client payments
+ The system tracks a client’s Source (e.g., illness, poverty, immigration status)
5
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
- Maintainability & Modifiability
2. Business Case
3.1 Entities
User Account Stores login credentials and security details for users
3.2 Relationships
6
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
User Profile is User Role 1:1 Each user profile has one role
(Client/Staff)
User Profile uses User Account 1:1 Each user profile is linked to one user
account.
User Profile has Next of Kin 1:0..N A client can have none or multiple next
(Client) of kin
User Profile has Source N:1 Many clients can have the same source
(Client)
User Profile submits Form 1:N A client can submit many forms
(Client)
User Profile processes Form 1:N Staff can process many forms
(Staff)
User Profile writes Feedback 1:0..N A client can write none or multiple
(Client) feedback
User Profile reviews Feedback 1:0..N Staff can review none or multiple
(Staff) feedback
User Role granted Rights 1:N User role is granted specific rights
User Profile stay Room M:N + A client can stay in multiple rooms
(Client) + A room can have multiple stays (by
many clients)
4. Business Scenarios
7
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
Room Assignment and Stay Management
- When a Form is approved, the system automatically assigns the client to a Room
- The “Stay” relationship links User Profile (Client) with Room and Payment
*p/s: There will be a transaction when linking User Profile (Client) with Room. Thus,
Payment will occur. This means Clients will make many payments while staying in
the transaction between them and Room.*
Payment Handling
- When a Stay relationship is created, a Payment record is automatically generated
- Payment includes amount, date, and purpose
- The system handles transactions (staff do not manage payments)
- Rights ensure only Clients see and process payments
Feedback Collection
- Clients can submit feedback about their experience
- Rights restrict feedback visibility to staff/admins
8
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
QUESTION B.
Using the physical characteristics of the entities, such as relationships, datatypes, cardinality,
etc., from the case above, create a complete ER diagram.
1. Conceptual ER Diagram
9
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
Source source_id
Accommodation accommodation_id
Payment payment_id
User Profile is User Role 1:1 Each user profile has one role
(Client/Staff)
User Profile uses User Account 1:1 Each user profile is linked to one user
account.
User Profile has Next of Kin 1:0..N A client can have none or multiple next
(Client) of kin
User Profile has Source N:1 Many clients can have the same source
(Client)
User Profile submits Form 1:N A client can submit many forms
(Client)
User Profile processes Form 1:N Staff can process many forms
(Staff)
User Profile writes Feedback 1:0..N A client can write none or multiple
(Client) feedback
User Profile reviews Feedback 1:0..N Staff can review none or multiple
(Staff) feedback
User Role granted Rights 1:N User role is granted specific rights
User Profile assigned to Stay (updated 1:N A client can have multiple stays over
(Client) entity) time
Stay (updated Assigned to Room N:1 A stay belongs to one room, but a room
entity) can have multiple stays over time
10
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
2.3 Attributes
ENTITIES ATTRIBUTES
(NAME)
user_profile_id (FK)
12
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
2.4 Logical ER Diagram
3. Physical ER Diagram
13
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
3.1Note of Explanation
User Profile
User Role
14
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
Rights
User Account
Next of Kin
Source
16
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
Form
status (Pending, Approved, NOT NULL Every form must have a status
Rejected)
Payment
Room
17
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
type (Single, Shared, etc.) NOT NULL Every room must have a
defined type
Stay
Accommodation
Feedback
19
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
- BLOB (Binary Large Object):
+ Description: A BLOB is used to store binary data, such as images, audio, or video
files. It can handle large amounts of unstructured data.
+ Use Case: Perfect for storing files directly in the database, though often a file path is
stored as text instead.
- INT CHECK (Integer with Check Constraint):
+ Description: An INT is a data type for storing whole numbers (integers). The CHECK
constraint allows defining rules that the integer values must satisfy.
+ Use Case: Useful for enforcing ranges or specific conditions on integer values, like a
rating system where values must be between 1 and 5.
- INT (Integer):
+ Description: A standard data type for storing whole numbers (integers).
+ Use Case: Used for numeric IDs, quantities, ages, and other whole number values.
- VARCHAR (Variable Character):
+ Description: A VARCHAR is used to store variable-length strings of characters.
Specifying the maximum length of the string when defining the column.
+ Use Case: Suitable for names, usernames, addresses, and other text data where the
length varies.
- TEXT:
+ Description: A TEXT data type is used to store large amounts of text data. It's similar
to VARCHAR, but it can handle much longer strings.
+ Use Case: Ideal for storing descriptions, comments, notes, and other long text fields.
- DECIMAL:
+ Description: A DECIMAL is used to store precise numeric values with a fixed
number of decimal places. Specifying how many decimal places and how many digits
there are in total.
+ Use Case: Essential for storing currency values, financial data, and other numbers
where precision is critical.
- DATE:
+ Description: A DATE is used to store date values (year, month, day).
+ Use Case: Perfect for storing dates of birth, submission dates, payment dates, and
other date-related information.
QUESTION C.
Using the case above as proof of normalization to the third normal
20
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
-> This table contains repeating groups and multiple facts per entity
+ Next_of_Kin table:
Profile_ID is a foreign key in the Next_of_Kin table, linking it to the User_Profile table. This
helps maintain the relationship between the user and their next of kin
21
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
1 1 Hoang Vy 0912345678
2 2 Le Vinh 0987654321
+ Source table:
Source_ID is a primary key for the SOURCE table and is used in the User_Profile table to
link a user to their source
-> Every non-key attribute is now fully dependent on its table’s primary key (no partial dependency).
-> Still having transitive dependencies inside Source, like Source_Address depending on
Source_Name.
+ Next_of_Kin table:
1 1 Hoang Vy 0912345678
2 2 Le Vinh 0987654321
22
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
+ Source table:
QUESTION D.
To illustrate the implementations, create tables and other required database objects.
QUESTION E.
Demonstrate that you have added more than 10 pieces of data to each database table.
QUESTION F.
To show that your database is working, you need to declare and execute two SQL queries that
join two or more of your tables.
23
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
Part 2
(Data Manipulation)
QUESTION A.
To retrieve every entry and column from the Adventureworks database's employee table, use
a SQL query. The results are sorted by job title in descending order.
● Query:
24
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
● Output:
25
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
26
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
QUESTION B.
Using table aliasing in the Adventureworks database, create a SQL query to retrieve every
row and column from the Person table. The output is sorted by first name in ascending order.
● Query:
● Output:
27
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
28
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
QUESTION C.
Create a SQL query to get every record and a subset of the AdventureWorks database's
person table's fields (FirstName, Middle, Name LastName, and businessentityid). The header
of the fourth column is now called Employee_id. Sorted the result by first name in ascending
order.
29
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
● Query:
● Output:
30
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
QUESTION D.
Write a SQL query to retrieve just the product rows with a productline of 'T' and a
sellstartdate that is not NULL. Give back the product name, product number, and product
ID. The output was arranged by name in ascending order.
31
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
● Query:
● Output:
32
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
QUESTION E.
Create a SQL query that will get every row from the Adventureworks database's
salesorderheader table and determine the tax percentage on the outstanding balance. Return
the customer ID, sales order ID, order date, subtotal, and tax percentage column. The result
set was arranged on the subtotal in ascending order.
● Query:
33
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
● Output:
QUESTION F.
Create a SQL query to generate a list of unique job titles for the Adventureworks database's
employee table. The resultset was sorted in ascending order using the return jobtitle column.
34
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
● Query:
● Output:
35
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
QUESTION G.
Create a SQL query to determine how much freight each client has paid overall. Account
number, territory ID, return customer ID, and total freight. Using the customerid, sort the
output in ascending order.
● Query:
36
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
● Output:
QUESTION H.
Create a SQL query to determine each customer's average and subtotal sum. Return
customer ID, subtotal amount, and average. The results were grouped by salesperson and
customer IDs. The customerid column is used to sort the results.
37
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
● Query:
● Output:
38
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong
39
Module Code: COMP1845
Lecturer Name: Vu Thi Thuy Duong