-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.sql
More file actions
74 lines (71 loc) · 1.5 KB
/
views.sql
File metadata and controls
74 lines (71 loc) · 1.5 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
use library;
-- VIEWS
CREATE VIEW author_view as
select a.author_id,
a.first_name,
a.last_name,
a.birth,
a.death,
a.nobel,
n.nation,
count(b.book_id) as 'Number of books'
from author as a
join nationality as n on a.nationality_id = n.nationality_id
join book as b on a.author_id = b.author_id
group by a.author_id
order by count(b.book_id) desc;
select * from author_view;
CREATE VIEW book_view_full AS
SELECT
b.book_id,
b.title,
b.year,
b.isbn,
b.pages,
b.available,
i.count,
g.genre,
b.description,
a.author_id,
a.last_name,
p.publisher_id,
p.publisher
FROM
book AS b
JOIN
author AS a ON b.author_id = a.author_id
JOIN
publisher AS p ON b.publisher_id = p.publisher_id
JOIN
inventory AS i ON b.book_id = i.book_id
JOIN
book_genre AS bg ON b.book_id = bg.book_id
JOIN
genre AS g ON bg.genre_id = g.genre_id;
select * from book_view_full;
create view book_view_short as
select
b.title,
b.isbn,
b.pages,
b.available,
a.first_name,
a.last_name
from book as b
join author as a
on b.author_id = a.author_id;
create view user_rental_view as
select
u.user_id,
u.username,
u.password,
u.first_name,
u.last_name,
u.email,
u.telephone,
u.active,
coalesce(count(r.rental_id), 'never rent any') as 'Number of rentals total'
from user as u
join rental as r
on u.user_id = r.user_id
group by u.user_id;