Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy...

23
Joe Celko’s SQL for Smarties Fourth Edition

Transcript of Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy...

Page 1: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

Joe Celko ’s SQL for Smar t ies

Fourth Edition

Page 2: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

Joe Celko’s Data, Measurements and Standards in SQL Joe Celko

Information Modeling and Relational Databases, 2 nd Edition Terry Halpin, Tony Morgan

Joe Celko’s Thinking in Sets Joe Celko

Business Metadata Bill Inmon, Bonnie O’Neil, Lowell Fryman

Unleashing Web 2.0 Gottfried Vossen, Stephan Hagemann

Enterprise Knowledge Management David Loshin

Business Process Change, 2 nd Edition Paul Harmon

IT Manager’s Handbook, 2 nd Edition Bill Holtsnider & Brian Jaffe

Joe Celko’s Puzzles and Answers, 2 nd Edition Joe Celko

Making Shoes for the Cobbler’s Children Charles Betz

Joe Celko’s Analytics and OLAP in SQL Joe Celko

Data Preparation for Data Mining Using SAS Mamdouh Refaat

Querying XML: XQuery, XPath, and SQL/XML in Context Jim Melton and Stephen Buxton

Data Mining: Concepts and Techniques, 2 nd Edition Jiawei Han and Micheline Kamber

Database Modeling and Design: Logical Design, 4 th Edition Toby J, Teorey, Sam S. Lightstone, Thomas P. Nadeau

Foundations of Multidimensional and Metric Data Structures Hanan Samet

Joe Celko’s SQL for Smarties: Advanced SQL Programming, 4 th Edition Joe Celko

Moving Objects Databases Ralf Hartmut Güting and Markus Schneider

Joe Celko’s SQL Programming Style Joe Celko

Data Mining, Second Edition: Concepts and Techniques Jiawei Han, Micheline Kamber, Jian pei

Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration Earl Cox

Data Modeling Essentials, 3 rd Edition Graeme C. Simsion and Graham C. Witt

Location-Based Services Jochen Schiller and Agnès Voisard

Managing Time in Relational Databases: How to Design, Update and Query Temporal Data Tom Johnston and Randall Weis

Database Modeling with Microsoft® Visio for Enterprise Architects Terry Halpin, Ken Evans, Patrick Hallock, Bill Maclean

Designing Data-Intensive Web Applications Stephano Ceri, Piero Fraternali, Aldo Bongio, Marco Brambilla, Sara Comai, Maristella Matera

Mining the Web: Discovering Knowledge from Hypertext Data Soumen Chakrabarti

Advanced SQL: 1999—Understanding Object-Relational and Other Advanced Features Jim Melton

Database Tuning: Principles, Experiments, and Troubleshooting Techniques Dennis Shasha, Philippe Bonnet

SQL:1999—Understanding Relational Language Components Jim Melton, Alan R. Simon

Information Visualization in Data Mining and Knowledge Discovery Edited by Usama Fayyad, Georges G. Grinstein, Andreas Wierse

Transactional Information Systems Gerhard Weikum and Gottfried Vossen

Spatial Databases Philippe Rigaux, Michel Scholl, and Agnes Voisard

Managing Reference Data in Enterprise Database Malcolm Chisholm

Understanding SQL and Java Together Jim Melton and Andrew Eisenberg

Database: Principles, Programming, and Performance, 2 nd Edition Patrick and Elizabeth O’Neil

The Object Data Standar Edited by R. G. G. Cattell, Douglas Barry

Data on the Web: From Relations to Semistructured Data and XML Serge Abiteboul, Peter Buneman, Dan Suciu

Data Mining, Third Edition Practical Machine Learning Tools and Techniques with Java Implementations Ian Witten, Eibe Frank

Joe Celko’s Data and Databases: Concepts in Practice Joe Celko

Developing Time-Oriented Database Applications in SQL Richard T. Snodgrass

Web Farming for the Data Warehouse Richard D. Hackathorn

Management of Heterogeneous and Autonomous Database Systems Edited by Ahmed Elmagarmid, Marek Rusinkiewicz, Amit Sheth

Object-Relational DBMSs: 2 nd Edition Michael Stonebraker and Paul Brown, with Dorothy Moore

Universal Database Management: A Guide to Object/Relational Technology Cynthia Maro Saracco

Readings in Database Systems, 3 rd Edition Edited by Michael Stonebraker, Joseph M. Hellerstein

Understanding SQL’s Stored Procedures: A Complete Guide to SQL/PSM Jim Melton

Principles of Multimedia Database Systems V. S. Subrahmanian

Principles of Database Query Processing for Advanced Applications Clement T. Yu, Weiyi Meng

Advanced Database Systems Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V. S. Subrahmanian, Roberto Zicari

Principles of Transaction Processing, 2 nd Edition Philip A. Bernstein, Eric Newcomer

Using the New DB2: IBMs Object-Relational Database System Don Chamberlin

Distributed Algorithms Nancy A. Lynch

Active Database Systems: Triggers and Rules For Advanced Database Processing Edited by Jennifer Widom, Stefano Ceri

Migrating Legacy Systems: Gateways, Interfaces, & the Incremental Approach Michael L. Brodie, Michael Stonebraker

Atomic Transactions Nancy Lynch, Michael Merritt, William Weihl, Alan Fekete

Query Processing for Advanced Database Systems Edited by Johann Christoph Freytag, David Maier, Gottfried Vossen

Transaction Processing Jim Gray, Andreas Reuter

Database Transaction Models for Advanced Applications Edited by Ahmed K. Elmagarmid

A Guide to Developing Client/Server SQL Applications Setrag Khoshafi an, Arvola Chan, Anna Wong, Harry K. T. Wong

The Morgan Kaufmann Series in Data Management Systems (Selected Titles)

Page 3: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

Joe Celko ’s SQL for Smar t ies Advanced SQL Programming

Fourth Edition

Joe Celko

AMSTERDAM • BOSTON • HEIDELBERG • LONDONNEW YORK • OXFORD • PARIS • SAN DIEGO

SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYOMorgan Kaufmann is an imprint of Elsevier

Page 4: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

Acquiring Editor: Rick Adams Development Editor: David Bevans Project Manager: Sarah Binns Designer: Joanne Blank

Morgan Kaufmann is an imprint of Elsevier 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA

© 2011 Elsevier Inc. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without permission in writing from the Publisher. Details on how to seek permission, further information about the Publisher’s permissions policies and our arrangements with organizations such as the Copyright Clearance Center and the Copyright Licensing Agency, can be found at our website: www.elsevier.com/permissions .

This book and the individual contributions contained in it are protected under copyright by the Publisher (other than as may be noted herein).

Notices Knowledge and best practice in this fi eld are constantly changing. As new research and experience broaden our understanding, changes in research methods or professional practices may become necessary. Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using any information or methods described herein. In using such information or methods they should be mindful of their own safety and the safety of others, including parties for whom they have a professional responsibility.

To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors assume any liability for any injury and/or damage to persons or property as a matter of product liability, negligence or otherwise, or from any use or operation of any methods, products, instructions, or ideas contained in the material herein.

Library of Congress Cataloging-in-Publication Data Application submitted.

British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library.

ISBN: 978-0-12-382022-8

Printed in the United States of America

10 11 12 13 14 10 9 8 7 6 5 4 3 2 1

Typeset by : diacriTech, Chennai, India

For information on all MK publications visit our website at www.mkp.com .

Page 5: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

To Ann and Jackers

Page 6: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced
Page 7: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

CONTENTS

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

Introduction to the Fourth Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

Chapter 1 Databases versus File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1 Tables as Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.2 Tables as Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.3 Rows versus Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.4 Columns versus Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.5 Schema Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.6 CREATE SCHEMA Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Chapter 2 Transactions and Concurrency Control . . . . . . . . . . . . . . . . . . . . 11

2.1 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11

2.2 Transactions and ACID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.3 Concurrency Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.4 Pessimistic Concurrency Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.5 SNAPSHOT Isolation and Optimistic Concurrency . . . . . . . . . . . . . . .19

2.6 Logical Concurrency Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.7 Deadlock and Livelocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Chapter 3 Schema Level Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.1 CREATE SCHEMA Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.2 CREATE PROCEDURE, CREATE FUNCTION, and

CREATE TRIGGER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3.3 CREATE DOMAIN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3.4 CREATE SEQUENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

3.5 CREATE ASSERTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.6 Character Set Related Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Chapter 4 Locating Data and Special Numbers . . . . . . . . . . . . . . . . . . . . . . 35

4.1 Exposed Physical Locators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

4.2 Generated Identifi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

4.3 Sequence Generator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

4.4 Preallocated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

4.5 Special Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

CONTENTS vii

Page 8: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

viii CONTENTS

Chapter 5 Base Tables and Related Elements . . . . . . . . . . . . . . . . . . . . . . . . 51

5.1 CREATE TABLE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

5.2 Nested UNIQUE Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

5.3 CREATE ASSERTION Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

5.4 TEMPORARY Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

5.5 Manipulating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

5.6 Avoiding Attribute Splitting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

5.7 Modeling Class Hierarchies in DDL . . . . . . . . . . . . . . . . . . . . . . . . . . 83

5.8 Exposed Physical Locators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

5.9 Auto-Incrementing Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

5.10 Generated Identifi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

5.11 A Remark on Duplicate Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

5.12 Other Schema Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

5.13 Temporary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

5.14 CREATE DOMAIN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

5.15 CREATE TRIGGER Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

5.16 CREATE PROCEDURE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

5.17 DECLARE CURSOR Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Chapter 6 Procedural, Semiprocedural, and

Declarative Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

6.1 Basics of Software Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105

6.2 Cohesion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105

6.3 Coupling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106

6.4 The Big Leap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107

6.5 Rewriting Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114

6.6 Functions for Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118

6.7 Procedural versus Logical Decomposition . . . . . . . . . . . . . . . . . . . . 119

Chapter 7 Procedural Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

7.1 CREATE PROCEDURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

7.2 CREATE TRIGGER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

7.3 CURSORs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

7.4 SEQUENCEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

7.5 Generated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

7.6 Table Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Page 9: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

CONTENTS ix

Chapter 8 Auxiliary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

8.1 The Series Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

8.2 Lookup Auxiliary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

8.3 Auxiliary Function Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

8.4 Global Constants Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

8.5 A Note on Converting Procedural

Code to Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

Chapter 9 Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

9.1 Functional and Multivalued Dependencies . . . . . . . . . . . . . . . . . . . 183

9.2 First Normal Form (1NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

9.3 Second Normal Form (2NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

9.4 Third Normal Form (3NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

9.5 Elementary Key Normal Form (EKNF) . . . . . . . . . . . . . . . . . . . . . . . .191

9.6 Boyce-Codd Normal Form (BCNF) . . . . . . . . . . . . . . . . . . . . . . . . . . .192

9.7 Fourth Normal Form (4NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194

9.8 Fifth Normal Form (5NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194

9.9 Domain-Key Normal Form (DKNF) . . . . . . . . . . . . . . . . . . . . . . . . . . .196

9.10 Practical Hints for Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . 204

9.11 Key Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

9.12 Practical Hints for Denormalization . . . . . . . . . . . . . . . . . . . . . . . . 208

Chapter 10 Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

10.1 Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

10.2 Numeric Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

10.3 Four Function Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

10.4 Arithmetic and NULL s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

10.5 Converting Values to and from NULL . . . . . . . . . . . . . . . . . . . . . . . 225

10.6 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

10.7 Unique Value Generators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

10.8 IP Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

Chapter 11 Temporal Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

11.1 Notes on Calendar Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

11.2 SQL Temporal Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

11.3 INTERVAL Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

Page 10: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

x CONTENTS

11.4 Temporal Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

11.5 The Nature of Temporal Data Models . . . . . . . . . . . . . . . . . . . . . . . 250

Chapter 12 Character Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

12.1 Problems with SQL Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

12.2 Standard String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

12.3 Common Vendor Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

12.4 Cutter Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268

12.5 Nested Replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

Chapter 13 NULLs: Missing Data in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

13.1 Empty and Missing Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

13.2 Missing Values in Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

13.3 Context and Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

13.4 Comparing NULL s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

13.5 NULLs and Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

13.6 Math and NULL s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

13.7 Functions and NULL s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

13.8 NULL s and Host Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

13.9 Design Advice for NULL s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

13.10 A Note on Multiple NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . 285

Chapter 14 Multiple Column Data Elements . . . . . . . . . . . . . . . . . . . . . . . . . 289

14.1 Distance Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

14.2 Storing an IPv4 Address in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

14.3 Storing an IPv6 Address in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

14.4 Currency and Other Unit Conversions . . . . . . . . . . . . . . . . . . . . . . 294

14.5 Social Security Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

14.6 Rational Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

Chapter 15 Table Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

15.1 DELETE FROM Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

15.2 INSERT INTO Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

15.3 The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

15.4 A Note on Flaws in a Common Vendor Extension . . . . . . . . . . . . 317

15.5 MERGE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

Page 11: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

CONTENTS xi

Chapter 16 Comparison or Theta Operators . . . . . . . . . . . . . . . . . . . . . . . . . 323

16.1 Converting Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

16.2 Row Comparisons in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

16.3 IS [NOT] DISTINCT FROM Operator . . . . . . . . . . . . . . . . . . . . . . 328

Chapter 17 Valued Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

17.1 IS NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

17.2 IS [NOT]{TRUE | FALSE | UNKNOWN} Predicate . . . . . . . . . . . 330

17.3 IS [NOT] NORMALIZED Predicate . . . . . . . . . . . . . . . . . . . . . . . . . 332

Chapter 18 CASE Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

18.1 The CASE Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

18.2 Subquery Expressions and Constants . . . . . . . . . . . . . . . . . . . . . . 341

18.3 Rozenshtein Characteristic Functions . . . . . . . . . . . . . . . . . . . . . . 342

Chapter 19 LIKE and SIMILAR TO Predicates . . . . . . . . . . . . . . . . . . . . 345

19.1 Tricks with Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346

19.2 Results with NULL Values and Empty Strings . . . . . . . . . . . . . . . . 347

19.3 LIKE Is Not Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

19.4 Avoiding the LIKE Predicate with a Join . . . . . . . . . . . . . . . . . . . . 348

19.5 CASE Expressions and LIKE Search Conditions . . . . . . . . . . . . . . 349

19.6 SIMILAR TO Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350

19.7 Tricks with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

Chapter 20 BETWEEN and OVERLAPSPredicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

20.1 The BETWEEN Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

20.2 OVERLAPS Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

Chapter 21 The [NOT] IN() Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

21.1 Optimizing the IN() Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

21.2 Replacing ORs with the IN() Predicate . . . . . . . . . . . . . . . . . . . . . 373

21.3 NULLs and the IN() Predicate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

21.4 IN() Predicate and Referential Constraints . . . . . . . . . . . . . . . . . . 376

21.5 IN() Predicate and Scalar Queries . . . . . . . . . . . . . . . . . . . . . . . . . 377

Page 12: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

xii CONTENTS

Chapter 22 EXISTS() Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

22.1 EXISTS and NULLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382

22.2 EXISTS and INNER JOINs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

22.3 NOT EXISTS and OUTER JOINs . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

22.4 EXISTS() and Quantifi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

22.5 EXISTS() and Referential Constraints . . . . . . . . . . . . . . . . . . . . . 386

22.6 EXISTS and Three-Valued Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

Chapter 23 Quantifi ed Subquery Predicates . . . . . . . . . . . . . . . . . . . . . . . . 389

23.1 Scalar Subquery Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

23.2 Quantifi ers and Missing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

23.3 The ALL Predicate and Extrema Functions . . . . . . . . . . . . . . . . . . 393

23.4 The UNIQUE Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

23.5 The DISTINCT Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

Chapter 24 The Simple SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 397

24.1 SELECT Statement Execution Order . . . . . . . . . . . . . . . . . . . . . . . . 397

24.2 One-Level SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

Chapter 25 Advanced SELECT Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 407

25.1 Correlated Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

25.2 Infi xed INNER JOINs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

25.3 OUTER JOINs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

25.4 UNION JOIN Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

25.5 Scalar SELECT Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

25.6 Old versus New JOIN Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

25.7 Constrained JOINs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428

25.8 Dr. Codd’s T-Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

Chapter 26 Virtual Tables: VIEWs, Derived Tables, CTEs,

and MQTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

26.1 VIEWs in Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

26.2 Updatable and Read-Only VIEWs . . . . . . . . . . . . . . . . . . . . . . . . . . 446

26.3 Types of VIEWs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448

26.4 How VIEWs Are Handled in the Database Engine . . . . . . . . . . . . . 453

26.5 WITH CHECK OPTION Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457

Page 13: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

CONTENTS xiii

26.6 Dropping VIEWs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462

26.7 Hints on Using VIEWs versus TEMPORARY TABLEs . . . . . . . . . . . . 463

26.8 Using Derived Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466

26.9 Common Table Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468

26.10 Recursive Common Table Expressions . . . . . . . . . . . . . . . . . . . . . 469

26.11 Materialized Query Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472

Chapter 27 Partitioning Data in Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

27.1 Coverings and Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

27.2 Relational Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478

27.3 Romley’s Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

27.4 Boolean Expressions in an RDBMS . . . . . . . . . . . . . . . . . . . . . . . . 489

27.5 FIFO and LIFO Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490

Chapter 28 Grouping Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

28.1 GROUP BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

28.2 GROUP BY and HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495

28.3 Multiple Aggregation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

28.4 Grouping on Computed Columns . . . . . . . . . . . . . . . . . . . . . . . . . 501

28.5 Grouping into Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502

28.6 Sorting and GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504

Chapter 29 Simple Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

29.1 COUNT() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

29.2 SUM() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

29.3 AVG() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512

29.4 Extrema Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

29.5 The LIST() Aggregate Function . . . . . . . . . . . . . . . . . . . . . . . . . . 530

29.6 The PRD() Aggregate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 532

29.7 Bitwise Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536

Chapter 30 Advanced Grouping, Windowed Aggregation,

and OLAP in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539

30.1 Star Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540

30.2 GROUPING Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540

30.3 The Window Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544

30.4 Windowed Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 547

Page 14: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

xiv CONTENTS

30.5 Ordinal Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547

30.6 Vendor Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550

30.7 A Bit of History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553

Chapter 31 Descriptive Statistics in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

31.1 The Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

31.2 The AVG() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556

31.3 The Median . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557

31.4 Variance and Standard Deviation . . . . . . . . . . . . . . . . . . . . . . . . . . 572

31.5 Average Deviation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573

31.6 Cumulative Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573

31.7 Cross Tabulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582

31.8 Harmonic Mean and Geometric Mean . . . . . . . . . . . . . . . . . . . . . . 588

31.9 Multivariable Descriptive Statistics in SQL . . . . . . . . . . . . . . . . . . 589

31.10 Statistical Functions in SQL:2006 . . . . . . . . . . . . . . . . . . . . . . . . . 591

Chapter 32 Subsequences, Regions, Runs, Gaps,

and Islands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595

32.1 Finding Subregions of Size (n) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596

32.2 Numbering Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597

32.3 Finding Regions of Maximum Size. . . . . . . . . . . . . . . . . . . . . . . . . 598

32.4 Bound Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602

32.5 Run and Sequence Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603

32.6 Summation of a Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607

32.7 Swapping and Sliding Values in a List . . . . . . . . . . . . . . . . . . . . . . 610

32.8 Condensing a List of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612

32.9 Folding a List of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612

32.10 Coverings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613

Chapter 33 Matrices in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617

33.1 Arrays via Named Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617

33.2 Arrays via Subscript Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621

33.3 Matrix Operations in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622

33.4 Flattening a Table into an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627

33.5 Comparing Arrays in Table Format . . . . . . . . . . . . . . . . . . . . . . . . . 628

Page 15: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

CONTENTS xv

Chapter 34 Set Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631

34.1 UNION and UNION ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632

34.2 INTERSECT and EXCEPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635

34.3 A Note on ALL and SELECT DISTINCT . . . . . . . . . . . . . . . . . . . . . 640

34.4 Equality and Proper Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640

Chapter 35 Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643

35.1 Every N-th Item in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643

35.2 Random Rows from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644

35.3 The CONTAINS Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649

35.4 Gaps in a Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654

35.5 Covering for Overlapping Intervals . . . . . . . . . . . . . . . . . . . . . . . . 656

35.6 Picking a Representative Subset . . . . . . . . . . . . . . . . . . . . . . . . . . . 659

Chapter 36 Trees and Hierarchies in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 665

36.1 Adjacency List Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666

36.2 The Path Enumeration Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670

36.3 Nested Set Model of Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . 673

36.4 Other Models for Trees and Hierarchies . . . . . . . . . . . . . . . . . . . . . 680

Chapter 37 Graphs in SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681

37.1 Adjacency List Model Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682

37.2 Split Node Nested Set Models

for Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693

37.3 Points inside Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698

37.4 Graph Theory References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700

Chapter 38 Temporal Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701

38.1 Temporal Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701

38.2 Personal Calendars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703

38.3 Time Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704

38.4 Julian Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719

38.5 Other Temporal Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722

38.6 Weeks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723

38.7 Modeling Time in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726

Page 16: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

38.8 Calendar Auxiliary Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729

38.9 Problems with the Year 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731

Chapter 39 Optimizing SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737

39.1 Access Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738

39.2 How to Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740

39.3 Give Extra Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744

39.4 Index Multiple Columns Carefully . . . . . . . . . . . . . . . . . . . . . . . . . 745

39.5 Watch the IN Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746

39.6 Avoid UNIONs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748

39.7 Prefer Joins over Nested Queries . . . . . . . . . . . . . . . . . . . . . . . . . . 748

39.8 Use Fewer Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749

39.9 Avoid Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750

39.10 Avoid CROSS JOINs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

39.11 Know Your Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

39.12 Recompile Static SQL after Schema Changes . . . . . . . . . . . . . . . 756

39.13 Temporary Tables Are Sometimes Handy . . . . . . . . . . . . . . . . . . 757

39.14 Update Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759

39.15 Do Not Trust Newer Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

General References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

Mathematical Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

Scales and Measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764

Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765

Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765

Graph Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

Introductory SQL Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

Optimizing Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767

Temporal Data and the Year 2000 Problem . . . . . . . . . . . . . . . . . . . . . . . 767

SQL Programming Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

Classics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

Updatable Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769

Theory, Normalization, and Advanced Database Topics . . . . . . . . . . . . 769

xvi CONTENTS

Page 17: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

CONTENTS xvii

Books on SQL-92 and SQL-99 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

Standards and Related Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

Web Sites Related to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771

Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771

Temporal Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771

Miscellaneous Citations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775

Page 18: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced
Page 19: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

ABOUT THE AUTHOR xix

About the Author Joe Celko served 10 years on ANSI/ISO SQL Standards Committee and contributed to the SQL-89 and SQL-92 Standards.

He has written over 900 columns in the computer trade and academic press, mostly dealing with data and databases, and has authored seven other books on SQL for Morgan Kaufmann: • SQL for Smarties (1995, 1999, 2005, 2010) • SQL Puzzles and Answers (1997, 2006) • Data and Databases (1999) • Trees and Hierarchies in SQL (2004) • SQL Programming Style (2005) • Analytics and OLAP in SQL (2005) • Thinking in Sets (2008)

Mr. Celko’s past columns include: • Columns for Simple Talk (Redgate Software) • “CELKO,” Intelligent Enterprise magazine (CMP) • BMC’s DBAzine.com e-magazine (BMC Software) • “SQL Explorer,” DBMS (Miller Freeman) • “Celko on SQL,” Database Programming and Design (Miller

Freeman) • “WATCOM SQL Corner,” Powerbuilder Developers’ Journal

(SysCon) • “SQL Puzzle,” Boxes and Arrows (Frank Sweet Publishing) • “DBMS/Report,” Systems Integration (Cahner Ziff) “Data Desk,”

Tech Specialist (R&D) • “Data Points,” PC Techniques (Coriolis Group) • “Celko on Software,” Computing (VNC Publications, UK ) • “SELECT * FROM Austin” (Array Publications, The Netherlands)

In addition, Mr. Celko was editor for the “Puzzles & Problems” section of ABACUS (SpringerVerlag) and he ran the CASEFORUM section 18, “Celko on SQL,” on CompuServe.

Page 20: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced
Page 21: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

INTRODUCTION TO THE FOURTH EDITION xxi

INTRODUCTION TO THE FOURTH EDITION

This book, like the fi rst, second, and third editions before it, is for the working SQL programmer who wants to pick up some advanced programming tips and techniques. It assumes that the reader is an SQL programmer with a year or more of actual experience. This is not an introductory book, so let’s not have any gripes in the amazon.com reviews about that like we did with the prior editions.

The fi rst edition was published 10 years ago, and became a minor classic among working SQL programmers. I have seen copies of this book on the desks of real programmers in real pro-gramming shops almost everywhere I have been. The true com-pliment are the Post-it® notes sticking out of the top. People really use it often enough to put stickies in it! Wow!

What Changed in Ten Years Hierarchical and network databases still run vital legacy systems in major corporations. SQL people do not like to admit that IMS and traditional fi les are still out there in the Fortune 500. But SQL people can be proud of the gains SQL-based systems have made over the decades. We have all the new applications and all the important smaller databases.

OO programming is fi rmly in place, but may give ground to functional programming in the next decade. Object and object-relational databases found niche markets, but never caught on with the mainstream.

XML is no longer a fad in 2010. Technically, it is syntax for describing and moving data from one platform to another, but its support tools allow searching and reformatting. There is an SQL/XML subcommittee in INCITS H2 (the current name of the original ANSI X3H2 Database Standards Committee) making sure they can work together.

Data warehousing is no longer an exotic luxury only for major corporations. Thanks to the declining prices of hardware and software, medium-sized companies now use the technology. Writing OLAP queries is different from OLTP queries and prob-ably needs its own “Smarties” book now.

Page 22: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

xxii INTRODUCTION TO THE FOURTH EDITION

Open Source databases are doing quite well and are gaining more and more Standards conformance. The LAMP platform (Linux, Apache, MySQL, and Python/PHP) has most of the web sites. Ingres, Postgres, Firebird, and other products have the ANSI SQL-92 features, most of the SQL-99, and some of the SQL:2003 features.

Columnar databases, parallelism, and Optimistic Concur rency are all showing up in commercial product instead of the labora-tory. The SQL Standards have changed over time, but not always for the better. Parts of it have become more relational and set- oriented while other parts put in things that clearly are proce-dural, deal with nonrelational data, and are based on fi le system models. To quote David McGoveran, “A committee never met a feature it did not like.” And he seems to be quite right.

But with all the turmoil the ANSI/ISO Standard SQL-92 was the common subset that will port across SQL products to do use-ful work. In fact, years ago, the US government described the SQL-99 standard as “a standard in progress” and required SQL-92 conformance for federal contracts.

We had the FIPS-127 conformance test suite in place during the development of SQL-92, so all the vendors could move in the same direction. Unfortunately, the Clinton administration canceled the program and conformance began to drift. Michael M. Gorman, President of Whitemarsh Information Systems Corporation and secretary of INCITS H2 for over 20 years, has a great essay on this and other political aspects of SQL’s history at Wiscorp.com that is worth reading.

Today, the SQL-99 standard is the one to use for portable code on the greatest number of platforms. But vendors are adding SQL:2003 features so rapidly, I do not feel that I have to stick to a minimal standard.

New in This Edition In the second edition, I dropped some of the theory from the book and moved it to Data and Databases (ISBN 13:978-1558604322). I fi nd no reason to add it back into this edition.

I have moved and greatly expanded techniques for trees and hierarchies into their own book ( Trees and Hierarchies in SQL , ISBN 13:978-1558609204) because there was enough material to justify it. There is a short mention of some techniques here, but not to the detailed level in the other book.

I put programming tips for newbies into their own book ( SQL Programming Style , ISBN 13:978-0120887972) because this book

Page 23: Joe Celko’s SQL for Smarties - Elsevier · PDF fileJoe Celko’s Data, ... Fuzzy Modeling and Genetic Algorithms for Data Mining and Exploration ... Relational and Other Advanced

INTRODUCTION TO THE FOURTH EDITION xxiii

is an advanced programmer’s book and I assume that the reader is now writing real SQL, not some dialect or his or her native programming language in a thin disguise. I also assume that the reader can translate Standard SQL into his or her local dialect without much effort.

I have tried to provide comments with the solutions, to explain why they work. I hope this will help the reader see under-lying principles that can be used in other situations.

A lot of people have contributed material, either directly or via Newsgroups and I cannot thank all of them. But I made a real effort to put names in the text next to the code. In case I missed anyone, I got material or ideas from Aaron Bertrand, Alejandro Mesa, Anith Sen, Craig Mullins (who has done the tech reads on several editions), Daniel A. Morgan, David Portas, David Cressey, Dawn M. Wolthuis, Don Burleson, Erland Sommarskog, Itzak Ben-Gan, John Gilson, Knut Stolze, Ken Henderson, Louis Davidson, Dan Guzman, Hugo Kornelis, Richard Romley, Serge Rielau, Steve Kass, Tom Moreau, Troels Arvin, Vadim Tropashko, Plamen Ratchev, Gert-Jan Strik, and probably a dozen others I am forgetting.

Corrections and Additions Please send any corrections, additions, suggestions, improvements, or alternative solutions to me or to the publisher. Especially if you have a better way of doing something.

www.mkp.com