1. Trang chủ
  2. » Công Nghệ Thông Tin

Joe Celko s SQL for Smarties - Advanced SQL Programming P82 pps

10 76 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 112,47 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Trang 1

COUNT() function, 440–43, 626

ALL keyword, 442, 443

cardinality, 440

DISTINCT keyword, 443

empty table, 440

expression counting, 440

forms, 440

of HAVING clause, 459

in missing data determination, 548

tricks, 440

Counting property, 633–34

Covariance, 546–47

Coverings, 568–73

defined, 401

holes, 572

partitions and, 401–6

CREATE ASSERTION constraints, 25

CREATE ASSERTION statement, 26

CREATE DOMAIN statement, 51–52

CHECK() clause, 52

defined, 51

syntax, 51–52

CREATE INDEX statement, 752

CREATE PROCEDURE statement, 53

CREATE SCHEMA statement, 3–5

CREATE TABLE statement, 5, 8–9

defined, 8

syntax, 8–9

CREATE TEMP TABLE statement,

390–91

GLOBAL option, 391

LOCAL option, 391

ON COMMIT ROWS clause, 391

syntax, 390

CREATE TRIGGER statement, 52–53

CREATE VIEW statement, 380

Croatian solution, 363–64

CROSS JOINs, 321–23

avoiding, 750–51

building, 338

crosstabs by, 542–43

relational division, 408 three-table, 750 two-table, 750 Crosstabs, 538–45 arrays and, 585

by CASE expression, 545

by CROSS JOIN, 542–43 defined, 538

example, 541–42

of (n) variable, 541

one-way, 539

by OUTER JOINs, 543–44 reporting package, 538

by subquery, 544–45 summary data, 542 two-way, 540

See also Statistics

CUBE supergroup, 713–14 Cumulative percentages, 531–33 defined, 531

example, 531–32

See also Statistics

Cumulative statistics, 528–38 cumulative percentages, 531–33 defined, 528

quintiles, 537–38 rankings, 533–37 running differences, 530–31 running totals, 529–30

See also Statistics

Currency amount, 205 conversion, 205–6 unit of, 205 Current modifications, 146–50 deletion, 147–48

update, 148–50 Current queries, 144 Cursors

declaring, 54 defined, 54

Trang 2

implicit, 55

loop, 58

ORDER BY clause, 328

proprietary versions, 53–54

in removing redundant duplicates,

216

<updatability clause>, 333

using, 56–58

CURSOR STABILITY isolation level,

726

Cutter tables, 182–83

copies, 183

defined, 182

properties, 183

D Data

cyclic, 643

independence, 63

integrity, 625

loading, 50

numeric, 101–18

partitioning, 401–23

state transition model, 670

temporal models, 129–67

Database management systems

(DBMSs), 50

Databases

advantages, 1

design, 1–59

historical, 135

normalized, 88

temporal, 135–37

Data types

BIGINT, 102

BIT, 104

BYTE, 104

categories, 9

CHAR(), 169

character, 169–83

for comparison operators, 235

converting, 236–37

DATE, 123, 128, 167 DECIMAL, 102 INTEGER, 102 INTERVAL, 123 NCHAR(), 169 NUMERIC, 102 NVARCHAR(), 169 SMALLINT, 102 source/target combination, 237 temporal, 119–67

TIME, 123, 167 TIMESTAMP, 123, 167 VARCHAR(), 169 Data warehouses denormalization, 91, 93 OLTP and, 709–10 DATE data type, 123, 128, 167 functions, 642–43

intervals, 128 DATE() function, 665–66 Dates

arithmetic, queries, 128–29 boundary, 654

current, returning, 129 format standards, 124–25 Georgian, 663

handling tips, 124 holiday, 644 Julian, 661–65 Julianized columns, 567 leap-year, 658

lists, building, 644 locating, 656–58 NULL, 655 personal calendars, 643–45 storage methods, 658 weird, in legacy data, 679–80 Date’s first median, 513–14 Date’s second median, 516 DDL (Data Definition Language), 1

Trang 3

modeling class hierarchies in, 34–

36

predicates in CHECK() constraint,

254

Deadlocks, 730

DEALLOCATE statement, 56

DECIMAL numeric type, 102

Declarative referential integrity (DRI),

17, 31

DECLARE CURSOR statement, 53–55,

58

defined, 54

syntax, 54–55

DEFAULT clause, 9–10

subqueries and, 608

use of, 28

Default ordering, 438

Deferrable constraints, 10, 17

DELETE FROM statement, 211–20, 694

columns, constants, functions, 216

correlation names and, 216

defined, 211

forms, 211–12

positioned, 211

redundant duplicate removal and,

217–19

searched, 212

searched, syntax, 212

WHERE clause, 212–16

DELETE statement, 58–59, 214

Deletions

current, 147–48

in multiple tables without

referential integrity, 220

nonsequenced, 155

positioned, 211

in same table, 216–19

searched, 216

second table data and, 216

sequenced, 150–52

DeMorgan’s law, 295

Denormalization CHECK() constraints and, 93 data warehouses, 91, 93 performance and, 91 practical hints, 91–93

See also Normalization

DENSE_RANK function, 711 Dependencies

multivalued, 75, 76 transitive, 71, 72 Deprecation, 328–29 Derived tables, 395–97 building, inside queries, 370 CASE expression and, 422 expressions, nesting, 353

in FROM clause, 395–97 for modes, 512

names, scope, 353–54 using, 395–97

VALUES constructor and, 397

in WITH clause, 397–99

See also Tables

Descending Order Algorithm, 361 DETERMINISTIC option, 607 Deviation

average, 528 standard, 527–28 Distance functions, 201–2 Distribution

independence, 63 skewed left/right, 527 Distribution Independence Rule, 63 Division

exact, 409 with JOINs, 412–13 relational, 406–8 with remainders, 408–9 Romley’s, 414–18 with set operators, 413–14 Todd’s, 410–12

Trang 4

DML (Data Manipulation Language),

254

Domain/Key Normal Form (DKNF),

78–87

defined, 78

determination, 78

See also Normal forms

DOMAINs

declaring, 52–53

uses, 52

DROP ASSERTION statement, 25

Dropping VIEWs, 389–90

DROP TABLE statement, 5, 6–7, 391

cascading, 390

defined, 6

syntax, 6

DROP VIEW statement, 389

<drop behavior>, 389–90

syntax, 389

Duplicate rows, 48–50

duplication degree, 49

fundamental relationships, 48–49

for loading data, 50

in results, 50

See also Rows

Duplicates

automatic removal, 142

current, 131, 133

defined, 131

interaction, 132

nonsequenced, 133

redundant, 217–19

removing with cursor, 216

result table, removing, 289

sequenced, 132, 134

temporal, 129–35

undesired, 142

value-equivalent, 132–33

Durability property, 722

Duration, pairs, 672–73

E Edges

costs of, 690 defined, 684 Elementary Key Normal Form (EKNF),

72–73 defined, 72 tables, 72, 73

See also Normal forms

ELSE clause, 431 ELSE NULL clause, 335–36 Enterprise-wide dimensional layer,

717–18 Equality determination, 602 proper subsets and, 602–3 row, 614

subset, 617–18 table, 613–18 tests, 589, 614 Equal (=) operator, 235 Errors, conceptual, 4 Exact division, 409 EXCEPT operator, 596–601 ALL option, 597, 615, 747 defined, 413

division with, 413–14 duplicate removal, 596 with NULLs and duplicates, 600– 601

result, 596 without NULLs and duplicates, 599–600

See also Set operators

Existential import, 312 EXISTS predicate, 216, 288, 299–308,

755 defined, 299 FALSE return, 307 indexes, 288, 299–300, 555

IN predicate conversion to, 301 nested, 410

Trang 5

NOT, 291–92, 302

NULLs and, 300–302

ORed list of, 297

quantifiers and, 304–5

referential constraints and, 305–6

speed, 302

syntax, 299

three-valued logic and, 306–8

TRUE return, 307

as two-value predicate, 308

EXP() function, 116

Exponential functions, 116

Exposed physical locators, 89–91

defined, 89

examples, 90

See also Keys

Expressions

on indexed columns, 746

logical, 739

simple, 734–38

string, 738

unnested queries and, 733–38

EXTRACT() function, 659, 665–66

Extrema functions, 449–65

ALL predicate and, 313–14

generalized, 451–60

GREATEST(), 117, 462–65

LEAST(), 117, 202, 462–65

MAX(), 439, 449

MIN(), 349, 449–50

multiple criteria, 460–62

simple, 449–51

F FETCH statement, 55–56, 194

FIFO subset, 421

Fifth Normal Form (5NF), 76–78

defined, 76

example problem, 77

ORM and, 78

See also Normal forms

File systems, 4

First Normal Form (1NF), 64–69 defined, 65

repeating groups and, 66–69 requirement, 65

See also Normal forms

Flattening tables, 93 into arrays, 585–87 with VIEWs, 393–95 Floating-point hardware, 236 FLOOR() function, 117, 663, 664 FOREIGN KEY constraint, 21 FORTRAN, 2, 4

Foundation Rule, 62 Four-function arithmetic, 108–9 Fourth Normal Form (4NF), 75–76 FROM clause

derived tables in, 395–97 SELECT statement, 321, 324 table subquery in, 433 FULL OUTER JOINs, 337, 349–50 defined, 349

illustrated, 351 use of, 350

See also OUTER JOINs Functions

ABS(), 473 AGE(), 658, 667

in aggregate functions, 348 AND, 475

ARCSIN(), 202 AVG(), 444–49, 512, 523 BIT_LENGTH(), 173 CAST(), 107, 457, 657 CEILING(), 117, 514–15 CHAR_LENGTH(), 173 COALESCE(), 39, 40, 111–13,

194, 251–52 COUNT(), 440–43, 548 current date, 129 DATE(), 665–66 DATE data type, 642–43

Trang 6

day of week, 129

DENSE_RANK, 711

distance, 201–2

EXP(), 116

EXTRACT(), 659, 665–66

extrema, 313–14, 449–65

FLOOR(), 117, 663, 664

GREATEST(), 117, 462–65

LEAST(), 117, 202, 462–65

LIST(), 465–68

LN(), 116, 471

LOG(), 116, 471

LOG10(), 116, 471

MAX(), 349, 449

MIN(), 349, 449–50

MOD(), 114–15, 472, 605

NULLIF(), 110–11, 193, 251–52,

473

NULLs and, 193–94

OCTET_LENGTH(), 173

OR, 474–75

partition by, 403–4

POSITION(), 173, 259, 540

POWER(), 116, 565

PRD(), 468–73

RANDOM(), 250, 608–10

RANK, 711

ROLLUP, 713, 716

ROUND(), 117

ROW_NUMBER, 711–12

SIGN(), 471, 475

SQRT(), 116, 547

SUBSTRING(), 172, 355, 467

SUM(), 107, 443–44

TRANSLATE(), 173

TRIM(), 173, 264, 467

TRUNCATE(), 117, 663, 664

user-defined, 659

XOR, 610

G GAAP (Generally Accepted Accounting

Practices), 206 Gaps

length, 648

in time series, 645–48 Geometric mean, 545–46 Georgian dates, converting, 663 Global constants tables, 506–7 Global temporary tables, 51, 392 Global Unique Identifiers (GUIDs),

41–42 defined, 41 generating, 41 indexes and, 42 interpretation problem, 42 storage requirement, 42 Graphs, 681–707

acyclic, 682, 695–97 adjacency matrix model, 705–6 all nodes, viewing, 682–83 characteristics, 682–86 defined, 623, 681 directed, 681 modeling, 682 nodes, 623 nodes, total number, 691 nonacyclic, 703–5 nonreconvergent, 681 paths, 683, 686–95 reconvergent, 681 Greater than (>) operator, 235 GREATEST() function, 117, 462–65 Grep() utility, 267, 268

GROUP BY clause, 425–27, 598, 616 correlated subqueries and, 456 execution order, 428

HAVING clause and, 427–29 partition basis, 425

removing, 748 SELECT statement, 319, 323 sorting by, 437–38

Trang 7

VIEWs and, 381, 432

GROUP BY query, 252–53

Grouped tables, 428

Grouped VIEWs, 374–75

Grouping operations, 425–38

GROUPING operators, 712–14

CUBES, 713–14

GROUPING SET, 712–13

ROLLUP, 713

Grouping set, 712–13

Groups

aggregation, 715–16

characteristics, 429–31

column, 425–26

on computed columns, 435–36

contiguous measurement, 651

CUBES, 713–14

empty, averages with, 446–48

forming, based on columns, 617

into pairs, 436–37

NULLs and, 427

repeating, 66–69

ROLLUP, 713

Guaranteed Access Rule, 62

H Harmonic mean, 545–46

Hashed indexes, 733

Hashing algorithms, 610

HAVING clause

aggregate functions and, 429

COUNT() function of, 459

GROUP BY clause and, 427–29

group characteristics and, 429–31

in grouped query, 524, 525

relational division, 416

Henderson’s median, 526–27

Hierarchical auxiliary tables, 490–91

constraints, 491

defined, 490

example, 490

See also Hierarchical auxiliary

tables Hierarchies class, modeling, 34–36 model choices, 639–40 nested set model, 631–39 Historical databases, 135 Host languages

INDICATOR variables in, 194 NULLs and, 194–95

Host programs initializing, 197 NULLs in, 197–98

I IDENTITY columns, 37–38

creating, 37–38 defined, 37 using, 38 Implicit cursors, 55 Indegree, 684–85 Indexed access, 732–33 Indexed columns, 746 Indexes

BETWEEN predicate and, 275 bit vector, 733

creating on strings, 271 EXISTS predicate, 288, 299–300 GUIDs and, 42

hashed, 733 ordering, 752–54 table, 740–42 too many, 740

as tree structures, 751 using, 751–52

VIEWs and, 383 Infixed joins, 352, 353, 354 Infixed notation, 352 Informational Rule, 62 Information Schema, 170 In-line text expansion, 380–82 INNER JOINs

Trang 8

EXISTS predicate and, 302–3

natural, 342

operators, 347, 352

query, 336

SELECT statement and, 327

See also JOINs

IN() predicate, 287–97

conversion to EXISTS predicates,

301

defined, 742

expanding, 294–95

with JOIN operation, 290

list of, 297

as membership test, 612

NOT, 291–92

NULLs and, 293–95

optimizing, 288–92, 742–44

referential constraints and, 295–96

replacing ORs with, 292–93

scalar queries and, 297

with subqueries, 288

syntax, 287

INSERT INTO statement, 221–23,

404, 644, 692

DEFAULT VALUES clause, 222

defined, 221

forms, 221

query, 221

syntax, 221

table constant, 221

VALUES clause, 221, 222

Insertions

nature of, 222

query, 221

table constant, 221

values list, 221

INSERT operation, 27

INTEGER numeric type, 102

Integer substrings, 485

Integrity constraints, 63, 631

Integrity Independence Rule, 63

Internal nodes, 686 Internal Rate of Return (IRR), 494 computational version, 504 computing, as straight line, 501–4 definition, 498

function, 497 negative, 498 International Telecommunications

Union (ITU), 122 Interpolation

with auxiliary function tables, 504–6

linear, 505 rules, 506 INTERSECT operator, 596–601 ALL option, 597, 747 behavior, 602

duplicate removal, 596 with NULLs and duplicates, 600– 601

result, 596 without NULLs and duplicates, 599–600

See also Set operators

INTERVAL data type, 123 Intervals

classes, 128 defined, 128 end point, 278 half-open, 141 overlapping, 279, 650

See also Time(s)

Inverse functions, with auxiliary tables,

495–504

IP addresses, storing, 202–5

IS [NOT] NORMALIZED predicate,

244–45 defined, 244 normal forms, 244–45

IS [NOT] TRUE | FALSE |

UNKNOWN predicate, 242–44

Trang 9

defined, 242

defining, 243

syntax, 242–43

IS NULL predicate, 241–42

defined, 241

examples, 242

extended version, 241–42

ISO-2711-1973, 124

ISO-8601, 122, 124

Isolated nodes, 685–86

Isolation levels, 724–26

CURSOR STABILITY, 726

initial three phenomena and, 725

READ COMMITTED, 725

READ UNCOMMITTED, 726

REPEATABLE READ, 725

SERIALIZABLE, 725

Isolation property, 721–22

Iterative loops, replacing, 483–85

J Join-Projection Normal Form See Fifth

Normal Form (5NF)

JOINs

conditions, 323

CROSS, 321–23, 338

division with, 412–13

execution order, 352–53

by function calls, 354–56

infixed, 352, 353, 354

INNER, 302–3, 327

LIKE predicate with, 264–66

NATURAL, 327

nonsequenced, 140

old vs new syntax, 351–53

OUTER, 303–4, 336–51

packing, 358–59

SELECT statement and, 317–36

sequenced, 140, 141

temporal, 139–45

T-join, 359–68

UNION, 350, 351, 356–57

in VIEWs, 377 Julian dates, 661–65 computing, 662 converting Georgian dates to, 663 Julianized dates vs., 662

See also Dates

Julianized dates, 662

K Karlsson, Anders, 364 Key generators, 127 Keys

artificial, 89 candidate, 34 classification system, 88 exposed physical locators, 89–91 long, 89

matching on, 614 natural, 89 nested, 22 overlapping, 22–25 primary, 22, 62 random-order, 45 sort, 328, 331 types, 88–99 unique, 127 unique sequential numbers for, 36–48

user-controlled, 91 VIEW, 371

Keywords ALL, 442, 443, 444 CREATE, 54 DECLARE, 54 DISTINCT, 443, 444–45 DROP, 7

L Leaf nodes

defined, 623 queries, 625 removing, 626

Trang 10

See also Nodes; Trees

Leap year

dates, 658

test, 676–77

year 2000, 676–77

LEAST() function, 117, 202, 462–65

LEFT OUTER JOINs, 337, 343

chain, 347

illustrated, 351

NATURAL, 344

See also OUTER JOINs

Less than (<) operator, 235

LIFO subset, 420, 421

LIKE predicate, 261–71, 629

avoiding with JOINs, 264–66

CASE expressions and, 266–67

defined, 261

empty string results, 264

<escape character>, 262

<match value>, 263

NULL value results, 264

<pattern>, 261, 262

pattern tricks, 262–64

syntax, 261

tricks with strings, 269–71

Linear congruence pseudo-random

number generators, 609–10

Linear regression, 548

LIST() function, 465–68

by crosstabs, 467–68

defined, 465

example, 465–66

with procedures, 466–67

syntax, 465

Lists

date, 644

enumerating, 479–81

numbers, condensing, 567

numbers, folding, 567–68

parsing, 68–69

sliding values in, 565–67

swapping values in, 565–67 Livelocks, 730

LN() function, 116, 471 Load utilities, 223 Local temporary tables, 51, 392 Locating dates, 656–58

Locks, 43, 44 LOG10() function, 116, 471 LOG() function, 116, 471 Logical concurrency control, 729 Logical Data Independence Rule, 63 Logical grouping, 716

Logical implication operator, 253 Lookup auxiliary tables, 485–93 for decoding, 494

hierarchical, 490–91 multiple parameter, 488–89 multiple translation, 487–88 OTLT, 491–93

range, 489–90 REFERENCES clause and, 296 translation, 487

See also Auxiliary tables

M Many-to-many relationships, 87 Martinis, Miljenko, 363

Materialization decision, 379 defined, 379 VIEW, 379–80 Matrix operations, 581–85 addition, 582–83 defined, 581 equality, 582 multiplication, 583–85 multiplication by column/row vector, 585

subtraction, 582–83 transposition, 585 MAX() function, 349, 449 correlated subquery with, 451

Ngày đăng: 06/07/2014, 09:20

TỪ KHÓA LIÊN QUAN