-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_Views.sql
More file actions
122 lines (110 loc) · 3.2 KB
/
08_Views.sql
File metadata and controls
122 lines (110 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**************************************************************
* MySQL 8.0 Views Tutorial
* Description: This script demonstrates creating simple views,
* join views, aggregate views, and JSON-producing
* views in MySQL.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
USE mysql_course;
DROP TABLE IF EXISTS animal_details;
CREATE TABLE animal_details
(
detail_id INT PRIMARY KEY AUTO_INCREMENT,
animal_name VARCHAR(60) NOT NULL,
detail_text VARCHAR(255) NOT NULL
) ENGINE = InnoDB;
INSERT INTO animal_details (animal_name, detail_text)
VALUES
('Leo', 'Large enclosure near the east wing.'),
('Polly', 'Requires daily enrichment activities.'),
('Nemo', 'Tank temperature monitored every hour.');
DROP VIEW IF EXISTS vw_animal_names;
DROP VIEW IF EXISTS vw_animal_details;
DROP VIEW IF EXISTS vw_animal_count_by_type;
DROP VIEW IF EXISTS vw_animal_json;
DROP VIEW IF EXISTS vw_animal_age_category;
-------------------------------------------------
-- Region: 1. Simple View
-------------------------------------------------
/*
1.1 View of animal names.
*/
CREATE VIEW vw_animal_names AS
SELECT name
FROM animals;
-------------------------------------------------
-- Region: 2. Join View
-------------------------------------------------
/*
2.1 Join animals with supporting detail rows.
*/
CREATE VIEW vw_animal_details AS
SELECT
a.animal_id,
a.name,
a.animal_type,
a.age,
d.detail_text
FROM animals AS a
LEFT JOIN animal_details AS d
ON d.animal_name = a.name;
-------------------------------------------------
-- Region: 3. Aggregate View
-------------------------------------------------
/*
3.1 Count animals by type.
*/
CREATE VIEW vw_animal_count_by_type AS
SELECT
animal_type,
COUNT(*) AS animal_count,
AVG(age) AS average_age
FROM animals
GROUP BY animal_type;
-------------------------------------------------
-- Region: 4. JSON View
-------------------------------------------------
/*
4.1 Create a JSON representation per animal row.
*/
CREATE VIEW vw_animal_json AS
SELECT
animal_id,
name,
JSON_OBJECT(
'animal_id', animal_id,
'name', name,
'animal_type', animal_type,
'age', age
) AS animal_json
FROM animals;
-------------------------------------------------
-- Region: 5. Computed View
-------------------------------------------------
/*
5.1 Categorize animals by age.
*/
CREATE VIEW vw_animal_age_category AS
SELECT
name,
age,
CASE
WHEN age < 1 THEN 'Infant'
WHEN age BETWEEN 1 AND 3 THEN 'Young'
WHEN age BETWEEN 4 AND 7 THEN 'Adult'
ELSE 'Senior'
END AS age_category
FROM animals;
-------------------------------------------------
-- Region: 6. Example Queries
-------------------------------------------------
SELECT * FROM vw_animal_names;
SELECT * FROM vw_animal_details;
SELECT * FROM vw_animal_count_by_type;
SELECT * FROM vw_animal_json;
SELECT * FROM vw_animal_age_category;
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------