+ All Categories
Home > Documents > The Relational Model The relational model refers to a class of data models that have relations as...

The Relational Model The relational model refers to a class of data models that have relations as...

Date post: 12-Jan-2016
Category:
Upload: zoe-chandler
View: 219 times
Download: 0 times
Share this document with a friend
28
The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query capabilities (relational algebra, relational calculus and logic programming), updating facilities, and integrity constraints (functional dependencies). Definition . Let D i be an enumerable infinite sequence of domain sets, and <a 1 …a n > be an n-tuple with objects a i such that a 1 D 1 , …, a n D n . A relation R is a subset of the cartesian product D 1 x D 2 x … x D n Database Systems
Transcript
Page 1: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

The Relational ModelThe relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query capabilities (relational algebra, relational calculus and logic programming), updating facilities, and integrity constraints (functional dependencies).

Definition. Let Di be an enumerable infinite sequence of domain sets, and <a1…an> be an n-tuple with objects ai such that a1D1, …, anDn. A relation R is a subset of the cartesian product D1 x D2 x … x Dn

Database Systems

Page 2: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

NormalizationFirst Normal Form (1NF). We say that a relation is in first formal form if all its attributes are atomic.

Second Normal Form (2NF). A relation is in second normal form if and only if it is in 1NF and any non-key attribute in the relation functionally depends on the whole of the key.

Third Normal Form (3NF). A relation is in third normal form if any non-key attribute in the relation does not functionally depend in a transitive way on part of the key.

Relational Databases

Page 3: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Problem: You are in charge to develop a database system to keep record of all surgeries in a hospital, take the following report and applying normalization up to 3NF show which entities are part of the hospital system and which attributes they contain.

Surgery-record =+ Patient-number + Room_number + Bed_number + Surgeon-number+ Patient-name + Patient-allergies+ Surgeon-name+ Surgery-Date+ Surgery-Time+ Surgery-type+ {medicine-number + medicine-name + prescription + secondary-effects}+ Patient-age

Relational Databases

Page 4: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

General concepts The relational model [Codd, 1970], marked the transformation of

the database field from an empirical discipline into a scientific one. A relation constitutes a simple abstraction of the concept of

a flat file, independent of physical structures and access methods.

This simplicity made possible the definition of data manipulation languages whose conciseness contrasts with the complexity of programs accessing databases of previous generations.

On the theoretical side, the relational model allowed the possibility of developing a mathematical and logical basis for a relational theory.

Relational Model

Page 5: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

General concepts (cont.) A relation may be viewed as a logical predicate. Many relational data manipulation languages relate to

an applied predicate calculus, that is, a predicate calculus whose interpretations are tied to the database.

Relational Model

Page 6: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Formal definitionThere are different ways to formalize the relational model

depending, for example: - on the degree of formalization of the concepts.- the importance of the role given to the domains.- the specific constraints considered or not, in the basic

definition. A domain is a finite (non-empty) set of atomic values. A relation is defined by a schema and a value. A value or extension of a relation is a subset of the

Cartesian product of domains, that is, a set of n-tuples <c1, … , cn> such that ci Di, where Di’s are domains (not necessary distinct).

Relational Model

Page 7: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Relational Model The arguments in the Cartesian product are distinguished

through indices i (1 i n) called attributes of the relation. Normally the arguments of a relation are referred by its name

and not by its position. In this presentation both representations are used interchangeably.

The relation schema describes the name of the relation and a set of attribute/domain pairs. E.g. father(Father/Dman, Child/Dhuman)

mother(Mother/Dwoman, Child/ Dhuman)

spouses(Husband/ Dman, Wife/Dwoman, #Children/Dten)Where: Dman ={ John, Larry, …},

Dwoman ={ Mary, Sally, Nelly, …}

Dhuman = Dman Dwoman

Dtenn ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Page 8: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

The extension of the database comprises de value of relations.

The database schema comprises the domains, the relation schemas and the integrity constraints.

An integrity constraint expresses a condition on the extension of the database. E.g. “if two individuals are, respectively, the father and the

mother of another individual, then both parents appear together in a tuple of the spouses relation.”

Several data manipulation languages (DML) have been associated with the relational model.

Queries in a DML specify an answer relation in terms of other relations, or they request the verification of a condition.

Relational Model

Page 9: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Assumptions Unique-name assumption (UNA). Two distinct constants

in the database necessarily designate two different objects.

Domain-closure assumption (DCA). There are no other objects in the universe than those designated by constants of the database.

Closed-word assumption (CWA). The database is complete in the sense that it contains all the positive information.

Relational Model

Page 10: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Query LanguagesA query language is a language in which a user requests

information from the database.

There are two kinds of query languages:(i) Procedural. The user instructs the system to perform a

sequence of operations on the database to compute the desired result. E.g. java, c.

(ii) Declarative. In a declarative language the user describes the desired information without giving a specific procedure for obtaining that information. E.g. mathematical logic.

Some well-known commercial languages are: SQL, QBE and Datalog.

Relational Model

Page 11: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

‘Pure’ query languages

The relational model has the following query languages: The relational Algebra. The tuple relational calculus. The domain relational calculus.

All three languages have approximately the same expressive power.

Relational Model

Page 12: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

1. Relational Algebra- The relational algebra is a procedural query language.- It consists of a set of operations on relations. All operations

are on relations, take one or two relations as arguments and produce a new relation.

- Fundamental operations: selection, projection, union, set difference, cartesian product and rename.

- Additional operations: set intersection, natural join, division and assignment (This operations are defined in terms of the fundamental operations).

Relational Model

Page 13: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Projection ()For a relation r with attributes A, B, the projection of r on atribute A is: A(r(A, B)) = { a | <a,b> r, for some b}

Selection()The following expressions are selections in relation r(A1, …, An),

AiAj(r(A1, …, An)) = {<a1,…,an> | ai aj }

Aic(r(A1, …, An)) = {<a1,…,an> | ai c }

where: c is a constant, and

::= < | > | =| <= | >=

Composition of functionsSince all operations are closed given a new relation as a result, we may use operations as arguments for another operation.

Relational Algebra

Page 14: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Union operation ()The union operation allows the union of two sets of tuples. Format: <relation-1> <relation-2>In general, we must ensure that unions are taken between compatible relations.

Difference operation (-)The difference operation allows us to find tuples which are in one relation but which are not in the second one. Format: <relation-1> - <relation-2>

Relational Model

Page 15: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Cartesian-product operation (x)The Cartesian-product operation allows you to combine information from any two relations.Format <relation-1> X <relation-2>

Assuming that <relation-1> has n1 tuples, and <relation-2> has n2 tuples, then the new relation will have n1 * n2 tuples. If we have relations r1(R1) and r2(R2), then r1xr2 is a relation whose schema is the concatenation fo R1 and R2. E.g.

“Find the names of all customers who have a loan at the Perryridge branch.”

Relational Model

Page 16: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Rename operation ()The rename operation allows you to assign a new name to a relation or a any relation generated from the relational algebra operation.Format (<new-name>, <algebraic-expression>)

Returns the result of the algebraic-expression under the name <new-name>.We can apply the rename operation to an existing relation to get the same relation under a different name.

Another format: (< new-name>(A1,A2,…,An), <algebraic-expression>)

Returns the result of the algebraic-expression with the <new-name> and a new name Ai for each of its attributes.

Relational Model

Page 17: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Examples:Consider the relational database shown below, where the

primary keys are underlined.

employee(person-name, street, city)works(person-name, company-name, salary)company(company-name, city)manages(person-name, manager-name)

Relational Model

Page 18: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Give an expression in relational algebra for the following queries:a. Find the names of all employees who work for First Bank Corporation.b. Find the names and cities of residence of all employees who work for

First Bank Corporation.c. Find the names, street address, and cities of residence of all employees

who work for First Bank Corporation and earn more than $10,000 per annum.

d. Find the names of all employees in this database who live in the same city as the company for which they work.

e. Find the names of all employees who live in the same city and on the same street as do their managers.

f. Find the names of all employees in this database who do not work for First Bank Corporation.

g. Find the names of all employees who earn more than every employee of Small Bank Corporation.

h. Assume the companies may be located in several cities. Find all the companies located in every city in which Small Bank Corporation is located.

Relational Model

Page 19: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Banking enterprise database

account(account-number, branch-name, balance)branch(branch-name, branch-city, assets)customer(customer-name, customer-street, customer-city)depositor(customer-name, account-number)loan(loan-number, branch-name, amount)borrower(customer-name, loan-number)

Relational Model

Page 20: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Intersection operation ()The intersection operation of r1 and r2, r1 r2, generates a relation containing all the tuples which are in r1 and r2.Format r1 r2 Definition: r1 r2 r1 – (r1 – r2)

Natural-Join operation (|x|): The natural-join is a binary operation that allows to combine certain selections and a Cartesian Product, into one operation. Definition:

r |x| s (R S, (r.A1=s.A1 … r.An=s.An, r x s))

Where: R S = { A1, A2, … , An}

Relational Model

Page 21: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Assignment Operation ()The assignment operation allows us to assign parts of a relational

expression to temporary variables. This operation works as the assignment operation in programming languages.

Format: <variable-name> <relational-algebra expression>

The relation variable may be used in subsequent expressions.

Division Operation ()

The division operation is suited to queries that include the phrase “for all.”

Definition. Let r(R) and s(S) be relations with relational schema R and S respectively, and let S R ( that is, every attribute occurring in S occurs as well in R). A tuple t is in r s if and only if both two conditions hold:

Relational Model

Page 22: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

1. t is in ([R-S],r)

2. For every tuple ts in s, there is a tuple tr r in r satisfying both of the following:

a. tr[S] = ts [S]

b. tr[R-S] = t

Definition in terms of the relational algebra operations:

r s = result

where: temp1 ([R-S], r)

temp2 ([R-S], (temp1 x s) – ([R-S,S],r))

result temp1 – temp2

Relational Model

Page 23: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Extended OperationsGeneralized projectionExtends the projection operation to allow for arithmetic

functions to be used in the projection list.Format

([F1, F2, … , Fn], E)

Where E is any relational-algebra expression, and each of F1,..,Fn is an arithmetic expression involving constants and attributes in the schema of E.

Examples: ([customer-name, (limit - credit-balance) as credit- available, credit-info)

Relational Model

Page 24: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Aggregate functions

Aggregate functions take a collection of values and return an single value as a result. Some of the functions are: sum, avg, count, min, max, and so on.

Format

G( list-of-fields, list-of-aggregate-functions, relation)

e.g.

G([branch-name],[sum(salary) as sum-salary], works)

Outer Join ( |X|)

The outer-join operation is an extension of the join operation to deal with missing information.

Relational Model

Page 25: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Relational Model2. Tuple Relational Language

The tuple relational language (TRL) L is a obtained by adapting the syntax of the first-order predicate calculus to a database schema S in the following manner:

-The individual constants of L are finite in number; the set of constants is the union of the domains of S.

- A tuple variable tn stands for a n-tuple of the form <x1,…, xn> such that t.Ai = xi, where Ai is an attribute name associated with domain Di, and xi Di.

-There are no functions.

-The number of predicates in L is finite.

- Among the predicates in L, there exists a distinguished binary predicate, equality (denoted =).

Page 26: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Relational Model-Every predicate R in L other than equality is associated with a relation schema in S and conversely. Each attribute Ai in the relation schema corresponds with an argument of the associated predicate.

A query in the TRL is expressed as:

{ t | (t) }

Which means the set of tuples t such that (t) is a formula of L.

Language definition

::= sn Rn | s.Ai = r.Aj | s.Ai < r.Aj | s.Ai > r.Aj | s.Ai <= r.Aj

| s.Ai >= r.Aj | & | V | | x. (x) | x. (x)

where Ai and Aj are associated with the same domain.

Page 27: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

Relational Model3. Domain Relational Language

The domain relational language (DRL) L is a obtained by adapting the syntax of the first-order predicate calculus to a database schema S in the following manner:

-The individual constants of L are finite in number; The universe is defined as D1U … U Dn,where D1, …, Dn are all the particular domains.

-There are no functions.

-The number of predicates in L is finite.

-Among the predicates in L, there exists a distinguished binary predicate, equality (denoted =).

-Every predicate in L other than equality is associated with a relation schema in S.

Page 28: The Relational Model The relational model refers to a class of data models that have relations as the data structure, and incorporate some of all the query.

A query in the DRL is expressed as:

{ <x1,…, xn> | (< x1,…, xn>) }

where x1,…, xn are domain variables such that x1 D1,…, xn Dn and

(< x1,…, xn>) is a formula of L.

Language definition

::= <x1,…, xn> Rn | xi = xj | xi < xj | xi > xj | xi <= xj| xi >= xj

| & | V | | xD. (x) | xD. (x)

where xi Di, xj Dj and Di = Dj. That is, xi and xj belong to the same domain.

Relational Model


Recommended