Lecture 4 - Structured Query Language

advertisement

E-R Model for Flower Store

Products m

1

Used for n

Occasion m

Orderline m 1

Orders m m

1

Recipient

Customer

1

SQL - 1

Data Definition and

Manipulation with SQL

SQL - 2

Structured Query Language

Developed by IBM

A standard for relational database access

IBM DB2, Microsoft SQL server, Oracle,

MySQL (open source)

SQL - 3

Data Definition Language

A data definition language (DDL) is used to define a database in DBMS.

A schema is a specification (or definition) of a database using the DDL.

Create table

Create view

Create index

Integrity control

SQL - 4

Table Definition with SQL

CREATE TABLE ORDERS

(ORDER# INTEGER NOT NULL,

CUSTOMER_ID CHAR(4),

ORDER_DATE

AMOUNT

DATE,

DECIMAL (8,2))

IN DATABASE INVENTORY

ALTER TABLE ORDERS

ADD DISCOUNT SMALLINT

DROP TABLE ORDERS

CREATE SYNONYM CUSTORDER FOR

ORDERS

SQL - 5

View Definition with SQL

CREATE VIEW GOODSTUD

(NAME, COURSE)

AS SELECT SNAME, CNAME

FROM STUD, ENROLL

WHERE STUD.SID = ENROLL.ID

AND ENROLL.GRADE = ‘A’

View can be used for data retrieval as virtual table

Not all views are updateable (insert, delete, update)

SQL - 6

Index Definition with SQL

Index is used to provide rapid random and sequential access

Consume extra storage space and maintenance time

Data access does not depend on the existence of index

SQL - 7

Create Index

CREATE INDEX DES ON

PRODUCTS (DESCRIPTION)

IN DATABASE INVENTORY

CREATE UNIQUE INDEX XP

ON PRODUCTS(PRODUCT#)

CREATE UNIQUE INDEX SXP

ON SUPPLY (SUPPLYER#, PRODUCT#)

DROP INDEX XP

SQL - 8

Data Integrity Control

Integrity refers to the accuracy or validity of data.

(validity check)

Key restrictions

 not null must be unique

Domain restrictions

 format

 range

Cross reference restrictions (referential integrity)

 valid foreign keys

SQL - 9

Integrity Control with SQL

Entity Integrity

No part of the primary key can be null.

Primary key must be unique

Specify PRIMARY KEY or define a unique index over that key

SQL - 10

Integrity Control with SQL

Referential Integrity

Insertion Integrity -- No row can be added into a dependent table unless the foreign key matches a primary key in parent table or the foreign key value is null (optional relationship)

Update Integrity -- Prevents the altering of any primary key value if there are dependent rows with matching values

(different from ON UPDATE CASCADE in the textbook)

Deletion Integrity -- Restrict, Cascade, and Set null

SQL - 11

Integrity Control with SQL

A general form:

CREATE TABLE table-name

(column-name type [NOT NULL], column-name type [NOT NULL],

...

[ PRIMARY KEY ( column-name) ]

[ FOREIGN KEY (column-name) REFERENCES table-name

ON DELETE RESTRICT | CASCADE | SET NULL

ON UPDATE CASCADE ] )

IN DATABASE database-name

EDITPROC edit-routine

VALIDPROC validation-routine

SQL - 12

Integrity Control with SQL

ON DELETE clause

RESTRICT -- makes users unable to delete a parent row if there are matching dependent rows

CASCADE -- when a parent row is deleted, all dependent rows will be deleted

SET NULL -- when a parent row is deleted, set the foreign key value to null in all the dependent rows.

SQL - 13

Integrity Control with SQL

EDITPROC clause

A routine that cannot contain any DB2 statements will be automatically invoked after retrieval or before storing a row. Used for data compression, decompression and encryption

VALIDPROC clause

An executable routine that will be invoked just before a row is stored. Used for data validation.

SQL - 14

Integrity Control with SQL

An example

CREATE TABLE ENROLL

(CID CHAR(4) NOT NULL,

SID CHAR(4) NOT NULL,

GRADE CHAR(2),

PRIMARY KEY ( CID,SID),

FOREIGN KEY (CID) REFERENCES COURSE

ON DELETE RESTRICT,

FOREIGN KEY (SID) REFERENCES STUDENT

ON DELETE CASCADE)

IN DATABASE your-database-name

SQL - 15

Data Dictionary / Directory

The data dictionary contains definitions of records, data items, relations and other data objects that are of interest to users or required by data management software.

The data directory contains information about where data are stored that is used exclusively by data management software.

System catalog

Systables

Syscolumns

Sysindex

SQL - 16

Database Design using

Microsoft Access

SQL - 17

SQL - 18

Basic Components of Access

Table

Query

Forms

Report

Macro

Module

SQL - 19

Database Creation using

MicroSoft Access

Create table

Specify field data type

Specify key

Specify field properties (domain constraints)

Create relationships

Specify relationship between tables

Specify relationship integrity control

SQL - 20

Create Tables

Specify fields

Field name, data type, description, and properties (input mask, default, lookup, etc.)

Specify Primary Key

Single and concatenated

Specify Table Name

SQL - 21

Set a field’s data type

Open the table in design view

Set a field’s data type

Choose a primary key

Set field properties

SQL - 22

Domain constraints

Size

Format

Default value

Validation rules

Index

Required

Lookup

SQL - 23

Specify relationship and integrity control

Specify links between tables

Integrity control: enforce referential integrity cascade update related fields cascade delete related records

SQL - 24

Relationships and Integrity Control

SQL - 25

Practice: Create Four tables

Using Access

SQL - 26

SQL - 27

SQL - 28

You need to enter data separately to Orders and Orderln tables SQL - 29

Data Manipulation Language

A Data Manipulation Language (DML) is used to describe the processing of the database.

select Name, Salary from Staff where Salary > 15000 insert into Staff

(ID, Name, Dept, Salary) values (510, ‘Harrison’, 38, 15400) update Staff set Salary = Salary * 1.15

where ID = 510 delete from Staff where ID = 540

SQL - 30

Relational Data Manipulation

Relational algebra

Manipulates one or two relations as operands and produces a new relation as the result.

Relational calculus

Manipulates relations implicitly by specifying conditions that can involve attributes from several relations.

Structured Query Language (SQL)

Tabular / Graphic data manipulation

Query - By- Example (QBE)

Most PS DBMS use variations of QBE

SQL - 31

Sample table

Q.STAFF

ID NAME DEPT JOB YEARS

10 SANDERS D20 MGR

20 PERNAL D20 SALES

30 MARENGHI D38 MGR

40 O’BRIEN D20 SALES

50 HANES

80 JAMES

7

8

5

6

D15 MGR 10

D20 CLERK -

DEPTNUMB DEPTNAME

Q.ORG

MANAGER DIVISION

SALARY COMM

18357.50 -

18171.25 612.45

17506.75 -

18006.00 846.55

20659.80 -

13540.60 -

LOCATION

D10

D15

D20

D38

HEAD OFFICE

NEW ENGLAND

160

50

MID ATLANTIC 10

SOUTH ATLANTIC 30

CORPORATE NEW YORK

EASTERN BOSTON

EASTERN

EASTERN

WASHINGTON

ATLANTA

SQL - 32

Tabular / Graphic data manipulation

Query - By- Example (QBE)

List department managers with salary > 20000

Q.STAFF | ID | DEPT | NAME | JOB | YEARS | SALARY

----------+--------+--------+--------+------+---------+-------------

| _MG | | P. | | | >20000

Q.ORG | DEPTNUMB | MANAGE | DIVISION | LOATION

----------+---------------+-----------+-------------+--------------

| P. AO. | _MG | |

Now widely used by PC RDBMS with many variations

SQL - 33

QBE Query in Access

SQL - 34

Relational Algebra select project join

SQL - 35

Relational Algebra

SELECT -- Extract specified tuples from a relation.

PROJECT -- Extract specified attributes from a relation.

JOIN -- Combines data from two relations based on values for a common attribute

SQL - 36

Types of Join (pp.225-227)

Student JOIN Enrollment on

Student.StudentID = Enrollment.StudentNumber

Equi join

Natural Join (not duplicate join fields)

Inner Join (not allow null in join fields)

Outer Join (allow null in join fields)

Left outer join (include all records in left join table

Student)

Right outer join (include all records in right join table

Enrollment

SQL - 37

Relational Calculus -- SQL

General form for data retrieval in SQL:

SELECT [ DISTINCT ] field(s)

FROM table(s)

[ WHERE predicate]

[ GROUP BY field(s) [ HAVING predicate ] ]

[ ORDER BY field(s)]

SQL - 38

Predicate

Operations:

Arithmetic operators:

+ - * /

Comparison operators:

= > >= <=

Boolean operators:

AND OR NOT IN BETWEEN LIKE

SQL - 39

Sample table

STAFF

ID NAME DEPT JOB YEARS

10 SANDERS D20 MGR

20 PERNAL D20 SALES

30 MARENGHI D38 MGR

40 O’BRIEN D20 SALES

50 HANES

80 JAMES

7

8

5

6

D15 MGR 10

D20 CLERK -

DEPTNUMB DEPTNAME

ORG

MANAGER DIVISION

SALARY COMM

18357.50 -

18171.25 612.45

17506.75 -

18006.00 846.55

20659.80 -

13540.60 -

LOCATION

D10

D15

D20

D38

HEAD OFFICE

NEW ENGLAND

160

50

MID ATLANTIC 10

SOUTH ATLANTIC 30

CORPORATE NEW YORK

EASTERN BOSTON

EASTERN

EASTERN

WASHINGTON

ATLANTA

SQL - 40

Simple SQL Queries

SELECT *

FROM STAFF

SELECT DISTINCT DEPT, JOB

FROM STAFF

ID NAME

Q.STAFF

DEPT JOB YEARS

10 SANDERS D20 MGR

20 PERNAL D20 SALES

7

8

30 MARENGHI D38 MGR

40 O’BRIEN D20 SALES

5

6

50 HANES

80 JAMES

D15 MGR 10

D20 CLERK -

SALARY COMM

18357.50 -

18171.25 612.45

17506.75 -

18006.00 846.55

20659.80 -

13540.60 -

SQL - 41

Simple SQL Queries

SELECT DEPT, NAME

FROM STAFF

ORDER BY DEPT DESC, NAME

SELECT ID, SALARY+COMM

FROM STAFF

WHERE DEPT = ‘D20’

ID NAME

10 SANDERS

20 PERNAL

DEPT JOB

D20

D20

MGR

SALES

30 MARENGHI D38

40 O’BRIEN D20

50 HANES D15

80 JAMES D20

MGR

SALES

MGR

CLERK

YEARS

7

8

5

6

10

-

SALARY COMM

18357.50 -

18171.25 612.45

17506.75 -

18006.00 846.55

20659.80 -

13540.60 -

SQL - 42

Conditions

WHERE SALARY / 12 < COMM * 4

WHERE COMM BETWEEN 200 AND 300

WHERE DEPT IN (‘D20’,’D38’,’D40’)

WHERE JOB =‘SALES’ AND (SALARY>18000

OR COMM >500)

WHERE NAME LIKE ‘W%’

WHERE ADDRESS LIKE ’%NY’

WHERE NOT JOB =‘MGR’

WHERE YEARS NOT NULL

SQL - 43

Built-in Functions

Functions: COUNT SUM AVG MIN MAX

Find out clerk’s average and minimum salary for each department

SELECT DEPT,

AVG (SALARY), MIN (SALARY), COUNT (*)

FROM STAFF

WHERE JOB =‘CLERK’

GROUP BY DEPT

SQL - 44

More Queries

How many different jobs?

SELECT COUNT (DISTINCT JOB)

FROM STAFF

Who ‘s salary is above average?

SELECT NAME, SALARY

FROM STAFF

WHERE SALARY > ( SELECT

AVG(SALARY) FROM STAFF )

SQL - 45

Group By and Having

HAVING is always followed by a built-in function

HAVING must follow GROUP BY

HAVING conditions only apply to grouped data

SQL - 46

Group By and Having

Find out average salary for each department with more than 4 employees

SELECT DEPT, AVG(SALARY)

FROM STAFF

GROUP BY DEPT

HAVING COUNT(*) > 4

Find out department with average salary higher than company’s average

SELECT DEPT, AVG(SALARY)

FROM STAFF

GROUP BY DEPT

HAVING AVG( SALARY) > (SELECT AVG(SALARY)

FROM STAFF)

SQL - 47

Join

List manager’s name for each division and each department

SELECT DIVISION, DEPTNAME, NAME

FROM ORG, STAFF

WHERE STAFF.DEPT = ORG.DEPTNUMB

AND JOB = ‘MGR’

ORDER BY DIVISION, DEPTNAME

Must specify the join condition based on the equal value of two common attributes from two tables. You can join more than two tables at the same time

SQL - 48

Join specified in MicroSoft SQL and Access SQL

List manager’s name for each division and each department

SELECT DIVISION, DEPTNAME, NAME

FROM ORG INNER JOIN STAFF

ON STAFF.DEPT = ORG.DEPTNUMB

WHERE JOB = ‘MGR’

ORDER BY DIVISION, DEPTNAME

In Microsoft SQL, INNER JOIN is specified in

FROM clause

SQL - 49

Correlation Variables

List employees whose salary is greater than their manager’s salary

SELECT X.NAME, Y.SALARY

FROM STAFF X, STAFF Y,

WHERE X.DEPT =Y.DEPT

AND Y.JOB = ‘MGR’

AND X.SALARY >Y.SALARY

X, Y are correlation variables refer to STAFF twice

SQL - 50

Find out Unknown Condition

Values: ANY, ALL, EXIST

List employees who work in

EASTERN division

SELECT NAME, ID

FROM STAFF

WHERE DEPT = ANY

(SELECT DEPTNUMB FROM ORG WHERE

DIVISION = ‘EASTERN’)

SQL - 51

ALL

Find the department with highest average salary

SELECT DEPT, AVG(SALARY) FROM STAFF

GROUP BY DEPT

HAVING AVG(SALARY) >= ALL

(SELECT AVG(SALARY) FROM STAFF GROUP

BY DEPT)

SQL - 52

Not Exist

List all managers who are not the department managers

SELECT ID, NAME, DEPT FROM STAFF XYZ

WHERE JOB =‘MGR’ AND NOT EXIST

(SELECT * FROM ORG WHERE MANAGER =

XYZ.ID)

SQL - 53

Union - Merging two or more resulting tables

Union is different from join list all employees with less than 3 year employment and applicants with more than

14 year education

SELECT NAME ‘EMPLOYEE’ FROM STAFF WHERE YEARS < 3

UNION

SELECT NAME, ‘APPLICANT’ FROM APPLICANT

WHERE EDLEVEL >14

NAME EXPRESSION1

BURKE EMPLOYEE

GASPARD APPLICANT

JACOBS APPLICANT

SQL - 54

Retrieve and Modify Data through a View

Create a view SALES as a list of all sales persons

CREATE VIEW SALES ( DIVISION, DEPARTMENT, NAME,

INCOME) AS

SELECT DIVISION, DEPTNAME, NAME, SALARY+COMM

FROM STAFF, ORG

WHERE DEPT = DEPTNUMB AND JOB = ‘SALES’

A view can be used in the same way as table for data retrieval

SQL - 55

Data Modification Restriction

Restrictions for data modification through a view

Only views that are simple row-and-column subset of a base table are updateable

A column in a view derived from mathematical expression on base data may not be updated

A new row may not be inserted into a view when the base table affected would have a missing value for a NOT NULL column.

SQL - 56

Practice

List all sales people’s name in the Eastern division sorted by name

List number of employees and average salary in each division

Tables:

STAFF

(ID, NAME,DEPT,JOB,YEARS,SALARY,COMM)

ORG

(DEPTNUMB,DEPTNAME,MANAGER,DIVISION

,LOCATION)

SQL - 57

Sample table

STAFF

ID NAME DEPT JOB YEARS

10 SANDERS D20 MGR

20 PERNAL D20 SALES

30 MARENGHI D38 MGR

40 O’BRIEN D20 SALES

50 HANES

80 JAMES

7

8

5

6

D15 MGR 10

D20 CLERK -

DEPTNUMB DEPTNAME

ORG

MANAGER DIVISION

SALARY COMM

18357.50 -

18171.25 612.45

17506.75 -

18006.00 846.55

20659.80 -

13540.60 -

LOCATION

D10

D15

D20

D38

HEAD OFFICE

NEW ENGLAND

160

50

MID ATLANTIC 10

SOUTH ATLANTIC 30

CORPORATE NEW YORK

EASTERN BOSTON

EASTERN

EASTERN

WASHINGTON

ATLANTA

SQL - 58

Practice Answer 1

List all sales people’s name in the

Eastern division sorted by name

SELECT NAME

FROM STAFF, ORG

WHERE DEPT = DEPTNUMB

AND JOB = ‘SALES’

AND DIVISION = ‘EASTERN’

ORDER BY NAME

SQL - 59

Practice Answer 2

List number of employees and average salary in each division

SELECT DIVISION, COUNT (*), AVG

(SALARY)

FROM STAFF, ORG

WHERE DEPT = DEPTNUMB

GROUP BY DIVISION

SQL - 60

Making Queries using Access

Add tables

Select fields for display

Using expression to display calculated fields

Specify criteria

Total: group by, sum, avg, count, min,max

Sort

Run, modify and save

SQL view

SQL - 61

The date format is #mm/dd/yy#

SQL - 62

Query

List total number of items sold by each product

SQL - 63

SQL - 64

SQL - 65

SQL View

SELECT Products.Product_Name,

Sum(Orderln.Quantity) AS

QuantityOfSum

FROM Products INNER JOIN Orderln ON

Products.Product_Code =

Orderln.Product_Code

GROUP BY Products.Product_Name;

SQL - 66

List Order Details

List Order Details including Order

Number, Order Date, Customer Name,

Ordered items, quantity, unit price, and extend price for each order.

SQL - 67

SQL - 68

SQL - 69

SQL View

SELECT Customers.Customer_Name,

Orders.Order_Number, Orders.Order_Date,

Orderln.Product_Code, Products.Product_Name,

Products.Unit_Price, Orderln.Quantity,

[Unit_Price]*[Quantity] AS [Extend Price]

FROM Products INNER JOIN ((Customers INNER

JOIN Orders ON Customers.Customer_Name =

Orders.Customer_Name) INNER JOIN Orderln ON

Orders.Order_Number = Orderln.Order_Number) ON

Products.Product_Code = Orderln.Product_Code

WHERE (((Orders.Order_Date)>#1/1/2002#));

SQL - 70

Online SQL Tutorial

 http://www.dbbm.fiocruz.br/class/Lectu re/d17/sql/jhoffman/sqltut.html

http://www.db.cs.ucdavis.edu/teaching/ sqltutorial/tutorial.pdf

SQL - 71

SQL - 72

Download