Chapter 6 SQL Homework.

Slides:



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

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.
Chapter 4 Hotel (hotelno, hotelname, city)
Oracle 10g Express. Download Oracle 10g Express Oracle 10g Express Edition: – edition/overview/index.htmlhttp://
1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:
Alter an existing Table Note: (a) Primary key: SID + Course_code.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Assignment6-1 Assignment6-2 Due Wednesday, March 13 1.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Lecture#7 (cont’d): Rel. model - SQL part3.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Mid-term Exam Answer Keys Week 7 Spring I. Multiple Choices Questions Each may have more than one answer (6 x 4 = 24 points total)
Concepts of Database Management, 4th Edition, Pratt & Adamski
Chapter 5 SQL Homework.
Using SQL to create tables Ways of using Databases.
Dec 15, 2003Murali Mani Transactions and Security B term 2004: lecture 17.
Create, Insert, Delete, Update. Create Create database Create table Create index – Primary – Secondary.
Concepts of Database Management Sixth Edition
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Project Phase I Phase II Due Monday, April 15 Groups 1.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Information Resources Management March 6, Agenda n Administrivia n SQL Part 2 n Homework #6.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Slide 1 Chapter 7 – Part 1 Data Definition Language & Data Manipulation Language.
CSc-340 3b1 Intermediate SQL Chapter 4 [2 of 2] Phase 1 of Student Projects SQL Data Types & Schemas Authorization.
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
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.
SQL queries ordering and grouping and joins
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
Chapter 5 SQL: Data Manipulation Thomas Connolly, Carolyn Begg, Database System, A Practical Approach to Design Implementation and Management, 4 th Edition,
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
SQL introduction 2013.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
1 Access control. 2 GRANT statement 3 Access control GRANT statement GRANT statement may be used by a user to authorise various kinds of access to his/her.
DBAdmin D D D D D a a a a a t t t t t a a a a a b b b b b a a a a a s s s s s e e e e eAdministration / Embedded SQL by: Glen Orsburn Chapter 6.
INF 280 Database Systems SQL:Join
Populating and Querying tables Insert, Update, Delete and View (DML)
CMPT 258 Database Systems Midterm Review. Regarding the Exam Oct 15 Thursday Close book Cheat sheet (1 side of an A4 paper)
CS 3630 Database Design and Implementation. Null Value The value of an attribute could be NULL NOT known at the moment or NOT Applicable Example Cell.
Constraints Review. What is a constraint? Unique – forbids duplicate values Referencial – Foreign key Check Constraint – sets restrictions on data added.
1 Announcements Reading for next week: Chapters 6 and 7  Next-to-Last reading from the text for a little while (I promise) Your database accounts should.
CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View Virtual table Records are not stored on disk Query is stored.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
SQL: Data Manipulation. Objectives Describe the purpose and importance of SQL. Demonstrate how to retrieve data from database using SELECT and: ▫Use compound.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
CS 3630 Database Design and Implementation. Joins Retrieve data from two or more tables Join Conditions PK and FK (Natural Join) Other attributes (Theta.
Copyright © Curt Hill SQL The Data Manipulation Language.
CS 3630 Database Design and Implementation
Assignment 2 Relational Algebra Which tables? What operations?
Managing Tables, Data Integrity, Constraints by Adrienne Watt
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
Thomas M. Connolly Carolyn E. Begg
Latihan Answer the following questions using the relational schema from the Exercises at the end of Chapter 3: Create the Hotel table using the integrity.
Latihan Create a separate table with the same structure as the Booking table to hold archive records. Using the INSERT statement, copy the records from.
Assignment 2.
CS 3630 Database Design and Implementation
אילוצי שלמות אילוצי תחום מפתחות ומפתחות זרים טריגרים triggers
CS 3630 Database Design and Implementation
אבטחת נתונים בסביבת SQL Data Security
CS 3630 Database Design and Implementation
Presentation transcript:

Chapter 6 SQL Homework

18. List the details of all rooms at the Grosvenor Hotel, including the name of the guest staying in the room, if the room is occupied.

Create a view with every room having a guest CREATE VIEW roomocp (hotelno, roomno, type, price, guestname) AS SELECT r.hotelno, r.roomno, r.type, r.price, g.guestname FROM hotel h, room r, booking b, guest g WHERE h.name = ‘Grosvenor’ AND (b.datefrom <= ‘SYSTEM DATE’ AND b.dateto >= ‘SYSTEM DATE’) AND h.hotelno = r.hotelno AND r.hotelno = b.hotelno AND r.roomno = b.roomno AND b.guestno = g.guestno;

Create a view of every room CREATE VIEW roomall (hotelno, roomno, type, price) AS SELECT r.hotelno, r.roomno, r.type, r.price FROM hotel h, room r WHERE h.hotelname ='Grosvenor’ AND h.hotelno = r.hotelno;

Find the answer SELECT r.roomno, r.hotelno, r.type, r.price, p.guestname FROM roomall r LEFT JOIN roomocp p ON r.roomno = p.roomno;

6.10 – 6.11 Create the Hotel, Room, Booking and Guest tables Hotel (hotelno, hotelname, city) Room (roomno, hotelno, Type, price) Booking (hotelno, guestno, datefrom, dateto, roomno) Guest (guestno, guestname, guestaddress)

CREATE TABLE hotel (hotelno NUMBER(5) NOT NULL, hotelname CHAR(20) NOT NULL, city CHAR(30) NOT NULL, CONSTRAINT pkhotel PRIMARY KEY (hotelno));

CREATE TABLE room Type must be one of Single, Double, or Family Price must be between $10 and $100 Roomno must be between 1 and 100

CREATE TABLE room (roomno NUMBER(3) NOT NULL CHECK (VALUE BETWEEN 1 AND 100), hotelno NUMBER(5) NOT NULL, type CHAR(8) CHECK (VALUE IN (‘Single’, ‘Double’, ‘Family’)), price NUMBER(5,2) CHECK (VALUE BETWEEN 10 AND 100), CONSTRAINT pkroom PRIMARY KEY (roomno, hotelno), CONSTRAINT fkrh FOREIGN KEY (hotelno) REFERENCES hotel (hotelno));

CREATE TABLE guest (guestno NUMBER(10) NOT NULL, guestname CHAR(20) NOT NULL, guestaddress CHAR(30) NOT NULL, CONSTRAINT pkguest PRIMARY KEY (guestno));

CREATE TABLE booking Datafrom and dateto must be greater than today’s date

Create TABLE booking (hotelno NUMBER(5) NOT NULL, guestno NUMBER(10) NOT NULL, datefrom DATE CHECK (datefrom > ‘systemdate’), dateto DATE CHECK (dateto > ‘systemdate’), roomno NUMBER(3) NOT NULL, CONSTRAINT pkbooking PRIMARY KEY (hotelno, guestno, roomno, datefrom), CONTRAINT fkbh FOREIGN KEY (hotelno) REFERENCES hotel (hotelno), CONTRAINT fkbg FOREIGN KEY (guestno) REFERENCES guest (guestno), CONTRAINT fkbr FOREIGN KEY (roomno, hotelno) REFERENCES room (roomno, hotelno));

The same room cannot be double-booked CONSTRAINT roombooked CHECK (NOT EXISTS (SELECT * FROM booking b WHERE b.datefrom <= booking.dateto AND b.dateto >= booking.datefrom AND b.roomno = booking.roomno AND b.hotelno = booking.hotelno))

The same guest cannot have overlapping booking CONSTRAINT guestbooked CHECK (NOT EXISTS (SELECT * FROM booking b WHERE b.datefrom <= booking.dateto AND b.dateto >= booking.datefrom AND b.guestno = booking.guestno))

6.12 Create a separate table with the same structure as the Booking table to hold archive records. Using the INSERT statement, copy the records from the Booking table to the archive table relating to bookings before 1st January 2003. Delete all bookings before 1st January 2003 from the Booking table.

CREATE TABLE bookinghis (hotelno NUMBER(5) NOT NULL, guestno NUMBER(10) NOT NULL, datefrom DATE, dateto DATE, roomno NUMBER(3) NOT NULL, CONSTRAINT pkbhh PRIMARY KEY (hotelno, guestno, datefrom), CONSTRAINT fkbh FOREGN KEY (hotelno) REFERENCES hotel (hotelno), CONSTRAINT fkbg FOREIGN KEY (guestno) REFERENCES guest (guestno), CONSTRAINT fkbr FOREIGN KEY (roomno, hotelno) REFERENCES room (roomno, hotelno));

INSERT INTO bookinghis (hotelno, guestno, datefrom, dateto, roomno) VALUES (SELECT * FROM booking WHERE dateto < '1/1/2003');   DELETE FROM booking WHERE dateto < '1/1/2003';

6.13 Create a view containing the hotel name and the names of the guests staying at the hotel.

CREATE VIEW hotgst (hotelname, guestname) AS SELECT hotelname, guestname FROM hotel h, booking b, guest g WHERE h.hotelno = b.hotelno AND b.guestno = g.guestno AND (datefrom <= ‘systemdate’) AND (dateto >=‘systemdate’);

6.14 Create a view containing the account for each guest at the Gorsvenor Hotel.

CREATE VIEW gact (guestno, guestname guestaddress) AS SELECT guestno, guestname, guestaddress FROM hotel h, booking b, guest g WHERE h.hotelno = b.hotelno AND b.guestno = g.guestno AND h.hotelname=‘Grosvenor’;

6.15 Give the users Manager and Director full access to these views, with the privilege to pass the access on to other users.

GRANT ALL PRIVILEGES ON gact TO Manger, Director WITH GRANT OPTIONS; ON hotgst

6. 16 Give the users Accounts SELECT access to these views 6.16 Give the users Accounts SELECT access to these views. Now revoke the access from user.

GRANT SELECT ON gact, hotgst TO Accounts; REVOKE SELECT FROM Accounts;