Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.

Similar presentations


Presentation on theme: "Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations."— Presentation transcript:

1 Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations Security and Authorization Security and Authorization Embedded SQL/ODBC/JDBC Embedded SQL/ODBC/JDBC

2 Derived Relations SQL allows a subquery expression to be used in the from clause SQL allows a subquery expression to be used in the from clause Find the average account balance of those branches where the average account balance is greater than $1200. Find the average account balance of those branches where the average account balance is greater than $1200. select branch_name, avg_balance from (select branch_name, avg (balance) from account group by branch_name ) as branch_avg ( branch_name, avg_balance ) where avg_balance > 1200;

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

4 Complex Query using With Clause Find all branches where the total account deposit is greater than the average of the total account deposits at all branches. Find all branches where the total account deposit is greater than the average of the total account deposits at all branches. with branch_total (branch_name, value) as select branch_name, sum (balance) from account group by branch_name with branch_total_avg (value) as select avg (value) from branch_total select branch_name from branch_total, branch_total_avg where branch_total.value >= branch_total_avg.value

5 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.) 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 loan number but has no need to see the loan amount. This person should see a relation described, in SQL, by Consider a person who needs to know a customer’s loan number but has no need to see the loan amount. This person should see a relation described, in SQL, by (select customer_name, loan_number from borrower, loan where borrower.loan_number = loan.loan_number ) (select customer_name, loan_number from borrower, loan where borrower.loan_number = loan.loan_number )

6 View Definition A view is defined using the create view statement which has the form A view is defined using the create view statement which has the form create view v as 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. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates.

7 Example Queries A view consisting of branches and their customers A view consisting of branches and their customers 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’

8 Modification of the Database – Deletion Delete all account tuples at the Perryridge branch 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 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 ’ )

9 Example Query Delete the record of all accounts with balances below the average at the bank. Delete the record of all accounts with balances below the average at the bank. delete from account where balance < (select avg (balance ) from account ) Problem: as we delete tuples from deposit, the average balance changes 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)

10 Modification of the Database – Insertion Add a new tuple to account 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’) 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 Add a new tuple to account with balance set to null insert into account values (‘A-777’,‘Perryridge’, null )

11 Modification of the Database – Insertion Provide as a gift for all loan customers of the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account Provide as a gift for all loan customers of the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account insert into account select loan_number, branch_name, 200 from loan where branch_name = ‘Perryridge’ insert into depositor select customer_name, loan_number from loan, borrower where branch_name = ‘ Perryridge’ and loan.account_number =borrower.account_number insert into account select loan_number, branch_name, 200 from loan where branch_name = ‘Perryridge’ insert into depositor select customer_name, loan_number from loan, borrower where branch_name = ‘ Perryridge’ and loan.account_number =borrower.account_number

12 Modification of the Database – Updates Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%. Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%. Write two update statements: 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 The order is important Can be done better using the case statement (next slide) Can be done better using the case statement (next slide)

13 Case Statement for Conditional Updates Same query as before: Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%. 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 update account set balance = case when balance <= 10000 then balance *1.05 else balance * 1.06 end

14 Update of a View Create a view of all loan data in the loan relation, hiding the amount attribute Create a view of all loan data in the loan relation, hiding the amount attribute create view branch_loan as select branch_name, loan_number from loan Add a new tuple to branch_loan Add a new tuple to branch_loan insert into branch_loan values (‘Perryridge’, ‘L-307’) This insertion must be represented by the insertion of the tuple (‘L-307’, ‘Perryridge’, null ) into the loan relation

15 Updates Through Views (Cont.) Some insertion to views cannot be translated uniquely Some insertion to views cannot be translated uniquely insert into all_customer values (‘ Perryridge’, ‘John’) insert into all_customer values (‘ Perryridge’, ‘John’) Have to choose loan or account, and create a new loan/account number! 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 Most SQL implementations allow updates only on simple views (without aggregates) defined on a single relation

16 Joined Relations Join operations take two relations and return as a result another relation. Join operations take two relations and return as a result another relation. These additional operations are typically used as subquery expressions in the from clause These additional operations are typically used as subquery expressions in the from clause Join condition – defines which tuples in the two relations match, and what attributes are present in the result of the join. Join condition – defines which tuples in the two relations match, and what attributes are present in the result of the join. Join type – defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated. Join type – defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.

17 Joined Relations

18 Joined Relations – Datasets for Examples Relation loan and borrower Note: borrower information missing for L-260 and loan information missing for L-155

19 Joined Relations – Examples loan inner join borrower on loan.loan_number = borrower.loan_number loan inner join borrower on loan.loan_number = borrower.loan_number loan left outer join borrower on loan.loan_number = borrower.loan_number

20 Joined Relations – Examples loan natural inner join borrower loan natural inner join borrower loan natural right outer join borrower

21 Joined Relations – Examples loan full outer join borrower using (loan_number) loan full outer join borrower using (loan_number) Find all customers who have either an account or a loan (but not both) at the bank. select customer_name from (depositor natural full outer join borrower ) where account_number is null or loan_number is null

22 Authorization Forms of authorization on parts of the database: Select - allows reading, but not modification of data. Select - allows reading, but not modification of data. Insert - allows insertion of new data, but not modification of existing data. Insert - allows insertion of new data, but not modification of existing data. Update - allows modification, but not deletion of data. Update - allows modification, but not deletion of data. Delete - allows deletion of data. Delete - allows deletion of data.

23 Authorization Forms of authorization to modify the database schema Index - allows creation and deletion of indices. Index - allows creation and deletion of indices. Resources - allows creation of new relations. Resources - allows creation of new relations. Alteration - allows addition or deletion of attributes in a relation. Alteration - allows addition or deletion of attributes in a relation. Drop - allows deletion of relations. Drop - allows deletion of relations.

24 Authorization Specification in SQL The grant statement is used to confer authorization The grant statement is used to confer authorization grant grant on to on to is: is: a user-id a user-id Public Public A role A role

25 Privileges in SQL select: allows read access to relation,or the ability to query using the view select: allows read access to relation,or the ability to query using the view Example: grant users U 1, U 2, and U 3 select authorization on the branch relation: Example: grant users U 1, U 2, and U 3 select authorization on the branch relation: grant select on branch to U 1, U 2, U 3

26 Revoking Authorization in SQL The revoke statement is used to revoke authorization. The revoke statement is used to revoke authorization. revoke revoke on from on from Example: Example: revoke select on branch from U 1, U 2, U 3 may be all to revoke all privileges the revokee may hold. may be all to revoke all privileges the revokee may hold. If includes public, all users lose the privilege except those granted it explicitly. If includes public, all users lose the privilege except those granted it explicitly.

27 Revoking Authorization in SQL If the same privilege was granted twice to the same user by different grantees, the user may retain the privilege after the revocation. If the same privilege was granted twice to the same user by different grantees, the user may retain the privilege after the revocation. All privileges that depend on the privilege being revoked are also revoked. All privileges that depend on the privilege being revoked are also revoked.

28 Examples in Oracle grant select on branch to public; grant select on branch to public; revoke select on branch from public; revoke select on branch from public; grant select, insert on branch to public; grant select, insert on branch to public; grant all privilege on branch to public; grant all privilege on branch to public; revoke all privilege on branch from public; revoke all privilege on branch from public; revoke select on branch from public; revoke select on branch from public;

29 Embedded SQL The SQL standard defines embeddings of SQL in a variety of programming languages such as C, Java, and Cobol. The SQL standard defines embeddings of SQL in a variety of programming languages such as C, Java, and Cobol. A language to which SQL queries are embedded is referred to as a host language, and the SQL structures permitted in the host language comprise embedded SQL. A language to which SQL queries are embedded is referred to as a host language, and the SQL structures permitted in the host language comprise embedded SQL. EXEC SQL statement is used to identify embedded SQL request to the preprocessor EXEC SQL statement is used to identify embedded SQL request to the preprocessor EXEC SQL EXEC SQL END_EXEC END_EXEC

30 Example Query Specify the query in SQL and declare a cursor for it Specify the query in SQL and declare a cursor for it EXEC SQL EXEC SQL declare c cursor for select customer_name, customer_city from depositor, customer, account where depositor.customer_name = customer.customer_name and depositor account_number = account.account_number and account.balance > :amount declare c cursor for select customer_name, customer_city from depositor, customer, account where depositor.customer_name = customer.customer_name and depositor account_number = account.account_number and account.balance > :amount END_EXEC END_EXEC

31 Embedded SQL (Cont.) The open statement causes the query to be evaluated The open statement causes the query to be evaluated EXEC SQL open c END_EXEC The fetch statement causes the values of one tuple in the query result to be placed on host language variables. The fetch statement causes the values of one tuple in the query result to be placed on host language variables. EXEC SQL fetch c into :cn, :cc END_EXEC Repeated calls to fetch get successive tuples in the query result The close statement causes the database system to delete the temporary relation that holds the result of the query. The close statement causes the database system to delete the temporary relation that holds the result of the query. EXEC SQL close c END_EXEC

32 Updates Through Cursors Can update tuples fetched by cursor by declaring that the cursor is for update declare c cursor for select * from account where branch_name = ‘Perryridge’ for update To update tuple at the current location of cursor c update account set balance = balance + 100 where current of c

33 Dynamic SQL Allows programs to construct and submit SQL queries at run time. Allows programs to construct and submit SQL queries at run time. Example of the use of dynamic SQL from within a C program. char * sqlprog = “update account set balance = balance * 1.05 where account_number = ?” EXEC SQL prepare dynprog from :sqlprog; char account [10] = “A-101”; EXEC SQL execute dynprog using :account; Example of the use of dynamic SQL from within a C program. char * sqlprog = “update account set balance = balance * 1.05 where account_number = ?” EXEC SQL prepare dynprog from :sqlprog; char account [10] = “A-101”; EXEC SQL execute dynprog using :account;

34 ODBC and JDBC API (application-program interface) for a program to interact with a database server API (application-program interface) for a program to interact with a database server Application makes calls to Application makes calls to Connect with the database server Connect with the database server Send SQL commands to the database server Send SQL commands to the database server Fetch tuples of result one-by-one into program variables Fetch tuples of result one-by-one into program variables ODBC (Open Database Connectivity) works with C, C++, C#, and Visual Basic ODBC (Open Database Connectivity) works with C, C++, C#, and Visual Basic JDBC (Java Database Connectivity) works with Java JDBC (Java Database Connectivity) works with Java

35 JDBC JDBC is a Java API for communicating with database systems supporting SQL JDBC is a Java API for communicating with database systems supporting SQL JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes Model for communicating with the database: Model for communicating with the database: Open a connection Open a connection Create a “statement” object Create a “statement” object Execute queries using the Statement object to send queries and fetch results Execute queries using the Statement object to send queries and fetch results Exception mechanism to handle errors Exception mechanism to handle errors


Download ppt "Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations."

Similar presentations


Ads by Google