Database Systems More SQL Database Design -- More SQL1.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Database Design -- Basic SQL
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
Introduction to SQL J.-S. Chou Assistant Professor.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Single-Table Queries
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Advanced SQL: Triggers & Assertions
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 7 More SQL: Complex Queries, Triggers, Views, and Schema Modification Slide 7- 1.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 7 SQL HUANG XUEHUA. Chapter Objectives Specification of more general constraints via assertions SQL facilities for defining views (virtual.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
CS580 Advanced Database Topics
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Advanced SQL: Views & Triggers
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 7 Introduction to Structured Query Language (SQL)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language (3)
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
CSC 453 Database Systems Lecture
SQL – Constraints & Triggers
Presentation transcript:

Database Systems More SQL Database Design -- More SQL1

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 Database Design -- More SQL2

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 Database Design -- More SQL3

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 Database Design -- More SQL4

Comparisons Involving NULL and Three-Valued Logic Database Design -- More SQL5

Comparisons Involving NULL and Three-Valued Logic SQL allows queries that check whether an attribute value is NULL – IS or IS NOT NULL Database Design -- More SQL6

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 Database Design -- More SQL7

Nested Queries Database Design -- More SQL8

Nested Queries Use tuples of values in comparisons – Place them within parentheses Database Design -- More SQL9

Nested Queries 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 ): >, >=, Database Design -- More SQL10

Nested Queries Avoid potential errors and ambiguities – Create tuple variables (aliases) for all tables referenced in SQL query Database Design -- More SQL11

Correlated Nested Queries Correlated nested query – Evaluated once for each tuple in the outer query These can be very slow Database Design -- More SQL12

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 Database Design -- More SQL13

Explicit Sets and Renaming of Attributes in SQL Can use explicit set of values in WHERE clause E. g. SELECT * FROM works_on where pno in (1,2,3) Use qualifier AS followed by desired new name – Rename any attribute that appears in the result of a query – Useful when you have ambiguous names Database Design -- More SQL14

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 Database Design -- More SQL15

Joined Tables in SQL and Outer Joins 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 Database Design -- More SQL16

Joined Tables in SQL and Outer Joins 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 These are fairly common, and slow Database Design -- More SQL17

Joined Tables in SQL and Outer Joins 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 Database Design -- More SQL18

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 Database Design -- More SQL19

Aggregate Functions in SQL NULL values discarded when aggregate functions are applied to a particular column Database Design -- More SQL20

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 Database Design -- More SQL21

Grouping: The GROUP BY and HAVING Clauses HAVING clause – Provides a condition on the summary information. That is, it applies to the data selected by the WHERE clause Database Design -- More SQL22

Summary of SQL Queries Database Design -- More SQL23

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 Database Design -- More SQL24

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 Database Design -- More SQL25

Introduction to Triggers in SQL CREATE TRIGGER statement – Used to monitor the database Typical trigger has three components: – Event(s) – Condition – Action Database Design -- More SQL26

Triggers 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) Database Design -- More SQL27

Views (Virtual Tables) in SQL Concept of a view in SQL – Single table derived from other tables – Considered to be a virtual table Database Design -- More SQL28

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 Database Design -- More SQL29

Specification of Views in SQL Specify SQL queries on a view (as if it were a table) View always up-to-date – Responsibility of the DBMS and not the user DROP VIEW command – Dispose of a view Database Design -- More SQL30

View Implementation, View Update, and Inline Views Complex problem of efficiently implementing a view for querying Query modificationapproach – Modify view query into a query on underlying base tables – Disadvantage: inefficient for views defined via complex queries that are time-consuming to execute Database Design -- More SQL31

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 Incremental update strategies – DBMS determines what new tuples must be inserted, deleted, or modified in a materialized view table Database Design -- More SQL32

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 Database Design -- More SQL33

View Update and Inline Views 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 Database Design -- More SQL34

Schema Change Statements in SQL Schema evolution commands – Can be done while the database is operational – Does not require recompilation of the database schema Database Design -- More SQL35

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; – This drops the entire company table Database Design -- More SQL36

SQL Injection Attack from XKCD.com Database Design -- More SQL37

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 Database Design -- More SQL38

The ALTER Command Change constraints specified on a table – Add or drop a named constraint Database Design -- More SQL39

Summary Complex SQL: – Nested queries, joined tables, outer joins, aggregate functions, grouping CREATE ASSERTION and CREATE TRIGGER Views – Virtual or derived tables Database Design -- More SQL40