forked from Ankit-Kum/DBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMS_10.sql
More file actions
343 lines (261 loc) · 7.82 KB
/
Copy pathDBMS_10.sql
File metadata and controls
343 lines (261 loc) · 7.82 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
CREATE VIEW
CREATE [OR REPLACE] VIEW [db_name.]view_name [(column_list)]
AS
select-statement;
----------------------------------------------
SELECT
customerName,
checkNumber,
paymentDate,
amount
FROM
customers
INNER JOIN
payments USING (customerNumber);
-- Next time, if you want to get the same information including customer name, check number, payment date, and amount, you need to issue the same query again.
-- One way to do this is to save the query in a file, either .txt or .sql file so that later you can open and execute it from MySQL Workbench or any other MySQL client tools.
-- A better way to do this is to save the query in the database server and assign a name to it. This named query is called a database view, or simply, view.
CREATE VIEW customerPayments
AS
SELECT
customerName,
checkNumber,
paymentDate,
amount
FROM
customers
INNER JOIN
payments USING (customerNumber);
SELECT * FROM customerPayments;
CREATE VIEW daysofweek (day) AS
SELECT 'Mon'
UNION
SELECT 'Tue'
UNION
SELECT 'Web'
UNION
SELECT 'Thu'
UNION
SELECT 'Fri'
UNION
SELECT 'Sat'
UNION
SELECT 'Sun';
SELECT * FROM daysofweek;
# Advantages of MySQL Views
-- 1) Simplify complex query
-- 2) Make the business logic consistent
-- 3) Add extra security layers
-- 4) Enable backward compatibility
/*Suppose, you want to normalize a big table into many smaller ones. And you don’t want to impact the current applications that reference the table.
In this case, you can create a view whose name is the same as the table based on the new tables so that all applications can reference the view as if it were a table.*/
# simple view example
CREATE VIEW salePerOrder AS
SELECT
orderNumber,
SUM(quantityOrdered * priceEach) total
FROM
orderDetails
GROUP by orderNumber
ORDER BY total DESC;
-- If you use the SHOW TABLE command to view all tables in the classicmodels database, you will see the viewsalesPerOrder is showing up in the list.
SHOW TABLES;
SHOW FULL TABLES;
SELECT * FROM salePerOrder;
# view based on another view example
CREATE VIEW bigSalesOrder AS
SELECT
orderNumber,
ROUND(total,2) as total
FROM
salePerOrder
WHERE
total > 60000;
SELECT
orderNumber,
total
FROM
bigSalesOrder;
## Creating a view with join example
CREATE OR REPLACE VIEW customerOrders AS
SELECT
orderNumber,
customerName,
SUM(quantityOrdered * priceEach) total
FROM
orderDetails
INNER JOIN orders o USING (orderNumber)
INNER JOIN customers USING (customerNumber)
GROUP BY orderNumber;
SELECT * FROM customerOrders
ORDER BY total DESC;
## Creating a view with a subquery example
CREATE VIEW aboveAvgProducts AS
SELECT
productCode,
productName,
buyPrice
FROM
products
WHERE
buyPrice > (
SELECT
AVG(buyPrice)
FROM
products)
ORDER BY buyPrice DESC;
SELECT * FROM aboveAvgProducts;
# updatable view example
-- First, create a view named officeInfo based on the offices table
CREATE VIEW officeInfo
AS
SELECT officeCode, phone, city
FROM offices;
SELECT * FROM officeInfo;
-- Update
UPDATE officeInfo
SET
phone = '+33 14 723 5555'
WHERE
officeCode = 4;
------------------------------------------
SELECT
*
FROM
officeInfo
WHERE
officeCode = 4;
-- Removing rows through the view
-- create a new table named items
CREATE TABLE items (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(11 , 2 ) NOT NULL
);
-- insert data into the items table
INSERT INTO items(name,price)
VALUES('Laptop',700.56),('Desktop',699.99),('iPad',700.50) ;
-- create a view based on items table
CREATE VIEW LuxuryItems AS
SELECT
*
FROM
items
WHERE
price > 700;
-- query data from the LuxuryItems view
SELECT
*
FROM
LuxuryItems;
-----------------------------
DELETE FROM LuxuryItems
WHERE id = 3;
-----------------------------------------
SELECT
*
FROM
LuxuryItems;
----------------------------------
SELECT
*
FROM
items;
-- info about view
SHOW CREATE VIEW LuxuryItems;
-- DROP VIEW IF EXISTS LuxuryItems;
DROP VIEW LuxuryItems;
-- Rename View
RENAME TABLE original_view_name
TO new_view_name;
RENAME TABLE LuxuryItems
TO LuxuryItems2;
-- check if the view has been renamed successfully:
SHOW FULL TABLES WHERE table_type = 'VIEW';
# Show View
-- MySQL treats the views as tables with the type 'VIEW'. Therefore, you can use the SHOW FULL TABLES statement to display all views in the current database
SHOW FULL TABLES
WHERE table_type = 'VIEW';
-- If you want to show all views in a specific database,
SHOW FULL TABLES
[{FROM | IN } database_name]
WHERE table_type = 'VIEW';
SHOW FULL TABLES IN classicmodels
WHERE table_type='VIEW';
-- Show View – Using INFORMATION_SCHEMA database
/*The information_schema database provides access to MySQL database metadata such as databases, tables, data types of columns, or privileges.
The information schema is also known as a database dictionary or system catalog*/
SELECT *
FROM information_schema.tables;
# WITH CHECK OPTION clause
/*Sometimes, you create a view to reveal the partial data of a table. However, a simple view is updatable,
and therefore, it is possible to update data that is not visible through the view.
This update makes the view inconsistent.*/
-- To ensure the consistency of the view, you use the WITH CHECK OPTION clause
/*The WITH CHECK OPTION is an optional clause of the CREATE VIEW statement.
This WITH CHECK OPTION prevents you from updating or
inserting rows that are not visible through the view.*/
CREATE OR REPLACE VIEW view_name
AS
select_statement
WITH CHECK OPTION;
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE employees(
id INT AUTO_INCREMENT PRIMARY KEY,
type VARCHAR(50) NOT NULL,
name VARCHAR(255) NOT NULL
);
INSERT INTO employees (type, name)
VALUES
('Full-time', 'John Doe'),
('Contractor', 'Jane Smith'),
('Temp', 'Alice Johnson'),
('Full-time', 'Bob Anderson'),
('Contractor', 'Charlie Brown'),
('Temp', 'David Lee'),
('Full-time', 'Eva Martinez'),
('Contractor', 'Frank White'),
('Temp', 'Grace Taylor'),
('Full-time', 'Henry Walker'),
('Contractor', 'Ivy Davis'),
('Temp', 'Jack Turner'),
('Full-time', 'Kelly Harris'),
('Contractor', 'Leo Wilson'),
('Temp', 'Mia Rodriguez'),
('Full-time', 'Nick Carter'),
('Contractor', 'Olivia Clark'),
('Temp', 'Pauline Hall'),
('Full-time', 'Quincy Adams');
SELECT * FROM employees;
------------------------------------
CREATE OR REPLACE VIEW contractors
AS
SELECT id, type, name
FROM
employees
WHERE
type = 'Contractor';
-------------------------------------
SELECT * FROM contractors;
INSERT INTO contractors(name, type)
VALUES('Andy Black', 'Contractor');
-- Andy Black has been added successfully.
-- The problem is that you can add an employee
-- with other types such as Full-time into the employees table via the contractors view.
-- For example:
INSERT INTO contractors(name, type)
VALUES('Deric Seetoh', 'Full-time');
--
-- To prevent this, you need to add the WITH CHECK OPTION clause to the CREATE OR REPLACE VIEW statement like this:
CREATE OR REPLACE VIEW contractors
AS
SELECT id, type, name
FROM
employees
WHERE
type = 'Contractor'
WITH CHECK OPTION;
INSERT INTO contractors(name, type)
VALUES('Brad Knox', 'Full-time');
--------------------------------------