Professional Documents
Culture Documents
CS2102
CS2102
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