Joe Celko's SQL for Smarties Advanced SQL Programming [3 ed.] 9780123693792, 0123693799

SQL for Smarties was hailed as the first book devoted explicitly to the advanced techniques needed to transform an exper

250 66 82KB

English Pages 811 Year 2005

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Contents......Page 3
1.1 What Changed in Ten Years......Page 19
1.2 What Is New in This Edition......Page 21
1.3 Corrections and Additions......Page 22
Database Design......Page 23
1.1 Schema and Table Creation......Page 24
1.1.1 CREATE SCHEMA Statement......Page 25
1.1.2 Manipulating Tables......Page 27
1.1.3 Column Constraints......Page 32
1.1.4 UNIQUE Constraints versus UNIQUE Indexes......Page 39
1.1.5 Nested UNIQUE Constraints......Page 40
1.1.6 Overlapping Keys......Page 44
1.1.8 Using VIEWs for Schema Level Constraints......Page 47
1.1.9 Using PRIMARY KEYs and ASSERTIONs for Constraints......Page 51
1.1.10 Avoiding Attribute Splitting......Page 53
1.1.11 Modeling Class Hierarchies in DDL......Page 56
1.2 Generating Unique Sequential Numbers for Keys......Page 58
1.2.1 IDENTITY Columns......Page 59
1.2.3 Sequential Numbering in Pure SQL......Page 61
1.2.4 GUIDs......Page 63
1.2.6 Unique Value Generators......Page 64
1.2.7 Preallocated Values......Page 66
1.2.8 Random Order Values......Page 67
1.3 A Remark on Duplicate Rows......Page 70
1.4.1 Schema Tables......Page 72
1.4.3 CREATE DOMAIN Statement......Page 73
1.4.4 CREATE TRIGGER Statement......Page 74
1.4.6 DECLARE CURSOR Statement......Page 75
Normalization......Page 82
2.2 First Normal Form (1NF)......Page 85
2.2.1 Note on Repeated Groups......Page 87
2.3 Second Normal Form (2NF)......Page 91
2.4 Third Normal Form (3NF)......Page 92
2.5 Elementary Key Normal Form (EKNF)......Page 93
2.6 Boyce-Codd Normal Form (BCNF)......Page 94
2.7 Fourth Normal Form (4NF)......Page 96
2.8 Fifth Normal Form (5NF)......Page 97
2.9 Domain-Key Normal Form (DKNF)......Page 99
2.10 Practical Hints for Normalization......Page 108
2.11 Key Types......Page 109
2.11.3 Exposed Physical Locators......Page 110
2.11.4 Practical Hints for Denormalization......Page 112
2.11.5 Row Sorting......Page 114
3.1 Numeric Types......Page 121
3.1.1 BIT, BYTE, and BOOLEAN Data Types......Page 124
3.2.1 Rounding and Truncating......Page 125
3.2.2 CAST() Function......Page 127
3.3 Four-Function Arithmetic......Page 128
3.4 Arithmetic and NULLs......Page 129
3.5.1 NULLIF() Function......Page 130
3.5.2 COALESCE() Function......Page 131
3.6.1 Number Theory Operators......Page 133
3.6.3 Scaling Functions......Page 136
3.6.4 Converting Numbers to Words......Page 137
4.1 Notes on Calendar Standards......Page 139
4.2 SQL Temporal Data Types......Page 143
4.2.2 Date Format Standards......Page 144
4.2.3 Handling Timestamps......Page 145
4.2.4 Handling Times......Page 147
4.3 Queries with Date Arithmetic......Page 148
4.4.1 Temporal Duplicates......Page 149
4.4.2 Temporal Databases......Page 155
4.4.3 Temporal Projection and Selection......Page 157
4.4.4 Temporal Joins......Page 159
4.4.5 Modifying Valid-Time State Tables......Page 165
4.4.6 Current Modification......Page 166
4.4.7 Sequenced Modification......Page 170
4.4.8 Nonsequenced Modification......Page 175
4.4.9 Transaction-Time State Tables......Page 176
4.4.10 Maintaining the Audit Log......Page 178
4.4.11 Querying the Audit Log......Page 180
4.4.13 Bitemporal Tables......Page 184
4.4.14 Temporal Support in Standard SQL......Page 187
Character Data Types in SQL......Page 188
5.1.1 Problems of String Equality......Page 189
5.1.2 Problems of String Ordering......Page 190
5.2 Standard String Functions......Page 191
5.3 Common Vendor Extensions......Page 193
5.3.1 Phonetic Matching......Page 194
5.4 Cutter Tables......Page 201
NULLs: Missing Data in SQL......Page 203
6.2 Missing Values in Columns......Page 205
6.3 Context and Missing Values......Page 207
6.5 NULLs and Logic......Page 208
6.5.1 NULLS in Subquery Predicates......Page 209
6.7 Functions and NULLs......Page 211
6.8 NULLs and Host Languages......Page 212
6.9 Design Advice for NULLs......Page 213
6.9.1 Avoiding NULLs from the Host Programs......Page 215
6.10 A Note on Multiple NULL Values......Page 216
7.1 Distance Functions......Page 219
7.2 Storing an IP Address in SQL......Page 220
7.2.2 One INTEGER Column......Page 221
7.3 Currency and Other Unit Conversions......Page 223
7.4 Social Security Numbers......Page 224
7.5 Rational Numbers......Page 227
8.1 DELETE FROM Statement......Page 228
8.1.2 The WHERE Clause......Page 229
8.1.4 Deleting within the Same Table......Page 233
8.1.5 Deleting in Multiple Tables without Referential Integrity......Page 237
8.2.1 INSERT INTO Clause......Page 238
8.2.2 The Nature of Inserts......Page 239
8.3.1 The UPDATE Clause......Page 240
8.3.2 The WHERE Clause......Page 241
8.3.3 The SET Clause......Page 242
8.3.4 Updating with a Second Table......Page 243
8.3.5 Using the CASE Expression in UPDATEs......Page 245
8.4 A Note on Flaws in a Common Vendor Extension......Page 248
8.5 MERGE Statement......Page 249
Comparison or Theta Operators......Page 252
9.1 Converting Data Types......Page 253
9.2 Row Comparisons in SQL......Page 255
10.1 IS NULL Predicate......Page 258
10.2 IS [NOT]{TRUE | FALSE | UNKNOWN} Predicate......Page 259
10.3 IS [NOT] NORMALIZED Predicate......Page 261
11.1 The CASE Expression......Page 263
11.1.1 The COALESCE() and NULLIF() Functions......Page 267
11.1.2 CASE Expressions with GROUP BY......Page 268
11.1.3 CASE, CHECK() Clauses and Logical Implication......Page 269
11.1.4 Subquery Expressions and Constants......Page 273
11.2 Rozenshtein Characteristic Functions......Page 274
LIKE Predicate......Page 276
12.1 Tricks with Patterns......Page 277
12.4 Avoiding the LIKE Predicate with a Join......Page 279
12.5 CASE Expressions and LIKE Predicates......Page 281
12.6 SIMILAR TO Predicates......Page 282
12.7.1 String Character Content......Page 284
12.7.3 Creating an Index on a String......Page 285
13.1 The BETWEEN Predicate......Page 287
13.1.3 Programming Tips......Page 288
13.2.1 Time Periods and OVERLAPS Predicate......Page 289
The [NOT] IN() Predicate......Page 300
14.1 Optimizing the IN() Predicate......Page 301
14.2 Replacing ORs with the IN() Predicate......Page 305
14.3 NULLs and the IN() Predicate......Page 306
14.4 IN() Predicate and Referential Constraints......Page 308
14.5 IN() Predicate and Scalar Queries......Page 310
EXISTS() Predicate......Page 311
15.1 EXISTS and NULLs......Page 312
15.2 EXISTS and INNER JOINs......Page 314
15.3 NOT EXISTS and OUTER JOINs......Page 315
15.4 EXISTS() and Quantifier......Page 316
15.5 EXISTS() and Referential Constraints......Page 317
15.6 EXISTS and Three-Valued Logic......Page 318
Quantified Subquey Predicates......Page 321
16.1 Scalar Subquery Comparisons......Page 322
16.2 Quantifiers and Missing Dat......Page 323
16.3 The ALL Predicate and Extrema Functions......Page 325
16.4 The UNIQUE Predicate......Page 326
17.1.1 One-Level SELECT Statement......Page 328
17.1.2 Correlated Subqueries in a SELECT Statement......Page 335
17.1.3 SELECT Statement Syntax......Page 337
17.1.4 The ORDER BY Clause......Page 339
17.2 OUTER JOINs......Page 347
17.2.1 Syntax for OUTER JOINs......Page 348
17.2.2 NULLs and OUTER JOINs......Page 353
17.2.3 NATURAL versus Searched OUTER JOINs......Page 355
17.2.4 Self OUTER JOINs......Page 356
17.2.5 Two or More OUTER JOINs......Page 357
17.2.6 OUTER JOINs and Aggregate Functions......Page 359
17.2.7 FULL OUTER JOIN......Page 360
17.2.8 WHERE Clause OUTER JOIN Operators......Page 361
17.3 Old versus New JOIN Syntax......Page 362
17.4 Scope of Derived Table Names......Page 364
17.5 JOINs by Function Calls......Page 365
17.6 The UNION JOIN......Page 367
17.7 Packing Joins......Page 369
17.8 Dr. Codd’s T-Join......Page 370
17.8.1 The Croatian Solution......Page 374
17.8.3 The Colombian Solution......Page 375
VIEWs, Derived Tables, Materialized Tables, and Temporary Tables......Page 380
18.1 VIEWs in Queries......Page 381
18.2 Updatable and Read-Only VIEWs......Page 382
18.3.3 Translated Columns......Page 384
18.3.4 Grouped VIEWs......Page 385
18.3.5 UNIONed VIEWs......Page 386
18.3.7 Nested VIEWs......Page 388
18.4.2 VIEW Materialization......Page 390
18.4.3 In-Line Text Expansion......Page 391
18.4.4 Pointer Structures......Page 393
18.5 WITH CHECK OPTION Clause......Page 394
18.5.1 WITH CHECK OPTION as CHECK() Clause......Page 399
18.6 Dropping VIEWs......Page 400
18.7 TEMPORARY TABLE Declarations......Page 401
18.8 Hints on Using VIEWs and TEMPORARY TABLEs......Page 402
18.8.2 Using TEMPORARY TABLEs......Page 403
18.8.3 Flattening a Table with a VIEW......Page 404
18.9.1 Derived Tables in the FROM clause......Page 406
18.10 Derived Tables in the WITH Clause......Page 408
19.1 Coverings and Partitions......Page 411
19.1.1 Partitioning by Ranges......Page 412
19.1.2 Partition by Functions......Page 413
19.1.3 Partition by Sequences......Page 414
19.2 Relational Division......Page 416
19.2.1 Division with a Remainder......Page 418
19.2.2 Exact Division......Page 419
19.2.4 Todd’s Division......Page 420
19.2.6 Division with Set Operators......Page 423
19.3 Romley’s Division......Page 424
19.4 Boolean Expressions in an RDBMS......Page 428
19.5 FIFO and LIFO Subsets......Page 430
20.1 GROUP BY Clause......Page 434
20.2 GROUP BY and HAVING......Page 436
20.2.1 Group Characteristics and the HAVING Clause......Page 438
20.3 Multiple Aggregation Levels......Page 440
20.3.1 Grouped VIEWs for Multiple Aggregation Levels......Page 441
20.3.2 Subquery Expressions for Multiple Aggregation Levels......Page 442
20.3.3 CASE Expressions for Multiple Aggregation Levels......Page 443
20.4 Grouping on Computed Columns......Page 444
20.5 Grouping into Pairs......Page 445
20.6 Sorting and GROUP BY......Page 446
Aggregate Functions......Page 448
21.1 COUNT() Functions......Page 449
21.2 SUM() Functions......Page 452
21.3 AVG() Functions......Page 453
21.3.1 Averages with Empty Groups......Page 455
21.3.2 Averages across Columns......Page 457
21.4.1 Simple Extrema Functions......Page 458
21.4.2 Generalized Extrema Functions......Page 460
21.4.3 Multiple Criteria Extrema Functions......Page 469
21.4.4 GREATEST() and LEAST() Functions......Page 471
21.5 The LIST() Aggregate Function......Page 474
21.5.1 The LIST() Function with a Procedure......Page 475
21.5.2 The LIST() Function by Crosstabs......Page 476
21.6 The PRD() Aggregate Function......Page 477
21.6.1 PRD() Function by Expressions......Page 478
21.6.2 The PRD() Aggregate Function by Logarithms......Page 479
21.7 Bitwise Aggregate Functions......Page 482
21.7.1 Bitwise OR Aggregate Function......Page 483
21.7.2 Bitwise AND Aggregate Function......Page 484
22.1 The Sequence Table......Page 485
22.1.1 Enumerating a List......Page 487
22.1.2 Mapping a Sequence into a Cycle......Page 489
22.1.3 Replacing an Iterative Loop......Page 491
22.2 Lookup Auxiliary Tables......Page 493
22.2.2 Multiple Translation Auxiliary Tables......Page 495
22.2.3 Multiple Parameter Auxiliary Tables......Page 496
22.2.4 Range Auxiliary Tables......Page 497
22.2.5 Hierarchical Auxiliary Tables......Page 498
22.2.6 One True Lookup Table......Page 499
22.3 Auxiliary Function Tables......Page 501
22.3.1 Inverse Functions with Auxiliary Tables......Page 503
22.3.2 Interpolation with Auxiliary Function Tables......Page 512
22.4 Global Constants Tables......Page 514
Statistics in SQL......Page 516
23.1 The Mode......Page 517
23.3 The Median......Page 519
23.3.1 Date’s First Median......Page 520
23.3.2 Celko’s First Median......Page 521
23.3.4 Murchison’s Median......Page 523
23.3.5 Celko’s Second Median......Page 524
23.3.6 Vaughan’s Median with VIEWs......Page 526
23.3.7 Median with Characteristic Function......Page 527
23.3.8 Celko’s Third Median......Page 529
23.3.9 Ken Henderson’s Median......Page 533
23.4 Variance and Standard Deviation......Page 534
23.6 Cumulative Statistics......Page 535
23.6.1 Running Totals......Page 536
23.6.2 Running Differences......Page 537
23.6.3 Cumulative Percentages......Page 538
23.6.4 Rankings and Related Statistics......Page 540
23.6.5 Quintiles and Related Statistics......Page 544
23.7 Cross Tabulations......Page 545
23.7.1 Crosstabs by Cross Join......Page 549
23.7.2 Crosstabs by Outer Joins......Page 550
23.7.3 Crosstabs by Subquery......Page 551
23.8 Harmonic Mean and Geometric Mean......Page 552
23.9.1 Covariance......Page 553
23.9.2 Pearson’s r......Page 554
23.9.3 NULLs in Multivariable Descriptive Statistics......Page 555
Regions, Runs, Gaps, Sequences, and Series......Page 556
24.1 Finding Subregions of Size (n)......Page 557
24.2 Numbering Regions......Page 558
24.3 Finding Regions of Maximum Size......Page 559
24.5 Run and Sequence Queries......Page 564
24.5.1 Filling in Sequence Numbers......Page 567
24.6 Summation of a Series......Page 569
24.7 Swapping and Sliding Values in a List......Page 572
24.9 Folding a List of Numbers......Page 574
24.10 Coverings......Page 575
Arrays in SQL......Page 581
25.1 Arrays via Named Columns......Page 582
25.2 Arrays via Subscript Columns......Page 586
25.3 Matrix Operations in SQL......Page 587
25.3.2 Matrix Addition......Page 588
25.3.3 Matrix Multiplication......Page 589
25.4 Flattening a Table into an Array......Page 591
25.5 Comparing Arrays in Table Format......Page 593
Set Operations......Page 596
26.1 UNION and UNION ALL......Page 597
26.1.1 Order of Execution......Page 599
26.1.3 UNION of Columns from the Same Table......Page 600
26.2 INTERSECT and EXCEPT......Page 601
26.2.1 INTERSECT and EXCEPT without NULLs and Duplicates......Page 604
26.2.2 INTERSECT and EXCEPT with NULLs and Duplicates......Page 605
26.3 A Note on ALL and SELECT DISTINCT......Page 606
26.4 Equality and Proper Subsets......Page 607
27.1 Every nth Item in a Table......Page 609
27.2 Picking Random Rows from a Table......Page 611
27.3.1 Proper Subset Operators......Page 616
27.3.2 Table Equality......Page 617
27.4 Picking a Representative Subset......Page 622
Trees and Hierarchies in SQL......Page 627
28.1 Adjacency List Model......Page 628
28.1.1 Complex Constraints......Page 629
28.1.2 Procedural Traversal for Queries......Page 631
28.2 The Path Enumeration Model......Page 632
28.2.1 Finding Subtrees and Nodes......Page 633
28.2.3 Deleting Nodes and Subtrees......Page 634
28.3 Nested Set Model of Hierarchies......Page 635
28.3.1 The Counting Property......Page 637
28.3.2 The Containment Property......Page 638
28.3.3 Subordinates......Page 639
28.3.5 Deleting Nodes and Subtrees......Page 640
28.3.6 Converting Adjacency List to Nested Set Model......Page 641
28.4 Other Models for Trees and Hierarchies......Page 643
Temporal Queries......Page 645
29.1 Temporal Math......Page 646
29.2 Personal Calendars......Page 647
29.3.1 Gaps in a Time Series......Page 649
29.3.2 Continuous Time Periods......Page 652
29.3.3 Missing Times in Contiguous Events......Page 656
29.3.4 Locating Dates......Page 660
29.3.5 Temporal Starting and Ending Points......Page 662
29.3.6 Average Wait Times......Page 664
29.4 Julian Dates......Page 665
29.5 Date and Time Extraction Functions......Page 669
29.6 Other Temporal Functions......Page 670
29.7 Weeks......Page 671
29.7.1 Sorting by Weekday Names......Page 673
29.8 Modeling Time in Tables......Page 674
29.8.1 Using Duration Pairs......Page 676
29.9 Calendar Auxiliary Table......Page 677
29.10.1 The Zeros......Page 679
29.10.2 Leap Year......Page 680
29.10.3 The Millennium......Page 681
29.10.4 Weird Dates in Legacy Data......Page 683
29.10.5 The Aftermath......Page 684
Graphs in SQL......Page 685
30.1.1 All Nodes in the Graph......Page 686
30.1.3 Reachable Nodes......Page 687
30.1.5 Indegree and Outdegree......Page 688
30.1.6 Source, Sink, Isolated, and Internal Nodes......Page 689
30.2 Paths in a Graph......Page 690
30.2.2 Shortest Path......Page 691
30.2.3 Paths by Iteration......Page 692
30.2.4 Listing the Paths......Page 695
30.3 Acyclic Graphs as Nested Sets......Page 699
30.4 Paths with CTE......Page 701
30.4.1 Nonacyclic Graphs......Page 707
30.5 Adjacency Matrix Model......Page 709
30.6 Points inside Polygons......Page 710
OLAP in SQL......Page 712
31.1 Star Schema......Page 713
31.2.2 Row Numbering......Page 714
31.2.3 GROUPING Operators......Page 715
31.2.4 The Window Clause......Page 717
31.2.5 OLAP Examples of SQL......Page 719
31.2.6 Enterprise-Wide Dimensional Layer......Page 720
31.3 A Bit of History......Page 721
32.1 Sessions......Page 722
32.2.1 Atomicity......Page 723
32.2.3 Isolation......Page 724
32.3.1 The Five Phenomena......Page 725
32.3.2 The Isolation Levels......Page 727
32.4 Pessimistic Concurrency Control......Page 729
32.5 SNAPSHOT Isolation: Optimistic Concurrency......Page 730
32.6 Logical Concurrency Control......Page 732
32.7 Deadlock and Livelocks......Page 733
Optimizing SQL......Page 734
33.1.2 Indexed Access......Page 735
33.2 Expressions and Unnested Queries......Page 736
33.2.1 Use Simple Expressions......Page 737
33.3 Give Extra Join Information in Queries......Page 741
33.4 Index Tables Carefully......Page 743
33.5 Watch the IN Predicate......Page 745
33.6 Avoid UNIONs......Page 747
33.7 Prefer Joins over Nested Queries......Page 748
33.9 Avoid Sorting......Page 749
33.10 Avoid CROSS JOINs......Page 753
33.11 Learn to Use Indexes Carefully......Page 754
33.12 Order Indexes Carefully......Page 755
33.13 Know Your Optimizer......Page 757
33.14 Recompile Static SQL after Schema Changes......Page 759
33.15 Temporary Tables Are Sometimes Handy......Page 760
33.16 Update Statistics......Page 763
References......Page 764
Index......Page 779
About the Author......Page 811

Joe Celko's SQL for Smarties Advanced SQL Programming [3 ed.]
 9780123693792, 0123693799

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers