Vous êtes sur la page 1sur 27
Part I - Relational Query Languages Important Topics Relational —algebra__—operations/additional _— relational ~— algebra operations/extended relational algebra operations. Relational algebra/tuple relational calculus and domain relational calculus queries. Short note on - safety of expressions, SQL queries. Short note on - i) Embedded SQL ii) Dynamic SQL iii} ODBC iv) Indexes v) Triggers vi) Stored procedures and functions View. A query language is a language in which a user requests information from the database. These languages are usually on a level higher than that of a standard programming languages. Query languages can be categorized as : i) Procedural language In procedural language, the user instructs the system to perform a sequence of operations on the database to compute the desired result. ii) Nonprocedural language In a nonprocedural language, the user describes the desired information without giving a specific procedure for obtaining that information. 2.1 The Relational Algebra : 6 Ma] The relational algebra is a procedural query language. It consists of a set of operations that take one or two operations as input and produce a new relation as their result. @-41) Database Management System 2-2 Partl-Relational Query Languages > Formal definition of the relational algebra : A basic expression in the relational algebra consists of either one of the following : ¢ Arelation in the database * A constant relation A constant relation is written by listing its tuples within | }, for example {(A -101 Downtown, 500) (A-215, mianus, 700) } A. general expression in relational algebra is constructed out of smaller sub expressions. Let E, and E) be relational algebra expressions. Then, following all are relational algebra expression : * RUE * B-E, © E)xEz * op (E:), where p is a predicate on attributes in E; + Ils (E)), where $ is a list consisting of some of the attributes in E, +p; (E:), where xis the new name for the result of Ey The @ relational algebraic operations are divided into two groups : i) The first group includes the set operations. Since each relation is defined as a set of tuples, the set operations are applicable to the relational data model. Set operations include the following operations © Union * Intersection + Set difference * Cartesian product ii) The second group of relational algebraic operations is developed specially for relational databases. Some of the operations of this group are : * Select © Project * Rename « Join * Division Database Management System 2-3 Part! -Relational Query Languages 2.1.1 Union, Intersection and Difference Union, intersection and difference operations require that the tables involved be union compatible. Two relations are said to be union compatible if the following conditions are satisfied : * The two relations/tables must contain the same number of columns (have the same degree). * Each column of the first relation/table must be either the same data type as the corresponding column of the second relation/table or convertible to the same data type as corresponding column of the second. Consider following two relations : Dopositor Customer_name City Hayes Pune Johnson Mumbai Jones Solapur Lindsay Nashik smith Pune Tumer Mumbai Borrower Gustomer_name City Adams Mumbai Cuny Pune Hayes Pune Jackson Solapur Jones Solapur Smith Pune Williams Kolhapur Fig, 2.4 A Depositor and Borrower relation The two relations are union compatible. Now we will see four set oriented operations. Database Management System 2-4 —_ Part|-Relational Query Languages 24.4.4 Union ‘The result of operation is denoted by Depositor \ Borrower, is the relation that includes all tuples that are either in Depositer or Borrower or in both. Duplicates are eliminated. Example : Find the names of all bank customers who have an account or loan or both. The result of union is : Depositor U Borrower Gustomer_name City ‘Hayes Pune Johnson Mumbai Jones Solapur Lindsay, Nashik Smith Pune Tumer Mumbai ‘Adams Mumbai Curry Pune Jackson Solapur Williams Kolhapur Fig. 2.2 Result of union operation 2.1.1.2: Intersection ‘The result of intersection operation is a relation that includes all tuples that are in both Depositor and Borrower. The intersection operation is denoted by Depositor Borrower. Example : Find the names of customers who have both an account and loan. The result of intersection is : Depositor ~ Borrower Custome_name City Hayes Pune Jones ‘Solapur Smith Pune Fig. 2.3 Result of intersection operation Database Management System 2+5 _ PartI- Relational Query Languages 21.1.3 Difference [Nov-04, 2 Marks] The difference operation is denoted by Depositor - Borrower. The result of the difference operator is the relation that contains all tuples in Depositor but not in Borrower. Depositor « Borrower Fig. 2.4 Result of difference operation Both union and intersection operations are commutative and associative operations. This means that following are true : + AUB= BUA and ANB=BOA + AU(BUC) = (AUB)UG and = AA(BOC)=(ANB)AC The difference operation is not commutative. This means that A — B is not the same as B - A or in other words A ~B#B-A. 21.1.4 Cartesian Product The cartesian product is also known as CROSS PRODUCT or CROSS JOINS. It is denoted by ‘x’. The cartesian product of two relations A and B is denoted by A x B. The result of cartesian product of two relations which have X and Y columns is a relation that has X + Y columns. The resulting relation will have one tuple or each combination of tuples from each participating relation. Hence if the relations have n and m tuples respectively, then the CARTESIAN PRODUCT will have n * m tuples. Consider following two relations : Publisher_Info and Book_Info. Fig. 2.5 Publisher_Info and Book_Info relation Database Management System 2-6 Part! - Relational Query Languages The relation Publisher_Info has 2 columns and 3 tuples. The relation Book_Info has 2 columns and 2 tuples. So the cartesian product has 4 columns (2+2) and 6 tuples (3* 2). The cartesian product of Publisher_Info and Book_Info is given in Fig. 2.6. Publisher_Info « Book_Info Publisher_ID Name Book_ID Title P0001 McGraw-Hill B0001 Dems Poo PHI 80001 DBMS P0003 Pearson 80001 DBMS P0001 McGraw-Hill B0002 Compiler P0002 PHI Bo002 Compiier P0003 Pearson Bo002 Compiler Fig. 2.6 Cartesian product of relations Publisher_Info and Book_info 2.41.2 The Select Operation + The select operation selects tuples that satisfy a given predicate. * The select operation is represented as follows : Se setect condition» (R) The symbol o is used to denote the select operator and the < selection condition > is an expression specified on the attributes of the relation R. The expression specified in the selection condition is made up of number of clauses of the form : < comparison operator > < constant value > or - < attribute name > < comparison operator > < attribute name > where, © is the name of an attribute (column) of the relation R. ‘=< comparison operator > is one of the following comparison operators : =, %, . ® << constant value > is the constant value from the attribute domain. Clauses are connected by boolean operators AND, OR and NOT to form general selection condition. Database Management System 2-7 Parti Relational Query Languages Consider following Book relation as shown in Fig. 2.7. Publisher | Year | Price McGraw-Hill | 2000 250 2004 350 2003 450 2000 500 Fig. 2.7 Book relation . > Exampl Following are the some examples of the select operation. 1) Display books published in year 2000. = Gyear = 2000 (Book) The output of above query is shown below : 2) Display all books having price greater than 300. = Oprice > 200(BOOK) The output of the above query is shown below : Book_Id Publisher | Year | Price “ Booo2 2004 350 0003 | OOMD | Rambaugh 2003 450 B0004 2000 500 3) Select the tuples for all books whose publishing year is 2000 or price is greater than 300. = Oyvear = 2000) OR (peice > 200) (Bock) Database Management System 2-8 — Part!-Relational Query Languages The output of the above query is shown below : Book_Id| Title | Author | Publisher | Year | Price 0001 | DBMS | Korth | McGraw - Hill | 2000 250 80002 | Compiler | Ulan 2004 350 60003 | OOMD | Rambaugh 2003 450 Bo004 | PPL | Sabista 2000 500 4) Select the tuples for all books whose publishing year is 2000 and price is greater than 300. = Fiyear = 2000) and (price > 300) (Book) The output of above query is shown below : Book_Id| Title Author Publisher Year Price Bo004 | PPL | Sabista 2000 500 2.1.3 The Project Operation [Nev-04, 2:Markes) The project operation selects certain columns from a table while discarding others. It removes any duplicate tuples from the result relation. The project operation is represented as follows : 1 < aitstutenst > (RD The symbol TI (pi) is used to denote the project operation and the attribute list is a list of attributes from the attributes of the relation R. The result of the project operation has only the attributes specified in the attribute list and in the same order as they appear in the list. Hence the degree (number of columns) of the result is equal to the number of attributes specified in the attribute list. > Examples: The following are the examples of project operation on Book relation. 1) Display all titles with author name. => M1 tite, Author (Book) Database Management System 2-9 Part! -Relational Query Languages The output of above query is given in Fig. 28. oe ook a Fig. 2.8 Title and Author of Book 2) Display all book titles with authors and price. = Mute autor, frice (Book) ‘The output of above query is given in Fig. 2.9. Title Author DBMS Korth Compiler Gatwin 350 oomD Rambaugh 450 PPL Sabista 500 Fig. 2.9 Title, Author and Price of Books 2.1.4 Composition of Select and Project Operations The relational operations select and project can be combined to form a complicated query. Example : 1) Display the titles of books having price greater than 300. = I nue (Grace > 300 (Book) The output of the above query is given in table 2.10. Title Compiler ‘0OMD PPL Fig. 2.10 Title of the Books Database Management System 2-40 2.1.5 The Rename Operation In relational algebra, you can rename either the relation or the attributes or both. The general rename operation can take any of the following forms : i) Pg inew attribute names (R) ii) ps (R) ili) p (new attbute names) (R) The symbol ‘p’ (rho) is used to denote the RENAME operator. ‘S’ is the new relation and ‘R’ is original relation. The first expression renames both the relation and its attributes, the second renemes the relation only and the third renames only the attributes. > Exampies: For example consider the Book relation with attributes Title, Author, Year and Price. The rename operator is used on Book relation as follows: 1) P Tenp(tname, Aname, Pyest, Bprice) (BOOK) Here both the relation name and the attribute names are renamed. 2) P temp (BOOK) Here only the relation name is renamed. 3) P iBone, Anime. fyear, Bprie) (BOOK) In this case, only the attribute names are renamed. 2.1.6 Additional Operations IMay-05, 6 Marks) The fundamental operations in relational algebra are select, project, union, set difference, cartesian product and rename. These fundamental operations are sufficient to express any relational algebra query. However, if we restrict ourselves to just the fundamental operations, certain common queries are lengthy to express. Therefore, we define additional operations that do not add any power to the algebra, but simplify common queries. For each new operation, we give an equivalent expression that uses only the fundamental operations. The additional operations are = © Set intersection © Natural join * Division operation Assignment operation Database Management System 2-11 Part! - Relatior Query Languages 2.1.6.1 Natural-Join Operation The natural-join is a binary operation that allows us to combine certain selections and a cartesian product into one operation. It is denoted by the ‘join’ symbol P< . The natural join operation forms a cartesian product of its two arguments, performs a selection forcing equality on those attributes that appear in both relation schemes and finally removes duplicate attributes. The difference between natural join and cartesian product is : the query involving cartesian product includes a selection operation on the result of the cartesian product. Example : Consider two relations : i) Employee, with attribute Emp_code and Emp-name ii) Salary, with attributes Emp_code and Salary. The Employee and Salary relations are shown below = Fig. 2.11 The Employee and Salary relations To display the names of all employees with salary, we need to combine the Employee and Salary relations using cartesian product as follows - => Temp-name, calary (Cemployes.omp_code = salary. amp_ceds (EMployee x Salary) The above query is rewritten using natural-join as follows : => Tlemp_name, salary (EmployeeD Examples : 1) Calculate total salary paid to employees. an)(Employee_salary) = Geumes The symbol ‘G’ is the letter G in calligraphic font ; read it as ‘calligraphic G’. The G signifies that aggregate function is to be applied, and its subscript specifies the aggregate operation to be applied. The result of above query is shown in Fig. 2.21. Salary 30000 Fig, 2.21 The result of Gum oiey)(Employee_salary) 2) Display average salary of employees. = Gargesiay)(Employee_salary) The result of above query is shown in Fig. 2.22. 7500 Fig. 2.22 The result of Garg (solam) (Employee_salary) 3) Count number of distinct departments in Employee_salary relation. = Geount-distinct (Department) (Employee_salary) The above query return the value 2. Database Management System 2-17 Part -Relational Query Languages 4) Display total salary of employees department wise. => department G.um(satiny(Employee_salary) The result of above query is shown in Fig, 2.23. Fig. 2.23 The result of department Gem(soiory) (Employee-salary) 24.7.3 Outer-Join The outer-join operation is an extension of the join operation to deal with missing information. Consider following two relations : a) Employee (Emp_name, City) b) Employee_salary (Emp_name, Department, Salary) These two relations are shown in Fig, 2.24 and Fig, 2.25. Employee Jey Solapur Fig. 2.24 The Employee Relation Employee_salary Emp_name | Department Salary Hari Computer 10000 Om 1 7000 Bil ‘Computer 8000 day 1 5000 Fig. 2.25 The Employee_salary Relation Database Management System 2-18 _ Partl - Relational Query Languages Consider that we want to generate a single relation with attributes emp_name, city, department and Salary. A possible approach is to use natural join operation as follows Employee > Employee_salary The result of this expression is given in Fig. 2.26. The resultant relation does not contain Smith and his city, similary Bill's department, and salary is also lost. Fig. 2.26 The result of Employee >< Employee_salary To avoid this loss of information, we can use outer join operation. > Types of outer-join There are three forms of outer-join = a) Left outerjoin. b) Right outer-join. ©) Full outer-join. a) Left outer-join : It is denoted by-D< . It takes all tuples in the left relation that did not match with any tuple in the right relation, pads the tuples with null values for all other attributes from the right relation and adds them to the result of the natural join. The result of the expression - Employee>4_ Employee_salary is shown in Fig. 227. Fig. 2.27 The result of Employee >< Employ: Database Management System 2-19 Part | - Relational Query Languages b) The right outer join The right outer join is denoted bypCEmployee_salary ¢) The full outer join operation It is denoted by DC . It pads tuples from the left relation that did not match with any tuple from the right relation, as well as tuples from the right relation that did not match with any tuple from the left relation and adds them to the resultant relation. The result of the expression Employee >< Employee_salary . is shown in Fig. 2.29. Fig. 2.29 The result of Employes >< Employee_salary Database Management System 2-20 Part !-Relational Query Languages) 2.1.8 Modification of the Database Different operations that modify the contents of the database are : « Delete + Insert * Update We express database modifications by using the assignment operation. 2.1.8.1 Deletion The deletion operation removes the selected tuples from the database. Using delete operation, we can delete only whole tuples ; we cannot delete values of any particular attribute. In relational algebra a deletion is expressed by, rer-E where, r - relation E - relational algebra query > Examples Following are examples of relational algebra delete requests. 1) Delete Smith’s record from employee => Employee Employee - emp numes “smith” (Employee) 2) Delete all employees working in IT department = Employee_salary Employee_salary - opepanmen «1 (Employee_salary) 2.1.8.2 Insertion The relational algebra expresses an insertion by rervE where, rt = relation E - relational algebra expression Database Management System 2-21 Part | - Relational Query Languages. > Examples Following, are the examples of relational algebra insert request. 1) Insert John’s record in Employee and Eraployee_salary relation. => Employee « Employee u { ("John", “Nagpur")} Employee_salary «~Employee_salary v {("John”, “computer”, 6000)} 2) Jime belongs to same city and department to which Smith belong, Insert Jim's record in Employee and Employee_salary relation = 1 © Neny (émp_name « “smith” (Employee) Employee < Employee U ({("Jim")} x1) ¥2 ©11 Department (GEmp_name = “smith" (Employee_salary)) Employee_salary ~ Employee_salary U ({("Jim)} x rz x {(6500) }) 3 Updation Sometimes, we wish to change a value in a tuple without changing all values in the tuple. We can use the generalized projection operator to do this : relyen(r) where, F - the i" attribute of r, if i'” attribute is not updated. Or F, - If the #* attribute of r is to be updated, then F is an expression involving only constants and the attributes of r, that gives the new value for the attribute. > Examples : Following are the examples of relation algebra update request : 1) Increase salary of all employees by 5 percent => Employee-salary Employee_salary @ [emp_name, Department, Salary*1.05 (Bsatary = 400 (Employee_salary)) OM tmp. name. Depsrtment Salary*.0x(TSaiary ~ evo (EMployee_salary)) Y Mlemp_ name. department salary 1.0x(Gsary > sooo (employee_salary)) Database Management System 2 Part I - Relational Query Languages 2.2_ Relational Calculus 2.2.1 Tuple Relational Calculus Introduction The tuple relational algebra is a nonprocedural query language. It describes the desired information without giving a specific procedure for obtaining that information. A query in the tuple relational calculus is expressed as, {#1 PO} that is, it is the set of all tuples ¢ such that predicate P is true for t. In tuple relational calculus - t[A]- denotes the value of tuple on attribute A, f& 1 - denotes tuple t in relation 7. ‘Consider following relations 1) Loan Loan_no Branch_name Amount ut Round Hill 900 14 Downtown 1500 LAs Perryridge: 1500 L416 Perryridge 1300 LA7 Downtown 1000 L23 Redwood 2000 L-93 Mianus 500 2) Depositor Database Management System 2-23 Partl-Relational Query Languages 3) Borrower Customer_name Loan_no Adams Lis Curry 93 Jackson Las Jones Lay ‘Smith un Smith L23 Williams Lay Fig. 2.30 The Loan, Depositor and Borrower relations >» Examples 1) Find al tuples fram the Depositor relation. => {tt © Depositor } 2) Find the Loan_no, Branch_name and Amount for loans of over $ 1200. => {t]¢ © Loan ~ t [Amount] > 1200 } Suppose we want only Loan_no attribute, rather than all attributes of the Loan relation. To express this request, we need to construct ‘there exist’ from mathematical logic. The notation, Bt. r(a(t)) means “there exists a tuple t in relation r such that predicate Q(t) is true" 3) Find loan number of each loan with loan amount greater than 1200. = {¢|3 se Loan (¢{[Loan_no] = s (Loan_nol 1s Amount] > 1200) } In English, we read the preceding expression as “The set of tuples t such that there exists a tuple s in relation Loan for which the values of t and s for the Loan_no attribute are equal, and the value of amount attribute is greater than $1200. 4) Find name and account numbers of all customers. = {t| 3s Depositor (¢ {Customer_name] = s [Customer_name] » t[Account_no = s [Account_no}} Databas: Janagement System 2-24 Part! tional Query Languag 5) Find the names of all customers who have a loan from the Perryridge branch. => {t| 3s eBorrower (t {Customer_name] = s [Customer_name] «3 u ¢Loan (u [Loan_no] = s[Loan_no] « u[Branch_name] = “Perryridge"))} 6) Find all customers who have an account, a loan or both. In relational algebra, we have used union operator. In tuple relational calculus we need two “there exists” clauses connected by or (v) => {t | 3 s © Depositor (t [Customer_namo] = s [Customer_name]) v 3 u € Borrower (t [Customer_name] = u [Customer_name})} 7) Find only those customers who have both an account and a loan at the bank. => {t | 3 s eDepositor (¢ [Customer_name] = s [Customer_name]) » 3 u eBorrower (t [Customer_name}) = u {Customer_name]) } 8) Find all customers who have an account at the bank but do not have a loan from the bank. => {t| 5 u eDepositor (t [Customer_name] = u [Customer_name] ~ +3 s Borrower (t [Customer_name] = s [Customer_name] ) } This expression uses the 3 u ¢ Depositor (...) clause to require that the customer have an account at the bank, and it uses the — 3 s € Borrower (...) clause to eliminate those customers who appear in some tuple of the Borrower relation as having a loan from the bank. 2.2.4.4 Formal Definition of Tuple Relational Calculus A tuple-relational calculus expression is of the form, {#1 PQ) } where, P- isa formula, several tuple variables may appear in a formula. ‘A tuple variable is said to be free variable unless it is qualified by a 3 or V. @: there exists, ¥ : for all) ‘Thus in, t © Loan a3 s € Customer [f [Branch_name] = s [Branch_name]) t- is a free variable s - is a bound variable Database Management System 2-25 — Part! - Relational Query Languages >» A tuple relational formula is built up out of atoms. An atom has one of the following forms : © ser, where s is a tuple variable and r is a relation. © s[x]@ u[y} where s and u are tuple variables, x is an attribute on which s is defined, y is an attribute on which u is defined, and @ is a comparision operator. (<,<,=,#,>,2) ; we require that attributes x and y have domains whose members can be compared by 0. +s [x] c where s is a tuple variable, x is an attribute on which s is defined, 8 is a comparison operator, and c is a constant in the domain of attribute x. » We build up formulae from atoms by using the following rules = * Anatom is a formula. * If P, is a formula, then so are — P, and (Ai). * if Py and Ps are formulae, then so are Pyv Py, Pin Py and Py=>P2 (where = is implies operator). © If P,(s) is a formula containing a free tuple variable s and r is a relation, then 3 s ¢r (P,(s) and Vs er (P;(s)) are also formulae. > Equivalences in tuple relational calculus : 1) Py a Pa is equivalent to =(=(P:)v +(P2)) 2) ¥t er(Pi() is equivalent to 3 t er (AA (8) 3) Pi=> P, is equivalent to=(P,)v Ps. 2.2.1.2 Safety of Expressions [IMP T.E. {Comp) : Oct-2000, 4 Marks, Oct-03, 2 Marks, April-03, 2 Marks] [1.E. (iT) : Dec-04, 4 Marks, May-05, 4 Marks} A tuple relational calculus expression may generate an infinite relation. Consider the expression, {£[- (te Loan)} means, tuples that are not in Loan. There are infinite tuples that are not in Loan relation. Hence, such tuples of expressions should not be allowed. Thus, to restrict the tuple relational calculus, the concept of the domain of a tuple relational formula, P, is introduced. The domain of P denoted by, dom (P) - is the set of all values referenced by P. They include values mentioned in P itself, as well as values that appear in a tuple of a relation mentioned in P. Thus, the domain of P is Database Management System 2-26 Part! - Relational Query Languages the set of all values that appear explicitly in P or that appear in one or more relations whose names appear in P. For example, dom (t € Loan a t [amount] > 1200) is the set containing 1200 as well as the set of all values appearing in Loan. Also dom (—(t € Loan)) is the set of all values appearing in Loan, since the Loan relation is mentioned in expression. Therefore, this expression is not safe. The expression {f | P(1)} is safe if all values that appear in the result are values from dom (P). 2.2.1.3 Expressive Power of Languages The tuple relational calculus restricted to safe expressions is equivalent to the basic relational algebra operations (with the operators U, -, X, a and p without extended relational operators). Thus, for every relational algebra expression using only the basic operation there is an equivalent expression in the tuple relational calculus and_for. every tuple relational culculus expression, there is equivalent relational algebra expression. 2.2.2 The Domain Relational Calculus [Dec-05, 2 Marks, May-06, 4 Mar Domain relational calculus uses domain variables that take on values from an attributes domain, rather than for an entire tuple. > Formal definition An expression in the domain relational calculus is of the form : {SM XE ccc n > | PQ 22 0-0Ke)} where, MX, - represent domain variables. P - formula composed of atoms. » An atom in the domain relational calculus has one of the following forms : # €7, where ris a relation on 1 attributes and x),x2,...,%, are domain variables or domain constants. * x @y, where x and y are domain variables and © is a comparision operator , S, =, #,>, 2). Attributes x and y have domains that can be compared by a. * x @c, where x is a domain variable, © is a comparision operator, and c¢ is a constant in the domain of the attribute for which x is a domain variable. Database Management System 2-27 > Formulae are build up from atoms u © Anatom is a formulae. * If P, is a formula, then so are — P; and (Pj). e If Py and P, are formulae, then so are Rv Py, Px Pz and Py => P3. © If (x) is a formula in x, where x is a domain variable, then 3 x (F (x)) and ¥ x (P\()) are also formulae. > Example queries : 1) Find the loan number, branch name and amount for loans of over $ 1200. = {<1,ba>| <1,.b,a> € loan » a > 1200} 2) Find all loan numbers for loans with an amount greater than $ 1200. > {c1>|3 Ba (<1,b,a> € loan a a > 1200)} 3) Find the names of all customers who have a loan from the Perryridge branch and find the Toan amount. => { |31( © borrower a 3 b(<1,b,a>€ Joann b =" Perryridge")} 4) Find the names of all customers who have a lean at the Perryridge branch. = {|31(e Borrower a3 ba ((<1,b.a>e Loan » b="Perryridge")) } 5) Find the names of all customers who have, an account at the perryridge branch. ={|3a ¢ Depositor a 3 b,n ( ¢ Account » b = "Perryridge")} 6) Find the names of all customers who have a loan, an account, or both at Perryridge branch. = { |31( € Borrower A 3 b,a(<1,b,a>€ loan a b = * Perryridge") v 3 a( eDepositor 44 bn ( eaccount ab = “Peryridge”))}

Vous aimerez peut-être aussi