Relational Database Systems 1 – Wolf-Tilo Balke – Institut Für Informationssysteme – TU Braunschweig 2
Total Page:16
File Type:pdf, Size:1020Kb
Overview • Homework • Normalization Relational – Functional dependencies Database Systems 1 – 2NF – 3NF Wolf-Tilo Balke – BCNF Joachim Selke – 4NF, 5NF, 6NF Institut für Informationssysteme • Denormalization Technische Universität Braunschweig www.ifis.cs.tu-bs.de Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 2 Exercise 8.1 Exercise 8.1 Hotel(hotelNo, hotelName, city) • Again, our hotel database: Room(roomNo, hotelNo → Hotel, type, price) Booking(hotelNo → Hotel/Room, guestNo → Guest, dateFrom, dateTo, roomNo → Room) Hotel(hotelNo, hotelName, city) Guest(guestNo, guestName, guestAddress) Room(roomNo, hotelNo → Hotel, type, price) Booking(hotelNo → Hotel/Room, guestNo → Guest, dateFrom, dateTo, roomNo → Room) Guest(guestNo, guestName, guestAddress) • CREATE SCHEMA hotelinfo • SET SCHEMA hotelinfo • Provide all SQL statements (in the right order!) that are necessary to create • CREATE TABLE hotel ( the table structure given above (including all primary keys and referential hotelNo INTEGER NOT NULL PRIMARY KEY, integrity), and additionally ensure the following: hotelName VARCHAR(200) NOT NULL, – All table should be contained in a new schema called hotelinfo – Allowed room types are single, double, and family city VARCHAR(100) NOT NULL) – The price of each room must be between 10 and 100 Euros – The same guest cannot have overlapping bookings at the same hotel Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 3 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 4 Exercise 8.1 Exercise 8.1 Hotel(hotelNo, hotelName, city) Hotel(hotelNo, hotelName, city) Room(roomNo, hotelNo → Hotel, type, price) Room(roomNo, hotelNo → Hotel, type, price) Booking(hotelNo → Hotel/Room, guestNo → Guest, dateFrom, dateTo, roomNo → Room) Booking(hotelNo → Hotel/Room, guestNo → Guest, dateFrom, dateTo, roomNo → Room) Guest(guestNo, guestName, guestAddress) Guest(guestNo, guestName, guestAddress) • CREATE TABLE room ( • roomNo INTEGER NOT NULL, CREATE TABLE guest ( hotelNo INTEGER NOT NULL REFERENCES hotel, guestNo INTEGER NOT NULL PRIMARY KEY, type VARCHAR(10) NOT NULL guestName VARCHAR(200) NOT NULL, CHECK (type IN (’single’, ’double’, ’family’)), guestAddress VARCHAR(1000)) price NUMERIC(3, 2) NOT NULL CHECK (price BETWEEN 10 AND 100), PRIMARY KEY (roomNo, hotelNo)) Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 5 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 6 Exercise 8.1 Exercise 8.2 • CREATE TABLE booking ( • The relational schema of a product database: hotelNo INTEGER NOT NULL REFERENCES hotel, guestNo INTEGER NOT NULL REFERENCES guest, – product(model, maker, price) dateFrom DATE NOT NULL, dateTo DATE NOT NULL, – pc(model → product, speed, ram, hd) roomNo INTEGER NOT NULL, – laptop(model → product, speed, ram, hd, screen) PRIMARY KEY (hotelNo, guestNo, dateFrom), FOREIGN KEY (hotelNo, roomNo) REFERENCES room, – printer(model → product, color, type) CHECK (dateFrom <= dateTo), CHECK (NOT EXISTS ( SELECT * FROM booking b1, booking b2 WHERE b1.hotelNo = b2.hotelNo AND b1.guestNo = b2.guestNo AND b1.dateFrom < b2.dateFrom AND b1.dateTo > b2.dateFrom))) Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 7 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 8 Exercise 8.2 Exercise 8.2 – product(model, maker, price) – product(model, maker, price) – pc(model → product, speed, ram, hd) – pc(model → product, speed, ram, hd) – laptop(model → product, speed, ram, hd, screen) – laptop(model → product, speed, ram, hd, screen) – printer(model → product, color, type) – printer(model → product, color, type) • Give SQL statements to modify the database as follows: b) Store the fact that PC model 1500 is made by manufacturer A, has speed 3.1, RAM 1024, hard disk 300, and sells for 2499 Euros. a) Delete all PCs with less than 200 gigabytes of hard disk. INSERT INTO product VALUES (1500, ’A’, 2499) CREATE TABLE temp (model INTEGER) INSERT INTO pc VALUES(1500, 3.1, 1024, 300) INSERT INTO temp (SELECT model FROM pc WHERE hd < 200 DELETE FROM pc WHERE model IN (SELECT * FROM temp) DELETE FROM product WHERE model IN (SELECT * FROM temp) DROP TABLE temp (As an alternative solution, you can make the assumption that all foreign keys have been defined using ON DELETE CASCADE. Then, only the deletion from the pc table has to be performed.) Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 9 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 10 Exercise 8.2 Exercise 8.2 – product(model, maker, price) – product(model, maker, price) – pc(model → product, speed, ram, hd) – pc(model → product, speed, ram, hd) – laptop(model → product, speed, ram, hd, screen) – laptop(model → product, speed, ram, hd, screen) – printer(model → product, color, type) – printer(model → product, color, type) c) Delete all laptops made by a manufacturer that does not make d) Manufacturer B buys manufacturer C. PCs. Change all products made by C so they are now made by B. CREATE TABLE temp (model INTEGER) UPDATE product SET maker = ’B’ WHERE maker = ’C’ INSERT INTO temp (SELECT model FROM laptop WHERE maker NOT IN e) For each PC, double the amount of hard disk and (SELECT maker FROM pc JOIN product pr ON pc.model = pr.model)) add 1024 megabytes to the amount of RAM. DELETE FROM laptop WHERE model IN (SELECT * FROM temp) DELETE FROM product WHERE model IN (SELECT * FROM temp) UPDATE pc SET (hd, ram) = (2 * hd, ram + 1024) DROP TABLE temp (Alternative solution: see above) Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 11 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 12 Exercise 8.2 Exercise 8.2 – product(model, maker, price) – product(model, maker, price) – pc(model → product, speed, ram, hd) – pc(model → product, speed, ram, hd) – laptop(model → product, speed, ram, hd, screen) – laptop(model → product, speed, ram, hd, screen) – → – printer(model → product, color, type) printer(model product, color, type) g) Insert the facts that for every laptop there is a PC with the same f) For each laptop made by manufacturer D, add 1 inch to manufacturer, speed, RAM, hard disk, a model number that is the screen size and subtract 200 Euros from the price. 1100 less, and a price that is 500 Euros less. CREATE TABLE temp (model INTEGER) CREATE TABLE temp (model INTEGER) INSERT INTO temp INSERT INTO temp (SELECT l.model (SELECT model FROM laptop l WHERE NOT EXISTS FROM laptop l JOIN product p ON l.model = p.model (SELECT * FROM product p WHERE p.model = l.model - 1100)) WHERE maker = ’D’) INSERT INTO product UPDATE laptop SET screen = screen + 1 (SELECT t.model - 1100, maker, price - 500 FROM temp t JOIN product p ON t.model = p.model) WHERE model IN (SELECT * FROM temp) INSERT INTO pc UPDATE product SET price = price - 200 (SELECT t.model - 1100, speed, ram, hd WHERE model IN (SELECT * FROM temp) FROM temp t JOIN laptop l ON t.model = p.model) DROP TABLE temp DROP TABLE temp Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 13 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 14 Exercise 8.3 Exercise 8.3 – marks(studentid, score) • A relation containing exam scores: – marks(studentid, score) • Write SQL queries to do the following: • We wish to assign grades to students based on a) Display the grade for each student, based on the marks relation. scores as follows: SELECT studentid, – Grade F if score < 40, CASE – grade C if 40 ≤ score < 60, WHEN score < 40 THEN ’F’ – grade B if 60 ≤ score < 80, and WHEN score < 60 THEN ’C’ WHEN score < 80 THEN ’B’ – grade A if 80 ≤ score. ELSE ’A’ END FROM marks Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 15 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 16 Exercise 8.3 Exercise 8.4 – marks(studentid, score) • The SQL-92 standard provides an n-ary operation • Write SQL queries to do the following: called COALESCE, which is defined as follows: a) Find the number of students with each grade. WITH grades(studentid, grade) AS COALESCE(A1, A2, ..., An) returns the first nonnull (SELECT studentid, Ai in the list A1, A2, ..., An and returns null if all of CASE WHEN score < 40 THEN ’F’ A1, A2, ..., An are null. WHEN score < 60 THEN ’C’ WHEN score < 80 THEN ’B’ • Show how to express the COALESCE operation ELSE ’A’ using the CASE operation. END AS grade FROM marks) SELECT grade, COUNT(*) AS count FROM grades GROUP BY grade Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 17 Relational Database Systems 1 – Wolf-Tilo Balke – Institut für Informationssysteme – TU Braunschweig 18 Exercise 8.4 9.1 Introduction • Show how to express the COALESCE operation • Up to now, we have learned ... using the CASE operation. – ... how the relational model works. – COALESCE(A1, A2, ..., An) is equivalent to the following – ... how it is implemented in current RDBMS. CASE operation with searched WHEN-clause: – ... how to create relational databases (SQL DDL). CASE – ... how to define constraints