Student(sid, name, addr, age, GPA) Class(dept, cnum, sec, unit, title, instructor) Enroll(sid, dept, cnum, sec) siddeptcnumsec 301CS11201 999CS14301 401AT00000.

Slides:



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

1 Constraints, Triggers and Active Databases Chapter 9.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
Jennifer Widom Constraints & Triggers Triggers – Introduction.
SQL Constraints and Triggers
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
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,
Basic SQL types String –Char(n): fixed length. Padded –Varchar(n): variable length Number –Integer: 32 bit –Decimal(5,2): –Real, Double: 32 bit,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Student(sid, name, addr, age, GPA) Class(dept, cnum, sec, unit, title, instructor) Enroll(sid, dept, cnum, sec) siddeptcnumsec 301CS CS EE14301.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
The Student Database Student sidnameaddrageGPA 301John183 Westwood Elaine301 Wilshire James183 Westwood Esther421 Gayley203.1 Class.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
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.
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
The Relational Model These slides are based on the slides of your text book.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam P. Anthony.
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.
SQL: DDL John Ortiz Cs.utsa.edu.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Recap of SQL Lab no 8 Advance Database Management System.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management COP4540, SCS, FIU Database Trigger.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
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.
Constraints and Triggers
SQL: Constraints and Triggers
Referential Integrity
CPSC-310 Database Systems
CS 405G: Introduction to Database Systems
The Relational Model Relational Data Model
Referential Integrity
SQL-1 Week 8-9.
SQL – Constraints & Triggers
CPSC-608 Database Systems
CS 405G: Introduction to Database Systems
Presentation transcript:

Student(sid, name, addr, age, GPA) Class(dept, cnum, sec, unit, title, instructor) Enroll(sid, dept, cnum, sec) siddeptcnumsec 301CS CS AT EE CS11201 sidnameaddrageGPA 301Elaine301 Wilshire James183 Westwood17 208Esther421 Wilshire203.1 deptcnumsecunittitleinstructor CS ModelingDick Muntz CS DB SystemsCarlo Zaniolo EE SignalDick Muntz ME MechanicsSusan Tracey

CREATE TABLE Course ( dept CHAR(2) NOT NULL, cnum INT NOT NULL, sec INT NOT NULL, unit INT, instructor VARCHAR(30), title VARCHAR(30), PRIMARY KEY(dept, cnum, sec), UNIQUE(dept, cnum, instructor), UNIQUE(dept, sec, title) ) Course(dept, cnum, sec, unit, instructor, title) Key Constraints

Referential Integrity Constraint CREATE TABLE Enroll ( sid INT REFERENCES Student(sid), dept CHAR(2), cnum INT, sec INT, FOREIGN KEY (dept, cnum, sec) REFERENCES Class(dept, cnum, sec) ON DELETE CASCADE ON UPDATE SET NULL ) Notes – Referencing attributes called FOREIGN KEY – Referenced attributes must be PRIMARY KEY or UNIQUE

CHECK Constraints CHECK( ) in CREATE TABLE Example: all CS classes are of 3 units or more. – CREATE TABLE Class ( dept CHAR(2), cnum INT, unit INT, title VARCHAR(50), instructor VARCHAR(30), CHECK (dept <> ’CS’ OR unit >= 3) ) – (dept=’CS’ → unit>3) ≡ (¬(dept=’CS’) ∨ unit>3) Constraint is checked whenever a tuple is inserted/updated. In SQL92, conditions can be more complex, e.g., with subqueries – In practice, complex CHECK conditions are not supported

General Assertions CREATE ASSERTION CHECK ( ) – Constraint on the entire relation or database Example: Average GPA >3.0 CREATE ASSERTION HighGPA CHECK (3.0 < (SELECT AVG(GPA) FROM Student))

Trigger: Event-Condition-Action rule CREATE TRIGGER // optional WHEN ( ) // optional – BEFORE | AFTER INSERT ON R – BEFORE | AFTER DELETE ON R – BEFORE | AFTER UPDATE [OF A1, A2,..., An] ON R – Any SQL statement – Multiple statements should be enclosed with BEGIN … END and be separated by ; – REFERENCING OLD|NEW TABLE|ROW AS,... FOR EACH ROW: row-level trigger FOR EACH STATEMENT (default): statement-level trigger