Chapter 6: Structured Query Language (SQL) : Customer Custid Custname Occupation
Chapter 6: Structured Query Language (SQL) : Customer Custid Custname Occupation
a. CREATE TABLES
CREATE TABLE CUSTOMER
( CustID char(4) NOT NULL,
CustName vchar(20),
Occupation vchar (20));
CUSTOMER
CustID CustName Occupation
b. ALTERING TABLES
ALTER TABLE CUSTOMER
ADD (RoomID char(3));
CUSTOMER
CustID CustName Occupation RoomID
c. DROP TABLES
DROP TABLE CUSTOMER;
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
SELECT *
FROM CUSTOMER
Result:
CustID CustName Occupation RoomID
C001 Harun Lecturer 210
C002 Karim Executive 311
C003 Maria Senior Manager 520
C004 Jamilah Lecturer 110
C005 Yusuf Manager 401
SELECT Occupation
FROM CUSTOMER;
Result:
Occupation
Lecturer
Executive
Senior Manager
Lecturer
Manager
Manager
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
ARITHMETIC EXPRESSIONS
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
RULES!!!
()
* or / are evaluated before + or -
start from left to right
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
Result: 5
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
COMPARISON OPERATORS
= ---------- equal to
<> / != ---------- not equal to
> ---------- greater than
< ---------- less than
>= ---------- greater than or equal to
<= ---------- less than or equal to
BETWEEN ---------- compares a range of values
IN ---------- tests against values in a test
LIKE ---------- compares a character pattern
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
SELECT RoomID
FROM ROOM
WHERE RoomType=’Deluxe’;
Result:
RoomID
110
401
Result:
RoomID RoomType
401 Deluxe
520 Suite
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
Result:
RoomID RoomType Price
210 Superior 350.00
311 Suite 550.00
Result:
RoomType Price
Deluxe 210.00
Superior 350.00
Deluxe 210.00
Result:
RoomType Price
Suite 550.00
Suite 550.00
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
SELECT *
FROM ROOM
WHERE RoomType LIKE ‘*Deluxe*’;
Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
401 Deluxe 210.00 5
SELECT *
FROM ROOM
WHERE RoomType LIKE ‘S*’;
Result:
RoomID RoomType Price Level
210 Superior 350.00 2
311 Suite 550.00 3
520 Suite 550.00 5
SELECT *
FROM ROOM
WHERE RoomType LIKE ‘*e’;
Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
SELECT *
FROM ROOM
WHERE RoomType LIKE ‘S _ _ _ e’;
Result:
RoomID RoomType Price Level
311 Suite 550.00 3
520 Suite 550.00 5
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
Result:
RoomID RoomType
110 Deluxe
210 Superior
311 Suite
AND OR
OPERATOR OPERATOR
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
ARITHMETIC EXPRESSIONS
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
Result:
RoomID RoomType Level Price DissPrice
520 Suite 5 550.00 522.50
401 Deluxe 5 210.00 199.50
b. JOINING TABLES
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
CUSTOMER
CustID CustName Occupation RoomID
C001 Harun Lecturer 210
C002 Karim Executive 311
C003 Maria Senior Manager 520
C004 Jamilah Lecturer 110
C005 Yusuf Manager 401
Result:
a. UPDATING ROWS
UPDATE ROOM
SET Price=Price+10;
Result:
RoomID RoomType Price Level
110 Deluxe 220.00 1
210 Superior 360.00 2
311 Suite 560.00 3
401 Deluxe 220.00 5
520 Suite 560.00 5
b. INSERTING ROWS
Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
622 Suite 600.00 6
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
c. DELETING ROWS
DELETE
FROM ROOM
WHERE RoomType = ‘Suite’;
Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
401 Deluxe 210.00 5