CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View...

20
CS 3630 Database Design and Implementation

description

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

Transcript of CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View...

Page 1: 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

Page 2: 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.

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

Page 3: 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.

Horizontal views (a subset)

Create View HotelsOfGlasgow asSelect *From HotelWhere Name = 'Glasgow';

Desc HotelsOfGlasgow

select * from HotelsOfGlasgow;

Drop View RoomsInHotel;

3

Page 4: 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.

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

Page 5: 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.

Horizontal and Vertical View

Create or replace view BookingOfGlasgow asSelect Room_No, Date_from, Date_toFrom BookingWhere Hotel_No in ('H05', 'H28');

5

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

Database System and File System

Views in a File System?

15

Page 16: 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.

Database Views

• Look like base tables• Used in queries the same way as base tables• Insert?• Delete?• Update?

16

Page 17: 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.

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

Page 18: 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.

Assignmet 9

Any Questions?

18

Page 19: 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.

Phase III

Any Questions?

19

Page 20: 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.

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