3. Formal Relational Query Languages Flashcards
What are the two types of Relational Query Languages?
- Relational algebra (procedural), consists of a set of operations that take one or two relations as input and produce a new relation as their result.
- Relational calculus (declarative), describes the desired information without giving a specific procedure for obtaining that information.
- Note that they are logically equivalent and can express the same queries (Codd’s theorem)
What is the basis of a relational algebra query?
It explains how to obtain the result
e.g course_id (semester=“Fall” Λ year=2014)
What is the basis of a relational calculus query?
It explains what is required
e.g {t | t in instructor Λ t[salary]>25000 }
What are the 6 fundamental relational algebra operations?
- Select (unary)
- Project (unary)
- Rename (unary)
- Union (binary)
- Set difference (binary)
- Cartesian product (binary)
What 2 operations can you derive from the fundamental relational algebra operations?
- Set intersection (binary)
* Natural join (binary)
What is Select?
Selection of tuples from a relation
• σ p(r)
• p = selection predicate, condition for selection
σ p(r) = {t | t ε r p(t)}
What is Project?
Selection of attributes from a relation
• π A1, A2, … Ak (r)
• r = relation name
π staff_id, name, salary (instructor)
What is Union?
Tuples that appear in r or s or both
• r ∪ s
• r ∪ s={t|t ε r Λ t ε s}
What 2 conditions have to be valid for Union?
- r, s must have same arity (same number of attributes)
2. The attribute domains must be compatible
What is Set difference?
Tuples that appear in r but not in s
• r – s
• Same conditions as Union
What is Cartesian product?
Combine two relations: each tuple in r is joined with each tuple in s
• r x s
• r x s = {t q | t ε r Λ q ε s}
What is Set intersection?
Tuples that appear in both r and s
• r ∩ s
• r ∩ s = { t | t ε r Λ t ε s }
What is Natural join?
Combine two relations by matching values of common attributes in r and s
• r ⨝ s