Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
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.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
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
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.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
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,
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.)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
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.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
1 Lecture 6: Views Friday, January 17th, Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,
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)
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.
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.
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
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.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
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.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
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
Database Management COP4540, SCS, FIU Database Trigger.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
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.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
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
Foreign Keys Local and Global Constraints Triggers
SQL: Constraints and Triggers
Referential Integrity
CPSC-310 Database Systems
The Relational Model Relational Data Model
Advanced SQL: Views & Triggers
Referential Integrity
SQL – Constraints & Triggers
CPSC-608 Database Systems
Lecture 05: SQL Wednesday, October 9, 2002.
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15

Dec 8, 2003Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNumber int, sName varchar (20), dept char (2), CONSTRAINT key PRIMARY KEY (sNumber), CONSTRAINT uniqueName UNIQUE (sName));

Dec 8, 2003Murali Mani Unique vs. primary keys Attribute values may be null even if they are declared unique (primary key attributes should not be null). We can have any number of unique constraints for a table (only one primary key constraint can be defined for a table).

Dec 8, 2003Murali Mani Foreign Keys: Referential Integrity Constraints Specified for a table as CONSTRAINT [ ] FOREIGN KEY ( ) REFERENCES ( ) eg: FOREIGN KEY R (a, b) REFERENCES S (a, b) Requires (a, b) be unique or primary key of S. Consider a row in R with values of a as a1, and b as b1 where a1, b1 are both non-null. There must be a row in S with values for (a, b) as (a1, b1).

Dec 8, 2003Murali Mani Maintaining referential integrity: Inserts/Deletes/Updates Default: reject any modification that violates the constraints. We can specify other policies for delete/update as set null/cascade. Eg: FOREIGN KEY (name) references Student (sName) ON DELETE SET NULL ON UPDATE CASCADE SET NULL: if the update violates foreign key constraint, the foreign key attributes are set to null. CASCADE: if the update violates foreign key constraint, the foreign key attributes are modified.

Dec 8, 2003Murali Mani Constraints on Attributes We can specify attributes as NULL or NOT NULL. Eg: sName varchar (20) NOT NULL We can specify CHECK constraints. Eg: gender char (1) CHECK (gender IN (‘F’, ‘M’)) salary int CHECK (salary >= 60000) CONSTRAINT c1 check (salary >= 60000) professor varchar (30) CHECK (professor IN select pname from professor); Constraints are checked only when inserting/updating the table.

Dec 8, 2003Murali Mani Constraints for a tuple CONSTRAINT CHECK ( ) Eg: CREATE TABLE Marks (midTerm int, final int, CHECK (midTerm + final >= 25)); Note: Constraints are checked only during insertion/update

Dec 8, 2003Murali Mani Altering Constraints ALTER TABLE ADD CONSTRAINT ALTER TABLE DROP CONSTRAINT

Dec 8, 2003Murali Mani Constraints on the entire relational schema Assertions: CREATE ASSERTION CHECK ( ) Eg: CREATE ASSERTION CHECK ( NOT EXISTS (SELECT * FROM PROFESSOR WHERE salary < 60000)); Condition is any condition that can appear in WHERE clause For any database modification, the assertion must be true.

Dec 8, 2003Murali Mani Triggers (Event, Condition, Action rules) We specify triggers as Event, Condition, Action rules. When event occurs, and condition is satisfied, the action is performed.

Dec 8, 2003Murali Mani Triggers CREATE TRIGGER BEFORE | AFTER INSERT|DELETE|UPDATE [OF ] ON | [REFERENCING OLD AS, NEW AS ] FOR EACH ROW [WHEN ( )] ;

Dec 8, 2003Murali Mani Triggers: example CREATE TRIGGER t1 AFTER INSERT ON Student REFERENCING NEW as newTuple FOR EACH ROW WHEN (newTuple.professor IS NOT NULL) BEGIN INSERT INTO Professor (pname) values (:newTuple.professor); END;. run;

Dec 8, 2003Murali Mani Statement Level: Triggers CREATE TRIGGER t1 AFTER INSERT ON Student REFERENCING NEW as newTable BEGIN INSERT INTO Professor (pname) SELECT professor FROM Student where (professor IS NOT NULL AND professor NOT IN (SELECT pname FROM Professor)); END;. run;