sql: data manipulation

62
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee

Upload: ailani

Post on 20-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

SQL: Data Manipulation. Presented by Mary Choi For CS157B Dr. Sin Min Lee. Introduction. Structured Query Language (SQL) Writing an SQL Command Retrieving Data Building SQL Statements Performing Database Updates. What is SQL?. SQL is an example of a transform-oriented language . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SQL: Data Manipulation

SQL: Data Manipulation

Presented by Mary Choi

For CS157B

Dr. Sin Min Lee

Page 2: SQL: Data Manipulation

Introduction

Structured Query Language (SQL) Writing an SQL Command Retrieving Data Building SQL Statements Performing Database Updates

Page 3: SQL: Data Manipulation

What is SQL? SQL is an example of a

transform-oriented language. A language designed to use

relations to transform inputs into required outputs.

Source: Database Systems Connolly/Begg

Page 4: SQL: Data Manipulation

What is SQL? ISO SQL has two major

components: Data Definition Language (DDL) Data Manipulation Language

(DML)

Source: Database Systems Connolly/Begg

Page 5: SQL: Data Manipulation

Data Definition Language (DDL)

Defining the database structure Tables

Controlling access to the data What a user can legally access

Source: Database Systems Connolly/Begg

Page 6: SQL: Data Manipulation

Data Manipulation Language (DML)

Retrieving Data Query tables

Updating Data Populate tables

Source: Database Systems Connolly/Begg

Page 7: SQL: Data Manipulation

Writing SQL Commands

SQL statement consists of reserved words and user-defined words Reserved words are a fixed part

of the SQL language and have a fixed meaning

User-defined words are made up by the user (according to syntax rules)

Source: Database Systems Connolly/Begg

Page 8: SQL: Data Manipulation

Reserved Words

Are fixed part of the SQL language Have a fixed meaning Require exact spelling Kept on the same line

Source: Database Systems Connolly/Begg

Page 9: SQL: Data Manipulation

User-defined Words

Are made up by the user Governed by a set of syntax rules Represent names of database

objects such as: Tables Columns Views Indexes

Source: Database Systems Connolly/Begg

Page 10: SQL: Data Manipulation

Data Manipulation

Select: query data in the database Insert: insert data into a table Update: updates data in a table Delete: delete data from a table

Source: Database Systems Connolly/Begg

Page 11: SQL: Data Manipulation

Literals

Non-numeric data values must be enclosed in single quotes: ’16 Holland Drive’ ‘CS157B’

Numeric data values must NOT be enclosed in single quotes: 6 600.00

Source: Database Systems Connolly/Begg

Page 12: SQL: Data Manipulation

Writing SQL Commands

Most components of an SQL statement are case insensitive, but one exception is that literal character data must be typed exactly as it appears in the database.

Source: Database Systems Connolly/Begg

Page 13: SQL: Data Manipulation

Simple Query

Select specifies which columns are to appear in the output.

From specifies the table(s) to be used.

Where filters the rows subject to some condition(s).

Source: Database Systems Connolly/Begg

Page 14: SQL: Data Manipulation

Simple Query

Group By forms groups of rows with the same column value.

Having filters the groups subject to some condition.

Order By specifies the order of the output.

Source: Database Systems Connolly/Begg

Page 15: SQL: Data Manipulation

Retrieve all columns and all rows

SELECT firstColumn,…,lastColumn

FROM tableName;

SELECT *

FROM tableName;

Page 16: SQL: Data Manipulation

Use of Distinct

SELECT DISTINCT columnName

FROM tableName;

columnName

A

B

C

D

columnName

A

A

B

B

C

D

Page 17: SQL: Data Manipulation

Calculated fields

SELECT columnName/2

FROM tableName

price

5.00

3.00

6.00

price

10.00

6.00

12.00

Page 18: SQL: Data Manipulation

Comparison Search Condition

= equals

< > is not equal to (ISO standard)

!= “ “ “ “ (allowed in some dialects)

< is less than

> is greater than

<= is less than or equal to

>= is greater than or equal to

Source: Database Systems Connolly/Begg

Page 19: SQL: Data Manipulation

Comparison Search Condition

An expression is evaluated left to right.

Subexpressions in brackets are evaluated first.

NOTs are evaluated before ANDs and ORs.

ANDs are evaluated before ORs.

Source: Database Systems Connolly/Begg

Page 20: SQL: Data Manipulation

Range Search ConditionSELECT columnNameFROM tableNameWHERE columnName BETWEEN 20

AND 30;

SELECT columnNameFROM tableNameWHERE columnName >= 20 AND columnName <= 30;

Page 21: SQL: Data Manipulation

Set membership search condition

SELECT columnName

FROM tableName

WHERE columnName

IN (‘name1’, ‘name2’);

SELECT columnName

FROM tableName

WHERE columnName = ‘name1’

OR columnName = ‘name2’;

Page 22: SQL: Data Manipulation

Pattern matching symbols

% represents any sequence of zero

or more characters (wildcard).

_ represents any single character

Source: Database Systems Connolly/Begg

Page 23: SQL: Data Manipulation

Pattern match search condition‘h%’ : begins with the character h .

‘h_ _ _’ : four character string beginning with the character h.

‘%e’ : any sequence of characters, of length at least 1, ending with the character e.

‘%CS157B%’ : any sequence of characters of any length containing CS157B

Source: Database Systems Connolly/Begg

Page 24: SQL: Data Manipulation

Pattern match search condition

LIKE ‘h%’

begins with the character h .

NOT LIKE ‘h%’

does not begin with the character h.

Source: Database Systems Connolly/Begg

Page 25: SQL: Data Manipulation

Pattern match search condition

To search a string that includes a

pattern-matching character

‘15%’

Use an escape character to represent

the pattern-matching character.

LIKE ‘15#%’ ESCAPE ‘#’

Source: Database Systems Connolly/Begg

Page 26: SQL: Data Manipulation

NULL search condition

DOES NOT WORK

comment = ‘ ’

comment != ‘ ’

DOES WORK

comment IS NULL

comment IS NOT NULL

Page 27: SQL: Data Manipulation

Sorting

The ORDER BY clause consists of list of column

identifiers that the result is to be sorted on, separated by commas.

Allows the retrieved rows to be ordered by ascending (ASC) or descending (DESC) order

Source: Database Systems Connolly/Begg

Page 28: SQL: Data Manipulation

Sorting

Column identifier may be A column name A column number (deprecated)

Source: Database Systems Connolly/Begg

Page 29: SQL: Data Manipulation

Sorting

SELECT type, rent

FROM tableName

ORDER BY type, rent ASC;

Source: Database Systems Connolly/Begg

type rent

Apt

Apt

Flat

Flat

450

500

600

650

type rent

Flat

Apt

Flat Apt

650 450

600 500

Page 30: SQL: Data Manipulation

Aggregate Functions

COUNT returns the number … SUM returns the sum … AVG returns the average … MIN returns the smallest … MAX returns the largest …

value in a specified column.

Source: Database Systems Connolly/Begg

Page 31: SQL: Data Manipulation

Use of COUNT( * )

How many students in CS157B?

SELECT COUNT( * ) AS my count

FROM CS157B

my count

40

Page 32: SQL: Data Manipulation

GROUP BY clause

When GROUP BY is used, each item in the SELECT list must be single-valued per group.

The SELECT clause may contain only Column names Aggregate functions Constants An expression involving combinations of the

above

Source: Database Systems Connolly/Begg

Page 33: SQL: Data Manipulation

GroupingSELECT dept, COUNT(staffNo) AS my count

SUM(salary)

FROM tableName

GROUP BY dept

ORDER BY dept

dept my count

Salary

A

B

C

2

2

1

300.00300.00 200.00

dept staffNo Salary

A

B

C

A

B

1

1

1

2

2

200.00

200.00

200.00

100.00

100.00

Page 34: SQL: Data Manipulation

Restricting Grouping

HAVING clause is with the GROUP BY clause. filters groups into resulting table. includes at least one aggregate

function. WHERE clause

filters individual rows into resulting table.

Aggregate functions cannot be used.

Source: Database Systems Connolly/Begg

Page 35: SQL: Data Manipulation

SELECT dept, COUNT(staffNo) AS my count, SUM(salary) AS my sum

FROM StaffGROUP BY deptHAVING COUNT(staffNo) > 1ORDER BY dept;

Source: Database Systems Connolly/Begg

dept my count

my sum

A

B

2

2

300.00 300.00

dept staffNo Salary

A

B

C

A

B

1

1

1

2

2

200.00

200.00

200.00

100.00

100.00

Page 36: SQL: Data Manipulation

Subqueries

SELECT columnNameA

FROM tableName1

WHERE columnNameB = (SELECT columnNameB

FROM tableName2

WHERE condition);

Source: Database Systems Connolly/Begg

result from inner SELECT applied as a condition for the outer SELECT

Page 37: SQL: Data Manipulation

Subquery with Aggregate Function

SELECT fName, salary –

( SELECT AVG(salary)

FROM Staff ) AS salDiff

FROM Staff

WHERE salary > ( SELECT AVG(salary)

FROM Staff );

Source: Database Systems Connolly/Begg

List all staff whose salary is greater than the average salary,show by how much their salary is greater than the average.

Page 38: SQL: Data Manipulation

Nested Subqueries: Use of IN

SELECT propertyFROM PropertyForRentWHERE staff IN(

SELECT staffFROM StaffWHERE branch = (

SELECT branch FROM Branch WHERE street = ‘112 A St’));

Source: Database Systems Connolly/Begg

Selects branch at 112 A St

Page 39: SQL: Data Manipulation

Nested Subqueries: Use of IN

SELECT property

FROM PropertyForRent

WHERE staff IN(

SELECT staff

FROM Staff

WHERE branch = ( branch ) );

Source: Database Systems Connolly/Begg

Select staff members who works at branch.

Page 40: SQL: Data Manipulation

Nested Subqueries: Use of IN

SELECT property

FROM PropertyForRent

WHERE staff IN( staffs who works

at branch on ‘112 A St’);

Source: Database Systems Connolly/Begg

Since there are more than one row selected, “=“ cannot be used.

Page 41: SQL: Data Manipulation

Use of ANY/SOME

SELECT name, salary

FROM Staff

WHERE salary > SOME( SELECT salary

FROM Staff

WHERE branch = ‘A’ );

Source: Database Systems Connolly/Begg

Result:{2000,3000,4000}

Result: {list of staff with salary greater than 2000.}

Page 42: SQL: Data Manipulation

Use of ALL

SELECT name, salary

FROM Staff

WHERE salary > ALL( SELECT salary

FROM Staff

WHERE branch = ‘A’ );

Source: Database Systems Connolly/Begg

Result:{2000,3000,4000}

Result: {list of staff with salary greater than 4000.}

Page 43: SQL: Data Manipulation

Use of Any/Some and All

If the subquery is empty: ALL returns true ANY returns false

ISO standard allows SOME to be

used interchangeably with ANY.

Source: Database Systems Connolly/Begg

Page 44: SQL: Data Manipulation

Multi-Table Queries

Join Inner Join Left Outer Join Right Outer Join Full Outer Join

Source: Database Systems Connolly/Begg

Page 45: SQL: Data Manipulation

JoinSELECT client

FROM Client c, View v

WHERE c.client = v.client;

Source: Database Systems Connolly/Begg

FROM Client c JOIN View v ON c.client = v.client(creates two identical client columns)

FROM Client JOIN View USING clientFROM Client NATURAL JOIN View

ISO standard Alternatives

Page 46: SQL: Data Manipulation

Join

The join operation combines data from two tables by forming pairs of related rows where the matching columns in each table have the same value.

If one row of a table is unmatched, the row is omitted from the resulting table.

Source: Database Systems Connolly/Begg

Page 47: SQL: Data Manipulation

Inner Join

Source: Database Systems Connolly/Begg

SELECT b.*,p.*

FROM Branch b, Property p

WHERE b.bCity = p.pCity;

branch bcity

B003

B004

B002

Glasgow

Bristol

London

property pcity

P103

P204

P102

Aberdeen

London Glasgow

Page 48: SQL: Data Manipulation

Inner Join Result

Source: Database Systems Connolly/Begg

SELECT b.*,p.*

FROM Branch b, Property p

WHERE b.bCity = p.pCity;

branch bcity

B003

B002

Glasgow

London

property pcity

P102

P204

Glasgow

London

Page 49: SQL: Data Manipulation

Left Outer Join

SELECT b.*,p.*

FROM Branch b

LEFT JOIN Property p

ON b.bCity = p.pCity;

Source: Database Systems Connolly/Begg

branch bcity

B003

B004

B002

Glasgow

Bristol

London

property pcity

P102

NULL

P204

Glasgow NULL

London

Page 50: SQL: Data Manipulation

Right Outer Join

SELECT b.*,p.*

FROM Branch b

RIGHT JOIN Property p

ON b.bCity = p.pCity;

Source: Database Systems Connolly/Begg

property pcity

P103

P204

P102

Aberdeen

London Glasgow

branch bcity

NULL

B002

B003

NULL

London Glasgow

Page 51: SQL: Data Manipulation

Full Outer JoinSELECT b.*,p.*

FROM Branch b

FULL JOIN Property p

ON b.bCity = p.pCity;

Source: Database Systems Connolly/Begg

branch bcity

B004

NULL

B002

B003

Bristol

NULL

London Glasgow

property pcity

NULL

P103

P204

P102

NULL

Aberdeen

London Glasgow

Page 52: SQL: Data Manipulation

Exists and Not Exists

For use only with subqueries. Produces true/false results.

Source: Database Systems Connolly/Begg

Page 53: SQL: Data Manipulation

EXISTS returns true IFF there exists at

least one row in the resulting table returned by the subquery

Source: Database Systems Connolly/Begg

NOT EXISTS returns false if the subquery is

empty.

Page 54: SQL: Data Manipulation

Exists and Not Exists

SELECT staff

FROM Staff s

WHERE EXISTS( SELECT *

FROM Branch b

WHERE s.branch = b.branch

AND city = ‘London’);

Source: Database Systems Connolly/Begg

Find all staff who work in a London branch office.

Page 55: SQL: Data Manipulation

Combining Result Tables

UNION INTERSECT EXCEPT

Source: Database Systems Connolly/Begg

R S R S R – S

R

S S S

RR

Page 56: SQL: Data Manipulation

Use of UNION

( SELECT city

FROM Branch

WHERE city IS NOT NULL)

UNION

( SELECT city

FROM Property

WHERE city IS NOT NULL);

Source: Database Systems Connolly/Begg

city

London

Glasgow

Aberdeen

Bristol

List of all cities where there is either a branch or property.

Page 57: SQL: Data Manipulation

Use of INTERSECT

( SELECT city FROM Branch )

INTERSECT

( SELECT city FROM Property );

Source: Database Systems Connolly/Begg

city

London

Glasgow

Aberdeen

List of all cities where there is both a branch and a property.

Page 58: SQL: Data Manipulation

Use of EXCEPT

( SELECT city FROM Branch )

EXCEPT

( SELECT city FROM Property );

Source: Database Systems Connolly/Begg

city

Bristol

List of all cities where there is a branch office but no properties.

Page 59: SQL: Data Manipulation

Database Updates

INSERT Adds new rows of data to a table

UPDATE Modifies existing data in a table

DELETE Removes rows of data from a table

Source: Database Systems Connolly/Begg

Page 60: SQL: Data Manipulation

Use of INSERT

INSERT INTO TableName[(columnList)]

VALUES (dataValueList)

Source: Database Systems Connolly/Begg

Page 61: SQL: Data Manipulation

Use of UPDATE

UPDATE TableName

SET columnnName1 = dataValueList

[, columnName2 = dataValue2…]

[WHERE searchCondition]

Source: Database Systems Connolly/Begg

Page 62: SQL: Data Manipulation

Use of DELETE

DELETE FROM TableName

[WHERE searchCondition]

Source: Database Systems Connolly/Begg