Skip to content

Commit 376126c

Browse files
authored
Merge pull request #316 from NikhilBhargav/main
How to Alter Constraints in SQL
2 parents 0793697 + a33e854 commit 376126c

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
INSERT INTO Department (id, name, code) VALUES
2+
(6, 'Social Sciences and Humanities', 'SSH');
3+
4+
5+
-- Insert in Course for SSH
6+
INSERT INTO Course (id, name, textbook, credits, is_active, department_id) VALUES
7+
('SSH111', 'Macroeconomics', 'Macroeconomics by Krugman, P., Wells, R., 2nd edition', 4, 'Yes', 6),
8+
('SSH211', 'Econometrics I', 'Introductory Econometrics: A modern approach by Jeffrey M. Wooldridge (4th edition, Cengage India)', 4, 'Yes', 6),
9+
('SSH411', 'Econometrics II', 'Microeconometrics: Methods and Applications by A. Colin Cameron and Pravin K. Trivedi', 6, 'Yes', 6),
10+
('SSH112', 'Market Design', 'Auction Theory by Vijay Krishna, Academic Press', 4, 'Yes', 6);
11+
12+
13+
--Drop the constraint
14+
ALTER TABLE Course
15+
DROP CONSTRAINT course_department_id_fkey;
16+
17+
--Recreate
18+
ALTER TABLE Course
19+
ADD CONSTRAINT course_department_id_fkey
20+
FOREIGN KEY (department_id) REFERENCES Department(id)
21+
ON DELETE CASCADE;
22+
23+
DELETE
24+
FROM Department
25+
WHERE id=6;
26+
27+
ALTER TABLE Student
28+
ADD CONSTRAINT CHK_GPA
29+
CHECK (gpa BETWEEN 3.0 AND 5.0);
30+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
INSERT INTO Department (id, name, code) VALUES
2+
(6, 'Social Sciences and Humanities', 'SSH');
3+
4+
-- Insert in Course for SSH
5+
INSERT INTO Course (id, name, textbook, credits, is_active, department_id) VALUES
6+
('SSH111', 'Macroeconomics', 'Macroeconomics by Krugman, P., Wells, R., 2nd edition', 4, 'Yes', 6),
7+
('SSH211', 'Econometrics I', 'Introductory Econometrics: A modern approach by Jeffrey M. Wooldridge (4th edition, Cengage India)', 4, 'Yes', 6),
8+
('SSH411', 'Econometrics II', 'Microeconometrics: Methods and Applications by A. Colin Cameron and Pravin K. Trivedi', 6, 'Yes', 6),
9+
('SSH112', 'Market Design', 'Auction Theory by Vijay Krishna, Academic Press', 4, 'Yes', 6);
10+
11+
--Drop the constraint
12+
ALTER TABLE Course
13+
DROP CONSTRAINT course_department_id_fkey;
14+
15+
--Recreate
16+
ALTER TABLE Course
17+
ADD CONSTRAINT course_department_id_fkey
18+
FOREIGN KEY (department_id) REFERENCES Department(id)
19+
ON DELETE CASCADE;
20+
21+
--Delete Course
22+
DELETE
23+
FROM Department
24+
WHERE id=6;
25+
26+
ALTER TABLE Student
27+
ADD CONSTRAINT CHK_GPA
28+
CHECK (gpa BETWEEN 3.0 AND 5.0);
29+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
INSERT INTO Department (id, name, code) VALUES
2+
(6, 'Social Sciences and Humanities', 'SSH');
3+
4+
--Insert in Course for SSH
5+
INSERT INTO Course (id, name, textbook, credits, is_active, department_id) VALUES
6+
('SSH111', 'Macroeconomics', 'Macroeconomics by Krugman, P., Wells, R., 2nd edition', 4, 'Yes', 6),
7+
('SSH211', 'Econometrics I', 'Introductory Econometrics: A modern approach by Jeffrey M. Wooldridge (4th edition, Cengage India)', 4, 'Yes', 6),
8+
('SSH411', 'Econometrics II', 'Microeconometrics: Methods and Applications by A. Colin Cameron and Pravin K. Trivedi', 6, 'Yes', 6),
9+
('SSH112', 'Market Design', 'Auction Theory by Vijay Krishna, Academic Press', 4, 'Yes', 6);
10+
11+
--List constraints
12+
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
13+
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
14+
WHERE TABLE_NAME = 'Course';
15+
16+
--Drop and alter constraint in same query
17+
ALTER TABLE Course
18+
NO CHECK CONSTRAINT course_department_id_fkey,
19+
DROP CONSTRAINT course_department_id_fkey,
20+
ADD CONSTRAINT course_department_id_fkey
21+
FOREIGN KEY (department_id) REFERENCES Department(id)
22+
ON DELETE CASCADE;
23+
24+
--Delete the course SSH
25+
DELETE
26+
FROM Department
27+
WHERE id=6;
28+
29+
30+
ALTER TABLE Student
31+
ADD CONSTRAINT CHK_GPA
32+
CHECK (gpa BETWEEN 3.0 AND 5.0);
33+

0 commit comments

Comments
 (0)