Questions tagged [relational-algebra]

Relational Algebra is an offshoot of first-order logic and of the algebra of sets that deals with relations (sets of tuples). In Computer Science, Relational Algebra is commonly used when dealing with databases. Operators in Relational Algebra use relations as operands and produce a relation as a result.

Relational Algebra provides a formal system for working with relations. In the context of databases, it can be thought of as a more formal way of constructing queries on relations.

Relational Algebra supports the following operations:

  • Set operators: Union (∪), Difference (-), Intersection (∩) (relations must be union compatible i.e., the number of attributes and their domains must match).
  • Projection (π): This is a unary operation that lets you select a specific subset of attributes (columns) from a relation.
  • Selection (σ): This is a unary operation such that the expression σφ(R) lets you select a subset of relations from the relation R that satisfy a certain propsition φ. This proposition is expressed using logical operators where the atoms are of the form aθb where a and b refer to attributes and θ refers to a binary operation in the set {<, <=, =, >=, >}
  • Rename (ρ): This is a unary operation of the form ρa / b(R) where the result is identical to R except that the attribute a has been renamed to b. This operator can be used to rename attributes or the relation itself.
  • Natural join (⋈): Natural joins (⋈) is a binary operator that is written as (R⋈S) where R and S are relations. The result of the operation is the set of all combinations of tuples in R and S that have equal values on their common attribute names.
  • Theta Join (⋈aθb): A theta join is a conditional join where the result of the operation is the set of all combinations of tuples in R and S that satisfy the condition specified. Using the already-defined operators a theta join can be expressed as R⋈φS = σφ(R × S).
  • Division (÷): Division is a binary operation that is written as R ÷ S. Assuming R is defined as R(a, b) and S is defined as S(b), the result of R ÷ S is the relation T(a) where a tuple <a> is only in T if there are tuples in R of the form <a, b> such that an a in R is associated with every value of b in S.

When teaching databases, Relational Algebra is used as a formal foundation before the introduction of SQL (Structured Query Language). SQL is similar to Relational Algebra except that it supports a few extended operators. A few of the differences can be summed up as follows:

  • Relational Algebra is procedural whereas SQL is declarative
  • Relational Algebra deals with sets of tuples whereas SQL deals with bags.
  • Standard Relational Algebra does not support aggregate operators like summing.
  • Standard Relational Algebra does not support grouping.
  • Standard Relational Algebra does not support ordering or sorting.
  • Standard Relational Algebra does not support pattern-matching operators.

External links:

577 questions
4
votes
1 answer

Functional dependencies in database

I have the following set of functional dependencies on the relation schema r(A, B, C, D, E, F ) : A -> BCD BC -> DE B -> D D -> A Can anyone show with explanation how to find the candidate keys for this relation ?
4
votes
1 answer

Arithmetic division in relational algebra

I have an SQL request: SELECT table1.nr1 / NULLIF(table2.nr2, 0) as percentage and I want to write this in relational algebra. Is it possible to represent arithmetic division in relational algebra?
Daniela
  • 471
  • 7
  • 25
4
votes
1 answer

Struggling to wrap my head around this Database Query

I am completing Stanford's Relational Algebra course and am having a hard time understanding one of the questions, although I have got the answer. Here is the schema: /* Delete the tables if they already exist */ drop table if exists…
philosopher
  • 1,079
  • 2
  • 16
  • 29
4
votes
2 answers

How to implement relational equivalent of the DIVIDE operation in SQL Server

I was reading relational algebra from one of the textbook. I came across DIVIDE operation. From Wikipedia: The division is a binary operation that is written as R ÷ S. The result consists of the restrictions of tuples in R to the attribute names…
Mahesha999
  • 22,693
  • 29
  • 116
  • 189
4
votes
2 answers

How to get the values that are repeated in an attribute of a relation?

So I have a relation: Cars(model, passenger) The models are all unique, let's say, {A, B, C, D, E}. Passengers is just the capacity of the car (any positive non-zero integer), let's say {1,2,2,3,3} Model|Passenger A |1 B |2 C |2 D …
Doronz
  • 704
  • 8
  • 21
4
votes
1 answer

SQL: How to enforce functional dependency using constraints?

Assume we have a table: CREATE TABLE Jobs ( JobID INT PRIMARY KEY , AssignedUser VARCHAR(10) , Zone VARCHAR(10) ) The constraint we need to enforce is this: make sure that no user is assigned jobs in more than one zone,…
4
votes
2 answers

Relational algebra for banking scenario

I don't know how to solve the relational algebra questions. Deposit (Branch, Acc-No, Cust-Name, Balance) Loan (Branch, Loan-No, Cust-Name, Balance) Branch (Branch, Assets, Branch-County) Customer (Cust-Name, Cust-County, Branch) Produce a relation…
user3145348
  • 105
  • 1
  • 9
4
votes
1 answer

Python code for determining which normal form tabular data is in

I'm looking for Python code that can take tabular data and establish which normal form(s) it is in (if any) and show any functional dependencies, etc.
4
votes
2 answers

Relational division in LINQ?

Relational division is one of Codd's primitive relational operators, colloquially known as the suppliers who supply all parts. There have been various translations into SQL e.g. Celko discusses several approaches using the example of the pilots who…
onedaywhen
  • 55,269
  • 12
  • 100
  • 138
4
votes
3 answers

Do the "columns" in a table in a RMDB have order?

I learned that there is no concept of order in terms of tuples (e.g. rows) in a table but according to wikipedia "a tuple is an ordered list of elements". Does that mean that attributes do have an order? If yes why would they be treated differently,…
Celeritas
  • 14,489
  • 36
  • 113
  • 194
4
votes
3 answers

Is there set division in SQL?

I'm fully aware that set division can be accomplished through a series of other operations, so my question is: Is there a command for set division in SQL?
Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
3
votes
1 answer

Absolute value in ARel (rails)

I need to do some absolute values in ARel. Basically need to understand how to execute this line (in Relational Algebra) using ARel: Assume P is a table with columns value1 and value2: Project((|p.value1 - 10| + |p.value2 - 10|) as match) P Not…
Nima Gardideh
  • 575
  • 1
  • 4
  • 10
3
votes
1 answer

Strange result from function -- what's going on?

I'm using the join function from clojure.set. join with two parameters is supposed to do a natural join. Load it up: user=> (use 'clojure.set) nil This makes sense -- if either side of a join has 0 rows, so should the result: user=> (join [{:a 1…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
3
votes
3 answers

Converting aggregate operators from SQL to relational algebra

I have several SQL queries written that I want to convert to relational algebra. However, some of the queries use aggregate operators and I don't know how to convert them. Notably they use COUNT and GROUP BY.. HAVING operators. Here is the…
smcg
  • 3,195
  • 2
  • 30
  • 40
3
votes
1 answer

How to determine candidate keys from record schema and functional dependencies?

If I have a schema say R={A,B,C,D} and functional dependencies say {B->C, D->A} will my set of candidate keys be {B,D} or {BD}?
Sean
  • 2,018
  • 4
  • 25
  • 32