0% found this document useful (0 votes)
1K views19 pages

Practice For SQL and Constraints : Product-PC-Laptop-Printer

model INT PRIMARY KEY, speed INT, This document discusses creating and querying ram INT, tables to store information about PCs, laptops hd INT, and printers. It provides SQL statements to: screen INT, 1) Create the tables and define primary and price FLOAT, foreign keys. 2) Insert, update, delete and CONSTRAINT chk_speed CHECK query records in the tables to answer questions (speed >= 800) about the data. 3) Add constraints such as ); minimum speed for laptops and allowed printer types.

Uploaded by

nik27s
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views19 pages

Practice For SQL and Constraints : Product-PC-Laptop-Printer

model INT PRIMARY KEY, speed INT, This document discusses creating and querying ram INT, tables to store information about PCs, laptops hd INT, and printers. It provides SQL statements to: screen INT, 1) Create the tables and define primary and price FLOAT, foreign keys. 2) Insert, update, delete and CONSTRAINT chk_speed CHECK query records in the tables to answer questions (speed >= 800) about the data. 3) Add constraints such as ); minimum speed for laptops and allowed printer types.

Uploaded by

nik27s
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Practice for

SQL and Constraints


(Product-PC-Laptop-Printer)
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 ( CREATE TABLE Laptop (
model INT,
maker CHAR(10),
speed INT,
model INT, ram INT,
type CHAR(5) hd INT,
); screen INT,
price INT
);
CREATE TABLE PC (
model INT, CREATE TABLE Printer (
speed INT, model INT,
ram INT, color CHAR(1),
hd INT, type CHAR(5),
price INT
rd INT,
);
price INT
);
Product(maker, model, type) a) Find those manufacturers
PC(model, speed, ram, hd, rd, price) that sell Laptops, but not
PC's.
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)

(SELECT maker
FROM Laptop NATURAL JOIN Product)
MINUS
(SELECT maker
FROM PC NATURAL JOIN Product);
Product(maker, model, type) b) Find those hard-disk sizes
PC(model, speed, ram, hd, rd, price) that occur in two or more
PC's.
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)

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

SELECT maker Or:


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

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

INSERT INTO PC(model,speed,ram,hd,rd,price)


VALUES(1100,1800,256,80,20,2499);
Product(maker, model, type) f) Insert the facts that for
PC(model, speed, ram, hd, rd, price) every PC there is a laptop
with the same
Laptop(model, speed, ram, hd, screen, price) manufacturer, speed, RAM
Printer(model, color, type, price) and hard disk, a 15-inch
screen, a model number
1000 greater, and a price
INSERT INTO Product(maker,model,type)
$500 more.
(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) g) Delete all PC’s with less
PC(model, speed, ram, hd, rd, price) than 20 GB of hard disk.
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)

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

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) i) Manufacturer A buys
PC(model, speed, ram, hd, rd, price) manufacturer B. Change all
products made by B so
Laptop(model, speed, ram, hd, screen, price) they are now made by A.
Printer(model, color, type, price)

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

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

UPDATE Laptop
SET screen=screen+1, price=price-100
WHERE model IN
(SELECT model
FROM Product
WHERE maker='B'
);
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.
Product(maker, model, type) First create keys and
PC(model, speed, ram, hd, rd, price) foreign key references.
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)
CREATE TABLE Product (
maker VARCHAR(10), CREATE TABLE Laptop (
model INT PRIMARY KEY, model INT PRIMARY KEY,
type VARCHAR(10) speed INT,
); ram INT,
hd INT,
CREATE TABLE PC ( screen INT,
model INT PRIMARY KEY, price FLOAT,
speed INT, CONSTRAINT fk_lap FOREIGN
KEY(model) REFERENCES
ram INT,
Product(model)
hd INT,
ON DELETE CASCADE
rd INT,
);
price FLOAT,
CONSTRAINT fk_pc FOREIGN KEY(model)
REFERENCES Product(model)
ON DELETE CASCADE
);
Product(maker, model, type) a) The speed of a laptop must
PC(model, speed, ram, hd, rd, price) be at least 800.
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)

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) b) The only types of printers
PC(model, speed, ram, hd, rd, price) are laser, ink-jet, and
Laptop(model, speed, ram, hd, screen, price)
bubble.

Printer(model, color, type, price)

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) c) A model of a product must
PC(model, speed, ram, hd, rd, price) also be the model of a PC,
Laptop(model, speed, ram, hd, screen, price)
a laptop, or a printer.

Printer(model, color, type, price)

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.

You might also like