CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking;...

23
CS 3630 Database Design and Implementation

Transcript of CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking;...

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

CS 3630 Database Design and Implementation

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

SQL QueryClause Select and From

Select *

From booking;

select hotel_no, guest_no, room_no

from booking;

select Distinct hotel_no, guest_no, room_no

from booking;

-- Remove duplicate records

-- Unique also works

2

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

Oracle Command Column (Col)Column Hotel_no format A9

Col Hotel_No format a9 heading 'Hotel No'

-- This also workcol Hotel_no format a9 heading "Hotel No"

-- to get leading decimal digitsColumn Price format $999.99 heading 'Price/Night'

-- to get leading 0sColumn Price format $099.99 heading 'Price/Night'

Col Date_To format a16 heading 'Date of Leaving' Col Date_From format a16 heading 'Date of|Arrival'

Select * from Room;

Select * from Booking;

Clear col 3

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

Clause Where

Select *

From Room

Where RType = 'Single'

-- Where clause to specify the condition

-- Operators And/Or

Select *

From Room

Where Price > 35 and RType = 'Single'

-- Where Price > 35 or RType = 'Single'

4

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

Operator NOT (!)

Select *

From Room

Where Price > 35 and RType != 'Family';

-- Where Price > 35 and Not (RType = 'Family‘)

-- Where Price > 35 and NOT RType = 'Family‘

-- Not working:

-- Where Price > 35 and !(RType = 'Family‘)

5

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

Operator NOT (!)

Select *

From Room

Where Not (Price > 35 and RType = 'Family');

-- Not the same!

Select *

From Room

Where Not Price > 35 and RType = 'Family';

--Where (Not Price > 35) and RType = 'Family';

--Where Price <= 35 and RType = 'Family';

6

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

Operator Between

Select *From RoomWhere Price between 30 and 45;

-- Operator Between-- inclusive

Select *From RoomWhere Price Not between 30 and 45;-- Where Not Price between 30 and 45;

7

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

Operator IN

Select *From RoomWhere Price in (30, 40, 45);-- Same as-- Where Price = 30 or Price = 40 or Price = 45

Select *From RoomWhere Price Not in (30, 40, 45);-- Same as-- Where Not Price in (30, 40, 45);

8

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

Comparing StringsOperator Like and %

Select *From guestWhere guest_name = 'Mary Tregear'Where guest_name like 'M%';-- All guests with name beginning with 'M‘-- %: any string of zero or more chars

9

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

Operator Like and Char ‘_’

-- All records whose Fname has 'M' for 3rd position

Select *

From Guest

Where guest_name like '__M%';

-- _: any single char

10

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

Null Value

Select *

From Guest

Where Address = null;

-- No row is selected

-- null != null

Select *

From Guest

Where Address is null;

-- Some rows may be selected

11

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

Changing Headings-- use " " for heading

-- Use ' ' for string

-- With or without "AS"

Select Guest_no, Guest_name as "Guest Name"

From Guest;

Select Guest_no, Guest_name "Guest Name"

From Guest;

Select Guest_no, Guest_name as GuestName

From Guest;

Select Guest_no, Guest_name GuestName

From Guest;12

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

Sorting the Result

Select *

From Room

Order by Price desc

-- ASC is the default

13

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

Sorting the Result

Select *

From Room

Order by Hotel_No, Price desc

-- Sorting first on Hotel_No (asc),

-- then on Price (desc)

-- ASC is the default

14

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

Sorting the Result

Select Hotel_No, Room_No, RType

From Room

Order by Hotel_No, Price desc

-- Price may not be selected

15

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

Sorting the Result

-- Can be used in Order by

Select Guest_no, Guest_name as GuestName

From Guest

Order by GuestName;

Select Guest_no, Guest_name as "Guest Name" From Guest Order by "Guest Name"

16

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

Numeric Operators/Functions

Select Hotel_No, Room_No, Price * 0.90 "Discount Price"

From Room

-- Where Hotel_No = ‘H01';

-- operators: +, -, /, *

-- functions: ceil, floor, mod, power, sign, sqrt

-- With As

17

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

Character Functions

Select Guest_Name || ' From ' || Address

From Guest

Where Address is not null;

18

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

Character Functions

Lower

Upper

Initcap

length

replace

-- Replace all occurrences

substr

Instr

19

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

Character Functions

Select substr(Guest_name, 1, 9)From Guest;-- substr(Guest_name, 1, 9) up to 9 characters

Select * From Guest where instr(Guest_name, 'T', 2) > 0

20

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

Assignment 7

Due Friday, March 28

By 5 PM

21

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

Assignment 8

Due Monday, April 7

Formatting

Style

Sample SQL Script File

22

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

Project

PhaseOne: Due Monday

23