01-09-2007noea/it fen - databases/sql1 more about sql nested select other joins views index

39
01-09-2007 NOEA/IT FEN - Databases/S QL 1 More about SQL Nested SELECT Other Joins Views Index

Upload: esther-leeming

Post on 02-Apr-2015

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 1

More about SQL

Nested SELECT

Other Joins

Views

Index

Page 2: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 2

SQL2 - DML

(Q16): Sub queries (nested SELECTs)

SELECT E.FNAME, E.LNAME

FROM EMPLOYEE E

WHERE E.SSN IN (SELECT ESSN

FROM DEPENDENT

WHERE ESSN = E.SSN

AND E.FNAME = DEPENDENT_NAME

AND SEX = E.SEX)

Also ANY (SOME) and ALL in combination with comparison operators

(>, >=, <, >= and <>).

Page 3: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 3

SQL2 - DML

(Q16): Sub queries (nested SELECTs)

SELECT E.FNAME, E.LNAME

FROM EMPLOYEE E

WHERE E.SSN IN (SELECT ESSN

FROM DEPENDENT

WHERE ESSN = E.SSN

AND E.FNAME = DEPENDENT_NAME

AND SEX = E.SEX)

For each row in outer table (E), the inner SELECT is executed.

If E.SSN is contained in the result of the inner SELECT, then E is included in the result table for the outer SELECT.

Page 4: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 4

SQL2 - DML(Q7): Existential quantifier - EXISTS:

SELECT FNAME, LNAME

FROM EMPLOYEE

WHERE EXISTS (SELECT *

FROM DEPENDENT

WHERE SSN = ESSN)

AND

EXISTS (SELECT *

FROM DEPARTMENTWHERE

SSN = MGRSSN)

Page 5: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 5

SQL2 - DML(Q6): NOT EXISTS:

SELECT FNAME, LNAME

FROM EMPLOYEE

WHERE NOT EXISTS (SELECT *

FROM DEPENDENT

WHERE SSN =ESSN)

Page 6: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 6

SQL2 - DML For All i SQL

• Although SQL is supposed to be an implementation of first order

predicate logic, it does not support the universal qualifier

(FORALL), only the existential quantifier (EXISTS) is supported.

• A well known (?) result from predicate logic can be used in a

workaround:

– Retrieving all elements satisfying some predicate is equivalent to

retrieving elements that are not in the set of elements that do not

satisfy the predicate:

SELECT *

FROM ---

WHERE NOT EXISTS (SELECT *

FROM ---

WHERE NOT EXISTS ----

Page 7: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 7

• Let x be an arbitrary element in some set and p a predicate stating some condition on x:

• De Morgan’s Law:(x: p(x)) x: p(x)

Apply to p(x):(x: p(x)) x: (p(x))

Reduce the right hand side:x: p(x) (x: p(x))

“it is not true that there exists x, so p(x) is not

true” – that is: “p is true for all x”

SELECT *

FROM ---

WHERE NOT EXISTS (SELECT *

FROM ---

WHERE NOT EXISTS ----

A Side: Predicate Logic

Page 8: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 8

SQL2 - DML

(Q3B): ”Retrieve the name of each employee who works on all projects controlled by department number 5”

SELECT LNAME, FNAME

FROM EMPLOYEE

WHERE NOT EXISTS

(SELECT *

FROM WORKS_ON B

WHERE (B.PNO IN (SELECT PNUMBER

FROM PROJECT

WHERE DNUM = 5))

AND

NOT EXISTS (SELECT *

FROM WORKS_ON C WHERE C.ESSN = SSN

AND C.PNO=B.PNO))

Page 9: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 9

SQL2 – DML: SELECT

Queries:

SELECT <attribute-list>

FROM <tables>

[WHERE <condition>]

[GROUP BY <attribute-list>]

[HAVING <condition>]

[ORDER BY <attribute-list>]

[...]: WHERE, GROUP BY, HAVING and ORDER BY may be omitted.

Page 10: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 10

SQL2 - DML

SELECT DISTINCT ESSN

FROM WORKS_ON

WHERE PNO IN (1,2,3)

SELECT FNAME, LNAME

FROM EMPLOYEE

WHERE SUPERSSN IS NULL

Page 11: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 11

SQL2 - DML

SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME

FROM EMPLOYEE AS E, EMPLOYEE AS S

WHERE E.SUPERSSN = S.SSN

New coulomb names in the resulting table.

AS may be omitted in FROM part.

Page 12: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 12

SQL2 - DML

Alternative notations for join:

SELECT FNAME, LNAME, ADDRESSFROM(EMPLOYEE JOIN DEPARTMENT ON

DNO=DNUMBER)WHERE DNAME = ’Research’

Provides a more clear syntax and opens for more specialised joins.

Page 13: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 13

SQL2 - DML

Natural join (not MS SQL Server):(Q1B):SELECT FNAME, LNAME, ADDRESSFROM (EMPLOYEE NATURAL JOIN (DEPARTMENT AS

DEPT

(DNAME,DNO,MSSN,MSDATE)))WHERE DNAME = ’Research’

DEPARTMENT.DNUMBER must be rename to DNO in order to match EMPLOYEE.DNO. Natural join is over two attributes with the same name (EMPLOYEE.DNO = DEPT.DNO).

Page 14: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 14

SQL2 - DML

Outer join:

SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME

FROM EMPLOYEE AS E, EMPLOYEE AS S

WHERE E.SUPERSSN = S.SSN

Retrieves only employees who have a supervisor.

Left Outer Join retrieves all employees and inserts NULL in the S-

attributes for employees with no supervisor.

SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME

FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S

ON E.SUPERSSN = S.SSN)

Also RIGTH OUTER JOIN and FULL OUTER JOIN.

Page 15: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 15

SQL2 - DML

Page 16: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 16

What about employees with no

supervisor?

Page 17: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 17

Here they are!

Page 18: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 18

SQL2 - DML

Page 19: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 19

SQL2 - DML

Page 20: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 20

SQL2 - DML

Also:– CROSS JOIN (Cartesian Product)– UNION JOIN

• SQL2 provides many different ways of expressing the same join:– This can be view as an advantage:

• More simple expressions

– Or as an disadvantage:• More complicated language

Page 21: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 21

SQL2 - DML

Aggregate Functions:

– COUNT– SUM– MAX– MIN– AVG

Page 22: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 22

SQL2 - DMLEx.: ”Number of Employees in the research department”

SELECT COUNT(*)

FROM EMPLOYEE, DEPARTMENT

WHERE DNO = DNUMBER

AND DNAME = ’Research’

Page 23: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 23

SQL2 - DML(Q24)

Page 24: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 24

Result of Q24

Page 25: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 25

SQL2 - DML(Q26)

Page 26: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 26

Result of Q26, 1

Page 27: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 27

Result of Q26, 2

Page 28: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 28

SQL2 - DML

Page 29: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 29

SQL - VIEWS

• A view is virtual table which is created from one or more existing base tables.

• Views may be used in a layered architecture to

provide different view of the database to different

users.

• May also be used to increase efficiency of frequent

queries, for instance to avoid JOINs.

Page 30: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 30

SQL - VIEWSCREATE VIEW WORKS_ON1

AS SELECT FNAME, LNAME, PNAME, HOURSFROM EMPLOYEE, PROJECT, WORKS_ONWHERE SSN=ESSN AND PNO=PNUMBER;

Using this view, the query:

SELECT FNAME, LNAME, PNAMEFROM EMPLOYEE, PROJECT, WORKS_ONWHERE PNAME = 'ProductX'

AND SSN = ESSN AND PNO = PNUMBER;May written as

SELECT FNAME, LNAME, PNAMEFROM WORKS_ON1WHERE PNAME = 'ProductX';

And hence saving the join

Page 31: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 31

SQL - VIEWS• Updating through views is problematic:• FX:

Transfer John Smith from the project 'ProductX' to the project 'ProductY’

UPDATE WORKS_ON1

SET PNAME = ’ProductY’

WHERE LNAME = ’Smith’

AND FNAME = ’John’

AND PNAME = ’ProductX’

Page 32: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 32

SQL - VIEWSWhich update of the base tables should be executed?This?

Or this?

Page 33: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 33

SQL - VIEWS

Views and update:

Updatableviews

Gray zone

Not updatable views

Page 34: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 34

SQL - VIEWS

Generally:• Views defined over one base table can be updated, if the

primary key (ore some candidate key) is included in the view.• Views defined by joining more base tables are generally not

updatable. • Some joined view are in principle updatable: all primary keys

from the base tables must be included in the view.• Views defined using aggregate or grouping functions are not

updatable.• SQL2 standard establishes that joined view are not updatable.

Page 35: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 35

Index• Index is often considered part of the DDL of SQL

– index is about internal physical storage access, and– as such NOT a part of the SQL2 standard– many SQL versions include index anyway

• Previous versions of SQL includes a CREATE INDEX statement– Is used for speeding up queries, for instance:

CREATE INDEX LNAME_INDEX ON EMPLOYEE(LNAME);

– Index is in some older SQL implementations also used to simulate

primary keys:

CREATE UNIQUE INDEX SSN_INDEX ON EMPLOYEE(SSN);

Page 36: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 36

Index

– Often it is possible to define one (and only one) clustering index:

CREATE INDEX DNO_INDEX

ONEMPLOYEE(DNO)

CLUSTER;

– The effect is that employees in the same department are stored physically close

– Hereby queries concerning employees in the same department are speeded up.

Why is that?

Page 37: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 37

Databaser (SQL) og programudviklingsværktøjer

DBMS/

SQL 3GL

Embedded SQL

OOPL 4GL (skærm- og

rapportgeneratorer, eget sprog)

Visuelle programmerings-

værktøjer (Visual Studio, Delphi, J-

Builder…)

ODBC/JDBC giver standardiseret tilgang til RDBMS

Page 38: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 38

Databaseprogrammering fra 3GLs og OOPLs

• SQL og typiske programmeringssprog som Java, C, Cobol etc. er ikke ”kompatible”:

• Impedans-mismatch:– Programmeringssprogets datatyper er ikke afstemt med

databasens domæner– SQL er mængde- (tabel-) orienteret:

• alle resultater er en tabel med et antal tupler.• 3GLs og OOPLs er tuple- (objekt-)orienteret, dvs.• der er behov for en datastruktur i

programmeringssproget

Page 39: 01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

01-09-2007 NOEA/IT FEN - Databases/SQL 39

Embedded SQL

Databaseaccess vha. SQL fra 3GL/OOPL (værtssprog):

Er baseret Cursor-begrebet:• En cursor kan opfattes som en pointer, der udpeger aktiv række i

tabellen, som SQL-sætningen resulterer i• En cursor er nødvendig, når resultatet af en SQL-sætning er mere

end een tuple (værtssproget er record- (objekt-) orienteret)• Er leverandørafhængig• Visse moderne værktøjer (fx Visual Studio.NET) kan

tilsyneladende returnere en mængde af forekomster (dataset eller ResultSet, men reelt er der tale om en indpakket cursor

• ODBC (Open Data Base Connectivity): Resultset svarer til en cursor.

• I OOPLs kaldes en cursor ofte en iterator (Enumerator i C#).