Relational databases - Student with RegClass 3 Tables
Relational databases - Student with RegClass 3 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:
This creates a database called SchoolDB. You would use this statement when setting up a new
database to store your tables and data.
Next, you would create the Students table. The CREATE TABLE statement defines the
structure of the table, including its columns and their data types.
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).
The Registration table stores class registration details and links to the RegClass from the
Students table.
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.
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).
Example Scenario:
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.
This inserts 10 students with unique StudentID values, names, and registration classes
(RegClass).
This inserts 10 rooms with unique RoomCode values, locations, and a boolean indicating
whether the room has IT facilities.
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:
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.
This query retrieves all columns and rows from the Students table.
Explanation:
Learning Outcome:
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:
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.
Explanation:
Learning Outcome:
Introduces GROUP BY for aggregation and using aggregate functions like COUNT().
Explanation:
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.
These queries will help students become familiar with common SQL operations and
understand how relational databases work with multiple tables and relationships.