0% found this document useful (0 votes)
32 views4 pages

Practical 1a

This document outlines a 1-hour practical exercise for students to design, implement, and manipulate a relational database for a school management system using SQL. It includes tasks for setting up the database, populating data, performing SQL queries, and applying indexing and constraints. By the end of the exercise, students will be able to create tables, write basic SQL queries, and understand the importance of indexing and constraints in databases.

Uploaded by

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

Practical 1a

This document outlines a 1-hour practical exercise for students to design, implement, and manipulate a relational database for a school management system using SQL. It includes tasks for setting up the database, populating data, performing SQL queries, and applying indexing and constraints. By the end of the exercise, students will be able to create tables, write basic SQL queries, and understand the importance of indexing and constraints in databases.

Uploaded by

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

1-Hour Practical Exercise: Database Management (Relational Database Design & SQL Queries)

This exercise is designed to help students design, implement, and manipulate a small relational
database for a school management system using MySQL, SQL Server, PostgreSQL, SQLite, or Microsoft
Access.

Objective:

• Understand how to create and manipulate relational databases.

• Learn to write basic SQL queries (CREATE, INSERT, UPDATE, DELETE, SELECT).

• Practice joins, indexing, and constraints.

Time Required: 1 hour

Part 1: Setting Up the Database (15 minutes)

Task: Create a School Management Database

1. Open MySQL Workbench (or any chosen database software) and create a new database called
SchoolDB.

2. Create the following tables with relationships:

o Students (StudentID, FirstName, LastName, Age, Gender, CourseID)

o Courses (CourseID, CourseName, InstructorID)

o Instructors (InstructorID, FirstName, LastName, Department)

SQL Code:

CREATE DATABASE SchoolDB;

USE SchoolDB;

CREATE TABLE Students (

StudentID INT PRIMARY KEY AUTO_INCREMENT,

FirstName VARCHAR(50),

LastName VARCHAR(50),

Age INT,

Gender ENUM('Male', 'Female', 'Other'),

CourseID INT,

FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)


);

CREATE TABLE Courses (

CourseID INT PRIMARY KEY AUTO_INCREMENT,

CourseName VARCHAR(100),

InstructorID INT,

FOREIGN KEY (InstructorID) REFERENCES Instructors(InstructorID)

);

CREATE TABLE Instructors (

InstructorID INT PRIMARY KEY AUTO_INCREMENT,

FirstName VARCHAR(50),

LastName VARCHAR(50),

Department VARCHAR(50)

);

Part 2: Populating Data (10 minutes)

Task: Insert Sample Data into Tables

SQL Code:

-- Insert data into Instructors table

INSERT INTO Instructors (FirstName, LastName, Department) VALUES

('John', 'Doe', 'Computer Science'),

('Jane', 'Smith', 'Mathematics'),

('Mike', 'Johnson', 'Physics');

-- Insert data into Courses table

INSERT INTO Courses (CourseName, InstructorID) VALUES

('Database Management', 1),

('Algebra', 2),
('Physics 101', 3);

-- Insert data into Students table

INSERT INTO Students (FirstName, LastName, Age, Gender, CourseID) VALUES

('Alice', 'Brown', 20, 'Female', 1),

('Bob', 'Green', 22, 'Male', 2),

('Charlie', 'White', 19, 'Male', 3),

('Diana', 'Black', 21, 'Female', 1);

Part 3: Querying the Database (15 minutes)

Task: Perform Basic SQL Queries

1⃣ Retrieve All Student Records

SELECT * FROM Students;

2⃣ Get Student Names and Their Enrolled Course

SELECT Students.FirstName, Students.LastName, Courses.CourseName

FROM Students

JOIN Courses ON Students.CourseID = Courses.CourseID;

3⃣ Update Student Information

UPDATE Students

SET Age = 23

WHERE FirstName = 'Bob' AND LastName = 'Green';

4️⃣ Delete a Student Record

DELETE FROM Students WHERE FirstName = 'Charlie' AND LastName = 'White';

5⃣ Count the Number of Students in Each Course

SELECT Courses.CourseName, COUNT(Students.StudentID) AS StudentCount

FROM Students

JOIN Courses ON Students.CourseID = Courses.CourseID

GROUP BY Courses.CourseName;
Part 4️: Indexing and Constraints (10 minutes)

Task: Create an Index & Add Constraints

1⃣ Create an Index for Faster Searches

CREATE INDEX idx_students_name ON Students(FirstName, LastName);

2⃣ Ensure No Duplicate Course Names

ALTER TABLE Courses ADD CONSTRAINT unique_course_name UNIQUE (CourseName);

3⃣ Add a Not Null Constraint to Ensure All Students Have an Age

ALTER TABLE Students MODIFY Age INT NOT NULL;

Part 5: Wrap-Up & Review (10 minutes)

1. Test the Queries – Run each query and verify the output.

2. Modify the Database – Try adding a new instructor and a new course.

3. Discussion Questions:

o What does indexing do?

o Why are constraints important in databases?

o How would you expand this database for a larger school system?

Expected Outcomes

By the end of this 1-hour practical exercise, students should be able to:
Create relational tables with primary and foreign keys.
Write basic SQL queries to insert, update, delete, and retrieve records.
Perform JOIN operations to combine data from multiple tables.
Use indexing and constraints to optimize and secure data.

Next Steps: Students can extend this exercise by:

• Creating stored procedures for automation.

• Implementing triggers for automated updates.

• Adding user authentication for a web application.

This hands-on approach makes database learning engaging and practical, preparing students for real-
world applications in business, IT, and software development.

You might also like