Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CS 3630 Database Design and Implementation

2 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

3 Horizontal views (a subset) Create View HotelsOfGlasgow as Select * From Hotel Where Name = 'Glasgow'; Desc HotelsOfGlasgow select * from HotelsOfGlasgow; Drop View RoomsInHotel; 3

4 Vertical View (Projection) Table Schema Booking (Hotel_no, Guest_no, Date_from, Date_to, Room_No) Create or replace view HotelBooking as Select Hotel_no, Room_No, Date_from, Date_to From Booking; -- Cannot use Create or replace for tables 4

5 Horizontal and Vertical View Create or replace view BookingOfGlasgow as Select Room_No, Date_from, Date_to From Booking Where Hotel_No in ('H05', 'H28'); 5

6 Horizontal and Vertical View -- using view HotelsOfGlasgow Create or replace view BookingOfGlasgow as Select Room_No, Date_from, Date_to From Booking Where Hotel_No in (Select Hotel_No From HotelsOfGlasgow); 6

7 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

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 HotelG; ERROR at line 3: ORA-00904: "HOTELG": invalid identifier 8

9 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

10 Sub-Queries -- List guests who don’t have any bookings during -- April 2005. Select * From guest Where guest_no Not IN (Select Distinct guest_no From booking Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05'); 10

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 guest Where guest_no Not IN BookingApril2005; ORA-00904: "BOOKINGAPRIL2005": invalid identifier 11

12 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 guest Where guest_no Not IN (Select * From BookingApril2005); 12

13 Using WITH With BookingApril2005 as (Select guest_no From booking Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05') Select * From guest Where guest_no Not IN (Select * From BookingApril2005); Must have () for the With sub-query 13

14 Outer Join -- List guests who don’t have any bookings during -- April 2005. Select G.* From guest G Left 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

15 Database System and File System Views in a File System? 15

16 Database Views Look like base tables Used in queries the same way as base tables Insert? Delete? Update? 16

17 Insert/Delete/Update Views -- View based on group Create 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

18 Assignmet 9 Any Questions? 18

19 Phase III Any Questions? 19

20 Schedule Assignment 10 Start Friday Quiz 4 Friday May 1 Test 2 Wednesday, May 6 Phase IV Friday, May 8 Final Exam Thursday, May 14 7 PM (Group #10) 20


Download ppt "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."

Similar presentations


Ads by Google