CS2102 final notes
when using the distinct keyword, and order by is used, the column used for
distinct has to be included in the the order by .
when using select with group by , what the user selects should be included in the
group by clause as per SQL standard.
union , intersect , and except by default make results distinct elements
order of executions. from , where , group by , having , select
where is order by??
04 ER diagrams
key attributes
any attribute in any of the keys considered key attribute? but which key? superkey?
key? or actual primary key of the table?
Candidate key
set of all attributes that uniquely identifies the entities.
but is it a set of set, or set of keys?
and is it referring to any possible set, or the actual attributes marked black in
the ERD?
3 rules
attribute set --> attributes
entity set -> relations
relationship set -> relations
candidate keys -> primary key or unique not null
3 exceptions
maximum is 1 in cardinality e.g.(x, 1) of a relationship
by right relationship set's primary key is the keys of participating entities.
change that to just the key of the entity set that has cardinality of max 1.
now you can't add more than one relationship for entity of that set by just
changing the other entity.
cardinality of minimum 1 e.g.(1, x)
merge the relationship table and the entity table with that cardinality
now it is impossible to forget, that when you create an entity, you also have to
give it a relationship.
weak entity (student, universty)
by the simple 3 rules the entity student is defined by matric number alone
But it is weak entity so studen't primary key should be (matric, university_key)
Nested Queries
ways to create temporary table
CREATE TABLE mytable AS ~
CREATE TEMPORARY TABLE ~
CREATE VIEW mytable as ~
WITH (SELECT ~) AS mytable --> best as only persistant in query
using tuples
use IN, ANY, ALL with nested queries
use EXISTS
Functions and Procedures
Function
return primitive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
create or replace function find_best_score()
returns int as $$
declare
mark int;
best int;
curs cursor for (Select * in scores);
begin
best := -1;
open curs;
r record;
loop
fetch curs into r;
exit when not found;
if r.mark > best or best < 0 then
r.best:= r.mark;
end if;
return next;
end loop;
close curs;
end;
$$ language plpgsql
Cursor
1
2
3
4
fetch prior from curs into r;
fetch first from curs into r;
fetch last from curs into r;
fetch absolute 3 from curs into r;
5
Loop
1
2
3
4
5
6
for r in select * from Scores order by mark desc
loop
name:= r.name;
score:= r.score;
return next;
end loop;
return record
1
create function top_mar
out mark int in function argument
just a select statement in body suffice
setof record
instead of returns setof record , returns table(name varchar, score int) also
suffice
table_name and setof table_name
normal select statement suffice
Triggers
1
2
3
4
create trigger check_something
before update or insert or delete on scores
for each row
execute function do_check();
Instead of operator
return null -> ignore operation
return non-null -> proceed as normal
but other operation in triger functio nmight work
1
2
3
4
create constraint trigger check_something
before update on Scores
deferrable initially deferred
-- rest the same
1
2
3
4
create constraint trigger check_something
before update on Scores
deferrable initially immediate
-- rest the same
Transaction
above can be used like
1
2
3
begin transaction;
set constraints balance_check deferred;
commit;
1
2
3
4
5
6
7
8
9
create function do_check() returns trigger as $$
declare
best int; worst int;
begin
if new.name > 'Marcus' then
new.mark = 'A'
end if;
end;
$$ language plpgsql
options
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if TG_OP = 'insert'
insert on table
delete on table
update [of column] on table
for each row [when (new.name = 'adi')]
-- no select in when
-- no old in when for insert
-- no new in when for delete
-- no when for instead of
after or before
instead of
for each row
for each statement
Raise Exception 'You are not supposed to';
return null;
Raise notice 'youare not...'
order of execution
1. before statement level
2. before row level
3. after row level
4. after statement level
Relational Algebra
σ[name=‘bill’](restaurant)
where
from restaurant where name = 'bill'
ρ(restaurant, r)
is renaming
restaurant as r
ρ(restaurant, r, r.name -> r.myName)
π[name](restaurant)
is renaming
is select
select name from restaurant
BCNF
is a table with functional dependencies where 'all the LHS' of non-trivial and decomposed
dependencies are superkeys
method: find any closure that is 'more but not all'
is a lossless join
from fact that 'two tables with common column that is superkey for one of them form
lossless join'
Algorithm
1. find any more but not all closure, {X}
+
2. divide the table into {X} and set of 'X with the other columns not in X'
+
3. check if the first table is in BCNF if not, decompose
4. check if the second table is in BCNF if not, decompose
Projecting dependencies
when projecting R to R
1
compute all closures of subset of R , using the FDs of R
1
remove attributes irrelevant to R
1
decompose it
3NF
FD preservation: when table R is decomposed to some tables R , R , … FD is preserved
when S , the set of functional dependencies of R and S , the set of functional
1
2
′
dependencies of the decomposed tables are equivalent.
FD equivalence: two sets of FD set are equal if and only if one can be derived from
another in both ways. (use closure for checking)
def: for all non-trivial and decomposed FDs, LHS should be a superkey or the RHS
should be a prime attribute
prime attribute: an attribute in a key of a table
a table is in BCNF -> it is 3NF
a table is not 3NF -> not BCNF
minimal basis/cover
from a FD set S , minimal cover S can be derived
m
A set S is minimal cover of S if
m
Sm
is equivalent to S
all FDs in S is non-trivial and decomposed
m
no FD in it is redundant
no element in LHS of any FD is redundant.(cannot be removed)
Steps, given a set R
1. convert all FDs to decomposed form
2. check any redundant attribute on the LHS of the FDs, and remove.
3. check any redundant FDs and remove.
3NF decomposition
1. Given table R and a FD set S , find minimal cover S
′
2. merge FDs with the same LHS
3. construct table for each FD in S
′
4. if there is no table with the key of R, create one for it
5. remove any subsumed table(table already contained in another)
3NF vs BCNF
3NF has anomalies in rare cases. BCNF has none
3NF preserves FDs.