Download presentation
Presentation is loading. Please wait.
Published byCecily Carson Modified over 9 years ago
1
-BY PROF. K. U. SHARMA UNIT 2 – Database Systems
2
Chapter – 3 Relational model
3
Content Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations Extended Relational-Algebra-Operations Null Values Modification of the Database 6/5/2016 3 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
4
Basic Structure Formally, given sets D 1, D 2, …. D n a relation r is a subset of D 1 x D 2 x … x D n Thus, a relation is a set of n-tuples (a 1, a 2, …, a n ) where each a i D i Example: If customer_name = {Jones, Smith, Curry, Lindsay, …} /* Set of all customer names */ customer_street = {Main, North, Park, …} /* set of all street names*/ customer_city = {Harrison, Rye, Pittsfield, …} /* set of all city names */ Then r = { (Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield) } is a relation over customer_name x customer_street x customer_city 6/5/2016 4 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
5
Attribute Types Each attribute of a relation has a name The set of allowed values for each attribute is called the domain of the attribute Attribute values are (normally) required to be atomic; that is, indivisible E.g. the value of an attribute can be an account number, but cannot be a set of account numbers Domain is said to be atomic if all its members are atomic The special value null is a member of every domain The null value causes complications in the definition of many operations We shall ignore the effect of null values in our main presentation and consider their effect later 6/5/2016 5 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
6
Relation Schema A 1, A 2, …, A n are attributes R = (A 1, A 2, …, A n ) is a relation schema Example: Customer_schema = (customer_name, customer_street, customer_city) r(R) denotes a relation r on the relation schema R Example: customer (Customer_schema) 6/5/2016 6 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
7
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 6/5/2016 7 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Jones Smith Curry Lindsay customer_name Main North Park customer_street Harrison Rye Pittsfield customer_city customer attributes (or columns) tuples (or rows)
8
Database A database consists of multiple relations Information about an enterprise is broken up into parts, with each relation storing one part of the information account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customers Storing all information as a single relation such as bank(account_number, balance, customer_name,..) results in repetition of information e.g.,if two customers own an account (What gets repeated?) the need for null values e.g., to represent a customer without an account Normalization theory (Chapter 7) deals with how to design relational schemas 6/5/2016 8 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
9
Keys Let K R K is a superkey of R if values for K are sufficient to identify a unique 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 superkeys of Customer, if no two customers can possibly have the same name In real life, an attribute such as customer_id would be used instead of customer_name to uniquely identify customers, but we omit it to keep our examples small, and instead assume customer names are unique. 6/5/2016 9 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
10
Keys K is a candidate key if K is minimal Example: {customer_name} is a candidate key for Customer, since it is a superkey and no subset of it is a superkey. Primary key: a candidate key chosen as the principal means of identifying tuples within a relation Should choose an attribute whose value never, or very rarely, changes. E.g. email address is unique, but may change 6/5/2016 10 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
11
Foreign Keys A relation schema may have an attribute that corresponds to the primary key of another relation. The attribute is called a foreign key. E.g. customer_name and account_number attributes of depositor are foreign keys to customer and account respectively. Only values occurring in the primary key attribute of the referenced relation may occur in the foreign key attribute of the referencing relation. Schema diagram 6/5/2016 11 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
12
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure” languages: Relational algebra Tuple relational calculus Domain relational calculus Pure languages form underlying basis of query languages that people use. 6/5/2016 12 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
13
Relational Algebra Procedural language Six basic operators select: project: union: set difference: – Cartesian product: x rename: The operators take one or two relations as inputs and produce a new relation as a result. 6/5/2016 13 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
14
Select Operation – Example n Relation r 6/5/2016 14 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 ABCD 1 5 12 23 7 3 10 ABCD 1 23 7 10 A=B ^ D > 5 (r)
15
Select Operation Notation: p (r) p is called the selection predicate Defined as: p (r) = {t | t r and p(t)} Where p is a formula in propositional calculus consisting of terms connected by : (and), (or), (not) Each term is one of: op or where op is one of: =, , >, . <. Example of selection: branch_name=“Perryridge” (account) 6/5/2016 15 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
16
Project Operation – Example n Relation r 6/5/2016 16 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 A,C (r) ABC 10 20 30 40 11121112 AC 11121112 = AC 112112
17
Project Operation Notation: where A 1, A 2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets Example: To eliminate the branch_name attribute of account account_number, balance (account) 6/5/2016 17 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
18
Union Operation – Example Relations r, s: 6/5/2016 18 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 n r s: AB 121121 AB 2323 r s AB 12131213
19
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 (example: 2 nd column of r deals with the same type of values as does the 2 nd column of s) Example: to find all customers with either an account or a loan customer_name (depositor) customer_name (borrower) 6/5/2016 19 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
20
Set Difference Operation – Example Relations r, s: 6/5/2016 20 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 n r-s: AB 121121 AB 2323 r s AB 1111
21
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 6/5/2016 21 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
22
Cartesian-Product Operation – Example Relations r, s: 6/5/2016 22 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 n r x s: AB 1212 CD 10 20 10 E aabbaabb r s AB 1111222211112222 CD 20 10 20 10 E aabbaabbaabbaabb
23
Cartesian-Product Operation Notation r x s Defined as: r x s = {t q | 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. 6/5/2016 23 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
24
Composition of Operations Can build expressions using multiple operations Example: A=C (r x s) r x s 6/5/2016 24 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 A=C (r X s) AB 1111222211112222 CD 10 20 10 20 10 E aabbaabbaabbaabb ABCDE 122122 20 aabaab
25
Rename Operation Allows us to name, and therefore to refer to, the results of relational- algebra expressions. Allows us to refer to a relation by more than one name. Example: x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A 1, A 2, …., A n. 6/5/2016 25 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
26
Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer_name, loan_number) 6/5/2016 26 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
27
Example Queries 6/5/2016 27 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find all loans of over $1200 n Find the loan number for each loan of an amount greater than $1200 amount > 1200 (loan) loan_number ( amount > 1200 (loan)) n Find the names of all customers who have a loan, an account, or both, from the bank customer_name (borrower) customer_name (depositor)
28
Example Queries 6/5/2016 28 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find the names of all customers who have a loan at the Perryridge branch. n Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank. customer_name ( branch_name = “Perryridge” ( borrower.loan_number = loan.loan_number (borrower x loan))) – customer_name (depositor) customer_name ( branch_name=“Perryridge ” ( borrower.loan_number = loan.loan_number (borrower x loan)))
29
Example Queries 6/5/2016 29 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find the largest account balance Strategy: Find those balances that are not the largest Rename account relation as d so that we can compare each account balance with all others Use set difference to find those account balances that were not found in the earlier step. The query is: balance (account) - account.balance ( account.balance < d.balance (account X d (account)))
30
Additional Operations We define additional operations that do not add any power to the relational algebra, but that simplify common queries. Set intersection Natural join Division Assignment 6/5/2016 30 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
31
Set-Intersection Operation Notation: r s Defined as: r s = { t | t r and t s } Assume: r, s have the same arity attributes of r and s are compatible Note: r s = r – (r – s) 6/5/2016 31 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
32
Set-Intersection Operation – Example Relation r, s: r s 6/5/2016 32 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 A B 121121 2323 rs 2
33
Division Operation Notation: 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 ) } Where tu means the concatenation of tuples t and u to produce a single tuple 6/5/2016 33 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
34
Division Operation – Example n Relations r, s: 6/5/2016 34 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 B 1212 AB 1231113461212311134612 s A nr s:nr s:
35
Another Division Example n Relations r, s: 6/5/2016 35 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 nr s:nr s: AB aaaaaaaaaaaaaaaa CD aabababbaabababb E 1111311111113111 D abab E 1111 r s AB aaaa C
36
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 a 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 x 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. 6/5/2016 36 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
37
Extended Relational-Algebra-Operations Generalized Projection : It extends the projection operation by allowing arithmetic function to be used in projection list. It has the form Π F1,F2,...,Fn (E). Where, E -> Relational Algebra Expression, F1,F2,…Fn -> Arithmetic Expression Aggregate Function : It takes a collection of values & returns single value as a result. That are avg(), count(), max(), min(), sum(). 6/5/2016 37 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
38
Example AVG() –It returns the average value of a numeric column. COUNT() –It returns the number of elements in collection. SUM() –It takes a collection of values and returns the sum of the values. MIN() & MAX() –It returns the minimum & maximum values in collection. Eg: Select sum(column_name) from table_name; Select min(column_name) from table_name; Select max(column_name) from table_name; 6/5/2016 38 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
39
Aggregate Operation – Example Relation r: 6/5/2016 39 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 AB C 7 3 10 sum(c ) 27 n g sum(c ) (r)
40
Aggregate Operation – Example Relation account grouped by branch-name: 6/5/2016 40 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 branch_nameaccount_numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch_name g sum( balance ) ( account ) branch_namebalance Perryridge Brighton Redwood 1300 1500 700
41
Aggregate Functions (Cont.) Result of aggregation does not have a name Can use rename operation to give it a name For convenience, we permit renaming as part of aggregate operation 6/5/2016 41 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 branch_name g sum(balance) as sum_balance ( account ) branch_namesum_balance Perryridge Brighton Redwood 1300 1500 700
42
Joins A SQL join clause combines records from two or more tables in a database. It creates a set that can be saved as a table or used as it is. A JOIN is a means for combining fields from two tables by using values common to each. ANSI standard SQL specifies four types of JOIN: INNER, NATURAL, OUTER, SELF. As a special case, a table (base table, view, or joined table) can JOIN to itself in a Self-Join. 6/5/2016 42 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
43
Example For explanation purpose let us consider the following two tables: Department Table: Employee Table: 6/5/2016 43 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 D_IDD_Name 31Sales 33Engineering 34Clerical 35Marketing LastNameD_ID Rafferty31 Jones33 Steinberg33 Robinson34 Smith34 JohnNULL
44
1. Inner Join (Equi-Join) An inner join requires each record in the two joined tables to have a matching record. An inner join essentially combines the records from two tables (A and B) based on a given join-predicate. select * from employee inner join department on employee.D_ID= department.D_ID; The result of the query will be: 6/5/2016 44 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Employee. LastName Employee. DepartmentID Department. DepartmentName Department. DepartmentID Robinson34Clerical34 Jones33Engineering33 Smith34Clerical34 Steinberg33Engineering33 Rafferty31Sales31
45
2. Natural Join A natural join is a join statement that compares the common columns of both tables with each other. One should check whether common columns exist in both tables before doing a natural join. SELECT *FROM employee NATURAL JOIN department; 6/5/2016 45 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 DepartmentIDEmployee.LastNameDepartment.DepartmentName 34SmithClerical 33JonesEngineering 34RobinsonClerical 33SteinbergEngineering 31RaffertySales
46
3. Outer Join An extension of the join operation that avoids loss of information. Computes the join and then adds tuples from one relation that does not match tuples in the other relation to the result of the join. An outer join does not require each record in the two joined tables to have a matching record. The joined table retains each record—even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins, depending on which table's rows are retained (left, right, or both). 6/5/2016 46 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
47
3.1 Left Outer Join It takes all tuples in left relation that did not match with right relation, pads the tuples with NULL value for all other attributes from right relation, and adds them to the result of the natural join. SELECT * FROM employee LEFT OUTER JOIN department ON employee.D_ID = department.D_ID; 6/5/2016 47 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Employee. LastName Employee. DepartmentID Department. DepartmentName Department. DepartmentID Jones33Engineering33 Rafferty31Sales31 Robinson34Clerical34 Smith34Clerical34 JohnNULL Steinberg33Engineering33
48
3.2 Right Outer Join It is symmetric with left outer join : pads tuple from the right relation that did not match any from the left relation with Null and adds them to the result of the Natural join SELECT * FROM employee RIGHT OUTER JOIN department ON employee.D_ID= department.D_ID; 6/5/2016 48 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Employee. LastName Employee. DepartmentID Department. DepartmentName Department. DepartmentID Smith34Clerical34 Jones33Engineering33 Robinson34Clerical34 Steinberg33Engineering33 Rafferty31Sales31 NULL Marketing35
49
3.3 Full Outer Join Does both those operations, padding tuples from left relation that did not match any from right relation, as well as tuples from right relation that did not match any from left relation, adds them to the result of join operation SELECT * FROM employee FULL OUTER JOIN department ON employee.D_ID = department.D_ID; 6/5/2016 49 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Employee. LastName Employee. D_ID Department. D_Name Department. D_ID Jones33Engineering33 Rafferty31Sales31 Robinson34Clerical34 Smith34NULL JohnNULL Steinberg33NULL Marketing35
50
Outer Join – Example Relation loan 6/5/2016 50 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 3000 4000 1700 loan_numberamount L-170 L-230 L-260 branch_name Downtown Redwood Perryridge n Relation borrower customer_nameloan_number Jones Smith Hayes L-170 L-230 L-155
51
Outer Join – Example Join loan borrower 6/5/2016 51 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 n Left Outer Join loan borrower loan_numberamount L-170 L-230 3000 4000 customer_name Jones Smith branch_name Downtown Redwood Jones Smith null loan_numberamount L-170 L-230 L-260 3000 4000 1700 customer_namebranch_name Downtown Redwood Perryridge
52
Outer Join – Example n Right Outer Join loan borrower 6/5/2016 52 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 n Full Outer Join loan borrower loan_numberamount L-170 L-230 L-155 3000 4000 null customer_name Jones Smith Hayes branch_name Downtown Redwood null loan_numberamount L-170 L-230 L-260 L-155 3000 4000 1700 null customer_name Jones Smith null Hayes branch_name Downtown Redwood Perryridge null
53
Different SQL Joins INNER JOIN: Returns all rows when there is at least one match in BOTH tables LEFT JOIN: Return all rows from the left table, and the matched rows from the right table RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table FULL JOIN: Return all rows when there is a match in ONE of the tables 6/5/2016 53 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
54
Null Values It is possible for tuples to have a null value, denoted by null, for some of their attributes null signifies an unknown value or that a value does not exist. The result of any arithmetic expression involving null is null. Aggregate functions simply ignore null values (as in SQL) For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same (as in SQL) 6/5/2016 54 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
55
Modification of the Database The content of the database may be modified using the following operations: Deletion Insertion Updating All these operations are expressed using the assignment operator. 6/5/2016 55 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
56
Deletion A delete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database. Can delete only whole tuples; cannot delete values on only particular attributes A deletion is expressed in relational algebra by: r r – E where r is a relation and E is a relational algebra query. 6/5/2016 56 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
57
Deletion Examples 6/5/2016 57 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Delete all account records in the Perryridge branch. n Delete all loan records with amount in the range of 0 to 50 loan loan – amount 0 and amount 50 (loan) account account – branch_name = “Perryridge” (account )
58
Insertion To insert data into a relation, we either: specify a tuple to be inserted write a query whose result is a set of tuples to be inserted in relational algebra, an insertion is expressed by: r r E where r is a relation and E is a relational algebra expression. The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple. 6/5/2016 58 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
59
Insertion Examples 6/5/2016 59 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch. account account {(“A-973”, “Perryridge”, 1200)} depositor depositor {(“Smith”, “A-973”)}
60
Updating A mechanism to change a value in a tuple without charging all values in the tuple Use the generalized projection operator to do this task Each F i is either the I th attribute of r, if the I th attribute is not updated, or, if the attribute is to be updated F i is an expression, involving only constants and the attributes of r, which gives the new value for the attribute 6/5/2016 60 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
61
Update Examples 6/5/2016 61 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Make interest payments by increasing all balances by 5 percent. n Pay all accounts with balances over $10,000 6 percent interest and pay all others 5 percent account account_number, branch_name, balance * 1.06 ( BAL 10000 (account )) account_number, branch_name, balance * 1.05 ( BAL 10000 (account)) account account_number, branch_name, balance * 1.05 (account)
62
Views We have assumed that the relations we are given are the actual relations stored in the database. For security and convenience reasons and for personalized collection of relations for a user. Any relation that is not part of the logical model, but is made visible to a user as a virtual relation is called as view. Once we define a view, we can use the view name to refer to the virtual relation that the view generates. 6/5/2016 62 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
63
View Definition A view is defined using the create view command: create view v as ; where is any legal query expression. To create a view consits of branches and their customer create view all_customer as Π bname, cname(depositor) ⋃ Π bname, cname(borrower); We can now find all customers of the SFU branch by writing Π cname (σ bname = “SFU” (all_customer)) 6/5/2016 63 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
64
Update through Views and Null Values Updates, insertions and deletions using views can cause problems. The modifications on a view must be transformed to modifications of the actual relations in the conceptual model of the database. An example will illustrate: consider a clerk who needs to see all information in the loan relation except amount. Let the view loan- branch be given to the clerk: create view loan_branch as Π loan_no, bname (loan); 6/5/2016 64 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
65
Example (Continued) Since we allow a view name to appear wherever a relation name is allowed, the clerk can write, Loan_branch loan_branch ⋃ {(L-37,”Perryridge”)} This insertion must be represented by an insertion into the relation loan, since loan is the actual relation from which the database system constructs the view loan-branch. However, to insert a tuple into loan, we must have some values for amount. There are two approaches to deal with this insertion: Reject the insertion and return an error message to the user Insert a tuple (L-37, “Perryridge”, null) into the loan relation 6/5/2016 65 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
66
Tuple Relational Calculus The tuple relational calculus is a nonprocedural language. A query in tuple relational calculus is expressed as {t | P(t)} i.e. the set of tuples t for which predicate P is true. We also use the notation: t[a] to indicate the value of tuple t on attribute a. t ∈ r to show that tuple t is in relation r. 6/5/2016 66 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
67
Example To find the branch-name, loan number, customer name and amount for loans over $1200: {t | t ∈ borrower ˄ t[amount] > 1200} The logical connectives ˄ (AND) and ˅ (OR) are allowed, as well as ¬ (negation). We also use the existential quantifier and the universal quantifier ˅. 6/5/2016 67 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
68
Example Find all customers having a loan, an account, or both at the SFU branch: {t | s ∈ borrower (t[cname] = s[cname] ˄ s[bname] = “SFU”) ˅ u ∈ depositor (t[cname] = u[cname] ˄ u[bname] = “SFU”)} Find all customers who have both a loan and an account at the SFU branch. Find customers who have an account, but not a loan at the SFU branch. 6/5/2016 68 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
69
Domain Relational Calculus Domain variables take on values from an attribute's domain, rather than values for an entire tuple. An expression is of the form: { | P(x1, x2,……, xn)} where the xi,1 ≤ i ≤ n, represent domain variables, and P is a formula. Example: Find branch name, loan number, customer name and amount for loans of over $1200. { | ∈ borrower ˄ a > 1200} 6/5/2016 69 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
70
Chapter – 4 SQL
71
Content Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification of the Database Joined Relations** 6/5/2016 71 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
72
History IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory Renamed Structured Query Language (SQL) ANSI and ISO standard SQL: SQL-86 SQL-89 SQL-92 SQL:1999 (language name became Y2K compliant!) SQL:2003 Commercial systems offer most, if not all, SQL-92 features, plus varying feature sets from later standards and special proprietary features. Not all examples here may work on your particular system. 6/5/2016 72 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
73
Data Definition Language Allows the specification of not only a set of relations but also information about each relation, including: The schema for each relation. The domain of values associated with each attribute. Integrity constraints The set of indices to be maintained for each relations. Security and authorization information for each relation. The physical storage structure of each relation on disk. 6/5/2016 73 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
74
Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). smallint. Small integer (a machine-dependent subset of the integer domain type). numeric(p,d). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. float(n). Floating point number, with user-specified precision of at least n digits. 6/5/2016 74 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
75
Create Table Construct An SQL relation is defined using the create table command: create table r (A 1 D 1, A 2 D 2,..., A n D n, (integrity-constraint 1 ),..., (integrity-constraint k )) r is the name of the relation each A i is an attribute name in the schema of relation r D i is the data type of values in the domain of attribute A i Example: create table branch (branch_namechar(15) not null, branch_citychar(30), assetsinteger) 6/5/2016 75 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
76
Integrity Constraints in Create Table not null primary key (A 1,..., A n ) Example: Declare branch_name as the primary key for branch create table branch (branch_namechar(15), branch_citychar(30), assetsinteger, primary key (branch_name)) primary key declaration on an attribute automatically ensures not null in SQL-92 onwards, needs to be explicitly stated in SQL-89 6/5/2016 76 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
77
Drop and Alter Table Constructs The drop table command deletes all information about the dropped relation from the database. The alter table command is used to add attributes to an existing relation: alter table r add A D where A is the name of the attribute to be added to relation r and D is the domain of A. All tuples in the relation are assigned null as the value for the new attribute. The alter table command can also be used to drop attributes of a relation: alter table r drop A where A is the name of an attribute of relation r Dropping of attributes not supported by many databases 6/5/2016 77 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
78
Basic Query Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select A 1, A 2,..., A n from r 1, r 2,..., r m where P A i represents an attribute R i represents a relation P is a predicate. This query is equivalent to the relational algebra expression. The result of an SQL query is a relation. 6/5/2016 78 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
79
The select Clause The select clause list the attributes desired in the result of a query corresponds to the projection operation of the relational algebra Example: find the names of all branches in the loan relation: select branch_name from loan In the relational algebra, the query would be: branch_name (loan) NOTE: SQL names are case insensitive (i.e., you may use upper- or lower-case letters.) E.g. Branch_Name ≡ BRANCH_NAME ≡ branch_name Some people use upper case wherever we use bold font. 6/5/2016 79 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
80
The select Clause (Cont.) SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Find the names of all branches in the loan relations, and remove duplicates select distinct branch_name from loan The keyword all specifies that duplicates not be removed. select all branch_name from loan 6/5/2016 80 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
81
The select Clause (Cont.) An asterisk in the select clause denotes “all attributes” select * from loan The select clause can contain arithmetic expressions involving the operation, +, –, , and /, and operating on constants or attributes of tuples. The query: select loan_number, branch_name, amount 100 from loan would return a relation that is the same as the loan relation, except that the value of the attribute amount is multiplied by 100. 6/5/2016 81 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
82
The where Clause The where clause specifies conditions that the result must satisfy Corresponds to the selection predicate of the relational algebra. To find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. select loan_number from loan where branch_name = 'Perryridge' and amount > 1200 Comparison results can be combined using the logical connectives and, or, and not. Comparisons can be applied to results of arithmetic expressions. 6/5/2016 82 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
83
The where Clause (Cont.) 6/5/2016 83 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 SQL includes a between comparison operator Example: Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is, $90,000 and $100,000) select loan_number from loan where amount between 90000 and 100000
84
The from Clause 6/5/2016 84 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 The from clause lists the relations involved in the query Corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product borrower X loan select from borrower, loan n Find the name, loan number and loan amount of all customers having a loan at the Perryridge branch. select customer_name, borrower.loan_number, amount from borrower, loan where borrower.loan_number = loan.loan_number and branch_name = 'Perryridge'
85
The Rename Operation 6/5/2016 85 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 The SQL allows renaming relations and attributes using the as clause: old-name as new-name Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id. select customer_name, borrower.loan_number as loan_id, amount from borrower, loan where borrower.loan_number = loan.loan_number
86
Tuple Variables 6/5/2016 86 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Tuple variables are defined in the from clause via the use of the as clause. Find the customer names and their loan numbers for all customers having a loan at some branch. n Find the names of all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = 'Brooklyn' n Keyword as is optional and may be omitted borrower as T ≡ borrower T select customer_name, T.loan_number, S.amount from borrower as T, loan as S where T.loan_number = S.loan_number
87
String Operations SQL includes a string-matching operator for comparisons on character strings. The operator “like” uses patterns that are described using two special characters: percent (%). The % character matches any substring. underscore (_). The _ character matches any character. Find the names of all customers whose street includes the substring “Main”. select customer_name from customer where customer_street like ' % Main% ' Match the name “Main%” like ' Main\% ' escape ' \ ' SQL supports a variety of string operations such as concatenation (using “||”) converting from upper to lower case (and vice versa) finding string length, extracting substrings, etc. 6/5/2016 87 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
88
Ordering the Display of Tuples List in alphabetic order the names of all customers having a loan in Perryridge branch select distinct customer_name from borrower, loan where borrower loan_number = loan.loan_number and branch_name = ' Perryridge ' order by customer_name We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default. Example: order by customer_name desc 6/5/2016 88 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
89
Set Operations The set operations union, intersect, and except operate on relations and correspond to the relational algebra operations Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m + n times in r union all s min(m,n) times in r intersect all s max(0, m – n) times in r except all s 6/5/2016 89 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
90
Set Operations 6/5/2016 90 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find all customers who have a loan, an account, or both: (select customer_name from depositor) except (select customer_name from borrower) (select customer_name from depositor) intersect (select customer_name from borrower) n Find all customers who have an account but no loan. (select customer_name from depositor) union (select customer_name from borrower) n Find all customers who have both a loan and an account.
91
Aggregate Functions These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values 6/5/2016 91 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
92
Aggregate Functions (Cont.) 6/5/2016 92 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find the average account balance at the Perryridge branch. n Find the number of depositors in the bank. n Find the number of tuples in the customer relation. select avg (balance) from account where branch_name = 'Perryridge' select count (*) from customer select count (distinct customer_name) from depositor
93
Aggregate Functions – Group By 6/5/2016 93 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find the number of depositors for each branch. Note: Attributes in select clause outside of aggregate functions must appear in group by list select branch_name, count (distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name
94
Aggregate Functions – Having Clause 6/5/2016 94 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find the names of all branches where the average account balance is more than $1,200. Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups select branch_name, avg (balance) from account group by branch_name having avg (balance) > 1200
95
Null Values It is possible for tuples to have a null value, denoted by null, for some of their attributes null signifies an unknown value or that a value does not exist. The predicate is null can be used to check for null values. Example: Find all loan number which appear in the loan relation with null values for amount. select loan_number from loan where amount is null The result of any arithmetic expression involving null is null Example: 5 + null returns null However, aggregate functions simply ignore nulls More on next slide 6/5/2016 95 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
96
Null Values and Three Valued Logic Any comparison with null returns unknown Example: 5 null or null = null Three-valued logic using the truth value unknown: OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown NOT: (not unknown) = unknown “P is unknown” evaluates to true if predicate P evaluates to unknown Result of where clause predicate is treated as false if it evaluates to unknown 6/5/2016 96 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
97
Null Values and Aggregates Total all loan amounts select sum (amount ) from loan Above statement ignores null amounts Result is null if there is no non-null amount All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes. 6/5/2016 97 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
98
Nested Subqueries SQL provides a mechanism for the nesting of subqueries. A subquery is a select-from-where expression that is nested within another query. A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality. 6/5/2016 98 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
99
Example Query 6/5/2016 99 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find all customers who have both an account and a loan at the bank. n Find all customers who have a loan at the bank but do not have an account at the bank select distinct customer_name from borrower where customer_name not in (select customer_name from depositor ) select distinct customer_name from borrower where customer_name in (select customer_name from depositor )
100
Set Comparison 6/5/2016 100 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find all branches that have greater assets than some branch located in Brooklyn. n Same query using > some clause select branch_name from branch where assets > some (select assets from branch where branch_city = ' Brooklyn ' ) select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = ' Brooklyn '
101
Definition of Some Clause 6/5/2016 101 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 F some r t r such that (F t ) Where can be: (5 < some) = true ) = false (5 some) = true (since 0 5) (read: 5 < some tuple in the relation) (5 < some ) = true (5 = some (= some) in However, ( some) not in 0 5 6 0 5 0 5 0 5
102
Example Query 6/5/2016 102 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Find the names of all branches that have greater assets than all branches located in Brooklyn. select branch_name from branch where assets > all (select assets from branch where branch_city = 'Brooklyn')
103
Definition of all Clause 6/5/2016 103 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 F all r t r (F t) (5 < all) = false ) = true (5 all ) = true (since 5 4 and 5 6) (5 < all ) = false (5 = all ( all) not in However, (= all) in 0 5 6 6 10 4 5 4 6
104
Test for Absence of Duplicate Tuples The unique construct tests whether a subquery has any duplicate tuples in its result. Find all customers who have at most one account at the Perryridge branch. select T.customer_name from depositor as T where unique ( select R.customer_name from account, depositor as R where T.customer_name = R.customer_name and R.account_number = account.account_number and account.branch_name = 'Perryridge') 6/5/2016 104 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
105
With Clause The with clause provides a way of defining a temporary view whose definition is available only to the query in which the with clause occurs. Find all accounts with the maximum balance with max_balance (value) as select max (balance) from account select account_number from account, max_balance where account.balance = max_balance.value 6/5/2016 105 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
106
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) Consider a person who needs to know a customer’s name, loan number and branch name, but has no need to see the loan amount. This person should see a relation described, in SQL, by (select customer_name, borrower.loan_number, branch_name from borrower, loan where borrower.loan_number = loan.loan_number ) A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view. 6/5/2016 106 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
107
View Definition A view is defined using the create view statement which has the form create view v as where is any legal SQL expression. The view name is represented by v. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. When a view is created, the query expression is stored in the database; the expression is substituted into queries using the view. 6/5/2016 107 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
108
Example Queries 6/5/2016 108 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 A view consisting of branches and their customers n Find all customers of the Perryridge branch create view all_customer as (select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number ) union (select branch_name, customer_name from borrower, loan where borrower.loan_number = loan.loan_number ) select customer_name from all_customer where branch_name = 'Perryridge'
109
View Definition One view may be used in the expression defining another view A view relation v 1 is said to depend directly on a view relation v 2 if v 2 is used in the expression defining v 1 A view relation v 1 is said to depend on view relation v 2 if either v 1 depends directly to v 2 or there is a path of dependencies from v 1 to v 2 A view relation v is said to be recursive if it depends on itself. 6/5/2016 109 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
110
View Expansion A way to define the meaning of views defined in terms of other views. Let view v 1 be defined by an expression e 1 that may itself contain uses of view relations. View expansion of an expression repeats the following replacement step: repeat Find any view relation v i in e 1 Replace the view relation v i by the expression defining v i until no more view relations are present in e 1 As long as the view definitions are not recursive, this loop will terminate 6/5/2016 110 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
111
Modification of the Database – Deletion Delete all account tuples at the Perryridge branch delete from account where branch_name = ' Perryridge ' Delete all accounts at every branch located in the city ‘Needham’. delete from account where branch_name in (select branch_name from branch where branch_city = ' Needham ' ) 6/5/2016 111 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
112
Example Query 6/5/2016 112 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329 Delete the record of all accounts with balances below the average at the bank. delete from account where balance < (select avg (balance ) from account ) l Problem: as we delete tuples from deposit, the average balance changes l Solution used in SQL: 1. First, compute avg balance and find all tuples to delete 2. Next, delete all tuples found above (without recomputing avg or retesting the tuples)
113
Modification of the Database – Insertion Add a new tuple to account insert into account values ('A-9732', 'Perryridge', 1200) or equivalently insert into account (branch_name, balance, account_number) values ('Perryridge', 1200, 'A-9732') Add a new tuple to account with balance set to null insert into account values ('A-777','Perryridge', null ) 6/5/2016 113 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
114
Modification of the Database – Updates Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%. Write two update statements: update account set balance = balance 1.06 where balance > 10000 update account set balance = balance 1.05 where balance 10000 The order is important Can be done better using the case statement (next slide) 6/5/2016 114 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
115
Case Statement for Conditional Updates Same query as before: Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%. update account set balance = case when balance <= 10000 then balance *1.05 else balance * 1.06 end 6/5/2016 115 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
116
Update of a View Create a view of all loan data in the loan relation, hiding the amount attribute create view loan_branch as select loan_number, branch_name from loan Add a new tuple to branch_loan insert into branch_loan values ('L-37‘, 'Perryridge‘) This insertion must be represented by the insertion of the tuple ('L-37', 'Perryridge', null ) into the loan relation 6/5/2016 116 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
117
Updates Through Views (Cont.) Some updates through views are impossible to translate into updates on the database relations create view v as select loan_number, branch_name, amount from loan where branch_name = ‘Perryridge’ insert into v values ( 'L-99','Downtown', '23') Others cannot be translated uniquely insert into all_customer values ('Perryridge', 'John') Have to choose loan or account, and create a new loan/account number! Most SQL implementations allow updates only on simple views (without aggregates) defined on a single relation 6/5/2016 117 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.