/
Assignment06JRicciardi.sql
455 lines (390 loc) · 13.4 KB
/
Assignment06JRicciardi.sql
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
--*************************************************************************--
-- Title: Assignment06
-- Author: JayRicciardi
-- Desc: This file demonstrates how to use Views
-- Change Log: When,Who,What
-- 2020-08-17,JayRicciardi,Created File
--**************************************************************************--
Begin Try
Use Master;
If Exists(Select Name From SysDatabases Where Name = 'Assignment06DB_JayRicciardi')
Begin
Alter Database [Assignment06DB_JayRicciardi] set Single_user With Rollback Immediate;
Drop Database Assignment06DB_JayRicciardi;
End
Create Database Assignment06DB_JayRicciardi;
End Try
Begin Catch
Print Error_Number();
End Catch
go
Use Assignment06DB_JayRicciardi;
-- Create Tables (Module 01)--
Create Table Categories
([CategoryID] [int] IDENTITY(1,1) NOT NULL
,[CategoryName] [nvarchar](100) NOT NULL
);
go
Create Table Products
([ProductID] [int] IDENTITY(1,1) NOT NULL
,[ProductName] [nvarchar](100) NOT NULL
,[CategoryID] [int] NULL
,[UnitPrice] [mOney] NOT NULL
);
go
Create Table Employees -- New Table
([EmployeeID] [int] IDENTITY(1,1) NOT NULL
,[EmployeeFirstName] [nvarchar](100) NOT NULL
,[EmployeeLastName] [nvarchar](100) NOT NULL
,[ManagerID] [int] NULL
);
go
Create Table Inventories
([InventoryID] [int] IDENTITY(1,1) NOT NULL
,[InventoryDate] [Date] NOT NULL
,[EmployeeID] [int] NOT NULL -- New Column
,[ProductID] [int] NOT NULL
,[Count] [int] NOT NULL
);
go
-- Add Constraints (Module 02) --
Begin -- Categories
Alter Table Categories
Add Constraint pkCategories
Primary Key (CategoryId);
Alter Table Categories
Add Constraint ukCategories
Unique (CategoryName);
End
go
Begin -- Products
Alter Table Products
Add Constraint pkProducts
Primary Key (ProductId);
Alter Table Products
Add Constraint ukProducts
Unique (ProductName);
Alter Table Products
Add Constraint fkProductsToCategories
Foreign Key (CategoryId) References Categories(CategoryId);
Alter Table Products
Add Constraint ckProductUnitPriceZeroOrHigher
Check (UnitPrice >= 0);
End
go
Begin -- Employees
Alter Table Employees
Add Constraint pkEmployees
Primary Key (EmployeeId);
Alter Table Employees
Add Constraint fkEmployeesToEmployeesManager
Foreign Key (ManagerId) References Employees(EmployeeId);
End
go
Begin -- Inventories
Alter Table Inventories
Add Constraint pkInventories
Primary Key (InventoryId);
Alter Table Inventories
Add Constraint dfInventoryDate
Default GetDate() For InventoryDate;
Alter Table Inventories
Add Constraint fkInventoriesToProducts
Foreign Key (ProductId) References Products(ProductId);
Alter Table Inventories
Add Constraint ckInventoryCountZeroOrHigher
Check ([Count] >= 0);
Alter Table Inventories
Add Constraint fkInventoriesToEmployees
Foreign Key (EmployeeId) References Employees(EmployeeId);
End
go
-- Adding Data (Module 04) --
Insert Into Categories
(CategoryName)
Select CategoryName
From Northwind.dbo.Categories
Order By CategoryID;
go
Insert Into Products
(ProductName, CategoryID, UnitPrice)
Select ProductName,CategoryID, UnitPrice
From Northwind.dbo.Products
Order By ProductID;
go
Insert Into Employees
(EmployeeFirstName, EmployeeLastName, ManagerID)
Select E.FirstName, E.LastName, IsNull(E.ReportsTo, E.EmployeeID)
From Northwind.dbo.Employees as E
Order By E.EmployeeID;
go
Insert Into Inventories
(InventoryDate, EmployeeID, ProductID, [Count])
Select '20170101' as InventoryDate, 5 as EmployeeID, ProductID, ABS(CHECKSUM(NewId())) % 100 as RandomValue
From Northwind.dbo.Products
Union
Select '20170201' as InventoryDate, 7 as EmployeeID, ProductID, ABS(CHECKSUM(NewId())) % 100 as RandomValue
From Northwind.dbo.Products
Union
Select '20170301' as InventoryDate, 9 as EmployeeID, ProductID, ABS(CHECKSUM(NewId())) % 100 as RandomValue
From Northwind.dbo.Products
Order By 1, 2
go
-- Show the Current data in the Categories, Products, and Inventories Tables
Select * From Categories;
go
Select * From Products;
go
Select * From Employees;
go
Select * From Inventories;
go
/********************************* Questions and Answers *********************************/
'NOTES------------------------------------------------------------------------------------
1) You can use any name you like for you views, but be descriptive and consistent
2) You can use your working code from assignment 5 for much of this assignment
3) You must use the BASIC views for each table after they are created in Question 1
------------------------------------------------------------------------------------------'
-- Question 1 (5 pts): How can you create BACIC views to show data from each table in the database.
-- NOTES: 1) Do not use a *, list out each column!
-- 2) Create one view per table!
-- 3) Use SchemaBinding to protect the views from being orphaned!
CREATE VIEW vCategories WITH SCHEMABINDING
AS
SELECT CategoryID, CategoryName
FROM dbo.Categories
GO
CREATE VIEW vEmployees WITH SCHEMABINDING
AS
SELECT EmployeeID, EmployeeFirstName, EmployeeLastName, ManagerID
FROM dbo.Employees
GO
CREATE VIEW vInventories WITH SCHEMABINDING
AS
SELECT InventoryID, InventoryDate, EmployeeID, ProductID, (Count)
FROM dbo.Inventories
GO
CREATE VIEW vProducts WITH SCHEMABINDING
AS
SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM dbo.Products
GO
-- Question 2 (5 pts): How can you set permissions, so that the public group CANNOT select data
-- from each table, but can select data from each view?
DENY SELECT ON Categories TO PUBLIC
DENY SELECT ON Employees TO PUBLIC
DENY SELECT ON Inventories TO PUBLIC
DENY SELECT ON Products TO PUBLIC
GRANT SELECT ON vCategories TO PUBLIC
GRANT SELECT ON vEmployees TO PUBLIC
GRANT SELECT ON vInventories TO PUBLIC
GRANT SELECT ON vProducts TO PUBLIC
GO
-- Question 3 (10 pts): How can you create a view to show a list of Category and Product names,
-- and the price of each product?
-- Order the result by the Category and Product!
CREATE VIEW vProductsbyCategories
AS
SELECT TOP 100 Percent
CategoryName
, ProductName
, UnitPrice
FROM vCategories AS c
JOIN vProducts AS p
ON c.CategoryID = p.CategoryID
ORDER BY CategoryName, ProductName
GO
SELECT * FROM vProductsbyCategories
-- Here is an example of some rows selected from the view:
-- CategoryName,ProductName,UnitPrice
-- Beverages,Chai,18.00
-- Beverages,Chang,19.00
-- Beverages,Chartreuse verte,18.00
-- Question 4 (10 pts): How can you create a view to show a list of Product names
-- and Inventory Counts on each Inventory Date?
-- Order the results by the Product, Date, and Count!
CREATE VIEW vInventoryLogWithProducts
AS
SELECT TOP 100 percent
ProductName
, [Count]
, InventoryDate
FROM vInventories AS i
JOIN vProducts AS p
ON i.ProductID = p.ProductID
ORDER BY ProductName, InventoryDate, [Count]
GO
SELECT * FROM vInventoryLogWithProducts
GO
-- Here is an example of some rows selected from the view:
--ProductName,InventoryDate,Count
--Alice Mutton,2017-01-01,15
--Alice Mutton,2017-02-01,78
--Alice Mutton,2017-03-01,83
-- Question 5 (10 pts): How can you create a view to show a list of Inventory Dates
-- and the Employee that took the count?
-- Order the results by the Date and return only one row per date!
CREATE VIEW vEmployeeInventoryCheck
AS
SELECT TOP 100 Percent
InventoryDate
, [EmployeeName] = e.EmployeeFirstName + ' ' + e.EmployeeLastName
FROM vInventories AS i
JOIN vEmployees AS e
ON i.EmployeeID = e.EmployeeID
GROUP BY InventoryDate, EmployeeFirstName, EmployeeLastName
GO
SELECT * FROM vEmployeeInventoryCheck
GO
-- Here is an example of some rows selected from the view:
-- InventoryDate,EmployeeName
-- 2017-01-01,Steven Buchanan
-- 2017-02-01,Robert King
-- 2017-03-01,Anne Dodsworth
-- Question 6 (10 pts): How can you create a view show a list of Categories, Products,
-- and the Inventory Date and Count of each product?
-- Order the results by the Category, Product, Date, and Count!
CREATE VIEW vProductCategoryInventory
AS
SELECT TOP 100 PERCENT
CategoryName
, ProductName
, InventoryDate
, [Count]
FROM vCategories AS c
JOIN vProducts as p
ON c.CategoryID = p.CategoryID
JOIN Inventories AS i
ON i.ProductID = p.ProductID
ORDER BY CategoryName, ProductName, InventoryDate, [Count]
GO
SELECT * FROM vProductCategoryInventory
GO
-- Select for results from the example below:
SELECT * FROM vProductCategoryInventory WHERE ProductName = 'Chai'
GO
-- Here is an example of some rows selected from the view:
-- CategoryName,ProductName,InventoryDate,Count
-- Beverages,Chai,2017-01-01,72
-- Beverages,Chai,2017-02-01,52
-- Beverages,Chai,2017-03-01,54
-- Question 7 (10 pts): How can you create a view to show a list of Categories, Products,
-- the Inventory Date and Count of each product, and the EMPLOYEE who took the count?
-- Order the results by the Inventory Date, Category, Product and Employee!
CREATE VIEW vEmployeeInventoryEvents
AS
SELECT TOP 100 Percent
CategoryName
, ProductName
, InventoryDate
, [Count]
, [EmployeeName] = e.EmployeeFirstName + ' ' + e.EmployeeLastName
FROM vCategories AS c
JOIN vProducts as p
ON c.CategoryID = p.CategoryID
JOIN vInventories AS i
ON i.ProductID = p.ProductID
JOIN vEmployees as e
ON e.EmployeeID = i.EmployeeID
ORDER BY InventoryDate, CategoryName, ProductName, EmployeeFirstName, EmployeeLastName
GO
SELECT * FROM vEmployeeInventoryEvents
GO
-- Here is an example of some rows selected from the view:
-- CategoryName,ProductName,InventoryDate,Count,EmployeeName
-- Beverages,Chai,2017-01-01,72,Steven Buchanan
-- Beverages,Chang,2017-01-01,46,Steven Buchanan
-- Beverages,Chartreuse verte,2017-01-01,61,Steven Buchanan
-- Question 8 (10 pts): How can you create a view to show a list of Categories, Products,
-- the Inventory Date and Count of each product, and the Employee who took the count
-- for the Products 'Chai' and 'Chang'?
CREATE VIEW vExploreChaiChangInventory
AS
SELECT TOP 100 Percent
CategoryName
, ProductName
, InventoryDate
, [Count]
, [EmployeeName] = e.EmployeeFirstName + ' ' + e.EmployeeLastName
FROM vCategories AS c
JOIN vProducts AS p
ON c.CategoryID = p.CategoryID
JOIN vInventories as i
ON i.ProductID = p.ProductID
JOIN vEmployees AS e
ON e.EmployeeID = i.EmployeeID
WHERE ProductName IN (SELECT ProductName FROM Products WHERE ProductName = 'Chai' OR ProductName = 'Chang')
ORDER BY InventoryDate, CategoryName, ProductName
GO
SELECT * FROM vExploreChaiChangInventory
GO
-- Here is an example of some rows selected from the view:
-- CategoryName,ProductName,InventoryDate,Count,EmployeeName
-- Beverages,Chai,2017-01-01,72,Steven Buchanan
-- Beverages,Chang,2017-01-01,46,Steven Buchanan
-- Beverages,Chai,2017-02-01,52,Robert King
-- Question 9 (10 pts): How can you create a view to show a list of Employees and the Manager who manages them?
-- Order the results by the Manager's name!
CREATE VIEW vManagerEmployeeLookup
AS
SELECT TOP 100 Percent
[Manager] = m.EmployeeFirstName + ' ' + m.EmployeeLastName
, [Employee] = e.EmployeeFirstName + ' ' + e.EmployeeLastName
FROM vEmployees AS e
JOIN vEmployees AS m
ON e.ManagerID = m.EmployeeID
ORDER BY 1, 2;
GO
Select * From vManagerEmployeeLookup
GO
-- Here is an example of some rows selected from the view:
-- Manager,Employee
-- Andrew Fuller,Andrew Fuller
-- Andrew Fuller,Janet Leverling
-- Andrew Fuller,Laura Callahan
-- Question 10 (10 pts): How can you create one view to show all the data from all four
-- BASIC Views?
CREATE VIEW vInventoriesByProductsByCategoriesByEmployees
AS
SELECT TOP 100 Percent
c.CategoryID
, c.CategoryName
, p.ProductID
, p.ProductName
, p.UnitPrice
, i.InventoryID
, i.InventoryDate
, i.[Count]
, e.EmployeeID,
[Employee] = e.EmployeeFirstName + ' ' + e.EmployeeLastName,
[Manager] = m.EmployeeFirstName + ' ' + m.EmployeeLastName
FROM vCategories as c INNER JOIN vProducts as p
ON c.CategoryID = p.CategoryID
JOIN vInventories as i
ON p.ProductID = i.ProductID
JOIN vEmployees as e
ON i.EmployeeID = e.EmployeeID
JOIN vEmployees AS m
ON e.ManagerID = m.EmployeeID
GO
Select * From vInventoriesByProductsByCategoriesByEmployees
GO
-- Here is an example of some rows selected from the view:
-- CategoryID,CategoryName,ProductID,ProductName,UnitPrice,InventoryID,InventoryDate,Count,EmployeeID,Employee,Manager
-- 1,Beverages,1,Chai,18.00,1,2017-01-01,72,5,Steven Buchanan,Andrew Fuller
-- 1,Beverages,1,Chai,18.00,78,2017-02-01,52,7,Robert King,Steven Buchanan
-- 1,Beverages,1,Chai,18.00,155,2017-03-01,54,9,Anne Dodsworth,Steven Buchanan
-- Test your Views (NOTE: You must change the names to match yours as needed!)
Select * From [dbo].[vCategories]
Select * From [dbo].[vProducts]
Select * From [dbo].[vInventories]
Select * From [dbo].[vEmployees]
Select * From [dbo].[vProductsByCategories]
Select * From [dbo].[vInventoryLogWithProducts]
Select * From [dbo].[vEmployeeInventoryCheck]
Select * From [dbo].[vProductCategoryInventory]
Select * From [dbo].[vEmployeeInventoryEvents]
Select * From [dbo].[vExploreChaiChangInventory]
Select * From [dbo].[vManagerEmployeeLookup]
Select * From [dbo].[vInventoriesByProductsByCategoriesByEmployees]
/***************************************************************************************/