I Break the table into smaller tables to remove these partial dependencies.
Resulting Tables (2NF):
Student Table:
Course Table:
Instructor Table:
Enrollment Table:
Task 5: Provide SQL Queries
Provide the SQL queries to show how data insertion, data retrieval, data
updation, and data deletion works in the normalized database.
-- Step 1: Create Database
CREATE DATABASE UniversityDB;
USE UniversityDB;
-- Step 2: Create Tables
CREATE TABLE Student (
Std_ID INT PRIMARY KEY,
Std_Name VARCHAR(50) NOT NULL
);
-- Table: Instructor
CREATE TABLE Instructor (
Instr_ID INT PRIMARY KEY,
Instr_Name VARCHAR(50) NOT NULL
);
-- Table: Course
CREATE TABLE Course (
Course_ID VARCHAR(10) PRIMARY KEY,
Course_Name VARCHAR(100) NOT NULL,
Instr_ID INT NOT NULL,
FOREIGN KEY (Instr_ID) REFERENCES Instructor(Instr_ID)
);
-- Table: Enrollment
CREATE TABLE Enrollment (
Std_ID INT NOT NULL,
Course_ID VARCHAR(10) NOT NULL,
Grade CHAR(1),
PRIMARY KEY (Std_ID, Course_ID),
FOREIGN KEY (Std_ID) REFERENCES Student(Std_ID),
FOREIGN KEY (Course_ID) REFERENCES Course(Course_ID)
);
-- Step 3: Insert Data into Tables
INSERT INTO Student (Std_ID, Std_Name) VALUES
(1, 'Arslan'),
(2, 'Fatima'),
(3, 'Ahmad'),
(4, 'Laiba');
-- Insert into Instructor table
INSERT INTO Instructor (Instr_ID, Instr_Name) VALUES
(1001, 'Dr. Ahmed'),
(1002, 'Dr. Sara'),
(1003, 'Dr. Khalid'),
(1004, 'Dr. Zainab');
-- Insert into Course table
INSERT INTO Course (Course_ID, Course_Name, Instr_ID) VALUES
('CS101', 'Data Structures', 1001),
('MTS203', 'Linear Algebra', 1002),
('CS103', 'Operating Systems', 1003),
('CS104', 'Machine Learning', 1002),
('CS105', 'Artificial Intelligence', 1004);
-- Insert into Enrollment table
INSERT INTO Enrollment (Std_ID, Course_ID, Grade) VALUES
(1, 'CS101', 'A'),
(1, 'MTS203', 'B'),
(2, 'CS101', 'B'),
(2, 'CS103', 'A'),
(3, 'CS104', 'A'),
(3, 'CS105', 'A'),
(4, 'CS101', 'C');
-- Step 4: Data Retrieval Queries
-- Retrieve all students
SELECT * FROM Student;
-- Retrieve all instructors
SELECT * FROM Instructor;
-- Retrieve all courses
SELECT * FROM Course;
-- Retrieve all enrollments
SELECT * FROM Enrollment;
-- Retrieve students with Grade 'A'
SELECT Student.Std_Name, Course.Course_Name, Enrollment.Grade
FROM Enrollment
JOIN Student ON Enrollment.Std_ID = Student.Std_ID
JOIN Course ON Enrollment.Course_ID = Course.Course_ID
WHERE Enrollment.Grade = 'A';
-- Step 5: Data Updation Queries
-- Update a student name
UPDATE Student
SET Std_Name = 'Ali'
WHERE Std_ID = 1;
-- Update an instructor name
UPDATE Instructor
SET Instr_Name = 'Dr. Imran'
WHERE Instr_ID = 1001;
-- Step 6: Data Deletion Queries
-- Delete a student record
DELETE FROM Student
WHERE Std_ID = 4;
-- Delete all enrollments for a course
DELETE FROM Enrollment
WHERE Course_ID = 'CS101';
-- Delete an instructor
DELETE FROM Instructor
WHERE Instr_ID = 1004;