To DROP a Unique Constraint
Suppose we have to DROP that column, particularly in the table.
Syntax:
ALTER TABLE Instructor
DROP INDEX ID;
Queries:
Find all the instructors that taught at most one course in the year 2017.
Instructor relation:
EmployeeID | Name | CourseID | Year |
---|---|---|---|
77505 | Alan | SC110 | 2017 |
77815 | Will | CSE774 | 2017 |
85019 | Smith | EE457 | 2017 |
92701 | Sam | PYS504 | 2017 |
60215 | Harold | HSS103 | 2016 |
77505 | Alan | BIO775 | 2017 |
92701 | Sam | ECO980 | 2017 |
Without Using Unique Constraint
Query:
SELECT I.EMPLOYEEID, I.NAME
FROM Instructor as I
WHERE UNIQUE (SELECT Inst.EMPLOYEEID
FROM Instructor as Inst
WHERE I.EMPLOYEEID = Inst.EMPLOYEEID
and Inst.YEAR = 2017);
By Using Unique Constraint
Query:
SELECT Name
FROM Instructor
WHERE Year = 2017
GROUP BY Name
HAVING COUNT(DISTINCT CourseID) = 1;
Output:
EmployeeID | Name |
---|---|
77815 | Will |
85019 | Smith |
Explanation: In the Instructor relation, only instructors Will and Smith teach a single course during the year 2017. The sub-query corresponding to these instructors contains only a single tuple and therefore the unique clause corresponding to these instructors evaluates to true thereby producing these two instructors in the output relation.
Example 2
Find all the courses in the Computer Science department that has only a single instructor allotted to that course.
Course Relation
CourseID | Name | Department | InstructorID |
---|---|---|---|
CSE505 | Computer Network | Computer Science | 11071 |
CSE245 | Operating System | Computer Science | 74505 |
CSE101 | Programming | Computer Science | 12715 |
HSS505 | Psychology | Social Science | 85017 |
EE475 | Signals & Systems | Electrical | 22150 |
CSE314 | DBMS | Computer Science | 44704 |
CSE505 | Computer Network | Computer Science | 11747 |
CSE314 | DBMS | Computer Science | 44715 |
Without Unique Constraint
Query:
SELECT C.COURSEID, C.NAME
FROM Course as C
WHERE UNIQUE (SELECT T.INSTRUCTORID
FROM Course as T
WHERE T.COURSEID = C.COURSEID
and C.DEPARTMENT = 'Computer Science');
By Using UNIQUE Constraint
Query:
SELECT CourseID
FROM Instructor
WHERE CourseID LIKE 'CSE%'
GROUP BY CourseID
HAVING COUNT(DISTINCT Name) = 1;
Output:
COURSE_ID | Name |
---|---|
CSE245 | Operating System |
CSE101 | Programming |
Explanation: In the course relation, the only courses in the computer science department that have a single instructor allotted are Operating Systems and Programming. The unique constraint corresponding to these two courses has only a single tuple consisting of the corresponding instructors. So, the unique clause for these two courses evaluates to true and these courses are displayed in output relation. Other courses in the Course relation either have two or more instructors or they do not belong to the computer science department and therefore, those courses aren’t displayed in the output relation.
SQL | UNIQUE Constraint
SQL Constraints Unique constraints in SQL is used to check whether the sub-query has duplicate tuples in its result. It returns a boolean value indicating the presence/absence of duplicate tuples. Unique constraint returns true only if the subquery has no duplicate tuples, else it returns false.