Click here to load reader

PostgreSQL: Introduction and Concepts - Introductio · PDF file Title: PostgreSQL: Introduction and Concepts Author: Bruce Momjian Subject: PostgreSQL Keywords: Copyright Addison-Wesley

  • View
    0

  • Download
    0

Embed Size (px)

Text of PostgreSQL: Introduction and Concepts - Introductio · PDF file Title: PostgreSQL:...

  • 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 by manufacturers and sellers to distinguish their products are claimed 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 [email protected] 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 LYX and LATEX 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 . . . . . . . . . . . .

Search related