Chapter 2 - Relational Algebra Flashcards

1
Q

What is relational algebra?

A

It is a procedural language used for performing operations on relational DB.
It serves as the foundational, low-level operations that are used by Database Management Systems (DBMSs) to manipulate data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Relational Algebra vs SQL?

A

Relational Algebra is procedural, meaning it specifies how operations should be carried out step by step. Users define a series of operations to be performed on data.

SQL, on the other hand, is declarative. In SQL, users specify what data they want to retrieve or manipulate and the DBMS determines how to execute the request.

Relational algebra is basically more low level and SQL more high level

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does it mean when we say relational algebra forms the basis for the implementation of DBMSs.

A

It provides the fundamental operations that a DBMS needs to perform tasks like querying, inserting, updating and deleting data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Click to see procedural and declarative difference

A

Procedure language
Specify what to get and how
to get
E.g.
do while(isspace(s)) s++;
while(
d++ = *s++)

Declarative language
Specify what to get
E.g.
out_str = re.sub (r’^\s+’, ‘ ‘,
input_string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can relational algebra operators be divided into? At a higher level.

A

It can be divided into basic operators and extended/derived operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What can basic operators be divided into?

A

Unary operators and Binary operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a unary operator

A

Unary operators are operators that operate on a SINGLE OPERAND.

These operators are often used for tasks such as NEGATION, INCREMENTING, DECREMENTING or CHANGING SIGN OF A SINGLE VALUE.

Example Unary operators in programming language include the negation operator -x, the logical NOT operator !x and the increment operator x++

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a binary operator?

A

Binary operators are operators that operate on two operands. They require two values or variables to perform an operation.

They are commonly used for arithmetic operations (addition, subtraction, multiplication, division), logical comparisons (equal to, not equal to, greater than, less than) and other operations that involve two values.

Examples of binary operators include addition (x+y), equality comparison (x==y), and logical AND (x && y).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What falls under Unary operators out of the Fundamental Operators?

A
  1. Projection (π)
  2. Selection operator (σ)
  3. Rename operator (p)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is the select operator represented?

A

The Select operator in relational algebra is represented as 𝜎𝑃(𝑟), where:
𝑟 represents one relation (table).
𝑃 represents one or many attributes of relation 𝑟, often referred to as predicates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is P

A

P stands for Predicate.
They are conditions or expressions that can involve logical operators like ‘and’ (∧), ‘or’ (∨), and ‘not’ (¬).

Comparison operators like >=, >, =, !=, <, and <= can also be used in predicates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Click to see how a select statement is written in relational algebra

A

𝜎(Gender = ‘Female’ ∧ Age > 20)(Students)

In this predicate:

“Gender = ‘Female’” is a condition that checks if the student’s gender is female.
“Age > 20” is a condition that checks if the student’s age is greater than 20.
The ‘∧’ (logical AND) operator is used to combine these conditions, meaning both conditions must be true for a row to be selected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is the project operator represented? and how does this operator work?

A

Notation: π𝑎,𝑏,…(𝑟)
r represents one relation.
a,b represents the attributes (columns) that you want to select from the relation.

Project operator is a column-wise operation that returns only specified columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is it possible during project operation to end up with return of fewer tuples?

A

It is possible for the Project operator to return fewer tuples (rows) than the original relation r. This happens when there are duplicate values in the selected columns.

Duplicate values are removed in the result, resulting in a reduced number of tuples

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What would the project operator look like in SQL?

A

SELECT a, b
FROM r

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What would the selection operator look like in SQL?

A

SELECT *
FROM r
WHERE P

15
Q

Click to see how a project statement is written in relational algebra

A

π(FirstName, LastName,Department)(Employees)

16
Q

Click to see example of partial relation input

A

π_{Location}(𝜎_{YOB>280}(GOT)).

17
Q

What does the set union operator do? How is it represented?

A

The Set Union operator, represented as r u s, is used to combine (union) the tuples of the two relations, denoted as r and s.
It retrieves all the tuples from both relations but ELIMINATES DUPLICATE TUPLES

18
Q

What is the key characteristics of set union?

A

The key characteristic is that duplicate tuples are not included in the result.

19
Q

What is the condition for Set Union to be valid?

A

The condition is that relations r and s must have the same relation schema.
The schemas should match in terms of attribute names, types and order.

20
Q

In Set Union, what happens when the schema of the relations are not the same?

A

We cut part of the columns (PROJECT).

21
Q

What is set difference

A

Let’s say r - s. We will deduct whatever is in relation s IN THE RELATION r.

22
Q

is r - s and s - r the same thing in set difference?

A

NO

23
Q

How does rename work?

A

Initial relation r:

Change relation r to relation x
𝜌𝑥 𝑟 , Relation x
Change relation and attributes to relation y with attributes c d e
𝜌𝑦(𝐶,𝐷,𝐸) 𝑟 , Relation y

24
Q

Why do we require additional operators?

What are these additional operators?

A

This is because fundamental operators are able to query, but the expression of the query is considered quite complex. With additional operators, it can help to simplify things.

The additional operators are:
1) Join operators
2) Divide operators
3) Intersection

25
Q

What is the notation for set intersection?

What is the condition for set intersection?

What is the formula for set intersection?

A

Notation: s ∩ t

Condition: Relation schemas must be compatible

Formula: (s ∪ t) – (s – t) – (t – s) OR s - (s - t)

26
Q

How is natural join represented?

A

s ⋈ t for relation s and relation t

It contains all the attributes of both tables, but only one copy of common column.
It only takes the stuff that matches too

27
Q

How is natural join different from outer join?

A

Natural join requires the perfect match. If you retain some imperfect matches, then that would be outer join.

28
Q

What happens since outer join keeps the imperfect matches?

A

We represent the missing values with NULL.

29
Q

What is left outer join and how is it represented?

A

Left outer join: r ⟕ s.

For tuples in left relation r, if not match, keep as NULLs.
For tuples in right relation s. If not match perfectly, delete.

30
Q

What is full outer join and how is it represented?

A

R ⟗ S.

Keep both that don’t match. KEEP EVERYTHING

31
Q

What is right outer join and how is it represented?

A

Right outer join: r ⟖ s.

For tuples in left relation r, if not match, delete
For tuples in right relation s. If not match perfectly, keep as NULL

32
Q

How does division work in relational algebra?

A

Basically u1/u2 and we only keep the things in U1 that totally contains U2.
(Refer to slides for info)

33
Q

How does assignment work in relational algebra?

A

Given new tuple(s) E and relation s:
* Suppose the constraint can be met, i.e., same relation schema.
* E potentially can be quite complicated, i.e., a long operation expression.
* Insert: s ← s ∪ E.
* Specify actual items, also feasible.
* Delete: s ← s – E.
* Delete Casterly Rock.
* s ← s – E, where
* E = 𝜎_{Loc=‘Casterly Rock’}(s).
* Or, can also be written as:
* s ← 𝜎_{Loc≠‘Casterly Rock’}(s).
* Update: s ← Π(s)
* s ← Π_{ID+5, Name, Location, YOB}(s).Specify the schema clearly.

34
Q

What constitutes good performance with regards to DB?

A

Making applications run faster
Lowering response time of queries/transactions
Improving overall transaction throughout