/
case study 11 - checks.sql
257 lines (202 loc) · 7.44 KB
/
case study 11 - checks.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
CREATE FUNCTION string$checkEntered
(
@value varchar(8000) --longest varchar value
)
RETURNS bit
AS
--used to check to see if a varchar value passed in is empty
--note: additional function required for unicode if desired
BEGIN
DECLARE @returnVal bit --just returns yes or no
--do an RTRIM, COALESCED to a '' if it is NULL, surround by *, and compare
IF ( '*' + COALESCE(RTRIM(@value),'') + '*') <> '**'
SET @returnVal = 1 --not empty
ELSE
SET @returnVal = 0 --empty
RETURN @returnVal
END
GO
-- empty condition
IF dbo.string$checkEntered('') = 0
SELECT 'Empty'
ELSE
SELECT 'Not Empty'
-- not empty condition
IF dbo.string$checkEntered('Any text will do') = 0
SELECT 'Empty'
ELSE
SELECT 'Not Empty'
GO
ALTER TABLE account
ADD CONSTRAINT chk$account$number$string$notEmpty
Check (dbo.string$checkEntered(number) = 1)
ALTER TABLE address
ADD CONSTRAINT chk$address$addressLine$string$notEmpty
Check (dbo.string$checkEntered(addressLine) = 1)
ALTER TABLE addressType
ADD CONSTRAINT chk$addressType$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE addressType
ADD CONSTRAINT chk$addressType$description$string$notEmpty
Check (dbo.string$checkEntered(description) = 1)
ALTER TABLE bank
ADD CONSTRAINT chk$bank$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE city
ADD CONSTRAINT chk$city$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE payee
ADD CONSTRAINT chk$payee$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE phoneNumber
ADD CONSTRAINT chk$phoneNumber$countryCode$string$notEmpty
Check (dbo.string$checkEntered(countryCode) = 1)
ALTER TABLE phoneNumber
ADD CONSTRAINT chk$phoneNumber$areaCode$string$notEmpty
Check (dbo.string$checkEntered(areaCode) = 1)
ALTER TABLE phoneNumber
ADD CONSTRAINT chk$phoneNumber$exchange$string$notEmpty
Check (dbo.string$checkEntered(exchange) = 1)
ALTER TABLE phoneNumber
ADD CONSTRAINT chk$phoneNumber$number$string$notEmpty
Check (dbo.string$checkEntered(number) = 1)
ALTER TABLE phoneNumberType
ADD CONSTRAINT chk$phoneNumberType$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE phoneNumberType
ADD CONSTRAINT chk$phoneNumberType$description$string$notEmpty
Check (dbo.string$checkEntered(description) = 1)
ALTER TABLE state
ADD CONSTRAINT chk$state$code$string$notEmpty
Check (dbo.string$checkEntered(code) = 1)
ALTER TABLE statementItem
ADD CONSTRAINT chk$statementItem$number$string$notEmpty
Check (dbo.string$checkEntered(number) = 1)
ALTER TABLE statementItem
ADD CONSTRAINT chk$statementItem$description$string$notEmpty
Check (dbo.string$checkEntered(description) = 1)
ALTER TABLE statementType
ADD CONSTRAINT chk$statementType$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE statementType
ADD CONSTRAINT chk$statementType$description$string$notEmpty
Check (dbo.string$checkEntered(description) = 1)
ALTER TABLE [transaction]
ADD CONSTRAINT chk$transaction$number$string$notEmpty
Check (dbo.string$checkEntered(number) = 1)
ALTER TABLE [transaction]
ADD CONSTRAINT chk$transaction$description$string$notEmpty
Check (dbo.string$checkEntered(description) = 1)
ALTER TABLE [transaction]
ADD CONSTRAINT chk$transaction$signature$string$notEmpty
Check (dbo.string$checkEntered(signature) = 1)
ALTER TABLE transactionAllocationType
ADD CONSTRAINT chk$transactionAllocationType$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE transactionAllocationType
ADD CONSTRAINT chk$transactionAllocationType$description$string$notEmpty
Check (dbo.string$checkEntered(description) = 1)
ALTER TABLE transactionType
ADD CONSTRAINT chk$transactionType$name$string$notEmpty
Check (dbo.string$checkEntered(name) = 1)
ALTER TABLE transactionType
ADD CONSTRAINT chk$transactionType$description$string$notEmpty
Check (dbo.string$checkEntered(description) = 1)
ALTER TABLE [user]
ADD CONSTRAINT chk$user$userName$string$notEmpty
Check (dbo.string$checkEntered(userName) = 1)
ALTER TABLE [user]
ADD CONSTRAINT chk$user$firstName$string$notEmpty
Check (dbo.string$checkEntered(firstName) = 1)
ALTER TABLE [user]
ADD CONSTRAINT chk$user$lastName$string$notEmpty
Check (dbo.string$checkEntered(lastName) = 1)
ALTER TABLE zipCode
ADD CONSTRAINT chk$zipCode$zipCode$string$notEmpty
Check (dbo.string$checkEntered(zipCode) = 1)
GO
CREATE FUNCTION date$rangeCheck
(
@dateValue datetime, -- first date value
@dateToCompareAgainst datetime -- pass in date to compare to
)
RETURNS int
AS
BEGIN
DECLARE @returnVal int
IF @dateValue > @dateToCompareAgainst
BEGIN
SET @returnVal = 1 -- date is in the future
END
ELSE IF @dateValue < @dateToCompareAgainst
BEGIN
SET @returnVal = -1 -- date in is the past
END
ELSE
SET @returnVal = 0 -- dates are the same
RETURN @returnVal
END
GO
--empty condition
SELECT dbo.date$rangeCheck('1/1/1989',getdate()) as [should be -1]
SELECT dbo.date$rangeCheck(getdate(),getdate()) as [should be 0]
SELECT dbo.date$rangeCheck('1/1/2020',getdate()) as [should be 1]
GO
CREATE FUNCTION date$removeTime
(
@date datetime
)
RETURNS datetime AS
BEGIN
SET @date = CAST(datePart(month,@date) as varchar(2)) + '/' +
CAST(datePart(day,@date) as varchar(2)) + '/' +
CAST(datePart(year,@date) as varchar(4))
RETURN @date
END
GO
ALTER TABLE [transaction]
ADD CONSTRAINT chkTransaction$date$date$notFuture
--0 is equal, -1 means in the past, 1 means in the future
CHECK (dbo.date$rangeCheck(date,dbo.date$removeTime(getdate())) > 0)
GO
--slight change from the book. Had to add it as null then alter to not null
ALTER TABLE transactionType
ADD minimumAmount money NULL
ALTER TABLE transactionType
ALTER COLUMN minimumAmount money NOT NULL
ALTER TABLE transactionType
ADD maximumAmount money NULL
ALTER TABLE transactionType
ALTER COLUMN maximumAmount money NOT NULL
--simple check constraint to make certaint that min is less than max
ALTER TABLE transactionType
ADD CONSTRAINT chkTransactionType$minAndMaxRangeCheck
CHECK (minimumAmount <= maximumAmount)
GO
--note that I forgot the RETURN clause in this function in the book
CREATE FUNCTION transactionType$validateAmountInRange
(
@transactionTypeId int,
@amount money
)
RETURNS bit
AS
BEGIN
--1 means within range, 0 out of range
DECLARE @returnValue bit
IF EXISTS ( select *
FROM transactionType
WHERE @amount NOT BETWEEN minimumAmount AND maximumAmount
AND transactionTypeId = @transactionTypeId)
BEGIN
SET @returnValue = 0
END
ELSE
SET @returnValue = 1
RETURN @returnValue
END
GO
ALTER TABLE [transaction]
ADD CONSTRAINT chkTransaction$amountProperForTransactionType
CHECK (dbo.transactionType$validateAmountInRange(transactionTypeId, amount) = 1 )
GO