We use set semantics in Relational Algebra :(
Operators
Projection () -
Set Theoretic Operators
We can use set-theoretic operations (say for relations , ) since we use set semantics in RA however they require type-compatibility:
- Same number of attributes in each relation:
- The relations are type-compatible:
Cross Joins
Recall that a relation is a set of tuples in the relational model.
A cross join of two relations is notated and is just the cartesian product from CS130!
As a reminder, .
The difference is that instead of putting elements in a pair (2-tuple), the elements are concatenated together. The rows from themselves are tuples, and you add the tuples together like so:
Example (for intuition)
More
More formally, for relations and we have:
.
We know that the elements in the resulting relation are formed from adding two relations; one of size from , and one of size from . Therefore, there are elements in the resulting relation. We can describe it like so:
.
DROP TABLE IF EXISTS Employees; -- ignore this; just so I can rerun it lmao
CREATE TABLE Employees (
EmployeeID Int PRIMARY KEY,
Dept varchar(1)
);
INSERT INTO Employees VALUES (1, 'S');
INSERT INTO Employees VALUES (2, 'Q');
INSERT INTO Employees VALUES (3, 'S');
DROP TABLE IF EXISTS Departments;
CREATE TABLE Departments (
Dept varchar(1) PRIMARY KEY,
DeptName varchar(10)
);
INSERT INTO Departments VALUES ('S', 'Software');
INSERT INTO Departments VALUES ('Q', 'Quant');
SELECT * FROM (Departments CROSS JOIN Employees) WHERE Departments.Dept = Employees.Dept ORDER BY EmployeeID ASC;