Download presentation

Presentation is loading. Please wait.

Published byCarol Griffin Modified over 2 years ago

1
YV - Relational Calculus, SQL, QBE 154 Κεφάλαιο 4 Η Γλώσσα SQL και Σχεσιακός Λογισμός

2
YV - Relational Calculus, SQL, QBE 155 Relational Calculus: Introduction The Relational Calculus (RC) is a non-procedural, formal language based on first-order predicate calculus Queries in RC specify WHAT is to be retrieved (declarative) while the system takes care of HOW Most commercial relational languages are based on relational calculus (QUEL, QBE, and SQL). Such languages emphasize ease and convenience of use. In terms of expressiveness, RELATIONAL ALGEBRA and RELATIONAL CALCULUS are identical.

3
YV - Relational Calculus, SQL, QBE 156 Relational Calculus: Introduction (2) The Relational Calculus uses the notion of VARIABLE There are two flavors of the language: –tuple calculus variables refer to tuples from a specified relation e.g., t is a tuple variable referring to a tuple of r(R) –domain calculus variables refer to individual values from a specified domain e.g., d is a domain variable referring to the value which a tuple in r(R) has for attribute A (from domain D)

4
YV - Relational Calculus, SQL, QBE 157 Tuple Calculus A TUPLE CALCULUS EXPRESSION defines a new relation in terms of existing (base) relations. An expression is constructed from the following elements: (1) tuple variables (e.g., t, v, w, t 1, t 2, t 3,... t n ), which are defined to range over a specified relation instance r(R) Tuple variables may be restricted, where, t.A, with A an attribute of R, denotes the A-component of the value of t Example: t.Name (2)conditions of the form x operator y, where -- x, y are restricted tuple variables or constant values -- operator Examples: t.Name = ‘mary’, t.City v.City

5
YV - Relational Calculus, SQL, QBE 158 Tuple Calculus Expressions (3)Well-Formed Formulas (WFFs), defined as: a condition is a WFF if f is a WFF, so is (f) and (f) (where is the logical NOT) if f and g are WFFs, so are (f g) and (f g) (where are the logical AND, OR respectively) if f is a WFF with free variable t, so are t(f), t(f) (where , are the existential and universal quantifiers) A tuple variable t is said to be bound in a formula f if it is one of the quantified variables in f. Otherwise, t is free in f. Example: t is free in f 1 : (t.City = ‘london’) and is bound in f 2 : ( t ) (t.DNumber = v.Dno), while v is free in f 2

6
YV - Relational Calculus, SQL, QBE 159 Tuple Calculus Expressions Examples of WFFs –t.City = v.City –(t.City = v.City) – (t.City = v.City) –(t.City v.City) –(t.City = v.City) (w.City = london) – t (t.City = v.City) – t (t.City = athens) if f is a WFF, then a tuple calculus expression is any expression of the form: { t i.A j | t i r(R k ) AND f } where R k are relation schemes and A j are attributes in R k

7
YV - Relational Calculus, SQL, QBE 160 Calculus Query Examples Recall the sailors-boats database SAILORS (Sid, SName, Rating) BOATS (Bid, BName, Color) RESERVE (Sid, Bid, Date) QUERY1: Find the names of sailors whose id is greater than 10 and have rating ‘a’ RANGE of t is SAILORS t.SName where (t.Sid > 10) (t.Rating = ‘a’) This is an equivalent form of the pure RC query: { t.SName | t r(SAILORS) (t.Sid > 10) (t.Rating = ‘a’)) }

8
YV - Relational Calculus, SQL, QBE 161 Calculus Query Examples (2) QUERY2: Find the names and ratings of sailors who have reserved boat number 3 RANGE of t is SAILORS RANGE of v is RESERVE t.SName, t.Rating where v ( (t.Sid = v.Sid) v.Bid = 3) ) QUERY3: Find the names sailors who have not reserved boat number 3 RANGE of t is SAILORS RANGE of v is RESERVE t.SName where ( v ( (t.Sid = v.Sid) v.Bid = 3) ) )

9
YV - Relational Calculus, SQL, QBE 162 SQL - Introduction SQL (Structured Query Language) has become the “standard” in query languages. It was first used in IBM’s prototype, called SYSTEM-R, developed at San Jose in the mid-seventies. SQL has gone over many evaluations. There are 4 basic commands: –select(not to be confused with SELECTION in algebra) –insert –update –delete The result of any query on relations is again a relation

10
YV - Relational Calculus, SQL, QBE 163 SQL - Informal Definition Assume the EMPLOYEE relation and the following query: “Find the names of employees who earn more than 30000” This is expressed in SQL as: selecte.Name fromEMPLOYEE e where(e.Salary > 30000) –e is a tuple variable defined to range over the relation EMPLOYEE (in the from clause) –e.Name, as a restricted tuple variable, specifies the value of e in attribute Name, and is the target list (specifies in the select clause the projections of columns) –(e.Salary > 30000) is the qualification (specifies in the where clause all selections and joins)

11
YV - Relational Calculus, SQL, QBE 164 SQL -- Formal Definition A selection clause is a comparison between a restricted tuple variable x and a constant c of the form : x operator c where operator Example: t.Name = ‘mary’, t.Salary A join clause is a comparison between two restricted tuple variables x and y of the form x operator y where x, y belong to different relations and operator Example: t.Name = v.EName

12
YV - Relational Calculus, SQL, QBE 165 SQL -- Formal Definition (2) A qualification is a Boolean combination (i.e., with logical and, or, not) of selection and join clauses.) Example: (t.Name = v.EName) and (t.Salary > 30000) QUALIFICATION SEMANTICS A qualification Q describes the subset of the Cartesian product of the ranges of its tuple variables that satisfy Q Example: Consider relations: EMPLOYEE(SSN, DNumber), and DEPARTMENT(Dno, mgrSSN), with e and d tuple variables Q = (e.DNumber = d.Dno) and (d.mgrSSN = 9876) Semantics of Q: The set of e, d pairs that satisfy Q

13
YV - Relational Calculus, SQL, QBE 166 SQL Qualification Semantics. X = Finally, after establishing the subset of EMPLOYEE X DEPARTMENT that satisfies the qualification Q, we get two tuples: Cartesian Product EMPLOYEEDEPARTMENT

14
YV - Relational Calculus, SQL, QBE 167 SQL -- Complete Format select[ distinct ]target_list fromtuple_variable_list [ wherequalification ] [ group bygrouping_attributes ] [ havinggroup_condition ] [ order bytarget_list_subset ] –A query is evaluated by first applying the WHERE clause, then GROUP-BY and HAVING (all optional), and finally the SELECT clause (target list) - ordering the resulting tuples if required in the ORDER BY clause (also optional).

15
YV - Relational Calculus, SQL, QBE 168 SQL: Sailor Examples Consider again the Sailors-Boats database: SAILORS (Sid, SName, Rating) BOATS (Bid, BName, Color) RESERVE (Sid, Bid, Date) SQUERY1: Find the names of sailors who have reserved boat number 2 selects.SName fromSAILORS s, RESERVE r wheres.Sid = r.Sid and r.Bid = 2

16
YV - Relational Calculus, SQL, QBE 169 SQL Sailor Examples (2) SQUERY2: Find the names of sailors who have reserved a red boat selects.SName fromSAILORS s, BOATS b, RESERVE r wheres.Sid = r.Sid and r.Bid = b.Bid and b.Color = “red” SQUERY3: Find the colors of the boats reserved by eleni selectb.Color fromSAILORS s, BOATS b, RESERVE r wheres.Sid = r.Sid and r.Bid =b.Bid and s.SName= “eleni”

17
YV - Relational Calculus, SQL, QBE 170 SQL Sailor Examples (3) SQUERY4: Find the names of the sailors who have reserved at least one boat selects.SName fromSAILORS s, RESERVE r wheres.Sid = r.Sid SQUERY5: Find the names of sailors who have reserved a red or a green boat selects.SName fromSAILORS s, BOATS b, RESERVE r wheres.Sid = r.Sid and r.Bid = b.Bid and (b.Color = “red” or b.Color = “green”)

18
YV - Relational Calculus, SQL, QBE 171 SQL Sailor Examples (4) SQUERY6: Find the names of sailors who have reserved both a red and a green boat selects.SName fromSAILORS s, BOATS b1, RESERVE r1, BOATS b2, RESERVE r2 wheres.Sid = r1.Sid and r1.Bid = b1.Bid and b1.Color = “red” and s.Sid = r2.Sid and r2.Bid = b2.Bid and b2.Color = “green”

19
YV - Relational Calculus, SQL, QBE 172 SQL Elaboration: Examples from the COMPANY database EMPLOYEE ( SSN, Name, BirthDate, Address, Sex, Salary, SupSSN, DNumber) DEPARTMENT ( DNumber, DName, MgrSSN, MgrStartDate) PROJECT ( PNumber, PName, Location, DNumber) DEPT_LOCATION ( DNumber, DLocation) WORKS_ON ( SSN, PNumber, HoursPW) DEPENDENT ( SSN, DependName, Sex, BirthDate, Relationship)

20
YV - Relational Calculus, SQL, QBE 173 SQL: Target List Examples (1) TARGET LISTS: Each item in a target list can be as general as: attribute_name = expression where expression is any arithmetic or string expression over restricted tuple variables and constants (also built- ins and aggregates.) CQUERY1: List, increased by 10000 the salary of employees who have worked on two different projects more than 25 hours selecte.Name, Salary = e.Salary + 10000 fromEMPLOYEE e, WORKS_ON w1, WORKS_ON w2 wheree.SSN = w1.SSN and e.SSN = w2.SSN and w1.HoursPW > 25 and w2.HoursPW > 25 and w1.PNumber != w2.PNumber

21
YV - Relational Calculus, SQL, QBE 174 SQL: Target List Examples (2) TARGET Lists may also contain the keyword DISTINCT –Since SQL does not treat relations as sets, duplicate tuples may appear, therefore DISTINCT is used to eliminate the duplicates CQUERY2: Show all distinct (different values) salaries that employees earn selectdistinct e.Salary fromEMPLOYEE e The above query also shows that the WHERE clause is optional (with missing WHERE, all tuples qualify)

22
YV - Relational Calculus, SQL, QBE 175 SQL: Target List Examples (3) The TARGET List may contain the wild character: “*” When a * is used, it implies that all attributes from the relation(s) should be retrieved CQUERY3: Show all employees in department number 4 select* fromEMPLOYEE e wheree.Dnumber = 4

23
YV - Relational Calculus, SQL, QBE 176 SQL: Use of Tuple Variables Relation names can be used instead of tuple variables Tuple variables can be implicit if the system can figure out which relation each attribute belongs to. CQUERY4: For every project located in Athens, list the project name, the controlling department number and the department’s manager name selectPName, DEPARTMENT.DNumber, Name fromEMPLOYEE, DEPARTMENT, PROJECT wherePROJECT.DNumber = DEPARTMENT.DNumber and MgrSSN = SSN and Location = “athens”

24
YV - Relational Calculus, SQL, QBE 177 SQL Qualification Examples -1 QUALIFICATIONS: Each item in a qualification can be as general as: expression = expression CQUERY5: Find the names of employees whose salary is more than double the salary of some other employee (also show their name) selectName1 = e1.Name, Name2 = e1.Name fromEMPLOYEE e1, EMPLOYEE e2 where2 * e1.Salary < e2.Salary

25
YV - Relational Calculus, SQL, QBE 178 SQL Qualification Examples -2 SQL provides direct support of several SET operations, like: –union –minus –intersect CQUERY6 : Find the names of employees who work in department number 4 and earn at most 40000 (selectName fromEMPLOYEE whereDNumber = 4 ) minus (selectName fromEMPLOYEE whereSalary > 40000 )

26
YV - Relational Calculus, SQL, QBE 179 SQL Qualification Examples -3 CQUERY7: List all project names for projects that involve an employee whose name is “jenny” as a worker or as a manager of the department that controls the project (select PName from EMPLOYEE, PROJECT, DEPARTMENT where EMPLOYEE.DNumber=DEPARTMENT.DNumber and MgrSSN = SSN and Name = “jenny” ) union (select PName from EMPLOYEE, PROJECT, WORKS_ON where PROJECT.PNumber = WORKS_ON.PNumber and WORKS_ON.SSN=EMPLOYEE.SSN and Name = “jenny” )

27
YV - Relational Calculus, SQL, QBE 180 SQL Qualification Examples-4 NESTING OF SQL QUERIES: A complete SELECT query (called the nested query) can be specified in the qualification of another query (called the outer query) CQUERY8: List all employees that work in the research department selectName fromEMPLOYEE whereDNumber in (select DNumber from DEPARTMENT where DName = “research” )

28
YV - Relational Calculus, SQL, QBE 181 SQL Qualification Examples-5 CORRELATED NESTED Queries: If a condition in the qualification of a nested query references an attribute in the outer query, the two are said to be CORRELATED The result of the nested query is different for each tuple of the relation(s) in the outer query CQUERY9: List the name of each employee who has a dependent with the same sex as the employee selectName fromEMPLOYEE e wheree.SSN in (select d.SSN from DEPENDENT d where d.SSN = e.SSN and d.Sex = e.Sex )

29
YV - Relational Calculus, SQL, QBE 182 SQL Qualification Examples-6 Any query that uses the IN comparison operator (tests for set membership) can always be expressed as a single block query (flat query) CQUERY9a: List the name of each employee who has a dependent with the same sex as the employee selectName fromEMPLOYEE e, DEPENDENT d wheree.SSN = d.SSN and d.Sex = e.Sex )

30
YV - Relational Calculus, SQL, QBE 183 SQL Qualification Examples-7 Similar connectives to IN are: not in (tests for set non-membership) OP any (OP relationship with some tuple in a set) OP all (OP relationship with all tuples in a set) where OP CQUERY10: List all employees that earn more than everybody in the research department selectName fromEMPLOYEE whereSalary > all (select Salary from EMPLOYEE e, DEPARTMENT d where e.DNumber = d.DNumber and DName = “research” )

31
YV - Relational Calculus, SQL, QBE 184 SQL Qualification Examples-8 SQL also provides SET COMPARATORS: –contains, not contains (a set (not) being a superset) –exists, not exists (a set (not) being empty) CQUERY11: List all employees who work on all projects controlled by department number 4 selectName fromEMPLOYEE e where(select w.PNumber from WORKS_ON where w.SSN = e.SSN) contains (select PNumber from PROJECT where DNumber = 4)

32
YV - Relational Calculus, SQL, QBE 185 SQL Qualification Examples-9 CQUERY9b: List the name of each employee who has a dependent with the same sex as the employee selectName fromEMPLOYEE e whereexists (select * fromDEPENDENT d where d.SSN=e.SSN and d.Sex = e.Sex) CQUERY12: List the employees with no dependents selectName fromEMPLOYEE e wherenot exists (select * from DEPENDENT d where d.SSN=e.SSN )

33
YV - Relational Calculus, SQL, QBE 186 SQL Aggregates and Groups-1 SQL supports FIVE AGGREGATE FUNCTIONS (can be applied to any attribute X of a relation): count ( [DISTINCT] X) :number of unique values in X sum ( [DISTINCT] X) :sum of unique values in X avg ( [DISTINCT] X) :average of unique values in X max (X) :maximum value in X min (X) :minimum value in X Aggregates return a single value

34
YV - Relational Calculus, SQL, QBE 187 SQL Aggregates and Groups-2 Some SQL implementations do not allow more than one value in the target list CQUERY13: List the maximum salary, the minimum salary, and the average salary among all employees selectmax(Salary), min(Salary), avg(Salary fromEMPLOYEE QUERY14: Find the number of employees selectcount(*) fromEMPLOYEE

35
YV - Relational Calculus, SQL, QBE 188 SQL Aggregates and Groups-3 Qualified Aggregates: The set on which aggregates apply can be restricted by the where-clause CQUERY15: Find the average salary of employees in department with number 4 selectavg(Salary) fromEMPLOYEE whereDNumber = 4

36
YV - Relational Calculus, SQL, QBE 189 SQL Aggregates and Groups-4 Aggregate Functions: Aggregates or groups of tuples are computed using the GROUP BY clause CQUERY16: In each department, find the minimum age of employees who earn more than 40000 select DNumber, max(BirthDate) from EMPLOYEE where Salary > 40000 group by DNumber –Note that the grouping attributes MUST ALSO appear in the select clause

37
YV - Relational Calculus, SQL, QBE 190 SQL Aggregates and Groups-5 CQUERY17: For each project, retrieve the project number, project name, and the number of employees that work on that project select p.PNumber, p.PName, count(*) from PROJECT p, WORKS_ON w where p.PNumber = w.PNumber group by PNumber, PName In the above query, the grouping and functions are applied after joining the relations PROJECT and WORKS_ON.

38
YV - Relational Calculus, SQL, QBE 191 SQL Aggregates and Groups-6 HAVING CLAUSE: Qualifications that have to be satisfied by each group formed by the group by- clause are put in a HAVING clause CQUERY18: Find the average salary of employees born after 1950 for each department with more than 10 such employees select DNumber, avg(Salary) from EMPLOYEE where BirthDate > “1.1.51” group by DNumber having count(*) > 10

39
YV - Relational Calculus, SQL, QBE 192 SQL Aggregates and Groups-6 CQUERY19: Find the average salary of employees born after 1950 for each department with more than 10 employees select e1.DNumber, avg(e1.Salary) from EMPLOYEE e1 where e1.BirthDate > “1.1.51” group by e1.DNumber having 10 < any (select count(e2.SSN) from EMPLOYEE e2 where e2.DNumber = e1.DNumber )

40
YV - Relational Calculus, SQL, QBE 193 SQL Updates (1) INSERT command insert into relation_name select-statement or insert into relation_name values (value_list) CUPDATE1: Insert a new department insert into DEPARTMENT values (6, “inventory”, 9879, “30.5.45”)

41
YV - Relational Calculus, SQL, QBE 194 SQL Updates (2) Suppose we have a relation DEPT_INFO as in: DEPT_INFO ( DeptName, NoOfEmpl, TotalSalary) We can insert tuples in this relation with (CUPDATE2): insert into DEPT_INFO select d.DName, count(*), sum(e.Salary) from DEPARTMENT d, EMPLOYEE e where d.DNumber=e.DNumber group by d.DName

42
YV - Relational Calculus, SQL, QBE 195 SQL Updates (3) DELETE command delete from relation_name where-qualification SEMANTICS: -- Execute the corresponding SELECT command and then remove the resulting tuples from relation_name CUPDATE3: delete from EMPLOYEE where DNumber in (selectDNumber fromDEPARTMENT whereDName = “admin”)

43
YV - Relational Calculus, SQL, QBE 196 SQL Updates (4) UPDATE command update relation_name set target_list where qualification SEMANTICS: -- Execute the two corresponding SELECT commands, then remove the old tuples from relation, then insert the new ones CUPDATE4: update EMPLOYEE set Salary = Salary * 1.14 where DNumber in (selectDNumber fromDEPARTMENT whereDName = “admin”)

Similar presentations

OK

1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.

1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on quality education fund Download ppt on reduce reuse recycle Ppt on java virtual machine Ppt on phonetic transcription ipa Ppt on customer based brand equity Ppt on formal education will make you a living Ppt on shell scripting language Ppt on media revolutionary Ppt on special types of chromosomes genes Signal generator and display ppt on tv