Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections 7.1-7.2. Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
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.
Constraints We have discussed three types of integrity constraints: primary keys, not null constraints, and unique constraints. CREATE TABLE Movies ( title.
Fall 2001Arthur Keller – CS 1808–1 Schedule Today Oct. 18 (TH) Schemas, Views. u Read Sections u Project Part 3 extended to Oct. 23 (T). Oct.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
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,
Winter 2002Judy Cushing8–1 Schedule Jan. 30 (Wed) u Modifications, Schemas, Views. Read Sections This Week (Feb 4ff) u Constraints Read Sections.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
Onsdag The concepts in a relation data model SQL DDL DML.
+ Assignment 5 Q’s CSCI 2141 W Foreign Key issue CONSTRAINT fk_model FOREIGN KEY (model) REFERENCES Desktop(Model) Problem – can’t have it reference.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
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.
1 Database Systems Defining Database Schema Views.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
1 Constraints, Views, Indexes Foreign Keys Constraints Triggers Virtual and Materialized Views Speeding Accesses to Data.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 Introduction to Database Systems, CS420 SQL Constraints.
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.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
Slides are reused by the approval of Jeffrey Ullman’s
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CMSC-461 Database Management Systems
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
Defining a Database Schema
CMSC-461 Database Management Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

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. 25 (TH) More Constraints, Triggers. u Read Sections Oct. 30 (T) Embedded SQL. u Read Section 8.1. Assignment 5 due. Project Part 4 due. Nov. 1 (TH) Midterm u Covers material through Oct. 25 (TH) lecture and readings (Chapters 1-3, 5-7).

Fall 2001Arthur Keller – CS 1809–2 Project Part 3 Extended deadline: Oct. 24 (W) midnight (because of power outages). Use submit (use submit -m for instructions) u Doesn’t yet work on mandelbrot u FTP your files to teach or learn and then submit in the meanwhile I have office hours today; I’ll arrange for some on Wednesday.

Fall 2001Arthur Keller – CS 1809–3 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages we learned earlier. In essence: SQL programming is used to describe constraints. Outline 1.Primary key declarations (already covered). 2.Foreign-keys = referential integrity constraints. 3.Attribute- and tuple-based checks = constraints within relations. 4.SQL Assertions = global constraints. u Not found in Oracle. 5.Oracle Triggers. u A substitute for assertions.

Fall 2001Arthur Keller – CS 1809–4 Foreign Keys In relation R a clause that “attribute A references S(B)” says that whatever values appear in the A column of R must also appear in the B column of relation S. B must be declared the primary key for S. Example CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY, manf CHAR(20) ); CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) REFERENCES Beers(name), price REAL );

Fall 2001Arthur Keller – CS 1809–5 Alternative: add another element declaring the foreign key, as: CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY beer REFERENCES Beers(name) ); Extra element essential if the foreign key is more than one attribute.

Fall 2001Arthur Keller – CS 1809–6 What Happens When a Foreign Key Constraint is Violated? Two ways: 1.Insert or update a Sells tuple so it refers to a nonexistent beer. Always rejected. 2.Delete or update a Beers tuple that has a beer value some Sells tuples refer to. a) Default: reject. b) Cascade: Ripple changes to referring Sells tuple. Example Delete “Bud.” Cascade deletes all Sells tuples that mention Bud. Update “Bud” to “Budweiser.” Change all Sells tuples with “Bud” in beer column to be “Budweiser.”

Fall 2001Arthur Keller – CS 1809–7 c) Set Null: Change referring tuples to have NULL in referring components. Example Delete “Bud.” Set-null makes all Sells tuples with “Bud” in the beer component have NULL there. Update “Bud” to “Budweiser.” Same change.

Fall 2001Arthur Keller – CS 1809–8 Selecting a Policy Add ON [DELETE, UPDATE] [CASCADE, SET NULL] to declaration of foreign key. Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, FOREIGN KEY beer REFERENCES Beers(name) ON DELETE SET NULL ON UPDATE CASCADE ); “Correct” policy is a design decision. u E.g., what does it mean if a beer goes away? What if a beer changes its name?

Fall 2001Arthur Keller – CS 1809–9 Attribute-Based Checks Follow an attribute by a condition that must hold for that attribute in each tuple of its relation. Form: CHECK ( condition ). u Condition may involve the checked attribute. u Other attributes and relations may be involved, but only in subqueries. u Oracle: No subqueries allowed in condition. Condition is checked only when the associated attribute changes (i.e., an insert or update occurs).

Fall 2001Arthur Keller – CS 1809–10 Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) CHECK( beer IN (SELECT name FROM Beers) ), price REAL CHECK( price <= 5.00 ) ); Check on beer is like a foreign-key constraint, except:  The check occurs only when we add a tuple or change the beer in an existing tuple, not when we delete a tuple from Beers.

Fall 2001Arthur Keller – CS 1809–11 Tuple-Based Checks Separate element of table declaration. Form: like attribute-based check. But condition can refer to any attribute of the relation. u Or to other relations/attributes in subqueries. u Again: Oracle forbids the use of subqueries. Checked whenever a tuple is inserted or updated.

Fall 2001Arthur Keller – CS 1809–12 Example Only Joe's Bar can sell beer for more than $5. CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20), price REAL, CHECK(bar = 'Joe''s Bar' OR price <= 5.00) );

Fall 2001Arthur Keller – CS 1809–13 SQL Assertions Database-schema constraint. Not present in Oracle. Checked whenever a mentioned relation changes. Syntax: CREATE ASSERTION CHECK( );

Fall 2001Arthur Keller – CS 1809–14 Example No bar may charge an average of more than $5 for beer. Sells(bar, beer, price) CREATE ASSERTION NoRipoffBars CHECK(NOT EXISTS( SELECT bar FROM Sells GROUP BY bar HAVING 5.0 < AVG(price) ) ); Checked whenever Sells changes.

Fall 2001Arthur Keller – CS 1809–15 Example There cannot be more bars than drinkers. Bars(name, addr, license) Drinkers(name, addr, phone) CREATE ASSERTION FewBar CHECK( (SELECT COUNT(*) FROM Bars) <= (SELECT COUNT(*) FROM Drinkers) ); Checked whenever Bars or Drinkers changes.