University Study Notes on the Relational Model and Relational Algebra
Fundamental Structure of Relational Databases
Attributes and Columns
In the relational model, a database is a collection of relations (tables).
Attributes represent the columns of the table.
Example attributes for an
instructorrelation include:IDnamedept_namesalary
Tuples and Rows
Tuples are the rows in a table.
A relation is defined as an unordered set of tuples that share the same type.
Example data from an
instructorrelation instance:
Relation Schema and Database Architecture
Relation Schema
Let be attributes.
A relation schema is defined as .
Example: .
Relation Instance
A relation instance defined over schema is denoted by .
The current values of a relation are specified in a table format.
An element of relation is a tuple (row).
Database Schema vs. Instance
Database Schema: The logical structure of the database.
Database Instance: A snapshot of the data in the database at a specific instant in time.
Physical vs. Logical Schemas
Physical Schemas: Describe the database design at the physical level, representing how data are actually stored on media.
Logical Schemas: Describe the design at the logical level, specifying what data are stored and the relationships between them.
Attributes and Properties of Relations
Attribute Domains
The domain is the set of allowed values for each attribute.
Attribute values are required to be atomic (indivisible).
The special value null is a member of every domain, indicating that a value is "unknown."
The use of null values complicates the definition of operations.
Unordered Nature
Relations are inherently unordered; the storage order of tuples is irrelevant.
Keys in Relational Databases
Superkeys
Let .
is a superkey if the values for are sufficient to identify a unique tuple in every possible relation .
Example: For the
instructorrelation, both and are superkeys.
Candidate Keys
A superkey is a candidate key if it is minimal (no subset of is also a superkey).
Example: is a candidate key for
instructor.
Primary Keys
One candidate key is selected as the primary key to uniquely identify tuples.
Foreign Keys
A foreign key is a primary key from another table.
The referencing relation contains the foreign key, which points to the referenced relation.
Example:
dept_namein theinstructortable is a foreign key referencing thedepartmenttable (wheredept_nameis the primary key).Foreign Key Constraint: A value in the referencing relation must appear in the referenced relation.
University Database Examples
Classroom Relation
Attributes:
building,room_number,capacity.Data examples: , , , , .
Note: Multiple buildings may have identical room numbers.
Department Relation
Attributes:
dept_name,building,budget.Data examples: , , , , , , .
Section Relation
Attributes:
course_id,sec_id,semester,year,building,room_number,time_slot_id.
Takes Relation
Attributes:
ID,course_id,sec_id,semester,year,grade.Data examples: , .
Relational Query Languages
Classification
Procedural: User specifies how to compute the result (e.g., Java, C++).
Non-procedural / Declarative: User specifies what data is needed without describing how to get it (e.g., SQL).
Non-procedural languages simplify query specification and allow query optimizers to determine the most efficient evaluation path.
Procedural languages are more powerful for complex computations that are difficult or impossible to express non-procedurally.
Pure Languages
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
These three are equivalent in computing power but are not Turing-machine equivalent.
Relational Algebra: Basic Operations
Relational algebra consists of six fundamental operations used to manipulate relations.
Search/Find Operation (Selection)
Notation: , where is the search predicate.
Purpose: Returns tuples that satisfy predicate .
Supported Comparisons: , , , , , .
Logical Connectives: (and), (or), (not).
Example 1: Find instructors in the Physics department: .
Example 2: Find instructors in Physics with salary > 90,000: .
Example 3: Find departments where the name is the same as the building name: .
Project Operation
Notation: .
Purpose: A unary operation that returns the argument relation with specific attributes excluded.
Behavior: Columns not listed are erased. Duplicate rows are removed because relations are treated as sets.
Example: Eliminate
dept_namefrom instructor: .
Composition of Operations
The result of any relational-algebra operation is also a relation.
Operations can be nested. Example (Find names of instructors in Physics):
Cartesian-Product Operation
Notation: .
Purpose: Combines information from two relations by constructing a tuple for every possible pair of tuples from the two relations.
Naming: If an attribute name exists in both relations (e.g.,
ID), they are prefixed with the original relation name: , .
Join Operation
Notation: .
Definition: Combines Cartesian product and Selection: .
Purpose: Filters the Cartesian product to show only relevant associated data (e.g., matching IDs).
Example: is equivalent to .
Union Operation
Notation: .
Requirements for validity:
Relations and must have the same arity (number of attributes).
Attribute domains must be compatible (e.g., types match column-by-column).
Example: Find courses taught in Fall 2017 OR Spring 2018:
Set-Intersection Operation
Notation: .
Purpose: Finds tuples present in both relations.
Requirements: Compatibility (same arity and compatible domains).
Example: Courses taught in both Fall 2017 AND Spring 2018:
Set-Difference Operation
Notation: .
Purpose: Finds tuples in the first relation that are not in the second.
Requirements: Compatibility (same arity and compatible domains).
Example: Courses taught in Fall 2017 but NOT in Spring 2018:
Assignment and Rename Operations
Assignment Operation
Notation: .
Purpose: Stores parts of a relational-algebra expression in temporary variables, similar to programming.
Example:
Rename Operation
Notation: .
Purpose: Assigns a name to the result of an expression or specific attributes.
Expression forms:
: Returns expression under the name .
: Renames the result to and the attributes to .
Query Equivalency
A query can often be written in multiple ways in relational algebra. Equivalent queries produce identical results on any database instance.
Example 1: Selection order
Query 1:
Query 2:
Example 2: Selection with Join
Query 1:
Query 2: