Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

● SET NULL: updates foreign keys of referencing Function

tuples to NULL
/* if else */ /* while loop */
IF temp > 60 THEN WHILE temp > 30 LOOP
ER diagrams
temp := temp/2; temp := temp / 2;
n-ary relation set: a relationship with n entity roles.
ELSIF temp > 50 THEN END LOOP;
n is the degree of the relationship.
temp := temp-20;
Total participation constraint on E wrt R: E
ELSE
participates at least once in relationship R
temp := temp - 10;
Weak entity set:
END IF;
1. Does not have its own key
Conditional Expression 2. must be a many-to-one relationship FOREACH d IN ARRAY denoms LOOP
select name, (case when marks >= 70 then ’A’ else ’D’ 3. total participation temp := temp / d;
end) as grade END LOOP;
Partial Participation constraint
from Scores; Each instance of E participates in >=
Coalesce 0 instances of R RETURN QUERY SELECT mark1, mark2;
Returns the first non-null value, if all null, returns -- can just select value into mark and no need return if
null value Key constraint on E wrt R not return table table
coalesce(col, [value]) returns value if col is null Each instance of E participates in at
NULLIF more 1 instance of R SELECT * FROM fname(1, 2);
nullif(v1, v2) returns null if v1 == v2, else return v1 SELECT fname(1, 2); -- both returns table
Total participation on E wrt R Procedure
COUNT(*) / CAST(c.population AS DECIMAL) Each instance of E participates in at CREATE OR REPLACE PROCEDURE AddGradeAttr() AS $$
least 1 instance of R ALTER TABLE Scores
-- division by integer gives math floor
select ’Price of ’ || round(price / 1.3) || ’ USD’ as ADD COLUMN IF NOT EXISTS Grade CHAR(1) DEFAULT NULL;
One-One relationship SELECT * FROM Scores;
menu Each instance of E participates in
-- CONCAT $$ LANGUAGE sql;
exactly 1 instance of R
INSERT INTO t(column_list) CALL AddGradeAttr();
VALUES (value_list),(value_list), ....; E is a weak entity with identifying Cursor
owner E’ and identifying relationship ● FETCH curs INTO r;
UPDATE t set R ● FETCH NEXT FROM curs INTO r;
SET c1 = new_value, c2 = new_value ● FETCH PRIOR FROM curs INTO r; -- Fetch from
Aggregation
WHERE condition; previous row
The box connected to the relationship denotes that it is
● FETCH FIRST FROM curs INTO r;
participating in the other relationship as an
DELETE FROM t; -- delete all from table aggregation. ● FETCH LAST FROM curs INTO r;
DELETE FROM t Conversion: If relationship R is the entity in ● FETCH ABSOLUTE 3 FROM curs INTO r;
WHERE condition; aggregation then must be defined first. ● FETCH RELATIVE -2 FROM curs INTO r;
ALTER TABLE t ADD c; ISA Hierarchies ● FETCH [PRIOR | FIRST | LAST | ABSOLUTE n | RELATIVE
ALTER TABLE t DROP COLUMN c; Overlap constraint: Entity can belong to multiple n] [FROM] <cursor> INTO <var>
ALTER TABLE t ADD/DROP constraint classes ● MOVE [PRIOR | FIRST | LAST | ABSOLUTE n | RELATIVE
Covering constraint: Entity must belong to >= 1 class n] [FROM] <cursor>; [UPDATE | DELETE] <table> ...
Foreign Key - WHERE CURRENT OF curs;
(1) Primary Key Value in reference relation Trigger
(2) null value (can have duplicates) ● TG_OP: trigger operation
● NO ACTION: rejects delete/update if it violates ● TG_TABLE_NAME: trigger table name
constraints CREATE CONSTRAINT TRIGGER trigger_name AFTER INSERT ON
● RESTRICT: similar to NO ACTION except that table_name
constraint checking can’t be deferred DEFERRABLE INITIALLY DEFERRED
● CASCADE: propagates delete/update to referencing FOR EACH ROW EXECUTE FUNCTION func();
tuples
● SET DEFAULT: updates foreign keys of referencing -- for constraint that is DEFERRABLE INITIALLY IMMEDIATE
tuples to some default value BEGIN TRANSACTION;
Conversion: REFERENCES PARENT ON DELETE CASCADE SET CONSTRAINTS bal_check_trigger DEFERRED; -- defer
COMMIT; 3NF Decomposition Algo

BEFORE Return value affects the action


INSERT/ ● RETURN NULL, no action performed;
UPDATE/ ● RETURN OLD is same as RETURN NULL for
DELETE INSERT if OLD is not initialised
AFTER: Return value does not matter

INSTEAD ● Occurs in place of the specified action


OF (only applicable for views)
INSERT/ ● only allowed on row-level
UPDATE/ ● RETURN NULL will cause all operations
DELETE (including other triggers) to be
ignored;
● Returning non-null value means proceed
as normal
BCNF 3NF
FOR ● Calls the trigger function for each
EACH tuple involved in the statement if every non-trivial iff for every non-trivial and
ROW and decomposed FD’s decomposed FD
LHS is a superkey. - Either LHS is a superkey
FOR ● Calls the trigger function once for the - Or the RHS is a prime
EACH whole statement attribute (i.e., it appears in
STATEME ● Return value does not matter a key)
NT ● RETURN NULL would not make the database
omit the subsequent operations violation of BCNF, Violation of 3NF, iff we have
● For subsequent operations to be iff we have a closure a closure that satisfies the
omitted, raise exception that satisfies the “more but not all” property
● NEW and OLD not defined "more but not all" and the extra attribute is not
condition a prime attribute
● No SELECT in WHEN BCNF Decomposition Algo
● No OLD in WHEN for INSERT ● recursively decompose into table with attributes in
● No NEW in WHEN for DELETE the closure {X}+ that violates BCNF and table with
● No WHEN for INSTEAD OF X and other attributes not in that closure.
CREATE TRIGGER for_Elise_trigger BEFORE INSERT ON ● If we have a table R(X, Y, Z) with {X}+ = {X, Y},
Scores FOR EACH ROW WHEN (NEW.Name = 'Elise') EXECUTE then decompose R into R1(X, Y) and R2(X, Z) until
FUNCTION for_Elise_func(); all tables are in BCNF
Minimal Basis Algo
CREATE OR REPLACE FUNCTION for_Elise_func() RETURNS
1. Transform the FDs, so that each RHS contains only
TRIGGER AS $$ BEGIN
NEW.Mark := 100; one attribute
RETURN NEW; END; $$ LANGUAGE plpgsql; 2. Remove redundant attributes on the LHS of each FD
3. Remove redundant FDs
Functional Dependencies E.g. S = {A → BD, AB → C, C → D, BC → D}
Axiom of Reflexivity: ABCD → ABC
Axiom of Augmentation: if A → B, then AC → BC
Axiom of Transitivity: If A → B and B → C then A → C
Rule of Decomposition: If A → BC, then A → B and A →
C
Rule of Union: If A → B and A → C, then A → BC
Prime attributes: attribute that appears in a key
Lossless-join Decomposition: common attribute in R1 and
R2 constitute a superkey of R1 or R2
Dependency Preservation
S - original set. S’ - decomposed set of fd.
S and S’ must be equivalent so every FD in S can be AY21/22 Semester 2
derived from S’ and every FD in S’ from S. Ang Koon Hwee, Wu Xiao Yun

You might also like