SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Similar documents
SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Slides by: Ms. Shree Jaswal

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

CS W Introduction to Databases Spring Computer Science Department Columbia University

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

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

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

SQL Introduction. CS 377: Database Systems

SQL STRUCTURED QUERY LANGUAGE

SQL- Updates, Asser0ons and Views

CS 348 Introduction to Database Management Assignment 2

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

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

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

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

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

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

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

Enhanced Data Models for Advanced Applications. Active Database Concepts

Session Active Databases (2+3 of 3)

Chapter 19 Query Optimization

CSEN 501 CSEN501 - Databases I

Database design process

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

CSC 742 Database Management Systems

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

Relational Calculus: 1

Relational Database Systems Part 01. Karine Reis Ferreira

ECE 650 Systems Programming & Engineering. Spring 2018

SQL DATA DEFINITION LANGUAGE

ECE 650 Systems Programming & Engineering. Spring 2018

Integrity Constraints (Reminder)

SQL Constraints and Triggers. Week 11

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

CS2300: File Structures and Introduction to Database Systems

Overview Relational data model

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

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

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

A taxonomy of SQL queries Learning Plan

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

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Chapter 3. Algorithms for Query Processing and Optimization

Intermediate SQL ( )

SQL: Data Definition Language

Basic SQL. Basic SQL. Basic SQL

Simple SQL Queries (contd.)

DATABASE CONCEPTS. Dr. Awad Khalil Computer Science & Engineering Department AUC

CT13 DATABASE MANAGEMENT SYSTEMS DEC 2015

Outline. Note 1. CSIE30600 Database Systems More SQL 2

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

Part 1 on Table Function

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

DATABASE DESIGN II - 1DL400

DBMS LAB SESSION PAVANKUMAR MP

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

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

CS5300 Database Systems

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

SQL: Queries, Constraints, Triggers (iii)

Database Technology. Topic 3: SQL. Olaf Hartig.

COSC Assignment 2

CSIE30600 Database Systems Basic SQL 2. Outline

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

Relational Model. CS 377: Database Systems

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

The Relational Model

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

COSC344 Database Theory and Applications. COSC344 Lecture 15 1

The Relational Model of Data (ii)

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Data Manipulation (DML) and Data Definition (DDL)

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

Database Systems External Sorting and Query Optimization. A.R. Hurson 323 CS Building

Homework #4 1. Suppose that each of the following Update operations is applied directly to the database state shown in Figure 5.6.

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

CIS611 Lab Assignment 1 SS Chung

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

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

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Data Definition Language (DDL)

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

CSC 261/461 Database Systems Lecture 6. Fall 2017

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

Integrity and Security

CS 338 Nested SQL Queries

Transcription:

SQL: A COMMERCIAL DATABASE LANGUAGE Complex Constraints

Outline 1. Introduction 2. Data Definition, Basic Constraints, and Schema Changes 3. Basic Queries 4. More complex Queries 5. Aggregate Functions and Grouping 6. Summary of SQL queries 7. Data Change statements 8. Views 9. Complex Constraints 10. Database Programming

9. Complex constraints (0) Suppose that we want to express the constraint: every PROJECT has at least one employee working on it. This constraint cannot be expressed by defining the attribute PNUMBER of PROJECT as a foreign key that refers to attribute PNO of WORKS_ON (why?). This type of constraint is called inclusion dependency and generalizes foreign key constraints. This type of constraint and othetr complex constraints can be expressed using CHECK constraints and ASSERTIONS n SQL.

9. Complex constraints (1) In SQL, users can specify more general constraints via declarative assertions, using the CREATE ASSERTION statement of the DDL. Each assertion is given a constraint name, and is specified via a condition similar to the WHERE clause of an SQL query. Example: The constraint the salary of an employee must not be greater than the salary of the manager of the department that the employee works for can be specified as follows: CREATE ASSERTION SAL_CONST CHECK (NOT EXISTS (SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D WHERE E.SALARY > M.SALARY AND E.DNO = D.DNUMBER AND D.MGRSSN = M.SSN) );

9. Complex constraints (2) The constraint is violated whenever some tuples in the database cause the condition of the assertion statement to evaluate to FALSE. The constraint is satisfied by a database instance if no combination of tuples in the database instance violates the constraint. The constraint name can be used later to refer to the constraint or drop it. The DBMS is responsible for ensuring that the condition is not violated. Any WHERE clause can be used, but many constraints can be specified using the EXISTS and NOT EXISTS style of conditions.

9. Complex constraints (3) The CHECK clause can also be attached to an attribute declaration in a CREATE TABLE statement (attribute-based CHECK). The condition may refer to the attribute being constraint by using the name of the attribute in its expression. An attribute-based CHECK constraint is checked whenever any tuple gets a new value for this attribute. If the constraint is violated by the new value, then the modification is rejected.

9. Complex constraints (4) Example: The declaration of DNUMBER attribute of relation DEPARTMENT could be: DNUMBER INT NOT NULL CHECK (DNUMBER <=20), The declaration of the SEX attribute of relation EMPLOYEE could be: SEX CHAR CHECK (SEX = F OR SEX = M )

9. Complex constraints (5) To declare a constraint on the tuples of a single table, when we define that table with a CREATE TABLE statement, we may add to the list of attributes and key or foreign key declarations one or more CHECK clauses (Tuple-Based CHECK constraint). The condition can be anything that could appear in a WHERE clause. The condition of a tuple-based CHECK constraint is checked every time a tuple is inserted into the relation or a tuple of the relation is updated. If the condition is false for that tuple, the insertion or update is rejected.

9. Complex constraints (6) Example: To enforce the constraint that a department cannot have more than 50 employees we can add the following CHECK clause to the definition of EMPLOYEE relation. CHECK (NOT EXISTS (SELECT DNO, COUNT(*) FROM EMPLOYEE GROUP BY DNO HAVING COUNT(*) > 50))

9. Complex constraints (7) Example: Alternatively, in order to enforce the constraint that a department cannot have more than 50 employees we can add the following CHECK clause to the definition of EMPLOYEE relation. CHECK (50 >= ALL (SELECT FROM GROUP BY COUNT(*) EMPLOYEE DNO) Observe that here: - we do not used NOT EXISTS. - the grouping attribute (DNO) does not appear in the SELECT clause. - we use the keyword ALL to compare 50 to the number of employees in every group.

9. Complex constraints (8) Example: Suppose that the DEPARTMENT relation has an additional attribute DEPT_CREATE_DATE to record the date when the department was created. We can add the following constraint at the end of the CREATE TABLE DEPARTMENT statement to ensure that a manager s start date is later that the department s creation date. CONSTRAINT DATES CHECK (DEPT_CREATE_DATE < MGRSTARTDATE)

9. Complex constraints (9) The CHECK clause can also be used in conjunction with the CREATE domain statement to specify constraints on a particular domain. Example: To restrict the values of department numbers to an integer number between 1 and 20 we write: CREATE DOMAIN D_NUM AS INTEGER CHECK (D_NUM > 0 AND D_NUM < 21);

9. Complex constraints (10) Table constraints vs. Assertions Table constraints (attribute based and tuple based constraints) are required to hold only if the associated table is not empty. A table constraint is checked in SQL only when the associated table is modified (insertion, deletion or update of a tuple). Table constraint checking can be implemented more efficiently by the DBMS. Assertions are not associated with any one table. A tuple-based constraint can involve more than one table. In this case it is better to create an assertion.

9.1. DEFERRING INTEGRITY CONSTRAINT CHECK (0) Recall circular foreign keys and the Chicken and Egg Problem. This problem appeared both when we were trying to define tables and when we tried to insert tuples in these tables. These problems can be addressed by deferring the checking of the integrity constraints.

9.2. DEFERRING INTEGRITY CONSTRAINT CHECK (1) A transaction consists of a sequence of query and/or update statements. The SQL standard specifies that a transaction begins implicitly when an SQL statement is executed. The intermediate states produced by a database transaction might be inconsistent (they might temporarily violate integrity constraints). What is important thought is that the constraints are satisfied when the transaction commits. To accommodate the possibility of temporary constraint violations, SQL allows the programmer to specify the mode of a particular constraint to be immediate or deferred. Immediate: a check is made after each SQL statement that changes the database. Deferred: a check is made only when a transaction commits.

9.3. DEFERRING INTEGRITY CONSTRAINT CHECK (2) When a constraint is initially defined, it can be specified with options. The SQL standard allows a clause to be added to a constraint specification. For example, check constraints conform to the rule: [CONSTRAINT constraint-name] CHECK conditional-expression [ { INITIALLY DEFERRED INITIALLY IMMEDIATE}] [ { DEFERABLE NOT DEFERRABLE } ]

9.4. DEFERRING INTEGRITY CONSTRAINT CHECK (3) The first option gives the initial mode of the constraint. If INITIALLY DEFERRED is specified, the constraint is checked in the deferred mode until the mode is changed by an explicit SET CONSTRAINTS statement. The second option tells whether or not the constraint can be deferred by a subsequent SET CONSTRAINTS statement. DEFERRABLE means that the constraint is checked immediately by default, but can be deferred when desired. It can be at immediate or deferred mode at different times. INITIALLY DEFERRED and NOT DEFERRABLE are considered contradictory and cannot be specified together.

9.5. DEFERRING INTEGRITY CONSTRAINT CHECK (4) The mode switch of a DEFERRABLE constraint is set with the following statement in a transaction. SET CONSTRAINTS {constraint-list ALL} {DEFERRED IMMEDIATE} where constraint-list is a list of constraint names given in constraint statements. For constraints declared as deferrable, executing the statement SET CONSTRAINTS constraint-list DEFERRED as part of a transaction causes the checking of the specified constraints to be deferred at the end of the transaction.

9.6. DEFERRING INTEGRITY CONSTRAINT CHECK (5) However, be aware that the default behavior is to check constraints immediately, and many database implementations do not support deferred constraint checking.