1. More SQL Data Definition
INSERT, UPDATE, DELETE
• INSERT - add a row to a
table
• UPDATE - change row(s) in
a table
• DELETE - remove row(s)
from a table
• UPDATE and DELETE use
‘WHERE clauses’ to specify
which rows to change or
remove
• BE CAREFUL with these - an
incorrect WHERE clause can
destroy lots of data
2. More SQL Data Definition
INSERT
INSERT INTO
<table>
(col1, col2, …)
VALUES
(val1, val2, …)
– The number of columns and
values must be the same
– If you are adding a value to
every column, you don’t have
to list them
– SQL doesn’t require that all
rows are different (unless a
constraint says so)
3. More SQL Data Definition
INSERT
Student
ID
1
Name
John
Year
1
Student
ID
1
2
Name
John
Mary
Year
1
Student
ID
1
2
Name
John
Mary
Year
1
3
Student
ID
1
2
Name
John
Mary
Year
1
3
INSERT INTO Student
(ID, Name, Year)
VALUES (2, ‘Mary’, 3)
INSERT INTO Student
(Name, ID)
VALUES (‘Mary’, 2)
INSERT INTO Student
VALUES (2, ‘Mary’, 3)
4. More SQL Data Definition
UPDATE
UPDATE <table>
SET col1 = val1
[,col2 = val2…]
[WHERE
<condition>]
– All rows where the condition
is true have the columns set
to the given values
– If no condition is given all
rows are changed so BE
CAREFUL
– Values are constants or can
be computed from columns
5. More SQL Data Definition
UPDATE
Student
ID
1
2
3
4
Name
John
Mark
Anne
Mary
Year
1
3
2
2
Student
ID
1
2
3
4
Name
John
Mark
Anne
Mary
Year
2
4
3
3
UPDATE Student
SET Year = Year + 1
UPDATE Student
SET Year = 1,
Name = ‘Jane’
WHERE ID = 4
Student
ID
1
2
3
4
Name
John
Mark
Anne
Jane
Year
1
3
2
1
6. More SQL Data Definition
DELETE
• Removes all rows which
satisfy the condition
DELETE FROM
<table>
[WHERE
<condition>]
– If no condition is given then
ALL rows are deleted - BE
CAREFUL
– Some versions of SQL also
have TRUNCATE TABLE <T>
which is like DELETE FROM
<T>
7. More SQL Data Definition
DELETE
Student
ID
1
2
3
4
Name
John
Mark
Anne
Mary
Year
1
3
2
2
Student
ID Name Year
DELETE FROM Student
or
TRUNCATE TABLE Student
DELETE FROM
Student
WHERE Year = 2
Student
ID
1
2
Name
John
Mark
Year
1
3
8. More SQL Data Definition
Being Careful
• When using DELETE and
UPDATE
– You need to be careful to
have the right WHERE clause
– You can check it by running a
SELECT statement with the
same WHERE clause first
Before running
DELETE FROM Student
WHERE Year = 3
run
SELECT * FROM Student
WHERE Year = 3
9. More SQL Data Definition
SQL SELECT Overview
SELECT
<column-list>
FROM <table-names>
[WHERE <condition>]
[GROUP BY <column-list>]
[HAVING <condition>]
[ORDER BY <column-list>]
• ([]- optional, | - or)
10. More SQL Data Definition
Simple SELECT
SELECT <columns>
FROM <table>
<columns> can be
– A single column
– A comma-separated list of
columns
– * for ‘all columns’
• Given a table Student with
columns
– stuID
– stuName
– stuAddress
– stuYear
13. Arithmetic Expressions
• Create expressions with number and date data by using
arithmetic operators.
13
Operator
+
-
*
/
Description
Add
Subtract
Multiply
Divide
15. Operators precedence
• Multiplication and division take priority over
addition and subtraction.
• Operators of the same priority are evaluated
from left to right.
• Parentheses are used to force prioritized
evaluation and to clarify statements.
15
* / +
_
18. Defining a NULL value
• A null is a value that is unavailable,
unassigned, unknown, or inapplicable.
• A null is not the same as zero or a blank
space.
18
SELECT last_name, job_id, salary, commission_pct
FROM employees;
…
…
19. NULL values in Arithmetic Expressions
• Arithmetic expressions containing a null value
evaluate to null.
19
SELECT last_name, 12*salary*commission_pct
FROM employees;
…
…
20. Defining a column alias
• A column alias:
– Renames a column heading
– Is useful with calculations
– Immediately follows the column name - there can
also be the optional AS keyword between the
column name and alias
– Requires double quotation marks if it contains
spaces or special characters or is case sensitive
20
21. Using column aliases
21
SELECT last_name "Name", salary*12 "Annual Salary"
FROM employees;
SELECT last_name AS name, commission_pct comm
FROM employees;
…
…
22. Concatenation operator
• A concatenation operator:
– Concatenates columns or character strings to
other columns
– Is represented by two vertical bars (||)
– Creates a resultant column that is a character
expression
22
24. Literal character strings
• A literal is a character, a number, or a date
included in the SELECT list.
• Date and character literal values must be
enclosed within single quotation marks.
• Each character string is output once for each
row returned.
24
25. Using literal character strings
25
…
SELECT last_name ||' is a '||job_id
AS "Employee Details"
FROM employees;
26. Duplicate rows
• The default display of queries is all rows,
including duplicate rows.
26
SELECT department_id
FROM employees;
…
27. Eliminating duplicate rows
• Eliminate duplicate rows by using the
DISTINCT keyword in the SELECT clause.
27
SELECT DISTINCT department_id
FROM employees;
28. The CASE Expression
Facilitates conditional inquiries by doing the work of
an IF-THEN-ELSE statement:
CASE expr WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END
29. SELECT last_name, job_id, salary,
CASE job_id WHEN 'IT_PROG' THEN 1.10*salary
WHEN 'ST_CLERK' THEN 1.15*salary
WHEN 'SA_REP' THEN 1.20*salary
ELSE salary END "REVISED_SALARY"
FROM employees;
Using the CASE Expression
Facilitates conditional inquiries by doing the work of
an IF-THEN-ELSE statement:
…
…
Hinweis der Redaktion
The CASE Expression
CASE expressions let you use IF-THEN-ELSE logic in SQL statements without having to invoke procedures.
In a simple CASE expression, Oracle searches for the first WHEN ... THEN pair for which expr is equal to comparison_expr and returns return_expr. If none of the WHEN ... THEN pairs meet this condition, and an ELSE clause exists, then Oracle returns else_expr. Otherwise, Oracle returns null. You cannot specify the literal NULL for all the return_exprs and the else_expr.
All of the expressions ( expr, comparison_expr, and return_expr) must be of the same data type, which can be CHAR, VARCHAR2, NCHAR, or NVARCHAR2.
Instructor Note
There is also a searched CASE expression. Oracle searches from left to right until it finds an occurrence of a condition that is true, and then returns return_expr. If no condition is found to be true, and an ELSE clause exists, Oracle returns else_expr. Otherwise Oracle returns null. For more information, see Oracle9i SQL Reference, “Expressions.”
Using the CASE Expression
In the preceding SQL statement, the value of JOB_ID is decoded. If JOB_ID is IT_PROG, the salary increase is 10%; if JOB_ID is ST_CLERK, the salary increase is 15%; if JOB_ID is SA_REP, the salary increase is 20%. For all other job roles, there is no increase in salary.
The same statement can be written with the DECODE function.