Lecture 4-6 (R.A)
Lecture 4-6 (R.A)
Lecture 4-6 (R.A)
John Washington
Tuples
Ella Boston
(rows)
Relation Instance
Customers
Relation
name
Database Relations
Relation schema: A named relation defined by a set of attribute and domain name
pairs.
Relational database schema: A set of relation schemas, each with a distinct name.
Relational Keys
SuperKey: An attribute, or set of attributes, that uniquely identifies a tuple within a
relation.
Candidate Key: A superkey that no proper subset is a superkey within the relation
Primary Key: The candidate key that is selected to identify tuples uniquely within
the relation.
Foreign Key: An attribute, or set of attributes, within one or more relation that
matches the candidate key of some (possibly the same) relation.
Integrity Constraints
Nulls: Represents a value for an attribute that is currently unknown or is not
applicable for this tuple.
Entity Integrity: In a base relation, no attribute of a primary key can be null.
Domain Integrity: refers to the rules defined for the values that can be stored for a
certain attribute
Referential integrity: If a foreign key exists in a relation, either the foreign key
value must match a candidate key value of some tuple in its home relation or the
foreign key value must be wholly null.
Relational Query Languages
Procedural
Relational algebra
Non-procedural, or declarative
Relational calculus
Tuple relational calculus
Domain relational calculus
Query languages used in practice include elements of both the procedural and the
nonprocedural approaches
Relational Algebra
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The Selection and Projection operations are unary operations, as they operate on one
relation, other operations work on pairs of relations and are therefore called binary
operations
The operators take one or two relations as inputs and produce a new relation as a
result.
Select Operation
Notation: p(R)
p is called the selection predicate or condition
σsalary>85000 (instructor )
Select Operation
In general, we allow comparisons using =, =, <, ≤, >, and ≥ in the selection
predicate. Furthermore, we can combine several predicates into a larger predicate
by using the connectives and (∧), or (∨), and not ( ¬ ).
Relation r
where A1, A2, ….,A k are attribute names and R is a relation name.
A,C (r)
Example
List all instructors’ ID, name, and
salary, from the instructor relation
Union Operation
Notation: r s
Defined as: r s = {t | t r or t s}
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd column of r deals with the
same type of values as does the 2nd column of s)
Example
Relations r, s:
r s:
Example
find the set of all courses taught in the Fall 2009 semester, the Spring 2010 semester,
or both.
Exercise
Write the following queries in relational algebra, using the university schema.
a. Find the titles of courses in the Comp. Sci. department that have 3 credits.
b. Find the IDs of all students who were taught by an instructor named Einstein;make
sure there are no duplicates in the result.
c. Find the highest salary of any instructor.
d. Find all instructors earning the highest salary (there may be more than one with the
same salary).
Suggested Reading
Chapter 5 of Database Systems: A Practical Approach to Design, Implementation,
and Management by Thomas Conolly 6th Edition
Chapter 2 of Database System Concepts by Abraham Silberschatz