is 230lecture 6slide 1 lecture 7 advanced sql introduction to database systems is 230 this is the...
TRANSCRIPT
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 4
Comparisons Involving NULLand 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
IS 230 Lecture 6 Slide 6
Comparisons Involving NULLand Three-Valued Logic (cont’d.) SQL allows queries that check whether an
attribute value is NULL IS or IS NOT NULL
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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
WHERE SSN=ESSN
AND Lname=‘Smith’);
IS 230 Lecture 6 Slide 9
Nested Queries (cont’d.)
Use tuples of values in comparisons Place them within parentheses
IS 230 Lecture 6 Slide 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): >, >=, <, <=, and <>
Nested Queries (cont’d.)
IS 230 Lecture 6 Slide 11
Nested Queries (cont’d.)
056
(5< some ) = true
05
0
) = false
5
05(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 <comp> some r t r s.t. (F <comp> t)
Where <comp> can be:
IS 230 Lecture 6 Slide 12
Nested Queries (cont’d.) Definition of all Clause
F <comp> all r t r (F <comp> t)
056
(5< all ) = false
610
4
) = true
5
46(5 all ) = true (since 5 4 and 5 6)
(5< all
) = false(5 = all
( all) not inHowever, (= all) in
IS 230 Lecture 6 Slide 13
Nested Queries (cont’d.)
Avoid potential errors and ambiguities Create tuple variables (aliases) for all tables
referenced in SQL query
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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 joinleft outer joinright outer joinfull outer join
Join Conditions
naturalon <predicate>using (A1, A2, ..., An)
IS 230 Lecture 6 Slide 18
Joined Tables in SQL and Outer Joins (cont’d.)
Relation loan amount
3000
4000
1700
Relation borrower customer-name loan-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
IS 230 Lecture 6 Slide 19
Joined Tables in SQL and Outer Joins (cont’d.)
loan inner join borrower onloan.loan-number = borrower.loan-number
branch-name amount
Downtown
Redwood
3000
4000
customer-name loan-number
Jones
Smith
L-170
L-230
branch-name amount
Downtown
Redwood
Perryridge
3000
4000
1700
customer-name loan-number
Jones
Smith
null
L-170
L-230
null
loan left outer join borrower onloan.loan-number = borrower.loan-number
loan-number
L-170
L-230
loan-number
L-170
L-230
L-260
Also called theta join
IS 230 Lecture 6 Slide 20
Joined Tables in SQL and Outer Joins (cont’d.)
loan natural inner join borrower
branch-name amount
Downtown
Redwood
3000
4000
customer-name
Jones
Smith
branch-name amount
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
IS 230 Lecture 6 Slide 21
Joined Tables in SQL and Outer Joins (cont’d.)
loan full outer join borrower using (loan-number)
branch-name amount
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-namefrom (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
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 23
Aggregate Functions in SQL (cont’d.)
NULL values discarded when aggregate functions are applied to a particular column
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 25
Grouping: The GROUP BY and HAVING Clauses (cont’d.)
HAVING clause Provides a condition on the summary
information
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 29
Introduction to Triggers in SQL
CREATE TRIGGER statement Used to monitor the database
Typical trigger has three components: Event(s) Condition Action
IS 230 Lecture 6 Slide 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);
IS 230 Lecture 6 Slide 31
Views (Virtual Tables) in SQL
Concept of a view in SQL Single table derived from other tables Considered to be a virtual table
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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.
IS 230 Lecture 6 Slide 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.
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 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;
IS 230 Lecture 6 Slide 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
IS 230 Lecture 6 Slide 38
The ALTER Command (cont’d.)
Change constraints specified on a table Add or drop a named constraint