Relational Algebra Cheat Sheet PDF

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

Relational Algebra:

Selection (σ): Projection (π):


Selects tuples that satify certain conditions. Projects a subset of a table’s columns.
Operators:​ −, =, ≠, ≥, <, >, ≤ Connectors:​ ​and​, ​or​, n
​ ot
π​ btag, first_name (Bird)
​ — Projects the tag and first name of all Birds
σ​ sentiment > 0.9 (Chirp)
​ — Selects Chirps with sentiment less than 0.9.
σ​ last_name = 'Trump' ​(Bird) — Selects Birds whose last name is Trump.

*Cross-Product (×): *Difference (−):


Combines two relations with every possible combination of tuples. Selects tuples that are present in one relation but not the other.

*Union (∪): Natural Join (⋈):


Selects tuples that are present in both relations. Combines two relations by finding a common attribute between them

Conditional Join (⋈​C​): Division (÷):


Combines two relations similar to cross product but with a condition Reduces a relation by performing the opposite of a cartesian product

*Must be union compatible: 1) Same number of columns 2) Corresponding columns are of the same variable type

Relational Calculus [Examples]:


Sailors(sid, sname, rating, age), Reserves(sid, bid, date), Boats(bid, bname, color)

1. Find sailors with a rating > 7


{s | s ∈ S ailors ⋀ s.rating > 7}
2. Find names of sailors who’ve reserved a red boat
{t(sname) | ∃s ∈ S ailors(t.sname = s.sname ⋀ ∃r ∈ Reserves(r.sid = s.sid ⋀ ∃b ∈ B oats(b.bid = r.bid ⋀ b.color = ′red′)))}
3. Find the names of sailors who’ve reserved all “Interlake” boats
{t(sname) | ∃s ∈ S ailors(t.sname = s.sname ⋀ ∀b ∈ B oats(b.bname = ′Interlake′ → (∃r ∈ Reserves(r.sid = s.sid ⋀ b.bid = r.bid))))}
MySQL:

Queries: Insertions:
SELECT INSERT INTO​ table_name (field1, field2, …fieldN)
● DISTINCT ​VALUES
(value1, value2, …valueN);
● T.attr, T.attr ​as​ attribute
● COUNT​(*) Deletions:
● MAX​(T.attr), ​MIN​(T.attr), ​AVG​(T.attr) DELETE FROM​ table_name [​WHERE​ clause]
FROM
● Table T Views:
● Table2 T2 CREATE VIEW​ view_name(attr1, attr2, …) ​AS
WHERE SELECT ​[]
● =, !=, >, <, >=, <= FROM ​[]
WHERE ​[]
● [NOT]​ IN
● [NOT] ​EXISTS
Procedures: ​CALL P​ rocedure(params)
● ANY CREATE PROCEDURE​ NewChirp(
● ALL new_btag ​VARCHAR​(30),
● LIKE _ ​(1 char), ​% ​(n chars) content ​VARCHAR​(255))
GROUP BY BEGIN
● attribute ​DECLARE ​new_cno ​INT​(11);
​SET ​new_cno = (​SELECT MAX​(cno)+1 ​FROM C ​ hirp
HAVING
​WHERE ​btag = new_btag);
● condition on grouping ​INSERT INTO​ Chirp(btag, cno, content)
​VALUES ​(new_btag, new_cno, content);
END

Alter Table:
DROP COLUMN​ col_name
DROP PRIMARY KEY
DROP FOREIGN KEY​ fk_name

Update Table:
UPDATE ​[​LOW_PRIORITY​] [​IGNORE​] table
SET ​column1 = expression1,
column2 = expression2,

[​WHERE​ conditions]
[​ORDER BY​ expression [​ASC​ | ​DESC​]]
[​LIMIT​ number_rows]

You might also like