Advanced Constraints SQL. by Joe Celko copyright 2007

Similar documents
SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

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

An Introduction to Structured Query Language

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

SQL: Data Definition Language

Slicing and Dicing Data in CF and SQL: Part 2

Constraints. Local and Global Constraints Triggers

user specifies what is wanted, not how to find it

Principles of Data Management

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

Databases 1. Defining Tables, Constraints

An Introduction to Structured Query Language

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

The Relational Model of Data (ii)

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

An Introduction to Structured Query Language

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

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Where are we? Week -4: Data definition (Creation of the schema) Week -3: Data definition (Triggers) Week -1: Transactions and concurrency in ORACLE.

Chapter 8 INTEGRITY 1

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

Midterm Review. Winter Lecture 13

3 February 2011 CSE-3421M Test #1 p. 1 of 14. CSE-3421M Test #1. Design

Summary Modifications. Database Construction (and Usage) Explicit attribute lists. Insertions with queries. Quiz. Default values

An Introduction to Structured Query Language

An Introduction to Structured Query Language

From E/R Diagrams to Relations

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

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

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

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

Oracle Create Table Foreign Key On Delete No

Data Manipulation (DML) and Data Definition (DDL)

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Slides by: Ms. Shree Jaswal

The attendee will get a deep dive into all the DDL changes needed in order to exploit DB2 V10 Temporal tables as well as the limitations.

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Chapter 1 SQL and Data

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

CSE 530A. Inheritance and Partitioning. Washington University Fall 2013

SQL Development Whitepaper I DO DECLARE BY JOE CELKO

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

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

CS2300: File Structures and Introduction to Database Systems

DB Creation with SQL DDL

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Structured Query Language (SQL)

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Full file at

The Relational Model

SQL: DDL. John Ortiz Cs.utsa.edu

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

Chapter 3: Introduction to SQL

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

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

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

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

MySQL Workshop. Scott D. Anderson

The Relational Model 2. Week 3

1) Introduction to SQL

14 October 2015 EECS-3421A Test #1 p. 1 of 14. EECS-3421A Test #1. Design

Data Definition Guide InterBase 2009

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

SQL Data Definition Language

The SQL data-definition language (DDL) allows defining :

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Lecture 04: SQL. Wednesday, October 4, 2006

Lecture 07. Spring 2018 Borough of Manhattan Community College

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Lab # 4. Data Definition Language (DDL)

CSIE30600 Database Systems Basic SQL 2. Outline

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

The Relational Model. Chapter 3

CS W Introduction to Databases Spring Computer Science Department Columbia University

BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

Constraints and Triggers

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Db2 Alter Table Alter Column Set Data Type Char

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

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Chapter 3: Introduction to SQL

Course Outline and Objectives: Database Programming with SQL

Introduction to Computer Science and Business

Transcription:

Advanced Constraints SQL by Joe Celko copyright 2007

Abstract The talk is a short overview of the options a programmer to use DDL (Data Declaration Language) in SQL to enforce a wide range of business rules

Biography Joe Celko Author and Consultant Joe Celko is an independent consultant and author living in Austin, Texas. He is the author of six books on SQL for Morgan-Kaufmann and was a member of the ANSI X3H2 Database Standards Committee from 1987 to 1997. He has written over 750 columns in the computer trade and academic press, mostly dealing with data and databases.

DDL is underused There are a lot of things that can be done with DDL to enforce business rules But people don t do it Programmers still think a column is a field and do not understand the power they have File layouts are easier to write in a hurry than good DDL Remember that a smart optimizer will put all these constraints into the execution plan! Constraints become predicates TRIGGERs and STORED PROCEDUREs tell the optimizer nothing

CREATE TABLE Statement CREATE TABLE <name> (<columns> [<constraints>]) Schema is created all at once This can mean circular references Table names are unique in a schema DBA can CREATE, DROP or ALTER tables Users cannot change database structures Users INSERT, UPDATE and DELETE data in the tables The constraints restrict what they can do at the database level Files have no restrictions

Column Definitions CREATE TABLE <name> (<column list> [<constraints>], ); <Column name> <datatype> [constraints] Column name is unique in table But should be used in other table Data elements should have one and only one name in the schema Constraints reference columns in the table where they are declared in most products In full SQL-92, they can also reference other tables or views

Column Constraints NOT NULL - column only DEFAULT <constant> - column only CHECK ( <search condition>) column, table or schema level UNIQUE and PRIMARY KEY table level REFERENCES Clause - schema level Multi-column forms of some of these constraints

NOT NULL Constraint Says this column cannot have a NULL in it Use this constraint automatically, then change it when you have a good reason Historical reasons this is not automatic Try not to use it Look for a natural default value in the domain of the column If a column is NULL-able, then explain what a NULL means in that column If it can have more than one meaning, you are in trouble You need to know WHY the missing value is missing!

DEFAULT Clause DEFAULT <constant or system call> System calls are CURRENT_TIMESTAMP, CURRENT_USER, etc Specifies a value for INSERT INTO statements when no value is given The default DEFAULT is NULL, unless declared as NOT NULL Can be used in UPDATE statements explicitly Make sure the datatypes match without a forced conversion INTEGER NOT NULL DEFAULT 0 INTEGER NOT NULL DEFAULT 0.00 conversion! CHAR(n) and VARCHAR(n)

CHECK ( ) Constraint -1 CHECK (<search condition >) References any column in the same row All rows in the table must test TRUE or UNKNOWN for the Boolean expression big difference from the WHERE clause! Full SQL-92 allows you to reference other tables in schema, but this is not yet common Full SQL-92 also has CREATE ASSERTION statements A CHECK() constraint outside the tables, at the database schema level All constraints are true inside an empty table This is the place for business rules

CHECK ( ) Constraint -2 Any predicate can go into a CHECK() constraint CHECK (sex IN (0,1,2,9)) CHECK (birthdate < CURRENT_TIMESTAMP) CHECK (IQ >= 0) CHECK (birthdate < hire_date) CHECK (partcode LIKE P-% ) CHECK (x BETWEEN 0 and 42) Remember that a smart optimizer will put all these constraints into the execution plan TRIGGERs and STORED PROCEDUREs tell the optimizer nothing

CHECK ( ) Constraint -3 CHECK() constraints can be given names which will appear in error messages. The syntax is simply CONSTRAINT <name> CHECK (<search condition>) The <name> is global, not local to the table. You can write a monster predicate in one CHECK() clause, but then you have to figure out which part of the monster predicate is causing the error. Better to have several short, well named constraints

CHECK ( ) Constraint -4 CHECK() constraints can use CASE expressions for complex logic CHECK (CASE WHEN x =1 THEN T WHEN x =2 AND y=1 THEN F ELSE F END = T ) This is where you put business rules in a table.

CHECK ( ) Constraint -5 While not widely available, you can reference the table itself in subquery predicates CHECK ((SELECT MAX(seq) FROM Foobar) = (SELECT COUNT(*) FROM Foobar) CHECK (NOT EXISTS (SELECT * FROM Personnel GROUP By dept HAVING COUNT(*) >= 100)) All constraints are TRUE for an empty table

Constant Tables -1 Trick: table to store physical constants, or other readonly data CREATE TABLE PhysicalConstants (keycol CHAR(1) NOT NULL PRIMARY KEY DEFAULT X CHECK(keycol = X ), pi FLOAT NOT NULL DEFAULT 3.141592653, e FLOAT NOT NULL DEFAULT 2.718282, ); INSERT INTO PhysicalConstants DEFAULTS makes the defaults appear CHECK() allows only one row

Constant Tables -2 SQL-92 Version! CREATE VIEW PhysicalConstants (pi, e,..) VALUES ( CAST (3.141592653 AS FLOAT), CAST(2.718282 AS FLOAT), ); The CAST() forces the data type

Transition Constraints -1 Transition constraints can be done with an Auxiliary table Can include duration between state changes and other data CREATE TABLE Transitions (prior_state INTEGER NOT NULL, current_state INTEGER NOT NULL, PRIMARY KEY (prior_state, current_state), etc.); CREATE TABLE Process (.., FOREIGN KEY (prior_state, current_state) REFERENCES Transitions (prior_state, current_state),..);

Transition Constraints -2 Born Married Dead Divorced

Transition Constraints -3 (Born, Born) -- initial state (Born, Dead) (Born, Married) (Married, Divorced) (Married, Dead) (Divorced, Married) (Divorced, Dead) (Dead, Dead) -- terminal state

UNIQUE Constraints -1 Two forms: PRIMARY KEY() and UNIQUE() Zero or one PRIMARY KEY per table No key means it is not really a table PRIMARY KEY(<column list>) is automatically both UNIQUE and NOT NULL UNIQUE (<column list>) can have NULLs in the column Not usually a good idea. Both are used by the REFERENCES constraint (later) A table can have more than one UNIQUE constraint and they can overlap each other

UNIQUE Constraints -2 CREATE TABLE Marriage (husband CHAR(15) NOT NULL UNIQUE, wife CHAR(15) NOT NULL UNIQUE); CREATE TABLE Polygamy (husband CHAR(15) NOT NULL, wife CHAR(15) NOT NULL UNIQUE);

UNIQUE Constraints -3 CREATE TABLE Polyandry (husband CHAR(15) NOT NULL, wife CHAR(15) NOT NULL UNIQUE); CREATE TABLE CorporateMarriages (husband CHAR(15) NOT NULL, wife CHAR(15) NOT NULL, PRIMARY KEY(husband, wife));

UNIQUE Constraints -4 Example:teacher's schedule CREATE TABLE Schedule (teacher VARCHAR(15) NOT NULL, class CHAR(15) NOT NULL, room INTEGER NOT NULL, period INTEGER NOT NULL, PRIMARY KEY (teacher, class, room, period)); That primary key is the most obvious one -- use all the columns

UNIQUE Constraints -5 The rules we want to enforce are: 1) A teacher is in only one room each period 2) A teacher teaches only one class each period 3) A room has only one class each period 4) A room has only one teacher in it each period

UNIQUE Constraints -7 CREATE TABLE Schedule_1 -- version one, wrong! ( UNIQUE (teacher, room, period), -- rule #1 UNIQUE (teacher, class, period), -- rule #2 UNIQUE (class, room, period), -- rule #3 UNIQUE (teacher, room, period), -- rule #4 PRIMARY KEY (teacher, class, room, period)); Insert three rows and give me a bad 6th period ('Celko', 'Database 101', 222, 6) ('Celko', 'Database 102', 223, 6) ('Ms Shields', 'Database 101', 223, 6)

UNIQUE Constraints -8 CREATE TABLE Schedule_2 -- corrected version ( UNIQUE (teacher, period), -- rules #1 and #2 UNIQUE (room, period)); -- rules #3 and #4 If a teacher is in only one room each period, then given a period and a teacher I should be able to determine only one room If a teacher teaches only one class each period, then class is dependent upon the combination of teacher and period The same logic holds for the last two rules: class is dependent upon the combination of room and period, and teacher is dependent upon the combination of room and period

REFERENCES Clause -1 Single or multi-column syntax <column declaration> REFERENCES <table 2>[(column list)], FOREIGN KEY <column list> REFERENCES <table 2>[(column list)] The column(s) in this table has matching value(s) in <table 2> The default referenced column(s) list is the PRIMARY KEY in <table 2> You can reference any UNIQUE constraint s column list

REFERENCES Clause -2 REFERENCES clause can also have actions ON DELETE [NO ACTION CASCADE SET NULL SET DEFAULT] ON UPDATE [NO ACTION CASCADE SET NULL SET DEFAULT] When the referenced table is changed, this option will take action in the referencing table

REFERENCES Clause -3 You can cascade all over the schema, which can be very handy a lot of work that would have been in application code is now moved to the database Business rules are in one place You can cascade all over the schema, which can be very dangerous It can take a lot of time to execute and lock the database If you have circular references, you can loop forever If you did not know what you were doing, it s too late now

REFERENCES Clause -4 Good rule is to avoid any circular references This also means selfreferences Change to A causes change to B and to C A B Change to B causes change to C Does C now reflect the actions of A or B? C

CREATE DOMAIN Statement CREATE DOMAIN <domain name> AS <datatype> <constraints> Part of SQL-92, but not widely implemented Might see vendor version of user defined data types (UDT) It is a column declaration Macro It does not define new operators on the domain You use it in CREATE TABLE statements like a datatype on a column declaration The ALTER DOMAIN statement allows you to change all the occurrences in the entire schema Example: ten digit UPC codes are going to be replaced by a 15 digit GTIN code

Classes in SQL CREATE TABLE Class (item_key INTEGER NOT NULL PRIMARY KEY sub_class CHAR(1) NOT NULL CHECK(sub_class IN ( A, B, C ), ); CREATE TABLE SubClassA (item_key INTEGER NOT NULL, sub_class CHAR(1) NOT NULL CHECK(sub_class = A ), PRIMARY KEY (item_key, sub_class), FOREIGN KEY (item_key, sub_class) REFERENCES Class (item_key, sub_class), ); You can also use an optional UNIQUE() on item_key to get an index

Using VIEWS for Table Constriants Technically, a CHECK() can reference any table in the schema with any predicate CREATE TABLE Foobar (.. CONSTRAINT only_ten_members CHECK ((SELECT COUNT(*) FROM Foobar) <= 10),..); Most products do not have this feature yet. Fake it with a VIEW..WITH CHECK OPTION CREATE VIEW NewFoobar AS SELECT * FROM Foobar WHERE (SELECT COUNT(*) FROM Foobar) <= 10 WITH CHECK OPTION;

Summary Programmers still think in procedural code CHECK() constraints are good You can use tables and DRI actions to build declarative constraints Most bad DML (queries) are attempts to repair bad DDL

Questions & Answers?