The document discusses advanced SQL features including more complex queries, triggers, views, and schema modifications. Complex queries allow features like nested queries, joins, outer joins, and aggregation. Triggers allow automatic actions to be performed in response to events. Views define virtual tables derived from other tables. Schema changes allow modifying the structure of tables and databases.
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
26 views
Module-3 Complex Queries
The document discusses advanced SQL features including more complex queries, triggers, views, and schema modifications. Complex queries allow features like nested queries, joins, outer joins, and aggregation. Triggers allow automatic actions to be performed in response to events. Views define virtual tables derived from other tables. Schema changes allow modifying the structure of tables and databases.
Chapter 5 Outline More Complex SQL Retrieval Queries Specifying Constraints as Assertions and Actions as Triggers Views (Virtual Tables) in SQL Schema Change Statements in SQL
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
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
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
Nested Queries (cont’d.) 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 <>
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
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
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
Joined Tables in SQL and Outer Joins (cont’d.) Specify different types of join NATURAL JOIN Various types of OUTER JOIN NATURAL JOIN on two relations R and S No join condition specified Implicit EQUIJOIN condition for each pair of attributes with same name from R and S
Joined Tables in SQL and Outer Joins (cont’d.) Inner join Default type of join in a joined table Tuple is included in the result only if a matching tuple exists in the other relation LEFT OUTER JOIN Every tuple in left table must appear in result If no matching tuple • Padded with NULL values for attributes of right table
Joined Tables in SQL and Outer Joins (cont’d.) RIGHT OUTER JOIN Every tuple in right table must appear in result If no matching tuple • Padded with NULL values for the attributes of left table FULL OUTER JOIN Can nest join specifications
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
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
Introduction to Triggers in SQL CREATE TRIGGER statement Used to monitor the database Typical trigger has three components: Event(s) Condition Action Syntax: CREATE TRIGGER trigger_name <BEFORE/AFTER INSERT/UPDATE/DELETE> on table_name for each row <Query>
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
View Implementation, View Update, and Inline Views Complex problem of efficiently implementing a view for querying Query modification approach Modify view query into a query on underlying base tables Disadvantage: inefficient for views defined via complex queries that are time-consuming to execute
View Implementation View materialization approach Physically create a temporary view table when the view is first queried Keep that table on the assumption that other queries on the view will follow Requires efficient strategy for automatically updating the view table when the base tables are updated
View Implementation (cont’d.) Incremental update strategies DBMS determines what new tuples must be inserted, deleted, or modified in a materialized view table
View Update and Inline Views Update on a view defined on a single table without any aggregate functions Can be mapped to an update on underlying base table View involving joins Often not possible for DBMS to determine which of the updates is intended
View Update and Inline Views (cont’d.) Clause WITH CHECK OPTION Must be added at the end of the view definition if a view is to be updated In-line view Defined in the FROM clause of an SQL query
Schema Change Statements in SQL Schema evolution commands Can be done while the database is operational Does not require recompilation of the database schema
The DROP Command DROP command Used to drop named schema elements, such as tables, domains, or constraint Drop behavior options: CASCADE and RESTRICT Example: DROP SCHEMA COMPANY CASCADE;