Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
COP-5725 Practice Exercises
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Chapter (7): Advanced SQL
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
Alter an existing Table Note: (a) Primary key: SID + Course_code.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
Database Management System Module 3:. Complex Constraints In this we specify complex integrity constraints included in SQL. It relates to integrity constraints.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
IC and Triggers in SQL. Find age of the youngest sailor with age
1 Lecture 11: Basic SQL, Integrity constraints
SQL Constraints and Triggers
1 Presentation by Irina Kogan. November 27, Presentation by Irina Kogan. November 27, 2000.
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Student(sid, name, addr, age, GPA) Class(dept, cnum, sec, unit, title, instructor) Enroll(sid, dept, cnum, sec) siddeptcnumsec 301CS CS AT00000.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
CSEN 5314 Quiz 3.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
1 times table 2 times table 3 times table 4 times table 5 times table
The Relational Model These slides are based on the slides of your text book.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 Translating E/R Diagrams into Relational Schemas.
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.
1 SQL: Constraints and Triggers Chapter 5,
Data Definition After this lecture, you should be able to:
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Database Management System Quiz 1. A company needs to store information about: the employees identified by EmpNo, Name, Salary and Phone; departments.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Constraints and Triggers Chapter 5,
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Concepts 2440: 180 Database Concepts Instructor:
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Assertions and Triggers in SQL
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
CSCI 4333 Database Design and Implementation – Exercise (2) Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Constraints Review. What is a constraint? Unique – forbids duplicate values Referencial – Foreign key Check Constraint – sets restrictions on data added.
Tables Learning Support
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Oracle Developer. Create Table SQL> create table catalog ( 2 cno varchar2(7), 3 ctitle varchar2(50), 4 primary key (cno)); Table created. SQL> describe.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
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.
MS SQL Create Table Cust USE jxc00 GO CREATE TABLE cust ( cust_id smallint IDENTITY(1,1) NOT NULL, cust_name char(10)
Times Tables.
CSCI 6315 Applied Database Systems – Exercise (1)
Referential Integrity
SQL: Structured Query Language (‘Sequel’)
The Relational Model Relational Data Model
CSCI 6315 Applied Database Systems – Exercise (3)
Referential Integrity
3 times tables.
6 times tables.
Presentation transcript:

Complex Integrity Constraints in SQL

Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER, age INTEGER, PRIMARY KEY (sid), CHECK (rating >= 1 AND rating <=10));

Example of Table Constraints Create TABLE Reserves (sid INTEGER, bid INTEGER, day DATE, FOREIGN KEY (sid) REFERENCES Sailors FOREIGN KEY (bid) REFERENCES Boats, CONSTRAINT noInterLakeRes CHECK (Interlake <> (SELECT B.bname FROM Boats B WHERE B.bid = Reserves.bid)));

Domain Constraints CREATE DOMAIN ratingval INTEGER DEFAULT 1 CHECK (VALUE >= 1 AND VALUE <=10) Source type: integer When creating table Sailors: ratingratingval Problem: CREATE DOMAIN SailorID INTEGER CREATE DOMAIN BoatID INTEGER Same source type: integer Fail to disallow the comparison between SailorID and BoatID

Distinct Types CREATE TYPE sidType AS INTEGER CREATE TYPE bidType AS INTEGER They are distinct from each other and from the source type.

ICs over Several Tables Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER, age INTEGER, PRIMARY KEY (sid), CHECK (rating >= 1 AND rating <=10) CHECK ((SELECT COUNT (S.sid) FROM Sailors S) + SELECT COUNT (B.bid) FROM Boats B) <100)) Problem: If the Sailors table is empty, this constraint is defined to always hold, which means the numbers of tuples in Boats can be anything.

Assertions CREATE ASSERTION smallClub CHECK ((SELECT COUNT (S.sid) FROM Sailors S) + SELECT COUNT (B.bid) FROM Boats B) <100) Advantage: not associated with either table. Another example: CREATE ASSERTION TotalPaticipate CHECK ((SELECT COUNT (S.sid) FROM Sailors S) = (SELECT COUNT (DISTINCT R.sid) FROM Reserves R))