Chapter 9. MORE SQL: Assertions, Views, and Programming Techniques. Constraints as Assertions. Assertions: An Example

Similar documents
SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

SQL- Updates, Asser0ons and Views

Assertions, Views, and Programming. CS157A Chris Pollett Oct. 31, 2005.

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

UNIT-V SQL Explain insert, delete and update statements in SQL with example.

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Ders # 7. Veri Bütünlüğü Programlama ve Güvenlik. From Elmasri/Navathe textbook Ch9,26 Sciore textbook, Ch 9-10

Views, assertions, and triggers

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Simple SQL Queries (contd.)

Slides by: Ms. Shree Jaswal

CT13 DATABASE MANAGEMENT SYSTEMS DEC 2015

More SQL: Complex Queries, Triggers, Views, and Schema Modification

INF1383 -Bancos de Dados

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

COSC344 Database Theory and Applications. COSC344 Lecture 15 1

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

CSC 261/461 Database Systems Lecture 6. Fall 2017

Chapter 13 Introduction to SQL Programming Techniques

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

More SQL: Complex Queries, Triggers, Views, and Schema Modification

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

CS5300 Database Systems

SQL STRUCTURED QUERY LANGUAGE

ITCS Implementation. Jing Yang 2010 Fall. Class 14: Introduction to SQL Programming Techniques (Ch13) Outline

CSC 742 Database Management Systems

Outline. Note 1. CSIE30600 Database Systems More SQL 2

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

ECE 650 Systems Programming & Engineering. Spring 2018

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

CSIE30600 Database Systems Basic SQL 2. Outline

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Extension to SQL: View, Triggers, Transaction. SS Chung

CS430 Final March 14, 2005

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

DATABASE DESIGN II - 1DL400

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

CS 338 Basic SQL Part II

HP NonStop Structured Query Language (SQL)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

ECE 650 Systems Programming & Engineering. Spring 2018

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

PHP Queries and HTML Forms Lecture 23

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

CS 338 Nested SQL Queries

Enhanced Data Models for Advanced Applications. Active Database Concepts

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Session Active Databases (2+3 of 3)

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 8: The Relational Algebra and The Relational Calculus

CSEN 501 CSEN501 - Databases I

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

WEEK 3 TERADATA EXERCISES GUIDE

Data Manipulation Language (DML)

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Database design process

Chapter 19 Query Optimization

The Relational Algebra

Relational Algebra 1

Introduction to the Structured Query Language [ SQL ] (Significant Concepts)

Querying Data with Transact-SQL (761)

Database Applications

A taxonomy of SQL queries Learning Plan

Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

CMP-3440 Database Systems

CS 348 Introduction to Database Management Assignment 2

CS2300: File Structures and Introduction to Database Systems

CIS611 Lab Assignment 1 SS Chung

Relational Database Management Systems for Epidemiologists: SQL Part II

Relational Model. CS 377: Database Systems

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

MTA Database Administrator Fundamentals Course

Essay Question: Explain 4 different means by which constrains are represented in the Conceptual Data Model (CDM).

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Subquery: There are basically three types of subqueries are:

Information Systems. Database System Architecture. Relational Databases. Nikolaj Popov

Database Management Systems,

Views. COSC 304 Introduction to Database Systems. Views and Security. Creating Views. Views Example. Removing Views.

Database Technology. Topic 6: Triggers and Stored Procedures

CSC Web Programming. Introduction to SQL

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Basic SQL. Basic SQL. Basic SQL

Oracle 9i Application Development and Tuning

The Relational Model and Relational Algebra

Transcription:

Chapter 9 MORE SQL: Assertions, Views, and Programming Techniques Constraints as Assertions General constraints: constraints that do not fit in the basic SQL categories Mechanism: CREATE ASSERTION components include: a constraint name, followed by CHECK, followed by a condition 2 Assertions: An Example The salary of an employee must not be greater than the salary of the manager of the department that the employee works for CREATE ASSERTION SALARY_CONSTRAINT CHECK (NOT EXISTS (SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D WHERE E.SALARY > M.SALARY AND E.DNO=D.NUMBER AND D.MGRSSN=M.SSN)) 3

4 Using General Assertions Specify a query that violates the condition; include inside a NOT EXISTS clause Query result must be empty if the query result is not empty, the assertion has been violated SQL Triggers Objective: to monitor a database and take action when a condition occurs Triggers are expressed in a syntax similar to assertions and include the following: event (e.g., an update operation) condition action (to be taken when the condition is satisfied) 5 SQL Triggers: An Example A trigger to compare an employee s salary to his/her supervisor during insert or update operations: CREATE TRIGGER INFORM_SUPERVISOR 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); 6

7 Views in SQL A view is a virtual table that is derived from other tables Allows for limited update operations (since the table may not physically be stored) Allows full query operations A convenience for expressing certain operations Specification of Views SQL command: CREATE VIEW Componenets: a table (view) name a possible list of attribute names (for example, when arithmetic operations are specified or when we want the names to be different from the attributes in the base relations) a query to specify the table contents 8 SQL Views: An Example Specify a different WORKS_ON table (with employee and project names) CREATE VIEW WORKS_ON_NEW AS SELECT FNAME, LNAME, PNAME, HOURS FROM EMPLOYEE, PROJECT, WORKS_ON WHERE SSN=ESSN AND PNO=PNUMBER GROUP BY PNAME; 9

10 SQL Views: An Example Specify a DEPT_INFO table with department names, number of employees, and their total salaries as a view CREATE VIEW DEPT_INFO (DEPT_NAME,NO_OF_EMPS,TOTAL_SAL) AS SELECT DNAME,COUNT(*),SUM(SALARY) FROM DEPARTMENT,EMPLOYEE WHERE DNUMBER=DNO GROUP BY DNAME; Using a Virtual Table We can specify SQL queries on a newly created table (view): SELECT FNAME, LNAME FROM WORKS_ON_NEW WHERE PNAME= Concordia ; When no longer needed, a view can be dropped: DROP VIEW WORKS_ON_NEW; 11 Efficient View Implementation Query modification: translate the view query into a query on the underlying base tables disadvantage: inefficient for views defined via complex queries (especially if additional queries are to be applied to the view within a short time period) 12

13 Efficient View Implementation View materialization: involves physically creating and keeping a temporary table assumption: other queries on the view will follow concerns: maintaining correspondence between the base table and the view when the base table is updated strategy: incremental update View Update Update on a single view without aggregate operations: update may map to an update on the underlying base table Views involving joins: an update may map to an update on the underlying base relations not always possible 14 Un-updatable Views Views defined using groups and aggregate functions are not updateable Views defined on multiple tables using joins are generally not updateable WITH CHECK OPTION: must be added to the definition of a view if the view is to be updated to allow check for updatability and to plan for an execution strategy 15

16 Database Programming Objective: to access a database from an application program (as opposed to interactive interfaces) Why? An interactive interface is convenient but not sufficient; a majority of database operations are made through application programs (increasingly through web applications) Database Programming Approaches Embedded commands: database commands are embedded in a general-purpose programming language Library of database functions: available to the host language for database calls; known as an API A brand new, full-fledged language (minimizes impedance mismatch) 17 Impedance Mismatch Incompatibilities between a host programming language and the database model, e.g., type mismatch and incompatibilities; requires a new binding for each language set vs. record-at-a-time processing need special iterators to loop over query results and manipulate individual values 18

19 Steps in Database Programming 1. Client program opens a connection to the database server 2. Client program submits queries to and/or updates the database 3. When database access is no longer needed, client program terminates the connection Embedded SQL Most SQL statements can be embedded in a general-purpose host programming language such as COBOL, C, Java An embedded SQL statement is distinguished from the host language statements by a tag such as EXEC SQL and a matching END-EXEC (or semicolon) shared variables (used in both languages) usually prefixed with a colon : in SQL 20