37
Relational Model & Relational Algebra

Relational Model & Relational Algebra

  • Upload
    jola

  • View
    75

  • Download
    0

Embed Size (px)

DESCRIPTION

Relational Model & Relational Algebra. Relational Model. Terminology of relational model. How tables are used to represent data. Connection between mathematical relations and relations in the relational model. Properties of database relations. - PowerPoint PPT Presentation

Citation preview

Page 1: Relational Model & Relational Algebra

Relational Model & Relational Algebra

Page 2: Relational Model & Relational Algebra

2

Relational Model

Terminology of relational model. How tables are used to represent data. Connection between mathematical relations

and relations in the relational model. Properties of database relations. How to identify candidate, primary, and

foreign keys. Meaning of entity integrity and referential

integrity.

Page 3: Relational Model & Relational Algebra

3

Relational Model Terminology

A relation is a table with columns and rows.– Only applies to logical structure of the

database, not the physical structure.

Attribute is a named column of a relation.

Domain is the set of allowable values for one or more attributes.

Page 4: Relational Model & Relational Algebra

4

Relational Model Terminology

Tuple is a row of a relation.

Degree is the number of attributes in a relation.

Cardinality is the number of tuples in a relation.

Relational Database is a collection of normalized relations with distinct relation names.

Page 5: Relational Model & Relational Algebra

5

Instances of Branch and Staff (part) Relations

Page 6: Relational Model & Relational Algebra

6

Examples of Attribute Domains

Page 7: Relational Model & Relational Algebra

7

Alternative Terminology for Relational Model

Page 8: Relational Model & Relational Algebra

8

Database Relations Relation schema– Named relation defined by a set of attribute

and domain name pairs.

Relational database schema– Set of relation schemas, each with a distinct

name.

Page 9: Relational Model & Relational Algebra

9

Properties of Relations Relation name is distinct from all other relation names

in relational schema.

Each cell of relation contains exactly one atomic (single) value.

Each attribute has a distinct name.

Values of an attribute are all from the same domain.

Page 10: Relational Model & Relational Algebra

10

Properties of Relations

Each tuple is distinct; there are no duplicate tuples.

Order of attributes has no significance.

Order of tuples has no significance, theoretically.

Page 11: Relational Model & Relational Algebra

11

Relational Keys Superkey– An attribute, or a set of attributes, that uniquely identifies

a tuple within a relation.

Candidate Key– Superkey (K) such that no proper subset is a superkey

within the relation. – In each tuple of R, values of K uniquely identify that tuple

(uniqueness).– No proper subset of K has the uniqueness property

(irreducibility).

Page 12: Relational Model & Relational Algebra

12

Relational Keys Primary Key– Candidate key selected to identify tuples uniquely

within relation.

Alternate Keys– Candidate keys that are not selected to be primary

key.

Foreign Key– Attribute, or set of attributes, within one relation

that matches candidate key of some (possibly same) relation.

Page 13: Relational Model & Relational Algebra

13

Relational Integrity

Null– Represents value for an attribute that is

currently unknown or not applicable for tuple.– Deals with incomplete or exceptional data.– Represents the absence of a value and is not the

same as zero or spaces, which are values.

Page 14: Relational Model & Relational Algebra

14

Relational Integrity Entity Integrity– In a base relation, no attribute of a primary key

can be null. Referential Integrity– If foreign key exists in a relation, either foreign

key value must match a candidate key value of some tuple in its home relation or foreign key value must be wholly null.

Enterprise Constraints– Additional rules specified by users or database

administrators.

Page 15: Relational Model & Relational Algebra

15

Relational Algebra

Meaning of the term relational completeness.

How to form queries in relational algebra.

Page 16: Relational Model & Relational Algebra

16

Introduction

Relational algebra is a formal language associated with the relational model.

Informally, relational algebra is a (high-level) procedural language

Page 17: Relational Model & Relational Algebra

17

Relational Algebra

Five basic operations in relational algebra: Selection, Projection, Cartesian product, Union, and Set Difference.

These perform most of the data retrieval operations needed.

Also have Join, Intersection which can be expressed in terms of 5 basic operations.

Page 18: Relational Model & Relational Algebra

18

Relational Algebra Operations

Page 19: Relational Model & Relational Algebra

19

Relational Algebra Operations

Page 20: Relational Model & Relational Algebra

20

Selection (or Restriction)

predicate (R)– Works on a single relation R and defines a

relation that contains only those tuples (rows) of R that satisfy the specified condition (predicate).

Page 21: Relational Model & Relational Algebra

21

Example - Selection (or Restriction)

List all staff with a salary greater than £10,000.

salary > 10000 (Staff)

Page 22: Relational Model & Relational Algebra

22

Projection

col1, . . . , coln(R)– Works on a single relation R and defines a

relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates.

Page 23: Relational Model & Relational Algebra

23

Example - Projection

Produce a list of salaries for all staff, showing only staffNo, fName, lName, and salary details.

staffNo, fName, lName, salary(Staff)

Page 24: Relational Model & Relational Algebra

24

Union R S– 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.

If R and S have I and J tuples, respectively, union is obtained by concatenating them into one relation with a maximum of (I + J) tuples.

Page 25: Relational Model & Relational Algebra

25

Example - Union

List all cities where there is either a branch office or a property for rent.

city(Branch) city(PropertyForRent)

Page 26: Relational Model & Relational Algebra

26

Set Difference

R – S– Defines a relation consisting of the tuples that

are in relation R, but not in S. – R and S must be union-compatible.

Page 27: Relational Model & Relational Algebra

27

Example - Set Difference

List all cities where there is a branch office but no properties for rent.

city(Branch) – city(PropertyForRent)

Page 28: Relational Model & Relational Algebra

28

Intersection

R S– Defines a relation consisting of the set of all

tuples that are in both R and S. – R and S must be union-compatible.

Expressed using basic operations:R S = R – (R – S)

Page 29: Relational Model & Relational Algebra

29

Example - Intersection

List all cities where there is both a branch office and at least one property for rent.

city(Branch) city(PropertyForRent)

Page 30: Relational Model & Relational Algebra

30

Cartesian product

R X S– Defines a relation that is the concatenation of

every tuple of relation R with every tuple of relation S.

Page 31: Relational Model & Relational Algebra

31

Example - Cartesian product List the names and comments of all clients who have

viewed a property for rent.(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment

(Viewing))

Page 32: Relational Model & Relational Algebra

32

Example - Cartesian product and Selection Use selection operation to extract those tuples where

Client.clientNo = Viewing.clientNo.Client.clientNo = Viewing.clientNo((clientNo, fName, lName(Client)) (clientNo,

propertyNo, comment(Viewing)))

Cartesian product and Selection can be reduced to a single operation called a Join.

Page 33: Relational Model & Relational Algebra

33

Join Operations Join is a derivative of Cartesian product.

Equivalent to performing a Selection, using join predicate as selection formula, over Cartesian product of the two operand relations.

One of the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMSs have intrinsic performance problems.

Page 34: Relational Model & Relational Algebra

34

Join Operations

Various forms of join operation– Theta join– Equijoin (a particular type of Theta join)– Natural join– Outer join– Semijoin

Page 35: Relational Model & Relational Algebra

35

Example - Equijoin

List the names and comments of all clients who have viewed a property for rent.

(clientNo, fName, lName(Client)) Client.clientNo = Viewing.clientNo

(clientNo, propertyNo, comment(Viewing))

Page 36: Relational Model & Relational Algebra

36

Natural join

R S– An Equijoin of the two relations R and S over all

common attributes x. One occurrence of each common attribute is eliminated from the result.

Page 37: Relational Model & Relational Algebra

37

Example - Natural join

List the names and comments of all clients who have viewed a property for rent.(clientNo, fName, lName(Client))

(clientNo, propertyNo, comment(Viewing))