Tải bản đầy đủ (.pdf) (9 trang)

Joe Celko s SQL for Smarties - Advanced SQL Programming P84 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (105 KB, 9 trang )

802 INDEX
SELECT, 58, 143–44, 317–68
SET TRANSACTION, 724–25
UPDATE, 58–59, 223–30
State transition model, 670
Static SQL
compilation, 756
recompiling, 756–57
Statistics, 509–48
average deviation, 528
AVG() function, 512
cross tabulations, 538–45
cumulative, 528–38
cumulative percentages, 531–33
descriptive, 509
geometric mean, 545–46
harmonic mean, 545–46
median, 512–27
mode, 510–12
multivariable descriptive, 546–48
quintiles, 537–38
rankings, 533–37
running differences, 530–31
running totals, 529–30
standard deviation, 527–28
updating, 760
Storage
date, 658
GUID requirement, 42
IP address, 202–5
physically contiguous, 90


temporary table, 51
Strings
case, changing, 170–71
character content, 269–70
declaring, 270
defined, 170
dummy, 196
equality, 170–71
expressions, 738
functions, 172–73
grouping, 172
index creation on, 271
length, 172
MAX() function for, 449
MIN() value for, 450
ordering, 171
problems, 170–72
searching, 270
tricks with, 269–71
Subordinates, 635
finding, 630
junior, 635
senior, 635
Subqueries
columnar, 257
constants and, 257–58
correlated, 310, 324–26, 436
crosstabs by, 544–45
DEFAULT clauses and, 608
empty, 315

execution, 290
grouped, 362
IN() predicates with, 288
for multiple aggregation levels,
433–34
nesting, 454
in
NOT EXISTS() predicates, 435
row, 257, 310
scalar, 257, 433
scalar comparisons, 310–11
SELECT DISTINCT in, 314
table, 433
tabular, 257
TRUE, 315
types of, 257
Subregions, finding, 550–51
Subscripts
columns, arrays via, 580–81
data types, 581
in declared range, 580
as enumerations, 585
Subsequences, 549, 554
INDEX 803
Subsets, 605–22
alphabetical ordering, 437
equality, 617–18
minimum, 620
operators, proper, 612–13
proper, equality and, 602–3

representative, picking, 618–22
row, finding, 618
SUBSTRING() function, 172, 467
empty string return, 355
joining tables with, 355
Substrings
integer, 485
operator, 381
Subtrees
deleting, 630–31, 636–37
finding, 629–30
See also Trees
SUM() function, 107, 443–44
ALL keyword, 443
defined, 443
DISTINCT keyword, 470
SUM keyword, 443
Summation of a series, 562–65
complexity, 564
running total, 562
Swaps
chain, 95
minimum number of, 95
pairs, 94
Swedish solution, 364
Sybase/SQL Server model, 759
Systematic Treatment of NULL Values,
62
T Tables
3NF, 79

4NF, 76
altering, 628
attribute split, 31–33
auxiliary, 477–507
base, 44
BCNF, 73–74
bitemporal, 164–67
calendar, 567
calendar auxiliary, 673–75
constraints, 5
Cutter, 182–83
cyclic data, 643
deleting in, 216–19
derived, 353–54, 395–97
EKNF, 72, 73
empty, 187
equality, 613–18
flattening, 93
flattening, with
VIEWs, 393–95
global constants, 506–7
grouped, 428
indexing, 740–42
loading with query, 355
lookup, 296
manipulating, 5–10
missing, 187
modeling time in, 670–73
name, 5
operations, 211–34

persistent, 3
preserved, 347
random rows, picking, 607–12
redundant duplicates, 217–18
rolling back, 160
schema, 50–51
sequence, 477–85
single-column range, 402–3
spreadsheets vs., 5
temporal granularity, 136
temporary, 51, 390–91, 757–60
timestamped, 131
transaction-time state, 156–58
unpreserved, 347
updating, 97
valid-time state, 136, 145–46
804 INDEX
virtual, 3, 369
See also Columns; rows
Tabular subqueries, 257
Temporal databases, 135–37
Temporal data models, 129–67
Temporal data types, 119–67
converting, 236
MAX() function for, 449
MIN() function for, 449–50
Temporal duplicates, 129–35
current, 131, 133
defined, 131
interaction, 132

nonsequenced, 133
sequenced, 132, 134, 135
value-equivalent, 132–33
See also Duplicates
Temporal
JOINs, 139–45
Temporal math, 642–45
Temporal projection/selection, 137–39
Temporal queries, 641–80
calendar auxiliary table, 673–75
date/time extraction functions,
665–66
Julian dates, 661–65
math, 642–43
modeling time in tables, 670–73
personal calendars, 643–45
time series, 645–61
weeks, 667–70
year 2000 problem, 675–80
See also Queries
Temporal starting/ending points, 658–
60
Temporal support, 167
Temporary tables, 51
creating, on fly, 393
declarations, 390–91
global, 51, 392
for intermediate results, 757
local, 51, 392
storage space, 51

usefulness, 757–60
using, 392–93
VIEWs and, 391–95
See also Tables
Text, in-line expansion, 380–82
Theta operators, 235–40
Third Normal Form (3NF), 71–72
with CASE tools, 78
normalization algorithm, 79
tables, 79
See also Normal forms
Three-valued logic (3VL), 193, 306–8
TIME data type, 123, 167
Timelines
diagram of overlapping cases, 279
diagrams, 280, 282
partitioned into intervals, 645
Time(s)
average wait, 660–61
CUT, 122
DST, 127
durations, 128
fixed events, 128
format, 128
handling, 127–28
handling tips, 124
intervals, 128
missing, in contiguous events,
652–56
modeling in tables, 670–73

overlapping, 277
user-defined, 135
UTC, 122–23, 127, 128
Time series, 645–61
average wait times, 660–61
continuous time periods, 648–52
gaps in, 645–48
locating dates, 656–58
missing times, 652–56
starting/ending points, 658–60
INDEX 805
See also Temporal queries
TIMESTAMP data type, 123, 167
time-varying data support with,
167
as unique identifiers, 126
Timestamps
handling, 125–27
handling tips, 124
purposes, 125
T-joins, 359–68
approaches, 360–61
Ascending Order Algorithm, 360
Colombian solution, 364–68
condition, maintaining, 361
Croatian solution, 363–64
defined, 359
Descending Order Algorithm, 361
procedural algorithm, 363
Swedish solution, 364

tables, 359–60
See also
JOINs
Todd’s division, 410–12
Transactions
ACID properties and, 720–22
deadlocks, 730
isolation levels, 724–26
livelocks, 730
Transaction-time tables, 156–58
challenge, 156
characterizations, 156–57
example, 157–58
modeling, 156
modifications, 164
nonsequenced queries, 162, 163
sequenced queries, 162, 163
See also Tables
Transitive dependencies, 71, 72
3NF and, 71, 72
computed columns, 72
removal, 74
Translated columns, 373–74
TRANSLATE() function, 173
Translation auxiliary tables, 487–88
multiple, 487–88
simple, 487
See also Auxiliary tables
Transposition, matrix, 585
Traversal

hiding, 628
modified preorder tree algorithm,
633
procedural, 627–28
Trees
adjacency list model, 624–28
binary, 623
defined, 623
defining, 624
as directed graphs, 681
edge, deleting, 628
model choices, 639–40
modified preorder, 633
nodes, 623
path enumeration model, 628–31
root, 623
specialized, 639
structure, 624
TRIGGERs, 27
advantages, 53
code, 39–40
declaring, 52–53
defined, 52
disadvantages, 53
performance, 29
uses, 53
Triggers
audit logs and, 158
DELETE, 160
INSERT, 160

INSTEAD OF, 28, 36
UPDATE, 160
TRIM() function, 173, 264, 467
TRUNCATE() function, 117, 663, 664
806 INDEX
Truncation, 105–6
defined, 105, 444
negative numbers, 444
positive numbers, 444
U UNION ALL operator, 68, 143, 375,
592–96, 617
defined, 592
duplicate preservation, 593
mixed, 595
optimizing, 593
order of execution, 594–95
syntax, 592
See also Set operators
UNIONed VIEWs, 375–77
UNION JOINs, 356–57
defined, 350, 356
illustrated, 351
use, 356–57
See also
JOINs
UNION operator, 142, 143, 375, 592–
96, 617
avoiding, 744
behavior, 602
defined, 592

implementation, 744
mixed, 595
optimizing, 593
order of execution, 594–95
syntax, 592
UNION ALL to replace, 744
See also Set operators
UNIQUE constraints, 14, 741
defined, 14
nested, 18–22
UNIQUE indexes vs., 17
UNIQUE indexes, 17
Uniqueness constraints, 31
UNIQUE predicate, 314–15
defined, 314
syntax, 314–15
Universal Coordinated Time (UTC),
122–23, 127, 128
Unload utilities, 223
Updatable
VIEWs, 371–73
defined, 371
queries, 372
queries criteria, 371
See also
VIEWs
Updates
audit logs, 158
bitemporal table, 165
logical current, 148–50

with second table, 226–28
sequenced, 152–55
statistics, 760
tables, 97
UPDATE statement, 27, 223–30, 583
with
CASE expression, 228–30
defined, 223
error message, 227
FROM clause, 231
positioned, 58–59
searched, 226
with second table, 226–28
SET clause, 224, 225–26
syntax, 223–24
WHERE clause, 224–25, 228, 230
User-controlled keys, 91
USING clause, 327
V Valid-time state tables, 136
current deletion on, 147–48
current modifications, 146–50
current update on, 148–50
modifying, 145–46
nonsequenced modifications, 155
sequenced deletion on, 150–52
sequenced modifications, 150–55
sequenced update on, 152–55
See also Tables
Valued predicates, 241–45
INDEX 807

IS [NOT] TRUE | FALSE |
UNKNOWN, 242–44
IS NULL, 241–42
Value-equivalent duplicates, 132–33
Value generators, 42–44
Values
approximate, 198
average, 445
calibration, 652
insertion, 221
last allocated, 43
missing, 109, 187–90
multiple missing, 199
negative, 114
NULL, multiple, 198–200
preallocated, 44–45
random order, 45–48
seed, 609
sorting, 333
swapping/sliding, 565–67
VALUES constructor, 397, 481
VARCHAR() data type, 169
Vaughan’s median, 519–20
Vendor extensions, 174–82
defined, 174
flaws, 231–32
FLIP(), 175
NUMTOWORDS(), 175
phonetic matching, 175–82
REPLACE(), 174

REPLICATE(), 174
REVERSE(), 175
SPACE(), 174
Vendor math functions, 113–18
exponential functions, 116
numbers to words conversion,
117–18
number theory operators, 113–16
scaling functions, 116–17
VIEWs
building, 282, 283–84
calculated columns, 373
changed, 371
defined, 369
definition, 370
dropping, 389–90
existence, 370
flattening, 393–95
GROUP BY clause and, 381, 432
grouped, 290, 374–75
grouped, for multiple aggregation
levels, 432
handling in database system, 379–
83
hiding in, 36
indexing and, 383
in-line text expansion, 380–82
JOINs in, 377
key, 371
materialization, 379–80

multitable, 383
names, 370
nested, 370, 377–79
pointer structures, 382–83
in queries, 370
read-only, 371–73, 374
recursive, 688
with schema-level constraints, 25–
29
for security, 392
single-table projection/restriction,
373
syntax, 370
temporary tables and, 391–95
translated columns, 373–74
types of, 373–79
UNIONed, 375–77
updatable, 371–73
Vaughan’s median with, 519–20
WITH CHECK OPTION clause,
383–89
Views
808 INDEX
in limiting user access, 45
reconstruction as, 160–61
updating rule, 63
valid-time state, 166
View Updating Rule, 63
Virtual tables, 3
Virtual views. See

VIEWs
VLDB (Very Large Databases), 39
WWeeks, 667–70
example, 668–69
weekday name sorting, 669–70
WHEN clause
CASE expressions, 248, 249, 250
in sequence numbers, 561
WHERE clause
complexity, 216
DELETE FROM statement, 212–16
one-level
SELECT statement, 318,
323
OUTER JOINs and, 350–51
redundancy, 739
scope rules, 216
UPDATE statement, 224–25, 228,
230
Window clause, 714–16
aggregation grouping, 715–16
format, 714
ordering, 715
partitioning, 715
subclauses, 714
WITH CHECK OPTION clause, 383–89
as
CHECK() clause, 388–89
CHECK constraint and, 384
defined, 383

updating and, 385
WHERE clause and, 384
WITH clause
derived tables in, 397–99
queries, building, 398
syntax, 397
Words, converting numbers to, 117–
18
X Xbase languages, 171
X/Open transaction model, 757
XOR function, 610
Y Year 2000 problems, 675–80
aftermath, 680
inventory retention programs, 678
leap year, 676–77
legacy data, 679–80
types of, 675
zeros, 675–76
Z Zeller’s algorithm, 666
Zeros, year 2000, 675–76


ABOUT THE AUTHOR

Joe Celko

is a noted consultant and lecturer, and one of the most-
read SQL authors in the world. He is well known for his 10 years of
service on the ANSI SQL standards committee, his column in


Intelli-
gent Enterprise

magazine (which won several Reader’s Choice
Awards), and the war stories he tells to provide real-world insights
into SQL programming. His best-selling books include

Joe Celko’s
SQL for Smarties: Advanced SQL Programming, second edition; Joe
Celko’s SQL Puzzles and Answers;

and

Joe Celko’s Trees and Hierarchies
in SQL for Smarties.

×