Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.

Similar presentations


Presentation on theme: "CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database."— Presentation transcript:

1 CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database

2 CIS552Relational Model2 Basic Structure Given sets A 1, A 2, …, A n a relation r is a subset of A 1  A 2  …  A n Thus a relation is a set of n-tuples (a 1, a 2, …, a n ) where a i  A i Example: If customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city = { Harrison, Rye, Pittsfield} Then r = {(Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} is a relation over customer-name  customer-street  customer-city

3 CIS552Relational Model3 Relation Schema A 1, A 2, …, A n are attributes R = (A 1, A 2, …, A n ) is a relation schema Customer-schema = (customer-name, customer-street, customer-city) r(R) is a relation on the relation schema R customer (Customer-schema)

4 CIS552Relational Model4 Relation Instance The current values (relation instance) of a relation are specified by a table. An element t of r is a tuple; represented by a row in a table. customer

5 CIS552Relational Model5 Keys Let K  R K is a superkey of R if values for K are sufficient to uniquely identify a tuple of each possible relation r(R). By “possible r” we mean a relation r that could exist in the enterprise we are modeling. Example: {customer-name, customer-street} and {customer-name} are both superkey of customer, if no two customers can possibly have the same name. K is a candidate key if K is minimal Example: {customer-name} is a candidate key for Customer, since it is a superkey (assuming no two customers can possibly have the same name), and no subset of it is a superkey

6 CIS552Relational Model6 Determining Keys from E-R Sets Strong entity set. The primary key of the entity set becomes the primary key of the relation. Weak entity set. The primary key of the relation consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set. Relationship set. The union of the primary keys of the related entity sets becomes a super key of the relation. For binary many-to-many relationship sets, above super key is also the primary key. For binary many-to-one relationship sets, the primary key of the “many” entity set becomes the relation’s primary key. For one-to-one relationship sets, the relation’s primary key can be that of either entity set.

7 CIS552Relational Model7 Query Languages Language in which user requests information form the database. Categories of languages: –Procedural –Non-procedural Formal languages: –Relational Algebra –Tuple Relational Calculus –Domain Relational Calculus Formal languages form underlying basis of query languages that people use.

8 CIS552Relational Model8 Relational Algebra Procedural language Six basic operators –select –project –union –set difference –cartesian product –rename The operators take two or more relations as inputs and give a new relation as a result.

9 CIS552Relational Model9 Select Operation Notation  p (r) Defined as:  p (r) = {t | t  r and p(t)} where P is a formula in propositional calculus, dealing with terms of the form: attribute  attribute or constant      connected by:  (and),  (or),  (not)

10 CIS552Relational Model10 Select Operation - Example Relation r:  A=B  D > 5 (r)

11 CIS552Relational Model11 Project Operation Notation:  A 1, A 2, …, A k (r) where A 1, A 2 are attributes and r is a relation. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows are removed from result, since relations are sets

12 CIS552Relational Model12 Project Operation - Example Relation r:  A, C (r) =

13 CIS552Relational Model13 Union Operation Notation: r  s Defined as: r  s = { t | t  r or t  s} For r  s to be valid, 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (e.g. 2nd column of r deals with the same type of values as does the 2nd column of s)

14 CIS552Relational Model14 Union Operation - Example Relations r, s: r  s

15 CIS552Relational Model15 Set Difference Operation Notation: r - s Defined as: r - s = { t | t  r and t  s} Set differences must be taken between compatible relations. –r and s must have the same arity –attribute domains of r and s must be compatible

16 CIS552Relational Model16 Set Difference Operation - Example

17 CIS552Relational Model17 Cartesian-Product Operation Notation: r  s Defined as: r  s = { tq | t  r and q  s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R  S =  ). If attributes of r(R) and s(S) are not disjoint, then renaming must be used.

18 CIS552Relational Model18 Cartesian-Product Operation - Example

19 CIS552Relational Model19 Banking Tables

20 CIS552Relational Model20 Customer  Borrower

21 CIS552Relational Model21 Customer  Borrower

22 CIS552Relational Model22 Composition of Operations Can build expressions using multiple operations Example:  A= C (r  s) Natural Join –Notation: r s –Let r and s be relations on schemas R and S respectively. The result is a relation on schema R  S which is obtained by considering each pair of tuples t r from r and t s from s. –If t r and t s have the same value on each of the attributes in R  S, a tuple t is added to the result, where t has the same values as t r on the attributes in R t has the same values as t s on the attributes in S

23 CIS552Relational Model23 Composition of Operations (Cont.) Example: R = (A, B, C, D) S = (E, B, D) Result schema = (A, B, C, D, E) r s is defined as:  r.A,r.B,r.C,r.D,s.E (  r.B = s.B  r.D = s.D ( r  s ) )

24 CIS552Relational Model24 Natural Join Operation - Example

25 CIS552Relational Model25 Division Operation r  s Suited to queries that include the phrase “for all”. Let r and s be relations on schemas R and S respectively, where: R = (A 1, …, A m, B 1, …, B n ) S = (B 1, …, B n ) The result of r  s is a relation on schema: R - S = (A 1, …, A m ) r  s = { t | t  R-S (r)   u  s (tu  r) }

26 CIS552Relational Model26 Division Operation (Cont.) Property  Let q = r  s  Then q is the largest relation satisfying: q  s  r Definition in terms of the basic algebra operation Let r(R) and s(S) be relations, and let S  R r  s =  R-S (r)   R-S ( (  R-S (r)  s)   R-S,S (r) ) To see why:   R-S,S (r) simply reorders attributes of r   R-S ((  R-S (r)  s)   R-S,S (r)) gives those tuples t in  R-S (r) such that for some tuple u  s, tu  r.

27 CIS552Relational Model27 Division Operation - Example = 

28 CIS552Relational Model28 Another Division Example = 

29 CIS552Relational Model29 Assignment Operation The assignment operation (  ) provides a convenient way to express complex queries; write query as a sequential program consisting of a series of assignments followed by an expression whose value is displayed as the result of the query. Assignment must always be made to a temporary relation variable. Example: Write r  s as temp1   R-S (r) temp2   R-S ((temp1  s)   R-S,S (r)) result = temp1  temp2 –The result to the right of the  is assigned to the relation variable on the left of the . –May use variable in subsequent expressions

30 CIS552Relational Model30 Example Queries Find those customers who have an account from at least the “Downtown” and “Updown” branches. Query 1  cn (  bn = “Downtown” ( depositor account ))   cn (  bn = “Uptown” ( depositor account )) where cn denotes customer-name and bn denotes branch-name Query 2  cn, bn (depositor account ))   temp(bn) ( {(“Downtown”), (“Uptown”)} )

31 CIS552Relational Model31 Example Queries (2) Find those customers who have accounts at all branches located in Brooklyn.  customer-name, branch-name (depositor account ))   branch-name (  branch-city = “Brooklyn” ( branch ))

32 CIS552Relational Model32 Sample Problems Consider the following relational schema: Flights (flno, from, to, distance, departs) Aircraft (aid, aname, range) Certified (eid, aid) Employees (eid, ename, salary) By definition, pilots are those employees who are certified on at least one aircraft. An aircraft can be used for any flight provided it has sufficient range. Pilots can pilot any flight provided they are certified on an aircraft with sufficient range.

33 CIS552Relational Model33 Sample Problems (cont.) 1. Find eid’s of pilots who are certified on some Boeing. 2. Find names of pilots who are certified on some Boeing. 3. Find aid’s of aircraft that can fly non-stop from LA to NY. 4. Find flno of flights that can be piloted by every pilot whose salary is over $100,000. 5. Solve problem 4 without using the division operator. 6. Find names of pilots who can operate planes with a range greater than 3,000 miles, but are not certified on any Boeing. 7. Find eid of employee(s) with the highest salary. 8. Find eid of employee(s) with the second highest salary. 9. Find eid of employee(s) certified on the most aircraft. 10. Find eid’s of employees certified on exactly three aircraft.


Download ppt "CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database."

Similar presentations


Ads by Google