Still More Operators: Outer Join

Similar documents
Chapter 6: Integrity Constraints

Chapter 5. SQL: Queries, Constraints, Triggers

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

SQL: Queries, Programming, Triggers

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Relational Algebra and SQL

Databases and BigData

Lecture 6. SQL, Logical DB Design

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

SQL NULL s, Constraints, Triggers

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

How To Create A Table In Sql (Ahem)

Introduction to SQL ( )

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Advance DBMS. Structured Query Language (SQL)

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Scheme G. Sample Test Paper-I

There are five fields or columns, with names and types as shown above.

Review: Participation Constraints

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Comp 5311 Database Management Systems. 3. Structured Query Language 1

The Relational Model. Why Study the Relational Model?

CSC 443 Data Base Management Systems. Basic SQL

3. Relational Model and Relational Algebra

Information Systems SQL. Nikolaj Popov

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

Review of Business Information Systems Third Quarter 2013 Volume 17, Number 3

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

EECS 647: Introduction to Database Systems

Section of DBMS Selection & Evaluation Questionnaire


Introduction to Microsoft Jet SQL

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

The Relational Data Model and Relational Database Constraints

6 QUERY-BY-EXAMPLE (QBE)

Θεµελίωση Βάσεων εδοµένων

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Data Modeling. Database Systems: The Complete Book Ch ,

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Simple SQL Queries (3)

Query-by-Example (QBE)

Introduction to tuple calculus Tore Risch

Introduction to SQL C H A P T E R3. Exercises

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

BCA. Database Management System

Schema Mappings and Data Exchange

Oracle Database 10g: Introduction to SQL

Using Temporary Tables to Improve Performance for SQL Data Services

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

ICAB4136B Use structured query language to create database structures and manipulate data

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Comp 3311 Database Management Systems. 2. Relational Model Exercises

ATTACHMENT 6 SQL Server 2012 Programming Standards

New York University Computer Science Department Courant Institute of Mathematical Sciences

Summary on Chapter 4 Basic SQL

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

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

IT2305 Database Systems I (Compulsory)

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Instant SQL Programming

AVOIDANCE OF CYCLICAL REFERENCE OF FOREIGN KEYS IN DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

Procedural Extension to SQL using Triggers. SS Chung

More SQL: Assertions, Views, and Programming Techniques

SQL Tables, Keys, Views, Indexes

Exercise 1: Relational Model

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

Oracle SQL. Course Summary. Duration. Objectives

10CS54: DATABASE MANAGEMENT SYSTEM

Chapter 4: SQL. Schema Used in Examples

The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Chapter 6: Integrity and Security. Domain Constraints

Maintaining Stored Procedures in Database Application

Ch.5 Database Security. Ch.5 Database Security Review

Oracle Database 10g Express

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

Programming with SQL

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Oracle Database: SQL and PL/SQL Fundamentals

SQL Server. 1. What is RDBMS?

Oracle 10g PL/SQL Training

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

IT2304: Database Systems 1 (DBS 1)

SQL Server An Overview

SQL. Short introduction

Database Programming with PL/SQL: Learning Objectives

Proposed solutions Project Assignment #1 (SQL)

DataBase Management Systems Lecture Notes

Transcription:

Still More Operators: Outer Join outer join is an extension of the join operation to deal with missing information three types: left outer join, right outer join, and full outer join left outer join computes the natural join, then takes all tuples (rows) in the left relation that did not match on the join attribute and includes them in the result, with all attributes of the right relation padded with null values right outer join is the same, except non-matching tuples in the right relation are included in the result padded with null values full outer join includes all non-matching tuples of both relations appropriately padded see examples in text, p108-109

Still More Operators division R/S: given R(A,B) and S(B), then a given tuple t is in R/S if for all s in S there exists an r in R such that r.b=s.b and t.a=r.a. So tuple t with attribute t.a is in the result if and only if R contained tuples (t.a, B 1 ), (t.a, B 2 ), (t.a, B 3 ), for every possible value B i contained in S. Note that S must be defined on a subset of the attributes of R for the operation to be meaningful.

A Short Interlude: Integrity the preceding slides covered chapter three up to section 3.3 before attacking chapter 4 (SQL), we re going to make a brief excursion up to chapter 6, touching sections 6.1-6.4 Integrity constraints attempt to enforce data consistency and prevent accidental damage to the database during updates We ve already seen two forms of integrity constraints: key declarations (stipulating that certain attributes form a candidate key for a given entity set) mapping form of a relationship (one-one, one-many, many-many)

Integrity Constraints Domain Constraints simplest form of integrity constraint type declarations are one such domain constraint (e.g., integer, floating point, double-precision, fixed length character string). domains can be further restricted (e.g., check clause in SQL can ensure that hourly wages are? 4.00 dollars) easily tested whenever a new data item is entered into the database extensions like date or currency can be easily supported on a strongly typed programming language Null values can be useful for values to be filled in later, but some attributes may need to be specified as not Null (e.g., primary keys cannot have a null value)

Integrity Constraints (2) Key Constraints keys must have unique values primary key -- a candidate key declared primary unique key -- a candidate key foreign key -- a set of attributes that are a primary key for some other relations foreign keys are an important concept because we need to treat foreign keys differently from other attributes (for example, protecting their uniqueness and insuring referential integrity) even though they aren t a primary key in the current relation

Referential Integrity We often want to be able to ensure that an attribute value in a tuple of a relation appears in at least one tuple of another relation. For example:» EMP(eno, ename, salary)» DEPT(dno, dname, floor)» WORKS-IN(eno, dno, hours) note that eno is a foreign key in WORKS-IN We want the following to be true:? eno (WORKS-IN)?? eno (EMP) (every eno is a real employee)? dno (WORKS-IN)?? dno (DEPT) (every dno is a real department) SQL allows the declaration of domain/key/referential integrity constraints with the clause check in its DDL

Referential Integrity: SQL DDL Example Create table customer (cust-name char(20) not null, cust-street char(30), cust-city char(30), primary key (cust-name)) Create table branch (branch-name char(15) not null, branch-city char(30), assets number, primary key (branch-name), check (assets?0)) Create table account (account-no char(10) not null, branch-name char(15), balance number, primary key (account-no), foreign key (branch-name) references branch, check (balance?0)) Create table depositor (cust-name char(20) not null, account-no char(10) not null, primary key (cust-name), foreign key (cust-name) references customer, foreign key (account-no) references account)

Referential Integrity and Database Modifications Database modifications may violate referential integrity Insertion: inserting a value into the referencing relation that is not in the referenced relation Deletion: deleting the last example of a given value in the referenced relation and leaving that value in the referencing one proper handling may lead to cascading deletions Update to the referencing relation (constraints as Insertion) Update to the referenced relation (constraints as Deletion)

Assertions An assertion is an arbitrary expression that the database must always satisfy e.g., student GPA > 2.8, or sum(all-charges) < credit-line Domain constraints and referential integrity constraints are special forms of assertion that are easy to test SQL supports assertions as follows: create assertion <assertion-name> check <predicate> When an assertion is made the system checks it for validity. If it is validated, every future modification of the database is checked against the assertion and allowed only if it is not violated. This can be very expensive if assertions are complex or numerous

Triggers A trigger is a statement that the system executes automatically as a side effect of an update to the database. A trigger has two parts: condition under which it is executed actions to be taken if it is executed Example: instead of having an assertion balance?0 for a checking account, use a trigger on negative balances that sets the balance to zero and creates a new loan for the amount of the overdraft Triggers make the system reactive Triggers are also called active rules Like Assertions, Triggers can be very expensive.

Trigger Example define trigger overdraft on update of account T (if new T.balance < 0 then (insert into borrow values (T.branch.name, T.account-number, T-customer-name, - new T.balance) update deposit S set S.balance = 0 where S.account-number = T.account-number)) (note: SQL syntax given here is slightly different from that in the text, p235)

SQL (Structured Query Language) (Astrahan, Gray, Lindsay, Selinger, ) Most common and influential commercial query language; well established as the industry standard query language for relational databases Developed (as Sequel ) at the IBM Research Lab in San Jose in the early 70s Four basic commands select insert delete update Result of each query is a relation

SQL Example select from e.name emp e where e.age > 30; e is a tuple variable ranging over the emp relation a tuple variable followed by a. and an attribute is an indexed tuple variable and specifies the corresponding attribute of the tuple, very similarly to in many programming languages what follows the keyword select is the target list what follows from is the tuple variable list and consists of a list of relations and variable names what follows where is the qualification clause; an arbitrary boolean expression

SQL Basic format of the select command select from where Semantics [distinct] target_list tuple_variable_list qualification [order by target_list_subset]; evaluate qualification: select the subset of the cartesian product of the ranges of the tuple variables that satisfy the qualification evaluate target list: eliminate columns that are not in the target list prepare the result as a relation with columns according to the target list if distinct is used, eliminate duplicate tuples if order by is used, sort the result accordingly

SQL: some example queries We will give a number of simple query examples using the following relational schema: sailors(sid, sname, rating) boats(bid, bname, colour) reserve(sid, bid, date) (1) Find the names of sailors who have reserved boat #2 select s.sname from sailors s, reserve r where s.sid=r.sid and r.bid=2

SQL: example queries (2) (2) Find the names of sailors who have reserved a red boat select s.sname from sailors s, reserve r, boats b where s.sid=r.sid and r.bid=b.bid and b.colour= red (3) Find the colours of all boats reserved by Pat select b.colour from sailors s, reserve r, boats b where s.sname= Pat and s.sid=r.sid and r.bid=b.bid

SQL: example queries (3) (4) Find the names of sailors who have reserved at least one boat select s.sname from sailors s, reserve r where s.sid=r.sid (5) Find the names of sailors who have reserved a red or a green boat select s.sname from sailors s, reserve r, boats b where s.sid=r.sid and r.bid=b.bid and (b.colour= red or b.colour= green )

SQL: example queries (4) (6) Find the names of sailors who have reserved a red and a green boat select s.sname from sailors s, reserve r, boats b, reserve r2, boats b2 where s.sid=r.sid and r.bid=b.bid and b.colour= red and s.sid=r2.sid and r2.bid=b2.bid and b2.colour= green ) Note: in the above query if sailor Pat has reserved one green boat and two red ones, the name Pat will appear twice in the results. To avoid that, use the keyword distinct in the select line, as in: select distinct s.sname