0% found this document useful (0 votes)
92 views30 pages

Database Design for Health and Fitness Tracking Applications

The document outlines a course work on Database Design for Health and Fitness Tracking Applications, focusing on creating a centralized system for managing user data related to fitness goals and activities. It includes detailed sections on the health and fitness database structure, table creation, and SQL queries for data retrieval. Key components discussed are the ER diagram, various tables for members, memberships, workouts, trainers, payments, and relationships between these entities.

Uploaded by

burzuyevrcb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views30 pages

Database Design for Health and Fitness Tracking Applications

The document outlines a course work on Database Design for Health and Fitness Tracking Applications, focusing on creating a centralized system for managing user data related to fitness goals and activities. It includes detailed sections on the health and fitness database structure, table creation, and SQL queries for data retrieval. Key components discussed are the ER diagram, various tables for members, memberships, workouts, trainers, payments, and relationships between these entities.

Uploaded by

burzuyevrcb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Azerbaijan State Oil and Industry University

Faculty: Information Technology and Management


Department: Computer Engineering
Group: 692.23E
Specialty: Information Security
Subject: Basics of Database
Topic: Database Design for Health and Fitness Tracking Applications

Course Work

Student: Najafzade Vusal


Instructor: Ibrahimova Ellada
Department Director: Rahimova N.A

BAKI - 2024
1
Contents
Chapter 1. Health and Fitness Database
1.1 Introduction…………………………………………………………………3

1.2 ER diagram…………………………………………………………………4

Chapter 2. Creating tables in the health and fitness database


2.1 List of the tables……………………………………………………………5

2.2 Alter, drop columns……………………………………………….............16

2.3 SQL constraints...................................................................................17

Chapter 3. SQL queries. Using SQL to Retrieve Information from tables


3.1 SQL select, order by, where command, update, delete statements.....18

3.2 The in, between, like, and, or, top operators........................................20

3.3 Aggregate functions.............................................................................22

3.4 Joining tables.......................................................................................24

3.5 SQL Subqueries...................................................................................26

3.6 SQL Views............................................................................................28

Conclusion...........................................................................................................29

2
1. Health and Fitness Database

1.1 Introduction

The existing approach to health and fitness tracking relies heavily on manual
methods, such as handwritten logs or scattered digital tools, which can be time-
consuming and prone to errors. As the number of users tracking their fitness goals
increases, the complexity of managing this data grows significantly. Recording activities,
analyzing progress, and integrating health metrics from various sources into a cohesive
system become challenging without a centralized solution. This lack of efficiency can
lead to missed insights and hinder individuals from achieving their health objectives
effectively.

The purpose of the Database Design for Health and Fitness Tracking Applications is
to create a robust and secure system for storing and managing user data, including
personal fitness goals, workout logs, dietary information, progress reports, and wearable
device integrations. By centralizing and automating these processes, the system ensures
accuracy, efficiency, and a seamless experience for users striving for better health
outcomes.

1.2 ER diagram

The ER (Entity Relationship) diagram represents the model of the Health and Fitness
Tracking Application. This diagram illustrates the visual structure of the database tables
and their relationships, including members, trainers, branches, workout plans, and
payment systems. It serves as a tool for organizing structured data and defining
relationships between various functional components of the application. Key features of
the Health and Fitness Tracking Application Database Design include managing
members' profiles, workout plans, equipment, trainers, branches, payments, and
membership details.

3
4
2. Creating tables in the health and fitness database

Tables form the foundation of any database, serving as containers for structured data.
In the context of a health and fitness database, creating tables involves defining table
names, specifying columns, and determining appropriate data types for each column.
The SQL CREATE TABLE statement facilitates the creation of these tables, while the
INSERT INTO statement allows the addition of data rows, enabling the storage and
management of key information related to health and fitness activities.

2.1 List of the tables

1) Members

CREATE TABLE members(


member_id INT IDENTITY (1,1) PRIMARY KEY,
member_name VARCHAR (30) NOT NULL,
member_surname VARCHAR (30) NOT NULL,
member_sex VARCHAR (10) NOT NULL,
member_birth DATE,
member_email VARCHAR(100) UNIQUE,
body_type VARCHAR(30) NOT NULL,
member_weight DECIMAL NOT NULL,
member_height DECIMAL NOT NULL);

 member_id – This is a unique ID of a member


 member_name – This is the name of a member
 member_surname – This is the surname of a member
 member_sex – This is the sex of a member
 member_birth – This is the date of birth of a member
 member_email – This is the email of a member. It should be unique for each
member
 body_type – This is the body type of a member
 member_weight – This is the weight of a member
 member_height – This is the height of a member

INSERT INTO members VALUES


('Vusal', 'Najafzade', 'Man', '07.06.2006',
'[email protected]', 'Mesomorph', 90, 181),
('Tural', 'Eliyev', 'Man', '2005.03.02',
'[email protected]', 'Ectomorph', 70, 170),

5
('Leman', 'Quliyeva', 'Woman', '1998.08.09',
'[email protected]', 'Ectomorph', 60, 175),
('Lale', 'Memmedova', 'Woman', '2001.02.03',
'lalememmedova2', 'Endomorph', 78, 168),
('Eli', 'Necefov', 'Man', '2002.01.04',
'[email protected]', 'Ectomorph', 70, 180),
('Aysel', 'Aliyeva', 'Woman', '1990.07.12',
'[email protected]', 'Mesomorph', 55, 162),
('Orkhan', 'Mammadov', 'Man', '1988.11.20',
'[email protected]', 'Endomorph', 85, 172),
('Leyla', 'Huseynova', 'Woman', '1995.03.14',
'[email protected]', 'Ectomorph', 60, 165),
('Farid', 'Guliyev', 'Man', '1992.05.25',
'[email protected]', 'Mesomorph', 88, 178),
('Nigar', 'Aliyeva', 'Woman', '1999.01.18',
'[email protected]', 'Endomorph', 75, 170);
select * from members

2) Membership

CREATE TABLE membership(


membership_id INT IDENTITY (1,1) PRIMARY KEY,
membership_name VARCHAR(70) UNIQUE,
date_of_start DATE,
date_of_finish DATE,
membership_price INT NOT NULL,
member_id INT,
FOREIGN KEY (member_id) REFERENCES members (member_id) ON DELETE
CASCADE ON UPDATE CASCADE
);

 membership_id – This is the unique ID of a membership

6
 membership_name – This is the name of the membership
 date_of_start – This is the starting date of a membership
 date_of_finish – This is the last date of a membership
 membership_price – This is the price of a membership
 member_id – This is the ID of a member who bought a membership, it references
to the ID column of the members table.

select * from membership

3) Workout Plan

CREATE TABLE workoutplan(


workoutplan_id INT IDENTITY (1,1) PRIMARY KEY,
plan_name VARCHAR (40) NOT NULL,
workoutplan_frequency VARCHAR(40) NOT NULL,
member_id INT,
FOREIGN KEY (member_id) REFERENCES members (member_id) ON DELETE
CASCADE ON UPDATE CASCADE
);

 workoutplan_id – This is the unique ID of a workoutplan


 plan_name – This is the name of the workout plan
 workout_plan – This is the frequency of doing a workout plan
 member_id – This is the ID of a member who bought the workout plan, it references
to the ID column of the members table.

select * from workoutplan

7
4) Trainers

CREATE TABLE trainers(


trainer_id INT IDENTITY (1,1) PRIMARY KEY,
trainer_name VARCHAR (30) NOT NULL,
trainer_surname VARCHAR (30) NOT NULL,
trainer_sex VARCHAR (10) NOT NULL,
date_of_birth DATE,
trainer_email VARCHAR (100),
trainer_experience VARCHAR (20) NOT NULL,
free_times VARCHAR (70) NOT NULL,
work_times VARCHAR (70) NOT NULL
);

 trainer_id – This the unique ID of the trainer.


 trainer_name – This is the name of a trainer
 trainer_surname – This is the surname of a trainer
 trainer_sex – This is the gender of a trainer
 date_of_birth – This is the birth date of a trainer
 trainer_email – This is the email of a trainer
 trainer_experience – This is the experience of a trainer
 free_times – This is the free times of a trainer
 work_times – This is the work times of a trainer

select * from trainers

8
5) trainer_member – This is the table for creating many-to-many relationship between
trainers and members tables.

CREATE TABLE trainer_member(


trainer_id INT,
member_id INT,
FOREIGN KEY (trainer_id) REFERENCES trainers (trainer_id) ON
DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (member_id) REFERENCES members (member_id) ON DELETE
CASCADE ON UPDATE CASCADE
);

 trainer_id – This is the ID of a trainer which references to the ID column of the


trainers table.
 member_id – This is the ID of a member which references to the ID column of the
members table.

select * from trainer_member

9
6) Payments

CREATE TABLE payments(


payment_id INT IDENTITY (1,1) PRIMARY KEY,
payment_method varchar(40),
payment_time DATE,
amount_of_payment INT NOT NULL,
member_id INT,
FOREIGN KEY (member_id) REFERENCES members (member_id) ON DELETE
CASCADE ON UPDATE CASCADE,
);

 payment_id – This is the unique ID of a payment


 payment_method – This is the method of a payment
 payment_time – This the time of a payment
 amount_of_payment – This is the amount of a payment
 member_id – This is the ID of a member who did the payment, it references to the
ID column of the members table.

select * from payments

7) Payment details

CREATE TABLE paymentdetails(


paymentdetails_id INT IDENTITY (1,1) PRIMARY KEY,
card_details varchar(20),
cash VARCHAR (30),
payment_id INT,

10
FOREIGN KEY (payment_id) REFERENCES payments (payment_id) ON
DELETE CASCADE ON UPDATE CASCADE
);

 paymentdetails_id – This is the unique ID of a payment detail


 card_details – This is the details of the card
 cash – This is the amount of payment with cash
 payment_id – This is the ID of a payment, it references to the ID column of the
payments table

select * from paymentdetails

8) Equipments

CREATE TABLE equipments(


equipment_id INT IDENTITY (1,1) PRIMARY KEY,
equipment_name VARCHAR (30) NOT NULL,
date_of_buying DATE,
quantity INT NOT NULL,
);

 equipment_id – This is the unique ID of an equipment


 equipment_name – This is the name of an equipment
 date_of_buying – This is the the date that we bought an equipment
 quantity – This is the quantity of an equipment
select * from equipments
11
9) Branches

CREATE TABLE branches(


branch_id INT IDENTITY (1,1) PRIMARY KEY,
branch_country VARCHAR (50) NOT NULL,
branch_region VARCHAR (50) NOT NULL,
branch_street VARCHAR (60) NOT NULL,
open_date DATE,
branch_rating DECIMAL(10,2),
);

 branch_id – This is the unique ID of a branch


 branch_country – This is the country that branch is located
 branch_region – This is the region that branch is located
 branch_street – This is the street that branch is located
 open_date – This is the date that the branch was opened
 branch_rating – This is the rating of the branch

select * from branches

12
10) branch_equipments – This is the table for creating many-to-many relationship
between branches and equipments tables.

CREATE TABLE branch_equipments(


branch_id INT,
equipment_id INT,
FOREIGN KEY (branch_id) REFERENCES branches (branch_id) ON DELETE
CASCADE ON UPDATE CASCADE,
FOREIGN KEY (equipment_id) REFERENCES equipments (equipment_id)
ON DELETE CASCADE ON UPDATE CASCADE
);

 branch_id – This is the ID of a branch which references to the ID column of the


branches table.
 equipment_id – This is the ID of an equipment which references to the ID column of
the equipments table.

select * from branch_equipments

13
11) branch_trainers – This is the table for creating many-to-many relationship between
branches and trainers tables.

CREATE TABLE branch_trainers(


branch_id INT,
trainer_id INT,
FOREIGN KEY (branch_id) REFERENCES branches (branch_id) ON DELETE
CASCADE ON UPDATE CASCADE,
FOREIGN KEY (trainer_id) REFERENCES trainers (trainer_id) ON
DELETE CASCADE ON UPDATE CASCADE
);

 branch_id – This is the ID of a branch which references to the ID column of the


branches table.
 trainer_id – This is the ID of a trainer which references to the ID column of the
trainers table.

select * from branch_trainers

12) Workers

CREATE TABLE workers(


worker_id INT IDENTITY (1,1) PRIMARY KEY,
worker_name VARCHAR (30) NOT NULL,
worker_surname VARCHAR (30) NOT NULL,

14
worker_sex VARCHAR (10) NOT NULL,
worker_position VARCHAR (50) NOT NULL,
worker_birth DATE,
worker_email VARCHAR (100) UNIQUE,
worker_salary DECIMAL NOT NULL,
branch_id INT,
FOREIGN KEY (branch_id) REFERENCES branches (branch_id) ON DELETE
CASCADE ON UPDATE CASCADE
);

 worker_id – This is the unique ID of a worker


 worker_name – This is the name of a worker
 worker_surname – This is the surname of a worker
 worker_sex – This is the gender of a worker
 worker_position – This is the position of a worker
 worker_birth – This is the birth date of a worker
 worker_email – This is the email of a worker
 worker_salary – This is the salary of a worker
 branch_id – This is the ID of a branch which a worker works, it references to the ID
column of the branches table

select * from workers

13) Gym department

CREATE TABLE gym_department(


department_id INT IDENTITY (1,1) PRIMARY KEY,
department_name VARCHAR (50) NOT NULL,
department_address VARCHAR (50) NOT NULL,
department_contact_number varchar(40) NOT NULL,
department_open_date DATE,
department_rating VARCHAR (15),

15
department_email VARCHAR (100) UNIQUE NOT NULL,
branch_id INT,
FOREIGN KEY (branch_id) REFERENCES branches (branch_id) ON DELETE
CASCADE ON UPDATE CASCADE
);

 department_id – This is the unique ID of the department


 department_name – This is the name of the department
 department_address – This is the address of the department
 department_contact_number – This is the contact number of the department
 department_open_date – This is the open date of the department
 department_rating – This is the rating of the department
 department_email – This is the email of the department
 branch_id – This is the ID of a branch which is associated to the department, it
references to the ID column of the branches table

select * from gym_department

2.2 Alter, drop columns

Alter Table members


ADD phone_number varchar(40);
ADD is used to add columns into an existing table. Sometimes we may need to add
additional information to our tables, in that case we do not need to create the whole
database again, ADD comes to our rescue.

Alter Table members


DROP Column phone_number;

DROP COLUMN is used to drop column in a table. Deleting the unwanted columns
from the table.

16
2.3 SQL constraints

SQL constraints are used to specify rules for the data in a table. Constraints are
used to limit the type of data that can go into a table.

There is a PRIMARY KEY in the ID column of the members, which means We can
not add same data to this column.

There are NOT NULL constraints in the member_name, member_surname,


member_sex, body_type, member_height and member_weight columns. This means that
we can not add NULL values to these columns.

There is UNIQUE constrant in the member_email column. This means that all the
values in the column are different.

With the following statement we add NOT NULL constraint to the member_birth
column of the members table.

ALTER TABLE members


ALTER COLUMN member_birth DATE NOT NULL;

17
3. SQL queries. Using SQL to Retrieve Information from tables

3.1 SQL select, order by, where command, update, delete statements

1. This query lists the branches that their ratings are below 5. Here we used ASC. The
ASC command is used to sort the data returned in ascending order.

select branch_country, branch_rating


from branches
where branch_rating < 4.5
order by branch_rating ASC;

2. This query lists the workers whose position is cleaner, depending on their birth date.

select CONCAT(worker_name,' ', worker_surname) AS 'worker',


worker_birth
from workers
where worker_position='cleaner'
order by worker_birth ASC;

18
3. The UPDATE statement is used to modify the existing records in a table. Here, we’ll
update the weight of a member.

update members
set member_weight = 95 where member_name = 'Vusal';

4. The DELETE statement is used to delete existing records in a table. Here, we’ll delete
member Eli from our table.

19
delete from members where member_id = 5

3.2 The in, between, like, and, or, top operators

1. The IN operator allows you to specify multiple values in a WHERE clause. The IN
operator is a shorthand for multiple OR conditions. The following query is to find all
members whose body types are either "Ectomorph" or "Endomorph".

SELECT
member_name,
member_surname,
body_type
FROM
members
WHERE
body_type IN ('Ectomorph', 'Endomorph');

2. The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates. The following query is to retrieve memberships with prices
between 50 and 200.
20
SELECT
membership_name,
membership_price
FROM
membership
WHERE
membership_price BETWEEN 50 AND 200;

3. The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column. The following query is to find members whose surname starts with the letter "S".

SELECT
member_name,
member_surname
FROM
members
WHERE
member_surname LIKE 'E%';

4. The following query is to retrieve members who weigh more than 70 kg and are either
"Male" or "Female" with a height greater than 170 cm.

SELECT
member_name,
member_surname,
member_weight,
member_height,
member_sex
FROM
21
members
WHERE
member_weight > 70
AND member_height > 170
AND (member_sex = 'Man' OR member_sex = 'Woman');

5. The following query is to retrieve the top 5 highest-paid workers.

SELECT TOP 5
worker_name,
worker_surname,
worker_salary
FROM
workers
ORDER BY
worker_salary DESC;

3.3 Aggregate functions

1. This query calculates the total sum of all payments made by all members. Here, we’ll
use SUM() aggregate function.

SELECT
SUM(amount_of_payment) AS total_payments
FROM
payments;

22
2. This query finds the count of members for each membership type. Here, we’ll use
COUNT() aggregate function.

SELECT
ms.membership_name,
COUNT(ms.member_id) AS member_count
FROM
membership ms
GROUP BY
ms.membership_name;

3. This query retrieves the minimum and maximum prices of memberships. Here, we’ll
use MIN() and MAX() aggregate functions.

SELECT
MIN(membership_price) AS minimum_membership_price,
MAX(membership_price) AS maximum_membership_price
FROM
membership;

23
4. This query calculates the average weight of members who are male. Here, we’ll use
AVG() aggregate function.

SELECT
AVG(member_weight) AS average_male_weight
FROM
members
WHERE
member_sex = 'Man';

3.3 Joining tables

1. The INNER JOIN keyword selects records that have matching values in both tables.
The following query is to fetch member names along with their trainer names using an
inner join.

SELECT member_name, member_surname


trainer_name, trainer_surname
FROM members AS m
INNER JOIN trainer_member AS tm ON m.member_id = tm.member_id
INNER JOIN trainers AS t ON tm.trainer_id = t.trainer_id;

24
2. The RIGHT JOIN keyword returns all records from the right table, and the matching
records from the left table. The following query is to list all branches and the equipment
assigned to each branch. This ensures that all branches are included, even if no
equipment has been assigned to them.

SELECT branch_country, branch_region, branch_street,


equipment_name
FROM branch_equipments AS be
RIGHT JOIN branches AS b ON be.branch_id = b.branch_id
RIGHT JOIN equipments AS e ON be.equipment_id = e.equipment_id
ORDER BY b.branch_country, b.branch_region;

3. The LEFT JOIN keyword returns all records from the left table , and the matching
records from the right table . The result is 0 records from the right side, if there is no
match. The following query is to list all trainers and their assigned branch details.

SELECT trainer_name, trainer_surname, trainer_email,


branch_country, branch_region
FROM trainers AS t
LEFT JOIN branch_trainers AS bt ON t.trainer_id = bt.trainer_id
LEFT JOIN branches AS b ON bt.branch_id = b.branch_id
ORDER BY t.trainer_name;

25
4. The following query is to list each membership program's name and the total payments
made for that program.

SELECT membership_name AS MembershipProgram,


SUM(amount_of_payment) AS TotalPayments
FROM membership AS m
JOIN payments AS p ON m.membership_id = p.member_id
GROUP BY m.membership_id, m.membership_name
ORDER BY TotalPayments DESC;

3.3 SQL subqueries

1. The following query is to find the Membership Program with the Maximum Price.

SELECT membership_name, membership_price


FROM membership
WHERE membership_price = (
SELECT MAX(membership_price)
FROM membership);
26
2. The following query is to find trainers who are associated with more than one member.

SELECT trainer_name, trainer_surname


FROM trainers
WHERE trainer_id IN (
SELECT trainer_id
FROM trainer_member
GROUP BY trainer_id
HAVING COUNT(member_id) > 1
);

3. The following query is to list branch details where at least one worker earns above the
average worker salary.

SELECT branch_country, branch_region, branch_street


FROM branches
WHERE branch_id IN (
SELECT branch_id
FROM workers
WHERE worker_salary > (
SELECT AVG(worker_salary) FROM workers
)
);

27
3.4 SQL views

1. A view is a virtual table based on the result-set of an SQL statement. The following
query is to combine the members and membership tables to display each member’s
name, their membership program, and the price.

CREATE VIEW MembershipDetails AS


SELECT
m.member_id,
m.member_name,
m.member_surname,
ms.membership_name,
ms.membership_price
FROM
members AS m
JOIN
membership AS ms
ON
m.member_id = ms.member_id;

SELECT * FROM MembershipDetails;

2. This view joins the payments and members tables to show payment details for each
member, including their name, payment amount, and payment method.

CREATE VIEW PaymentsSummary AS


SELECT
p.payment_id,
m.member_name,
28
m.member_surname,
p.amount_of_payment,
p.payment_method,
p.payment_time
FROM
payments AS p
JOIN
members AS m
ON
p.member_id = m.member_id;

SELECT * FROM PaymentsSummary;

Conclusion

In today’s data-driven world, efficient database systems are essential for managing
and organizing information. The Health and Fitness Tracking Application database
simplifies the process of storing and retrieving member data, membership details, trainer
schedules, payments, and equipment inventory. Traditionally, fitness centers relied on
manual processes to manage their operations, which were time-consuming and prone to
errors.

With the implementation of this database system, tasks such as assigning trainers to
members, tracking membership payments, and managing gym equipment are
streamlined and automated. This ensures accuracy, reduces manual workload, and
enhances the overall experience for both staff and members. The system also supports
easy scalability, making it adaptable for fitness centers of all sizes. By adopting such a
system, fitness centers can focus more on delivering quality services while ensuring
efficient and reliable data management.

29
30

You might also like