-
Notifications
You must be signed in to change notification settings - Fork 1
/
SQL07-View
169 lines (147 loc) · 6.84 KB
/
SQL07-View
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* EXPLORING VIEW POSSIBILITIES */
-- Query --
select
upper(name) as 'Name',
sex as 'Sex',
upper(email) as 'Email',
ifnull(kind,'NO EMAIL') as 'Kind',
ifnull(number,'NO PHONE') as 'Number',
upper(city) as 'City',
country as 'Country'
from client
left join phone P
on idClient = P.id_Client
left join adress A
on idClient = A.id_Client;
+------+-----+----------------+----------+-------------+------------+---------+
| Name | Sex | E-mail | Kind | Number | City | Country |
+------+-----+----------------+----------+-------------+------------+---------+
| JOHN | M | [email protected] | Mobile | 111111111 | CALIFORNIA | USA |
| JOHN | M | [email protected] | House | 22222222222 | CALIFORNIA | USA |
| JOHN | M | [email protected] | Office | 9999999 | CALIFORNIA | USA |
| MARY | F | [email protected] | House | 194943 | CALIFORNIA | USA |
| DAVE | M | [email protected] | Other | 109499 | CALIFORNIA | USA |
| RYAN | M | [email protected] | Mobile | 954973 | CALIFORNIA | USA |
| RYAN | M | [email protected] | House | 318494849 | CALIFORNIA | USA |
| CATY | F | [email protected] | Office | 166194 | CALIFORNIA | USA |
| BETY | F | [email protected] | House | 64161 | CALIFORNIA | USA |
| KELY | M | [email protected] | Other | 1797821 | CALIFORNIA | USA |
| NOAH | M | [email protected] | NO EMAIL | NO PHONE | CALIFORNIA | USA |
| TONY | M | [email protected] | NO EMAIL | NO PHONE | CALIFORNIA | USA |
+------+-----+----------------+----------+-------------+------------+---------+
-- no need to creat or copy/paste query --
CREATE VIEW V_Data
AS
select
upper(name) as 'Name',
sex as 'Sex',
upper(email) as 'Email',
ifnull(kind,'NO EMAIL') as 'Kind',
ifnull(number,'NO PHONE') as 'Number',
upper(city) as 'City',
country as 'Country'
from client
left join phone P
on idClient = P.id_Client
left join adress A
on idClient = A.id_Client;
show tables;
+-------------------------+
| Tables_in_accountsystem |
+-------------------------+
| adress |
| client |
| phone |
| v_data |
+-------------------------+
desc V_Data;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| Name | varchar(30) | YES | | NULL | |
| Sex | enum('M','F') | NO | | NULL | |
| E-mail | varchar(30) | YES | | NULL | |
| Kind | varchar(8) | NO | | | |
| Number | varchar(15) | NO | | | |
| City | varchar(30) | YES | | NULL | |
| Country | varchar(30) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
select * from V_Data;
+------+-----+----------------+----------+-------------+------------+---------+
| Name | Sex | E-mail | Kind | Number | City | Country |
+------+-----+----------------+----------+-------------+------------+---------+
| JOHN | M | [email protected] | Mobile | 111111111 | CALIFORNIA | USA |
| JOHN | M | [email protected] | House | 22222222222 | CALIFORNIA | USA |
| JOHN | M | [email protected] | Office | 9999999 | CALIFORNIA | USA |
| MARY | F | [email protected] | House | 194943 | CALIFORNIA | USA |
| DAVE | M | [email protected] | Other | 109499 | CALIFORNIA | USA |
| RYAN | M | [email protected] | Mobile | 954973 | CALIFORNIA | USA |
| RYAN | M | [email protected] | House | 318494849 | CALIFORNIA | USA |
| CATY | F | [email protected] | Office | 166194 | CALIFORNIA | USA |
| BETY | F | [email protected] | House | 64161 | CALIFORNIA | USA |
| KELY | M | [email protected] | Other | 1797821 | CALIFORNIA | USA |
| NOAH | M | [email protected] | NO EMAIL | NO PHONE | CALIFORNIA | USA |
| TONY | M | [email protected] | NO EMAIL | NO PHONE | CALIFORNIA | USA |
+------+-----+----------------+----------+-------------+------------+---------+
-- Delete View --
drop view V_Data;
show tables;
+-------------------------+
| Tables_in_accountsystem |
+-------------------------+
| adress |
| client |
| phone |
+-------------------------+
-- Recreating View --
CREATE VIEW V_Data
AS
select
upper(name) as 'Name',
sex as 'Sex',
upper(email) as 'Email',
ifnull(kind,'NO EMAIL') as 'Kind',
ifnull(number,'NO PHONE') as 'Number',
upper(city) as 'City',
country as 'Country'
from client
left join phone P
on idClient = P.id_Client
left join adress A
on idClient = A.id_Client;
-- selecting from view --
select Name, Email, Kind, Number, LOCALTIME() as 'Time of ETL'
from V_Data
where kind != 'NO EMAIL';
+------+----------------+--------+-------------+---------------------+
| Name | Email | Kind | Number | Time of ETL |
+------+----------------+--------+-------------+---------------------+
| JOHN | [email protected] | Mobile | 111111111 | 2022-01-10 19:19:52 |
| JOHN | [email protected] | House | 22222222222 | 2022-01-10 19:19:52 |
| JOHN | [email protected] | Office | 9999999 | 2022-01-10 19:19:52 |
| MARY | [email protected] | House | 194943 | 2022-01-10 19:19:52 |
| DAVE | [email protected] | Other | 109499 | 2022-01-10 19:19:52 |
| RYAN | [email protected] | Mobile | 954973 | 2022-01-10 19:19:52 |
| RYAN | [email protected] | House | 318494849 | 2022-01-10 19:19:52 |
| CATY | [email protected] | Office | 166194 | 2022-01-10 19:19:52 |
| BETY | [email protected] | House | 64161 | 2022-01-10 19:19:52 |
| KELY | [email protected] | Other | 1797821 | 2022-01-10 19:19:52 |
+------+----------------+--------+-------------+---------------------+
select Name, Email, Kind, Number, LOCALTIME() as 'Time of ETL'
from V_Data
where kind != 'NO EMAIL'
order by Name ASC;
+------+----------------+--------+-------------+---------------------+
| Name | Email | Kind | Number | Time of ETL |
+------+----------------+--------+-------------+---------------------+
| BETY | [email protected] | House | 64161 | 2022-01-10 20:37:06 |
| CATY | [email protected] | Office | 166194 | 2022-01-10 20:37:06 |
| DAVE | [email protected] | Other | 109499 | 2022-01-10 20:37:06 |
| JOHN | [email protected] | Mobile | 111111111 | 2022-01-10 20:37:06 |
| JOHN | [email protected] | House | 22222222222 | 2022-01-10 20:37:06 |
| JOHN | [email protected] | Office | 9999999 | 2022-01-10 20:37:06 |
| KELY | [email protected] | Other | 1797821 | 2022-01-10 20:37:06 |
| MARY | [email protected] | House | 194943 | 2022-01-10 20:37:06 |
| RYAN | [email protected] | Mobile | 954973 | 2022-01-10 20:37:06 |
| RYAN | [email protected] | House | 318494849 | 2022-01-10 20:37:06 |
+------+----------------+--------+-------------+---------------------+