Upload
bertina-harper
View
213
Download
0
Embed Size (px)
DESCRIPTION
Horizontal views (a subset) Create View HotelsOfGlasgow as Select * From Hotel Where Name = 'Glasgow'; Desc HotelsOfGlasgow select * from HotelsOfGlasgow; Drop View RoomsInHotel; 3
Citation preview
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 Records are generated when requested
Looks like a table The users don’t know the difference
2
Horizontal views (a subset)
Create View HotelsOfGlasgow asSelect *From HotelWhere Name = 'Glasgow';
Desc HotelsOfGlasgow
select * from HotelsOfGlasgow;
Drop View RoomsInHotel;
3
Vertical View (Projection)Table SchemaBooking (Hotel_no, Guest_no, Date_from, Date_to, Room_No)
Create or replace view HotelBooking as Select Hotel_no, Room_No, Date_from, Date_toFrom Booking;
-- Cannot use Create or replace for tables
4
Horizontal and Vertical View
Create or replace view BookingOfGlasgow asSelect Room_No, Date_from, Date_toFrom BookingWhere Hotel_No in ('H05', 'H28');
5
Horizontal and Vertical View
-- using view HotelsOfGlasgow Create or replace view BookingOfGlasgow as Select Room_No, Date_from, Date_toFrom BookingWhere Hotel_No in (Select Hotel_No From HotelsOfGlasgow);
6
Views based on grouping
-- must give names to aggregate functions -- and all fields when one aggregate -- function exists. Create or replace view RoomsInHotel (Hotel_no, Room_Count) as Select Hotel_no, Count(*) From Room Group By Hotel_No;
7
Using Views in Queries
Create or Replace View HotelG As Select Hotel_No From Hotel Where Name = 'Grosvenor';
Select * From Room Where Hotel_No in HotelG;
ERROR at line 3:ORA-00904: "HOTELG": invalid identifier
8
Using Views in Queries
Create or Replace View HotelG As Select Hotel_No From Hotel Where Name = 'Grosvenor';
Select * From Room Where Hotel_No in (Select * from HotelG);
-- works if one value from the view Where Hotel_No = (Select * from HotelG);
9
Sub-Queries
-- List guests who don’t have any bookings during-- April 2005.
Select *From guestWhere guest_no Not IN (Select Distinct guest_no From booking Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05');
10
Using View-- List guests who don’t have any bookings for -- April 2005.
Create or Replace View BookingApril2005 as(Select guest_no From booking
Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05');
Select *From guestWhere guest_no Not IN BookingApril2005;
ORA-00904: "BOOKINGAPRIL2005": invalid identifier
11
Using View-- List guests who don’t have any bookings for -- April 2005.
Create or Replace View BookingApril2005 as(Select guest_no From booking
Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05');
Select *From guestWhere guest_no Not IN (Select * From BookingApril2005);
12
Using WITH
With BookingApril2005 as(Select guest_no From booking
Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05')Select *From guestWhere guest_no Not IN (Select * From BookingApril2005);
Must have () for the With sub-query
13
Outer Join
-- List guests who don’t have any bookings during-- April 2005.
Select G.*From guest GLeft Join Booking B on G.guest_no = B.guest_no and date_from <= '30-Apr-05' and date_to >= '01-Apr-05'Where B.guest_no is null;
14
Database System and File System
Views in a File System?
15
Database Views
• Look like base tables• Used in queries the same way as base tables• Insert?• Delete?• Update?
16
Insert/Delete/Update Views
-- View based on groupCreate or Replace view RoomsInHotel(Hotel, Room_Count) as Select Hotel_no, Count(*) From Room Group By Hotel_No;
Insert into RoomsInHotel Values ('H11', 5); -- should insert 5 records into Room with 'H11' -- which 5 rooms? -- RNo (part of PK) not specified.-- Cannot do it!
17
Assignmet 9
Any Questions?
18
Phase III
Any Questions?
19
Schedule• Assignment 10
Start Friday
• Quiz 4Friday May 1
• Test 2Wednesday, May 6
• Phase IVFriday, May 8
• Final ExamThursday, May 147 PM (Group #10)
20