-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsm_hw3d.sql
More file actions
79 lines (68 loc) · 3.48 KB
/
dsm_hw3d.sql
File metadata and controls
79 lines (68 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
-- Develop triggers that support the following integrity constraints using the HW3.sql:
-- Offsprings have at MOST one male, one female parent. Consider NULLS.
-- When inserting or updating specimen or ancestry, have trigger(s) raise the following exceptions:
-- If parent gender is null: Parent gender is set to NULL.
-- If the offspring already has two parents: Offspring already has two parents.
-- If the offspring has the same gendered parent: Offspring already has this gender parent, fix ancestries first.
-- --trial 1
-- /*One male, one female*/
-- INSERT INTO Ancestry VALUES(1004, 1002);
-- INSERT INTO Ancestry VALUES(1004, 1003);
-- /*Two females*/
-- INSERT INTO Ancestry VALUES(1001, 1004);
-- INSERT INTO Ancestry VALUES(1001, 1002);
--expected output: ERROR: Offspring already has this gender parent, fix ancestries first.
-- --------------------------
-- --trial 2
-- /*Update male specimen to female or NULL*/
-- UPDATE Specimen SET Gender='F' WHERE EID = 1003;
--expected output: ERROR: Offspring already has this gender parent, fix ancestries first.
-- --------------------------
-- --trial 3
-- /*Update male specimen to female or NULL*/
-- UPDATE Specimen SET Gender=NULL WHERE EID = 1003;
--expected output: ERROR: Parent gender is set to NULL.
-- --------------------------
CREATE OR REPLACE FUNCTION check_parent_count() RETURNS TRIGGER AS $$
BEGIN
IF (SELECT COUNT(*) FROM Ancestry WHERE EID = NEW.EID) > 1 THEN
RAISE EXCEPTION 'ERROR: Offspring already has two parents.';
-- ELSE if new.parent's gender = 'F' AND IF there exists already one 'F' parent for this offspring
-- Raise ERROR
ELSEIF (SELECT COUNT(*) FROM Specimen WHERE EID = NEW.Parent AND gender ='F')>=1 THEN
IF (SELECT COUNT(*) FROM Ancestry JOIN Specimen ON Ancestry.Parent=Specimen.EID WHERE NEW.EID=Ancestry.EID
AND Specimen.Gender ='F')>=1 THEN
RAISE EXCEPTION 'ERROR: Offspring already has this gender parent, fix ancestries first.';
END IF;
-- ELSE if new.parent's gender = 'M' AND IF there exists already one 'M' parent for this offspring
-- Raise ERROR
ELSEIF (SELECT COUNT(*) FROM Specimen WHERE EID = NEW.Parent AND gender ='M')>=1 THEN
IF (SELECT COUNT(*) FROM Ancestry JOIN Specimen ON Ancestry.Parent=Specimen.EID WHERE NEW.EID=Ancestry.EID
AND Specimen.Gender ='M')>=1 THEN
RAISE EXCEPTION 'ERROR: Offspring already has this gender parent, fix ancestries first.';
END IF;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER check_parent_count_trigger
BEFORE INSERT OR UPDATE ON Ancestry
FOR EACH ROW EXECUTE PROCEDURE check_parent_count();
--1. FIND THE NEW.EID'S OFFSTRING IN ANCESTRY TABLE
--2. CHECK IF THERE EXISTS SAME GENDER PARENT IN ANCESTRY TABLE AS NEW'S PARENT
--3. IF YES, RAISE ERROR
--4. IF NO, RETURN NEW
CREATE OR REPLACE FUNCTION check_parent_specimen() RETURNS TRIGGER AS $$
BEGIN
IF (SELECT COUNT(*) FROM Ancestry a1 JOIN Ancestry a2 ON a1.EID=a2.EID JOIN Specimen ON a2.parent = Specimen.EID WHERE a1.parent = NEW.EID
AND a2.parent <> NEW.EID AND Specimen.GENDER=NEW.GENDER) >= 1 THEN
RAISE EXCEPTION 'ERROR: Offspring already has this gender parent, fix ancestries first.';
ELSEIF NEW.GENDER IS NULL THEN
RAISE EXCEPTION 'ERROR: Parent gender is set to NULL.';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER check_parent_specimen_trigger
BEFORE INSERT OR UPDATE ON Specimen
FOR EACH ROW EXECUTE PROCEDURE check_parent_specimen();