CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO...

17
8 - 1 Copyright © 2012 Robinson College of Business, Georgia State University David S. McDonald Director of Emerging Technologies Tel: 404-413-7368; e-mail: [email protected] CIS 8040 - Structured Query Language (SQL) SQL Outline Data Definition in SQL Indexes in SQL Database Queries in SQL Aggregate Functions of SQL Special Features of SQL Database Update in SQL

Transcript of CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO...

Page 1: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 1

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

CIS 8040 -

Structured Query

Language (SQL)

SQL Outline

Data Definition in SQL

Indexes in SQL

Database Queries in SQL

Aggregate Functions of SQL

Special Features of SQL

Database Update in SQL

Page 2: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 2

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

SQL Statements Relational DBMS must

Allow to define and manipulate relations

Manage DB in terms of relations

Accept normalized relations

SQL ( structured query language )

ANSI and ISO standard for relational DB

Supported by other non-relational DB systems

SQL statements

Data definition:

create table, alter table, drop table, create view, drop view, create index, drop

index

Data manipulation:

select, insert, delete

A Relational DB Example

(print out and bring to class) Supplier SPJ

Part

Project

Page 3: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 3

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

DDL - Create Relations

CREATE TABLE TableName

(colName dataType [NOT NULL] [UNIQUE]

[DEFAULT defaultOption]

[CHECK searchCondition] [,...]} ]

[PRIMARY KEY (listOfColumns),]

[UNIQUE (listOfColumns),] […,]]

[FOREIGN KEY (listOfFKColumns)

REFERENCES ParentTableName [(listOfCKColumns)],

[ON UPDATE referentialAction]

[ON DELETE referentialAction ]] [,…]]

[CHECK (searchCondition)] [,…]] )

BNF Notation (revisited):

•All caps or boldface indicate required reserved words

•Mixed case or italics indicate user-supplied values

•Square brackets indicates optional

•Parentheses are significant. Must be included if shown

•Bar character (“|”) indicates an or condition

Create Relations (alternate BNF)

Create Table TableName

(colName dataType [Not Null] [Unique]

[Default defaultOption]

[Check searchCondition] [,...]} ]

[Primary Key (listOfColumns),]

[Unique (listOfColumns)] [,…,]]

[Foreign Key (listOfFKColumns)

References ParentTableName [(listOfCKColumns)],

[On Update referentialAction]

[On Delete referentialAction ]] [,…]]

[Check (searchCondition)] [,…]] )

BNF Notation:

•All caps or boldface indicate required reserved words

•Mixed case or italics indicate user-supplied values

•Square brackets indicates optional

•Parentheses are significant. Must be included if shown

•Bar character (“|”) indicates an or condition

Page 4: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 4

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Create Relations - Example

Some attribute types in Oracle:

integer, number( p, q ), char( n ),

varchar2(n), nchar(n), nvarchar2(n),

date( dd-monthabbrev-yyyy ), long,

raw(p), long raw, rowid.

DB2 types accepted by Oracle:

smallint, decimal(p,q), float,

varchar(n), long, varchar

Note: the actual entering the DDL

statement is not normally case

sensitive

BUT, the one shown above is

unique to Oracle and can be

case sensitive

The NULL value represents a

unknown value or is not applicable.

Create table DMCDONALD.AUTHORS

(AUID NVARCHAR2(10) NOT NULL,

BOOKNO NVARCHAR2(13) NOT NULL,

AUNAME NVARCHAR2(25),

PRIMARY KEY (AUID),

FOREIGN KEY (BOOKNO)

REFERENCES DMCDONALD."Books"

ON DELETE CASCADE);

Create Table - Notes

Creates a table with one or more columns of the specified dataType.

With NOT NULL, system rejects any attempt to insert a null in the column.

Can specify a DEFAULT value for the column.

Primary keys should always be specified as NOT NULL.

FOREIGN KEY clause specifies FK along with the referential action

Page 5: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 5

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Create Relations (cont’d)

CREATE DOMAIN OwnerNumber AS VARCHAR(5) CHECK (VALUE IN (SELECT ownerNo FROM PrivateOwner));

CREATE DOMAIN StaffNumber AS VARCHAR(5) CHECK (VALUE IN (SELECT staffNo FROM Staff));

CREATE DOMAIN PNumber AS VARCHAR(5);

CREATE DOMAIN PRooms AS SMALLINT; CHECK(VALUE BETWEEN 1 AND 15);

CREATE DOMAIN PRent AS DECIMAL(6,2) CHECK(VALUE BETWEEN 0 AND 9999.99);

Create Relations (cont’d)

CREATE TABLE PropertyForRent (

propertyNo PNumber NOT NULL, ….

rooms PRooms NOT NULL DEFAULT 4,

rent PRent NOT NULL DEFAULT 600,

ownerNo OwnerNumber NOT NULL,

staffNo StaffNumber,

branchNo BranchNumber NOT NULL,

PRIMARY KEY (propertyNo),

FOREIGN KEY (staffNo) REFERENCES Staff

ON DELETE SET NULL….);

Page 6: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 6

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Alter a Relation (a.k.a. Table)

New attribute values in existing tuples are initialized to null

values

Values for the new attribute must set by the user

Only one attribute may be added at a time

ALTER TABLE base-relation-name ADD attr-name data-type;

DROP TABLE base-relation-name;

alter table SUPPLIER add AptNum char( 5 );

drop table SUPPLIER;

Indexes There is no order among tuples

All tuples in a relation may be sorted by the primary key attribute values

Indexes speed up data retrieval

How many students’ tuples would we search for a query with and without indexes?

Find all suppliers who are based in Paris?

Supplier Index Table

AddrIndex Ptr

London

Athens

…..

Page 7: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 7

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Create Indexes

CREATE [UNIQUE] INDEX index name ON base-relation-name

( attr-name [order], attr-name[order] ... ) [CLUSTER];

An index may consist of one or more attributes

Example: Address/Age index

The Unique option forces unique index tuples

The primary key attribute of a relation can be simulated as an indexed attribute with

Unique option created by the DBMS.

The Cluster option groups index in a physical block

An ascending order is by default

Drop Indexes

DROP INDEX index-name;

drop index City_index;

What are the major advantages and disadvantages of using index?

create unique index supplier_id

on SUPPLIER ( S# ASC )

CLUSTER;

create index City_index

on Supplier (City)

create unique index Sname_Index

on SUPPLIER (Sname DESC, Status )

CLUSTER;

Page 8: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 8

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

DML - Data Retrieval

SELECT [ DISTINCT ] attribute1 [,attr2, attr3,...] FROM relation1 [, relation2, relation3,...] [ WHERE condition ] [ GROUP BY attribute1 [,attr2, ...] ] [ Having condition ] [ order by attribute1 [,attr2,...] [ASC|DESC] ] ;

Projection: List all supplier names and cities.

select Sname, City

from SUPPLIER;

Restriction: List all supplier with STATUS > 20

select *

from SUPPLIER

where STATUS > 20;

Data Retrieval...

Combined Project & Restriction: List all part colors and cities which have weight > 12.0 and color = Red.

Select Color, City from PART where weight > 12.0 and color = 'Red' order by weight DESC;

SQL does not remove duplicated tuples

Distinct requests to remove duplicate tuples

Order by sorts the result in ascending or descending order

Page 9: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 9

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Join

Extended Cartesian Product: List all suppliers’ information and all information on active projects (spj) regardless of

whether the supplier is involved in the project.

select SUPPLIER.*, SPJ.*

from SUPPLIER, SPJ;

Equijoin: List all suppliers and all information about projects for which they are involved (spj)

select SUPPLIER.*, SPJ.*

from SUPPLIER S, SPJ C where S.S# = C.S#; (this will fail…can anyone guess why?)

QUERY: For each red part which is supplied from supplier Adams

and which has weight > 12.0, list the part and city where the part

is located.

Join...

Greater-Than Join:

Supplier XCity > City Supplier

SQL: ?

Not-Equal Join:

Supplier XCity <> City Supplier

SQL: ?

Page 10: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 10

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Join…

Classrooms

Left-side table Right-side table

Outer Join:

Returns all rows from one table and only the matching rows (on the join

attribute) from another

What are the IDs, addresses, and classes for all students regardless of

whether they are currently taking a class?

E.g. Select S.ID, S.Address, C.course#

From Student S left outer join Classrooms C on S.ID = C.SID order by S.ID;

EMP# SID Course#

e1 e3 e2 e2 e3 e4 e2 e1 e1

s1 s3 s2 s1 s1 s4 s2 s3 s2

CIS2010 CIS3210 CIS3215 CIS3215 CIS3210 CIS3730 CIS3300 CIS2010 CIS2010

Time Room

12:10 pm 8:00 am 4:30 pm 4:30 pm 8:00 am 7:15 pm 4:30 pm 12:10 pm 12:10 pm

100-CS 521-K 327-ALS 327-ALS 521-K 621-G 327-S 100-CS 100-CS

ID Name

s1 s2 s3 s4 s5

Jose Alice Tom Sue Steve

Student

Age

21 18 32 20 27

Address

Stone Mountain Buck Head Dunwoody Atlanta Stone Mountain

GPA

3.7 3.88 2.89 2.9 3.5

s6

s7

s9

s10

Peter

Richard

Lisa

Lee

22

36

23

32

College Park

Marietta

College Park

Decatur

3.8

4.0

2.5

1.7

Result

Why is s10 out of order?

Page 11: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 11

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Join… • Outer Join (right) Who are the suppliers who are based in the same city as their

warehouse and all the information about the parts they supply, but

also include part availability from warehouses in city’s other than

where the supplier is located.

Select S.sname, S.city, P.*

From supplier S right outer join part P on S.city= P.city;

S# SNAME STATUS CITY

--- --------------- ---------- ------ ---------

S1 Smith 20 London

S2 Jones 10 Paris

S3 Blake 30 Paris

S4 Clark 20 London

S5 Adams 30 Athens

P# PNAME COLOR WEIGHT CITY

--- --------------- ---------- ---------- ------- --------

P1 Nut Red 12 London

P2 Bolt Green 17 Paris

P3 Screw Blue 17 Oslo

P4 Screw Red 14 London

P5 Cam Blue 12 Paris

P6 Cog Red 19 London

Left-side table Right-side table

Result?

Aggregate Functions

COUNT (attr) -- Number of the values in attr

SUM(attr) -- Sum of the values in attr AVG (attr) -- Average of the values in attr

MAX (attr) -- Maximum value in attr

MIN (attr) -- Minimum value in attr

FIRST(attr) -- First value in attr LAST(attr) -- Last value in attr STDDEV(attr) – Standard deviation of values in

attr

Page 12: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 12

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

A Relational DB Example

(print out and bring to class) Supplier SPJ

Part

Project

Grouping Results (used with aggregate functions only)

Double Grouping List the average quantity of parts used for each project grouped by the suppliers.

select j#, s#, AVG(QTY) as “Average Quantity” /*(Note: change quotes to text)*/

from PART, SPJ where Part.p# = SPJ.p# group by j#, s# order by j#;

Aggregate functions are applied to the query results, not to the

result construction

“Having” is to “Group by” as “Where is to “Select”

List the cities and the total number of parts supplied from each city

select city, count( p# ) from PART group by city;

List the cities and the total number of parts for each city that supplies more than 2 parts.

select city, count (p#) from PART group by city Having count (p#) > 2;

Page 13: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 13

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Retrieval with Like Command

'%' any string which n characters, n >= 0 '_' any single character (usually needs

“%” following the “_” character). x exact sequence of string x.

Result:

List all projects in London and names starting with R.

select * from PROJECT where city like ‘London%' and jname like 'R%';

J# Jname City

J5 RAID London

List all Projects with names starting with T.

select * from PROJECT where jname like ‘T%';

J# Jname City

J7 Tape London

Retrieval with Like Command

List all suppliers whose names start with ‘Cla’ and ends with two more characters

select * from SUPPLIER where sname like 'Cla__%';

List all suppliers with city names starting with L

select * from SUPPLIER

where city like 'L%';

S# Sname Status City

S4 Clark 20 London

S# Sname Status City

S1 Smith 20 London

S4 Clark 20 London

Page 14: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 14

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Retrieval with Null Values

Queries Involve Null Values:

List all suppliers who have either unknown status or unknown city.

select *

from SUPPLIER

where Status is null or City is null;

Nested Sub-queries

SELECT attribute1 [, attrib2, attrib3, ...] FROM relation1 [, relation2, relation3, ...] WHERE attr [not] { in | comparison operator |

exists } ( query statement(s) );

List names of suppliers with supplying exactly a quantity of 800 parts

select Sname from SUPPLIER where S# in ( select S# from SPJ where Qty=800);

List names of suppliers who are not supplying 700 or more

parts to active projects select Sname from SUPPLIER where S# not in ( select S# from SPJ where Qty >= 700);

Page 15: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 15

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Nested Sub-queries...

QUERY: List all P#,J# and

quantities ordered by supplier

Smith (assuming there is only

one suppler with name Smith).

QUERY: List the supplier name and id for each

London supplier who’s status is greater than

10 and is currently supplying parts to Athens

projects.

select *

from SPJ

where S# in

( select S#

from SUPPLIER

where Sname = 'Smith');

In-class exercise:

Nested Sub-queries...

The condition involving the key word Exists returns a true value if the

result of the sub-query is not empty. Otherwise, the condition is

false.

select *

from SUPPLIER S

where exists

( select *

from SPJ C

where C.QTY > 400);

QUERY: List all suppliers who ordered quantities greater than

400.

Page 16: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 16

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Update Relations

UPDATE relation-name SET attr= scalar-expression

[ , attr = scalar-expression ] ... [ WHERE condition ] ;

Give 20% increase for weights to all parts which comes from

London

update PART

set part.weight= part.weight * 1.2

where part.CITY = 'London';

How many

tuples are modified by this single statement?

What should one do if only one tuple should be modified?

Delete Tuples

DELETE FROM relation-name

[ WHERE condition ]

Remove all suppliers data who live in

London.

delete from SUPPLIER

where city like '%London%'; How many tuples are removed by this single statement?

What should one do if only one tuple should be removed?

Page 17: CIS 8040 - Structured Query Language (SQL) · SQL ( structured query language ) ANSI and ISO standard for relational DB Supported by other non-relational DB systems SQL statements

8 - 17

Copyright © 2012

Robinson College of Business, Georgia State University

David S. McDonald Director of Emerging Technologies

Tel: 404-413-7368; e-mail: [email protected]

Insert New Tuples

INSERT INTO relation-name [ ( attr [ , attr ] ... ) ] VALUES ( value [, value ] ... ) ; INSERT INTO relation-name [ ( attr [ , attr ] ... ) ] subquery;

What is the value of

Status?

insert into SUPPLIER

values ( ‘s6’, ‘John’, 50, ‘Atlanta’);

insert into SUPPLIER ( S#, Sname, City )

values ( ‘s7’, ‘Mike’ , ‘New York’ );

insert into SUPPLIERINFO ( Supplier#, SInfoName, Project# )

select S.S#, S.SName, C.J#

from SUPPLIER S, SPJ C

where S.S# = C.S# and status > 20 and Qty>500;

create table SUPPLIERINFO

(Supplier# varchar2( 11 ) not null, SInfoName char( 30 ) not null,

Project# char(10) not null, Primary Key (Supplier#,Project#)

For what common

use would you

use this?

Query-By-Example (QBE)

Data displayed in tabular form

Requests made by filling in parts of table

Place P. (w. Microsoft, use a check) in all columns to be

selected or col. 1 for all

Place specified value in column to select matching rows

Multiple values on same line implies "AND" condition

Multiple values on different lines implies "OR" condition

~ in front of a value implies NOT...e.g. ~RED = NOT RED

Use same example (underlined) in same column of two (or

more) tables to show a join (w. Microsoft, draw connecting

relationship-line from one table to another)