/
Chapter 11.sql
663 lines (530 loc) · 14.3 KB
/
Chapter 11.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
--Note that the exact data that is returned from these queries will vary slightly from what is in
--the text of the book. The examples were not developed in the linear manner that the
--examples may be executed in this script
----------------------------------------------------------------------
-- Example Table
----------------------------------------------------------------------
CREATE TABLE artist
(
artistId int NOT NULL IDENTITY,
name varchar(60),
--note that the primary key is clustered to make a
--point later in the chapter
CONSTRAINT XPKartist PRIMARY KEY CLUSTERED (artistId),
CONSTRAINT XAKartist_name UNIQUE NONCLUSTERED (name)
)
INSERT INTO artist(name)
VALUES ('the beatles')
INSERT INTO artist(name)
VALUES ('the who')
GO
CREATE TABLE album
(
albumId int NOT NULL IDENTITY,
name varchar(60),
artistId int NOT NULL,
--note that the primary key is clustered to make a
--point later in the chapter
CONSTRAINT XPKalbum PRIMARY KEY CLUSTERED(albumId),
CONSTRAINT XAKalbum_name uNIQUE NONCLUSTERED (name),
CONSTRAINT artist$records$album FOREIGN KEY (artistId) REFERENCES artist
)
INSERT INTO album (name, artistId)
VALUES ('the white album',1)
INSERT INTO album (name, artistId)
VALUES ('revolver',1)
INSERT INTO album (name, artistId)
VALUES ('quadraphenia',2)
go
----------------------------------------------------------------------
-- Scalar Function
----------------------------------------------------------------------
CREATE FUNCTION integer$increment
(
@integerVal int
)
RETURNS int AS
BEGIN
SET @integerVal = @integerVal + 1
RETURN @integerVal
END
GO
----------------------------
DECLARE @integer int
EXEC @integer = integer$increment @integerVal = 1
SELECT @integer AS value
go
----------------------------
--this will raise an error
SELECT integer$increment (1) AS value
go
--this will not
SELECT dbo.integer$increment (1) AS value
go
----------------------------
--these also will not
SELECT dbo.integer$increment @integerVal = 1 AS value
SELECT dbo.integer$increment (@integerVal = 1) AS value
go
--nesting calls
SELECT dbo.integer$increment(dbo.integer$increment(dbo.integer$increment (1))) AS value
go
----------------------------
CREATE FUNCTION string$properCase
(
@inputString varchar(2000)
)
RETURNS varchar(2000) AS
BEGIN
-- set the whole string to lower
SET @inputString = LOWER(@inputstring)
-- then use stuff to replace the first character
SET @inputString =
--STUFF in the uppercased character in to the next character,
--replacing the lowercased letter
STUFF(@inputString,1,1,UPPER(SUBSTRING(@inputString,1,1)))
--@i is for the loop counter, initialized to 2
DECLARE @i int
SET @i = 1
--loop from the second character to the end of the string
WHILE @i < LEN(@inputString)
BEGIN
--if the character is a space
IF SUBSTRING(@inputString,@i,1) = ' '
BEGIN
--STUFF in the uppercased character into the next character
SET @inputString = STUFF(@inputString,@i +
1,1,UPPER(SUBSTRING(@inputString,@i + 1,1)))
END
--increment the loop counter
SET @i = @i + 1
END
RETURN @inputString
END
GO
----------------------------
SELECT name, dbo.string$properCase(name) AS artistProper
FROM artist
go
----------------------------
CREATE FUNCTION album$recordCount
(
@artistId int
)
RETURNS int AS
BEGIN
DECLARE @count AS int -- variable to hold output
--count the number of rows in the table
SELECT @count = count(*)
FROM album
WHERE artistId = @artistId
RETURN @count
END
GO
----------------------------
SELECT dbo.album$recordCount(1) AS albumCount
go
----------------------------
SELECT name,
artistId,
dbo.album$recordCount(artistId) AS albumCount
FROM album
Go
----------------------------------------------------------------------
--Inline Table-Valued Functions
----------------------------------------------------------------------
CREATE FUNCTION album$returnKeysByArtist
(
@artistId int
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN (
SELECT albumId
FROM dbo.album
WHERE artistId = @artistId )
go
----------------------------
EXEC dbo.album$returnKeysbyArtist @artistId = 1
go
----------------------------
SELECT album.albumId, album.name
FROM album
JOIN dbo.album$returnKeysbyArtist(1) AS functionTest
ON album.albumId = functionTest.albumId
go
----------------------------------------------------------------------
--Schema Binding
----------------------------------------------------------------------
DROP TABLE album
go
ALTER TABLE album
ADD description varchar(100) NULL
ALTER TABLE album
DROP COLUMN description
go
----------------------------
--this will cause an error
ALTER TABLE album
DROP COLUMN artistId
go
----------------------------
--this will cause an error
ALTER FUNCTION album$returnKeysByArtist
(
@artistId int
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN (
SELECT albumId
FROM album ---- used to be dbo.album
WHERE artistId = @artistId)
go
----------------------------
alter FUNCTION album$returnKeysByArtist
(
@artistId int
)
RETURNS TABLE
AS
RETURN (
SELECT albumId
FROM dbo.album
WHERE artistId = @artistId )
go
----------------------------------------------------------------------
--Multi-Statement Table-Valued Functions
----------------------------------------------------------------------
CREATE FUNCTION album$returnNamesInUpperCase
(
@artistId int
)
RETURNS @outTable table (
albumId int,
name varchar(60) )
WITH SCHEMABINDING
AS
BEGIN
--add all of the albums to the output table
INSERT INTO @outTable
SELECT albumId, name
FROM dbo.album
WHERE artistId = @artistId
--second superfluous update to show that it
--is a multi-statement function
UPDATE @outTable
SET name = UPPER(name)
RETURN
END
GO
----------------------------
SELECT * FROM dbo.album$returnNamesInUpperCase(1)
go
----------------------------------------------------------------------
--What User Defined Functions Cannot Do
----------------------------------------------------------------------
--this will fail because of the update
CREATE FUNCTION album$setAllToTheBeatles
(
)
RETURNS int AS
BEGIN
DECLARE @rowcount int
UPDATE album
SET artistId = 1
WHERE artistId <> 1
RETURN 1
END
go
----------------------------------------------------------------------
--literal defaults
----------------------------------------------------------------------
SELECT * FROM artist
go
ALTER TABLE album
ADD CONSTRAINT dfltAlbum$artistId$integer$one
DEFAULT (1)
FOR artistId
go
----------------------------
ALTER TABLE artist
ADD defaultFl bit NOT NULL
CONSTRAINT dfltArtist$defaultFl$boolean$false
DEFAULT (0)
go
----------------------------
UPDATE artist
SET defaultFl = 1
WHERE name = 'the beatles'
go
----------------------------
CREATE FUNCTION artist$returnDefaultId
(
)
RETURNS int
WITH SCHEMABINDING
AS
BEGIN
DECLARE @artistId int
SELECT @artistId = artistId
FROM dbo.artist
WHERE defaultFl = 1
RETURN @artistId
END
GO
----------------------------
SELECT dbo.artist$returnDefaultId() AS defaultValue
go
----------------------------
--not in text
alter table album
drop constraint dfltAlbum$artistId$integer$one
go
ALTER TABLE album
ADD CONSTRAINT dfltAlbum$artistId$function$useDefault
DEFAULT dbo.artist$returnDefaultId()
FOR artistId
go
----------------------------
INSERT INTO album(name)
VALUES ('rubber soul')
----------------------------
SELECT album.name AS albumName, artist.name AS artistName
FROM album
JOIN artist
ON album.artistId = artist.artistId
WHERE album.name = 'rubber soul'
go
----------------------------------------------------------------------
--Check Constraints
----------------------------------------------------------------------
--in a transaction so we don't actually add the rows to the database
begin transaction
INSERT INTO album ( name, artistId)
VALUES ('',1)
INSERT INTO album ( name, artistId)
VALUES ('',1)
rollback transaction
go
----------------------------
ALTER TABLE album WITH CHECK
ADD CONSTRAINT chkAlbum$name$string$noEmptyString
--trim the value in name, adding in stars to handle
--values safer than just checking length of the rtrim which
--will be NULL if name is NULL, and a single char if not. The coalesce
--makes certain
CHECK (( '*' + COALESCE(rtrim(name),'') + '*') <> '**' )
go
----------------------------
--not in text
INSERT INTO album ( name, artistId)
VALUES ('',1)
go
/* already added, this is just in case you want to try it
ALTER TABLE album WITH NOCHECK
ADD CONSTRAINT chkAlbum$name$string$noEmptyString
--trim the value in name, adding in stars to handle
--values safer than just checking length of the rtrim
CHECK (( '*' + COALESCE(rtrim(name),'') + '*') <> '**' )
*/
--slight deviation from the text to make things a bit more linear in the
--code samples. This will cause the error.
UPDATE album
SET name = ''
go
----------------------------
CREATE FUNCTION string$checkForEmptyString
(
@stringValue varchar(8000)
)
RETURNS bit
AS
BEGIN
DECLARE @logicalValue bit
IF ( '*' + COALESCE(RTRIM(@stringValue),'') + '*') = '**'
SET @logicalValue = 1
ELSE
SET @logicalValue = 0
RETURN @logicalValue
END
go
----------------------------
--not in text, but is here for cleanup
ALTER TABLE album
drop CONSTRAINT chkAlbum$name$string$noEmptyString
go
----------------------------
ALTER TABLE album WITH NOCHECK
ADD CONSTRAINT chkAlbum$name$string$noEmptyString
--trim the value in name, adding in stars to handle
--values safer than just checking length of the rtrim
CHECK (dbo.string$checkForEmptyString(name) = 0)
go
----------------------------
ALTER TABLE album
ADD catalogNumber char(12) NOT NULL
--temporary default so we can make the column
--not NULL in intial creation
CONSTRAINT tempDefaultAlbumCatalogNumber DEFAULT ('111-11111-11')
----------------------------
--drop it because this is not a proper default
ALTER TABLE album
DROP tempDefaultAlbumCatalogNumber
go
----------------------------
CREATE FUNCTION album$catalogNumberValidate
(
@catalogNumber char(12)
)
RETURNS bit
AS
BEGIN
DECLARE @logicalValue bit
--check to see if the value like the mask we have set up
IF @catalogNumber LIKE
'[0-9][0-9][0-9]-[0-9][0-9][0-9a-z][0-9a-z][0-9a-z]-[0-9][0-9]'
SET @logicalValue = 1 --yes it is
ELSE
SET @logicalValue = 0 --no it is not
--note that we cannot just simply say RETURN 1, or RETURN 0 as the
--function must have only one exit point
RETURN @logicalValue
END
go
----------------------------
IF dbo.album$catalogNumberValidate('433-11qww-33') = 1
SELECT 'Yes' AS result
ELSE
SELECT 'No' AS result
go
----------------------------
ALTER TABLE album
ADD CONSTRAINT
chkAlbum$catalogNumber$function$artist$catalogNumberValidate
--keep it simple here, encapsulating all we need to in the
--function
CHECK (dbo.album$catalogNumbervalidate(catalogNumber) = 1)
go
----------------------------
UPDATE album
SET catalogNumber = '1'
WHERE name = 'the white album'
go
UPDATE album
SET catalogNumber = '433-43ASD-33'
WHERE name = 'the white album'
go
----------------------------
ALTER TABLE artist
ADD catalogNumberMask varchar(100) NOT NULL
-- it is acceptable to have a mask of percent in this case
CONSTRAINT dfltArtist$catalogNumberMask$string$percent
DEFAULT ('%')
go
----------------------------
--not in text. We drop the constraint so we can change the function
ALTER TABLE album
drop CONSTRAINT
chkAlbum$catalogNumber$function$artist$catalogNumberValidate
go
----------------------------
ALTER FUNCTION album$catalogNumberValidate
(
@catalogNumber char(12),
@artistId int --now based on the artist id
)
RETURNS bit
AS
BEGIN
DECLARE @logicalValue bit, @catalogNumberMask varchar(100)
SELECT @catalogNumberMask = catalogNumberMask
FROM artist
WHERE artistId = @artistId
IF @catalogNumber LIKE @catalogNumberMask
SET @logicalValue = 1
ELSE
SET @logicalValue = 0
RETURN @logicalValue
END
GO
----------------------------
--note that we have set these via the artistId which is an identity value
--you may have to modify these values if you had trouble loading the artist
--data
UPDATE artist
SET catalogNumberMask =
'[0-9][0-9][0-9]-[0-9][0-9][0-9a-z][0-9a-z][0-9a-z]-[0-9][0-9]'
WHERE artistId = 1 --the beatles
UPDATE artist
SET catalogNumberMask =
'[a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
WHERE artistId = 2 --the who
go
----------------------------
IF dbo.album$catalogNumberValidate('433-43ASD-33',1) = 1
SELECT 'Yes' AS result
ELSE
SELECT 'No' AS result
IF dbo.album$catalogNumberValidate('aa1111111111',2) = 1
SELECT 'Yes' AS result
ELSE
SELECT 'No' AS result
GO
----------------------------
ALTER TABLE album
ADD CONSTRAINT
chkAlbum$catalogNumber$function$artist$catalogNumberValidate
CHECK (dbo.album$catalogNumbervalidate(catalogNumber,artistId) = 1)
go
----------------------------
--to find where our data is not ready for the constraint,
--we run the following query
SELECT dbo.album$catalogNumbervalidate(catalogNumber,artistId)
AS validCatalogNumber,
artistId, name, catalogNumber
FROM album
WHERE dbo.album$catalogNumbervalidate(catalogNumber,artistId) <> 1
go
----------------------------
UPDATE album
SET catalogNumber = '1'
WHERE name = 'the white album'
go
UPDATE album
SET catalogNumber = '433-43ASD-33'
WHERE name = 'the white album' --the artist is the beatles
go
----------------------------------------------------------------------
--Handling Errors Caused by Constraints
----------------------------------------------------------------------
BEGIN TRANSACTION
DECLARE @errorHold int
UPDATE album
SET catalogNumber = '1'
WHERE name = 'the white album'
SET @errorHold = @@error
IF @errorHold <> 0
BEGIN
RAISERROR 50000 'Error modifying the first catalogNumber for album'
ROLLBACK TRANSACTION
END
IF @errorHold = 0
BEGIN
UPDATE album
SET catalogNumber = '1'
WHERE name = 'the white album'
SET @errorHold = @@error
IF @errorHold <> 0
BEGIN
RAISERROR 50000 'Error modifying the second catalogNumber'
ROLLBACK TRANSACTION
END
END
IF @errorHold = 0
COMMIT TRANSACTION
ELSE
RAISERROR 50000 'Update batch did not succeed'
go