Lecture 5-6
Lecture 5-6
Marina Gul
Sukkur IBA University Khairpur Campus
2 Agenda
Structure of Relational database
Relational Keys
Integrity Constraints
Relational Query Languages
The Relational Algebra
3 Structure of Relational database
Attributes
Relation: a relation is a table with columns and rows. (column headers) Tuples
(rows)
Attribute: an attribute is a named column of a
relation.
Domain: a domain is a set of allowable values for one Name Address
or more attribute.
Tuple: A tuple is a row of a relation. John Washington
Degree: The degree of a relation is the number of
attribute it contain Ella Boston
Cardinality: The cardinality of a relation is the Customers
number of tuples it contain
Relation Instance
Relational database: a collection of normalized
relations with distinct relation names. Relation
Relation schema: A named relation defined by a set name
of attribute and domain name pairs.
Relational database schema: A set of relation
schemas, each with a distinct name.
4 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.
5 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.
6 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
7 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.
8 Select Operation p(R)
The select operation selects tuples that satisfy a given predicate.
Notation: p(R):
the lowercase Greek letter sigma σ to denote selection.
p is called the selection predicate or condition
R is the relation (table)
Relation r
σsalary>85000
(instructor )
1
1
Project Operation A1,A2,AK(R)
Notation: A1,A2,AK(R)
Relation r
the uppercase Greek letter pi to donate projection
Notation: r 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) rs
1
4
Example
Find the set of all courses taught in the
Fall 2009 semester, the Spring 2010
semester, or both.
1
5
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).
1
6
Suggested Reading
Chapter 5 of Database Systems: A Practical Approach to Design,
Implementation, and Management by Thomas Conolly 6th Edition
Chapter 2 and 6 of Database System Concepts by Abraham
Silberschatz