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

Relational databases - Student with RegClass 3 Tables

The document explains the use of SQL CREATE statements to define and create tables and databases in a relational database, specifically for a school database. It provides detailed examples of creating a database, tables for students, rooms, and registrations, along with inserting dummy data into these tables. Additionally, it includes various SQL queries to demonstrate how to interact with the database, covering basic to complex operations like selecting, filtering, joining, and aggregating data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Relational databases - Student with RegClass 3 Tables

The document explains the use of SQL CREATE statements to define and create tables and databases in a relational database, specifically for a school database. It provides detailed examples of creating a database, tables for students, rooms, and registrations, along with inserting dummy data into these tables. Additionally, it includes various SQL queries to demonstrate how to interact with the database, covering basic to complex operations like selecting, filtering, joining, and aggregating data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

In relational databases, the CREATE statement is used to define and create new tables,

databases, or other database objects. It's essential for structuring and organizing your data
within the database. Below, I'll provide SQL CREATE statements for the example in the image
and explain their use.

1. Create Database:

First, you can create a database to hold your tables:

CREATE DATABASE SchoolDB;

This creates a database called SchoolDB. You would use this statement when setting up a new
database to store your tables and data.

2. Create Students Table:

Next, you would create the Students table. The CREATE TABLE statement defines the
structure of the table, including its columns and their data types.

CREATE TABLE Students (


StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
RegClass VARCHAR(10)
);

 StudentID: This is an INT data type, and it is the Primary Key for the table, meaning
it must be unique for each student.
 FirstName and LastName: These are VARCHAR(50), meaning they are variable-
length character fields up to 50 characters long.
 RegClass: This column is a VARCHAR(10), storing the registration class (e.g., 9A,
10B).

3. Create Registration Table:

The Registration table stores class registration details and links to the RegClass from the
Students table.

CREATE TABLE Registration (


RegClass VARCHAR(10) PRIMARY KEY,
Teacher VARCHAR(50),
RoomCode VARCHAR(10),
FOREIGN KEY (RegClass) REFERENCES Students(RegClass),
FOREIGN KEY (RoomCode) REFERENCES Room(RoomCode)
);

 RegClass: This column is a VARCHAR(10) and acts as the Primary Key for this table.
 Teacher: A VARCHAR(50) to store the name of the teacher assigned to the class.
 RoomCode: A VARCHAR(10) which links to the RoomCode column in the Room table,
making it a Foreign Key.
4. Create Room Table:

The Room table contains information about the rooms where classes take place.

CREATE TABLE Room (


RoomCode VARCHAR(10) PRIMARY KEY,
Location VARCHAR(100),
IT_facilities BOOLEAN
);

 RoomCode: This is a VARCHAR(10) and is the Primary Key for the Room table.
 Location: A VARCHAR(100) field to store the room's location (e.g., IT Block, Maths
Block).
 IT_facilities: A BOOLEAN data type that indicates whether the room has IT facilities
(TRUE or FALSE).

Explanation of CREATE Usage:

 CREATE DATABASE: Used to set up a new database.


 CREATE TABLE: Used to create a new table within the database. It is followed by the
table name and a list of columns with their data types and constraints.
 PRIMARY KEY: Ensures that each value in the column is unique and non-null. It is
often used for identifying records.
 FOREIGN KEY: Establishes a link between two tables. It ensures referential integrity by
making sure the value in the foreign key column matches a primary key value in
another table.
 Data Types: Each column in the table has a specified data type, such as INT,
VARCHAR, or BOOLEAN, to define the kind of data it can store.

Example Scenario:

1. You create a database called SchoolDB to store student-related data.


2. You create a table called Students to store details about students.
3. Then, you create a Registration table that links student classes to specific teachers
and rooms.
4. Finally, you create a Room table that stores information about each room in the school.

This structure allows the tables to interact through foreign keys and ensures that the data is
organized without redundancy.
Here’s an example of an INSERT statement that adds dummy data to the three tables
(Students, Registration, and Room) in the relational database based on the structure you
provided earlier.

1. Insert Data into Students Table:


INSERT INTO Students (StudentID, FirstName, LastName, RegClass)
VALUES
(027341, 'Bradley', 'Jenkins', '9A'),
(027342, 'Jamie', 'Cable', '9A'),
(027343, 'Charlotte', 'Pegg', '10B'),
(027344, 'Imogen', 'Fletcher', '10B'),
(027345, 'Alex', 'Taylor', '9B'),
(027346, 'Jordan', 'Morris', '9B'),
(027347, 'Olivia', 'King', '10A'),
(027348, 'Samuel', 'Adams', '10A'),
(027349, 'Ethan', 'Roberts', '9A'),
(027350, 'Lily', 'Brown', '10B');

This inserts 10 students with unique StudentID values, names, and registration classes
(RegClass).

2. Insert Data into Room Table:


INSERT INTO Room (RoomCode, Location, IT_facilities)
VALUES
('Room 7', 'IT Block', TRUE),
('Room 5', 'Maths Block', FALSE),
('Room 21', 'IT Block', TRUE),
('Room 8', 'Science Block', FALSE),
('Room 12', 'Library', TRUE),
('Room 15', 'Art Block', FALSE),
('Room 9', 'History Block', TRUE),
('Room 14', 'Sports Hall', FALSE),
('Room 11', 'Music Room', TRUE),
('Room 6', 'Drama Block', FALSE);

This inserts 10 rooms with unique RoomCode values, locations, and a boolean indicating
whether the room has IT facilities.

3. Insert Data into Registration Table:


INSERT INTO Registration (RegClass, Teacher, RoomCode)
VALUES
('9A', 'Mr Craddock', 'Room 7'),
('9A', 'Mr Rouse', 'Room 5'),
('10B', 'Mr Thompson', 'Room 7'),
('10B', 'Miss Walters', 'Room 12'),
('9B', 'Mr Morris', 'Room 5'),
('9B', 'Mr Daniels', 'Room 9'),
('10A', 'Miss Walters', 'Room 21'),
('10A', 'Mr Adams', 'Room 6'),
('9A', 'Mrs Clark', 'Room 15'),
('10B', 'Miss Evans', 'Room 11');
This inserts 10 rows into the Registration table, linking students' registration classes
(RegClass) to their teachers and room codes (RoomCode).

Explanation of Data:

 The Students table contains data about 10 students, including their IDs, names, and
registration classes.
 The Room table contains 10 room codes, with their respective locations and whether
IT facilities are available.
 The Registration table links each student's registration class with the appropriate
teacher and room code.

Relationships:

 The RegClass in the Students table links with the RegClass in the Registration
table (Foreign Key).
 The RoomCode in the Room table is referenced in the Registration table as a foreign
key to ensure that valid room codes are used.

This setup ensures the integrity of your data by linking the tables correctly.
Here's the SQL document that contains both the CREATE and INSERT statements for creating
the tables and inserting the dummy data:

-- Create Database (optional, if not already created)


CREATE DATABASE SchoolDB;

-- Use the database (if not already in use)


USE SchoolDB;

-- Create Students Table


CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
RegClass VARCHAR(10)
);

-- Create Room Table


CREATE TABLE Room (
RoomCode VARCHAR(10) PRIMARY KEY,
Location VARCHAR(100),
IT_facilities BOOLEAN
);

-- Create Registration Table


CREATE TABLE Registration (
RegClass VARCHAR(10) PRIMARY KEY,
Teacher VARCHAR(50),
RoomCode VARCHAR(10),
FOREIGN KEY (RegClass) REFERENCES Students(RegClass),
FOREIGN KEY (RoomCode) REFERENCES Room(RoomCode)
);

-- Insert Dummy Data into Students Table


INSERT INTO Students (StudentID, FirstName, LastName, RegClass)
VALUES
(027341, 'Bradley', 'Jenkins', '9A'),
(027342, 'Jamie', 'Cable', '9A'),
(027343, 'Charlotte', 'Pegg', '10B'),
(027344, 'Imogen', 'Fletcher', '10B'),
(027345, 'Alex', 'Taylor', '9B'),
(027346, 'Jordan', 'Morris', '9B'),
(027347, 'Olivia', 'King', '10A'),
(027348, 'Samuel', 'Adams', '10A'),
(027349, 'Ethan', 'Roberts', '9A'),
(027350, 'Lily', 'Brown', '10B');

-- Insert Dummy Data into Room Table


INSERT INTO Room (RoomCode, Location, IT_facilities)
VALUES
('Room 7', 'IT Block', TRUE),
('Room 5', 'Maths Block', FALSE),
('Room 21', 'IT Block', TRUE),
('Room 8', 'Science Block', FALSE),
('Room 12', 'Library', TRUE),
('Room 15', 'Art Block', FALSE),
('Room 9', 'History Block', TRUE),
('Room 14', 'Sports Hall', FALSE),
('Room 11', 'Music Room', TRUE),
('Room 6', 'Drama Block', FALSE);
-- Insert Dummy Data into Registration Table
INSERT INTO Registration (RegClass, Teacher, RoomCode)
VALUES
('9A', 'Mr Craddock', 'Room 7'),
('9A', 'Mr Rouse', 'Room 5'),
('10B', 'Mr Thompson', 'Room 7'),
('10B', 'Miss Walters', 'Room 12'),
('9B', 'Mr Morris', 'Room 5'),
('9B', 'Mr Daniels', 'Room 9'),
('10A', 'Miss Walters', 'Room 21'),
('10A', 'Mr Adams', 'Room 6'),
('9A', 'Mrs Clark', 'Room 15'),
('10B', 'Miss Evans', 'Room 11');

Explanation:

 CREATE DATABASE: This creates the SchoolDB database (optional if it already exists).
 CREATE TABLE: Each table is defined with specific columns and their data types:
o Students table stores student information.
o Room table stores room details.
o Registration table links RegClass and RoomCode between Students and
Room.
 INSERT INTO: Adds dummy data to each table. It populates the Students, Room, and
Registration tables with 10 rows of data each.

To execute this script, you can run it in a MySQL, PostgreSQL, or similar relational database
management system (RDBMS) that supports SQL syntax.
Here are five SQL queries, ranging from simple to complex, designed to help students learn
how to interact with relational databases. These queries are based on the previous tables
(Students, Room, and Registration) and demonstrate the basic to advanced concepts in
SQL.

1. Simple Query: Select All Students

This query retrieves all columns and rows from the Students table.

SELECT * FROM Students;

Explanation:

 SELECT *: This selects all columns from the table.


 FROM Students: This specifies the table from which to retrieve the data.

Learning Outcome:

 Introduces the SELECT statement to retrieve all records from a table.

2. Query with WHERE Clause: Filter Students by Registration Class

This query retrieves students who are registered in class 9A.

SELECT FirstName, LastName, RegClass


FROM Students
WHERE RegClass = '9A';

Explanation:

 WHERE RegClass = '9A': This filters the students who belong to class 9A.
 SELECT FirstName, LastName, RegClass: This selects only the FirstName,
LastName, and RegClass columns.

Learning Outcome:

 Introduces the WHERE clause to filter data based on specific conditions.

3. Join Query: Get Students with Their Room Details

Here's the Join Query without using aliases:

SELECT Students.FirstName, Students.LastName, Students.RegClass,


Registration.RoomCode
FROM Students
JOIN Registration ON Students.RegClass = Registration.RegClass;
Explanation:

 Students.FirstName, Students.LastName, Students.RegClass,


Registration.RoomCode: This selects the first name, last name, and registration
class from the Students table and the room code from the Registration table.
 JOIN Registration ON Students.RegClass = Registration.RegClass: This
performs an inner join between the Students and Registration tables, linking them by
the RegClass column.

This query works the same as the previous one, but without using table aliases (s and r). The
full table names are used directly in the query.

4. Aggregate Query: Count the Number of Students in Each Class

This query counts the number of students in each registration class.

SELECT RegClass, COUNT(*) AS StudentCount


FROM Students
GROUP BY RegClass;

Explanation:

 COUNT(*): This counts the number of students in each RegClass.


 GROUP BY RegClass: This groups the data by RegClass so the count is calculated for
each class.

Learning Outcome:

 Introduces GROUP BY for aggregation and using aggregate functions like COUNT().

5. Complex Query: Students with Room Information and IT Facilities

Here’s the Complex Query without using aliases:

SELECT Students.FirstName, Students.LastName, Students.RegClass,


Room.RoomCode, Room.Location, Room.IT_facilities
FROM Students
JOIN Registration ON Students.RegClass = Registration.RegClass
JOIN Room ON Registration.RoomCode = Room.RoomCode
WHERE Room.IT_facilities = TRUE
ORDER BY Students.LastName;

Explanation:

 Students.FirstName, Students.LastName, Students.RegClass,


Room.RoomCode, Room.Location, Room.IT_facilities: This selects the students'
names, their registration class, and room details (including whether the room has IT
facilities).
 JOIN Registration ON Students.RegClass = Registration.RegClass: Joins
the Students table with the Registration table based on the RegClass column.
 JOIN Room ON Registration.RoomCode = Room.RoomCode: Joins the Registration
table with the Room table based on the RoomCode column.
 WHERE Room.IT_facilities = TRUE: Filters the results to only show rooms that
have IT facilities.
 ORDER BY Students.LastName: Orders the results by students' last names in
ascending order.

This version of the query avoids the use of table aliases, using full table names instead.

Learning Outcome:

 Combines multiple JOIN operations and WHERE conditions to retrieve data from
multiple tables.
 Demonstrates how to filter and sort data using ORDER BY.

Summary of Learning Outcomes:

1. Basic SELECT query to retrieve all records from a table.


2. Filtering with WHERE to get specific records.
3. JOIN operations to combine related data from different tables.
4. Using aggregation with GROUP BY to perform calculations on grouped data.
5. Multiple JOINs and complex filtering to work with data from multiple tables and
conditions.

These queries will help students become familiar with common SQL operations and
understand how relational databases work with multiple tables and relationships.

You might also like