INFORMIX-Universal Server Guide to SQL: rkr/doc/informix/  · INFORMIX-Universal Server

  • View
    219

  • Download
    1

Embed Size (px)

Text of INFORMIX-Universal Server Guide to SQL: rkr/doc/informix/  · INFORMIX-Universal Server

  • INFORMIX-UniversalServer

    Informix Guide to SQL: Tutorial

    Version 9.1March 1997Part No. 000-3856

  • ii INFORMIX

    Published by INFORMIX Press Informix Software, Inc.4100 Bohannon DriveMenlo Park, CA 94025

    Copyright 1981-1997 by Informix Software, Inc. or their subsidiaries, provided that portions may becopyrighted by third parties, as set forth in documentation. All rights reserved.

    The following are worldwide trademarks of Informix Software, Inc., or its subsidiaries, registered in theUnited States of America as indicated by , and in numerous other countries worldwide:

    INFORMIX; INFORMIX-OnLine Dynamic Server; DataBlade

    The following are worldwide trademarks of the indicated owners or their subsidiaries, registered in theUnited States of America as indicated by , and in numerous other countries worldwide:

    Adobe Systems Incorporated: PostScript

    International Business Machines Corporation: DRDA; IBMMicrosoft Corporation: Microsoft; MS; MS-DOS; CodeView; MS Windows; Windows; Windows

    NT; ODBC; Visual Basic; Visual C++Microsoft Memory Management Product: HIMEM.SYS(DOS as used herein refers to MS-DOS and/or PC-DOS operating systems.)

    X/OpenCompany Ltd.: UNIX; X/Open

    All other marks or symbols are registered trademarks or trademarks of their respective owners.

    Documentation Team: Diana Chase, Brian Deutscher, Geeta Karmarkar, Jennifer Leland

    To the extent that this software allows the user to store, display, and otherwise manipulate various forms ofdata, including, without limitation, multimedia content such as photographs, movies, music and other binarylarge objects (blobs), use of any single blob may potentially infringe upon numerous different third-partyintellectual and/or proprietary rights. It is the user's responsibility to avoid infringements of any such third-party rights.

    RESTRICTED RIGHTS/SPECIAL LICENSE RIGHTS

    Software and documentation acquired with US Government funds are provided with rights as follows: (1) iffor civilian agency use, with Restricted Rights as defined in FAR 52.227-19; (2) if for Dept. of Defense use, withrights as restricted by vendor's standard license, unless superseded by negotiated vendor license as prescribedin DFAR 227.7202. Any whole or partial reproduction of software or documentation marked with this legendmust reproduce the legend.

  • Table of Contents

    Table ofContents

    IntroductionAbout This Manual . . . . . . . . . . . . . . . . . . 3

    Organization of This Manual . . . . . . . . . . . . . 3Types of Users . . . . . . . . . . . . . . . . . . 6Software Dependencies . . . . . . . . . . . . . . . 6Assumptions About Your Locale. . . . . . . . . . . . 6Demonstration Database . . . . . . . . . . . . . . 7

    Major Features . . . . . . . . . . . . . . . . . . . 7Documentation Conventions . . . . . . . . . . . . . . 8

    Typographical Conventions . . . . . . . . . . . . . 9Icon Conventions . . . . . . . . . . . . . . . . . 10Sample-Code Conventions . . . . . . . . . . . . . . 11On-Line Manuals . . . . . . . . . . . . . . . . . 11Printed Manuals . . . . . . . . . . . . . . . . . 12Error Message Files . . . . . . . . . . . . . . . . 12Documentation Notes, Release Notes, Machine Notes . . . . 13

    Compliance with Industry Standards . . . . . . . . . . . 13Informix Welcomes Your Comments . . . . . . . . . . . . 14

    Section I Using Basic SQL

    Chapter 1 Informix DatabasesThe Data Illustration of a Data Model . . . . . . . . . . . 1-3

    Concurrent Use and Security . . . . . . . . . . . . . 1-8Centralized Management . . . . . . . . . . . . . . 1-8

    Important Database Terms . . . . . . . . . . . . . . . 1-10The Object-Relational Model . . . . . . . . . . . . . 1-10

    Structured Query Language . . . . . . . . . . . . . . . 1-15Standard SQL . . . . . . . . . . . . . . . . . . 1-15Informix SQL and ANSI SQL . . . . . . . . . . . . . 1-16

  • iv Inform

    ANSI-Compliant Databases . . . . . . . . . . . . . 1-17GLS Databases . . . . . . . . . . . . . . . . . 1-17

    Summary . . . . . . . . . . . . . . . . . . . . . 1-17

    Chapter 2 Composing Simple SELECT StatementsIntroducing the SELECT Statement . . . . . . . . . . . . 2-4

    Some Basic Concepts . . . . . . . . . . . . . . . 2-5The Forms of SELECT . . . . . . . . . . . . . . . 2-9Special Data Types . . . . . . . . . . . . . . . . 2-10

    Single-Table SELECT Statements . . . . . . . . . . . . . 2-10Selecting All Columns and Rows . . . . . . . . . . . 2-11Selecting Specific Columns . . . . . . . . . . . . . 2-17Using the WHERE Clause. . . . . . . . . . . . . . 2-27Creating a Comparison Condition . . . . . . . . . . . 2-28Expressions and Derived Values . . . . . . . . . . . 2-45Using Functions in SELECT Statements . . . . . . . . . 2-50Using SPL Routines in SELECT Statements . . . . . . . . 2-64

    Multiple-Table SELECT Statements . . . . . . . . . . . . 2-66Creating a Cartesian Product. . . . . . . . . . . . . 2-66Creating a Join. . . . . . . . . . . . . . . . . . 2-67Some Query Shortcuts . . . . . . . . . . . . . . . 2-73

    Summary . . . . . . . . . . . . . . . . . . . . . 2-78

    Chapter 3 Composing Advanced SELECT StatementsUsing the GROUP BY and HAVING Clauses . . . . . . . . 3-4

    Using the GROUP BY Clause . . . . . . . . . . . . 3-4Using the HAVING Clause . . . . . . . . . . . . . 3-9

    Creating Advanced Joins . . . . . . . . . . . . . . . 3-11Self-Joins . . . . . . . . . . . . . . . . . . . 3-11Outer Joins . . . . . . . . . . . . . . . . . . . 3-20

    Subqueries in SELECT Statements . . . . . . . . . . . . 3-29Using ALL . . . . . . . . . . . . . . . . . . . 3-31Using ANY . . . . . . . . . . . . . . . . . . . 3-31Single-Valued Subqueries . . . . . . . . . . . . . . 3-32Correlated Subqueries . . . . . . . . . . . . . . . 3-34Using EXISTS . . . . . . . . . . . . . . . . . . 3-35

    Set Operations . . . . . . . . . . . . . . . . . . . 3-38Union. . . . . . . . . . . . . . . . . . . . . 3-39Intersection . . . . . . . . . . . . . . . . . . . 3-46Difference . . . . . . . . . . . . . . . . . . . 3-47

    Summary . . . . . . . . . . . . . . . . . . . . . 3-49

    ix Guide to SQL: Tutorial

  • Chapter 4 Modifying DataStatements That Modify Data . . . . . . . . . . . . . . 4-4

    Deleting Rows . . . . . . . . . . . . . . . . . . 4-4Deleting a Known Number of Rows. . . . . . . . . . . 4-5Inserting Rows . . . . . . . . . . . . . . . . . . 4-7Updating Rows . . . . . . . . . . . . . . . . . . 4-13

    Database Privileges . . . . . . . . . . . . . . . . . . 4-16Displaying Table Privileges. . . . . . . . . . . . . . 4-18

    Data Integrity . . . . . . . . . . . . . . . . . . . . 4-19Entity Integrity . . . . . . . . . . . . . . . . . . 4-19Semantic Integrity . . . . . . . . . . . . . . . . . 4-20Referential Integrity . . . . . . . . . . . . . . . . 4-21Object Modes and Violation Detection . . . . . . . . . . 4-25

    Interrupted Modifications . . . . . . . . . . . . . . . 4-27The Transaction . . . . . . . . . . . . . . . . . . 4-28Transaction Logging . . . . . . . . . . . . . . . . 4-29Specifying Transactions . . . . . . . . . . . . . . . 4-30

    Backups and Logs . . . . . . . . . . . . . . . . . . 4-31Backing Up with INFORMIX-Universal Server . . . . . . . 4-31

    Concurrency and Locks . . . . . . . . . . . . . . . . 4-32Data Replication . . . . . . . . . . . . . . . . . . . 4-32

    INFORMIX-Universal Server Data Replication . . . . . . . 4-33Summary . . . . . . . . . . . . . . . . . . . . . 4-34

    Chapter 5 Programming with SQLSQL in Programs . . . . . . . . . . . . . . . . . . . 5-4

    SQL in SQL APIs . . . . . . . . . . . . . . . . . 5-4Static Embedding . . . . . . . . . . . . . . . . . 5-5Dynamic Statements . . . . . . . . . . . . . . . . 5-5Program Variables and Host Variables . . . . . . . . . . 5-6

    Calling the Database Server . . . . . . . . . . . . . . . 5-8The SQL Communications Area . . . . . . . . . . . . 5-8The SQLCODE Field . . . . . . . . . . . . . . . . 5-9The SQLERRD Array . . . . . . . . . . . . . . . . 5-10The SQLWARN Array . . . . . . . . . . . . . . . 5-12The SQLERRM Character Array . . . . . . . . . . . . 5-13The SQLSTATE Value. . . . . . . . . . . . . . . . 5-13

    Retrieving Single Rows . . . . . . . . . . . . . . . . 5-14Data Type Conversion . . . . . . . . . . . . . . . 5-15Working with Null Data . . . . . . . . . . . . . . . 5-16Dealing with Errors . . . . . . . . . . . . . . . . 5-17

    Table of Contents v

  • vi Inform

    Retrieving Multiple Rows . . . . . . . . . . . . . . . 5-19Declaring a Cursor . . . . . . . . . . . . . . . . 5-20Opening a Cursor . . . . . . . . . . . . . . . . 5-20Fetching Rows. . . . . . . . . . . . . . . . . . 5-21Cursor Input Modes. . . . . . . . . . . . . . . . 5-22The Active Set of a Cursor . . . . . . . . . . . . . 5-23Using a Cursor: A Parts Explosion . . . . . . . . . . . 5-26

    Dynamic SQL . . . . . . . . . . . . . . . . . . . 5-28Preparing a Statement . . . . . . . . . . . . . . . 5-29Executing Prepared SQL . . . . . . . . . . . . . . 5-31Dynamic Host Variables . . . . . . . . . . . . . . 5-32Freeing Prepared Statements . . . . . . . . . . . . . 5-32Quick Execution . . . . . . . . . . . . . . . . . 5-33

    Embedding Data Definition Statements . . . . . . . . . . 5-33Embedding Grant and Revoke Privileges . . . . . . . . 5-34

    Summary . . . . . . . . . . . . . . . . . . . . . 5-36

    Chapter 6 Modifying Data Through SQL ProgramsUsing DELETE . . . . . . . . . . . . . . . . . . . 6-3

    Direct Deletions . . . . . . . . . . . . . . . . . 6-4Deleting with a Cursor . . . . . . . . . . . . . . . 6-7

    Using INSERT . . . . . . . . . . . . . . . . . . . 6-9Using an Insert Cursor . . . . . . . . . . . . . . . 6-9Rows of Constants . . . . . . . . . . . . . . . . 6-12An Insert Example . . . . . . . .