🖥️ Relational algebra
- Algebra
- Mathematical system consisting of
- Operands: variables or values from which new values can be constructed
- Operators: symbols denoting procedures that construct new values from given operands
- Relational Algebra
- A procedural language consisting of a set of operations that take one or two relations as input and produce a new relation as their output
Select: σ
- The select operation selects tuples that satisfy a given predicate
- Notation: σp(r)
∧ (and), ∨ (or), ¬ (not)
Ex_ ![](https://velog.velcdn.com/images/choihjin/post/1ad3c6f6-bc85-425e-816b-b2070f7b1672/image.png)
![](https://velog.velcdn.com/images/choihjin/post/8538524f-ebb4-4ba1-b11d-e6aec0b69fb7/image.png)
![](https://velog.velcdn.com/images/choihjin/post/2054d4ae-91c5-4919-9ab3-425c516f8d63/image.png)
Project: ∏
- A unary operation that returns its argument relation, with certain attributes left out
- Notation:∏A1,A2,A3,...,Ak(r)
➡️ Duplicate rows are removed
Ex_![](https://velog.velcdn.com/images/choihjin/post/46a706b8-7a0c-4b28-9284-308e7263e8b5/image.png)
![](https://velog.velcdn.com/images/choihjin/post/107feee4-68da-4e6e-9ae8-0ca6eaab1a88/image.png)
Cartesian product: ×
- The Cartesian-product operation (denoted by ×) combines information from any two relations
- Binary operator (2 operands) ➡️ cf. σ, ∏ : unary operator(1 operand)
Ex_![](https://velog.velcdn.com/images/choihjin/post/de4c3ff6-48bd-4de2-ad1d-aaa972217801/image.png)
Join: ⋈
- The join operation combines a select operation and a Cartesian- Product operation into a single operation
r⋈θs=σθ(r×s)
Ex_![](https://velog.velcdn.com/images/choihjin/post/ccbe5c35-870c-4f03-a137-ddc0c884327c/image.png)
Union: ∪
- The union operation combines two relations as a superset of both
- Notation: r∪s
- ⭐️ For r∪s tobevalid,
- r, s must have the same number of attributes (same arity)
- The attribute domains must be compatible
- E.g., the 2nd column of r deals with the same type of values as does the 2nd column of s
➡️ ⭐️ Duplicate remove
Ex_![](https://velog.velcdn.com/images/choihjin/post/9f9caed4-cde3-4e90-ab8d-fb9c05c7f756/image.png)
Set-intersection: ∩
- The set-intersection operation finds tuples that are in both the input relations
- Notation: r∩s
- Assumptions:
- r,s have the same arity
- Attributes of r and s are compatible
Ex_![](https://velog.velcdn.com/images/choihjin/post/1ae2c28e-3ae5-42e4-a925-1f06a606a433/image.png)
Set-difference: –
- The set-difference operation finds tuples that are in one relation but are not in another
- Notation: r–s
- Assumptions:
- r,s have the same arity
- Attributes of r and s are compatible
Ex_![](https://velog.velcdn.com/images/choihjin/post/2034b3e4-5da8-4b78-b3b9-a1419e1ae46c/image.png)
Assignment Operation: ⬅️
- It is convenient at times to write a relational-algebra expression by assigning parts of it to temporary relation variables
- Notation: ⬅️
- With the assignment operation, a query can be written as a sequential program
Ex_![](https://velog.velcdn.com/images/choihjin/post/52fbf1fa-c9ad-4371-813e-641375874f1a/image.png)
Rename: ρ
- The rename operator, r, sets names to relational-algebra expressions
- Notation: ρnew_name(E) E : Entity ( Relation or Attribute )
HGU 전산전자공학부 홍참길 교수님의 23-1 Database System 수업을 듣고 작성한 포스트이며, 첨부한 모든 사진은 교수님 수업 PPT의 사진 원본에 필기를 한 수정본입니다.
Example problem
Find the records of the instructor(s) who get(s) the largest salary
tmp1 ⬅️ ρinst1(instructor)×ρinst2(instructor)
tmp2 ⬅️ σinst1.salary<inst2.salary(tmp1)
tmp2 ⬅️ ∏inst1.ID,inst1.name,inst1.dept_name,inst1.salary(tmp2)
A. instructor−tmp3