Midterm Review/Practice for SQL and Constraints. Exercise – Suppliers and Parts Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price)

Slides:



Advertisements
Similar presentations
Practice for SQL and Constraints (Product-PC-Laptop-Printer)
Advertisements

COP4540 Database Management System Midterm Review
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
Database Languages Chapter 7. The Relational Algebra.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
COP-5725 Practice Exercises
Chapter (7): Advanced SQL
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,
Relational Algebra.
Relational Operations on Bags Extended Operators of Relational Algebra.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Practice (Suppliers, Parts, Catalog)
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Triggers Examples.
Subqueries Example Find the name of the producer of ‘Star Wars’.
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. Some basic ones Some basic integrity constraints: primary keys, not null constraints, and unique constraints. Examples: CREATE TABLE Movies.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
Relational Operations on Bags Extended Operators of Relational Algebra.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Database Systems Lecture 5 Natasha Alechina
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
SQL. Query SELECTList_Of_Attributes FROM List_Of_Tables [WHERECondition] [ORDER BY List_Of_Ordering_Attributes ] [GROUP BY List_Of_Grouping_Attributes.
Onsdag The concepts in a relation data model SQL DDL DML.
Slide 1 Chapter 7 – Part 1 Data Definition Language & Data Manipulation Language.
Structured Query Language Chris Nelson CS 157B Spring 2008.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
SQL introduction 2013.
SQL John Nowobilski. What is SQL? Structured Query Language Manages Data in Database Management Systems based on the Relational Model Developed in 1970s.
COMP3030 Database Management System Final Review
1 SQL Insert Update Delete Create table Alter table.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
CMPT 258 Database Systems Midterm Review. Regarding the Exam Oct 15 Thursday Close book Cheat sheet (1 side of an A4 paper)
SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Working with MySQL A290/A590, Fall /07/2014.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
1 Indexes ► Sort data logically to improve the speed of searching and sorting operations. ► Provide rapid retrieval of specified rows from the table without.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
SQL Exercises. 1) Names of suppliers. Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)
Basic SQL Queries Go over example queries, like 10 > ALL.
Lecture 05: SQL Wednesday, January 12, 2005.
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CMSC-461 Database Management Systems
Lecture 06: SQL Monday, October 11, 2004.
실습 4주차.
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Presentation transcript:

Midterm Review/Practice for SQL and Constraints

Exercise – Suppliers and Parts Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price) a)Find the names of suppliers who supply every part. b)Find the names of suppliers who supply every red part. c)Find the part names supplied by IBM and no one else. d)Find the sid’s of suppliers who charge more for some part that the average price of that part (averaged over all suppliers who supply that part.) e)For each part, find the name of the supplier who charges the least for that part. f)For all suppliers that supply more than three red parts find how many green parts they supply.

Creation of Tables CREATE TABLE Suppliers ( sid INT, sname VARCHAR(20), address VARCHAR(20) ); CREATE TABLE Parts ( pid INT, pname VARCHAR(10), color VARCHAR(10) ); CREATE TABLE Catalog ( sid INT, pid INT, price INT );

SELECT sname FROM Suppliers X WHERE NOT EXISTS ( --If a supplier supplies all the parts, then this subq. should return empty result (SELECT pid FROM Parts) MINUS (SELECT pid FROM Catalog WHERE sid=X.sid) ); Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price) a)Find the names of suppliers who supply every part.

SELECT sname FROM Suppliers X WHERE NOT EXISTS ( (SELECT pid FROM Parts WHERE color='red') MINUS (SELECT pid FROM Catalog NATURAL JOIN Parts WHERE sid=X.sid AND color='red') ); Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price) b)Find the names of suppliers who supply every red part.

CREATE VIEW SupCatPar AS SELECT sid, sname, address, pid, pname, color, price FROM Suppliers NATURAL JOIN Catalog NATURAL JOIN Parts; SELECT pname FROM SupCatPar WHERE sname='IBM' AND pid NOT IN ( SELECT pid FROM SupCatPar WHERE sname<>'IBM' ); Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price) c)Find the part names supplied by IBM and no one else.

SELECT sid FROM Catalog X WHERE price > ( SELECT AVG(price) FROM Catalog WHERE pid=X.pid ); Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price) d)Find the sid’s of suppliers who charge more for some part that the average price of that part (averaged over all suppliers who supply that part.)

SELECT pname, sname FROM SupCatPar X WHERE X.price = ( SELECT MIN(price) FROM Catalog WHERE pid=X.pid ); Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price) e)For each part, find the name of the supplier who charges the least for that part.

CREATE VIEW SIDs_RED AS SELECT sid FROM SupCatPar WHERE color='red' GROUP BY sid HAVING COUNT(pid)>3; CREATE VIEW SupCatPar_Green AS SELECT * FROM SupCatPar WHERE color='green'; SELECT sname, COUNT(pid) AS number_green_parts FROM SIDs_RED NATURAL LEFT OUTER JOIN SupCatPar_Green GROUP BY sid, sname; DROP VIEW SIDs_RED; DROP VIEW SupCatPar_Green; Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price) f)For all suppliers that supply more than three red parts find how many green parts they supply.

Exercise – PC/Laptop/Printer Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) a)Find those manufacturers that sell Laptops, but not PC's b)Find those hard-disk sizes that occur in two or more PC's. c)Find those manufacturers of at least two different computers (PC or Laptops) with speed of at least 700. d)Find the manufacturers who sell exactly three different models of PC. e)Using two INSERT statements, store in the database the fact that PC model 1100 is made by manufacturer C, has speed 1800, RAM 256, hard disk 80, a 20x DVD, and sells for $2499. f)Insert the facts that for every PC there is a laptop with the same manufacturer, speed, RAM and hard disk, a 15-inch screen, a model number 1000 greater, and a price $500 more. g)Delete all PC’s with less than 20 GB of hard disk. h)Delete all laptops made a manufacturer that doesn’t make printers. i)Manufacturer A buys manufacturer B. Change all products made by B so they are now made by A. j)For each PC, double the amount of RAM and add 20 GB to the amount of hard disk. k)For each laptop made by manufacturer B, add one inch to the screen size and subtract $100 from the price.

Creation of Tables CREATE TABLE Product ( maker CHAR(10), model INT, type CHAR(5) ); CREATE TABLE PC ( model INT, speed INT, ram INT, hd INT, rd INT, price INT ); CREATE TABLE Laptop ( model INT, speed INT, ram INT, hd INT, screen INT, price INT ); CREATE TABLE Printer ( model INT, color CHAR(1), type CHAR(5), price INT );

(SELECT maker FROM Laptop NATURAL JOIN Product) MINUS (SELECT maker FROM PC NATURAL JOIN Product); Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) a)Find those manufacturers that sell Laptops, but not PC's.

SELECT hd FROM PC GROUP BY hd HAVING COUNT(model) >= 2; Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) b)Find those hard-disk sizes that occur in two or more PC's.

SELECT maker FROM ( (SELECT model, speed FROM PC) UNION (SELECT model, speed FROM Laptop) ) NATURAL JOIN Product WHERE speed>=700 GROUP BY maker HAVING COUNT(model) >= 2; Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) c)Find those manufacturers of at least two different computers (PC or Laptops) with speed of at least 700. Or: SELECT maker FROM ( (SELECT model, speed FROM PC) UNION (SELECT model, speed FROM Laptop) ) C JOIN Product ON C.model=Product.model WHERE C.speed>=700 GROUP BY Product.maker HAVING COUNT(C.model) >= 2;

SELECT Product.maker FROM PC, Product WHERE PC.model=Product.model GROUP BY Product.maker HAVING COUNT(PC.model)=3; Or: SELECT maker FROM PC NATURAL JOIN Product GROUP BY maker HAVING COUNT(model)=3; Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) d)Find the manufacturers who sell exactly three different models of PC.

INSERT INTO Product(maker,model,type) VALUES('C',1100,'PC'); INSERT INTO PC(model,speed,ram,hd,rd,price) VALUES(1100,1800,256,80,20,2499); Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) e)Using two INSERT statements, store in the database the fact that PC model 1100 is made by manufacturer C, has speed 1800, RAM 256, hard disk 80, a 20x DVD, and sells for $2499.

INSERT INTO Product(maker,model,type) (SELECT maker,model+1000,'Laptop' FROM Product WHERE type='PC' ); INSERT INTO Laptop(model,speed,ram,hd,screen,price) (SELECT model+1000, speed, ram, hd, 15, price+500 FROM PC ); Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) f)Insert the facts that for every PC there is a laptop with the same manufacturer, speed, RAM and hard disk, a 15-inch screen, a model number 1000 greater, and a price $500 more.

DELETE FROM PC WHERE hd<20; Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) g)Delete all PC’s with less than 20 GB of hard disk.

DELETE FROM Laptop WHERE model IN (SELECT model FROM Product WHERE maker IN ( (SELECT maker FROM Product NATURAL JOIN Laptop) MINUS (SELECT maker FROM Product NATURAL JOIN Printer) ) ); Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) h)Delete all laptops made by a manufacturer that doesn’t make printers.

UPDATE Product SET maker='B' WHERE maker='C'; Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) i)Manufacturer A buys manufacturer B. Change all products made by B so they are now made by A.

UPDATE PC SET ram=ram*2, hd=hd+20; Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) j)For each PC, double the amount of RAM and add 20 GB to the amount of hard disk.

UPDATE Laptop SET screen=screen+1, price=price-100 WHERE model IN (SELECT model FROM Product WHERE maker='B' ); Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) k)For each laptop made by manufacturer B, add one inch to the screen size and subtract $100 from the price.

Constraints – PCs, Laptops, Printers Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) First create keys and foreign key references. Then create the following constraints. a)The speed of a laptop must be at least 800. b)The only types of printers are laser, ink-jet, and bubble. c)A model of a product must also be the model of a PC, a laptop, or a printer.

CREATE TABLE Product ( maker VARCHAR(10), model INT PRIMARY KEY, type VARCHAR(10) ); CREATE TABLE PC ( model INT PRIMARY KEY, speed INT, ram INT, hd INT, rd INT, price FLOAT, CONSTRAINT fk_pc FOREIGN KEY(model) REFERENCES Product(model) ON DELETE CASCADE ); Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) First create keys and foreign key references. CREATE TABLE Laptop ( model INT PRIMARY KEY, speed INT, ram INT, hd INT, screen INT, price FLOAT, CONSTRAINT fk_lap FOREIGN KEY(model) REFERENCES Product(model) ON DELETE CASCADE );

Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) a)The speed of a laptop must be at least 800. CREATE TABLE Laptop ( model INT PRIMARY KEY, speed INT CHECK(speed >= 800), ram INT, hd INT, screen INT, price FLOAT, CONSTRAINT fk_lap FOREIGN KEY(model) REFERENCES Product(model) ON DELETE CASCADE );

Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) b)The only types of printers are laser, ink-jet, and bubble. CREATE TABLE Printer ( model INT PRIMARY KEY, color VARCHAR(10), type VARCHAR(10) CHECK(type IN ('laser', 'ink-jet', 'bubble')), price FLOAT, CONSTRAINT fk_printer FOREIGN KEY(model) REFERENCES Product(model) ON DELETE CASCADE );

Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) c)A model of a product must also be the model of a PC, a laptop, or a printer. CREATE VIEW ProductSafe(maker,model,type) AS SELECT maker, model, type FROM Product WHERE model IN ( (SELECT model FROM PC) UNION (SELECT model FROM Laptop) UNION (SELECT model FROM Printer) ) WITH CHECK OPTION; Then, we insert into this view as opposed to directly into Product. Also, make the FOREIGN KEY constraints in PC, Laptop, and Printer deferrable initially deferred.