Introduction To SQL: Information Management
Introduction To SQL: Information Management
Summer/Fall 2010
Introduction to SQL
Information Management
Agenda
■ SQL
■ Data Definition Language
■ Data Manipulation Language
■ JOINs
■ Data Control Language
■ Transaction Control Statements
SQL in a nutshell
Standard language of relational database access is SQL (Structured
Query Language), designed for accessing tabular data
Data Definition Language (DDL)
Defines properties of data objects
CREATE, ALTER, DROP, TRANSFER OWNERSHIP
Data Manipulation Language (DML)
Used to retrieve, add, edit and delete data
SELECT, INSERT, UPDATE, DELETE
Data Control Language (DCL)
Controls access to databases and data objects
GRANT, REVOKE
Transaction Control Languages (TCL)
Groups DML statements into transactions that can collectively be
applied to a database or undone in the event of a failure
COMMIT, ROLLBACK, SAVEPOINT
3 © 2010 IBM Corporation
Information Management
CREATE Statement
Used to CREATE database objects
■ Table ■ Stored procedures
■ Index ■ Trigger
■ Schema ■ Alias
■ View ■ Method
■ User-defined function ■ Nickname
■ User-defined data type ■ Sequence
■ Buffer pool ■ Table space
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
DROP Statement
Used to REMOVE database objects
■ Can drop any object created with CREATE <database object >
and DECLARE <table> statements.
–Syntax:
SELECT <%EXPRESSIONS%>
FROM <%TABLE REFERENCES%>
Select Statement
EXAMPLES
SELECT *
FROM EMPLOYEE
Retrieves all fields in the employee table
Note: * will return all fields
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
EMPNOFNA LNAM EMPNO FNA LNAM
ME E ME E
161 Bob Joe 155 John Smith
143 Steve Craig 161 Bob Joe
No particular order!
n FNAME, LNAME
SELECT EMPNO,
FROM EMPLOYEE
155EMPNOJohn
WHERE > 150 Smith
AND FNAME = 'Bob'
– Finding the Bobs' that have EMPNO greater than 150
Select Statement
Renaming the
Examples column to
SELECT MAX(SALARY) as MAX_SALARY, TITLE MAX_SALAR
FROM EMPLOYEE
GROUP BY TITLE
Y
Get the Highest salary for each title bracket
EMPNO FNA LNA TITLE SALA Calculated
ME ME RY from the
Aggregate
151 Joe Brian MGR 100 function
000 MAX()
123 Steve Jack ENTR 35 000
Non-
Y
Aggregate
166 Tom MAX_SALARY TITLE
Jones SENI 75 000 columns must
120 000 OR MGR be grouped
190 Eric 45
Jame
000 MGR ENTRY
120 together
s 000
13
75 000 SENIOR © 2010 IBM Corporation
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 SALARY_ TITLE SALARY_A
AVG VG
SENIOR 75 000 MGR 110 000
SELECT TITLE,AVG (SALARY) AS SALARY_AVG
SENIOR 75 000
MGR 110 000
FROM EMPLOYEE
GROUP BY TITLE
HAVING AVG (SALARY) > 50000
ORDER BY TITLE
– The above query, ordered by Title
OR
Insert Statement
Examples
INSERT INTO EMPLOYEE (FNAME , LNAME)
VALUES ('Joe' , 'Bob')
Value was
not provided,
a NULL (if
column is
nullable) will
place hold
16 © 2010 IBM Corporation
Information Management
Insert Statement
INSERT INTO EMPLOYEE (FNAME,LNAME,TITLE)
SELECT FNAME,LNAME,CATEGORY
FROM OUTSOURCED
WHERE CATEGORY = 'TRANSFER'
Update Statement
Example
UPDATE EMPLOYEE
SET BONUS = SALARY/10
Update every entry in the employee table and set the
bonus field to 10% of the salary field
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
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).
Delete Statement
Example
DELETE EMPLOYEE
All entries inside employee are deleted
EMPNO STATU
S EMPNO STAT
1232 RETIR US
ED
DELETE EMPLOYEE
WHERE141
STATUS =ACTIVE
'Retired'
51 RETIR
Remove all the entries
where the status is retired
ED
EMPNO STATU EMPNO STAT
S
US
1232 RETIR
141 ACTIV
23
ED © 2010 IBM Corporation
Information Management
DELETE Rules
■ 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.
Joins
■ Linking of one or more tables
SELECT *
FROM EMPLOYEE E
INNER JOIN TEAM T ON E.TEAMID = T.ID
Cartesian Product
■ Joins every entry to a corresponding entry regardless of
criteria
■ Also called a Cross Join
■ This is particularly useful if you need all possible
permutations
–For Example:
T1 T2 T1.ID C2 T2.I C4
D
ID C2 ID C4 A 11 A 33
A 11 A 33 B 22 Z 44
B 22 Z 44 A 11 Z 44
T1.ID C2 T2.I C4 B 22 A 33
D
T1.ID C2 T2.I C4
WHE A 11 A 33
D
RE B 22 Z 44
A 11 A 33
A 11 Z 44 © 2010 IBM Corporation
28
Information Management
INNER Join
■ INNER join requires a match on both sides
■ Both entries must satisfy the condition that was set in the
ON clause
■ This is useful if you need to create views that have relevance
to each other
–For example:
Require Descriptions from a Look-up table linking with an ID
■ Will return nothing if there is nothing in common
T1 T2
ID C2 ID C4 T1.ID C2 T2.I C4
A 11 A 33 D
B 22 Z 44 A 11 A 33
T1 T2
T1.ID C2 T2.ID C4
ID C2 ID C4
A 11 A 33
A 11 A 33
NULL NULL Z 44
B 22 Z 44
T1.ID C2 T2.ID C4
T1.ID C2 T2.ID C4
A 11 A 33
WHE A 11 A 33
NULL NULL Z 44
RE
T1 T2
T1.ID C2 T2.I C4
ID C2 ID C4
D
A 11 A 33
A 11 A 33
B 22 Z 44 NULL
B 22 NUL
T1.ID C2 T2.I C4 L
T1.ID C2 T2.I C4
WHE D
D
RE A 11 A 33
A 11 A 33
B 22 NUL NULL
L
37 © 2010 IBM Corporation
Information Management
T1 T2
T1.ID C2 T2.ID C4
ID C2 ID C4
A 11 A 33
A 11 A 33
B 22 NULL NULL
B 22 Z 44 NULL NULL Z 44
T1.ID C2 T2.ID C4
T1.ID C2 T2.ID C4
A 11 A 33
WHE A 11 A 33
B 22 NULL NULL
RE
NULL NULL Z 44
40 © 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
ID RANK
Employee Type
ID DESCRIPT
1 Private
EMPNO AI TI
D D ION 2 Sergeant
9021 1 3 1 Army 3 Captain
4533 3 3 2 Marine 4 Major
1345 1 1 3 Air Force 5 General
EMPNO RANK DESCRIPTION
9038 4 1 RESULT:
9038 Major Army
41 © 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
Employee Attribute
ID RANK
EMPNO AI TI
1 Private
D D RESULT: RANK
9021 1 3 2 Sergeant Private
4533 3 3 3 Captain
1345 1 1 4 Major
42
9038 4 1 5 General © 2010 IBM Corporation
Information Management
SELECT *
FROM TABLE T1 INNER JOIN T2
ON T1.ID = T2.ID
– IS EQUIVALENT TO
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
Aliases
■ Aliases : a reference to an object
E-mail: [email protected]
Subject: “DB2 Academic Workshop”
Information Management