CMSC-461 Database Management Systems

Slides:



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

Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Fall 2001Arthur Keller – CS 18010–1 Schedule Oct. 25 (TH) More Constraints, Triggers. u Read Sections Oct. 30 (T) Embedded SQL. u Read Section.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
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 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 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
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.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 PL/SQL Oracle’s Version of Triggers and PSM. 2 PL/SQL uOracle uses a variant of SQL/PSM which it calls PL/SQL. uPL/SQL not only allows you to create.
Triggers.
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.
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.
Databases 1 9th lecture. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
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.
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.
Introduction to PL/SQL. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
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.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Database Management COP4540, SCS, FIU Database Trigger.
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.
CPSC-310 Database Systems
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
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Database Design: DBS CB, 2nd Edition
Introduction to Database Systems, CS420
Database Construction (and Usage)
CPSC-608 Database Systems
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
IST 210: Organization of Data
CPSC-310 Database Systems
CPSC-608 Database 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
-Transactions in SQL -Constraints and Triggers
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

CMSC-461 Database Management Systems

Outerjoin R  S = R  S with dangling tuples padded with nulls and included in the result. A tuple is dangling if it doesn't join with any other tuple.

Dangling Tuples R = A B 1 2 2 3 S = B C 2 5 2 6 7 8 R  S = A B C 1 2 2 3 S = B C 2 5 2 6 7 8 R  S = A B C 1 2 5 1 2 6 3 4 NULL NULL 7 8

Outerjoin in SQL2 A number of forms are provided. Can be used either stand-alone (in place of a select-from-where) or to define a relation in the FROM-clause. R NATURAL JOIN S R JOIN S ON condition e.g., condition: R:B = S:B R CROSS JOIN S R OUTER JOIN S

Outerjoin in SQL2 (II) The latter can be modified by: Optional NATURAL in front of JOIN. Optional ON condition at end. Optional LEFT, RIGHT, or FULL before OUTER. LEFT = pad dangling tuples of R only; RIGHT = pad dangling tuples of S only.

Oracle Outerjoin Ain't no such thing. But parenthesized select-from-where allowed in a FROM clause. Really a way to define a view and use it in a single query.

Example Find the average over all bars of the maximum price the bar charges for a beer. Sells(bar, beer, price) SELECT AVG(maxPrice) FROM (SELECT bar, MAX(price) AS maxPrice FROM Sells GROUP BY Bar);

Problem Can we express the outerjoin in Oracle SQL as some more complicated expression?

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 (covered). 2. Foreign-keys = referential integrity constraints. E.g., if Sells mentions a beer, then we should be able to find that beer in Beers. 3. Attribute- and tuple-based checks = constraints within relations. 4. SQL2 Assertions = global constraints. Not found in Oracle 7.3.2. 5. Oracle Triggers. A substitute for assertions. 6. SQL3 triggers and assertions.

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 );

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.

What Happens When a Foreign Key Constraint is Violated? Two ways: 1. Insert a Sells tuple referring 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"  "Budweiser." Change all Sells tuples with "Bud" in beer column to be "Budweiser."

What Happens When a Foreign Key Constraint is Violated? (II) 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"  "Budweiser." Same change.

Selecting a Policy Add ON [DELETE, UPDATE] [CASCADE, SET NULL] to declaration of foreign key.

Selecting a Policy (II) 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 );

Selecting a Policy (III) "Correct" policy is a design decision. E.g., what does it mean if a beer goes away? What if a beer changes its name?

Attribute-Based Checks Follow an attribute by a condition that must hold for that attribute in each tuple of its relation. Form: CHECK (condition). Condition may involve the checked attribute. Other attributes and relations may be involved, but only in subqueries. Oracle 7.3.2: No subqueries allowed in condition.

Attribute-Based Checks Condition is checked only when the associated attribute changes (i.e., an insert or update occurs).

Example CREATE TABLE Sells ( bar CHAR(20), beer CHAR(20) CHECK( beer IN (SELECT name FROM Beers) ), price REAL CHECK( price <= 5.00 ) );

Attribute-Based Checks (III) 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.

Tuple-Based Checks Separate element of table declaration. Form: like attribute-based check. But condition can refer to any attribute of the relation. Or to other relations/attributes in subqueries. Again: Oracle 7.3.2 forbids the use of subqueries.

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) );

Triggers Often called event-condition-action rules. Event = a class of changes in the DB, e.g., "insert into Beers." Condition = a test as in a where-clause for whether or not the trigger applies. Action = one or more SQL statements.

Triggers (II) Oracle version and SQL3 version; not in SQL2. Differ from checks or SQL2 assertions in that: 1. Event is programmable, rather than implied by the kind of check. 2. Condition not available in checks.

Example Whenever we insert a new tuple into Sells, make sure the beer mentioned is also mentioned in Beers, and insert it (with a null manufacturer) if not.

Example (II) Sells(bar, beer, price) CREATE OR REPLACE TRIGGER BeerTrig AFTER INSERT ON Sells FOR EACH ROW WHEN(new.beer NOT IN (SELECT name FROM Beers)) BEGIN INSERT INTO Beers(name) VALUES(:new.beer); END; . Run

Options Can omit OR REPLACE. Effect is that it is an error if a trigger of this name exists. AFTER can be BEFORE. INSERT can be DELETE or UPDATE OF < attribute > ON. FOR EACH ROW can be omitted, with an important effect: the action is done once for the relation(s) consisting of all changes.

Notes There are two special variables new and old, representing the new and old tuple in the change. old makes no sense in an insert, and new makes no sense in a delete. Notice: in WHEN we use new and old without a colon, but in actions, a preceding colon is needed.

Notes (II) The action is a PL/SQL statement. Simplest form: surround one or more SQL statements with BEGIN and END. However, select-from-where has a limited form. Dot and run cause the definition of the trigger to be stored in the database. Oracle triggers are elements of the database, like tables or views.

Example Maintain a list of all the bars that raise their price for some beer by more than $1. Sells(bar, beer, price)

Example (II) CREATE TRIGGER PriceTrig AFTER UPDATE OF price ON Sells FOR EACH ROW WHEN(new.price > old.price + 1.00) BEGIN INSERT INTO RipoffBars VALUES(:new.bar); END; . run