Top Banner
Introduction to Standard Query Language Erik Zeitler UDBL [email protected]
42

Introduction to Standard Query Language Erik Zeitler UDBL [email protected].

Dec 16, 2015

Download

Documents

Dwayne Kennedy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Introduction to

Standard Query Language

Erik Zeitler

UDBL

[email protected]

Page 3: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

SQL does the job

• Data Definition Language (DDL)• Define/re-define database structure

• Data Manipulation Language (DML)• Updates• Queries

• Additional facilities• Views• Security, authorization• Integrity constraints• Transaction constraints• Rules for embedding SQL statements into other

languages

Page 4: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Outline

• Overview• What can SQL do for you?

• Background• and a simple example

• SQL and the relational data model• Example queries

• NULL values and 3-valued logic• Example queries

Page 5: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Background

• History• SEQUEL (Structures English QUery Language) – early

70’s, IBM Research• SQL (ANSI 1986), SQL1 or SQL86• SQL2 or SQL92• SQL3 or SQL99

• Core specification and optional specialized packages

• SQL consists of ~20 basic commands• A lot of research money for each SQL command…

• Standard language for all commercial DBMS• Each DBMS has features outside standard

Page 6: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Terminology

Theoretical foundation:

The relational data model

• relation –table

• tuple –row

• attribute –column

column1 … columnn

<row 2>

<row n>

relation

Attribute1 Attribute2 Attribute2

Page 7: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Example database

Schema diagram, datbase state (E/N ch 5, p 136-137)

(c) Addison Wesley Longman Inc

Page 8: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.
Page 9: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

CREATE TABLE employee ( fname varchar(100), minit char(1), lname varchar(100), ssn int(10) unsigned NOT NULL, bdate date, address varchar(100), sex char(1), salary int(10), superssn int(10), dno int(10), PRIMARY KEY (ssn)) ;

Page 10: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

unix$ mysql –u root –p> CREATE DATABASE comp;> CONNECT comp;

> CREATE TABLE emp (fname varchar(100),lname varchar(100),ssn bigint unsigned NOT NULLPRIMARY KEY (ssn)

);

> INSERT INTO emp VALUES(’Erik’, ’Zeitler’, 197510061111

);

> SELECT * FROM emp;> SELECT fname FROM emp; #

Page 11: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Recommendation

• www.mysql.com

• www.mimer.com

• Download & install on your PC

• Excellent reference manuals on the web sites

Page 12: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Basic query statement: select – from – where

SELECT A1, A2, …, An

FROM r1, r2, …, rm

WHERE P;

• A1, A2, …, An – list of attribute names to be retrieved

• r1, r2, …, rm – List of tables required to process the query

• P – Conditional expression identifying the tuples to be retrieved

• AND, OR, NOT, <, <=, =, >=, >

• Result of the query is a table

Page 13: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

SQL and the relational data model

• Projection

• Cartesian product

• Selection

• Set operations• Union

• Difference

• Intersection

• Assignment operator• Rename relations

• Join join

• Equijoin

• Natural join

Page 14: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Relation algebra projection

• Projection is done in the SELECT clause:

Ex 1, Look at interesting fields

> select * from employee;> select fname from employee;> select fname, bdate from employee;

The star (*) denotes ”all attributes”

Ex 2, projection!

> select x,y,z from vectors;

> select x,y from vectors;

Page 15: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

The SQL SELECT clause

• Projection

• Remove duplicates: distinct> select plocation from project;> select distinct plocation from project;

• Arithmetic expressions > select x/10, (y*z)/2, z+3 from vectors;

> select ssn, salary, salary*.327 from employee; #

Page 16: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Relational algebra selection

SELECT A1, A2, …, An

FROM r1, r2, …, rm

WHERE P;

• P is the selection predicate • operates on attributes in relations r1, r2, …, rm

• Selects tuples to be returned

• selection filtering

Selection in SQL: The WHERE clause

Page 17: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

The SQL WHERE clause

• Ex 1, Look for employee info

> select * from employee

where fname=’John’;

• Ex 3, vector length!

> select x,y,z from vectors

where x > 10 and x*x+y*y+z*z < 200;

• Ex 2, Look for employee info

> select * from employee

where bdate > ’1955-01-01’

and salary between 30000 and 50000;

Page 18: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Rel. algebra Cartesian product

Similar to Cartesian product of two vectors

nnn

n

nn

wvwv

wvwv

wwwvvv

1

111

2121

The Cartesian product forms

all possible pairs

of the elements

of the operands

Page 19: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

The SQL FROM clauseSimilarly, given two database tables

persons

Alex

John

Mike

cars

Audi

BMW

Mercedes

select *from persons, cars;

Alex Audi

John Audi

Mike Audi

Alex BMW

John BMW

Mike BMW

Alex Mercedes

John Mercedes

Mike Mercedes

, this SQL query generates all possible persons-cars combinations.

x =

More… #

Page 20: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Select … from … where

revisited

Relational algebra

• Cartesian product

• Selection

• Projection

Basic SQL query: three clausesselect <projection-predicate>

from <table list>

where <selection-predicate>

Page 21: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Select – from – where

Ex 1: Find all employees working at research dept

SELECT EMPLOYEE.LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENTWHERE DEPARTMENT.NAME=‘Research’

AND DNUMBER=DNO;

Ex 2:

SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAMEFROM EMPLOYEE E, EMPLOYEE SWHERE E.SUPERSSN=S.SSN;

All employees and their managers

Page 22: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

SQL and the relational data model

SELECT … FROM … WHERE …

projection,

cartesian product,

selection

• Set operations• Union

• Difference

• Intersection

• Assignment operator• Rename relations

• Join join

• Equijoin

• Natural join

Operands must be union compatible

Page 23: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Examples of set operations

• Retrieve all first names in the database> select fname from employee

union

select dependent_name from dependent;

• Are there any projects in a town without departments?> select plocation FROM project p

except

select dlocation FROM dept_locations;

#

Page 24: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

SQL and the relational data model

• Assignment operator• Rename relations

• Join join

• Equijoin

• Natural join

SELECT … FROM … WHERE …

projection,

cartesian product,

selection

• Set operations• Union – union

• Difference – except

• Intersection – intersect

Page 25: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Rename, assignment

• Rename: as

> select distinct superssnas ’manager social security number’from employee;

• Assignment: create table … as select …

> create table names asselect fname from employee

union

select dependent_name from dependent;

Page 26: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

SQL and the relational data model

• Assignment operator• Rename relations

• Join join

• Equijoin

• Natural join

SELECT … FROM … WHERE …

projection,

cartesian product,

selection

• Set operations• Union – union

• Difference – except

• Intersection – intersect

Page 27: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Join

• Relational algebra notation: R C S

• C – join condition • C is on the form AR AS

is one of {=, <, >, ≤, ≥, }

• Several terms can be connected as C1 C2…CK.

• Special cases• Equijoin: is =

• Natural join: All identically named attributes in relations R and S have matching values

Page 28: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

SQL join

• Recall this querySELECT EMPLOYEE.LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENTWHEREDEPARTMENT.NAME=‘Research’

AND DNUMBER=DNO;

• Equijoin• of employee and department tables• w.r.t. employee.dnumber and department.dno.

• Joins are cartesian productswith some selection criteria

Page 29: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

SQL join

• Another way:• alter table project change pnumber pno int(10);

Page 30: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

One more example

• Show the resulting salaries if every employee working on the ‘ProductX’ project is given a 10 percent raise

SELECT FNAME, LNAME,

1.1*SALARY AS INC_SAL

FROM EMPLOYEE, WORKS_ON, PROJECT

WHERE SSN=ESSN

AND PNO=PNUMBER

AND PNAME=‘ProductX’;

Page 31: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Special comparison• Matching string patterns

• Use LIKE• % for any number of arbitrary symbol• _ for any symbol

select * from employee where address like ’%Houston%’;

• Approx math equality• Use abs(x-x1) <

select * from employeewhere abs(salary-30000) < 8000;

• Use BETWEEN:select * from employeewhere salary between 22000 and

38000;

Page 32: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

NULL values

• Sometimes an attribute is• Unknown (date of birth unknown)• Unavailable/withheld (refuses to list home

phone #)• Not applicaple (last college degree)

• Need to represent these cases in a DB!

• Solution: NULL.• What about logical operations involving NULL?

Need to extend logic…

Page 33: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

3-valued logic

AND TRUE FALSE UNKNOWN

TRUE TRUE FALSE UNKNOWN

FALSE FALSE FALSE FALSE

UNKNOWN UNKNOWN FALSE UNKNOWN

OR TRUE FALSE UNKNOWN

TRUE TRUE TRUE TRUE

FALSE TRUE FALSE UNKNOWN

UNKNOWN TRUE UNKNOWN UNKNOWN

NOT TRUE FALSE UNKNOWN

FALSE TRUE UNKNOWN

Page 34: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Comparison of NULL values• =, , >, <, LIKE, …

• won’t work. NULL is UNDEFINED!

• SQL check for NULL•IS NULL•IS NOT NULL

• JOIN operations• Tuples with NULL values in the join columns

Not included in result

• Exception: OUTER JOIN (E/N 8.5.6)

Page 35: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

NULL

• Find out who is The Big Bossselect fname, lname

from employee

where superssn is NULL;

Page 36: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Aggregate functions

• Avg – average value

• Min – minimum value

• Max – maximum value

• Sum – sum of values

• Count – number of values

Page 37: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Aggregate functions – group by

• Average salaryselect avg(salary)

from employee;

• Average salary at each departmentselect dname, avg(salary)

from employee, department

where dno=dnumber group by dno;

Page 38: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Aggregate functions – HAVING

• Find the projects that more than two employees are assigned to:• retrieve the project number, • its name, • and the number of its employees

SELECT project.pnumber, pname , count(*)

FROM project, works_on

WHERE project.pnumber = works_on.pno

GROUP BY project.pnumber, pname

HAVING count(*)>2;

Page 39: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Summary

• Clauses:SELECT <attribute list>

FROM <table list>

[WHERE <condition>]

[GROUP BY <grouping attributes>

[HAVING <group condition>]

[ORDER BY <attribute list>]

• More Than One Way To Do It™…

Page 40: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Views

• Frequently posed queries should be expressed as views.

> create view tax_view as

select ssn, salary, salary*.327 from employee;

> select * from tax_view;

Page 41: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Views

• Creating a view will not result in a new table. Views are not tables themselves

– they are views of the underlying tables.

• A view query will return the state of the underlying tables.

• Consequence:

underlying tables are changed

the view will change

Page 42: Introduction to Standard Query Language Erik Zeitler UDBL erik.zeitler@it.uu.se.

Views• Ex 1:> update table employee

set salary = 1000000where ssn = 123456;

> select * from tax_view;

• Ex 2:We are removing one column!> alter table employee drop salary;

The view will not work any more> select * from tax_view;