Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.

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 Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Referential Integrity & Foreign Keys Objectives of the Lecture : To consider Referential Integrity & Foreign Keys; To consider Referential Integrity Constraints.
Database Management System Module 3:. Complex Constraints In this we specify complex integrity constraints included in SQL. It relates to integrity constraints.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
THE RELATIONAL DATABASE MODEL & THE DATABASE DEVELOPMENT PROCESS Fact of the Week: According to a Gartner study in ‘06, Microsoft SQL server had the highest.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
Security and Integrity
Objectives of the Lecture :
CSC271 Database Systems Lecture # 6. Summary: Previous Lecture  Relational model terminology  Mathematical relations  Database relations  Properties.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
The Relational Model These slides are based on the slides of your text book.
Chapter 4 The Relational Model.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Chapter 9 Integrity. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs.
RDBMSSection Relational DBMS DATABASE DEVELOPMENT And Franchise Colleges By MANSHA NAWAZ.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
The Relational Database Model & The Database Development Process
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
Further Consolidation Objectives of the Lecture : To use simple queries to check out a DB. To use insertions, deletions and amendments to maintain a DB.
Consolidation Objectives of the Lecture : To review a given design of a relational database. To amplify the design by adding Integrity Constraints. To.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Foreign Keys Local and Global Constraints Triggers
CPSC-608 Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CMSC-461 Database Management Systems
SQL-1 Week 8-9.
SQL – Constraints & Triggers
Presentation transcript:

Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects of integrity constraints in SQL.

Why Ad Hoc Constraints ? l These are other constraints on values held in a relation. l They need to be applied to ensure the database holds accurate data, but don’t fit into any of the previous categories. l They usually fall into two classes : 1. To ensure physical reality applies. Example : weights are always positive. 2.To ensure Business Rules are kept - this reflects the way the organisation works. Example : it is company policy to only buy cars with an engine capacity of less than 2 litres.

The nature of Ad Hoc Constraints l In general, an integrity constraint is a limitation on the permissible values that a DB relation can hold. l It could be expressed as : IF a tuple appears in a DB relation THEN that tuple satisfies a certain condition. l The condition must always be a binary condition, i.e. one that evaluates to true or false. l Attribute type, candidate key, and referential integrity constraints are special cases of integrity constraints. l An ad hoc constraint is one where the binary condition is directly provided. A tuple can be accepted in a relation only if the binary condition is applied to the tuple and the result evaluates to true. l In fact, all integrity constraints must be met before a tuple is acceptable in a relation.

Ad Hoc Constraints in SQL l They are applied using the Check option. l The format is Check ( condition ) l A whole variety of conditions can be inserted. l Just as with candidate and foreign key constraints, ad hoc constraints (Check constraints) can be part of an attribute sub-statement OR in a separate sub-statement at the end of the Create Table statement. l Likewise ad hoc constraints (Check constraints) have integrity constraint names, that are either supplied by the user with the prefix Constraint, or by the DBMS as a default.

Example Check Conditions (1) l Constraining the values in a relation about products. Create Table PRODUCT ( ProdnoChar(5) Primary Key, WeightNumber Check ( Weight > 0 ), PriceNumber Check ( Price > 0 ), QuantityNumber, Constraint Value Check( Quantity * Price < ) ); Ensuring Physical Reality. Applying a Business Rule about the maximum permissible stock. Applying a Business Rule !

Example Check Conditions (2) l Put more complex constraints on the product relation instead. Create Table PRODUCT ( ProdnoChar(5) Primary Key, WeightNumber Check ( Weight > 0 And Not (Weight > 100) ), PriceNumber Check ( (Price > 0 And Price 50 And Price 250 And Price < 320) ), QuantityNumber, Constraint Value Check( Quantity * Price < ) );

SQL Assertions l An SQL Assertion is essentially an SQL Check constraint that can be specified independently of any Create Table or Alter Table statement. Format : Create Assertion ASSERTION_NAME Check ( condition ) (There is also a corresponding Drop Assertion ASSERTION_NAME statement). l Although an assertion can apply to one relation, its main purpose is to apply an integrity constraint that applies jointly to 2, 3 or more relations, a sort of “referential integrity ad hoc constraint”. It is good practice to limit its use to constraints applicable jointly to 2 or more relations. l Typically the condition uses an SQL query (that must evaluate to true or false).

Multiple Integrity Constraints l Although not illustrated yet, it is possible and sometimes desirable in SQL to assign more than one integrity constraint in an attribute sub-statement. Example : Consider a different version of the PRODUCT relation. Create Table PRODUCT ( ProdnoChar(5) Primary Key, WeightNumber Constraint WT_NOT_NULL Not Null Constraint POS_WT Check ( Weight > 0 ) Constraint WT_REF References LIST_WEIGHTS (Wts), PriceNumber, QuantityNumber, );

Limitations of Ad Hoc Constraints l Suppose the relation EMPLOYEE were to contain an attribute that holds the ages of employees, and there are age limits on who can be employed (to meet legal requirements). l To reflect this, the following ad hoc constraint could be added to the Age attribute : Check ( Age >= 16 And Age <= 65 ) l However it may be legal and normal to employ school children for some part-time jobs, and retired people for some part-time consultancy that utilises their experience. Now both comparisons in the ad hoc constraint are inappropriate. What should replace them ? l The problem arises because the boundary conditions of legal age limits are fuzzy. There is no simple solution to this. Fuzzy limits can be difficult to handle and judgement must be used.

Limitations of Integrity Constraints l Suppose the following tuple were to appear in the EMPLOYEE relation : (‘E2’, ‘Fenwick’, ‘M’, 40000) Assume this tuple satisfies all the integrity constraints, which are properly specified. l If the company has no employee called “Fenwick”, then the DB still has false data in it, despite all the integrity constraints. l In this example, no integrity constraint can possibly check which of all the names that are permissible in the DB actually correspond to current employees (& hence are valid). Conclusion : the integrity constraints form a limited approximation to the real world constraints that would ideally apply. Additional validation is often required, typically provided by human intervention or some specially designed input system. Note : the Closed World Assumption applies to DBs. Any data in the DB must be true; any not in it must be false.

The following example was used earlier to illustrate the derivation of specific data types from underlying types :- Create Table EMPLOYEE ( ENoChar(2), ENameVarchar2(30), M-SChar(1) Check( M-S In (‘S’, ‘M’, ‘W’, ‘D’ ) ), SalNumber Check( Sal >999 And Sal < ) ) ; Ad Hoc Constraints & Specific Data Types Previously CHECK was used to derive specific data types. But these 2 CHECK conditions could equally well be regarded as applying ad hoc constraints.

SQL Check Constraints l Conceptually, specific data types and ad hoc constraints are different. l However the CHECK option is used for both in SQL. If the parameters to a data type - e.g. CHAR(1) - don’t narrow the underlying type down sufficiently to achieve the specific data type, then using CHECK may be appropriate. l Some integrity constraints can validly be considered as either specific data types or ad hoc constraints. Example : ensuring “Weight” in the PRODUCT relation is a positive value can be considered from either viewpoint.

SQL Default Values l It is possible to specify that an attribute has a default value. This means that : IF a tuple is to be put into a relation AND IF no value is specified for that attribute in the tuple THEN the attribute is given the default value in that tuple. l Default values are not integrity constraints. They are mentioned here because they are useful in helping to make sure that attribute values do satisfy the integrity constraints. l SQL syntax for defaults is : Default default_value Example : Create Table EMPLOYEE ( ENameVarchar2(30), M-SChar(1) Default ‘S’ Check( M-S In (‘S’, ‘M’, ‘W’, ‘D’ ) ) ) ;

SQL Alter Table l It can be useful, or even necessary, to alter relations some time after they have been created. l SQL provides the Alter Table command for this. l Alter Table provides the following ways of changing a relation : l Add or drop an attribute; an additional attribute may also have integrity constraints assigned to it, using the same syntax as for Create Table. l Add or drop an existing integrity constraint; again adding integrity constraints uses the same syntax as for Create Table; dropping an existing integrity constraint requires referring to it by name. l Set or drop a default value for an existing attribute.

Example Alter Table Statements Alter Table EMPLOYEE Add Column Sex CHAR(1) Check (Sex = ‘M’ Or Sex = ‘F’) Set Default ‘M’ ; Alter Table EMPLOYEE Alter Column Sex Drop Default ; Alter Table EMPLOYEE Add Constraint SENSIBLE_SAL Check (Sal > 5000 And Sal < 50000) ; Use default value to ensure new column gets a valid value that is most frequently occurring. Remove default value as it is now no longer needed.