Professional Documents
Culture Documents
1.6 - Introduction To SQL - Odp
1.6 - Introduction To SQL - Odp
1.6 - Introduction To SQL - Odp
Introduction to SQL
Information Management
Information Management
Agenda
SQL
JOINs
Information Management
SQL in a nutshell
Information Management
Information Management
CREATE Statement
Used to CREATE database objects
Table
Stored procedures
Index
Trigger
Schema
Alias
View
Method
User-defined function
Nickname
Sequence
Buffer pool
Table space
name>
CREATE TABLE <table name>...
CREATE SCHEMA <schema name>...
5
Information Management
ALTER Statement
Used to CHANGE database objects
Table
Sequence
Table space
Type
Database partition
View
Procedure
Method
Function
User mapping
Nickname
Buffer pool
Schema
Index
Information Management
DROP Statement
Used to REMOVE database objects
Can drop any object created with CREATE <database object >
and DECLARE <table> statements.
Information Management
Retrieving Data
Select Statement
Inserting Data
Insert Statement
Updating Data
Update Statement
Deleting Data
Delete Statement
Information Management
Information Management
10
Information Management
Select Statement
EXAMPLES
SELECT *
FROM EMPLOYEE
11
161
Bob
Joe
161
Bob
Joe
143
Steven
Craig
143
Steven
Craig
155
John
Smith
155
John
Smith
IB M D B 2 9
Information Management
W ith W H E R E c la u s e
U s e th e W H E R E c la u s e to s e le c t s p e c ific r o w s fr o m a ta b le o r
v ie w .
R e la tio n a l o p e r a to r s in c lu d in g :
=, >, <, >=, <=, <>
T h e W H E R E c la u s e o f S E L E C T s ta te m e n t c a n in c lu d e :
- L IK E a n d N O T L IK E
_ a n d % w ild c a rd c h a r a c te r w ith L IK E / N O T L IK E
- IS N U L L a n d IS N O T N U L L
-BETW EEN and NOT BETW EEN
- IN a n d N O T IN
- A N D , N O T , a n d O R C o n n e c tiv e s in C o m p le x C o n d itio n s
11
12
Information Management
Select Statement
SELECT EMPNO, FNAME, LNAME
FROM EMPLOYEE WHERE EMPNO > 150
Retrieves the EMPNO,FNAME,LNAME columns from the table
EMPLOYEE where the EMPNO is greater than 150
Bob
Joe
143
Steven
Craig
155
John
Smith
No particular order!
Information Management
Select Statement
Examples
Renaming
SELECT MAX(SALARY) as MAX_SALARY,
TITLE
FROM EMPLOYEE
GROUP BY TITLE
EMPNO
FNAME LNAME
TITLE
SALARY
151
Joe
Brian
MGR
100 000
123
Steve
Jack
Calculated
ENTRY
35 000
166
Tom
Jones
SENIOR
75 000
190
Eric
James
MGR
120 000
202
Kevin
Bob
ENTRY
45 000
MAX_SALARY
120 000
45 000
75 000
14
TITLE
MGR
ENTRY
SENIOR
Information Management
Select Statement
SELECT TITLE,AVG (SALARY) AS SALARY_AVG
FROM EMPLOYEE
GROUP BY TITLE
HAVING AVG (SALARY) > 50000
Get all of the Titles that have an average salary
above 50000
TITLE
SENIOR
MGR
SALARY_AVG
75 000
110 000
TITLE
MGR
SENIOR
SALARY_AVG
110 000
75 000
15
Information Management
16
Information Management
Insert Statement
Examples
INSERT INTO EMPLOYEE
VALUES
(FNAME , LNAME)
('Joe' , 'Bob')
FNAME
LNAME
NULL
Joe
Bob
17
Information Management
Insert Statement
INSERT INTO EMPLOYEE (FNAME,LNAME,TITLE)
SELECT FNAME,LNAME,CATEGORY
FROM OUTSOURCED
WHERE CATEGORY = 'TRANSFER'
Selects the people from table Outsourced with the
category field as Transfer into the Employee Table
Note: If both tables have the exact same fields
then an explicit mapping is not required.
OUTSOURCED
18
EMPLOYEE
FNAME
LNAME
CATEGORY
TITLE
FNAME
LNAME
Billy
Sue
TRANSFER
Susan
John
NEW HIRE
NULL
TRANSFER
Joe
Billy
Bob
Sue
Information Management
19
Information Management
Update Statement
Example
UPDATE EMPLOYEE
SET BONUS = SALARY/10
20
SALARY
BONUS
SALARY
50 000
NULL
50 000
BONUS
5 000
35 000
20 000
35 000
3 500
100 000
NULL
100 000
10 000
150 000
NULL
150 000
15 000
Information Management
Update Statement
UPDATE EMPLOYEE
SET TITLE = 'Executive'
WHERE TITLE = 'Manager' and Tenure = 10
Update title of Employees who has tenure of 10 and is
currently a manager in the Employee Table
21
FNAME
LNAME
TITLE
TENURE
Stan
Jones
Manager
Bob
Billy
Executive
12
Joe
Moe
Senior
14
Susie
Susan
Manager
10
FNAME
LNAME
TITLE
TENURE
Stan
Jones
Manager
Bob
Billy
Executive
12
Joe
Moe
14
Susie
Susan
Senior
Executive
10
2010 IBM Corporation
Information Management
Rules
INSERT Rules
INSERT rule is implicit when a foreign key is specified.
A row can be inserted at any time into a parent table without any action being taken
in dependent table.
A row cannot be inserted into dependent table unless there is a row in parent table
with a parent key value equal to foreign key value of row being inserted, unless
foreign key value is null.
If an INSERT operation fails for one row during an attempt to insert more than one
row, all rows inserted by the statement are removed from the database.
UPDATE Rules
RESTRICT Update for parent key will be rejected if a row in dependent table
matches original values of key.
NO ACTION Update operation for parent key will be rejected if any row in
dependent table does not have a corresponding parent key when update statement
is completed (default).
22
Information Management
23
Information Management
Delete Statement
Example
DELETE EMPLOYEE
EMPNO
STATUS
1232
RETIRED
141
ACTIVE
51
RETIRED
EMPNO
STATUS
DELETE EMPLOYEE
WHERE STATUS = 'Retired'
24
STATUS
1232
RETIRED
141
ACTIVE
51
RETIRED
EMPNO
STATUS
141
ACTIVE
2010 IBM Corporation
Information Management
DELETE Rules
25
RESTRICT
Prevents any row in parent table from being deleted if any dependent
rows are found.
NO ACTION (default)
Enforces the presence of a parent row for every child after all the
referential constraints are applied.
The difference between NO ACTION and RESTRICT is based on when
constraint is enforced.
CASCADE
Implies that deleting a row in parent table automatically deletes any
related rows in dependent table.
SET NULL
Ensures that deletion of a row in parent table sets values of foreign key
in any dependent row to null (if nullable).
Other parts of row are unchanged.
Information Management
Joins
26
Information Management
Cartesian Product
ROLE
EXT TITLE
EMPNO
ROLE
EXT TITLE
27
Information Management
DETAIL (D)
INFO (I)
EMPNO
001
002
003
FNAME
Emily
John
James
EMPNO
001
002
003
001
002
003
28
LNAME
Stevens
Doe
Smith
FNAME
Emily
John
James
Emily
John
James
CROSS
JOIN
LNAME
Stevens
Doe
Smith
Stevens
Doe
Smith
EMPNO
ROLE
EXT TITLE
001
004
MGR
EXEC
445
101
EXT
445
101
445
101
445
101
TITLE
Mrs.
Dr.
Mrs.
Dr.
Mrs.
Dr.
EMPNO
001
004
001
004
001
004
ROLE
MGR
EXEC
MGR
EXEC
MGR
EXEC
Mrs.
Dr.
Information Management
T1
T1.ID C2
T2.ID
C4
11
33
ID
C2
ID
C4
22
44
11
33
11
44
22
44
22
33
WHERE
29
T2
T1.ID C2
T2.ID
C4
11
33
22
44
T1.ID C2
T2.ID
C4
11
44
33
22
33
11
Information Management
INNER Join
Both entries must satisfy the condition that was set in the
ON clause
30
Information Management
INFO (I)
EMPNO
001
002
003
FNAME
Emily
John
James
LNAME
Stevens
Doe
Smith
EMPNO FNAME
001
Emily
LNAME
Stevens
matching
31
INNER
JOIN
Information Management
T1
32
T2
ID
C2
ID
C4
T1.ID C2
T2.ID
C4
11
33
33
22
44
11
Information Management
33
Information Management
INFO (I)
EMPNO
001
002
003
FNAME
Emily
John
James
LNAME
Stevens
Doe
Smith
EMPNO FNAME
001
Emily
NULL
NULL
RIGHT
OUTER
JOIN
matching
34
Information Management
T1
ID
C2
ID
C4
11
33
22
44
WHERE
35
T2
T1.ID C2
T2.ID
C4
33
NULL NULL Z
44
11
T1.ID C2
T2.ID
C4
33
NULL NULL Z
44
11
T1.ID C2
T2.ID
C4
33
11
Information Management
36
Information Management
FNAME
Emily
John
James
EMPNO
001
002
003
37
LNAME
Stevens
Doe
Smith
FNAME
Emily
John
James
LEFT
OUTER
JOIN
matching
TITLE
Mrs.
NULL
NULL
Information Management
T1
ID
C2
ID
C4
11
33
22
44
WHERE
38
T2
T1.ID C2
T2.ID
C4
11
33
22
NULL
NULL
T1.ID C2
T2.ID
C4
11
33
22
NULL
NULL
T1.ID C2
T2.ID
C4
33
11
Information Management
Find the cars that are blue, cars that are white or neither
39
Information Management
FNAME
Emily
John
James
EMPNO
001
002
003
NULL
40
LNAME
Stevens
Doe
Smith
FNAME
Emily
John
James
NULL
FULL
OUTER
JOIN
LNAME
Stevens
Doe
Smith
NULL
matching
EMPNO
001
NULL
NULL
004
ROLE
MGR
NULL
NULL
EXEC
EXT
445
NULL
NULL
101
TITLE
Mrs.
NULL
NULL
Dr.
2010 IBM Corporation
Information Management
T1
T2
T1.ID C2
T2.ID
C4
11
33
33
22
NULL
NULL
44
NULL NULL Z
T1.ID C2
T2.ID
C4
11
33
22
NULL
NULL
ID
C2
ID
C4
11
22
WHERE
NULL NULL Z
41
44
T1.ID C2
T2.ID
C4
33
11
44
2010 IBM Corporation
Information Management
Challenge Question
SELECT E.EMPNO, A.RANK, T.DESCRIPTION
FROM EMPLOYEE E
LEFT OUTER JOIN ATTRIBUTE A ON E.AID = A.ID
FULL OUTER JOIN TYPE T ON E.TID = T.ID
WHERE A.RANK = 'Major'
Attribute
Employee
Type
RANK
Private
Sergeant
EMPNO AID
TID
ID
DESCRIPTION
9021
Army
Captain
4533
Marine
Major
1345
Air Force
General
9038
RESULT:
42
ID
EMPNO
RANK
DESCRIPTION
9038
Major
Army
2010 IBM Corporation
Information Management
Challenge Question
SELECT RANK
FROM EMPLOYEE
INNER JOIN ATTRIBUTE
ON EMPLOYEE.AID = ATTRIBUTE.ID
GROUP BY RANK
HAVING COUNT (EMPNO) > 1
Attribute
Employee
43
EMPNO AID
TID
9021
4533
1345
9038
ID
RANK
Private
Sergeant
Captain
Major
General
RESULT: RANK
Private
Information Management
SELECT *
FROM TABLE T1,TABLE T2
WHERE T1.ID = T2.ID
IS EQUIVALENT TO
SELECT *
FROM TABLE T1 RIGHT OUTER JOIN TABLE T2
ON T1.ID = T2.ID
WHERE T1.ID IS NOT NULL
44
Information Management
Aliases
45
Information Management
Information Management
47
Questions?
Summer/Fall 2010
E-mail: imschool@us.ibm.com
Subject: DB2 Academic Workshop
Information Management