Joe Celko's SQL for Smarties
Total Page:16
File Type:pdf, Size:1020Kb
Joe Celko’s SQL for Smarties Fourth Edition The Morgan Kaufmann Series in Data Management Systems (Selected Titles) Joe Celko’s Data, Measurements and Location-Based Services Web Farming for the Data Warehouse Standards in SQL Jochen Schiller and Agnès Voisard Richard D. Hackathorn Joe Celko Managing Time in Relational Databases: Management of Heterogeneous and Information Modeling and Relational How to Design, Update and Query Autonomous Database Systems Databases, 2 nd Edition Temporal Data Edited by Ahmed Elmagarmid, Marek Terry Halpin, Tony Morgan Tom Johnston and Randall Weis Rusinkiewicz, Amit Sheth Joe Celko’s Thinking in Sets Database Modeling with Microsoft® Visio Object-Relational DBMSs: 2 nd Edition Joe Celko for Enterprise Architects Michael Stonebraker and Paul Brown, Business Metadata Terry Halpin, Ken Evans, Patrick Hallock, with Bill Inmon, Bonnie O’Neil, Lowell Fryman Bill Maclean Dorothy Moore Unleashing Web 2.0 Designing Data-Intensive Web Applications Universal Database Management: A Guide Gottfried Vossen, Stephan Hagemann Stephano Ceri, Piero Fraternali, Aldo to Object/Relational Technology Bongio, Marco Brambilla, Sara Comai, Cynthia Maro Saracco Enterprise Knowledge Management Maristella Matera rd David Loshin Readings in Database Systems, 3 Edition Mining the Web: Discovering Knowledge Edited by Michael Stonebraker, Joseph M. nd Business Process Change, 2 Edition from Hypertext Data Hellerstein Paul Harmon Soumen Chakrabarti Understanding SQL’s Stored Procedures: nd IT Manager’s Handbook, 2 Edition Advanced SQL: 1999—Understanding Object- A Complete Guide to SQL/PSM Bill Holtsnider & Brian Jaffe Relational and Other Advanced Features Jim Melton Joe Celko’s Puzzles and Answers, Jim Melton Principles of Multimedia Database Systems nd 2 Edition Database Tuning: Principles, Experiments, V. S. Subrahmanian Joe Celko and Troubleshooting Techniques Principles of Database Query Processing Making Shoes for the Cobbler’s Children Dennis Shasha, Philippe Bonnet for Advanced Applications Charles Betz SQL:1999—Understanding Relational Clement T. Yu, Weiyi Meng Joe Celko’s Analytics and OLAP in SQL Language Components Advanced Database Systems Joe Celko Jim Melton, Alan R. Simon Carlo Zaniolo, Stefano Ceri, Christos Data Preparation for Data Mining Using Information Visualization in Data Mining Faloutsos, Richard T. Snodgrass, V. S. SAS and Knowledge Discovery Subrahmanian, Roberto Zicari Mamdouh Refaat Edited by Usama Fayyad, Georges G. Principles of Transaction Processing, Querying XML: XQuery, XPath, and SQL/ Grinstein, Andreas Wierse 2 nd Edition XML in Context Transactional Information Systems Philip A. Bernstein, Eric Newcomer Jim Melton and Stephen Buxton Gerhard Weikum and Gottfried Vossen Using the New DB2: IBMs Object- Data Mining: Concepts and Techniques, Spatial Databases Relational Database System 2 nd Edition Philippe Rigaux, Michel Scholl, and Agnes Don Chamberlin Jiawei Han and Micheline Kamber Voisard Distributed Algorithms Database Modeling and Design: Logical Managing Reference Data in Enterprise Nancy A. Lynch th Design, 4 Edition Database Active Database Systems: Triggers and Toby J, Teorey, Sam S. Lightstone, Thomas Malcolm Chisholm Rules For Advanced Database Processing P. Nadeau Understanding SQL and Java Together Edited by Jennifer Widom, Stefano Ceri Foundations of Multidimensional and Jim Melton and Andrew Eisenberg Migrating Legacy Systems: Gateways, Metric Data Structures Database: Principles, Programming, and Interfaces, & the Incremental Approach Hanan Samet Performance, 2nd Edition Michael L. Brodie, Michael Stonebraker Joe Celko’s SQL for Smarties: Advanced Patrick and Elizabeth O’Neil Atomic Transactions th SQL Programming, 4 Edition The Object Data Standar Nancy Lynch, Michael Merritt, William Joe Celko Edited by R. G. G. Cattell, Douglas Barry Weihl, Alan Fekete Moving Objects Databases Data on the Web: From Relations to Query Processing for Advanced Database Ralf Hartmut Güting and Markus Semistructured Data and XML Systems Schneider Serge Abiteboul, Peter Buneman, Dan Suciu Edited by Johann Christoph Freytag, Joe Celko’s SQL Programming Style Data Mining, Third Edition Practical David Maier, Gottfried Vossen Joe Celko Machine Learning Tools and Techniques Transaction Processing Data Mining, Second Edition: Concepts with Java Implementations Jim Gray, Andreas Reuter and Techniques Ian Witten, Eibe Frank Database Transaction Models for Jiawei Han, Micheline Kamber, Jian pei Joe Celko’s Data and Databases: Concepts Advanced Applications Fuzzy Modeling and Genetic Algorithms in Practice Edited by Ahmed K. Elmagarmid for Data Mining and Exploration Joe Celko A Guide to Developing Client/Server SQL Earl Cox Developing Time-Oriented Database Applications Data Modeling Essentials, 3rd Edition Applications in SQL Setrag Khoshafi an, Arvola Chan, Anna Graeme C. Simsion and Graham C. Witt Richard T. Snodgrass Wong, Harry K. T. Wong Joe Celko’s SQL for Smarties Advanced SQL Programming Fourth Edition Joe Celko AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Morgan Kaufmann is an imprint of Elsevier 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 . To Ann and Jackers CONTENTS vii 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 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 CONTENTS ix Chapter 8 Auxiliary Tables . ..