Assignment 3: SQL Query-Based Approach to Detect and Remove Partial Dependencies
Scenario:
CREATE TABLE Student_Courses (
StudentID INT,
CourseID INT,
InstructorName VARCHAR(50),
InstructorEmail VARCHAR(50),
StudentName VARCHAR(50),
PRIMARY KEY (StudentID, CourseID)
);1. SQL Query to Detect Partial Dependencies and Explanation:
- Concept of Partial Dependency: A partial dependency exists when a non-prime attribute (an attribute that is not part of the primary key) is functionally dependent on only a part of the composite primary key.
- Partial Dependencies in the Table:
StudentNamedepends only onStudentID.InstructorNameandInstructorEmaildepend only onCourseID.
SQL Query to Illustrate Partial Dependency on StudentName:
SELECT StudentID, StudentName
FROM Student_Courses
GROUP BY StudentID, StudentName
HAVING COUNT(DISTINCT CourseID) > 1;- This query groups by
StudentIDandStudentName. If a student (identified byStudentID) has the sameStudentNamebut is enrolled in multiple courses (indicated byCOUNT(DISTINCT CourseID) > 1), it demonstrates thatStudentNameis only dependent onStudentIDand not the entire primary key.
SQL Query to Illustrate Partial Dependency on InstructorName and InstructorEmail:
SELECT CourseID, InstructorName, InstructorEmail
FROM Student_Courses
GROUP BY CourseID, InstructorName, InstructorEmail
HAVING COUNT(DISTINCT StudentID) > 1;- Similar to the previous query, this one groups by
CourseID,InstructorName, andInstructorEmail. If a course has the same instructor details but multiple students enrolled, it demonstrates thatInstructorNameandInstructorEmailare dependent onCourseIDonly.
2. Decomposition into Two or More Tables:
To remove partial dependencies, we decompose the table into three tables:
Students(StudentID, StudentName)Courses(CourseID, InstructorName, InstructorEmail)StudentCourses(StudentID, CourseID) - a junction table to link students and courses.
3. SQL Commands to Create New Tables and Insert Data:
-- Create Students table
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(50)
);
-- Create Courses table
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
InstructorName VARCHAR(50),
InstructorEmail VARCHAR(50)
);
-- Create StudentCourses table
CREATE TABLE StudentCourses (
StudentID INT,
CourseID INT,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
-- Insert data (example)
INSERT INTO Students (StudentID, StudentName) VALUES
(1, 'Alice'),
(2, 'Bob');
INSERT INTO Courses (CourseID, InstructorName, InstructorEmail) VALUES
(101, 'Dr. Smith', 'smith@example.com'),
(102, 'Prof. Johnson', 'johnson@example.com');
INSERT INTO StudentCourses (StudentID, CourseID) VALUES
(1, 101),
(1, 102),
(2, 101);4. Demonstrate How the New Structure Eliminates Partial Dependencies:
- In the new structure,
StudentNameis directly and fully dependent onStudentIDin theStudentstable. InstructorNameandInstructorEmailare directly and fully dependent onCourseIDin theCoursestable.- The
StudentCoursestable only contains the composite primary key (StudentID,CourseID), and there are no non-prime attributes, thus no partial dependencies.
Queries to Retrieve Data:
-- Get student names and the courses they are enrolled in
SELECT S.StudentName, C.CourseID
FROM Students S
JOIN StudentCourses SC ON S.StudentID = SC.StudentID
JOIN Courses C ON SC.CourseID = C.CourseID;
-- Get instructor details for a specific course
SELECT InstructorName, InstructorEmail
FROM Courses
WHERE CourseID = 101;