PostgreSQL Introduction and Concepts PostgreSQL Introduction and Concepts Bruce Momjian ADDISON–WESLEY Boston San Francisco New York Toronto Montreal London Munich Paris Madrid Cape Town Sidney Tokyo Singapore Mexico City Many of the designations used bymanufacturers and sellerstodistinguish their products areclaimed as trademarks. Where those designations appear in this book, and we were aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers discounts on this book when ordered in quantity for special sales. For more information, please contact: Pearson Education Corporate Sales Division One Lake Street Upper Saddle River, NJ 07458 (800) 382-3419 corpsales@pearsontechgroup.com Visit AW on the Web: www.awl.com/cseng/ Copyright © 2001 by Addison–Wesley. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada. Library of Congress Cataloging-in-Publication Data Momjian, Bruce. PostgreSQL : introduction and concepts / Momjian, Bruce. p. cm. ISBN 0-201-70331-9 1. Database management. 2. PostgreSQL. I. Title. QA76.9.D3 M647 2000 005.75’85–dc21 00-045367 CIP This book was prepared with L Y X and L A T E X and reproduced by Addison–Wesley from files supplied by the author. Text printed on recycled and acid-free paper 1 2 3 4 5 6 7 8 9-MA-0403020100 First Printing, November 2000 To my wonderful wife, Christine, and my fine boys, Matthew, Luke, and Peter Contents List of Figures xv List of Tables xxi Foreword xxiii Preface xxv Acknowledgments xxvii 1 History of POSTGRESQL 1 1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 University of California at Berkeley . . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 Development Leaves Berkeley . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.4 POSTGRESQL Global Development Team . . . . . . . . . . . . . . . . . . . . . . 2 1.5 Open Source Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 Issuing Database Commands 5 2.1 Starting a Database Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.2 Controlling a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.3 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.4 Exiting a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3 Basic SQL Commands 11 3.1 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.2 Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3 Adding Data with INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.4 Viewing Data with SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.5 Selecting Specific Rows with WHERE . . . . . . . . . . . . . . . . . . . . . . . . 17 vii viii CONTENTS 3.6 Removing Data with DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.7 Modifying Data with UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.8 Sorting Data with ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.9 Destroying Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 4 Customizing Queries 23 4.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.2 Quotes Inside Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 4.3 Using NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 4.4 Controlling DEFAULT Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 4.5 Column Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 4.6 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.7 AND/OR Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.8 Range of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 4.9 LIKE Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.10 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4.11 CASE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.12 Distinct Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.13 Functions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.14 SET, SHOW, and RESET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.15 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5 SQL Aggregates 49 5.1 Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.2 Using GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.3 Using HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.4 Query Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 6 Joining Tables 57 6.1 Table and Column References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 6.2 Joined Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 6.3 Creating Joined Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 6.4 Performing Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 6.5 Three- and Four-Table Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6.6 Additional Join Possibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.7 Choosing a Join Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 6.8 One-to-Many Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.9 Unjoined Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 6.10 Table Aliases and Self-joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 CONTENTS ix 6.11 Non-equijoins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 6.12 Ordering Multiple Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 6.13 Primary and Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 6.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 7 Numbering Rows 79 7.1 Object Identification Numbers (OIDs) . . . . . . . . . . . . . . . . . . . . . . . . 79 7.2 Object Identification Number Limitations . . . . . . . . . . . . . . . . . . . . . 81 7.3 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 7.4 Creating Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 7.5 Using Sequences to Number Rows . . . . . . . . . . . . . . . . . . . . . . . . . 82 7.6 Serial Column Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 7.7 Manually Numbering Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 7.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 8 Combining SELECTs 87 8.1 UNION, EXCEPT, and INTERSECT Clauses . . . . . . . . . . . . . . . . . . . . . . 87 8.2 Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 8.3 Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.4 Subqueries in Non-SELECT Queries . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.5 UPDATE with FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.6 Inserting Data Using SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 8.7 Creating Tables Using SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 8.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 9 Data Types 107 9.1 Purpose of Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 9.2 Installed Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 9.3 Type Conversion Using CAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 9.4 Support Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 9.5 Support Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 9.6 Support Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 9.7 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 9.8 Large Objects (BLOBs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 9.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 10 Transactions and Locks 121 10.1 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 10.2 Multistatement Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 10.3 Visibility of Committed Transactions . . . . . . . . . . . . . . . . . . . . . . . . 124 10.4 Read Committed and Serializable Isolation Levels . . . . . . . . . . . . . . . . . 125 x CONTENTS 10.5 Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 10.6 Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 10.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 11 Performance 131 11.1 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 11.2 Unique Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 11.3 CLUSTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 11.4 VACUUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 11.5 VACUUM ANALYZE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.6 EXPLAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 12 Controlling Results 137 12.1 LIMIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 12.2 Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 12.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 13 Table Management 141 13.1 Temporary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 13.2 ALTER TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 13.3 GRANT and REVOKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 13.4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 13.5 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 13.6 Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 13.7 LISTEN and NOTIFY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 13.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 14 Constraints 155 14.1 NOT NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 14.2 UNIQUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 14.3 PRIMARY KEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 14.4 Foreign Key/REFERENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 14.5 CHECK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 14.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 15 Importing and Exporting Data 169 15.1 Using COPY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 15.2 COPY File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 15.3 DELIMITERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 15.4 COPY Without Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 [...]... experience in helping beginners with POSTGRESQL The text is easy to understand and full of practical tips Momjian captures database concepts using simple and easy-to-understand language He also presents numerous real-life examples throughout the book In addition, he does an outstanding job of covering many advanced POSTGRESQL topics Enjoy reading the book and have fun exploring POSTGRESQL! It is our hope this... world and go on to become a phenomenon POSTGRESQL is one of those projects Its popularity and success are a testament to the dedication and hard work of the POSTGRESQL global development team Although developing an advanced database system is no small feat, maintaining and enhancing an inherited code base are even more challenging The POSTGRESQL team has managed to not only improve the quality and usability... of POSTGRESQL 1.1 Introduction POSTGRESQL is the most advanced open source database server In this chapter, you will learn about databases, open source software, and the history of POSTGRESQL Three basic office productivity applications exist: word processors, spreadsheets, and databases Word processors produce text documents critical to any business Spreadsheets are used for financial calculations and. .. Database Commands In this chapter, you will learn how to connect to the database server and issue simple commands to the POSTGRESQL server At this point, the book makes the following assumptions: • You have installed POSTGRESQL • You have a running POSTGRESQL server • You are configured as a POSTGRESQL user • You have a database called test If not, see Appendix B 2.1 Starting a Database Session POSTGRESQL. .. much POSTGRESQL- specific functionality that cannot be covered in a book of this length Sections of the xxv xxvi PREFACE documentation are referenced in this book where appropriate This book uses italics for identifiers, SMALLCAPS for SQL keywords, and a monospaced font for SQL queries The Web site for this book is located at http://www .postgresql. org/docs/awbook.html Acknowledgments POSTGRESQL and this... It is our hope this book will not only teach you about using POSTGRESQL, but also inspire you to delve into its innards and contribute to the ongoing POSTGRESQL development effort Chen and Andrew Yu, co-authors of Postgres95 xxiii Preface This book is about POSTGRESQL, the most advanced open source database From its origins in academia, POSTGRESQL has moved to the Internet with explosive growth It... additional information about POSTGRESQL Appendix B provides information about installing POSTGRESQL Appendix C lists the features of POSTGRESQL not found in other database systems Appendix D contains a copy of the POSTGRESQL manual pages which should be consulted anytime you have trouble with query syntax Also, do not overlook the excellent documentation that is part of POSTGRESQL This documentation covers... (particularly date/time and geometric objects), and SQL standards compatibility • Mikheev, Vadim B., in San Francisco, California, United States, does large projects, like vacuum, subselects, triggers, and multi-version concurrency control (MVCC) • Momjian, Bruce, in Philadelphia, Pennsylvania, United States, maintains FAQ and TODO lists, code cleanup, patch application, training materials, and some coding... the POSTGRESQL package for Debian Linux • Horak, Daniel, near Pilzen, Czech Republic, did the WinNT port of POSTGRESQL (using the Cygwin environment) • Inoue, Hiroshi, in Fukui, Japan, improved btree index access • Ishii, Tatsuo, in Zushi, Kanagawa, Japan, handles multibyte foreign language support and porting issues • Martin, Dr Andrew C R., in London, United Kingdom, created the ECPG interface and. .. xv LIST OF FIGURES xvi 4.10 4.11 4.12 4.13 4.14 4.15 4.16 4.17 4.18 4.19 4.20 4.21 4.22 4.23 4.24 New friends WHERE test for Sandy Gleason Friends in New Jersey and Pennsylvania Incorrectly mixing AND and OR clauses Correctly mixing AND and OR clauses Selecting a range of values Firstname begins with D Regular expression sample queries Complex regular expression . PostgreSQL Introduction and Concepts PostgreSQL Introduction and Concepts Bruce Momjian ADDISON–WESLEY Boston. Data Momjian, Bruce. PostgreSQL : introduction and concepts / Momjian, Bruce. p. cm. ISBN 0-201-70331-9 1. Database management. 2. PostgreSQL. I. Title. QA76.9.D3