0% found this document useful (0 votes)
19 views16 pages

Lecture 5-6

The document provides an introduction to the relational model, covering the structure of relational databases, keys, integrity constraints, and query languages. It explains key concepts such as relations, attributes, and various types of keys, as well as operations in relational algebra like selection and projection. Additionally, it includes practice exercises and suggested readings for further study.

Uploaded by

eemanf240
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views16 pages

Lecture 5-6

The document provides an introduction to the relational model, covering the structure of relational databases, keys, integrity constraints, and query languages. It explains key concepts such as relations, attributes, and various types of keys, as well as operations in relational algebra like selection and projection. Additionally, it includes practice exercises and suggested readings for further study.

Uploaded by

eemanf240
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture 5-6

Introduction to relational Model

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)

Example: List those tuples of the instructor relation


where the instructor is in the “Physics” department.

σdept name =“Physics” (instructor )


9 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

Example: Find the instructors in Physics with a salary greater


than $90,000.
σdept_name=“Physics”∧ salary>90000 (instructor )
A=B ^ D > 5 (r)
1
0
Practice

List employees having salary greater than


$85000

σsalary>85000
(instructor )
1
1
Project Operation A1,A2,AK(R)

The project operation used to extract the values of


specified attributes.

Notation: A1,A2,AK(R)
Relation r
the uppercase Greek letter pi  to donate projection

A1, A2, ….,A k are attribute names and R is a relation name.

Duplicate rows removed from result, since relations are


sets
A,C (r)
1
2
Practice
List all instructors’ ID, name, and
salary, from the instructor relation
1
3
Union Operation r  s

The union of two relations r and S defines a relation that


contains all the tuples of r, or S, or both r and S, duplicate
tuples being eliminated. r and S must be union-compatible. Relations r, s

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) rs
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

You might also like