Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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 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

2 Normalization First 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

3 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

4 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

5 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

6 Formal definition There 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 such that c i  D i, where D i ’s are domains (not necessary distinct). Relational Model

7 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/D man, Child/D human ) mother(Mother/D woman, Child/ D human ) spouses(Husband/ D man, Wife/D woman, #Children/D ten ) Where: D man ={ John, Larry, …}, D woman ={ Mary, Sally, Nelly, …} D human = D man  D woman D tenn ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

8 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

9 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

10 Query Languages A 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

11 ‘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

12 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

13 Projection (  ) For a relation r with attributes A, B, the projection of r on atribute A is:  A (r(A, B)) = { a |  r, for some b} Selection(  ) The following expressions are selections in relation r(A 1, …, A n ),  Ai  Aj (r(A 1, …, A n )) = { | a i  a j }  Ai  c (r(A 1, …, A n )) = { | a i  c } where: c is a constant, and  ::= | =| = Composition of functions Since all operations are closed given a new relation as a result, we may use operations as arguments for another operation. Relational Algebra

14 Union operation (  ) The union operation allows the union of two sets of tuples. Format:  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: - Relational Model

15 Cartesian-product operation (x) The Cartesian-product operation allows you to combine information from any two relations. Format X Assuming that has n1 tuples, and 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

16 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  (, ) Returns the result of the algebraic-expression under the name. We can apply the rename operation to an existing relation to get the same relation under a different name. Another format:  ( (A 1,A 2,…,A n ), ) Returns the result of the algebraic-expression with the and a new name A i for each of its attributes. Relational Model

17 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

18 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

19 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

20 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

21 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:  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

22 1. t is in  ([R-S],r) 2. For every tuple t s in s, there is a tuple t r r in r satisfying both of the following: a. t r [S] = t s [S] b. t r [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

23 Extended Operations Generalized projection Extends 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

24 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

25 2. 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 t n stands for a n-tuple of the form such that t.A i = x i, where A i is an attribute name associated with domain D i, and x i  D i. - 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 =).

26 Relational Model - Every predicate R in L other than equality is associated with a relation schema in S and conversely. Each attribute A i 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  ::= s n  R n | s.A i = r.A j | s.A i r.A j | s.A i <= r.A j | s.A i >= r.A j |  &  |  V  |    |  x.  (x) |  x.  (x) where A i and A j are associated with the same domain.

27 Relational Model 3. 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 D 1 U … U D n,where D 1, …, D n 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.

28 A query in the DRL is expressed as: { |  ( ) } where x 1,…, x n are domain variables such that x 1  D 1,…, x n  D n and  ( ) is a formula of L. Language definition  ::=  R n | x i = x j | x i x j | x i = x j |  &  |  V  |    |  x  D.  (x) |  x  D.  (x) where x i  D i, x j  D j and D i = D j. That is, x i and x j belong to the same domain. Relational Model


Download ppt "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."

Similar presentations


Ads by Google