39
Database technology Lecture 2: Relational databases and SQL Jose M. Peña [email protected]

TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

Embed Size (px)

DESCRIPTION

TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL. Jose M. Peña j [email protected]. Database design process. Attributes. Relation schema. Tuples. EMPLOYEE ( FNAME, M, LNAME, SSN, BDATE, ADDRESS, S, SALARY, SUPERSSN, DNO). - PowerPoint PPT Presentation

Citation preview

Page 1: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

Database technology

Lecture 2: Relational databases and SQL

Jose M. Peñ[email protected]

Page 2: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

Database design process

Page 3: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

3

EMPLOYEE

Relational model concepts

FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO

Ramesh K Narayan 666884444 1962-09-15 … M 38000 888665555 5

Joyce A English 453453453 1972-07-31 … F 25000 888665555 5

Ahmad V Jabbar 987987987 1969-03-29 … M 25000 888665555 4

James E Borg 888665555 1937-11-10 … M 55000 null 1

Tuples ...

Relation: Set of tuples, i.e. no duplicates are allowed.Database: Collection of relations.

Attributes

......

Relation schema

EMPLOYEE ( FNAME, M, LNAME, SSN, BDATE, ADDRESS, S, SALARY, SUPERSSN, DNO)

Page 4: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

4

Relational model concepts

EMPLOYEE FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO

Ramesh K Narayan 666884444 1962-09-15 … M 38000 888665555 5

Joyce Null English 453453453 1972-07-31 … F 38000 888665555 5

Ahmad V Jabbar 987987987 1969-03-29 … M 25000 888665555 4

James Null Borg 888665555 1937-11-10 … M 55000 Null 1

Domain String shorter than 30 chars

Integer400 < x < 8000Character

M or Fyyyy-mm-dd

NULL value

Page 5: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

5

Relational model constraints

EMPLOYEE

Entity integrity constraint

FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO

Ramesh K Narayan 666884444 1962-09-15 … M 38000 888665555 5

Joyce Null English 453453453 1972-07-31 … F 38000 888665555 5

Ahmad V Jabbar 987987987 1969-03-29 … M 25000 888665555 4

James Null Borg 888665555 1937-11-10 … M 55000 Null 1

Page 6: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

6

DEPARTMENT

Relational model constraints

EMPLOYEE FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO

Ramesh K Narayan 666884444 1962-09-15 … M 38000 888665555 5

Joyce A English 453453453 1972-07-31 … F 25000 888665555 5

Ahmad V Jabbar 987987987 1969-03-29 … M 25000 888665555 4

James E Borg 888665555 1937-11-10 … M 55000 Null 1

DNAME DNUMBER MGRSSN MGRSTARTDATE

Research 5 666884444 1988-05-22

Administration 4 987987987 1995-01-01

Headquarters 1 888665555 1981-06-19

Foreign keys

Referential integrity constraint

Page 7: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

7

Relational model constraints

(Atomic) domain (or NULL). Key. Entity integrity: A PK cannot take NULL values. Referential integrity: A FK in a relation can only

refer to the PK of another relation, and the domains of the FK and PK must coincide, and the FK takes NULL value or values that exist for the PK.

Page 8: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

8

SQLrelational data model SQL relation table

attribute column

tuple row

Used in many DBMSs. Declarative (what data to get, not how). DDL (Data Definition Language)

CREATE, ALTER, DROP Queries

SELECT DML (Data Manipulation Language)

INSERT, DELETE, UPDATE …

Page 9: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

9

COMPANY schema EMPLOYEE (FNAME, MINIT, LNAME, SSN, BDATE,

ADDRESS, SEX, SALARY, SUPERSSN, DNO)

DEPT_LOCATIONS (DNUMBER, DLOCATION)

DEPARTMENT (DNAME, DNUMBER, MGRSSN, MGRSTARTDATE)

WORKS_ON (ESSN, PNO, HOURS)

PROJECT (PNAME, PNUMBER, PLOCATION, DNUM)

DEPENDENT (ESSN, DEPENDENT-NAME, SEX, BDATE, RELATIONSHIP)

Page 10: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

10

Create tables

CREATE TABLE <tablename> ( <colname> <datatype> [<constraint>], …, [<constraint>], …);

Data types: Integer, decimal, number, varchar,char, etc. Constraints: Not null, primary key, foreign key, unique, etc.

Page 11: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

11

Create tablesCREATE TABLE WORKS_ON (

ESSN integer,PNO integer,HOURS decimal(3,1),

constraint pk_workson primary key (ESSN, PNO),

constraint fk_works_emp FOREIGN KEY (ESSN) references EMPLOYEE(SSN),

constraint fk_works_proj FOREIGN KEY (PNO) references PROJECT(PNUMBER)

);

Page 12: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

12

Modify tables Change the definition of a table: Add, delete and modify

columns and constraints.

ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS CASCADE;

ALTER TABLE WORKS_ON DROP FOREIGN KEY fk_works_emp;ALTER TABLE WORKS_ON ADD CONSTRAINT fk_works_emp

FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN);

Delete a table and its definition

DROP TABLE EMPLOYEE;

Page 13: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

13

SELECT <attribute-list>FROM <table-list>WHERE <condition>;

Attribute list: A1, …, Ar Attributes whose values are required. Table list: R1, …, Rk

Relations to be queried Condition: Boolean expression It identifies the tuples that should be retrieved. It

may include comparison operators(=, <>, >, >=, etc.) and/or logical operators (and, or, not).

Query tables

Page 14: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

14

FROM EMPLOYEE;SELECT SSN

Simple query

List the SSN for all employees.

SELECTFROM

SSN

123456789333445555999887777987654321666884444453453453987987987888665555

Page 15: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

15

FROM EMPLOYEEWHERE DNO = 5;

SELECT FNAME, MINIT, LNAME,SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNO

Use of *

List all information about the employees of department 5.

SELECT

FROMWHERE

SELECT *FROM EMPLOYEE WHERE DNO = 5;

or Comparison operators {=, <>, >, =>, etc.}

Page 16: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

16

List the last name, birth date and address for all employees whose name is `Alicia J. Zelaya‘.

Simple query

SELECT FROM EMPLOYEE WHERE

LNAME BDATE ADDRESS

Zelaya 1968-07-19 3321 Castle, Spring, TX

FNAME = ‘Alicia’ AND MINIT = ‘J’

AND LNAME = ‘Zelaya’;

LNAME, BDATE, ADDRESS

Logical operators {and, or, not}

Page 17: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

17

Pattern matching

List the birth date and address for all employees whose last name contains the substring ‘aya’.

SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE LNAME LIKE ‘%aya%’;

LIKE comparison operator% replaces 0 or more characters_ replaces a single character

LNAME BDATE ADDRESS

Zelaya 1968-07-19 3321 Castle, Spring, TXNarayan 1962-09-15 975 Fire Oak, Humble, TX

Page 18: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

18

Tables as sets

List all salaries.

SELECT SALARYFROM EMPLOYEE;

SALARY

3000040000250004300038000250002500055000

Page 19: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

19

Tables as sets

SQL considers a table as a multi-set (bag), i.e. tuples can occur more than once in a table. This is different in a relational model.

Why? Removing duplicates is expensive.User may want information about duplicates.Aggregation operators.

Page 20: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

20

Example

List all salaries.

SELECT SALARYFROM EMPLOYEE;

SALARY

3000040000250004300038000250002500055000

SALARY

300004000025000430003800055000

List all salaries without duplicates.

SELECT DISTINCT SALARYFROM EMPLOYEE;

Page 21: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

21

Retrieve the first names of all people in the database.

SELECT FNAME FROM EMPLOYEEUNIONSELECT DEPENDENT_NAME FROM DEPENDENT;

Which department managers have dependents? Show their SSN.

SELECT MGRSSN FROM DEPARTMENTINTERSECTSELECT ESSN FROM DEPENDENT;

Set operationsQueries can be combined by set operations: UNION, INTERSECT, EXCEPT (MySQL only supports UNION)

Duplicate tuples are removed.

E D

M DE

Page 22: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

22

Ambiguous names: Aliasing What if the same attribute name is used in different

relations ?

No alias SELECT NAME, NAME FROM EMPLOYEE, DEPARTMENT

WHERE DNO=DNUMBER; Whole name SELECT EMPLOYEE.NAME, DEPARTMENT.NAME

FROM EMPLOYEE, DEPARTMENT WHERE

EMPLOYEE.DNO=DEPARTMENT.DNUMBER; Alias SELECT E.NAME, D.NAME

FROM EMPLOYEE E, DEPARTMENT DWHERE E.DNO=D.DNUMBER;

Page 23: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

23

Join: Cartesian product List all employees and the names of their departments.

SELECT LNAME, DNAMEFROM EMPLOYEE, DEPARTMENT;

LNAME DNAME

Smith ResearchWong ResearchZelaya ResearchWallace Research

Narayan Research

English Research

Jabbar Research

Borg Research

Smith Administration

Wong Administration

Zelaya Administration

Wallace AdministrationNarayan Administration

English Administration

Jabbar Administration

Borg Administration

Smith Headquarters

Wong Headquarters

Zelaya Headquarters

Wallace Headquarters

Narayan Headquarters

English Headquarters

Jabbar Headquarters

Borg Headquarters

ResearchAdministrationheadquarters

DNAMEDEPARTMENT DNUM

541

SmithWong Zelaya WallaceNarayanEnglishJabbarBorg

LNAMEEMPLOYEE DNO

55445541

Page 24: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

24

Join: Equijoin

Equijoin

List all employees and the names of their departments.

SELECT LNAME, DNAMEFROM EMPLOYEE, DEPARTMENTWHERE DNO = DNUMBER;

Thetajoin {=, <>, >, =>, <=, !=}

LNAME DNO DNAME DNUMBER

Smith 5 Research 5Wong 5 Research 5Zelaya 4 Research 5Wallace 4 Research 5Narayan 5 Research 5English 5 Research 5Jabbar 4 Research 5Borg 1 Research 5Smith 5 Administration 4Wong 5 Administration 4Zelaya 4 Administration 4Wallace 4 Administration 4Narayan 5 Administration 4English 5 Administration 4Jabbar 4 Administration 4Borg 1 Administration 4Smith 5 Headquarters 1Wong 5 Headquarters 1Zelaya 4 Headquarters 1Wallace 4 Headquarters 1Narayan 5 Headquarters 1English 5 Headquarters 1Jabbar 4 Headquarters 1Borg 1 Headquarters 1

Primary key in DEPARTMENT

Foreign key inEMPLOYEE

Cartesian product

Page 25: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

25

Join: Self-join

List the last name for all employees together with the last names of their bosses.

SELECT E.LNAME “Employee”, S. LNAME “Boss”

FROM EMPLOYEE E, EMPLOYEE SWHERE E.SUPERSSN = S.SSN;

Employee Boss

Smith WongWong BorgZelaya WallaceWallace BorgNarayan WongEnglish WongJabbar Wallace

Page 26: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

26

Join: Inner join

List the last name for all employees together with the last names of their bosses.

SELECT E.LNAME “Employee”, S.LNAME “Boss” FROM EMPLOYEE E, EMPLOYEE SWHERE E.SUPERSSN = S.SSN;

SELECT E.LNAME “Employee”, S.LNAME “Boss” FROM EMPLOYEE E INNER JOIN EMPLOYEE S

ON E.SUPERSSN = S.SSN;

Page 27: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

27

Join: Outer join List the last name for all employees

and, if available, show the last names of their bosses.

SELECT E.LNAME “Employee”, S. LNAME “Boss” FROM EMPLOYEE E LEFT JOIN EMPLOYEE S ON E.SUPERSSN = S.SSN;

LEFT JOIN, RIGHT JOIN, FULL JOIN

Employee Boss

Smith WongWong BorgZelaya WallaceWallace BorgNarayan WongEnglish WongJabbar WallaceBorg NULL

Page 28: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

28

Joins revisited A1 A2100 A

null B

300 C

null D

B1 B2100 W

200 X

null Y

null Z

A B

Cartesian productSELECT * FROM a, b;

Equijoin, natural join, inner joinSELECT * from A, B WHERE A1=B1;

A2 A1 B1 B2A 100 100 W

B null 100 W

C 300 100 W

D null 100 W

A 100 200 X

B null 200 X

C 300 200 X

D null 200 X

A 100 null Y

B null null Y

C 300 null Y

D null null Y

A 100 null Z

B null null Z

C 300 null Z

D null null Z

A2 A1 B1 B2A 100 100 W

A2 A1 B1 B2C 300 100 W

C 300 200 X

Thetajoin SELECT * from A, B WHERE A1>B1;

Page 29: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

29

Outer joins revisited A1 A2100 A

null B

300 C

null D

B1 B2100 W

200 X

null Y

null Z

A B

Full outer join (union of right+left)SELECT * FROM A FULL JOIN b on A1=B1;

Left outer joinSELECT * FROM A LEFT JOIN B on A1=B1;

A2 A1 B1 B2A 100 100 W

C 300 null null

B null null null

D null null null

Right outer join SELECT * FROM A RIGHT JOIN B on A1=B1;

A2 A1 B1 B2A 100 100 W

null null 200 X

null null null Y

null null null Z

A2 A1 B1 B2A 100 100 W

null null 200 X

null null null Y

null null null Z

C 300 null null

B null null null

D null null null

Page 30: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

30

Subqueries List all employees that do not have any project assignment with more than 10

hours.

SELECT LNAME FROM EMPLOYEE, WORKS_ON WHERE SSN = ESSN AND HOURS <= 10.0;

SELECT LNAME FROM EMPLOYEEWHERE SSN NOT IN (SELECT ESSN FROM WORKS_ON

WHERE HOURS > 10.0);OrSELECT LNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM WORKS_ON

WHERE SSN = ESSN AND HOURS > 10.0);

{>, >=, <, <=, <>} +{ANY, SOME, ALL}

EXISTS

Page 31: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

31

Extended SELECT syntaxSELECT <attribute-list and function-list>FROM <table-list>[ WHERE <condition> ][ GROUP BY <grouping attribute-list>] [ HAVING <group condition> ][ ORDER BY <attribute-list> ];

Page 32: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

32

Aggregate functions Built-in functions: AVG(), SUM(), MIN(), MAX(), COUNT() They appear only in SELECT and HAVING clauses. NULL values are not considered in the computations.

List the total number of employees.

SELECT COUNT(*)FROM EMPLOYEE;

50 50100 100Null 075 50AVG()

Page 33: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

33

Grouping Used to apply an aggregate function to subgroups of tuples

in a relation. GROUP BY: Grouping attributes. HAVING: Condition that a group has to satisfy.

List, for each department with more than two employees, the department number, the number of employees and the average salary.

SELECT DNO, COUNT(*), AVG(SALARY)FROM EMPLOYEE GROUP BY DNOHAVING COUNT(*) > 2;

DNO COUNT(*) AVG(SALARY)

5 4 332504 3 310001 1 55000

Page 34: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

34

Sort query results Show the department names and their locations in

alphabetical order.

SELECT DNAME, DLOCATION FROM DEPARTMENT D, DEPT_LOCATIONS DLWHERE D.DNUMBER = DL.DNUMBERORDER BY DNAME ASC, DLOCATION DESC;

DNAME DLOCATION

Administration Stafford Headquarters Houston Research SugarlandResearch Houston Research Bellaire

Page 35: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

35

Null values

List all employees that do not have a boss.

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE SUPERSSN IS NULL;

‘SUPERSSN = NULL’ and‘SUPERSSN <> NULL’will not return any matching tuples, because NULL is incomparable to any value, including another NULL.

Page 36: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

36

Insert data INSERT INTO <table> (<attr>,…) VALUES ( <val>, …) ;INSERT INTO <table> (<attr>, …) <subquery> ;

Store information about how many hours an employee works for the project ’1' into WORKS_ON.

INSERT INTO WORKS_ON VALUES (123456789, 1, 32.5);

Integrity constraint!Referential integrity constraint!

Page 37: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

37

Update data

UPDATE <table> SET <attr> = <val> ,… WHERE <condition> ;

UPDATE <table> SET (<attr>, ….) = ( <subquery> )WHERE <condition> ;

Give all employees in the ‘Research’ department a 10% raise in salary.

UPDATE EMPLOYEESET SALARY = SALARY*1.1WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME = ‘Research’);

Integrity constraint!Referential integrity constraint!

Page 38: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

38

DEPARTMENT

8886655551Headquarters

9876543214Administration

3334455555Research

MGRSSNDNUMBERDNAME

Foreign key

Delete dataDELETE FROM <table> WHERE <condition> ;

Delete the employees having the last name ‘Borg’ from the EMPLOYEE table.

DELETE FROM EMPLOYEEWHERE LNAME = ‘Borg’;

EMPLOYEE FNAME M LNAME SSN

Ramesh K Narayan 666884444

Joyce A English 453453453

Ahmad V Jabbar 987987987

James E Borg 888665555

ON DELETE SET NUL / DEFAULT / CASCADE ?

Referential integrity constraint!

Page 39: TDDD37 Database technology TDDD46 Database technology Lecture 2: Relational databases and SQL

39

Views A virtual table derived from another (possibly

virtual) tables, i.e. always up-to-date.

CREATE VIEW dept_view AS SELECT DNO, COUNT(*), AVG(SALARY)

FROM EMPLOYEE GROUP BY DNO; Why?

Simplify query commands. Provide data security. Enhance programming productivity.

Update problems.