0% found this document useful (0 votes)
44 views

DB Designs Lecture

The document outlines the structure and activities for the HI5033 Database Systems course, including software installation for PostgreSQL and DBeaver, and initial tasks for database modeling. It describes the requirements for a university database system and includes tasks for creating ER diagrams and SQL queries. The tutorials emphasize hands-on experience with database management tools and practical applications of database concepts.

Uploaded by

getawarenessnow
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

DB Designs Lecture

The document outlines the structure and activities for the HI5033 Database Systems course, including software installation for PostgreSQL and DBeaver, and initial tasks for database modeling. It describes the requirements for a university database system and includes tasks for creating ER diagrams and SQL queries. The tutorials emphasize hands-on experience with database management tools and practical applications of database concepts.

Uploaded by

getawarenessnow
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

HI5033 Database Systems - Tutorial 1

Welcome to the initial phase of our unit. It’s crucial to set the stage for what promises to be
an enlightening academic endeavor. While the first week serves as an introductory phase
without specific class tutorial activities, we lay the groundwork for hands-on sessions that
commence in Week 2.

Software Installation: A Key Step Forward

Our exploration into the realms of data management and analysis will be supported by
robust software tools, integral to the practical aspects of our unit. To ensure a seamless
transition into the hands-on sessions, it is essential that we prepare our digital workspaces in
advance.

1. PostgreSQL: The Backbone of Our Data Endeavors

Overview:
PostgreSQL stands tall as a leading Open Source Database Management System (DBMS),
renowned for its advanced capabilities and robustness. Its position as the 4th ranked DBMS
globally attests to its widespread adoption and trust within the open-source community.

Installation:
Available for installation at the Holmes Institutes computer laboratories, PostgreSQL offers
the flexibility of being accessible from your personal computers as well. For those who prefer
to set up a personal learning environment, the software can be freely downloaded from the
PostgreSQL Official Website (https://www.postgresql.org).

Benefits:
Engaging with PostgreSQL will not only enhance your understanding of database
management concepts but also equip you with hands-on experience in one of the industry’s
most respected DBMS.

2. DBeaver: Your Gateway to Database Interaction

Overview:
Complementing our use of PostgreSQL, DBeaver emerges as a versatile, multi-platform
database tool tailored for developers, database administrators, analysts, and anyone
engaged in database work. The Community Edition of DBeaver, which we will be utilizing,
offers a rich set of features conducive to effective database management and analysis.

Installation:
Echoing the accessibility of PostgreSQL, DBeaver can be installed in our computer
labs and is also available for personal download and installation. To integrate this tool into
your personal learning toolkit, visit DBeaver’s Official Website (https://dbeaver.io).

Benefits:
Through DBeaver, you will gain the ability to interact with databases in a more intuitive and
productive manner, enhancing your ability to analyse, manage, and derive insights from
data.

HI5033 Database Systems - Tutorial 2


In this session we are going to work on database modeling to consolidate what we have
covered in the lectures.

Consider the following University Database case study

Suppose that a database is needed to keep track of student enrollments in classes and
students’ final grades. After analyzing the miniworld rules and the users’ needs, the
requirements for this database were determined to be as follows (for brevity, we show the
chosen entity type names and attribute names for the conceptual schema in parentheses as
we describe the requirements; relationship type names are only shown in the ER
schema diagram):

● The university is organized into colleges (COLLEGE), and each college has a unique
name (Name), a main office (COffice) and phone (CPhone), and a particular faculty
member who is dean of the college. Each college administers a number of academic
departments (DEPT). Each department has a unique name (DName), a unique code
number (DCode), a main office (DOffice) and phone (DPhone), and a particular
faculty member who chairs the department. We keep track of the start date
(CStartDate) when that faculty member began chairing the department.

● A department offers a number of courses (COURSE), each of which has a unique


course name (CoName), a unique code number (CCode), a course level (Level: this
can be coded as 1 for freshman level, 2 for sophomore, 3 for junior, 4 for senior, 5 for
MS level, and 6 for PhD level), a course credit hours (Credits), and a course
description (CDesc). The database also keeps track of instructors (INSTRUCTOR);
and each instructor has a unique identifier (Id), name (IName), office (IOffice), phone
(IPhone), and rank (Rank); in addition, each instructor works for one primary
academic department.

● The database will keep student data (STUDENT) and stores each student’s name
(SName, composed of first name (FName), middle name (Mname), last name
(LName)), student id (Sid, unique for every student), address (Addr), phone (Phone),
major code (Major), and date of birth (DoB). A student is assigned to one primary
academic department. It is required to keep track of the student’s grades in each
section the student has completed.

● Courses are offered as sections (SECTION). Each section is related to a single


course and a single instructor and has a unique section identifier (SecId). A section
also has a section number (SecNo: this is coded as 1, 2, 3, . . . for multiple sections
offered during the same semester/year), semester (Sem), year (Year), classroom
(CRoom: this is coded as a combination of building code (Bldg) and room number
(RoomNo) within the building), and days/times (DaysTime: for example, ‘MWF
9am-9.50am’ or ‘TR 3.30pm-5.20pm’— restricted to only allowed days/time values).
(Note: The database will keep track of all the sections offered for the past several
years, in addition to the current offerings. The SecId is unique for all sections, not just
the sections for a particular semester.) The database keeps track of the students in
each section, and the grade is recorded when available (this is a many-to many
relationship between students and sections). A section must have at least five
students.

Task 1
Develop a crow's foot ERD for the above scenario (your tutor will give you some time to
work on this, then lead a discussion on the solution)

Task 2
In this task we want you all to create an account with Draw.io. This can be accessed online
for free or can even be downloaded on your local computer. This application lets you draw
a number of different types of computer-based diagrams, and Entity Relationship diagrams
are one of them. While VISIO on the lab computers can also be used to draw ER diagrams,
they do not provide the full set of ER symbols. Draw.io does.+

To start Draw.io, type into your browser the following URL:

www.draw.io or https://app.diagrams.net

Your tutor will lead you through the basics of creating an ER diagram in draw.io, but you
can also watch an instructive YouTube video in your own time at:

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

Once your tutor has led you through some of the basics of draw.io, build the ER diagram we
developed on paper in Task 1 with draw.io.

Task 3
Consider the ERD for the simple University system we developed in Task 1. In this example
we were using North American terminology, and the structure was quite simple, lets add
more complexity.

The term course will be redefined as per the Australian meaning, where a course represents
a collection of units (subjects), each course has a course code as before, but a course is
composed of a number of Units where each Unit has a unit code a name and a number of
credit points associated with it. A Unit could be common to a number of courses.
Rather than Sections as per Task 1, each semester a number of units are offered for
students to be able to enroll in, as part of their course. A full-time student will normally enroll
in a maximum of 4 units per semester, a part-time student will normally enroll in a maximum
of 2 units per semester. However, these numbers can vary (more or less) depending on
departmental approval. Additionally, a student could possibly take leave and not enroll in any
units in a particular semester.

● As well as the above information consider what attributes would need to be


stored in order to be able to print out a student transcript. This details all the
Units a student has undertaken for a particular course, and prints out (ordered
by semester), each unit, the letter grade obtained, and the total number of
points earned in the degree so far.

● Make the appropriate changes to the ER diagram developed in Task 1 to


account for these new expanded requirements.

Task 4
Consider a MAIL_ORDER database in which employees take orders for parts from
customers. The data requirements are summarized as follows:
● The mail order company has employees, each identified by a unique employee
number, first and last name, and Zip Code
● Each customer of the company is identified by a unique customer number, first and
● last name, and Zip Code
● Each part sold by the company is identified by a unique part number, a part name,
● price, and quantity in stock
● Each order placed by a customer is taken by an employee and is given a unique
order number. Each order contains specified quantities of one or more parts. Each
order has a date of receipt as well as an expected ship date. The actual ship date is
also recorded

No table of contents
entries found.

HI5033 Database Systems - Tutorial 3

In the last tutorial we had a session on Data Modeling.

In this session we have a bit to get through including arranging groups for the upcoming
group assessment. We will additionally recap lecture 3 which is on the relational Data Model,
as well as recapping what we did in week 2 tutorial, Data Modeling.
Your tutor will lead you through this and take questions on the previous week. Additionally,
we will set aside some time for the formation of the outstanding groups in the tutorial
session.

Once the house keeping has been settled, we will look at becoming familiar with
PostgreSQL and Dbeaver.

PostgreSQL
PostgreSQL has already been downloaded and installed on the lab computers for you. Once
installed, PostgreSQL runs as a database server in the background on the PC all the time,
unless you specifically stop the service. But when the computer is restarted, the PostgreSQL
service will start again. We can interact directly with the PostgreSQL Server by directly
running one of the interfacing programs that comes along with it, but we will not do that
today. Instead we will interface with the PostgreSQL server through a Database
Management tool, Dbeaver.

If you want to install PostgreSQL on your own computer, you can look at the following online
tutorial to do this https://www.postgresqltutorial.com/install-postgresql/

DBeaver
To Initiate DBeaver, look for the DBeaver icon on the desktop (or search for DBeaver in the
Windows application list). The DBeaver icon looks like the following

Double click the icon to load the DBeaver community edition (which is the free
edition). This can take some time to load while it initializes.

You should see the following screen when it loads


The above is the MAC version, but the Windows version will look similar. This is how the
opening screen looks without any displayed connections. We now need to connect to a
Database by creating a Database connection.

When PostgreSQL was installed it automatically created a sample database called postgres.
We will begin by connecting to that

Your tutor will lead you through a quick discussion on the Deaver Icon Toolbar

Initially to make a new connection, we need to click the first icon on the toolbar, this will ten
display the following

Click the PostgreSQL icon, then choose the 'next' button below when it becomes available.

The very first time this is done, you will need to download appropriate Java JDBC driver, you
will see the following pop-up box (you don't need to do this for subsequent PostgreSQL
connections)
Just let it default and click the 'download' button. The driver will then automatically download
and install, then you will see the connection screen that needs to be filled out every time a
new connection is made.
The name of the database we are connecting to is the default database created by
PostgreSQL, called 'Postgres', that is already filled out. You need to fill in the username and
password you used when installing PostgreSQL. The default user name is normally
'postgres' as shown above, but when it was installed in the labs it was changed to 'Student',
so you need to change the to the following

Username: 'Student'
Password: 'Student

You need to use this username and password combination for all database connections on
the lab computers. Once you change the username and password, before you click'Finish',
click 'Test Connection' to make sure all is OK first. Then click 'Finish'. The connection will
now have been created and will be available, even after you close the computer down and
start it up again. See below

You should now be connected to the 'postgres' database. You Tutor will lead you through
how to disconnect and re-connect to databases through DBeaver.

Additionally, your tutor will lead you through the process of Creating a new Database through
DBeaver.

Task 1
Create a new Database called 'Northwind', then create a new DBeaver connection to enable
a connection to the Northwind database.

Your Tutor will now discuss how we are going to create the tables and populate the
Northwind using two downloadable files from the HI5033 Database blackboard Shell. These
are located in the 'Tutorial Materials' section, by clicking the Tutorial Materials link on the left
of the Blackboard class shell. In the Item 'Tutorial 3 Sheet and Files', you will see two
downloadable files
northwind_ddl.sql.txt (the DDL statements to create the tables)
northwind_data.sql.txt (the DDL/DML statements to add records)

Download these two files and briefly examine them in Windows using a text viewer, these
are just text files containing SQL statements. We will discuss SQL in detail beginning next
week.

Your tutor will show you how to load and execute these files in DBeaver

Task 2
➔ Create the Northwind Database tables.
➔ Load in all the sample data into the Northwind database tables
➔ Explore the tables and structure of the Northwind database
➔ Examine the sample data which was input via the SQL scripts
➔ Explore the inbuilt ER diagram facility of DBeaver.

Your Tutor will lead you through the process of backing up the Database to a USB drive

Task 4
Using the DBeaver Data View of the Customers Table, add a new customer to the database.
Use your own Student ID as the Customer_id and use your own as the company name.
Save any changes.

Task 5
Make a backup of your version of the database onto a USB drive.

Don't forget to bring your USB Drive to the class next week.

HI5033 Database Systems - Tutorial 4


In the last tutorial we looked at some preliminary aspects of PostgreSQL and DBeaver.

We learned how to create a Database connection in DBeaver to a PostgreSQL database,


how to create a new PostgresSQL database, and make a connection to it. Additionally, we
utilized two SQL scripts

northwind_ddl.sql
northwind_data.sql

available on the HI5033 Database Systems Blackboard shell to create the tables of the
Northwind database, and to insert sample data in the tables.

We also learned how to backup and restore the Database to a USB drive or to the Desktop.

Pre-Task requirements
Make sure you have a copy of the Northwind database created on your lab computer. If you
did not attend the previous tutorial, you will need to go through Tutorial Sheet 3 before you
start this one.

Using the backup you took of the Northwind database last week, restore the Northwind
database to the state it was after completing Task 4 in the Week 3 Tutorial sheet from last
week.

This should include the new customer record you added with the customer_id value of your
Student ID.

Task 1
Create a query to list the entire employees table

Task 2
Create an SQL select statement to retrieve the customer_id, company_name and
contact_name of all customers based in London

Task 3
Perform the same query as in Task 2, but in the query result table, name the
customer_id field to ID, company_name field to company and contact_name to
contact

Task 4
Create a query to to retrieve the order_id, company_name and order_date of all
orders placed before 11 July 1996

Task 5
create a query to list the product_name and quantity_per_unit of all products from the
product table.

Modify this query to select only those products that are not discontinued, and in the
result table, label the product_name column Product, and the quantity_per_unit
column PerUnit

modify the query to include the price and sort the output so we can easily find the
most expensive product and the least expensive product

Task 6
Write a query to count all products
Modify this to count only the discontinued products

Task 7
Write a query to get a Product list (name, units on order , units in stock) of all current
stock which has less available units than the quantity on order
Task 8
Find the Total Number of Units Ordered of Product ID 3

Task 9
construct a query to list the order id, order date, company name placing the order, the
product name and the quantity ordered of all items for the orders from customer with
ID CONSH

Task 10
construct an SQL statement to update employee Robert Kings title to Dr
construct an SQL statement to insert a new employee record for Frodo Baggins

Task 11
construct SQL statements to update the price of:
Perth Pasties to 45 dollars
Vegie-spread to 50 dollars
Filo Mix to 8 dollars and 50 cents

Construct an SQL statement to insert an order for the customer you added to the
customers table last week (with your student ID number), with the order date of 1
April 2021, the required date of 5 May 2021, a shipped date of 16 April 2021, shipped
via DHL to the same shipping address as in your specific customer record and this
will be created by employee Robert King.

This order should order the following 3 items with the specified quantities
Perth Pasties, 20 units
Vegie-spread,10 units
Filo Mix, 45 units

there is no discount for any of the products

Exit Task
Make a backup copy of your data in the database. You will need this for next week. It
will be used in grading your tutorial work.

HI5033 Database Systems - Tutorial 4 (SAMPLE SOLUTIONS)

Task 1
select *
from employees

Task 2
select customer_id, company_name, contact_name
from customers
where city ='London'

Task 3
select customer_id as ID, company_name as Company, contact_name as Contact
from customers
where city ='London'

Task 4
select order_id, company_name, order_date
from orders o, customers c
where o.customer_id =c.customer_id
and order_date < '1996-07-11'

Task 5
select product_name, quantity_per_unit
from products;

select product_name Product, quantity_per_unit PerUnit, unit_price Price


from products
where discontinued = 0
order by unit_price;

--Assuming products that are not discontinued are indicated by a value of 0


for the discontinued attribute

select product_name Product, quantity_per_unit PerUnit, unit_price Price


from products
where discontinued = 0
order by unit_price

Task 6
select count(*)
from products ;

select count(*)
from products
where discontinued =1;

Task 7
select
product_name,
units_on_order,
units_in_stock
from products
where discontinued = 0
and units_in_stock < units_on_order;

Task 8
select SUM(units_on_order) as total_units_ordered
from products
where product_id = 3;

Task 9
select
o.order_id,
o.order_date,
c.company_name,
p.product_name,
od.quantity
from orders o, customers c, order_details od, products p
where o.customer_id = c.customer_id
and o.order_id = od.order_id
and od.product_id = p.product_id
and c.customer_id = 'CONSH'

HI5033 Database Systems - Tutorial 5

In this tutorial, our focus will be on working with SQL statements to perform data selection
and updates within the Northwind database.

Task 1
Construct an SQL statement to display a list of city names along with the number of
customers in each city, sorted in descending order based on the number of customers.

Task 2
Construct an SQL statement to find the products that have the highest price within their
categories. Display the product name, category name, and unit price.

Task 3
Construct an SQL statement to find the employee who manages the highest number of
territories. Display the employee ID, first name, last name, and the number of territories
he/she manages.

Task 4
Construct an SQL statement to update employee Robert Kings title to Dr
Construct SQL statements to update the price
of: Perth Pasties to 45 dollars
Vegie-spread to 50 dollars
Filo Mix to 8 dollars and 50 cents
Construct an SQL statement to insert a new employee record for Frodo Baggins

Note: For Lab 1 Exercise, you are required to submit a file in Microsoft Word format, which
contains a cover page and solutions (SQL statements) for 4 tasks in Tutorial 5.

HI5033 Database Systems - Tutorial 6


In this tutorial we will get some further experience in developing ER diagrams, and then
translate these to SQL DDL Statements.

Consider the following brief scenario

EliteVideo is a startup company providing concierge DVD kiosk service in upscale


neighbourhoods. EliteVideo can own several copies (VIDEO) of each movie (MOVIE). For
example, a kiosk may have 10 copies of the movie Twist in the Wind. In the database, Twist
in the Wind would be one MOVIE, and each copy would be a VIDEO. A rental transaction
(RENTAL) involves one or more videos being rented to a member (MEMBERSHIP). A video
can be rented many times over its lifetime; therefore, there is an M:N relationship between
RENTAL and VIDEO. (DETAILRENTAL) is the bridge table to resolve this relationship. The
pricing structure is such that there are a number of price categories, such as 'Overnight', '2
Day Rental', '3 Day Rental', 'Weekly' etc. The cost of renting each MOVIE will fall into one of
these (PRICE) categories.

Task 1
List the attributes you would expect to find in each of the tables mentioned above

Task 2
Create an ER Diagram for the above Database tables

Task 3
Review the PostgreSQL Data types from the PostgreSQL Data Tupe documentation.
This can be found online at
https://www.postgresql.org/docs/9.5/datatype.html
Determine a datatype for each of the attribute fields identified in Task 1

Task 4
Review the PostgreSQL documentation on Table Basics, Default Values and Constraints.
This can be located online at

https://www.postgresql.org/docs/9.5/ddl.html

Once you do this write the SQL DDL Statements to create the above tables we identified in
Task 1 and refined in Task 2 above. Making sure to identify all the primary and foreign keys.

Task 5
In PostgreSQL and DBeaver, create the new database (call it MovieDB, prefixed by your
Student ID number), as we did in the first tutorial, and create a new DBeaver connection to
this new database.

Create the new tables in the database by running the SQL DDL statements you created in
Task 4 as scripts in the new database. You will need to fix any errors in the SQL statements
as they appear.

HI5033 Database Systems - Tutorial 7

In this Tutorial you will continue working on the EliteVideo database from last week.

You need to finish all the tasks from the previous week including Task 5. If you have not
done these, you need to work on these during the tutorial.

Store all the SQL DDL statements in a text file similar to the one for the Northwind database
that you downloaded in Tutorial 3 (Northwind_ddl.sql). Name this file EliteVideo_ddl.sql and
make sure that its content can be executed in DBeaver using the command Execute SQL
script.

Note: For Lab 2 Exercise, you are required to submit a file in Microsoft Word format, which
contains a cover page, ERD Diagram and DDL statements for EliteVideo database.

HI5033 Database Systems - Tutorial 8

In this Tutorial you will continue working on the EliteVideo database introduced in Week 6.
You may need to consult the tutorial materials from the last two weeks.

To complete the task for this week, you must ensure that the EliteVideo database has been
created on your computer.

Task 1
Generate sample data for the EliteVideo database, each table contains a minimum of 5 rows
of data.

● Make sure that one row in the ‘memberShip’ table has your student ID as the primary
key and your full name as the name.
● Make sure that there is a movie titled ‘Attack of the Killer Tomatoes’ and another one
named ‘Goosebumps’.
● Make sure that there is a rental transaction for the member with your student ID on
15/08/2023 for ‘Attack of the Killer Tomatoes’, ‘Goosebumps’ and one other movie.
● Make sure that the pricing structure includes a variety of price categories (such as
Overnight,’ ‘2-Day Rental,’ ‘3-Day Rental,’ ‘Weekly,’ etc.) and the cost of renting each
movie will fall into one of these categories.
Store all the SQL DML statements in a text file similar to the one for the Northwind Database
that you downloaded in tutorial 3 (Northwind_data.sql). Name this file EliteVideo_data.sql
and make sure that its content can be executed in DBeaver using the command Execute
SQL script.

HI5033 Database Systems - Tutorial 9

In this Tutorial you will continue working on the EliteVideo database from last week. If you
have not finished Task 1 in Tutorial 8, you need to work on it during the tutorial.

Make sure that you have already created the EliteVideo database and entered sample data
on your computer before starting the tasks for this week.

Task 1
Construct a SQL statement that retrieves the movie titles along with the count of rentals for
each movie.

Task 2
Construct a SQL statement to display name, email, phone number and address of the
members who have rented movies released after 01/01/2023.

Task 3
Construct a nested SQL statement using the EXISTS function to display members
(memberID, name, address) who have rented at least one movie.

Task 4
Construct a nested SQL statement using the EXISTS function to display members
(memberID, name, address) who have rented movies with a rating higher than 4 (Assuming
that the scale of rating is from 1 to 5).

Note: For Lab 3 Exercise, you are required to submit a file in Microsoft Word format, which
contains a cover page and solutions (SQL statements) for 4 tasks in Tutorial 9.

HI5033 Database Systems - Tutorial 10

In this Tutorial you will continue working on the EliteVideo database.

Task 1
● Create a new database connection for the user Student in DBeaver.
● Create a new schema named readonlyEV for EliteVideo database.
Task 2
Create a new role for the EliteVideo database and give this role the same name as your
Student ID.

Task 3
Use your own first name to create a new user for EliteVideo database. Assign the new user
the role created in Task 2.

Task 4
Assign the role the privileges needed to access the new schema in Task 1.

Task 5
Create a view that can see all data within the Movie table in EliteVideo database.

Task 6
Create a new database connection for the new user in DBeaver.
Within the connection, construct a SELECT statement to view all data in the view created in
Task 5.

Store all the SQL statements in a file named EliteVideo_DCL.sql.

Use the online documentation as needed located at:


https://www.postgresql.org/docs/13/index.html

HI5033 Database Systems - Tutorial 11

Task 1
In this task, you first create a new database connection for user ‘Student’ in DBeaver, and
then write SQL statements for the following sub-tasks:

● Create a new database named ‘Database11’.


● Create three new users: ‘user1’, ‘user2’, ‘user3’ with the same password
‘helloHolmes’.
● In database ‘Database11’, create a schema named ‘ABC_schema’ and assign ‘user1’
as the owner of this schema.

Task 2
In this task, you first create a new database connection for user ‘user1’ in DBeaver, and then
write SQL statements for the following sub-tasks in schema ‘ABC_schema’:
● Create a table ‘car’.
● Enter data into the table ‘car’.
● Create a view named 'view1' containing the first three columns of table ‘car’.
● Grant the permission to access the schema ‘ABC_schema’ to two user ‘user2’.
● Grant the permission to access the schema ‘ABC_schema’ to two user ‘user3’.
● Grant SELECT permission on table ‘car’ to user ‘user2’.
● Grant SELECT permission on view ‘view1’ to user ‘user3’.

Task 3
In this task, you first create a new database connection for user ‘user2’ in DBeaver, and then
write a SQL statement to show all data of table ‘car’ (in schema ‘ABC_schema’).

Task 4
In this task, you first create a new database connection for user ‘user3’ in DBeaver, and then
write a SQL statement show all data of view ‘view1’ (in schema ‘ABC_schema’).

Note: For Lab 4 Exercise, you are required to submit a file in Microsoft Word format, which
contains a cover page and solutions (SQL statement) for 4 tasks in Tutorial 11.

You might also like