0% found this document useful (0 votes)
2 views

SQL

The document provides an overview of SQL, its data model, and various commands including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). It covers the creation and management of database tables, data insertion, deletion, updating, and querying techniques. Additionally, it discusses the importance of constraints like primary keys and foreign keys in maintaining data integrity within relational databases.

Uploaded by

fahim.csecu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL

The document provides an overview of SQL, its data model, and various commands including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). It covers the creation and management of database tables, data insertion, deletion, updating, and querying techniques. Additionally, it discusses the importance of constraints like primary keys and foreign keys in maintaining data integrity within relational databases.

Uploaded by

fahim.csecu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 136

Database Systems

SQL

Dr. Rudra Prtap Deb Nath


Assistant Professor
Department of Computer Science and
Engineering
University of Chittagong
Motivation

SQL is the query language


SQL is very widely used
SQL is well supported by relational database systems
Learning Goals

Explain and use the SQL data model


Create non-trivial database tables
Modify non-trivial database tables
Create non-trivial SQL statements
SQL

SQL is a declarative query language


it specifies what is to obtain and not how to do it

Data Transaction
Data Definition Data Query Data Control
Manipulation Control
Language Language Language
Language Language
BEGIN;
CREATE TABLE INSERT INTO
professor( SELECT name
professor GRANT select
empid integer, FROM professor
VALUES (2136, COMMIT; ON professor
name WHERE
varchar(10), 'Curie', 'C4', TO some_user;
rank = 'C4';
rank char(2)); 36);
ROLLBACK;

DDL DML DQL TCL DCL


Relations vs. tables

Until now we have considered relations as sets of tuples


In SQL we work with tables (bags, multisets)
When working with tables, we will refer to the attributes as columns and to
the tuples as rows
What is the difference in terms of duplicates, order, keys?
DDL – Create Table

CREATE TABLE professor(


empid integer UNIQUE NOT NULL , • char(n) always uses n bytes
name varchar(10) NOT NULL , • varchar(n) uses only the
rank char(2)
);
required place, plus length
information
empid name rank
2125 Socrates C4
2126 Russel C4
2128 Russel C2 The UNIQUE constraint
2133 Popper C3 allows the same value in a
2134 Augustinus C3 column to occur only once.
2137 Kant C4

Data types
DDL – Create Table

CREATE TABLE professor( CREATE TABLE professor2 AS


empid integer UNIQUE NOT NULL, (SELECT * FROM professor);
name varchar(10) NOT NULL,
rank char(2)
);

empid name rank empid name rank


2125 Socrates C4 2125 Socrates C4
2126 Russel C4 2126 Russel C4
2128 Russel C2 2128 Russel C2
2133 Popper C3 2133 Popper C3
2134 Augustinus C3 2134 Augustinus C3
2137 Kant C4 2137 Kant C4
DDL – Primary key

PRIMARY KEY declares an attribute as primary key


The primary key constraint includes the NOT NULL and UNIQUE constraints.

CREATE TABLE professor( CREATE TABLE professor(


empid integer PRIMARY KEY, empid integer,
name varchar(10) NOT NULL, name varchar(10) NOT NULL,
rank char(2) rank char(2),
); PRIMARY KEY(empid)
);
DDL - Foreign keys

Valid values of a foreign key must reference an existing value of a key in the
referenced table
CREATE TABLE course(
courseid integer PRIMARY KEY,
title varchar(30) NOT NULL,
ects integer,
taughtby integer,
FOREIGN KEY(taughtby) REFERENCES professor(empid)
);
DDL - Foreign keys

Foreign keys can also reference candidate keys enforced with UNIQUE
constraints
CREATE TABLE professor(
CREATE TABLE course( empid integer PRIMARY KEY,
courseID integer PRIMARY KEY, name varchar(10) NOT NULL,
title varchar(30) UNIQUE NOT NULL, rank char(2)
ects integer );
);
CREATE TABLE teaches(
title varchar(30) REFERENCES course(title) ON DELETE CASCASE,
empid integer REFERENCES professor(empid) ON DELETE SET NULL,
semester char(1)
);
DDL – Default values

When inserting data into a table, all values that are not explicitly stated are
set to null (standard default value).
When defining a table, we can specify another default value.

CREATE TABLE professor(


empid integer PRIMARY KEY,
name varchar(10) NOT NULL,
rank char(2) DEFAULT 'C1'
);
DDL – Sequence number generators

Sequence number generators automatically create continuous identifiers.

CREATE SEQUENCE serial START 1000;

CREATE TABLE professor(


empid integer PRIMARY KEY,DEFAULT nextval('serial'),
name varchar(10) NOT NULL,
rank char(2)
);
DDL – Alter Table
Add an attribute
ALTER TABLE professor
ADD COLUMN (office integer);

CREATE TABLE professor( Delete an attribute


empid integer PRIMARY KEY, ALTER TABLE professor
name varchar(10) NOT NULL, DROP COLUMN rank;
rank char(2)
);
Change an attribute type
ALTER TABLE professor
ALTER COLUMN name type varchar(30);
DDL – Delete Table

Delete a table
DROP TABLE professor;

Delete the content of a table

TRUNCATE TABLE professor;


Cannot be used if there is another table with a foreign key referencing the
table we want to delete. Use CASCADE CONSTRAINTS to force the
deletion.
DML – Data insertion CREATE TABLE professor(
empid integer PRIMARY KEY,
name varchar(10) NOT NULL,
rank char(2) DEFAULT 'C1'
INSERT INTO professor VALUES );
(2136, 'Curie', 'C4');
empid name rank
2125 Socrates C4
INSERT INTO professor VALUES
2126 Russel C4
(2146, 'Curie');
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2136 Curie C4
2146 Curie C1
DML – Data insertion
CREATE TABLE student (
studid integer UNIQUE NOT NULL,
name varchar(30) NOT NULL,
semester integer
);

studid name semester


INSERT INTO student (studid, name) 24002 Xenokrates 18
25403 Jonas 12
VALUES 26120 Fichte 10
(29999, 'James'), 26830 Aristoxenos 8
(31555, 'Christian'); 27550 Schopenhauer 6
28106 Carnap 3
29120 Theophrastos 2
29555 Feuerbach 2
29999 James ⊥
31555 Christian ⊥
DML – Data insertion
CREATE TABLE takes(
studid integer references student(studid),
INSERT INTO takes courseid integer references course(courseid)
);
SELECT studid, courseid
FROM student, course
WHERE title = 'Basics'; studid courseid
24002 5001
student 25403 5001
studid name semester 26120 5001
24002 Xenokrates 18
25403 Jonas 12
26830 5001
26120 Fichte 10
26830 Aristoxenos 8
course
courseid title ects taughtBy
5001 Basics 4 2137
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126
DML – Data deletion
CREATE TABLE student (
studid integer UNIQUE NOT NULL,
name varchar(30) NOT NULL,
semester integer
);

studid name semester studid name semester


24002 Xenokrates 18 25403 Jonas 12
DELETE FROM student 26120 Fichte 10
25403 Jonas 12
26120 Fichte 10 WHERE semester > 13; 26830 Aristoxenos 8
26830 Aristoxenos 8 27550 Schopenhauer 6
27550 Schopenhauer 6 28106 Carnap 3
28106 Carnap 3 29120 Theophrastos 2
29120 Theophrastos 2 29555 Feuerbach 2
29555 Feuerbach 2
** An attempt to delete a record with a value that is tied to an integrity constraint raises an error. Use
ON DELETE CASCASE or ON DELETE SET NULL when you create the foreign key constraint.
DML – Data deletion
CREATE TABLE student (
studid integer UNIQUE NOT NULL,
name varchar(30) NOT NULL,
semester integer
);

studid name semester studid name semester


24002 Xenokrates 18
25403 Jonas 12
26120 Fichte 10 DELETE FROM student;
26830 Aristoxenos 8
27550 Schopenhauer 6
28106 Carnap 3
29120 Theophrastos 2
29555 Feuerbach 2
DML – Data update
CREATE TABLE student (
studid integer UNIQUE NOT NULL,
name varchar(30) NOT NULL,
semester integer
);

studid name semester studid name semester


24002 Xenokrates 18 24002 Xenokrates 19
25403 Jonas 12
UPDATE student 25403 Jonas 13
26120 Fichte 10 SET semester=semester+1; 26120 Fichte 11
26830 Aristoxenos 8 26830 Aristoxenos 9
27550 Schopenhauer 6 27550 Schopenhauer 7
28106 Carnap 3 28106 Carnap 4
29120 Theophrastos 2 29120 Theophrastos 3
29555 Feuerbach 2 29555 Feuerbach 3
DML – Data update
CREATE TABLE student (
studid integer UNIQUE NOT NULL,
name varchar(30) NOT NULL,
semester integer
);

studid name semester studid name semester


24002 Xenokrates 18 UPDATE student 24002 Xenokrates 18
25403 Jonas 12 SET semester=semester+1 25403 Jonas 13
26120 Fichte 10 WHERE semester < 18; 26120 Fichte 11
26830 Aristoxenos 8 26830 Aristoxenos 9
27550 Schopenhauer 6 27550 Schopenhauer 7
28106 Carnap 3 28106 Carnap 4
29120 Theophrastos 2 29120 Theophrastos 3
29555 Feuerbach 2 29555 Feuerbach 3
*Check the availabilty of the value in the parent table when you update a foreign key columnm
DCL – Right management

Grant access rights


GRANT select (empid), update (office)
ON professor
TO some_user, another_user;

Revoke access rights


REVOKE ALL PRIVILEGES
ON professor
FROM some_user, another_user;

Privileges (rights) on tables, columns,…: select, insert, update, delete, rule,


references, trigger
TCL – Transaction Control

Begin a transaction
BEGIN;

End a transaction and make all the changes made by the transaction visible
to others and become permanent
COMMIT;

Reset a transaction and discard all the changes made by the transaction
ROLLBACK;
DQL – Query Language

Basic building blocks of an SQL query

Projection list, it can include arithmetic


π SELECT <list of columns>
operators
× FROM <list of tables> Table list with optional renaming

σ WHERE <condition>; Selection and join conditions, it can


include nested queries

SFW block
DQL – Cross Product (Cartesian product)

If the FROM clause enumerates more than a single tables, the cross product
is computed
The result is the set of all combinations of tuples in the involved tables

SELECT *
FROM professor, course;

SELECT *
FROM professor CROSS JOIN course;
DQL - Standard join

Join can be expressed by cross product in combination with a predicate in


the WHERE clause (early SQL versions had no explicit join operators)

SELECT *
FROM professor, course
WHERE professor.empid = course.taughtby;
DQL – Natural Join
professor course
empid name rank courseid title ects empid
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2134 Augustinus C3 5043 Theory of Cognition 3 2126
2137 Kant C4

SELECT * FROM professor NATURAL JOIN course;

empid name rank courseid title ects


2125 Socrates C4 5041 Ethics 4
2126 Russel C4 5043 Theory of Cognition 3
2137 Kant C4 5001 Basics 4
DQL – Natural Join
SELECT * FROM professor CROSS JOIN course;
professor empid name rank course courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2134 Augustinus C3 5043 Theory of Cognition 3 2126
2137 Kant C4

empid name rank courseid title ects taughtby


2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5001 Basics 4 2137
2134 Augustinus C3 5001 Basics 4 2137
2137 Kant C4 5001 Basics 4 2137
2125 Socrates C4 5041 Ethics 4 2125
2126 Russel C4 5041 Ethics 4 2125
2134 Augustinus C3 5041 Ethics 4 2125
2137 Kant C4 5041 Ethics 4 2125
2125 Socrates C4 5043 Theory of Cognition 3 2126
2126 Russel C4 5043 Theory of Cognition 3 2126
2134 Augustinus C3 5043 Theory of Cognition 3 2126
2137 Kant C4 5043 Theory of Cognition 3 2126
DQL – Join
Theta join
SELECT *
FROM professor JOIN course
ON professor.empid = course.taughtby
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2134 Augustinus C3 5043 Theory of Cognition 3 2126
2137 Kant C4

empid name rank courseid title ects taughtby


2125 Socrates C4 5041 Ethics 4 2125
2126 Russel C4 5043 Theory of Cognition 3 2126
2137 Kant C4 5001 Basics 4 2137
DQL – Join
Equi join
SELECT * FROM professor JOIN course USING (empid);
professor course
empid name rank courseid title ects empid
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2134 Augustinus C3 5043 Theory of Cognition 3 2126
2137 Kant C4

empid name rank courseid title ects


2125 Socrates C4 5041 Ethics 4
2126 Russel C4 5043 Theory of Cognition 3
2137 Kant C4 5001 Basics 4
Example professor(empid, name, rank)
course(courseid, title, ects, empid)

SELECT empid, name, title


FROM professor, course
WHERE professor.empid = course.empid;

Why does this statement yield an error?


The result of the join has two
columns with name “empid”
(ambiguous reference)
SELECT professor.empid, name, title
FROM professor, course Correct statement
WHERE professor.empid = course.empid;
DQL – Renaming Tables

Table names can optionally be assigned a tuple variable


SELECT * SELECT * SELECT *
FROM professor; FROM professor AS P; FROM professor P;

professor
P empid name rank
2125 Socrates C4
2126 Russel C4
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2137 Kant C4 The keyword AS is optional
DQL – Renaming Tables

If a tuple variable (table alias) is declared, columns can only be referenced


by the variable
SELECT *
FROM professor, course
WHERE professor.empid = course.taughtby;

SELECT *
FROM professor P, course C
WHERE P.empid = C.taughtby;
Naming intermediate results

Intermediate result tables obtained from SQL operations or SFW blocks can
be assigned names (tuple variables)
Variables that rank over tuples of a table

SELECT teaching.name
FROM (professor NATURAL JOIN course) AS teaching;
The keyword AS is optional
DQL – Accessing tables multiple times

The declaration of tuple variables enables accessing a table multiple times


course courseid title ects taughtBy
SELECT * 5001 Basics 4 2137
FROM course c1, course c2; 5041 Ethics 4 2125
5043 Theory of Cognition 3 2126
c1 c2

courseid title ects taughtBy courseid title ects taughtBy


5001 Basics 4 2137 5001 Basics 4 2137
5001 Basics 4 2137 5041 Ethics 4 2125
5001 Basics 4 2137 5043 Theory of Cognition 3 2126
5041 Ethics 4 2125 5001 Basics 4 2137
5041 Ethics 4 2125 5041 Ethics 4 2125
5041 Ethics 4 2125 5043 Theory of Cognition 3 2126
5043 Theory of Cognition 3 2126 5001 Basics 4 2137
5043 Theory of Cognition 3 2126 5041 Ethics 4 2125
5043 Theory of Cognition 3 2126 5043 Theory of Cognition 3 2126
DQL - The projection columns

SELECT [DISTINCT] projectionList


FROM ...

“*” represents all columns of all tables in the FROM clause


It can include a list of:
Columns of tables in the FROM clause
Arithmetic expressions over constants and columns of tables in the FROM clause
Aggregate functions over columns of tables in the FROM clause
DQL - Duplicate Elimination

SELECT name
FROM professor;
professor
empid name rank name
2125 Socrates C4 Socrates
2126 Russel C4 Russel The results is a
2128 Russel C2 Russel bag (multiset)
2133 Popper C3 Popper
2134 Augustinus C3 Augustinus
2137 Kant C4 Kant
DQL - Duplicate Elimination

SELECT DISTINCT name


FROM professor;
professor
empid name rank name
2125 Socrates C4 Socrates The results is a set
2126 Russel C4 Russel and corresponds to the
2128 Russel C2 Popper projection operation in
2133 Popper C3 Augustinus relational algebra
2134 Augustinus C3 Kant
2137 Kant C4
DQL - Ordering

SELECT name
FROM professor
ORDER BY rank
professor
empid name rank name
2125 Socrates C4 C2 Russel The output tuples are
2126 Russel C4 C3
Popper sorted ascending by
2128 Russel C2 Augustinus default
2133 Popper C3 Socrates
2134 Augustinus C3 C4 Russel
2137 Kant C4 Kant
DQL - Ordering

SELECT name ASC: ascending


FROM professor DESC: descending
ORDER BY rank DESC, name ASC;
professor
empid name rank name
2125 Socrates C4 Kant
2126 Russel C4 C4 Russel
2128 Russel C2 Socrates
2133 Popper C3 C3
Augustinus
2134 Augustinus C3 Popper
2137 Kant C4 C2 Russel
DQL - Conditions
SELECT ...
FROM ...
WHERE condition

Comparisons to other columns and/or constants


Operators: =, <>, >, <, >=, <=, LIKE, BETWEEN, IN, IS
Combinations of conditions using operators AND, OR, NOT
Any theta-join predicate can be used in the WHERE clause

IS NULL tests for null values


DQL - Conditions on a single table

SELECT empid, name


FROM professor
WHERE rank = 'C4';
professor empid name
empid name rank 2125 Socrates
2125 Socrates C4 2126 Russel
2126 Russel C4 2137 Kant
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2137 Kant C4
Conditions on multiple tables
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126
professor(empid, name, rank) course(courseid, title, ects, taughtby)

Which professor teaches “Ethics”? SQL does not define the order
in which selection, projection,
SELECT DISTINCT name and join are executed
FROM professor, course
WHERE empid = taughtby AND title = 'Ethics';
πname(σempid=taughtby ∧ title='Ethics'(professor x course))
Set Operations

Set operations require union compatibility: same number of attributes with


compatible domains
Identical domains, domains based on characters, or domains based on numerical
values
Result schema: column names of the first table empid name
2125 Socrates
SELECT empid, name FROM professor professor 2126 Russel
UNION 2137 Kant
SELECT studid, name FROM student; 24002 Xenokrates
student 25403 Jonas
26120 Fichte
Duplicates and set operations

For set operations, duplicate elimination (e.g., UNION DISTINCT) is the


default
name
(SELECT name FROM professor) Socrates
UNION Russel
(SELECT name FROM professor); Kant

(SELECT name FROM professor) name


Socrates
UNION ALL
Russel
(SELECT name FROM professor); Kant
Socrates
See other set operations Russel
Kant
Nested Queries

Subqueries are necessary for comparisons to sets of values


Standard comparisons in combination with quantifiers: ALL or ANY
Special keywords to access sets: IN and EXISTS

SELECT name
FROM professor
Uncorrelated subquery
WHERE empid IN (SELECT taughtby
FROM course);
Uncorrelated Subqueries

Determine the empid of the professors with offices in the same building as
professors from the department of Philosophy (PHI)

SELECT empid
FROM professor
WHERE building IN (SELECT building
professor FROM professor
empid name rank dept building WHERE dept = 'PHI');
2125 Socrates C4 PHI 2
empid
2126 Russel C4 SCI 7
2128 Russel C2 PHI 2 2125
2133 Popper C3 CS 9 2128
2134 Augustinus C3 THE 2 2134
2137 Kant C4 PHI 2 2137
Example

Determine the empid of the professors with offices in the same building as
professors from the department of Philosophy (PHI) without using IN
professor(empid, name, rank, dept, building)

SELECT DISTINCT p1.empid


FROM professor p1, professor p2
WHERE p1.building = p2.building AND p2.dept = 'PHI';
Uncorrelated Subqueries

ANY compares a value with a set of values


The condition is true for at least of element of the set

SELECT name name


FROM professor Socrates
WHERE rank > ANY (SELECT rank Russel
FROM professor); Popper
professor empid name rank Augustinus
2125 Socrates C4
2126 Russel C4
Kant
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2137 Kant C4
Uncorrelated Subqueries

ALL compares a value with a set of values


The condition is true for all the elements of the set
name
SELECT name
Socrates
FROM professor
WHERE rank >= ALL (SELECT rank Russel
FROM professor); Kant
professor empid name rank
2125 Socrates C4
2126 Russel C4
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2137 Kant C4
Simulation of the difference operator

πempid(professor) − πtaughtby(course)

professor
empid name rank
2125 Socrates C4 SELECT empid
2126 Russel C4 FROM professor
2128 Russel C2 WHERE empid NOT IN (SELECT taughtby
2133 Popper C3
2134 Augustinus C3
FROM course);
2137 Kant C4

course empid
courseid title ects taughtby 2128
5001 Basics 4 2137
2133
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126 2134
Simulation of the difference operator

πempid(professor) − πtaughtby(course)

professor
empid name rank
2125 Socrates C4 (SELECT empid FROM professor)
2126 Russel C4 EXCEPT
2128 Russel C2
2133 Popper C3
(SELECT taughtby FROM course);
2134 Augustinus C3
2137 Kant C4

course empid
courseid title ects taughtby 2128
5001 Basics 4 2137
2133
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126 2134
Correlated Subqueries

What is computed here? Names of the professors


professor that teach any courses
empid name rank SELECT name
2125 Socrates C4 FROM professor p
2126 Russel C4
2128 Russel C2
WHERE EXISTS (SELECT *
2133 Popper C3 FROM course c
2134 Augustinus C3 WHERE c.taughtby = p.empid);
2137 Kant C4

course name
courseid title ects taughtby Socrates
5001 Basics 4 2137 Russel
5041 Ethics 4 2125 Kant
5043 Theory of Cognition 3 2126
Correlated Subqueries

What is computed here?


We test for existence, for each
professor inner result tuple, 42 is returned
empid name rank
2125 Socrates C4 SELECT name
2126 Russel C4 FROM professor p
2128 Russel C2 WHERE EXISTS (SELECT 42
2133 Popper C3
2134 Augustinus C3
FROM course c
2137 Kant C4 WHERE c.taughtby = p.empid);

course name
courseid title ects taughtby Socrates
5001 Basics 4 2137 Russel
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126 Kant
IN vs. EXISTS

SELECT name
FROM professor
WHERE empid IN (SELECT taughtby
FROM course);
Is the “left tuple” in the “right set”
SELECT name
FROM professor
WHERE EXISTS (SELECT *
FROM course c Is the “right set” nonempty?
WHERE taughtby = empid);
Expressiveness of the SQL core

Relational algebra SQL


π projection SELECT DISTINCT
σ selection WHERE without nesting
⨝ join FROM, WHERE
FROM with JOIN or NATURAL JOIN
ρ renaming FROM with tuple variable; AS
- difference WHERE with nesting / EXCEPT
∩ intersection WHERE with nesting / INTERSECT
∪ union UNION
DQL – Advanced SQL

Extension of the SFW block

… plus application of scalar operations


π SELECT <list of columns>
and aggregate functions
× FROM <list of tables> … plus additional join variants

σ WHERE <condition>; … plus additional types of conditions

… plus GROUP BY and HAVING,


WITH, window functions, CASE
DQL – Join Variants
Standard formulation (CROSS JOIN)
SELECT *
FROM R1, R2
WHERE R1.A = R2.B;
Alternative formulation (INNER JOIN)
SELECT *
FROM R1 JOIN R2
ON R1.A = R2.B;
NATURAL JOIN
SELECT *
FROM R1 NATURAL JOIN R2;
Other variants:
LEFT, RIGHT, or FULL OUTER JOIN
DQL – NATURAL JOIN

List all the students that have been graded along with their grades

SELECT *
FROM student NATURAL JOIN grades;
student grades
studid name semester studid courseid empid grade
24002 Xenokrates 18 28106 5001 2126 1
25403 Jonas 12 25403 5041 2125 2
26120 Fichte 10 27550 4630 2137 2
26830 Aristoxenos 8
27550 Schopenhauer 6
28106 Carnap 3 studid name semester courseid empid grade
29120 Theophrastos 2 25403 Jonas 12 5041 2125 2
29555 Feuerbach 2 27550 Schopenhauer 6 4630 2137 2
28106 Carnap 3 5001 2126 1
DQL – INNER JOIN

List all the students that have been graded along with their grades
SELECT s.studid, name, semester, courseid, empid, grade
FROM student s INNER JOIN grades g
ON s.studid = g.studid;
student grades
studid name semester studid courseid empid grade
24002 Xenokrates 18 28106 5001 2126 1
25403 Jonas 12 25403 5041 2125 2
26120 Fichte 10 27550 4630 2137 2
26830 Aristoxenos 8
27550 Schopenhauer 6
28106 Carnap 3 studid name semester courseid empid grade
29120 Theophrastos 2 25403 Jonas 12 5041 2125 2
29555 Feuerbach 2 27550 Schopenhauer 6 4630 2137 2
28106 Carnap 3 5001 2126 1
DQL – LEFT OUTER JOIN

List all the students along with the grades they have obtained
grades
SELECT *
studid courseid empid grade
FROM student s LEFT OUTER JOIN grades g 28106 5001 2126 1
ON s.studid = g.studid; 25403 5041 2125 2
27550 4630 2137 2

studid name semester studid courseid empid grade


24002 Xenokrates 18 ⊥ ⊥ ⊥ ⊥
25403 Jonas 12 25403 5041 2125 2
26120 Fichte 10 ⊥ ⊥ ⊥ ⊥
26830 Aristoxenos 8 ⊥ ⊥ ⊥ ⊥
27550 Schopenhauer 6 27550 4630 2137 2
28106 Carnap 3 28106 5001 2126 1
29120 Theophrastos 2 ⊥ ⊥ ⊥ ⊥
29555 Feuerbach 2 ⊥ ⊥ ⊥ ⊥
DQL – Aggregate functions

Aggregate functions compute new values for a column, e.g., the sum or the
average of all values in a column
Aggregate functions: AVG, MAX, MIN, COUNT, SUM

SELECT SUM(ects) SELECT AVG(grade)


FROM course FROM grades
WHERE taughtby = 2125; WHERE courseid = 5001;
DQL – Aggregate functions and duplicates

The argument columns can optionally be accompanied by the keyword


DISTINCT and ALL (except in case of COUNT(*))
DISTINCT: duplicates are removed before evaluating the aggregate function
ALL: duplicates are considered for evaluation (default)
NULL values are removed before evaluation (except in case of COUNT(*))
Examples

Number of professors

SELECT COUNT(*) AS number


FROM professor;
professor number
empid name rank 6
2125 Socrates C4
2126 Russel C4
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2137 Kant C4
Examples

Number of different ranks of the professors

SELECT COUNT(DISTINCT rank) AS number


FROM professor;
professor number
empid name rank 3
2125 Socrates C4
2126 Russel C4
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2137 Kant C4
Examples
List all the students that have grades above the average
SELECT DISTINCT studid, name, semester
FROM student NATURAL JOIN grades
WHERE grade > (SELECT AVG(grade) FROM grades);
student grades AVG(grade)
studid name semester studid courseid empid grade 1.6666666666666667
24002 Xenokrates 18 28106 5001 2126 1
25403 Jonas 12 25403 5041 2125 2
26120 Fichte 10 27550 4630 2137 2
26830 Aristoxenos 8
27550 Schopenhauer 6 studid name semester
28106 Carnap 3 25403 Jonas 12
29120 Theophrastos 2 27550 Schopenhauer 6
29555 Feuerbach 2
DQL – Aggregate function in the WHERE
clause
Aggregate functions produce a single value and can be used to compare with
other single value expressions in the WHERE clause

takes (studid, courseid)


SELECT *
student (studid, name, semester)
FROM student s
WHERE 1 = (SELECT COUNT(*)
FROM takes t
WHERE t.studid = s.studid); All students taking a
single course
** It is also a correlated subquery. See Chapter 18 in Introduction to Oracle 9i for the details of correlated subqueries.
DQL – Aggregate function in the SELECT
clause
Subqueries in the SELECT clause with aggregate functions are evaluated
once for each result tuple
The subquery is correlated

SELECT empid, name, (SELECT SUM(ects)


FROM course
WHERE taughtby = empid) AS teachingLoad
FROM professor;
DQL – Aggregate functions cannot be
nested
Compute the average teaching load the professors have

SELECT AVG(teachingLoad) AS result SELECT f1(temp) AS result


FROM (SELECT SUM(ects) AS teachingLoad FROM (SELECT f2(A) AS temp
FROM R ...);
FROM course c
GROUP BY taughtby) as r;

SELECT f1(f2(A)) AS result


SELECT AVG(SUM(ects)) AS result FROM R ...;
FROM course c
GROUP BY taughtby); ** Oracle Supports nesting aggregate
functions.
DQL – Grouping

Aggregate function is computed per group

SELECT SUM(ects) AS teachingLoad


FROM course c
GROUP BY taughtby;

All tuples with the same value for column taughtby form a group
The sum is computed for each group
SELECT SUM(ects) AS teachingLoad
DQL – Grouping FROM course c
GROUP BY taughtby;

courseid title ects taughtby


5001 Basics 4 2137
G1
4630 Constructive Criticism 4 2137
teachingLoad
5041 Ethics 4 2125
5049 DBS 1 2125 G2 G1 8

4052 Logics 4 2125 G2 9

5043 Theory of Cognition 3 2126 G3 9

5052 Theory of Science 3 2126 G3 G4 2

5216 Bioethics 3 2126 G5 2

5259 Advanced Algorithms 2 2133 G4


5022 Belief and Knowledge 2 2134 G5
What is the problem with the following
statement?

SELECT rank, COUNT(empid), name


FROM professor
GROUP BY rank;

SQL generates one result tuple per group


All columns references in the SELECT clause must either be listed in
the GROUP BY clause or involved only in aggregate functions as an
argument
Correct or incorrect?

SELECT COUNT(*)
FROM course
GROUP BY taughtby;

SELECT taughtby, COUNT(*)


FROM course
GROUP BY ects;

SELECT taughtby, COUNT(*)


FROM course
GROUP BY ects, taughtby;
The HAVING clause

The HAVING clause expresses additional conditions that groups contributing


to the result have to fulfill

SELECT empid, name, SUM(ects)


FROM course, professor
WHERE taughtby = empid AND rank = 'C4'
GROUP BY taughtby, name
HAVING AVG(ects) > 3;
What is computed here?
And what are the steps?
Executing a query with GROUP BY and
HAVING SELECT empid, name, SUM(ects)
FROM course, professor
WHERE taughtby = empid AND rank = 'C4'
GROUP BY taughtby, name
HAVING AVG(ects) > 3;
courseid title ects taughtby empid name rank office
5001 Basics 4 2137 2125 Socrates C4 226
5041 Ethics 4 2125 2125 Socrates C4 226
5043 Theory of Cognition 3 2126 2125 Socrates C4 226
5049 DBS 2 2125 2125 Socrates C4 226
4052 Logics 4 2125 2125 Socrates C4 226
5052 Theory of Science 3 2126 2125 Socrates C4 226
5216 Bioethics 2 2128 2125 Socrates C4 226
5259 Advanced Algorithms 2 2133 2125 Socrates C4 226
5022 Belief and Knowledge 2 2134 2125 Socrates C4 226
4630 Constructive Criticism 4 2137 2125 Socrates C4 226
5001 Basics 4 2137 2126 Russel C4 232
… … … … … … … …
Executing a query with GROUP BY and
HAVING SELECT empid, name, SUM(ects)
FROM course, professor
WHERE taughtby = empid AND rank = 'C4'
GROUP BY taughtby, name
HAVING AVG(ects) > 3;

courseid title ects taughtby empid name rank office


5041 Ethics 4 2125 2125 Socrates C4 226
5049 DBS 2 2125 2125 Socrates C4 226
4052 Logics 4 2125 2125 Socrates C4 226
5043 Theory of Cognition 3 2126 2126 Russel C4 232
5052 Theory of Science 3 2126 2126 Russel C4 232
5001 Basics 4 2137 2137 Kant C4 7
4630 Constructive Criticism 4 2137 2137 Kant C4 7
Executing a query with GROUP BY and
HAVING SELECT empid, name, SUM(ects)
FROM course, professor
WHERE taughtby = empid AND rank = 'C4'
GROUP BY taughtby, name
HAVING AVG(ects) > 3;

courseid title ects taughtby empid name rank office AVG(ects) > 3
5041 Ethics 4 2125 2125 Socrates C4 226
5049 DBS 2 2125 2125 Socrates C4 226 true
4052 Logics 4 2125 2125 Socrates C4 226
5043 Theory of Cognition 3 2126 2126 Russel C4 232
2126 Russel false
5052 Theory of Science 3 2126 2126 Russel C4 232
5001 Basics 4 2137 2137 Kant C4 7
2137 Kant true
4630 Constructive Criticism 4 2137 2137 Kant C4 7
Executing a query with GROUP BY and
HAVING SELECT empid, name, SUM(ects)
FROM course, professor
WHERE taughtby = empid AND rank = 'C4'
GROUP BY taughtby, name
HAVING AVG(ects) > 3;
courseid title ects taughtby empid name rank office SUM(ects)
5041 Ethics 4 2125 C4 226
5049 DBS 2 2125 2125 Socrates C4 226 10
4052 Logics 4 2125 C4 226
5001 Basics 4 2137 C4 7
2137 Kant 8
4630 Constructive Criticism 4 2137 C4 7

empid name SUM(ects)


2125 Socrates 10
2137 Kant 8
DQL – Null values

NULL values may lead to unexpected query results


SELECT COUNT(semester)
FROM student
WHERE semester < 13 OR semester >= 13;

SELECT COUNT(semester)
FROM student;
Do these queries produce the same result?
Yes, in both queries the tuples with NULL values in the column semester are not counted
DQL – Null values

student
SELECT COUNT(semester) AS c
FROM student; c
studid name semester
5
24002 Xenokrates ⊥
25403 Jonas 12
26120 Fichte 10
SELECT COUNT(*) AS c
FROM student; c
26830 Aristoxenos ⊥
8
27550 Schopenhauer 6
28106 Carnap ⊥
29120 Theophrastos 2 SELECT COUNT(DISTINCT semester) AS c
29555 Feuerbach 2 FROM student; c
4
DQL – Null values

SELECT studid
student FROM student
studid name semester semester > 5 WHERE semester > 5;
24002 Xenokrates ⊥ unknown
25403 Jonas 12 true
26120 Fichte 10 true studid
26830 Aristoxenos ⊥ unknown 25403
27550 Schopenhauer 6 true 26120
28106 Carnap ⊥ unknown 27550
29120 Theophrastos 2 false
29555 Feuerbach 2 false
Tuples evaluated to unknown or
false will not be part of the result
DQL – Null values
SELECT semester, COUNT(studid) as c
FROM student
student
GROUP BY semester;
studid name semester
24002 Xenokrates ⊥ semester c
25403 Jonas 12 ⊥ 3
26120 Fichte 10 12 1
26830 Aristoxenos ⊥ 10 1
27550 Schopenhauer 6 6 1
28106 Carnap ⊥ 2 2
29120 Theophrastos 2
29555 Feuerbach 2
null is interpreted as an
independent value and
Number of students per semester results in its own group
DQL – Null values
SELECT studid
FROM student
WHERE semester + 1 <= 12;
student
studid name semester semester + 1 semester + 1 <= 12
24002 Xenokrates ⊥ ⊥ unknown
25403 Jonas 12 13 false
studid
26120 Fichte 10 11 true
unknown 26120
26830 Aristoxenos ⊥ ⊥ 27550
7 true
27550 Schopenhauer 6 29120
28106 Carnap ⊥ ⊥ unknown
true 29555
29120 Theophrastos 2 3
29555 Feuerbach 2 3 true

More on null values in expressions


DQL – Recursion

Which courses need to be taken before taking the course “Theory of Science”
requires course
predecessor successor courseid title ects taughtby
5001 5041 5001 Basics 4 2137
5001 5043 5041 Ethics 4 2125
5001 5049 5043 Theory of Cognition 3 2126
5041 5216 5049 DBS 2 2125
5043 5052 4052 Logics 4 2125
5041 5052 5052 Theory of Science 3 2126
5052 5259 5216 Bioethics 2 2128
5259 Advanced Algorithms 2 2133
SELECT predecessor 5022 Belief and Knowledge 2 2134
4630 Constructive Criticism 4 2137
FROM requires, course
WHERE successor = courseid But this query only finds the
AND title = 'Theory of Science'; direct predecessors
What courses form the result of this query?

SELECT r2.predecessor
FROM requires r1, requires r2, course c
WHERE c.title = 'Theory of Science' AND
c.courseid = r1.successor AND
r1.predecessor = r2.successor;
Theory of
r1 Advanced
Theory of
Cognition Algorithms
r2 5043
Science 5052
5259

Bioethics
Basics 5001 Ethics 5041 5216

DBS 5049
What courses form the result of this query?

SELECT rn.predecessor
FROM requires r1, requires r2,..., requires rn, course c
WHERE c.title = 'Theory of Science' AND
c.courseid = r1.successor AND
r1.predecessor = r2.successor AND
...
rn_minus_1.predecessor = rn.successor;
Level n predecessors
How to compute the complete list of predecessors (all levels)?
Level 1 UNION level 2 UNION level 3 UNION …
DQL – Recursion in SQL

WITH RECURSIVE mytable(number) AS (


... Non-recursive part
UNION
SELECT ...
FROM mytable It may reference
Recursive part
WHERE ... mytable
)
SELECT ...
FROM mytable Main query
...
;
See an example
DQL – Recursion in SQL

WITH RECURSIVE transitiveCourse(pred, succ) AS (
SELECT predecessor, successor
FROM requires Non-recursive part
UNION
SELECT DISTINCT t.pred, r.successor
FROM transitiveCourse t, requires r Recursive part
WHERE t.succ = r.predecessor
)
SELECT *
FROM transitiveCourse Main query
ORDER BY(pred, succ) ASC;
Determine all predecessors for all courses
WITH RECURSIVE transitiveCourse(pred, succ) AS (
SELECT predecessor, successor pred succ
FROM requires
UNION
5001 5041
SELECT DISTINCT t.pred, r.successor 5001 5043
FROM transitiveCourse t, requires r
WHERE t.succ = r.predecessor 5001 5049
) 5001 5052
SELECT *
FROM transitiveCourse 5001 5216
ORDER BY(pred, succ) ASC;
5001 5259
Theory of Advanced
Theory of
Algorithms 5041 5052
Cognition Science 5052
5043 5259 5041 5216
5041 5259
Bioethics
Basics 5001 Ethics 5041 5216 5043 5052
5043 5259
DBS 5049 5052 5259
Preventing infinite recursion

Most DBMS have a parameter that limits maximum recursion depth


The limit can also be encoded directly in the query
WITH RECURSIVE transitiveCourse(pred, succ, depth) AS (
SELECT predecessor, successor, 0
FROM requires
UNION
SELECT DISTINCT t.pred, r.successor, t.depth + 1
FROM transitiveCourse t, requires r
WHERE t.succ = r.predecessor AND t.depth < 1
)
SELECT *
FROM transitiveCourse
ORDER BY(pred, succ) ASC;
Determine all predecessors for all courses
WITH RECURSIVE transitiveCourse(pred, succ) AS (
SELECT predecessor, successor pred succ depth
FROM requires
UNION
5001 5041 0
SELECT DISTINCT t.pred, r.successor 5001 5043 0
FROM transitiveCourse t, requires r
WHERE t.succ = r.predecessor 5001 5049 0
) 5001 5052 1
SELECT *
FROM transitiveCourse 5001 5216 1
ORDER BY(pred, succ) ASC;
5001 5259 2
Theory of Advanced
Theory of
Algorithms 5041 5052 0
Cognition Science 5052
5043 5259 5041 5216 0
5041 5259 1
Bioethics
Basics 5001 Ethics 5041 5216 5043 5052 0
5043 5259 1
DBS 5049 5052 5259 0
Determine all predecessors for all courses
WITH RECURSIVE transitiveCourse(pred, succ, depth) AS (
SELECT predecessor, successor, 0 pred succ depth
FROM requires
UNION 5001 5041 0
SELECT DISTINCT t.pred, r.successor, t.depth + 1 5001 5043 0
FROM transitiveCourse t, requires r
WHERE t.succ = r.predecessor AND t.depth < 1 5001 5049 0
)
5001 5052 1
SELECT *
FROM transitiveCourse 5001 5216 1
ORDER BY(pred, succ) ASC;
5001 5259 2
Theory of Advanced
Theory of
Algorithms 5041 5052 0
Cognition Science 5052
5043 5259 5041 5216 0
5041 5259 1
Bioethics
Basics 5001 Ethics 5041 5216 5043 5052 0
5043 5259 1
DBS 5049 5052 5259 0
Determine all predecessors for a course
WITH RECURSIVE transitiveCourse(pred, succ) AS (
SELECT predecessor, successor
FROM requires
UNION
SELECT DISTINCT t.pred, r.successor
FROM transitiveCourse t, requires r
WHERE t.succ = r.predecessor title
) Theory of Cognition
SELECT c2.title
FROM transitiveCourse tc, course c1, course c2 Ethics
WHERE tc.succ = c1.courseid AND c1.title = 'Theory of Science' Basics
AND tc.pred = c2.courseid;
Theory of Advanced
Theory of
Cognition Algorithms
Science 5052
5043 5259

Bioethics
Basics 5001 Ethics 5041 5216

DBS 5049
DQL – Limiting the size of the results

How to limit the result to the top 5?


studid name semester
24002 Xenokrates 18
SELECT *
25403 Jonas 12
FROM student 26120 Fichte 10
ORDER BY semester DESC; 26830 Aristoxenos 8
27550 Schopenhauer 6
28106 Carnap 3
29120 Theophrastos 2
29555 Feuerbach 2
DQL – Limiting the size of the results

How to limit the result to the top 5?


studid name semester
SELECT * 24002 Xenokrates 18
25403 Jonas 12
FROM student 26120 Fichte 10
ORDER BY semester DESC 26830 Aristoxenos 8
FETCH FIRST 5 ROWS ONLY; 27550 Schopenhauer 6

• SQL 2008
• Supported by IBM DB2, Sybase
SQL Anywhere, PostgreSQL,…
DQL – Limiting the size of the results

How to limit the result to the top 5?


studid name semester
SELECT * 24002 Xenokrates 18
25403 Jonas 12
FROM student 26120 Fichte 10
ORDER BY semester DESC 26830 Aristoxenos 8
LIMIT 5; 27550 Schopenhauer 6

• Non standard syntax


• Supported by MySQL, Sybase
SQL Anywhere, PostgreSQL,…
More syntax
Database system abstraction layers
external external external View layer
schema 1 … schema n
schema 2

conceptual
schema Logical layer

internal
schema Physical layer

Physical data independence: changes on the physical layer have no


influence on the logical layer
Logical data independence: changes on the logical layer have no influence
on the view layer
External Schemas – Views
course:
{[ courseid, title, ects, taughtby ]}
professor:
{[ empid, name, rank, office ]}

CREATE VIEW profsAndtheirCourses AS


SELECT c.title, p.name
FROM professor p, course c
WHERE p.empid = c.taughtby;

SELECT * FROM profsAndtheirCourses;


External Schemas – Views
student:
{[ studid, name, semester ]}
takes:
{[ studid, courseid ]}
course:
{[ courseid, title, ects, taughtby ]}
CREATE VIEW ectsPerStud AS
SELECT s.name, s.studid, SUM(c.ects) AS sum
FROM student s, takes t, course c
WHERE t.courseid = c.courseid AND s.studid = t.studid
GROUP BY s.name, t.studid;

SELECT sum FROM ectsPerStud;

Views can be used to represent


derived attributes (ER diagram)
Altering views

REPLACE VIEW expects the same columns (same order, same types)

CREATE OR REPLACE VIEW profsAndtheirCourses AS


SELECT c.title, p.name
FROM professor p, course c
WHERE p.empid = c.taughtby;
Altering views

Alternative to REPLACE VIEW: delete the view and recreate it afterwards

DROP VIEW profsAndtheirCourses ;

CREATE VIEW profsAndtheirCourses AS


SELECT c.title, p.name
FROM professor p, course c
WHERE p.empid = c.taughtby;
Views vs. Materialized views

(Dynamic) views represent a macro of a query and their result is computed


when used
The query result of a materialized view is pre-computed
The computational load of materialized views is before any queries are
executed
Views vs. Materialized views

(Dynamic) views represent a macro of a query and their result is computed


when used
The query result of a materialized view is pre-computed
The computational load of materialized views is before any queries are
executed
Which one is the better choice?
Views vs. Materialized views

(Dynamic) views represent a macro of a query and their result is computed


when used
The query result of a materialized view is pre-computed
The computational load of materialized views is before any queries are
executed
Which one is the better choice?
Both: there is a trade-off runtime vs. update frequency
professor empid name rank office
2125 Socrates C4 226
Updating views empid avgGrade
2125 3
CREATE VIEW howTough AS 2126 2
SELECT empid, AVG(grade) AS avgGrade 2137 2
FROM grades grades
GROUP BY empid; studid courseid empid grade
24002 5001 2126 3
28106 5001 2126 1
UPDATE howTough 25403 5041 2125 2
SET avgGrade = 1.0 27550 5041 2125 4
WHERE empid = (SELECT empid 27550 4630 2137 2
FROM professor
WHERE name = 'Socrates');

How to adapt the grades in


the original table?
Updating views
course:
{[ courseid, title, ects, taughtBy ]}
CREATE VIEW courseView AS professor:
SELECT title, ects, name {[ empid, name, rank, office ]}
FROM course, professor
WHERE taughtBy = empid;

INSERT INTO courseView


VALUES ('Nihilism', 2, 'Nobody');

What tuples shall be inserted into the original tables?


A view is updatable if…

The FROM clause has only one table


The SELECT clause contains only attribute names of the table (no
aggregates, no distinct, no expressions)
Attributes not included in the SELECT clause can be set to null
Does not include GROUP BY or HAVING clause

Views in PostgreSQL “cannot” be updated


DQL – Integrity constraints

Try to avoid insertion of inconsistent data


PRIMARY KEY, NOT NULL, UNIQUE
FOREIGN KEY
Cardinality constraints of relationship types
Generalization (subtype entity is a supertype entity)
Attribute domains
Static integrity constraints

Each instance of a database must fulfill all static integrity constraints

CREATE TABLE professor(


empid integer PRIMARY KEY,
name varchar(10) NOT NULL,
rank char(2),
CHECK rank IN ('C2', 'C3', 'C4')
);
Static integrity constraints

Each instance of a database must fulfill all static integrity constraints

CREATE TABLE student (


studid integer PRIMARY KEY,
name varchar (30) NOT NULL,
semester integer,
CHECK semester BETWEEN 1 AND 20
);
Referential integrity
CREATE TABLE course (
courseid integer NOT NULL,
title character varying(30),
ects integer,
taughtby integer REFERENCES professor(empid)
);

INSERT INTO course


VALUES (5100, 'Spying for Dummies', 4, 007);

Foreign keys must always reference existing tuples or be NULL


Handling updates

Dynamic integrity constraints need to be fulfilled by each change of a


database
Possible responses to changes of referenced data
Rejection of updates (default behavior)
Propagation of updates (CASCADE)
Set references to “unknown” (SET NULL)
Set to a default value (SET DEFAULT, available in PostgreSQL)
Example

professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126

UPDATE professor DELETE FROM professor


SET empid = 2121 WHERE empid = 2125;
WHERE empid = 2125;

What to do now?
Option 1: reject the update
CREATE TABLE course (
courseid integer PRIMARY KEY,
title varchar(30),
ects integer,
taughtby integer REFERENCES professor(empid)
);

UPDATE professor DELETE FROM professor


SET empid = 2121 WHERE empid = 2125;
WHERE empid = 2125;

Result of the operation:


DB remains unchanged
Option 2: cascading updates
CREATE TABLE course (
courseid integer PRIMARY KEY,
title varchar(30),
ects integer,
taughtby integer REFERENCES professor(empid)
ON UPDATE CASCADE ON DELETE CASCADE
);

UPDATE professor DELETE FROM professor


SET empid = 2121 WHERE empid = 2125;
WHERE empid = 2125;
Option 2: cascading updates
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126

UPDATE professor
SET empid = 2121 Result of the UPDATE operation:
WHERE empid = 2125; Keys in professor and course updated
professor course
empid name rank courseid title ects taughtby
2121 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2121
2137 Kant C4 5043 Theory of Cognition 3 2126
Option 2: cascading updates
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126

DELETE FROM professor Result of the DELETE operation:


WHERE empid = 2125; Tuples in professor and course deleted

professor course
empid name rank courseid title ects taughtby
2126 Russel C4 5001 Basics 4 2137
2137 Kant C4 5043 Theory of Cognition 3 2126

Use ON DELETE CASCADE carefully!


Option 3: update and set null
CREATE TABLE course (
courseid integer PRIMARY KEY,
title varchar(30),
ects integer,
taughtby integer REFERENCES professor(empid)
ON UPDATE SET NULL ON DELETE SET NULL
);

UPDATE professor DELETE FROM professor


SET empid = 2121 WHERE empid = 2125;
WHERE empid = 2125;
Option 3: update and set null
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126

UPDATE professor Result of the UPDATE operation:


SET empid = 2121 empid set to 2121,
WHERE empid = 2125; taughtby set to NULL
professor course
empid name rank courseid title ects taughtby
2121 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 ⊥
2137 Kant C4 5043 Theory of Cognition 3 2126
Option 3: delete and set null
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126

Result of the DELETE operation:


DELETE FROM professor
Tuple in professor deleted,
WHERE empid = 2125; taughtby set to NULL

professor course
empid name rank courseid title ects taughtby
2126 Russel C4 5001 Basics 4 2137
2137 Kant C4 5041 Ethics 4 ⊥
5043 Theory of Cognition 3 2126
Complex constraints
CREATE TABLE grades (
studid integer REFERENCES student ON DELETE CASCADE,
courseid integer REFERENCES course,
grade numeric(2,1) CHECK (grade BETWEEN 0.7 AND 5.0),
PRIMARY KEY(studid, courseid)
CONSTRAINT hasTaken
CHECK (EXISTS (SELECT *
FROM takes t
WHERE t.courseid = grades.courseid AND
t.studid = grades.studid))
);
The CHECK clause is evaluated for each update or insert
Operation is rejected if the check evaluates to false
Summary

SQL is more than query language (DDL, DML, DCL, TCL, DQL)
DQL is a declarative query language, SFW block is the basis
Complex predicates, nested queries
Grouping and aggregation
Recursive queries, (materialized) views
Integrity constraints
Appendix
Basic data definitions in SQL – Data types
character(n), char(n)

character varying(n), varchar(n)

integer, smallint

numeric(p,s), decimal(p,s), ...


p – precision: max. number of digits in total
s – scale: number of digits after the comma

Real, double

blob or raw for very large binary data

clob for large string attributes

date for dates

xml for XML documents

...
varchar(n) vs. char(n)

Both varchar(n) and char(n) are limited to a length of n


char(n) always uses n bytes
varchar(n) uses only the required place, plus length information

Back to DDL
BETWEEN

BETWEEN is used for ranges


Students in the first four semester

student SELECT name, studid


studid name semester FROM student
24002 Xenokrates 18 WHERE semester BETWEEN 1 AND 4;
25403 Jonas 12
26120 Fichte 10
26830 Aristoxenos 8 studid name
27550 Schopenhauer 6 28106 Carnap
28106 Carnap 3 29120 Theophrastos
29120 Theophrastos 2 29555 Feuerbach
29555 Feuerbach 2 Back to conditions
LIKE
“%” represents an arbitrary string
(including length 0)
There is also “_” which represents
LIKE is used for regular expressions a single arbitrary character

Students with names that start with “F”

student SELECT name, studid


studid name semester FROM student
24002 Xenokrates 18 WHERE name LIKE ‘F%’;
25403 Jonas 12
26120 Fichte 10
26830 Aristoxenos 8 studid name
27550 Schopenhauer 6 26120 Fichte
28106 Carnap 3 29555 Feuerbach
29120 Theophrastos 2
29555 Feuerbach 2 Back to conditions
Intersect and Except
(SELECT * FROM A)
x
INTERSECT 2
(SELECT * FROM B); 3
A x B x
1 2
2 3
3 4 (SELECT * FROM A)
x
EXCEPT
1
(SELECT * FROM B);

INTERSECT and EXCEPT can also be Set operators in combination with ALL are
combined with ALL (analogous to UNION) not supported by all DBMSs

Back to set operations


The WITH statement course courseid title ects taughtby
5001 Basics 4 2137
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126
Creates a temporary table in a query 5049 DBS 2 2125
4052 Logics 4 2125
5052 Theory of Science 3 2126
WITH teachingLoad AS( 5216 Bioethics 2 2128
SELECT taughtby AS pid, SUM(ects) AS load 5259 Advanced Algorithms 2 2133
FROM course c 5022 Belief and Knowledge 2 2134
GROUP BY taughtby 4630 Constructive Criticism 4 2137
) teachingLoad pid load
SELECT pid 2125 10
FROM teachingLoad 2126 6
WHERE load = (SELECT MAX(load) 2128 2
FROM teachingLoad); 2133 2
2134 2
2137 8

Result pid
2125
Back to advanced SQL
The CASE construct

Output or computation of a value in dependence on the evaluation of a


predicate
Can be used in SELECT and WHERE clauses
CASE
WHEN predicate1 THEN expression1
...
WHEN predicaten−1 THEN expressionn−1
[ ELSE expressionn ]
END
The CASE construct

SELECT studid, ( CASE WHEN grade < 1.5 THEN 'excellent'


WHEN grade < 2.5 THEN 'good'
WHEN grade < 3.5 THEN 'satisfactory'
WHEN grade <= 4.0 THEN 'sufficient'
ELSE 'insufficient' END)
FROM grades;

The first qualifying WHEN clause is executed

Back to advanced SQL


Evaluation in presence of null values

Arithmetic expressions: null values are “propagated”


null + 1  null
null * 0  null

Comparison operators
SQL has a three-valued logic: true, false, and unknown
If at least one argument is null, then the result is unknown
semester < 12  unknown whenever semester is null
Evaluation of logical expressions
NOT
true false
unknown unknown
false true

AND true unknown false


true true unknown false
unknown unknown unknown false
false false false false

OR true unknown false


true true true false
unknown true unknown unknown
false true unknown false
Back to null values
DQL – Recursion in SQL

WITH RECURSIVE mytable(number) AS (


VALUES(1) Non-recursive part
UNION
SELECT number + 1
FROM mytable It may reference
Recursive part
WHERE number < 100 mytable
)
SELECT sum(number)
Main query
FROM mytable;

Back to recursion
SQL 2003: window function
SELECT * FROM (
SELECT ROW NUMBER() OVER (ORDER BY semester DESC) AS
number, studid, name, semester
FROM student
) AS studTmp
WHERE number <= 5;

SELECT * FROM (
SELECT RANK() OVER (ORDER BY semester DESC) AS number,
studid, name, semester
FROM student
) AS studTmp
Back to advanced SQL
WHERE number <= 5;
Other non standard syntax

SELECT * FROM student Supported by Oracle


WHERE ROWNUM <= 5;

SELECT TOP 5 FROM student; Supported by MS SQL server

Back to limit

You might also like