Database Systems SQL. A.R. Hurson 323 CS Building

Similar documents
Database Systems Relational Algebra. A.R. Hurson 323 CS Building

CS5300 Database Systems

Tuple Relational Calculus

Relational Algebra & Calculus

Database Design and Normalization

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL---Part 1

But RECAP. Why is losslessness important? An Instance of Relation NEWS. Suppose we decompose NEWS into: R1(S#, Sname) R2(City, Status)

Relational Algebra and Calculus

INF1383 -Bancos de Dados

Relational Algebra 2. Week 5

Homework Assignment 2. Due Date: October 17th, CS425 - Database Organization Results

Database Applications (15-415)

Constraints: Functional Dependencies

Constraints: Functional Dependencies

Introduction to Data Management. Lecture #12 (Relational Algebra II)

Sub-Queries in SQL SQL. 3 Types of Sub-Queries. 3 Types of Sub-Queries. Scalar Sub-Query Example. Scalar Sub-Query Example


Databases 2011 The Relational Algebra

INTRODUCTION TO RELATIONAL DATABASE SYSTEMS

Relations. Relations of Sets N-ary Relations Relational Databases Binary Relation Properties Equivalence Relations. Reading (Epp s textbook)

CAS CS 460/660 Introduction to Database Systems. Functional Dependencies and Normal Forms 1.1

Introduction to Database Systems CSE 414. Lecture 20: Design Theory

Relational Algebra on Bags. Why Bags? Operations on Bags. Example: Bag Selection. σ A+B < 5 (R) = A B

CSE 132B Database Systems Applications

Correlated subqueries. Query Optimization. Magic decorrelation. COUNT bug. Magic example (slide 2) Magic example (slide 1)

Schema Refinement and Normal Forms. Chapter 19

Databases Exam HT2016 Solution

Lineage implementation in PostgreSQL

Relational operations

Instructor: Sudeepa Roy

Databases through Python-Flask and MariaDB

Spatial Database. Ahmad Alhilal, Dimitris Tsaras

COSC 430 Advanced Database Topics. Lecture 2: Relational Theory Haibo Zhang Computer Science, University of Otago

Database Applications (15-415)

L13: Normalization. CS3200 Database design (sp18 s2) 2/26/2018

Today s topics. Introduction to Set Theory ( 1.6) Naïve set theory. Basic notations for sets

Introduction to Data Management. Lecture #6 (Relational DB Design Theory)

Relational Design: Characteristics of Well-designed DB

CS632 Notes on Relational Query Languages I

(! "! # ) T *+!, &'( ) % -.* / *3 '!'1 2 .=! =&6 0 * ' ) 4 50 *6 7 ) 8 ) 9 :; )!'

Exam 1. March 12th, CS525 - Midterm Exam Solutions

ON SITE SYSTEMS Chemical Safety Assistant

CS54100: Database Systems

Relational-Database Design

General Overview - rel. model. Carnegie Mellon Univ. Dept. of Computer Science /615 DB Applications. Motivation. Overview - detailed

Lecture 24: Bloom Filters. Wednesday, June 2, 2010

Query Processing. 3 steps: Parsing & Translation Optimization Evaluation

Approximate String Joins in a Database (Almost) for Free

ALTER TABLE Employee ADD ( Mname VARCHAR2(20), Birthday DATE );

Phil Introductory Formal Logic

Schema Refinement. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

Schedule. Today: Jan. 17 (TH) Jan. 24 (TH) Jan. 29 (T) Jan. 22 (T) Read Sections Assignment 2 due. Read Sections Assignment 3 due.

a (b + c) = a b + a c

7 RC Simulates RA. Lemma: For every RA expression E(A 1... A k ) there exists a DRC formula F with F V (F ) = {A 1,..., A k } and

Moving into the information age: From records to Google Earth

Robust Programs with Filtered Iterators

Accountability. User Guide

CMPT 354: Database System I. Lecture 9. Design Theory

ICS141: Discrete Mathematics for Computer Science I

Foundations of Logic Programming

VCell Tutorial. Building a Rule-Based Model

Sets. Introduction to Set Theory ( 2.1) Basic notations for sets. Basic properties of sets CMSC 302. Vojislav Kecman

Functional Dependencies and Normalization. Instructor: Mohamed Eltabakh

Active Integrity Constraints and Revision Programming

Schema Refinement and Normal Forms. Case Study: The Internet Shop. Redundant Storage! Yanlei Diao UMass Amherst November 1 & 6, 2007

Review: Keys. What is a Functional Dependency? Why use Functional Dependencies? Functional Dependency Properties

2 Plain Kolmogorov Complexity

Lecture #7 (Relational Design Theory, cont d.)

b. states.xml Listing of states and their postal abbreviations.

You are here! Query Processor. Recovery. Discussed here: DBMS. Task 3 is often called algebraic (or re-write) query optimization, while

Exam 1 Solutions Spring 2016

n CS 160 or CS122 n Sets and Functions n Propositions and Predicates n Inference Rules n Proof Techniques n Program Verification n CS 161

Introduction. Normalization. Example. Redundancy. What problems are caused by redundancy? What are functional dependencies?

Association Rules. Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION. Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION

ST-Links. SpatialKit. Version 3.0.x. For ArcMap. ArcMap Extension for Directly Connecting to Spatial Databases. ST-Links Corporation.

1 Alphabets and Languages

Module 10: Query Optimization

Schema Refinement and Normal Forms

Algorithms for Data Science

MA/CSSE 474 Theory of Computation

Quiz 2. Due November 26th, CS525 - Advanced Database Organization Solutions

Multiple-Site Distributed Spatial Query Optimization using Spatial Semijoins

Smart Data Collection and Real-time Digital Cartography

CS322: Database Systems Normalization

CSC 261/461 Database Systems Lecture 10 (part 2) Spring 2018

A Lower Bound of 2 n Conditional Jumps for Boolean Satisfiability on A Random Access Machine

Lectures 6. Lecture 6: Design Theory

DATABASE DESIGN I - 1DL300

Introduction to Data Management. Lecture #7 (Relational DB Design Theory II)

3 Propositional Logic

On Ordering Descriptions in a Description Logic

Introduction to Data Management. Lecture #6 (Relational Design Theory)

Measurement and Data. Topics: Types of Data Distance Measurement Data Transformation Forms of Data Data Quality

CS570 Introduction to Data Mining

Geometric Algorithms in GIS

Equivalence of SQL Queries In Presence of Embedded Dependencies

ISIS/Draw "Quick Start"

Question Answering on Statistical Linked Data

Description of the ED library Basic Atoms

E23: Hotel Management System Wen Yunlu Hu Xing Chen Ke Tang Haoyuan Module: EEE 101

Transcription:

SQL A.R. Hurson 323 CS Building

Structured Query Language (SQL) The SQL language has the following features as well: Embedded and Dynamic facilities to allow SQL code to be called from a host language or a query be constructed at run-time. Triggers which are actions executed by DBMS whenever certain changes to the database meet certain conditions.

Structured Query Language (SQL) Security to control users accesses to data objects. Transaction management commands to allow the execution of transactions. Remote database accesses to allow client server or distributed environments.

Data Definition Language CREATE TABLE: The general format is as follows: CREATE TABLE base-table (column-definition [, column-definition] [, primary-key-definition] [, foreign-key-definition [, foreign-key-definition] ]); where a column-definition is in the form of columndata-type [NOTNULL] Note that the specification of primary-key is optional.

CREATE TABLE S ( S# CHAR(5) NOT NULL, SNAME CHAR(20) NOT NULL, STATUS INTEGER NOT NULL, CITY CHAR(15) NOT NULL, PRIMARY KEY ( S# ) ) ; This will create an empty table. The data values now can be entered using INSERT command. Result: S S# SNAME STATUS CITY

INSERT INTO S ( S#, SNAME, STATUS, CITY) VALUES ( S1, SMITH, 10, LONDON ): RESULT S# SNAME STATUS CITY S1 SMITH 10 LONDON

ALTER TABLE: Allows to add a new column, at the right, to an existing base-table. Its general format is as follows: ALTER TABLE base-table ADD column data-type

ALTER TABLE S ADD DISCOUNT INTEGER: RESULT S S# SNAME STATUS CITY DISCOUNT Discount column is added to the table S. All existing tuples are (conceptually) expanded, and the value of the new column is null in every record. Specification of NOT NULL is not allowed in ALTER TABLE).

DROP TABLE: Allows to destroy an existing base-table. DROP TABLE base-table;

Integrity Constraints As noted before, constraints can be defined either as the table constraints (over a single table) or assertions.

Constraints over a single table Primary Key/Candidate Key CREATETABLE Student ( Sid Studentskey is called the constraint name - It will be returned if the constraint is violated. name login age CHAR(20), CHAR(30), CHAR(20), INTEGER, gpa REAL, UNIQUE (name, age), CONSTRAINT Studentskey PRIMARY KEY (Sid) )

Constraints over a single table Foreign Key Key words FOREIGN KEY and REFERENCE are used to specify this constraint: CREATE TABLE Enroll ( Sid Referenced relation Cid grade CHAR(20), CHAR(20), CHAR(10), PRIMARY KEY (Sid, Cid), FOREIGN KEY (Sid) REFERENCES Students)

Constraints over a single table The keyword CHECK along with a conditional expression can be used to enforce a constraint over an attribute.

Constraints over a single table CREATE TABLE Student ( Sid CHAR(20), name CHAR(30), login CHAR(20), age INTEGER, gpa REAL, UNIQUE (name, age), CONSTRAINT Studentskey PRIMARYKEY (Sid) CHECK (age >= 16 AND age <=30)) When a new tuple is inserted into the table or an existing tuple is modified, the conditional statement is checked. If the result is false, the command is rejected.

Constraints over a single table Assume the following tables: Sailors(sid:integer, sname:string, rating:integer, age:real) Boats(bid:integer, bname:string, color:string) Reserves(sid:integer, bid:integer, day:date) Define a constraint that Interlake boat cannot be reserved.

Constraints over a single table CREATETABLE Reserves ( Sid INTEGER, bid day INTEGER, DATE, PRIMARY KEY (Sid, bid), FOREIGN KEY (Sid) REFERENCES Sailors) FOREIGN KEY (bid) REFERENCES Boats) CONSTRAINT nointerlakeres. CHECK ( Interlake <> (SELECT FROM WHERE B.bname Boats B B.bid = Reserve.bid)))

Constraints over a single table Command CREATE can also be used to define a domain: CREATE DOMAIN Qtyvalue INTEGER DEFAULT 1 CHECK (VALUE >= 1 AND VALUE <=1000) Here INTEGER is the base type for domain Qtyvalue, however its contents is restricted by the CHECK statement.

Constraints over a single table Once a domain is defined, it can be used to limit contents of a column in a table. The DEFAULT keyword assigns a default value to a domain this value will be automatically assumed in an attempt to insert a tuple into the relation without an initial value for an attribute defined over Qtyvalue.

Data Definition Language CREATE TABLE CREATE VIEW CREATE DOMAIN DROP TABLE DROP VIEW ALTER TABLE Define KEY CONSTRIANTS CHECK

Data Manipulation Language (DML) SQL provides four DML statements: SELECT, UPDATE, DELETE, and INSERT.

The following strategy is used to evaluate an SQL expression: Compute the cross-product of relation-list, Discard resulting tuples if they fail qualifications (restrict), Delete attributes that are not in target-list (project). If DISTINCT is specified, then duplicate tuples are eliminated.

SELECT; SELECT specifies field (s) FROM a specific table WHERE specific condition (s) is true. SELECT FROM [WHERE [DISTINCT] item(s) table (s) condition] [GROUP BY field (s)] [ORDER BY field (s)]; Target list, a list of attributes Relation list Qualifier expressions involving constants and/or column names combined using AND, OR, and NOT.

The following tables are assumed for the rest of this section: Supplier Relation S S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens

Part Relation P P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London

SP Relation S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Simple Queries SELECT S#, STATUS FROM S WHERE CITY = Paris ; RESULT S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens S# Status S 2 10 S 3 30

Simple Retrieval SELECT P# FROM SP ; S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400 Duplicates are not removed RESULT P# P 1 P 2 P 3 P 4 P 5 P 6 P 1 P 2 P 2 P 2 P 4 P 5

SELECT DISTINCT P# FROM SP ; RESULT P# P 1 P 2 P 3 P 4 P 5 P 6 S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Retrieval of Computed Values: Part relation is in Pound; Assume weight in SELECT P.P#, Weight in Grams =, P.Weight * 454 FROM P ; P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London P# Result P 1 Weight in Grams = 5448 P 2 Weight in Grams = 7718 P 3 Weight in Grams = 7718 P 4 Weight in Grams = 6356 P 5 Weight in Grams = 5448 P 6 Weight in Grams = 8626

Naming Fields in the resultant relation AS and = are two ways to name fields in result; SELECT FROM Supplier name = Sname, STATUS S WHERE CITY = Paris ; S# Sname Status City Result S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens Supplier name Status Jones 10 Blake 30

SELECT Sname As Supplier name, STATUS FROM S WHERE CITY = Paris ; Result Supplier name Status Jones 10 Blake 30 S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens

LIKE is the keyword that allows string matching (pattern matching) operation; SELECT Sname As Supplier name, City FROM S WHERE CITY LIKE %s ; Note that _ stands for any one character (Don t care), and % stands for 0 or more arbitrary characters (repeated don t care).

Result Supplier name Jones Blake Adams City Paris Paris Athens S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens

Get all parts whose names begin with the letter C. SELECT P.* FROM P WHERE P.Pname LIKE C% ; P# Pname Color Weight City P 1 Nut Red 12 London Result P# Pname Color Weight City P5 Cam Blue 12 Paris P6 Cog Red 19 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London

Similarly, NOT LIKE can also be used in the WHERE clause; SELECT FROM P.* P WHERE P.City NOT LIKE %E% ; In this case, the condition is evaluated to true if City does not contain an E.

Retrieval Involving NULL Assume for the sake of the example that supplier S 5 has a status value of null. Get supplier numbers for supplier with status greater than 25;

SELECT S# FROM S WHERE Status > 25 ; Result S# S 3 S 5 S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens

Result S# S 3 S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams Athens Unlike previous case S 5 does not qualify.

Get full detail of all suppliers SELECT * FROM S ; This is equivalent to: SELECT S.S#, S.Sname, S.Status, S.City FROM S ;

RESULT S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens

Qualified Retrieval: Get supplier numbers for suppliers in Paris with STATUS > 20 SELECT S# FROM S WHERE CITY = Paris RESULT AND STATUS > 20 ; S# S 3 S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens

Retrieval with Ordering: The result may be ordered based on the contents of one or several fields; column [ order ] [, column [ order ] ] where order is either ASC or DESC, and ASC as the default.

Get supplier numbers and Status for suppliers in Paris in descending order of status. SELECT S#, STATUS FROM S WHERE CITY = Paris ORDER BY STATUS DESC ; RESULT S# Status S 3 30 S 2 10 S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens

Join Queries Simple equi-join SELECT S.*, P.* FROM S, P WHERE S.CITY = P.CITY ; Note that the field referenced in the WHERE clause here must be qualified by the table names.

Conceptually, you may generate the Cartesian product of the tables listed in the FROM clause. Then eliminate all the tuples that do not satisfy the join condition defined in WHERE clause.

S S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens P P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London

RESULT S# Sname Status S.City P# Pname Color Weight P.City S 1 Smith 20 London P 1 Nut Red 12 London S 1 Smith 20 London P 4 Screw Red 14 London S 1 Smith 20 London P 6 Cog Red 19 London S 2 Jones 10 Paris P 2 Bolt Green 17 Paris S 2 Jones 10 Paris P 5 Cam Blue 12 Paris S 3 Blake 30 Paris P 2 Bolt Green 17 Paris S 3 Blake 30 Paris P 5 Cam Blue 12 Paris S 4 Clark 20 London P 1 Nut Red 12 London S 4 Clark 20 London P 4 Screw Red 14 London S 4 Clark 20 London P 6 Cog Red 19 London

Greater-than join: Get all combinations of supplier and part information such that the supplier city follows the part city in alphabetical order; SELECT S.*, P.* FROM S, P WHERE S.CITY > P.CITY ;

S S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens P P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London

RESULT S# Sname Status S.City P# Pname Color Weight P.City S 2 Jones 10 Paris P 1 Nut Red 12 London S 2 Jones 10 Paris P 4 Screw Red 14 London S 2 Jones 10 Paris P 6 Cog Red 19 London S 3 Blake 30 Paris P 1 Nut Red 12 London S 3 Blake 30 Paris P 4 Screw Red 14 London S 3 Blake 30 Paris P 6 Cog Red 19 London

Get all combinations of supplier information and part information where the supplier and part concerned are co-located, but omitting supplier with status 20; SELECT S.*, P.* FROM WHERE S, P S.CITY = P.CITY AND STATUS < > 20 ;

S S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens P P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London

RESULT S# Sname Status S.City P# Pname Color Weight P.City S 2 Jones 10 Paris P 2 Bolt Green 17 Paris S 2 Jones 10 Paris P 5 Cam Blue 12 Paris S 3 Blake 30 Paris P 2 Bolt Green 17 Paris S 3 Blake 30 Paris P 5 Cam Blue 12 Paris

Aggregate Functions Aggregate functions are used to enhance the retrieval power of SQL. These are: COUNT SUM AVG MAX MIN number of values in the column sum of the values in the column average of the values in the column largest value in the column smallest value in the column

Aggregate Functions For SUM and AVG, column must be numeric values. Key word DISTINCT can be used to eliminate the duplicate values. For COUNT, DISTINCT must be specified.

Get the total number of suppliers SELECT COUNT (*) FROM S ; RESULT 5 S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens Note that the result is a table with a single value.

Get the total number of suppliers currently supplying part; SELECT COUNT ( DISTINCT S#) FROM SP ; RESULT 4 S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Get the number of shipments for part P 2 ; SELECT COUNT (*) FROM SP WHERE P# = P 2 ; RESULT 4 S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Get the total quantity of part P 2 supplied; SELECT SUM (QTY) FROM SP WHERE P# = P 2 ; RESULT 1,000 S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Query Using Union: Union is traditional union operator borrowed from set theory. Get supplier numbers for parts that either weight more than 16 Pounds or are supplied by supplier S 2.

SELECT P# FROM P WHERE WEIGHT > 16 UNION SELECT P# FROM SP WHERE S# = S 2 ; Note redundant duplicate rows are always eliminated. However, we can use UNION ALL operator to include the duplicates.

RESULT P# P 1 P 2 P 3 P 6 P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Previous query can also be written as: SELECT DISTINCT P# FROM P, SP WHERE P.P# = SP.P# AND P.WEIGHT > 16 OR SP.S# = S 2 ;

Query Using INTERSECT: Similarly INTERSECT operator has also been borrowed from traditional set theory: SELECT P# FROM P WHERE WEIGHT > 16 INTERSECT SELECT P# FROM SP WHERE S# = S 2 ;

RESULT P# P 2 P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Previous query can also be specified as: SELECT P# FROM P, SP WHERE P.P# = SP.P# AND P.WEIGHT > 16 AND SP.S# = S 2 ) ;

Modification Operations The SQL DML supports three modification operations: UPDATE DELETE INSERT Note these operations change the contents of the database, hence they may violate the integrity constraints.

UPDATE: The general format of UPDATE operation is; UPDATE table SET field = scalar-expression [, field = scalar-expression] [ WHERE condition] ; All records in table satisfying condition are modified in accordance with the assignments.

Change the color of part P 2 to yellow, increase its weight by 5, and set its city to unknown (null); UPDATE P SET COLOR = Yellow WEIGHT = WEIGHT + 5, CITY = NULL WHERE P# = P 2 ;

Double the status of all suppliers in London ; UPDATE S SET STATUS = STATUS * 2 WHERE CITY = London ;

Set the shipment quantity to zero for all supplies in London ; UPDATE SP SET QTY = 0 WHERE London = ( SELECT CITY FROM S WHERE S.S# = SP.S# ) ;

Update Multiple Tables: UPDATE S SET S# = S 9 WHERE S# = S 2 UPDATE SP SET S# = S 9 WHERE S# = S 2 The first UPDATE will force the database to become inconsistent, since now in shipment table there is a supplier S 2 that does not exist. The database remains in inconsistent state until after the second UPDATE is executed.

DELETE: general format; This command has the following DELETE FROM table [ WHERE condition ] ; All records in table satisfying condition are deleted.

Delete all shipments with quantity greater than 300; DELETE FROM SP WHERE QTY > 300 ; Delete supplier S 5 ; DELETE FROM S WHERE S# = S 5 ;

Delete all shipments; DELETE FROM SP ; Note that now SP is an empty table. Delete all shipments for suppliers in London ; DELETE FROM SP WHERE London = ( SELECT CITY FROM S WHERE S.S# = SP.S# ) ;

INSERT: Insert comes in two formats; INSERT INTO table [ (field [, field ] ) ] VALUE (literal [, literal ] ) ; In this case a record with the contents defined in VALUE clause is added to the table.

INSERT INTO table [ (field [, field ] ) ] sub-query ; In this case, the result of the sub-query (may be multiple rows) is added to the table. In both cases, omitting the list of fields means all fields in the table, in left to right order.

Add part P 7 (city Athens, weight 24) name and color unknown to the P relation; INSERT INTO P ( P#, CITY, WEIGHT) VALUE ( P 7, Athens, 24) ; Note we assumed that COLOR and Pname are not defined as NOT NULL.

Add part P 8 (name Sprocket, color Pink, city Nice, weight 14) to the P relation; INSERT INTO P VALUE ( P 8, Sprocket, Pink, 14, Nice ) ; Add a new shipment with supplier number S 20, part number P 20 and quantity 1000. INSERT INTO SP (S#, P#, QTY) VALUE ( S 20, P 20, 1000 ) ;

For each part supplied, get the part number and the total quantity supplied supported for that part and save the result in the database; CREATE TABLE TEMP ( P# CHAR (6) NOT NULL, TOTQTY INTEGER NOT NULL, PRIMARY KEY (P# )) ;

INSERT INTO TEMP (P#, TOTQTY ) SELECT (P#, SUM (QTY) FROM P# GROUP BY P# ; SELECT is executed and result is copied in Temp relation. User, now, can do whatever he/she wants to do with Temp relation. Eventually, DROP TABLE TEMP ; will eliminate Temp relation from the database.

Questions: Using the following relations; S P S# Sname Status City S 1 Smith 20 London S 2 Jones 10 Paris S 3 Blake 30 Paris S 4 Clark 20 London S 5 Adams 30 Athens P# Pname Color Weight City P 1 Nut Red 12 London P 2 Bolt Green 17 Paris P 3 Screw Blue 17 Rome P 4 Screw Red 14 London P 5 Cam Blue 12 Paris P 6 Cog Red 19 London SP S# P# QTY S 1 P 1 300 S 1 P 2 200 S 1 P 3 400 S 1 P 4 200 S 1 P 5 100 S 1 P 6 100 S 2 P 1 300 S 2 P 2 400 S 3 P 2 200 S 4 P 2 200 S 4 P 4 300 S 4 P 5 400

Get supplier names for suppliers who supply part P 2. Get supplier names for suppliers who supply at least one red part. Get supplier names for suppliers who supplies all parts. Get supplier numbers for suppliers who supply at least all those parts supplied by supplier S 2. Get supplier names for suppliers who do not supply part P 2. Get all pairs of supplier numbers such that the two suppliers concerned are co-located