Published by Patrick Mutisya · 14 days ago
Write an SQL script to query or modify data (DML) that are stored in at most two database tables.
| Command | Purpose | Example |
|---|---|---|
| CREATE TABLE | Define a new table and its columns. | CREATE TABLE Student (StudentID INT PRIMARY KEY, Name \cdot ARCHAR(50), Age INT); |
| ALTER TABLE | Modify an existing table (add, drop, or change columns). | ALTER TABLE Student ADD Email \cdot ARCHAR(100); |
| DROP TABLE | Delete a table and all its data. | DROP TABLE Student; |
| Command | Purpose | Example |
|---|---|---|
| SELECT | Retrieve rows that satisfy a condition. | SELECT Name, Age FROM Student WHERE Age > 18; |
| INSERT | Add new rows to a table. | INSERT INTO Student (StudentID, Name, Age) VALUES (101, 'Alice', 19); |
| UPDATE | Modify existing rows. | UPDATE Student SET Age = Age + 1 WHERE StudentID = 101; |
| DELETE | Remove rows from a table. | DELETE FROM Student WHERE StudentID = 101; |
In relational algebra, a query can be expressed as \$ \sigma{condition}(R) \$ (selection) or \$ \pi{attributes}(R) \$ (projection). SQL implements these operations with WHERE and SELECT clauses respectively.
We will work with the following tables:
Student – stores basic student information.Enrolment – records which courses each student is taking.CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name \cdot ARCHAR(50) NOT NULL,
Age INT CHECK (Age >= 16)
);
CREATE TABLE Enrolment (
EnrolmentID INT PRIMARY KEY,
StudentID INT REFERENCES Student(StudentID),
CourseCode \cdot ARCHAR(10) NOT NULL,
Semester \cdot ARCHAR(6) NOT NULL
);
The script below demonstrates inserting data, updating a record, deleting a row, and retrieving information using a join.
-- 1. Insert new students
INSERT INTO Student (StudentID, Name, Age) VALUES
(201, 'Bob', 20),
(202, 'Clara', 19);
-- 2. Enrol the students in courses
INSERT INTO Enrolment (EnrolmentID, StudentID, CourseCode, Semester) VALUES
(301, 201, 'CS101', 'Fall21'),
(302, 202, 'CS101', 'Fall21'),
(303, 202, 'MA102', 'Fall21');
-- 3. Update a student's age (Bob has a birthday)
UPDATE Student
SET Age = Age + 1
WHERE StudentID = 201;
-- 4. Delete an enrolment (Clara drops MA102)
DELETE FROM Enrolment
WHERE EnrolmentID = 303;
-- 5. Query: list all students and the courses they are currently enrolled in
SELECT s.Name, e.CourseCode, e.Semester
FROM Student s
JOIN Enrolment e ON s.StudentID = e.StudentID
ORDER BY s.Name;
INSERT to add data to both tables.UPDATE to modify a single attribute.DELETE to remove a specific enrolment.SELECT with an INNER JOIN to combine data from the two tables.StudentID in Enrolment must exist in Student.DELETE without a WHERE clause removes all rows.WHERE clause uniquely identifies the target rows to avoid unintended changes.Student (1) and Enrolment (M).
Phone to the Student table?REFERENCES clause in the Enrolment table definition is important.ALTER TABLE Student ADD Phone \cdot ARCHAR(15);SELECT Name FROM Student WHERE StudentID NOT IN (SELECT StudentID FROM Enrolment);REFERENCES clause enforces referential integrity, preventing enrolment records that refer to non‑existent students and ensuring that deletions/updates cascade appropriately.