Вы находитесь на странице: 1из 14

DB2 TRANSPARENCIES (Unit 5)

DB2_Tr Ver. 1.0.0 04/12/1998 Page 1 of 14

14 SQL Data Modification Commands


Objectives - Student will
Be able to use INSERT, UPDATE and DELETE in addition to the SELECT statement

DB2_Tr Ver. 1.0.0 04/12/1998

Page 2 of 14

14 SQL Data Modification Commands


INSERT
If column values are not supplied, then the default value is used A value must be supplied for NOT NULL columns

INSERT INTO
tablename (column name,column name,,,,,) VALUES (value1,value2,,,,,,) WHERE predicate or (subselect statement)
DB2_Tr Ver. 1.0.0 04/12/1998 Page 3 of 14

14 SQL Data Modification Commands


UPDATE
Changes existing column value

UPDATE SET

tablename column name = expression or value ,column name = expression or value ..........

WHERE predicate
DB2_Tr Ver. 1.0.0 04/12/1998 Page 4 of 14

14 SQL Data Modification Commands


DELETE

DELETE FROM tablename WHERE predicate

DB2_Tr Ver. 1.0.0 04/12/1998

Page 5 of 14

Sorting and Eliminating Duplicates

Objectives - Student will


Know how to use ORDER BY and DISTINCT Understand GROUP BY and HAVING

DB2_Tr Ver. 1.0.0 04/12/1998

Page 6 of 14

Sorting and Eliminating Duplicates

ORDER BY Clause
Is the only way to guarantee the sequence of the rows returned from a query Must be the last clause of a SELECT ORDER BY expressions are limited to SELECTed columns or positions May be ASCending or DESCending

DB2_Tr Ver. 1.0.0 04/12/1998

Page 7 of 14

Sorting and Eliminating Duplicates

Example 25

select empid , lname , years , salary + commission from staff

where department = 184


order by years , 4;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 8 of 14

Sorting and Eliminating Duplicates

DISTINCT Qualifier
References the entire SELECT list Used to eliminate duplicate rows from the result table

DB2_Tr Ver. 1.0.0 04/12/1998

Page 9 of 14

Sorting and Eliminating Duplicates

Example 26/27/28

select distinct job from staff;

select distinct department from staff where job = sales';

select distinct department, job from staff where years > 10;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 10 of 14

Sorting and Eliminating Duplicates

GROUP BY
Returns one row for each value of the specified column If used, must appear after the WHERE clause and before the ORDER BY clause Must use a column name The GROUP BY column must be SELECTed The SELECTed columns must be named in the GROUP BY clause unless they are used with a column function

DB2_Tr Ver. 1.0.0 04/12/1998

Page 11 of 14

Sorting and Eliminating Duplicates

Example 29

select state, zip from customer group by state, zip;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 12 of 14

Sorting and Eliminating Duplicates

Example 30/31

select distinct state from customer;

select state from customer group by state;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 13 of 14

Sorting and Eliminating Duplicates

HAVING Clause
Used with GROUP BY clause is mentioned here but is not relevant until the unit on column functions

DB2_Tr Ver. 1.0.0 04/12/1998

Page 14 of 14

Вам также может понравиться