-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab4_1.sql
112 lines (99 loc) · 2 KB
/
Lab4_1.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
/*
Variant #9
group 651003
Kornienko Anastasia
*/
USE AdventureWorks2012;
GO
--point a)
-- char(6) for action because the maximum word length in data set is 6.
CREATE TABLE Sales.SpecialOfferHst (
ID INT IDENTITY(1, 1) PRIMARY KEY,
Action CHAR(6) NOT NULL CHECK(Action IN('insert', 'update', 'delete')),
ModifiedDate DATETIME NOT NULL,
SourceID INT NOT NULL,
UserName VARCHAR(50) NOT NULL
);
SELECT * FROM Sales.SpecialOfferHst
GO
--point b)
CREATE TRIGGER Sales.[SpecialOffer.InsertTrigger]
ON Sales.SpecialOffer
AFTER INSERT AS
INSERT INTO Sales.SpecialOfferHst(
Action,
ModifiedDate,
SourceID,
UserName
)
SELECT 'insert',
GETDATE(),
inserted.SpecialOfferID,
USER_NAME()
FROM inserted;
GO
CREATE TRIGGER Sales.[SpecialOffer.UpdateTrigger]
ON Sales.SpecialOffer
AFTER UPDATE AS
INSERT INTO Sales.SpecialOfferHst(
Action,
ModifiedDate,
SourceID,
UserName
)
SELECT 'update',
GETDATE(),
inserted.SpecialOfferID,
USER_NAME()
FROM inserted;
GO
CREATE TRIGGER Sales.[SpecialOffer.DeleteTrigger]
ON Sales.SpecialOffer
AFTER DELETE AS
INSERT INTO Sales.SpecialOfferHst(
Action,
ModifiedDate,
SourceID,
UserName
)
SELECT 'delete',
GETDATE(),
deleted.SpecialOfferID,
USER_NAME()
FROM deleted;
GO
--pont c)
CREATE VIEW Sales.SpecialOfferView
WITH ENCRYPTION
AS SELECT * FROM Sales.SpecialOffer;
GO
SELECT * from Sales.SpecialOffer;
SELECT * from Sales.SpecialOfferView;
--point d)
INSERT INTO Sales.SpecialOfferView (
Description,
DiscountPct,
Type,
Category,
StartDate,
EndDate
)
VALUES (
'Description',
0.15,
'Seasonal Discount',
'Reseller',
GETDATE(),
GETDATE()
);
UPDATE Sales.SpecialOfferView
SET Category = 'Customer'
WHERE SpecialOfferID = (
SELECT MAX(SpecialOfferID) FROM Sales.SpecialOfferView
);
DELETE Sales.SpecialOfferView
WHERE SpecialOfferID = (
SELECT MAX(SpecialOfferID) FROM Sales.SpecialOfferView
);
SELECT * FROM Sales.SpecialOfferHst;
GO