A tuple is dangling if it doesn't join with any

Similar documents
CS54100: Database Systems

Outerjoins, Constraints, Triggers

Interpretation is not exactly ëmissing value." There could be many reasons why no value is

Constraints. Local and Global Constraints Triggers

Databases 1. Defining Tables, Constraints

Constraints and Triggers

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

From E/R Diagrams to Relations

Chapter 6 The database Language SQL as a tutorial

Chapter 7: Constraints and Triggers. Foreign Keys Local and Global Constraints Triggers

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

Subqueries. Must use a tuple-variable to name tuples of the result

Design Techniques. 1. Avoid redundancy 2. Limit the use of weak entity sets 3. Don t use an entity set when an attribute will do

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

Database Design and Programming

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

Databases-1 Lecture-01. Introduction, Relational Algebra

Databases 1. SQL/PSM and Oracle PL/SQL

Chapter 6 The database Language SQL as a tutorial

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

CSC 343 Winter SQL: Aggregation, Joins, and Triggers MICHAEL LIUT

Introduction to SQL. Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Real SQL Programming Persistent Stored Modules (PSM)

CS W Introduction to Databases Spring Computer Science Department Columbia University

Assign expressions to declared variables with :=. END IF; EXIT WHEN éconditioné END LOOP;

Chapter 2 The relational Model of data. Relational model introduction

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Relational Algebra and SQL. Basic Operations Algebra of Bags

Transactions, Views, Indexes. Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data

CSCI3030U Database Models

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

SQL: Data Manipulation Language

CSCD43: Database Systems Technology. Lecture 4

Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Constraints, Views & Indexes. Running Example. Kinds of Constraints INTEGRITY CONSTRAINTS. Keys Foreign key or referential integrity constraints

Chapter 2 The relational Model of data. Relational algebra

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

Database Management

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

Relational Algebra. Algebra of Bags

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Contains slides made by Naci Akkøk, Pål Halvorsen, Arthur M. Keller, Vera Goebel

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Outline. CS 235: Introduction to Databases. DB Application Programming. Interface Solutions. Basic PSM Form. Persistent Stored Modules

The Relational Model 2. Week 3

Beersèname, manfè. Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

CS2300: File Structures and Introduction to Database Systems

Schedule. Today: Feb. 21 (TH) Feb. 28 (TH) Feb. 26 (T) Mar. 5 (T) Read Sections , Project Part 6 due.

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

Concepts of Database Management Seventh Edition. Chapter 4 The Relational Model 3: Advanced Topics

Views, Indexes, Authorization. Views. Views 8/6/18. Virtual and Materialized Views Speeding Accesses to Data Grant/Revoke Priviledges

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

Relational Query Languages

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Databases 2012 Constraints, Triggers, and Views

Summary Modifications. Database Construction (and Usage) Explicit attribute lists. Insertions with queries. Quiz. Default values

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Introduction to Transactions: Controlling Concurrent "Behavior" Virtual and Materialized Views" Indexes: Speeding Accesses to Data"

Relational data model

Where are we? Week -4: Data definition (Creation of the schema) Week -3: Data definition (Triggers) Week -1: Transactions and concurrency in ORACLE.

The Relational Model. Chapter 3

5 Integrity Constraints and Triggers

Chapter 10 Advanced topics in relational databases

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

Introduction to Data Management CSE 344

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Sql Server Syllabus. Overview

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

ORACLE DATABASE 12C INTRODUCTION

Introduction to Database Systems CSE 414

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

COSC344 Database Theory and Applications. Lecture 11 Triggers

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Students Guide. Requirements of your homework

Logical Query Languages. Motivation: 1. Logical rules extend more naturally to. recursive queries than does relational algebra. Used in SQL recursion.

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

On-Line Application Processing

Querying Data with Transact SQL

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

5. Single-row function

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Transcription:

Outerjoin R./ S = R./Swith dangling tuples padded with nulls and included in the result. A tuple is dangling if it doesn't join with any other tuple. R = A B 1 2 3 4 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 1

Outerjoin in SQL2 A number of forms are provided. Can be used either stand-alone (in place of a select-from-where) or to dene 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 The latter can be modied by: 1. Optional NATURAL in front of JOIN. 2. Optional ON condition at end. 3. Optional LEFT, RIGHT, or FULL before OUTER. LEFT = pad dangling tuples of R only; RIGHT = pad dangling tuples of S only. 2

Oracle Outerjoin Ain't no such thing. But parenthesized select-from-where allowed in a FROM clause. Example Really a way to dene a view and use it in a single query. 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? 3

Constraints Commercial relational systems allow much more \ne-tuning" of constraints than do the modeling languages we learned earlier. Outline In essence: SQL programming is used to describe constraints. 1. Primary key declarations (covered). 2. Foreign-keys = referential integrity constraints. E.g., if Sells mentions a beer, then we should be able to nd 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. 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 price REAL Beers(name), 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. 6

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." 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"! \Budweiser." Same change. 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. E.g., what does it mean if a beer goes away? What if a beer changes its name? 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). 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. Condition is checked only when the associated attribute changes (i.e., an insert or update occurs). 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. 11

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

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. Oracle version and SQL3 version; not in SQL2. Dier 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. 14

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. 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 15

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

Notes More information in on-line document orplsql.html 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. 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 denition of the trigger to be stored in the database. Oracle triggers are elements of the database, like tables or views. 17

Example Maintain a list of all the bars that raise their price for some beer by more than $1. Sells(bar, beer, price) 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 18