Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Introduction to

Databases with SQL


Viewing
authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


authors authored books
name id author_id book_id id title

Eva Baltasar 23 23 1 1 Boulder

Han Kang 31 31 74 74 The White Book

Gauz 27 27 4 4 Standing Heavy

Olga Tokarczuk 58 58 78 78 Flights


name id author_id book_id id title

Eva Baltasar 23 23 1 1 Boulder

Han Kang 31 31 74 74 The White Book

Gauz 27 27 4 4 Standing Heavy

Olga Tokarczuk 58 58 78 78 Flights


name title

Eva Baltasar Boulder

Han Kang The White Book

Gauz Standing Heavy

Olga Tokarczuk Flights


name title

Eva Baltasar Boulder

Han Kang The White Book

Gauz Standing Heavy

Olga Tokarczuk Flights


View
A virtual table de ned by a query
fi
authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


name title

Eva Baltasar Boulder

Han Kang The White Book

Gauz Standing Heavy

Olga Tokarczuk Flights


Simplifying
Aggregating
Partitioning
Securing

Simplifying
authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


name title

Eva Baltasar Boulder

Han Kang The White Book

Gauz Standing Heavy

Olga Tokarczuk Flights


authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


authors authored books
id name author_id book_id id title

23 Eva Baltasar 23 1 1 Boulder

31 Han Kang 31 74 74 The White Book

27 Gauz 27 4 4 Standing Heavy

58 Olga Tokarczuk 58 78 78 Flights


authors authored books
name id author_id book_id id title

Eva Baltasar 23 23 1 1 Boulder

Han Kang 31 31 74 74 The White Book

Gauz 27 27 4 4 Standing Heavy

Olga Tokarczuk 58 58 78 78 Flights


name id author_id book_id id title

Eva Baltasar 23 23 1 1 Boulder

Han Kang 31 31 74 74 The White Book

Gauz 27 27 4 4 Standing Heavy

Olga Tokarczuk 58 58 78 78 Flights


name title

Eva Baltasar Boulder

Han Kang The White Book

Gauz Standing Heavy

Olga Tokarczuk Flights


SELECT …;
CREATE VIEW name AS
SELECT …;
Aggregating
book_id rating

1 4
1 3
1 4
2 2
2 3
book_id rating

1 4
1 3
1 4
2 2
2 3
book_id rating

1 4
3
2 2
3
book_id rating

1 3.67
2 2.5
CREATE VIEW …
CREATE TEMPORARY VIEW …
CTE
Common Table Expression
WITH name AS (
SELECT …
), …
SELECT … FROM name;
Partitioning
books
id title year

1 Boulder 2023

2 Whale 2023

14 Paradais 2022

23 After the Sun 2022

34 Minor Detail 2021

39 The Perfect Nine 2021


books
id title year

1 Boulder 2023

2 Whale 2023

14 Paradais 2022

23 After the Sun 2022

34 Minor Detail 2021

39 The Perfect Nine 2021


books
id title year

1 Boulder 2023

2 Whale 2023

14 Paradais 2022

23 After the Sun 2022

34 Minor Detail 2021

39 The Perfect Nine 2021


books
id title year

1 Boulder 2023

2 Whale 2023

14 Paradais 2022

23 After the Sun 2022

34 Minor Detail 2021

39 The Perfect Nine 2021


Securing
rides
id origin destination rider

1 Good Egg Galaxy Honeyhive Galaxy Peach

2 Castle Courtyard Cascade Kingdom Mario

3 Metro Kingdom Mushroom Kingdom Luigi

4 Seaside Kingdom Deep Woods Bowser


rides
id origin destination

1 Good Egg Galaxy Honeyhive Galaxy

2 Castle Courtyard Cascade Kingdom

3 Metro Kingdom Mushroom Kingdom

4 Seaside Kingdom Deep Woods


Soft Deletions
collections
id title deleted

1 Farmers working … 0

2 Imaginative land… 0

3 Profusion of … 0

4 Peonies and … 0
collections
id title deleted

1 Farmers working … 1

2 Imaginative land… 0

3 Profusion of … 0

4 Peonies and … 0
SELECT * FROM "collections"
WHERE "deleted" = 0;
collections current_collections
id title deleted id title

1 Farmers working … 0 1 Farmers working …

2 Imaginative land… 0 2 Imaginative land…

3 Profusion of … 0 3 Profusion of …

4 Peonies and … 0 4 Peonies and …


collections current_collections
id title deleted id title

1 Farmers working … 0 1 Farmers working …

2 Imaginative land… 0 2 Imaginative land…

3 Profusion of … 0 3 Profusion of …

4 Peonies and … 0 4 Peonies and …


collections current_collections
id title deleted id title

1 Farmers working … 1 2 Imaginative land…

2 Imaginative land… 0 3 Profusion of …

3 Profusion of … 0 4 Peonies and …

4 Peonies and … 0
CREATE TRIGGER name
CREATE TRIGGER name
INSTEAD OF DELETE ON view
CREATE TRIGGER name
INSTEAD OF DELETE ON view
FOR EACH ROW
CREATE TRIGGER name
INSTEAD OF DELETE ON view
FOR EACH ROW
BEGIN
CREATE TRIGGER name
INSTEAD OF DELETE ON view
FOR EACH ROW
BEGIN
…;
CREATE TRIGGER name
INSTEAD OF DELETE ON view
FOR EACH ROW
BEGIN
…;
END;
CREATE TRIGGER name
INSTEAD OF INSERT ON view
FOR EACH ROW
BEGIN
…;
END;
CREATE TRIGGER name
INSTEAD OF INSERT ON view
FOR EACH ROW WHEN condition
BEGIN
…;
END;
collections current_collections
id title deleted id title

1 Farmers working … 1 2 Imaginative land…

2 Imaginative land… 0 3 Profusion of …

3 Profusion of … 0 4 Peonies and …

4 Peonies and … 0
collections current_collections
id title deleted id title

1 Farmers working … 1 2 Imaginative land…

2 Imaginative land… 0 3 Profusion of …

3 Profusion of … 0 4 Peonies and …

4 Peonies and … 0
collections current_collections
id title deleted id title

1 Farmers working … 0 1 Farmers working …

2 Imaginative land… 0 2 Imaginative land…

3 Profusion of … 0 3 Profusion of …

4 Peonies and … 0 4 Peonies and …


Introduction to
Databases with SQL
Viewing

You might also like