Introduction to Relational Model

Chapter 2: Relational Model
Information Technology Department
Bryar Hassan (MSc Eng.)
[email protected]
Structure of Relational Database
•
•
•
•
•
A relational database consists of a collection of tables
Relation: it is used to refer to a table in relational model
Tuple: it is used to refer to a row
Attribute: it is used to a column of a table
Relation Instance: it refers to a specific instance of a relation. e.g.,
containing a specific set of rows
Database System Concepts
1
Example of Relation
Database System Concepts
2
Attribute Types
• The set of allowed values for each attribute is called the domain of
the attribute
• Attribute values are (normally) required to be atomic; that is,
indivisible
• The special value null is a member of every domain
• The null value causes complications in the definition of many
operations
Database System Concepts
3
Keys
• We must have a way to specify how tuples within a given relation
are distinguished.
• This is expressed in terms of their attributes.
– The values of the attribute values of a tuple must be such that they can
uniquely identify the tuple.
– No two tuples in a relation are allowed to have exactly the same
value for all attributes.
• A key (whether primary, candidate, or super) is a property of the
entire relation, rather than of the individual tuples
• Super key: is a set of one or more attributes that, taken collectively,
allow us to identify uniquely a tuple in the relation.
• Candidate key: If K is a superkey, then so is any superset of K. We
are often interested in superkeys for which no proper subset is a
superkey. Such minimal superkeys are called candidate keys
Database System Concepts
4
Keys
• Primary key: is a candidate key that is chosen by the database
designer as the principal means of identifying tuples within a
relation
• Foreign key: A relation, say r1, may include among its attributes
the primary key of another relation, say r2. This attribute is called a
foreign key from r1, referencing r2.
– The relation r1 is also called the referencing relation of the foreign key
dependency, and r2 is called the referenced relation of the foreign key
Database System Concepts
5
Primary Key Versus Foreign Key
• A table can only have one primary key. A primary key is a field or
set of fields that:
– Has a unique value for each record
– Is indexed
– Identifies the record
• You can add the primary key from one table to another table to
create a relationship between them. In the other table, it is called a
foreign key.
Database System Concepts
6
Primary Key Versus Foreign Key
• A foreign key, simply stated, is another table's primary key. The
values in a foreign key field match values in the primary key,
indicating that the two records are related
• For example, a customer and an order that she has placed. Unlike
primary keys:
– A table can have more than one foreign key.
– A foreign key does not necessarily have unique values.
– A foreign key cannot reliably identify a particular record. For example, you
cannot always tell which record you are viewing from the Orders table by
looking at the Customer ID.
Database System Concepts
7
Example
• Suppose you use the primary key of the Customers table in the
Orders table. In the Orders table it is a foreign key.
1. Primary key
2. Foreign key
Database System Concepts
8
Database Schema
• Database schema: it is the logical design of the database
• Example of the schema diagram for University database
Database System Concepts
9
Relational Algebra
• The relational algebra defines a set of operations on relations,
paralleling the usual algebraic operations such as addition,
subtraction or multiplication, which operate on numbers.
•
Just as algebraic operations on numbers take one or more numbers as input and
return a number as output, the relational algebra operations typically take one or
two relations as input and return a relation as output.
Database System Concepts
10
Selection of Tuples
Database System Concepts
11
Selection of Columns (Attributes)
Database System Concepts
12
Joining Two Relations – Cartesian Product
Database System Concepts
13
Union of Two Relations
Database System Concepts
14
Set Different of Two Relations
Database System Concepts
15
Set Intersection of Two Relations
Database System Concepts
16
Joining Two Relations – Natural Join
• Let r and s be relations on schemas R and S
respectively.
Then, the “natural join” of relations R and S is a relation
on schema R  S obtained as follows:
– Consider each pair of tuples tr from r and ts from s.
– If tr and ts have the same value on each of the
attributes in R  S, add a tuple t to the result, where
• t has the same value as tr on r
• t has the same value as ts on s
Database System Concepts
17
Natural Join Example
Database System Concepts
18
Summary of Relational Algebra
Database System Concepts
19
Summary
•
•
•
•
•
•
Structure of Relational Database
Attribute Types
Keys
Primary Key Versus Foreign Key
Database Schema
Relational Aljebra
Database System Concepts
20