Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.

Similar presentations


Presentation on theme: "IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook."— Presentation transcript:

1 IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook for complete material. Text Book: Chapter 5

2 IS 230Lecture 6Slide 2 Chapter 6 Outline  More Complex SQL Retrieval Queries  Specifying Constraints as Assertions and Actions as Triggers  Views (Virtual Tables) in SQL  Schema Change Statements in SQL

3 IS 230Lecture 6Slide 3 More Complex SQL Retrieval Queries  Additional features allow users to specify more complex retrievals from database:  Nested queries, joined tables, outer joins, aggregate functions, and grouping

4 IS 230Lecture 6Slide 4 Comparisons Involving NULL and Three-Valued Logic  Meanings of NULL  Unknown value  Unavailable or withheld value  Not applicable attribute  Each individual NULL value considered to be different from every other NULL value  SQL uses a three-valued logic:  TRUE, FALSE, and UNKNOWN

5 IS 230Lecture 6Slide 5 Comparisons Involving NULL and Three-Valued Logic (cont’d.)

6 IS 230Lecture 6Slide 6 Comparisons Involving NULL and Three-Valued Logic (cont’d.)  SQL allows queries that check whether an attribute value is NULL  IS or IS NOT NULL

7 IS 230Lecture 6Slide 7 Nested Queries, Tuples, and Set/Multiset Comparisons  Nested queries  Complete select-from-where blocks within WHERE clause of another query  Outer query  Comparison operator IN  Compares value v with a set (or multiset) of values V  Evaluates to TRUE if v is one of the elements in V

8 IS 230Lecture 6Slide 8 Nested Queries (cont’d.)  List project names and numbers involving Smith (Lname): SELECT Pnumber, Pname FROM PROJECT WHERE Pnumber IN (SELECT Pno FROM WORKS_ON, EMPLOYEE WHERESSN=ESSN AND Lname=‘Smith’);

9 IS 230Lecture 6Slide 9 Nested Queries (cont’d.)  Use tuples of values in comparisons  Place them within parentheses

10 IS 230Lecture 6Slide 10  Use other comparison operators to compare a single value v  = ANY (or = SOME ) operator Returns TRUE if the value v is equal to some value in the set V and is hence equivalent to IN  Other operators that can be combined with ANY (or SOME ): >, >=, Nested Queries (cont’d.)

11 IS 230Lecture 6Slide 11 Nested Queries (cont’d.) 0 5 6 (5< some) = true 0 5 0 ) = false 5 0 5 (5  some) = true (since 0  5) (read: 5 < some tuple in the relation) (5< some ) = true (5 = some (= some)  in However, (  some)  not in  Definition of Some Clause  F some r  t  r  s.t. (F t) Where can be: 

12 IS 230Lecture 6Slide 12 Nested Queries (cont’d.)  Definition of all Clause  F all r  t  r  (F t) 0 5 6 (5< all) = false 6 10 4 ) = true 5 4 6 (5  all) = true (since 5  4 and 5  6) (5< all ) = false (5 = all (  all)  not in However, (= all)  in

13 IS 230Lecture 6Slide 13 Nested Queries (cont’d.)  Avoid potential errors and ambiguities  Create tuple variables (aliases) for all tables referenced in SQL query

14 IS 230Lecture 6Slide 14 The EXISTS and UNIQUE Functions in SQL  EXISTS function  Check whether the result of a correlated nested query is empty or not  EXISTS and NOT EXISTS  Typically used in conjunction with a correlated nested query  SQL function UNIQUE(Q)  Returns TRUE if there are no duplicate tuples in the result of query Q

15 IS 230Lecture 6Slide 15 Explicit Sets and Renaming of Attributes in SQL  Can use explicit set of values in WHERE clause  Use qualifier AS followed by desired new name  Rename any attribute that appears in the result of a query

16 IS 230Lecture 6Slide 16 Joined Tables in SQL and Outer Joins  Joined table  Permits users to specify a table resulting from a join operation in the FROM clause of a query  The FROM clause in Q1A  Contains a single joined table

17 IS 230Lecture 6Slide 17 Joined Tables in SQL and Outer Joins (cont’d.)  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  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 Types inner join left outer join right outer join full outer join Join Conditions natural on using (A 1, A 2,..., A n )

18 IS 230Lecture 6Slide 18 Joined Tables in SQL and Outer Joins (cont’d.) Relation loan amount 3000 4000 1700 Relation borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L-155 branch-name Downtown Redwood Perryridge loan-number L-170 L-230 L-260 Note: borrower information missing for L-260 and loan information missing for L-155

19 IS 230Lecture 6Slide 19 Joined Tables in SQL and Outer Joins (cont’d.)  loan inner join borrower on loan.loan-number = borrower.loan-number branch-nameamount Downtown Redwood 3000 4000 customer-nameloan-number Jones Smith L-170 L-230 branch-nameamount Downtown Redwood Perryridge 3000 4000 1700 customer-nameloan-number Jones Smith null L-170 L-230 null  loan left outer join borrower on loan.loan-number = borrower.loan-number loan-number L-170 L-230 loan-number L-170 L-230 L-260 Also called theta join

20 IS 230Lecture 6Slide 20 Joined Tables in SQL and Outer Joins (cont’d.)  loan natural inner join borrower branch-nameamount Downtown Redwood 3000 4000 customer-name Jones Smith branch-nameamount Downtown Redwood null 3000 4000 null customer-name Jones Smith Hayes loan-number L-170 L-230 loan-number L-170 L-230 L-155  loan natural right outer join borrower

21 IS 230Lecture 6Slide 21 Joined Tables in SQL and Outer Joins (cont’d.)  loan full outer join borrower using (loan-number) branch-nameamount Downtown Redwood Perryridge null 3000 4000 1700 null customer-name Jones Smith null Hayes 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 loan-number L-170 L-230 L-260 L-155

22 IS 230Lecture 6Slide 22 Aggregate Functions in SQL  Used to summarize information from multiple tuples into a single-tuple summary  Grouping  Create subgroups of tuples before summarizing  Built-in aggregate functions  COUNT, SUM, MAX, MIN, and AVG  Functions can be used in the SELECT clause or in a HAVING clause

23 IS 230Lecture 6Slide 23 Aggregate Functions in SQL (cont’d.)  NULL values discarded when aggregate functions are applied to a particular column

24 IS 230Lecture 6Slide 24 Grouping: The GROUP BY and HAVING Clauses  Partition relation into subsets of tuples  Based on grouping attribute(s)  Apply function to each such group independently  GROUP BY clause  Specifies grouping attributes  If NULLs exist in grouping attribute  Separate group created for all tuples with a NULL value in grouping attribute

25 IS 230Lecture 6Slide 25 Grouping: The GROUP BY and HAVING Clauses (cont’d.)  HAVING clause  Provides a condition on the summary information

26 IS 230Lecture 6Slide 26 Discussion and Summary of SQL Queries

27 IS 230Lecture 6Slide 27 Specifying Constraints as Assertions and Actions as Triggers  CREATE ASSERTION  Specify additional types of constraints outside scope of built-in relational model constraints  CREATE TRIGGER  Specify automatic actions that database system will perform when certain events and conditions occur

28 IS 230Lecture 6Slide 28 Specifying General Constraints as Assertions in SQL  CREATE ASSERTION  Specify a query that selects any tuples that violate the desired condition  Use only in cases where it is not possible to use CHECK on attributes and domains

29 IS 230Lecture 6Slide 29 Introduction to Triggers in SQL  CREATE TRIGGER statement  Used to monitor the database  Typical trigger has three components:  Event(s)  Condition  Action

30 IS 230Lecture 6Slide 30 Introduction to Triggers in SQL CREATE TRIGGER SALARY_VIOLATION BEFORE INSERT OR UPDATE OF Salary, Supervisor_Ssn ON EMPLOYEE FOR EACH ROW WHEN (NEW.Salary > ( SELECT Salary FROM EMPLOYEE WHERE Ssn = NEW.Supervisor_Ssn)) INFORM_SUPERVISOR(NEW.Supervisor_Ssn, NEW. Ssn);

31 IS 230Lecture 6Slide 31 Views (Virtual Tables) in SQL  Concept of a view in SQL  Single table derived from other tables  Considered to be a virtual table

32 IS 230Lecture 6Slide 32 Specification of Views in SQL  CREATE VIEW command  Give table name, list of attribute names, and a query to specify the contents of the view

33 IS 230Lecture 6Slide 33 Views in SQL (cont)  DROP VIEW is used to delete the view.  SQL queries are used with the view in the same way as they are used with the base tables.  It is the responsibility of the DBMS to make the views always up to date.

34 IS 230Lecture 6Slide 34 Views in SQL (cont) View implementation and Update  Implementing the view efficiently is a complex task.  Two approaches have been proposed:  Query Modification: Rebuild the view every time a query is issued. Easy in implementation. Time consuming in processing especially in complex relations.  View Materialization: Materialize the view into a table and update this table according to updates on the base tables. Querying the view will get good performance.

35 IS 230Lecture 6Slide 35 Specification of Views in SQL (cont’d.)  Specify SQL queries on a view  View always up-to-date  Responsibility of the DBMS and not the user  DROP VIEW command  Dispose of a view

36 IS 230Lecture 6Slide 36 The DROP Command  DROP command  Dispose of a view  DROP VIEW command  Used to drop named schema elements, such as tables, domains, or constraint  Drop behavior options:  CASCADE and RESTRICT  Example:  DROP SCHEMA COMPANY CASCADE;

37 IS 230Lecture 6Slide 37 The ALTER Command  Alter table actions include:  Adding or dropping a column (attribute)  Changing a column definition  Adding or dropping table constraints  Example:  ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12);  To drop a column  Choose either CASCADE or RESTRICT

38 IS 230Lecture 6Slide 38 The ALTER Command (cont’d.)  Change constraints specified on a table  Add or drop a named constraint

39 IS 230Lecture 6Slide 39 Summary  Complex SQL:  Nested queries, joined tables, outer joins, aggregate functions, grouping  CREATE ASSERTION and CREATE TRIGGER  Views  Virtual or derived tables


Download ppt "IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook."

Similar presentations


Ads by Google