1 / 28

Database Systems

Database Systems. The Relational Model

zoe-noble
Download Presentation

Database Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Database Systems 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 Di be an enumerable infinite sequence of domain sets, and <a1…an> be an n-tuple with objects ai such that a1D1, …, anDn. A relation R is a subset of the cartesian product D1 x D2 x … x Dn

  2. Relational Databases • 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.

  3. Relational Databases • 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

  4. Relational Model 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.

  5. Relational Model 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.

  6. Relational Model 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 <c1, … , cn> such that ci Di, where Di’s are domains (not necessary distinct).

  7. 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 relationschema 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}

  8. Relational Model • 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.

  9. Relational Model 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.

  10. Relational Model Query Languages A query language is a language in which a user requests information from the database. There are two kinds of query languages: • Procedural. The user instructs the system to perform a sequence of operations on the database to compute the desired result. E.g. java, c. • 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.

  11. Relational Model ‘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.

  12. Relational Model 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).

  13. Relational Algebra • 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), AiAj(r(A1, …, An)) = {<a1,…,an> | ai aj } Aic(r(A1, …, An)) = {<a1,…,an> | ai 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.

  14. Relational Model • 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>

  15. Relational Model • 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.”

  16. Relational Model • 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.

  17. Relational Model 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)

  18. Relational Model Give an expression in relational algebra for the following queries: • Find the names of all employees who work for First Bank Corporation. • Find the names and cities of residence of all employees who work for First Bank Corporation. • 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. • Find the names of all employees in this database who live in the same city as the company for which they work. • Find the names of all employees who live in the same city and on the same street as do their managers. • Find the names of all employees in this database who do not work for First Bank Corporation. • Find the names of all employees who earn more than every employee of Small Bank Corporation. • Assume the companies may be located in several cities. Find all the companies located in every city in which Small Bank Corporation is located.

  19. Relational Model • 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)

  20. Relational Model • 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}

  21. Relational Model 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:

  22. Relational Model 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

  23. Relational Model 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)

  24. Relational Model 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.

  25. Relational Model 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 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 =).

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

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

  28. Relational Model 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  |    | xD. (x) | xD. (x) where xi  Di,xj  Djand Di = Dj. That is, xi and xj belong to the same domain.

More Related