IBM Informix Guide To SQL - Syntax
IBM Informix Guide To SQL - Syntax
Version 10.0/8.5
G251-2284-02
Version 10.0/8.5
G251-2284-02
Note: Before using this information and the product it supports, read the information in Notices on page D-1.
Third Edition (December 2005) This document contains proprietary information of IBM. It is provided under a license agreement and is protected by copyright law. The information contained in this publication does not include any product warranties, and any statements provided in this manual should not be interpreted as such. When you send information to IBM, you grant IBM a nonexclusive right to use or distribute the information in any way it believes appropriate without incurring any obligation to you. Copyright International Business Machines Corporation 1996, 2005. All rights reserved. US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
In This Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix About This Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Types of Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Software Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Assumptions About Your Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Demonstration Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x New Features in Dynamic Server, Version 10.0 . . . . . . . . . . . . . . . . . . . . . . . . xi 3 New Features in Dynamic Server, Version 10.00.xC3 . . . . . . . . . . . . . . . . . . . . . . xii 4 New Features in Dynamic Server, Version 10.00.xC4 . . . . . . . . . . . . . . . . . . . . . xiii New Features in Extended Parallel Server, Version 8.5 . . . . . . . . . . . . . . . . . . . . . xiii Documentation Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv Typographical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv Feature, Product, and Platform Markup . . . . . . . . . . . . . . . . . . . . . . . . . xiv Syntax Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Example Code Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Additional Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix IBM Informix Information Center . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Installation Guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Online Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Informix Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii IBM Informix Dynamic Server Version 10.0 and CSDK Version 2.90 Documentation Set . . . . . . . . . xxii Compliance with Industry Standards . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv IBM Welcomes Your Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
iii
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 ALLOCATE COLLECTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 ALLOCATE DESCRIPTOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6 ALLOCATE ROW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 ALTER ACCESS_METHOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9 ALTER FRAGMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11 ALTER FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-31 ALTER INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33 ALTER PROCEDURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-35 ALTER ROUTINE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-37 ALTER SEQUENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-40 ALTER TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-43 BEGIN WORK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-66 CLOSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-68 CLOSE DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-71 COMMIT WORK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-73 CONNECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-75 CREATE ACCESS_METHOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-82 CREATE AGGREGATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-84 CREATE CAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-87 CREATE DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-90 CREATE DISTINCT TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-93 CREATE DUPLICATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-96 CREATE EXTERNAL TABLE (XPS) . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-98 CREATE FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-107 CREATE FUNCTION FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-114 CREATE INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-116 CREATE OPAQUE TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-136 CREATE OPCLASS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-141 CREATE PROCEDURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-145 CREATE PROCEDURE FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-153 CREATE ROLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-155 CREATE ROUTINE FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-157 CREATE ROW TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-158 CREATE SCHEMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-162 CREATE SCRATCH TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-164 CREATE SEQUENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-165 CREATE SYNONYM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-168 CREATE TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-171 CREATE TEMP TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-208 CREATE Temporary TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-209 CREATE TRIGGER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-216 CREATE VIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-247 3 CREATE XADATASOURCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-252 3 CREATE XADATASOURCE TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-253 DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-255 DEALLOCATE COLLECTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-257 DEALLOCATE DESCRIPTOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-258 DEALLOCATE ROW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-259 DECLARE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-260 DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-275 DESCRIBE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-281 DESCRIBE INPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-286 DISCONNECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-291 DROP ACCESS_METHOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-294 DROP AGGREGATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-295 DROP CAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-296 DROP DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-297 DROP DUPLICATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-299 DROP FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-300 DROP INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-302
iv
DROP OPCLASS . . . . . DROP PROCEDURE . . . . DROP ROLE . . . . . . . DROP ROUTINE . . . . . DROP ROW TYPE . . . . . DROP SEQUENCE . . . . . DROP SYNONYM . . . . . DROP TABLE . . . . . . DROP TRIGGER. . . . . . DROP TYPE . . . . . . . DROP VIEW . . . . . . . DROP XADATASOURCE . . . DROP XADATASOURCE TYPE EXECUTE . . . . . . . . EXECUTE FUNCTION . . . EXECUTE IMMEDIATE . . . EXECUTE PROCEDURE . . . FETCH . . . . . . . . . FLUSH . . . . . . . . . FREE . . . . . . . . . GET DESCRIPTOR . . . . . GET DIAGNOSTICS . . . . GRANT . . . . . . . . GRANT FRAGMENT . . . . INFO . . . . . . . . . INSERT . . . . . . . . . LOAD . . . . . . . . . LOCK TABLE . . . . . . MERGE . . . . . . . . MOVE TABLE . . . . . . OPEN . . . . . . . . . OUTPUT . . . . . . . . PREPARE . . . . . . . . PUT . . . . . . . . . . RENAME COLUMN . . . . RENAME DATABASE . . . . RENAME INDEX . . . . . RENAME SEQUENCE. . . . RENAME TABLE . . . . . REVOKE . . . . . . . . REVOKE FRAGMENT . . . . ROLLBACK WORK . . . . SAVE EXTERNAL DIRECTIVES SELECT . . . . . . . . SET ALL_MUTABLES . . . . SET AUTOFREE . . . . . . SET COLLATION . . . . . SET CONNECTION . . . . SET CONSTRAINTS . . . . SET Database Object Mode . . SET DATASKIP . . . . . . SET DEBUG FILE . . . . . SET Default Table Space . . . SET Default Table Type . . . SET DEFERRED_PREPARE . . SET DESCRIPTOR . . . . . SET ENCRYPTION PASSWORD SET ENVIRONMENT . . . . SET EXPLAIN . . . . . . SET INDEX . . . . . . . SET INDEXES . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-304 2-305 2-307 2-308 2-310 2-312 2-313 2-314 2-316 2-317 2-318 2-319 2-320 2-321 2-329 2-334 2-336 2-344 2-353 2-355 2-357 2-362 2-371 2-388 2-393 2-395 2-407 2-413 2-416 2-419 2-424 2-431 2-433 2-442 2-449 2-451 2-452 2-453 2-454 2-456 2-471 2-474 2-476 2-479 2-527 2-529 2-531 2-534 2-538 2-539 2-545 2-547 2-549 2-550 2-552 2-554 2-560 2-563 2-568 2-573 2-574
Contents
SET ISOLATION . . . . . SET LOCK MODE . . . . . SET LOG . . . . . . . . SET OPTIMIZATION . . . . SET PDQPRIORITY . . . . SET PLOAD FILE . . . . . SET Residency . . . . . . SET ROLE . . . . . . . . SET SCHEDULE LEVEL . . . SET SESSION AUTHORIZATION SET STATEMENT CACHE . . SET TABLE . . . . . . . SET TRANSACTION . . . . SET Transaction Mode . . . . SET TRIGGERS . . . . . . START VIOLATIONS TABLE . STOP VIOLATIONS TABLE . . 4 TRUNCATE (IDS) . . . . . TRUNCATE (XPS) . . . . . UNLOAD . . . . . . . . UNLOCK TABLE . . . . . UPDATE . . . . . . . . UPDATE STATISTICS . . . . WHENEVER . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
2-575 2-580 2-582 2-584 2-586 2-589 2-590 2-592 2-594 2-595 2-597 2-601 2-602 2-606 2-608 2-609 2-622 2-624 2-628 2-630 2-635 2-636 2-649 2-659
vi
Appendix A. Reserved Words for IBM Informix Dynamic Server . . . . . . . . . . . A-1 Appendix B. Reserved Words for IBM Informix Extended Parallel Server . . . . . . . B-1 Appendix C. Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . C-1 Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D-1 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . X-1
Contents
vii
viii
Introduction
In This Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix About This Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Types of Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Software Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Assumptions About Your Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Demonstration Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x New Features in Dynamic Server, Version 10.0 . . . . . . . . . . . . . . . . . . . . . . . . xi 3 New Features in Dynamic Server, Version 10.00.xC3 . . . . . . . . . . . . . . . . . . . . . . xii 4 New Features in Dynamic Server, Version 10.00.xC4 . . . . . . . . . . . . . . . . . . . . . xiii New Features in Extended Parallel Server, Version 8.5 . . . . . . . . . . . . . . . . . . . . . xiii Documentation Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv Typographical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv Feature, Product, and Platform Markup . . . . . . . . . . . . . . . . . . . . . . . . . xiv Syntax Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv How to Read a Command-Line Syntax Diagram . . . . . . . . . . . . . . . . . . . . . xvi Keywords and Punctuation . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Identifiers and Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Example Code Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Additional Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix IBM Informix Information Center . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Installation Guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Online Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Locating Online Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Online Notes Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Informix Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Online Manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Printed Manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii IBM Informix Dynamic Server Version 10.0 and CSDK Version 2.90 Documentation Set . . . . . . . . . xxii Compliance with Industry Standards . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv IBM Welcomes Your Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
In This Introduction
This introduction provides an overview of the information in this manual and describes the documentation conventions that it uses.
ix
access and manipulate the data in your databases. The IBM Informix Database Design and Implementation Guide shows how to use SQL to implement and manage relational databases.
Types of Users
This manual is written for the following users: v Database users v Database administrators v Database-application programmers This manual assumes that you have the following background: v A working knowledge of your computer, your operating system, and the utilities that your operating system provides v Some experience working with relational databases or exposure to database concepts v Some experience with computer programming If you have limited experience with relational databases, SQL, or your operating system, refer to the IBM Informix Getting Started Guide for your database server for a list of supplementary titles.
Software Dependencies
This manual assumes that you are using one of the following database servers: v IBM Informix Extended Parallel Server, Version 8.5 v IBM Informix Dynamic Server, Version 10.0
Demonstration Databases
The DBAccess utility, which is provided with your IBM Informix database server products, includes one or more of the following demonstration databases: v The stores_demo database illustrates a relational schema with information about a fictitious wholesale sporting-goods distributor. Many examples in IBM Informix manuals are based on the stores_demo database.
v For Extended Parallel Server only, the sales_demo database illustrates a dimensional schema for data-warehousing applications. For conceptual information about dimensional data modeling, see the IBM Informix Database Design and Implementation Guide. v For Dynamic Server only, the superstores_demo database illustrates an object-relational schema that contains examples of extended data types, data-type inheritance and table inheritance, and user-defined routines. For information about how to create and populate the demonstration databases, see the IBM Informix DBAccess User's Guide. For descriptions of the databases and their contents, see the IBM Informix Guide to SQL: Reference. The scripts that you use to install the demonstration databases reside in the $INFORMIXDIR/bin directory on UNIX platforms and in the %INFORMIXDIR%\bin directory in Windows environments.
SET ROLE DEFAULT v The DBSA (Database Server Administrator) can use the GRANT ROLE EXTEND statement to assign the new built-in role EXTEND to users. When this security feature is enabled, only users who hold the EXTEND role can register or drop external UDRs or create shared libraries in the database. v This release introduces default roles that the DBA can grant or revoke. These take effect when a user who holds a default role connects to the database, so that appropriate access privileges are available for using applications that do not include GRANT statements that reference individual users. New SQL functions can return the current role or the default role of the user.
Introduction
xi
v The CREATE INDEX and DROP INDEX statements can specify the ONLINE keyword to define or drop an index without applying table locks that might interfere with concurrent users. v Tables and indexes now can use fragmentation strategies that define multiple named fragments within the same dbspace. v Data values returned by distributed DML operations or UDRs in databases of the same Dynamic Server instance now can return the built-in opaque data types BLOB, BOOLEAN, CLOB, and LVARCHAR. They can also return UDTs, and DISTINCT types whose base types are built-in types, if the UDTs and DISTINCT types are explicitly cast to built-in data types, and if the casts, DISTINCT types, and UDTs are defined in all the participating databases. v The DS_NONPDQ_QUERY_MEM configuration parameter can be set to improve the performance of certain non-PDQ queries. v This release supports external optimizer directives that reside in the database and that are automatically applied to specified queries. These can be enabled or disabled for a given session by a configuration parameter. v The SET ENVIRONMENT OPTCOMPIND statement can reset the value of OPTCOMPIND dynamically, overriding the corresponding configuration parameter or environment variable setting. This can improve performance during sessions that use queries for both DS (decision-support) and OLTP (online-transaction-processing). v The SET ENCRYPTION PASSWORD statement can specify a session password to support column-level and cell-level encryption of sensitive data. This statement, and new built-in encryption and decryption functions, can support compliance with data security and data confidentiality requirements that are mandated for some industries and jurisdictions. v User-defined functions can include multiple INOUT parameters. 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
xii
3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
If you also include the ORDER BY clause, qualifying rows are first arranged by the ORDER BY specification before SKIP offset and LIMIT max clauses are applied.
Introduction
xiii
Documentation Conventions
This section describes the conventions that this manual uses. These conventions make it easier to gather information from this and other volumes in the documentation set. The following conventions are discussed: v Typographical conventions v Other conventions v Syntax diagrams v Command-line conventions v Example code conventions
Typographical Conventions
This manual uses the following conventions to introduce new terms, illustrate screen displays, describe command syntax, and so forth.
Convention KEYWORD italics Meaning Keywords of SQL, SPL, and some other programming languages appear in uppercase letters in a serif font. Within text, new terms and emphasized words appear in italics. Within syntax and code examples, variable values that you are to specify appear in italics. Names of program entities (such as classes, events, and tables), environment variables, file and pathnames, and interface elements (such as icons, menu items, and buttons) appear in boldface. Information that the product displays and information that you enter appear in a monospace typeface. Keys that you are to press appear in uppercase letters in a sans serif font. This symbol indicates a menu item. For example, Choose Tools > Options means choose the Options item from the Tools menu.
boldface
Tip: When you are instructed to enter characters or to execute a command, immediately press RETURN after the entry. When you are instructed to type the text or to press other keys, no RETURN is required.
xiv
of this markup follow: Dynamic Server Identifies information that is specific to IBM Informix Dynamic Server End of Dynamic Server Windows Only Identifies information that is specific to the Windows environment End of Windows Only This markup can apply to one or more paragraphs within a section. When an entire section applies to a particular product or platform, this is noted as part of the heading text, for example: Table Sorting (Linux)
Syntax Diagrams
This guide uses syntax diagrams built with the following components to describe the syntax for statements and all commands other than system-level commands. Syntax diagrams depicting SQL and command-line statements have changed in the following ways: v The symbols at the beginning and end of statements are double arrows. v The symbols at the beginning and end of syntax segment diagrams are vertical lines. v How many times a loop can be repeated is explained in a diagram footnote, whose marker appears above the path that is describes. v Syntax statements that are longer than one line continue on the next line. v Product or condition-specific paths are explained in diagram footnotes, whose markers appear above the path that they describe. v Cross-references to the descriptions of other syntax segments appear as diagram footnotes, whose markers immediately follow the name of the segment that they reference. The following table describes syntax diagram components.
Component represented in PDF Component represented in HTML >>---------------------Meaning Statement begins.
----------------------->
Statement continues on next line. Statement continues from previous line. Statement ends. Required item.
>--------------------------------------------->< --------SELECT----------
Introduction
xv
Required item with choice. One and only one item must be present.
Optional items with choice are shown below the main line, one of which you might specify. The values below the main line are optional, one of which you might specify. If you do not specify an item, the value above the line will be used as the default. Optional items. Several items are allowed; a comma must precede each repetition.
-t table
xvi
The second line in this diagram has a segment named Setting the Run Mode, which according to the diagram footnote, is on page Z-1. If this was an actual cross-reference, you would find this segment in on the first page of Appendix Z. Instead, this segment is shown in the following segment diagram. Notice that the diagram uses segment start and end components. Setting the Run Mode:
l c -f d p a u n N
To see how to construct a command correctly, start at the top left of the main diagram. Follow the diagram to the right, including the elements that you want. The elements in this diagram are case sensitive because the illustrates utility syntax. Other types of syntax, such as SQL, are not case sensitive. The Creating a No-Conversion Job diagram illustrates the following steps: 1. Type onpladm create job and then the name of the job. 2. Optionally, type -p and then the name of the project. 3. Type the following required elements: v -n v -d and the name of the device v -D and the name of the database v -t and the name of the table 4. Optionally, you can choose one or more of the following elements and repeat them an arbitrary number of times: v -S and the server name v -T and the target server name v The run mode. To set the run mode, follow the Setting the Run Mode segment diagram to type -f, optionally type d, p, or a, and then optionally type l or u. 5. Follow the diagram to the terminator. Your diagram is complete.
xvii
shown in uppercase letters. When you use a keyword in a command, you can write it in uppercase or lowercase letters, but you must spell the keyword exactly as it appears in the syntax diagram. You must also use any punctuation in your statements and commands exactly as shown in the syntax diagrams.
When you write a SELECT statement of this form, you replace the variables column_name and table_name with the name of a specific column and table.
To use this SQL code for a specific product, you must apply the syntax rules for that product. For example, if you are using DBAccess, you must delimit multiple statements with semicolons. If you are using an SQL API, you must use EXEC SQL at the start of each statement and a semicolon (or other appropriate delimiter) at the end of the statement. Tip: Ellipsis points in a code example indicate that more code would be added in a full application, but it is not necessary to show it to describe the concept being discussed. For detailed directions on using SQL statements for a particular application development tool or SQL API, see the manual for your product.
xviii
Additional Documentation
For additional information, refer to the following types of documentation: v Installation guides v Online notes v Informix error messages v Manuals v Online help
Installation Guides
Installation guides are located in the /doc directory of the product CD or in the /doc directory of the products compressed file if you downloaded it from the IBM Web site. Alternatively, you can obtain installation guides from the IBM Informix Online Documentation site at http://www.ibm.com/software/data/informix/pubs/library/ or the IBM Informix Information Center at http://publib.boulder.ibm.com/infocenter/ids9help/index.jsp.
Online Notes
The following sections describe the online files that supplement the information in this manual. Please examine these files before you begin using your IBM Informix product. They contain vital information about application and performance issues.
Introduction
xix
Description The TOC (Table of Contents) notes file provides a comprehensive directory of hyperlinks to the release notes, the fixed and known defects file, and all the documentation notes files for individual manual titles. The documentation notes file for each manual contains important information and corrections that supplement the information in the manual or information that was modified since publication. The release notes file describes feature differences from earlier versions of IBM Informix products and how these differences might affect current products. For some products, this file also contains information about any known problems and their workarounds.
Format HTML
Documentation Notes
HTML, text
Release Notes
HTML, text
Machine Notes
(Non-Windows platforms only) The machine text notes file describes any platform-specific actions that you must take to configure and use IBM Informix products on your computer. This text file lists issues that have been identified with the current version. It also lists customer-reported defects that have been fixed in both the current version and in previous versions. text
xx
ids_win_fixed_and_known _defects_version.txt
Manuals
Online Manuals
A CD that contains your manuals in electronic format is provided with your IBM Informix products. You can install the documentation or access it directly from the CD. For information about how to install, read, and print online manuals, see the installation insert that accompanies your CD. You can also obtain the same online manuals from the IBM Informix Online Documentation site at http://www.ibm.com/software/data/informix/pubs/library/ or in the IBM Informix Information Center at http://publib.boulder.ibm.com/infocenter/ids9help/index.jsp.
Introduction
xxi
Printed Manuals
To order hardcopy manuals, contact your sales representative or visit the IBM Publications Center Web site at http://www.ibm.com/software/howtobuy/data.html.
Online Help
IBM Informix online help, provided with each graphical user interface (GUI), displays information about those interfaces and the functions that they perform. Use the help facilities that each GUI provides to display the online help.
Accessibility
IBM is committed to making our documentation accessible to persons with disabilities. Our books are available in HTML format so that they can be accessed with assistive technology such as screen reader software. The syntax diagrams in our manuals are available in dotted decimal format, which is an accessible format that is available only if you are using a screen reader. For more information about the dotted decimal format, see the Accessibility appendix.
IBM Informix Dynamic Server Version 10.0 and CSDK Version 2.90 Documentation Set
The following tables list the manuals that are part of the IBM Informix Dynamic Server, Version 10.0 and the CSDK Version 2.90, documentation set. PDF and HTML versions of these manuals are available at http://www.ibm.com/software/data/informix/pubs/library/ or in the IBM Informix Information Center at http://publib.boulder.ibm.com/infocenter/ids9help/index.jsp. You can order hardcopy versions of these manuals from the IBM Publications Center at http://www.ibm.com/software/howtobuy/data.html.
Table 1. Database Server Manuals Manual Administrators Guide Administrators Reference Subject Understanding, configuring, and administering your database server. Reference material for Informix Dynamic Server, such as the syntax of database server utilities onmode and onstat, and descriptions of configuration parameters, the sysmaster tables, and logical-log records. The concepts and methods you need to understand when you use the ON-Bar and ontape utilities to back up and restore data. Using the following DataBlade modules that are included with Dynamic Server: v MQ DataBlade module, to allow IBM Informix database applications to communicate with other MQSeries applications. v Large Object Locator, a foundation DataBlade module that can be used by other modules that create or store large-object data. DB-Access Users Guide DataBlade API Function Reference DataBlade API Programmers Guide Using the DB-Access utility to access, modify, and retrieve data from Informix databases. The DataBlade API functions and the subset of ESQL/C functions that the DataBlade API supports. You can use the DataBlade API to develop client LIBMI applications and C user-defined routines that access data in Informix databases. The DataBlade API, which is the C-language application-programming interface provided with Dynamic Server. You use the DataBlade API to develop client and server applications that access data stored in Informix databases.
xxii
Table 1. Database Server Manuals (continued) Manual Database Design and Implementation Guide Enterprise Replication Guide Error Messages file Getting Started Guide Subject Designing, implementing, and managing your Informix databases. How to design, implement, and manage an Enterprise Replication system to replicate data between multiple database servers. Causes and solutions for numbered error messages you might receive when you work with IBM Informix products. Describes the products bundled with IBM Informix Dynamic Server and interoperability with other IBM products. Summarizes important features of Dynamic Server and the new features for each version. Information about Informix databases, data types, system catalog tables, environment variables, and the stores_demo demonstration database. Detailed descriptions of the syntax for all Informix SQL and SPL statements. A tutorial on SQL, as implemented by Informix products, that describes the basic ideas and terms that are used when you work with a relational database. Accessing and using the High-Performance Loader (HPL), to load and unload large quantities of data to and from Informix databases. Instructions for installing IBM Informix Dynamic Server on Windows. Instructions for installing IBM Informix Dynamic Server on UNIX and Linux. Writing user-defined routines (UDRs) in the Java programming language for Informix Dynamic Server with J/Foundation. Conversion to and reversion from the latest versions of Informix database servers. Migration between different Informix database servers. The Optical Subsystem, a utility that supports the storage of BYTE and TEXT data on optical disk. Configuring and operating IBM Informix Dynamic Server to achieve optimum performance. Creating R-tree indexes on appropriate data types, creating new operator classes that use the R-tree access method, and managing databases that use the R-tree secondary access method. The IBM Informix subagent that allows a Simple Network Management Protocol (SNMP) network manager to monitor the status of Informix servers. Informix Storage Manager (ISM), which manages storage devices and media for your Informix database server. The secure-auditing capabilities of Dynamic Server, including the creation and maintenance of audit logs. How to define new data types and enable user-defined routines (UDRs) to extend IBM Informix Dynamic Server. Creating a secondary access method (index) with the Virtual-Index Interface (VII) to extend the built-in indexing schemes of IBM Informix Dynamic Server. Typically used with a DataBlade module. Creating a primary access method with the Virtual-Table Interface (VTI) so that users have a single SQL interface to Informix tables and to data that does not conform to the storage scheme of Informix Dynamic Server.
Guide to SQL: Reference Guide to SQL: Syntax Guide to SQL: Tutorial High-Performance Loader Users Guide Installation Guide for Microsoft Windows Installation Guide for UNIX and Linux J/Foundation Developers Guide Migration Guide Optical Subsystem Guide Performance Guide R-Tree Index Users Guide
SNMP Subagent Guide Storage Manager Administrators Guide Trusted Facility Guide User-Defined Routines and Data Types Developers Guide Virtual-Index Interface Programmers Guide Virtual-Table Interface Programmers Guide
Introduction
xxiii
Table 2. Client/Connectivity Manuals Manual Client Products Installation Guide Embedded SQLJ Users Guide Subject Installing IBM Informix Client Software Developers Kit (Client SDK) and IBM Informix Connect on computers that use UNIX, Linux, and Windows. Using IBM Informix Embedded SQLJ to embed SQL statements in Java programs.
ESQL/C Programmers Manual The IBM Informix implementation of embedded SQL for C. GLS Users Guide The Global Language Support (GLS) feature, which allows IBM Informix APIs and database servers to handle different languages, cultural conventions, and code sets. Installing and using Informix JDBC Driver to connect to an Informix database from within a Java application or applet. Using Informix .NET Provider to enable .NET client applications to access and manipulate data in Informix databases. Using the Informix ODBC Driver API to access an Informix database and interact with the Informix database server. Installing and configuring Informix OLE DB Provider to enable client applications, such as ActiveX Data Object (ADO) applications and Web pages, to access data on an Informix server. The architecture of the C++ object interface and a complete class reference.
JDBC Driver Programmers Guide .NET Provider Reference Guide ODBC Driver Programmers Manual OLE DB Provider Programmers Guide Object Interface for C++ Programmers Guide
Table 3. DataBlade Developers Kit Manuals Manual DataBlade Developers Kit Users Guide DataBlade Module Development Overview DataBlade Module Installation and Registration Guide Subject Developing and packaging DataBlade modules using BladeSmith and BladePack. Basic orientation for developing DataBlade modules. Includes an example illustrating the development of a DataBlade module. Installing DataBlade modules and using BladeManager to manage DataBlade modules in Informix databases.
xxiv
docinf@us.ibm.com This email address is reserved for reporting errors and omissions in our documentation. For immediate help with a technical problem, contact IBM Technical Support. For instructions, see the IBM Informix Technical Support website at http://www306.ibm.com/software/data/informix/support/contact.html. We appreciate your suggestions.
Introduction
xxv
xxvi
In This Chapter
This chapter provides information about how to use the SQL statements, SPL statements, and syntax segments that subsequent chapters of this book discuss. The chapter is organized into the following sections.
Section How to Enter SQL Statements How to Enter SQL Comments on page 1-3 Categories of SQL Statements on page 1-6 ANSI/ISO Compliance and Extensions on page 1-8 Scope How to use syntax diagrams and descriptions to enter SQL statements correctly How to enter comments in SQL statements The SQL statements, listed by functional category The SQL statements, listed by degree of ANSI/ISO compliance
1-1
was not set to 1 when the database server was initialized, the database server stores the owner name in uppercase letters. Statement descriptions are provided in this manual to help you to enter SQL statements successfully. A statement description includes this information: v A brief introduction that explains what the statement does v A syntax diagram that shows how to enter the statement correctly v A syntax table that explains each input parameter in the syntax diagram v Rules of usage, typically with examples that illustrate these rules For some statements, this information is provided for individual clauses. Most statement descriptions conclude with references to related information in this manual and in other manuals. Chapter 2 provides descriptions of each SQL statement, arranged in alphabetical order. Chapter 3 describes each of the SPL statements, using the same format. The major aids for entering SQL statements include: v The combination of the syntax diagram and syntax table v The examples of syntax that appear in the rules of usage v The references to related information
1-2
The diagrams generally provide an intuitive notation for what is valid in a given SQL statement, but for some statements, dependencies or restrictions among syntax elements are identified only in the text of the Usage section.
Using Examples
To understand the main syntax diagram and subdiagrams for a statement, study the examples of syntax that appear in the rules of usage for each statement. These examples have two purposes: v To show how to accomplish specific tasks with the statement or its clauses v To show how to use syntax of the statement or its clauses in a concrete way Tip: An efficient way to understand a syntax diagram is to find an example of the syntax and compare it with the keywords and parameters in the syntax diagram. By mapping the concrete elements of the example to the abstract elements of the syntax diagram, you can understand the syntax diagram and use it more effectively. For an explanation of the conventions used in the examples in this manual, see Example Code Conventions on page xviii of the Introduction. These code examples are program fragments to illustrate valid syntax, rather than complete SQL programs. In some code examples, ellipsis ( . . . ) symbols indicate that additional code has been omitted. To save space, however, ellipses are not shown at the beginning or end of the program fragments.
1-3
The following table shows the SQL comment indicators that you can enter in your code. Here a Y in a column signifies that you can use the symbol with the product or with the type of database identified in the column heading. An N in a column signifies that you cannot use the symbol with the indicated product or with a database of the indicated ANSI-compliance status.
ANSICompliant Databases Y Databases Not ANSI Compliant Y
ESQL/C Y
SPL Routine Y
DBAccess Y
Description The double hyphen precedes a comment within a single line. To comment more than one line, put double hyphen symbols at the beginning of each comment line. Braces enclose the comment. The { precedes the comment, and the } follows it. Braces can delimit single- or multiple-line comments, but comments cannot be nested. C-language style slash and asterisk ( /* */ ) paired delimiters enclose the comment. The /* precedes the comment, and the */ follows it. These can delimit single-line or multiple-line comments, but comments cannot be nested.
braces ({...})
Characters within the comment are ignored by the database server. The section Optimizer Directives on page 5-34 describes a context where information within comments can influence query plans of Dynamic Server. If the product that you use supports all of these comment symbols, your choice of a comment symbol depends on requirements for ANSI/ISO compliance: v Double hyphen ( -- ) complies with the ANSI/ISO standard for SQL. v Braces ( { } ) are an Informix extension to the ANSI/ISO standard. v C-style slash-and-asterisk ( /* . . . */ ) comply with the SQL-99 standard. If ANSI/ISO compliance is not an issue, your choice of comment symbols is a matter of personal preference. In DBAccess, you can use any of these comment symbols when you enter SQL statements with the SQL editor and when you create SQL command files with the SQL editor or a system editor. C-style ( /* . . . */ ) comments, however, are not supported by DBAccess within CONNECT, DISCONNECT, INFO, LOAD, OUTPUT, SET CONNECT, nor UNLOAD statements.
1-4
An SQL command file is an operating-system file that contains one or more SQL statements. Command files are also known as command scripts. For more information about command files, see the discussion of command scripts in the IBM Informix Guide to SQL: Tutorial. For information on how to create and modify command files with the SQL editor or a system editor in DBAccess, see the IBM Informix DBAccess User's Guide. You can use either comment symbol in any line of an SPL routine. See the discussion of how to comment and document an SPL routine in the IBM Informix Guide to SQL: Tutorial. In ESQL/C, the double hyphen ( -- ) can begin a comment that extends to the end of the same line. For information on language-specific comment symbols in ESQL/C programs, see the IBM Informix ESQL/C Programmer's Manual.
The following example uses the same SQL statement and the same comment as in the preceding example, but places the comment on a separate line:
SELECT * FROM customer -- Selects all columns and rows
In the following example, the user enters the same SQL statement as in the preceding example but now enters a multiple-line comment:
SELECT * FROM customer -- Selects all columns and rows -- from the customer table
The next example uses the same SQL statement and the same comment as in the preceding example, but the comment appears on a line by itself:
SELECT * FROM customer {Selects all columns and rows}
In the following example, the same SQL statement as in the preceding example is followed by a multiple-line comment:
SELECT * FROM customer {Selects all columns and rows from the customer table}
1-5
1-6
3 3 3 3 3 3 3 3 3 3 3 3 3 3
CREATE XADATASOURCE TYPE DROP ACCESS_METHOD DROP AGGREGATE DROP CAST DROP DATABASE DROP DUPLICATE DROP FUNCTION DROP INDEX DROP OPCLASS DROP PROCEDURE DROP ROLE DROP ROUTINE DROP ROW TYPE DROP SEQUENCE
3 3 3 3 3 3 3 3 3 3 3 3 3 3
DROP SYNONYM DROP TABLE DROP TRIGGER DROP TYPE DROP VIEW DROP XADATASOURCE DROP XADATASOURCE TYPE MOVE RENAME COLUMN RENAME DATABASE RENAME INDEX RENAME SEQUENCE RENAME TABLE TRUNCATE
1-7
Optimization Statements
SAVE EXTERNAL DIRECTIVES SET ALL_MUTABLES SET Default Table Space SET Default Table Type SET ENVIRONMENT SET EXPLAIN SET OPTIMIZATION SET PDQPRIORITY SET Residency SET SCHEDULE LEVEL SET STATEMENT CACHE UPDATE STATISTICS
Auxiliary Statements
GET DIAGNOSTICS INFO OUTPUT SET COLLATION SET DATASKIP SET ENCRYPTION PASSWORD WHENEVER
ANSI/ISO-Compliant Statements
CLOSE COMMIT WORK EXECUTE IMMEDIATE ROLLBACK WORK SET SESSION AUTHORIZATION SET TRANSACTION
1-8
CREATE TABLE CREATE Temporary TABLE CREATE VIEW DEALLOCATE DESCRIPTOR DECLARE DELETE DESCRIBE DESCRIBE INPUT DISCONNECT EXECUTE FETCH GET DESCRIPTOR GET DIAGNOSTICS
GRANT INSERT OPEN PREPARE REVOKE SELECT SET CONNECTION SET CONSTRAINTS SET DESCRIPTOR SET Transaction Mode UPDATE WHENEVER
1-9
RELEASE RENAME COLUMN RENAME DATABASE RENAME INDEX RENAME SEQUENCE RENAME TABLE RESERVE REVOKE FRAGMENT SAVE EXTERNAL DIRECTIVES SET ALL_MUTABLES SET AUTOFREE SET COLLATION SET Database Object Mode SET DATASKIP SET DEBUG FILE TO SET Default Table Space SET Default Table Type SET DEFERRED_PREPARE SET ENCRYPTION PASSWORD SET ENVIRONMENT SET EXPLAIN SET ISOLATION SET LOCK MODE SET LOG SET MOUNTING TIMEOUT SET OPTIMIZATION SET PDQPRIORITY SET PLOAD FILE SET Residency SET ROLE SET SCHEDULE LEVEL SET STATEMENT CACHE START VIOLATIONS TABLE STOP VIOLATIONS TABLE TRUNCATE UNLOAD UNLOCK TABLE UPDATE STATISTICS
1-10
2-1
DROP AGGREGATE . . . . DROP CAST . . . . . . . DROP DATABASE . . . . . DROP DUPLICATE . . . . . DROP FUNCTION . . . . . DROP INDEX . . . . . . DROP OPCLASS . . . . . DROP PROCEDURE . . . . DROP ROLE . . . . . . . DROP ROUTINE . . . . . DROP ROW TYPE . . . . . DROP SEQUENCE . . . . . DROP SYNONYM . . . . . DROP TABLE . . . . . . DROP TRIGGER. . . . . . DROP TYPE . . . . . . . DROP VIEW . . . . . . . DROP XADATASOURCE . . . DROP XADATASOURCE TYPE EXECUTE . . . . . . . . EXECUTE FUNCTION . . . EXECUTE IMMEDIATE . . . EXECUTE PROCEDURE . . . FETCH . . . . . . . . . FLUSH . . . . . . . . . FREE . . . . . . . . . GET DESCRIPTOR . . . . . GET DIAGNOSTICS . . . . GRANT . . . . . . . . GRANT FRAGMENT . . . . INFO . . . . . . . . . INSERT . . . . . . . . . LOAD . . . . . . . . . LOCK TABLE . . . . . . MERGE . . . . . . . . MOVE TABLE . . . . . . OPEN . . . . . . . . . OUTPUT . . . . . . . . PREPARE . . . . . . . . PUT . . . . . . . . . . RENAME COLUMN . . . . RENAME DATABASE . . . . RENAME INDEX . . . . . RENAME SEQUENCE. . . . RENAME TABLE . . . . . REVOKE . . . . . . . . REVOKE FRAGMENT . . . . ROLLBACK WORK . . . . SAVE EXTERNAL DIRECTIVES SELECT . . . . . . . . SET ALL_MUTABLES . . . . SET AUTOFREE . . . . . . SET COLLATION . . . . . SET CONNECTION . . . . SET CONSTRAINTS . . . . SET Database Object Mode . . SET DATASKIP . . . . . . SET DEBUG FILE . . . . . SET Default Table Space . . . SET Default Table Type . . . SET DEFERRED_PREPARE . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-295 2-296 2-297 2-299 2-300 2-302 2-304 2-305 2-307 2-308 2-310 2-312 2-313 2-314 2-316 2-317 2-318 2-319 2-320 2-321 2-329 2-334 2-336 2-344 2-353 2-355 2-357 2-362 2-371 2-388 2-393 2-395 2-407 2-413 2-416 2-419 2-424 2-431 2-433 2-442 2-449 2-451 2-452 2-453 2-454 2-456 2-471 2-474 2-476 2-479 2-527 2-529 2-531 2-534 2-538 2-539 2-545 2-547 2-549 2-550 2-552
2-2
SET DESCRIPTOR . . . . . SET ENCRYPTION PASSWORD SET ENVIRONMENT . . . . SET EXPLAIN . . . . . . SET INDEX . . . . . . . SET INDEXES . . . . . . SET ISOLATION . . . . . SET LOCK MODE . . . . . SET LOG . . . . . . . . SET OPTIMIZATION . . . . SET PDQPRIORITY . . . . SET PLOAD FILE . . . . . SET Residency . . . . . . SET ROLE . . . . . . . . SET SCHEDULE LEVEL . . . SET SESSION AUTHORIZATION SET STATEMENT CACHE . . SET TABLE . . . . . . . SET TRANSACTION . . . . SET Transaction Mode . . . . SET TRIGGERS . . . . . . START VIOLATIONS TABLE . STOP VIOLATIONS TABLE . . 4 TRUNCATE (IDS) . . . . . TRUNCATE (XPS) . . . . . UNLOAD . . . . . . . . UNLOCK TABLE . . . . . UPDATE . . . . . . . . UPDATE STATISTICS . . . . WHENEVER . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-554 2-560 2-563 2-568 2-573 2-574 2-575 2-580 2-582 2-584 2-586 2-589 2-590 2-592 2-594 2-595 2-597 2-601 2-602 2-606 2-608 2-609 2-622 2-624 2-628 2-630 2-635 2-636 2-649 2-659
In This Chapter
This chapter describes the syntax and semantics of SQL statements that are recognized by Dynamic Server or by Extended Parallel Server. Statements (and statement segments, and notes describing usage) that are not explicitly restricted to one of these database servers are valid for both. The statement descriptions appear in alphabetical order. For some statements, important details of the semantics appear in other volumes of this documentation set, as indicated by cross-references. For many statements, the syntax diagram, or the table of terms immediately following the diagram, or both, includes references to syntax segments in Chapter 4, Data Types and Expressions, on page 4-1 or in Chapter 5, Other Syntax Segments, on page 5-1. When the name of an SQL statement includes lowercase characters, such as CREATE Temporary TABLE, it means that the first mixed-lettercase string in the statement name is not an SQL keyword, but that two or more different SQL keywords can follow the preceding uppercase keyword. For an explanation of the structure of statement descriptions, see Chapter 1, Overview of SQL Syntax, on page 1-1.
2-3
ALLOCATE COLLECTION
ALLOCATE COLLECTION
Use the ALLOCATE COLLECTION statement to allocate memory for a variable of a collection data type (such as LIST, MULTISET, or SET) or an untyped collection variable. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. Use this statement with ESQL/C.
Syntax
ALLOCATE COLLECTION variable
Element variable
Usage
The ALLOCATE COLLECTION statement allocates memory for an ESQL/C variable that can store the value of a collection data type. To create a collection variable for an ESQL/C program: 1. Declare the collection variable as a client collection variable in an ESQL/C program. The collection variable can be a typed or untyped collection variable. 2. Allocate memory for the collection variable with the ALLOCATE COLLECTION statement. The ALLOCATE COLLECTION statement sets SQLCODE (that is, sqlca.sqlcode) to zero (0) if the memory allocation was successful and to a negative error code if the allocation failed. You must explicitly release memory with the DEALLOCATE COLLECTION statement. After you free the collection variable with the DEALLOCATE COLLECTION statement, you can reuse the collection variable. Tip: The ALLOCATE COLLECTION statement allocates memory for an ESQL/C collection variable only. To allocate memory for an ESQL/C row variable, use the ALLOCATE ROW statement.
Examples
The following example shows how to allocate resources with the ALLOCATE COLLECTION statement for the untyped collection variable, a_set:
EXEC SQL BEGIN DECLARE SECTION; client collection a_set; EXEC SQL END DECLARE SECTION; . . . EXEC SQL allocate collection :a_set;
The following example uses ALLOCATE COLLECTION to allocate resources for a typed collection variable, a_typed_set:
2-4
ALLOCATE COLLECTION
EXEC SQL BEGIN DECLARE SECTION; client collection set(integer not null) a_typed_set; EXEC SQL END DECLARE SECTION; . . . EXEC SQL allocate collection :a_typed_set;
Related Information
Related examples: Refer to the collection-variable example in PUT. Related statements: ALLOCATE ROW and DEALLOCATE COLLECTION For a discussion of collection data types in ESQL/C programs, see the IBM Informix ESQL/C Programmer's Manual.
2-5
ALLOCATE DESCRIPTOR
Use the ALLOCATE DESCRIPTOR statement to allocate memory for a system-descriptor area (SDA). Use this statement with ESQL/C.
Syntax
ALLOCATE DESCRIPTOR descriptor descriptor_var WITH MAX items items_var
Restrictions
Syntax
Enclose in single ( ) quotes. Must be Quoted String on unique among SDA names page 4-142. Language specific Literal Number on page 4-137 Language specific
Host variable that stores the name of Must contain name of unallocated a system-descriptor area system-descriptor area Number of item descriptors in descriptor. Default value is 100. Host variable that contains the number of items Must be an unsigned INTEGER greater than zero Data type must be INTEGER or SMALLINT
Usage
The ALLOCATE DESCRIPTOR statement creates a system-descriptor area, which is a location in memory that holds information that the DESCRIBE statement can display, or that holds information about the WHERE clause of a query. A system-descriptor area (SDA) contains one or more fields called item descriptors. Each item descriptor holds a data value that the database server can receive or send. The item descriptors also contain information about the data, such as data type, length, scale, precision, and nullability. A system-descriptor area holds information that a DESCRIBE...USING SQL DESCRIPTOR statement obtains or that holds information about the WHERE clause of a dynamically executed statement. If the name that you assign to a system-descriptor area matches the name of an existing system-descriptor area, the database server returns an error. If you free the descriptor with the DEALLOCATE DESCRIPTOR statement, however, you can reuse the same descriptor name.
2-6
ALLOCATE DESCRIPTOR
EXEC SQL allocate descriptor :descname with max :occ; EXEC SQL allocate descriptor desc1 with max 3;
Related Information
Related statements: DEALLOCATE DESCRIPTOR, DECLARE, DESCRIBE, EXECUTE, FETCH, GET DESCRIPTOR, OPEN, PREPARE, PUT, and SET DESCRIPTOR For more information on system-descriptor areas, refer to the IBM Informix ESQL/C Programmer's Manual.
2-7
ALLOCATE ROW
Use the ALLOCATE ROW statement to allocate memory for a row variable. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. Use this statement with ESQL/C.
Syntax
ALLOCATE ROW variable
Element variable
Usage
The ALLOCATE ROW statement allocates memory for a host variable that stores row-type data. To create a row variable, an ESQL/C program must do the following: 1. Declare the row variable. The row variable can be a typed or untyped row variable. 2. Allocate memory for the row variable with the ALLOCATE ROW statement. The following example shows how to allocate resources with the ALLOCATE ROW statement for the typed row variable, a_row:
EXEC SQL BEGIN DECLARE SECTION; row (a int, b int) a_row; EXEC SQL END DECLARE SECTION; . . . EXEC SQL allocate row :a_row;
The ALLOCATE ROW statement sets SQLCODE (the contents of sqlca.sqlcode) to zero (0) if the memory allocation operation was successful, or to a negative error code if the allocation failed. You must explicitly release memory with the DEALLOCATE ROW statement. Once you free the row variable with the DEALLOCATE ROW statement, you can reuse the row variable. Tip: The ALLOCATE ROW statement allocates memory for an ESQL/C row variable only. To allocate memory for an ESQL/C collection variable, use the ALLOCATE COLLECTION statement. When you use the same row variable in multiple function calls without deallocating it, a memory leak on the client computer results. Because there is no way to determine if a pointer is valid when it is passed, ESQL/C assumes that the pointer is not valid and assigns it to a new memory location.
Related Information
Related statements: ALLOCATE COLLECTION and DEALLOCATE ROW For a discussion of complex data types in ESQL/C programs, see the IBM Informix ESQL/C Programmer's Manual.
2-8
ALTER ACCESS_METHOD
The ALTER ACCESS_METHOD statement changes the attributes of a user-defined access method in the sysams system catalog table. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
ALTER ACCESS_METHOD access_method
Notes: 1
Element access _method purpose _keyword Description Name of the access method to alter A keyword that indicates which feature to change
Usage
Use ALTER ACCESS_METHOD to modify the definition of a user-defined access method. You must be the owner of the access method or have DBA privileges to alter an access method. When you alter an access method, you change the purpose-option specifications (purpose functions, purpose methods, purpose flags, or purpose values) that define the access method. For example, you might alter an access method to assign a new user-defined function or method name or to provide a multiplier for the scan cost on a table. If a transaction is in progress, the database server waits to alter the access method until after the transaction is committed or rolled back. No other users can execute the access method until the transaction has completed.
Example
The following statement alters the remote user-defined access method:
ALTER ACCESS_METHOD remote ADD am_scancost = FS_scancost, ADD am_rowids, DROP am_getbyid, MODIFY am_costfactor = 0.9;
2-9
ALTER ACCESS_METHOD
The preceding example makes the following changes to the access method: v Adds a user-defined function or method named FS_scancost( ), which is associated in the sysams table with the am_scancost keyword v Sets (adds) the am_rowids flag v Drops the user-defined function or method associated with the am_getbyid keyword v Modifies the am_costfactor value
Related Information
Related statements: CREATE ACCESS_METHOD and DROP ACCESS_METHOD For information about how to set purpose-option specifications, see Purpose Options on page 5-46. For the schema of the sysams system catalog table, see the IBM Informix Guide to SQL: Reference. For more information on primary-access methods, see the IBM Informix Virtual-Table Interface Programmer's Guide. For more information on secondary-access methods, see the IBM Informix Virtual-Index Interface Programmer's Guide and the IBM Informix User-Defined Routines and Data Types Developer's Guide. For a discussion of privileges, see the GRANT statement in this chapter, or the IBM Informix Database Design and Implementation Guide.
2-10
ALTER FRAGMENT
Use the ALTER FRAGMENT statement to change the distribution strategy or the storage location of an existing table or index. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
ALTER FRAGMENT ON
(1) TABLE surviving_table ATTACH Clause (2) DETACH Clause (3) INIT Clause (4) ADD Clause (5) DROP Clause MODIFY Clause (5) INDEX surviving_index INIT Clause (4) ADD Clause (6) DROP Clause (7) MODIFY Clause (3)
(6) (7)
Notes: 1 2 3 4 5 6 7
Element surviving _index surviving _table Description Index on which to modify the distribution or storage Table on which to modify the distribution or storage
See ATTACH Clause on page 2-13 See DETACH Clause on page 2-19 See INIT Clause on page 2-20 See ADD Clause on page 2-26 Dynamic Server only See DROP Clause (IDS) on page 2-27 See MODIFY Clause (IDS) on page 2-28
Restrictions Must exist when the statement executes Syntax Database Object Name on page 5-17 Database Object Name on page 5-17
Must exist. See Restrictions on the ALTER FRAGMENT Statement on page 2-12.
2-11
ALTER FRAGMENT
Usage
The ALTER FRAGMENT statement applies only to table fragments or index fragments that are located at the current site (or the current cluster, for Extended Parallel Server). No remote information is accessed or updated. You must have the Alter or the DBA privilege to change the fragmentation strategy of a table. You must have the Index or the DBA privilege to alter the fragmentation strategy of an index. Attention: This statement can cause indexes to be dropped and rebuilt. Before undertaking alter operations, check corresponding sections in your IBM Informix Performance Guide to review effects and strategies. Clauses of the ALTER FRAGMENT statement support the following tasks. Clause ATTACH DETACH INIT Effect Combines two or more tables that have the same schema into a single fragmented table Detaches a table fragment (or in Extended Parallel Server, a slice) from a fragmentation strategy, and places it in a new table Provides the following options: v Defines and initializes a fragmentation strategy on a table v Creates a fragmentation strategy for tables v Changes the order of evaluation of fragment expressions v Alters the fragmentation strategy of a table or index v Changes the storage location of an existing table Adds an additional fragment to an existing fragmentation list Drops an existing fragment from a fragmentation list Changes an existing fragmentation expression
Use the CREATE TABLE statement or the INIT clause of the ALTER FRAGMENT statement to create fragmented tables. After a dbspace has been renamed successfully by the onspaces utility, only the new name can reference the renamed dbspace. Existing fragmentation strategies for tables or indexes are automatically updated, however, by the database server to replace the old dbspace name with the new name. You do not need to take any additional action to update a distribution strategy or storage location that was defined using the old dbspace name, but you must use the new name if you reference the dbspace in an ALTER FRAGMENT or ALTER TABLE statement.
2-12
ALTER FRAGMENT
In Dynamic Server, you cannot use ALTER FRAGMENT on a typed table that is part of a table hierarchy.
ATTACH Clause
Use the ATTACH clause of the ALTER FRAGMENT ON TABLE statement to combine tables that have identical structures into a fragmentation strategy. ATTACH Clause:
, (1) ATTACH surviving_table consumed_table AS (2) PARTITION new_frag (1) expr AFTER old_frag BEFORE (2) REMAINDER (3)
Notes: 1 2 3 Use path no more than once Dynamic Server only; required if another surviving_table fragment has same name as dbspace Required for fragmentation by expression; optional for round-robin fragmentation
2-13
ALTER FRAGMENT
Element consumed _table Description Table that loses its identity to be merged with surviving_table Restrictions Syntax
Must exist. Cannot include serial columns Database Object nor unique, referential, or primary key Name on page constraints. See also General Restrictions for 5-17 the ATTACH Clause on page 2-14. Can include only columns from the current table and only data values from a single row. See also General Restrictions for the ATTACH Clause on page 2-14. Must be unique among the names of partitions and of dbspaces that store fragments of surviving_table Must exist. See also Altering Hybrid-Fragmented Tables (XPS) on page 2-16. Condition on page 4-5; Expression on page 4-34 Identifier on page 5-22 Identifier on page 5-22
expr
Expression defining which rows are stored in a fragment of a fragmented-by-expression table Name declared here for consumed_table fragment. Default is the dbspace name. Partition or dbspace where a surviving_table fragment exists Table on which to modify the distribution or storage location
new_frag
old_frag
surviving _table
Must exist. Cannot have any constraints. See Database Object also Restrictions on the ALTER FRAGMENT Name on page Statement on page 2-12. 5-17
To use this clause, you must have the DBA privilege or else be the owner of the specified tables. The ATTACH clause supports the following tasks: v Creates a single fragmented table by combining two or more identically-structured, nonfragmented tables (See Combining Nonfragmented Tables to Create a Fragmented Table on page 2-15.) v Attaches one or more tables to a fragmented table (See Attaching a Table to a Fragmented Table on page 2-15.)
2-14
ALTER FRAGMENT
v If the consumed tables contain data that belongs in some existing fragment of the surviving table v If existing data in the surviving table would belong in a new fragment Thus, you cannot use the ATTACH clause for data movement among fragments. To perform this task, see the INIT Clause on page 2-20.
In Dynamic Server, when you attach one or more tables to a fragmented table, a consumed_table must be nonfragmented. In Extended Parallel Server, when you attach one or more tables to a fragmented table, a consumed_table can be nonfragmented or have hash fragmentation. If you
2-15
ALTER FRAGMENT
specify a consumed_table that has hash fragmentation, the hash column specification must match that of the surviving_table and of any other consumed_table involved in the attach operation.
2-16
ALTER FRAGMENT
What Happens to Triggers and Views?: When you attach tables, triggers on the surviving table survive the ATTACH, but triggers on the consumed table are automatically dropped. No triggers are activated by the ATTACH clause, but subsequent data-manipulation operations on the new rows can activate triggers. Views on the surviving table survive the ATTACH operation, but views on the consumed table are automatically dropped. What Happens with the Distribution Scheme?: You can attach a nonfragmented table to a table with any type of supported distribution scheme. In general, the resulting table has the same fragmentation strategy as the prior fragmentation strategy of the surviving_table. When you attach two or more nonfragmented tables, however, the distribution scheme can either be based on expression or round-robin. For Dynamic Server, only the following distribution schemes can result from combining the distribution schemes of the tables in the ATTACH clause.
Prior Distribution Scheme of Surviving Table None Round-robin Expression Prior Distribution Scheme of Consumed Table None None None Resulting Distribution Scheme Round-robin or expression Round-robin Expression
For Extended Parallel Server, this table shows the distribution schemes that can result from different distribution schemes of the tables in the ATTACH clause.
Prior Distribution Scheme of Surviving Table None None Round-robin Expression Hash Hash Hybrid Hybrid Prior Distribution Scheme of Consumed Table None Hash None None None Hash None Hash Resulting Distribution Scheme Round-robin or expression Hybrid Round-robin Expression Hybrid Hybrid Hybrid Hybrid
In Extended Parallel Server, when you attach a nonfragmented table to a table that has hash fragmentation, the resulting table has hybrid fragmentation. You can attach a table with a hash distribution scheme to a table that currently has no fragmentation, or hash fragmentation, or hybrid fragmentation. In any of these situations, the resulting table has a hybrid distribution scheme. The following examples illustrate the use of the ATTACH clause to create fragmented tables with different distribution schemes. Round-Robin Distribution Scheme: The following example combines nonfragmented tables pen_types and pen_makers into a single, fragmented table, pen_types. Table pen_types resides in dbspace dbsp1, and table pen_makers resides in dbspace dbsp2. Table structures are identical in each table.
ALTER FRAGMENT ON TABLE pen_types ATTACH pen_types, pen_makers
Chapter 2. SQL Statements
2-17
ALTER FRAGMENT
After you execute the ATTACH clause, the database server fragments the table pen_types using a round-robin scheme into two dbspaces: the dbspace that contained pen_types and the dbspace that contained pen_makers. Table pen_makers is consumed and no longer exists; all rows that were in table pen_makers are now in table pen_types. Expression Distribution Scheme: Consider the following example that combines tables cur_acct and new_acct and uses an expression-based distribution scheme. Table cur_acct was originally created as a fragmented table and has fragments in dbspaces dbsp1 and dbsp2. The first statement of the example shows that table cur_acct was created with an expression-based distribution scheme. The second statement of the example creates table new_acct in dbsp3 without a fragmentation strategy. The third statement combines the tables cur_acct and new_acct. Table structures (columns) are identical in each table.
CREATE TABLE cur_acct (a int) FRAGMENT BY EXPRESSION a < 5 in dbsp1, a >= 5 and a < 10 in dbsp2; CREATE TABLE new_acct (a int) IN dbsp3; ALTER FRAGMENT ON TABLE cur_acct ATTACH new_acct AS a>=10;
When you examine the sysfragments system catalog table after you alter the fragment, you see that table cur_acct is fragmented by expression into three dbspaces. For additional information about the sysfragments system catalog table, see the IBM Informix Guide to SQL: Reference. In addition to simple range rules, you can use the ATTACH clause to fragment by expression with hash or arbitrary rules. For a discussion of all types of expressions that you can use in an expression-based distribution scheme, see page Fragmenting by EXPRESSION on page 2-192. Warning: When you specify a date value as the default value for a parameter, make sure to specify 4 digits instead of 2 digits for the year. If you specify a 2-digit year, the setting of the DBCENTURY environment variable can affect how the database server interprets the date value. For more information, see the IBM Informix Guide to SQL: Reference. Hybrid Fragmentation Distribution Scheme (XPS): Consider a case where monthly sales data is added to the sales_info table defined below. Due to the large amount of data, the table is distributed evenly across multiple coservers with a system-defined hash function. To manage monthly additions of data to the table, it is also fragmented by a date expression. The combined hybrid fragmentation is declared in the following CREATE TABLE statement:
CREATE TABLE sales_info (order_num INT, sale_date DATE, ...) FRAGMENT BY HYBRID (order_num) EXPRESSION sale_date >= 01/01/1996 AND sale_date < 02/01/1996 IN sales_slice_9601, sale_date >= 02/01/1996 AND sale_date < 03/01/1996 IN sales_slice_9602, . . . sale_date >= 12/01/1996 AND sale_date < 01/01/1997 IN sales_slice_9612;
Values for a new month are originally loaded from an external source. The data values are distributed evenly across the same coservers on which the sales_info table is defined, using a system-defined hash function on the same column:
CREATE TABLE jan_97 (order_num INT, sale_date DATE, ...) FRAGMENT BY HASH (order_num) IN sales_slice_9701 INSERT INTO jan_97 SELECT (...) FROM ... ;
2-18
ALTER FRAGMENT
After data values are loaded, you can attach the new table to sales_info. You can issue the following ALTER FRAGMENT statement to attach the new table:
ALTER FRAGMENT ON TABLE sales_info ATTACH jan_97 AS sale_date >= 01/01/1997 AND sale_date < 02/01/1997
DETACH Clause
Use the DETACH clause of the ALTER FRAGMENT ON TABLE statement to detach a table fragment from a distribution scheme and place the contents into a new nonfragmented table. This clause is not valid in ALTER FRAGMENT ON INDEX statements. In Extended Parallel Server, the new table can also be a table with hash fragmentation. For an explanation of distribution schemes, see FRAGMENT BY Clause on page 2-190. DETACH Clause:
DETACH (1) PARTITION fragment new_table
Notes: 1
Element fragment Description Partition (IDS only), dbspace, or dbslice (XPS only) that contains the table fragment to be detached. With hybrid-fragmentation, dbslice identifies a set of dbspaces. See Altering Hybrid-Fragmented Tables (XPS) on page 2-16.
new_table
Nonfragmented table that results after you execute the ALTER Must not exist FRAGMENT statement. (In XPS, this can also be a hash-fragmented before table.) execution
The new table that results from executing the DETACH clause does not inherit any indexes nor constraints from the original table. Only data values remain. Similarly, the new table does not inherit any privileges from the original table. Instead, the new table has the default privileges of any new table. For further information on default table-level privileges, see the GRANT statement on Table-Level Privileges on page 2-374. The DETACH clause cannot be applied to a table if that table is the parent of a referential constraint or if a rowid column is defined on the table. In Dynamic Server, if you omit the PARTITION keyword, the name of the fragment must be the name of the dbspace where the fragment is stored. In Extended Parallel Server, you cannot use the DETACH clause if the table has a dependent generalized key (GK) index defined on it.
2-19
ALTER FRAGMENT
This example detaches dbsp2 from the distribution scheme for cur_acct and places the rows in a new table, accounts. Table accounts now has the same structure (column names, number of columns, data types, and so on) as table cur_acct, but the table accounts does not contain any indexes or constraints from the table cur_acct. Both tables are now nonfragmented. The following example shows a table that contains three fragments:
ALTER FRAGMENT ON TABLE bus_acct DETACH dbsp3 cli_acct
This statement detaches dbsp3 from the distribution scheme for bus_acct and places the rows in a new table, cli_acct. Table cli_acct now has the same structure (column names, number of columns, data types, and so on) as bus_acct, but the table cli_acct does not contain any indexes or constraints from the table bus_acct. Table cli_acct is a nonfragmented table, and table bus_acct remains a fragmented table.
In this example, data from January 1996 is detached from the sales_info table and placed in a new table called jan_96.
INIT Clause
The INIT clause of the ALTER FRAGMENT statement can define or modify the fragmentation strategy or the storage location of an existing table or (for Dynamic Server) an existing index. The INIT clause has the following syntax. INIT Clause:
(2) INIT (1) WITH ROWIDS (1) (1) FRAGMENT BY Clause for Tables dbspace (3) PARTITION fragment dbslice (4) FRAGMENT BY Clause for Indexes IN
2-20
ALTER FRAGMENT
Notes: 1 2 3 4 Dynamic Server only See 2-22 Extended Parallel Server only See FRAGMENT BY Clause for Indexes (IDS) on page 2-24
Description Dbslice storing fragmented data Dbspace storing fragmented data Partition within dbspace Restrictions Must exist at time of execution Must exist at time of execution No more than 2048 for same table Syntax Identifier on page 5-22 Identifier on page 5-22 Identifier on page 5-22
The INIT clause can accomplish tasks that include the following: v Move a nonfragmented table from one partition (IDS only) or dbslice (XPS only) or dbspace to another partition or to another dbslice or dbspace. v Convert a fragmented table to a nonfragmented table. v Fragment an existing non-fragmented table without redefining it. v Convert a fragmentation strategy to another fragmentation strategy. v Fragment an existing index that is not fragmented without redefining the index (IDS only). v Convert a fragmented index to a nonfragmented index (IDS only). v Add a new rowid column to the table definition (IDS only). With Extended Parallel Server, you cannot use the INIT clause to change the fragmentation strategy of a table that has a GK index. When you use the INIT clause to modify a table, the tabid value in the system catalog tables changes for the affected table. The constrid value of all unique and referential constraints on the table also changes. For more information about the storage spaces in which you can store a table, see Using the IN Clause on page 2-189. Attention: When you execute the ALTER FRAGMENT statement with this clause, it results in data movement if the table contains any data. If data values move, the potential exists for significant logging, for the transaction being aborted as a long transaction, and for a relatively long exclusive lock being held on the affected tables. Use this statement when it does not interfere with day-to-day operations.
2-21
ALTER FRAGMENT
When you use the WITH ROWIDS option to add a new rowid column to a fragmented table, the database server assigns a unique rowid number to each row and creates an index that it can use to find the physical location of the row. Performance using this access method is comparable to using a SERIAL or SERIAL8 column. The rowid value of a row cannot be updated, but remains stable during the existence of the row. Each row requires an additional four bytes to store the rowid column after you specify the WITH ROWIDS option. Attention: It is recommended that users creating new applications move toward using primary keys as a method of row identification instead of using rowid values.
You must use the IN dbspace clause to place the table in a dbspace explicitly. When you use the INIT clause to change a fragmented table to a nonfragmented table, all attached indexes become nonfragmented indexes. In addition, constraints that do not use existing user-defined indexes (detached indexes) become nonfragmented indexes. All newly nonfragmented indexes exist in the same dbspace as the new nonfragmented table. Using the INIT clause to change a fragmented table to a nonfragmented table has no effect on the fragmentation strategy of detached indexes, nor of constraints that use detached indexes. Use the FRAGMENT BY portion of the INIT clause to fragment an existing non-fragmented table, or to convert one fragmentation strategy to another. FRAGMENT BY Clause for Tables:
, FRAGMENT BY (1) PARTITION BY , (1) IN dbspace EXPRESSION ( (2) HASH ( , (2) HYBRID ( column ) Expression List column ) IN (dbspace, dbslice dbspace ) PARTITION part Fragment List ) , ROUND ROBIN IN dbspace (2) dbslice
2-22
ALTER FRAGMENT
Fragment List:
, ( expr ) (3) REMAINDER IN dbspace
Expression List:
, EXPRESSION expr (3) REMAINDER ( dbspace dbspace ) IN dbslice ,
Notes: 1 2 3 Dynamic Server only Extended Parallel Server only If included, must be the last item in fragment list
Description Column to which the strategy applies Dbslice that contains the table fragment Dbspace that contains the table fragment Expression that defines a table fragment Name declared here for a partition of dbspace that contains the table fragment Restrictions Must exist in the table Must be defined Must specify at least 2 but no more than 2,048 dbspaces Must evaluate to a Boolean value (t or f) Must be unique among names of fragments of table in dbspace Syntax Identifier on page 5-22 Identifier on page 5-22 Identifier on page 5-22 Expression on page 4-34 Identifier on page 5-22
In the HYBRID option of Extended Parallel Server, column identifies a column on which to apply the hash portion of the hybrid table fragmentation strategy. The expression can reference columns only from the current table and data values only from a single row. Subqueries, aggregates, and the built-in CURRENT, DATE, and TODAY functions are not valid in the expression. For more information on the available fragmentation strategies for tables, see the FRAGMENT BY Clause on page 2-190.
2-23
ALTER FRAGMENT
The following example shows the statement that originally defined the fragmentation strategy on the table account and then shows an ALTER FRAGMENT statement that redefines the fragmentation strategy:
CREATE TABLE account (col1 INT, col2 INT) FRAGMENT BY ROUND ROBIN IN dbsp1, dbsp2; ALTER FRAGMENT ON TABLE account INIT FRAGMENT BY EXPRESSION col1 < 0 IN dbsp1, col2 >= 0 IN dbsp2;
If an existing dbspace is full when you redefine a fragmentation strategy, you must not use it in the new fragmentation strategy.
In Dynamic Server, when you use the INIT clause to fragment an existing nonfragmented table, all indexes on the table become fragmented in the same way as the table. In Extended Parallel Server, when the INIT clause fragments an existing nonfragmented table, any indexes retain their existing fragmentation strategy.
2-24
ALTER FRAGMENT
Element dbspace expr Description Dbspace that contains the fragmented information Expression defining an index fragment Restrictions Must specify at least two, but no more than 2,048 dbspaces Must return a Boolean value Syntax Identifier on page 5-22 Condition on page 4-5; Expression on page 4-34 Identifier on page 5-22
part
Name that you declare here for a partition of the specified dbspace
Required for any partition in the same dbspace as another partition of the same index
The keywords FRAGMENT BY and PARTITION BY are synonyms in this context. You can convert an existing fragmentation strategy to another expression-based fragmentation strategy. Dynamic Server discards the existing fragmentation strategy and moves the data records to fragments that you define in the new fragmentation strategy. (To convert an existing fragmented index to a nonfragmented index, you can use the INIT clause to specify IN dbspace or else PARTITION partition IN dbspace as the only storage specification for a previously fragmented index.) The expression can contain only columns from the current table and data values from only a single row. No subqueries nor aggregates are allowed. The built-in CURRENT, DATE, and TODAY functions are not valid here.
2-25
ALTER FRAGMENT
ADD Clause
Use the ADD clause to add another fragment to an existing fragment list for a table or (for Dynamic Server only) for an index. ADD Clause:
ADD REMAINDER IN (1) PARTITION part expression IN dbspace (1) PARTITION part BEFORE AFTER fragment (1) PARTITION part IN dbspace
Name of an existing fragment in the Must exist at the time when you Identifier on page fragmentation list execute the statement 5-22 Expression that defines the new fragment that is to be added Must return a Boolean value (t or f) Condition on page 4-5; Expression on page 4-34
dbspace part
Name of dbspace to be added to the Must exist at the time when you Identifier on page fragmentation scheme execute the statement 5-22 Name that you declare here for the fragment. The default name is the name of the dbspace Required for any partition in the same dbspace as another partition of the same index Identifier on page 5-22
The expression can contain column names only from the current table and data values only from a single row. No subqueries or aggregates are allowed. In addition, the built-in current, date, and time functions are not valid here.
To add a new fragment that is stored in a partition within a dbspace, you can use the ADD clause, as in the following example:
2-26
ALTER FRAGMENT
ALTER FRAGMENT ON TABLE book ADD PARTITION chapter3 IN dbsp1;
The new distribution uses dbsp1, dbsp4, and chapter3 as storage locations for a 3-part round-robin fragmentation scheme. The records in the fragment chapter3 are stored in a separate partition of dbsp1 from the records in the first fragment, which are stored in a partition whose default name is dbsp1. (The database server issues an error if you attempt to declare the same name for different fragments of the same fragmented table or index.)
To add another fragment in a new partition of dbspace dbsp2 to hold rows for c1 values between 200 and 299, use the following ALTER FRAGMENT statement:
ALTER FRAGMENT ON TABLE news ADD PARTITION century3 (c1 >= 200 AND c1 < 300) IN dbsp2;
Any rows that were formerly in the remainder fragment but that fit the criteria (c1 >= 200 AND c1 < 300) move to the new century3 partition in dbspace dbsp2.
2-27
ALTER FRAGMENT
Element fragment Description Name of a partition of the dbspace that stores the dropped fragment Restrictions Must exist when you execute the statement Syntax Identifier on page 5-22
If the table is fragmented by expression, you cannot drop a fragment containing data that cannot be moved to another fragment. (If the distribution scheme has a REMAINDER option, or if the expressions overlap, you can drop a fragment that contains data.) You cannot drop a fragment if the table has only two fragments. When you want to make a fragmented table nonfragmented, use either the INIT clause or the DETACH clause of the ALTER FRAGMENT statement. If the fragment was not given a name when it was created or added then the name of the dbspace is also the name of the fragment. When you drop a fragment, the underlying partition or dbspace is not affected. Only the fragment data values within that partition or dbspace are affected. When you drop a fragment, the database server attempts to move all records in the dropped fragment to another fragment. In this case, the destination fragment might not have enough space for the additional records. If this happens, follow one of the procedures that ALTER FRAGMENT and Transaction Logging on page 2-13 describes to increase your available space, and retry the procedure. The following examples show how to drop a fragment from a fragmentation list. The first line shows how to drop an index fragment, and the second line shows how to drop a table fragment.
ALTER FRAGMENT ON INDEX cust_indx DROP dbsp2; ALTER FRAGMENT ON TABLE customer DROP dbsp1;
2-28
ALTER FRAGMENT
Element dbspace expression Description Dbspace that stores the modified information Modified expression Restrictions Must exist at time of execution Can specify columns in current table only and data from only a single row Must be unique in fragmentation list among names of partitions of dbspace Must exist at time of execution Syntax Identifier on page 5-22 Condition on page 4-5; Expression on page 4-34 Identifier on page 5-22 Identifier on page 5-22
new old
Name that you declare here for a partition of dbspace Name of an existing fragment
Here dbspace and old (or old and new) can be identical, if you are not changing the storage location. You must declare a new partition (using the PARTITION keyword) if more than one fragment of the same table or index is named the same as the dbspace. The expression must evaluate to a Boolean value (true or false). No subqueries or aggregates are allowed in the expression. In addition, the built-in CURRENT, DATE, and TODAY functions are not valid. When you use the MODIFY clause, the underlying dbspaces are not affected. Only the fragment data values within the partitions or dbspaces are affected. You cannot change a REMAINDER fragment into a nonremainder fragment if records within the REMAINDER fragment do not satisfy the new expression. When you use the MODIFY clause to change an expression without changing the storage location for the expression, you must use the same name for the old and the new fragment (or else the same name for old and for dbspace, if the dbspace consists of only a single partition, as in the following example):
ALTER FRAGMENT ON TABLE cust_acct MODIFY dbsp1 TO acct_num < 65 IN dbsp1
When you use the MODIFY clause to move an expression from one dbspace to another, old is the name of the dbspace where the expression was previously located, and dbspace is the new location for the expression:
ALTER FRAGMENT ON TABLE cust_acct MODIFY dbsp1 TO acct_num < 35 IN dbsp2
Here the distribution scheme for the cust_acct table is modified so that all row items in column acct_num that are less than 35 are now contained in the dbspace dbsp2. These items were formerly contained in the dbspace dbsp1. When you use the MODIFY clause to move an expression from one partition of a dbspace to another partition, old is the name of the partition where information fragmented by the expression was previously located, and new is the name of the partition that is the new location for the expression, as in the following example:
ALTER FRAGMENT ON TABLE cust_acct MODIFY PARTITION part1 TO PARTITION part2 (acct_num < 35) IN dbsp2
2-29
ALTER FRAGMENT
Here the distribution scheme for the cust_acct table is modified so that all row items in column acct_num that have a value less than 35 are now contained in the partition part2 of dbspace dbsp2. These items were formerly contained in the partition part1. To use the MODIFY clause both to change the expression and to move its corresponding fragment to a new storage location, you must change the expression and you must also specify the name of a different dbspace or partition. If the indexes on a table are attached indexes, and you modify the table, the index fragmentation strategy is also modified.
Related Information
Related statements: CREATE TABLE, CREATE INDEX, and ALTER TABLE For a discussion of fragmentation strategy, refer to the IBM Informix Database Design and Implementation Guide. For information on how to maximize performance when you make fragment modifications, see your IBM Informix Performance Guide.
2-30
ALTER FUNCTION
Use the ALTER FUNCTION statement to change the routine modifiers or pathname of a user-defined function. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
, ALTER FUNCTION function ( parameter_type (1) Specific Name )
SPECIFIC FUNCTION ,
(2) WITH( ADD Routine Modifier MODIFY DROP (3) MODIFY EXTERNAL NAME = )
Notes: 1 2 3 4
Element function
See Specific Name on page 5-68 See Routine Modifier on page 5-54 External routines only See Shared-Object Filename on page 5-65
Restrictions Must be registered in the database. If the name does not uniquely identify a function, you must enter one or more appropriate values for parameter_type. Must be the same data types (and specified in the same order) as in the definition of function Syntax Database Object Name on page 5-17 Identifier on page 5-22
parameter_type
Usage
The ALTER FUNCTION statement can modify a user-defined function to tune its performance by modifying characteristics that control how the function executes. You can also add or replace related user-defined routines (UDRs) that provide alternatives for the query optimizer, which can improve performance. All modifications take effect on the next invocation of the function. Only the UDR owner or the DBA can use the ALTER FUNCTION statement.
2-31
ALTER FUNCTION
WITH
If the routine modifier is a BOOLEAN value, MODIFY sets the value to be t (equivalent of using the keyword ADD to add the routine modifier). For example, both of the following statements alter the func1 function so that it can be executed in parallel in the context of a parallelizable data query:
ALTER FUNCTION func1 WITH (MODIFY PARALLELIZABLE) ALTER FUNCTION func1 WITH (ADD PARALLELIZABLE)
Related Information
Related Statements: ALTER PROCEDURE, ALTER ROUTINE, CREATE FUNCTION, and CREATE PROCEDURE For a discussion of how to create and use SPL routines, see the IBM Informix Guide to SQL: Tutorial. For a discussion of how to create and use external routines, see IBM Informix User-Defined Routines and Data Types Developer's Guide and the IBM Informix DataBlade API Programmer's Guide.
2-32
ALTER INDEX
Use the ALTER INDEX statement to change the clustering attribute or the locking mode of an existing index. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
(1) ALTER INDEX index (2) LOCK MODE NORMAL COARSE TO NOT CLUSTER
Notes: 1 2
Element index Description Name of the index to be altered
Usage
ALTER INDEX is valid only for indexes that the CREATE INDEX statement created explicitly. ALTER INDEX cannot modify an index on a temporary table, nor an index that the database server created implicitly to support a constraint. You cannot change the collating order of an existing index. If you use ALTER INDEX to modify an index after the SET COLLATION statement of Dynamic Server has specified a non-default collating order, the SET COLLATION statement has no effect on the index.
TO CLUSTER Option
The TO CLUSTER option causes the database server to reorder the rows of the physical table according to the indexed order. The next example shows how you can use the ALTER INDEX TO CLUSTER statement to order the rows in the orders table physically. The CREATE INDEX statement creates an index on the customer_num column of the table. Then the ALTER INDEX statement causes the physical ordering of the rows.
CREATE INDEX ix_cust ON orders (customer_num); ALTER INDEX ix_cust TO CLUSTER;
For an ascending index, TO CLUSTER puts rows in lowest-to-highest order. For a descending index, the rows are reordered in highest-to-lowest order. When you reorder, the entire file is rewritten. This process can take a long time, and it requires sufficient disk space to maintain two copies of the table. While a table is clustering, it is locked IN EXCLUSIVE MODE. When another process is using the table to which the index name belongs, the database server
Chapter 2. SQL Statements
2-33
ALTER INDEX
cannot execute the ALTER INDEX statement with the TO CLUSTER option; it returns an error unless lock mode is set to WAIT. (When lock mode is set to WAIT, the database server retries the ALTER INDEX statement.) Over time, if you modify the table, you can expect the benefit of an earlier cluster to disappear because rows are added in space-available order, not sequentially. You can recluster the table to regain performance by issuing another ALTER INDEX TO CLUSTER statement on the clustered index. You do not need to drop a clustered index before you issue another ALTER INDEX TO CLUSTER statement on a currently clustered index. Extended Parallel Server cannot use the CLUSTER option on STANDARD tables.
The first two statements create indexes for the orders table and cluster the physical table in ascending order on the customer_num column. The last two statements recluster the physical table in ascending order on the order_num column.
Related Information
Related statements: CREATE INDEX and CREATE TABLE For a discussion of the performance implications of clustered indexes, see your IBM Informix Performance Guide.
2-34
ALTER PROCEDURE
Use the ALTER PROCEDURE statement to change the routine modifiers or pathname of a previously defined external procedure. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
, ALTER PROCEDURE procedure ( parameter_type (1) Specific Name )
SPECIFIC PROCEDURE ,
(2) WITH( ADD Routine Modifier MODIFY DROP (3) MODIFY EXTERNAL NAME = )
Notes: 1 2 3 4
Element procedure
See Specific Name on page 5-68 See Routine Modifier on page 5-54 External routines only See Shared-Object Filename on page 5-65
Restrictions Must be registered in the database. If the name does not uniquely identify a function, you must enter one or more appropriate values for parameter_type. Must be the same data types (and specified in the same order) as in the definition of procedure Syntax Database Object Name on page 5-17 Identifier on page 5-22
parameter_type
Usage
The ALTER PROCEDURE statement enables you to modify an external procedure to tune its performance by modifying characteristics that control how it executes. You can also add or replace related UDRs that provide alternatives for the optimizer, which can improve performance. All modifications take effect on the next invocation of the procedure. Only the UDR owner or the DBA can use the ALTER PROCEDURE statement. If the procedure name is not unique among routines registered in the database, you must enter one or more appropriate values for parameter_type.
2-35
ALTER PROCEDURE
The following keywords introduce what you want to modify in procedure.
Keyword ADD MODIFY DROP MODIFY EXTERNAL NAME (for external procedures only) Effect Add a new routine modifier to the UDR Change an attribute of a routine modifier Delete a routine modifier from the UDR Replace the file specification of the executable file. When the IFX_EXTEND_ROLE configuration parameter = ON, this option is valid only for users to whom the DBSA has granted the EXTEND role. With IFX_EXTEND_ROLE = OFF (or not set), the UDR owner or the DBA can use this option. Replace the file specification of the executable file. (Valid only for users who have the EXTEND role) Introduces all modifications
If the routine modifier is a BOOLEAN value, MODIFY sets the value to be T (equivalent to using the keyword ADD to add the routine modifier). For example, both of the following statements alter the proc1 procedure so that it can be executed in parallel in the context of a parallelizable data query:
ALTER PROCEDURE proc1 WITH (MODIFY PARALLELIZABLE) ALTER PROCEDURE proc1 WITH (ADD PARALLELIZABLE)
Related Information
Related statements: ALTER ROUTINE, CREATE PROCEDURE, DROP PROCEDURE, and DROP ROUTINE. For a discussion of SPL routines, see the IBM Informix Guide to SQL: Tutorial. For a discussion of how to create and use external routines, see IBM Informix User-Defined Routines and Data Types Developer's Guide. For information about how to create C UDRs, see the IBM Informix DataBlade API Programmer's Guide.
2-36
ALTER ROUTINE
Use the ALTER ROUTINE statement to change the routine modifiers or pathname of a previously defined user-defined routine (UDR). Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
, ALTER ROUTINE routine ( parameter_type (1) Specific Name )
SPECIFIC ROUTINE ,
(2) WITH( ADD Routine Modifier MODIFY DROP (3) MODIFY EXTERNAL NAME = )
Notes: 1 2 3 4
Element routine
See Specific Name on page 5-68 See Routine Modifier on page 5-54 External routines only See Shared-Object Filename on page 5-65
Restrictions Must be registered in the database. If the name does not uniquely identify a routine, you must enter one or more appropriate values for parameter_type. Must be the same data types (and specified in the same order) as in the definition of routine Syntax Database Database Object Name on page 5-17 Identifier on page 5-22
parameter_type
Usage
The ALTER ROUTINE statement allows you to modify a previously defined UDR to tune its performance by modifying characteristics that control how the UDR executes. You can also add or replace related UDRs that provide alternatives for the optimizer, which can improve performance. This statement is useful when you do not know whether a UDR is a user-defined function or a user-defined procedure. When you use this statement, the database server alters the appropriate user-defined procedure or user-defined function. All modifications take effect on the next invocation of the UDR. Only the UDR owner or the DBA can use the ALTER ROUTINE statement.
2-37
ALTER ROUTINE
Restrictions
If the name does not uniquely identify a UDR, you must enter one or more appropriate values for parameter_type. When you use this statement, the type of UDR cannot be ambiguous. The UDR that you specify must refer to either a user-defined function or a user-defined procedure. If either of the following conditions exist, the database server returns an error: v The name (and parameters) that you specify applies to both a user-defined procedure and a user-defined function. v The specific name that you specify applies to both a user-defined function and a user-defined procedure.
WITH
If the routine modifier is a BOOLEAN value, MODIFY sets the value to be T (equivalent to using the keyword ADD to add the routine modifier). For example, both of the following statements alter the func1 UDR so that it can be executed in parallel in the context of a parallelizable data query statement:
ALTER ROUTINE func1 WITH (MODIFY PARALLELIZABLE) ALTER ROUTINE func1 WITH (ADD PARALLELIZABLE)
Because the name func1 is not unique to the database, the data type parameters are specified so that the routine signature is unique. If this function had a Specific Name, for example, raise_sal, specified when it was created, you could identify the function with the following first line:
ALTER SPECIFIC ROUTINE raise_sal
2-38
ALTER ROUTINE
Related Information
Related Statements: ALTER FUNCTION, ALTER PROCEDURE, CREATE FUNCTION, CREATE PROCEDURE, DROP FUNCTION, DROP PROCEDURE, and DROP ROUTINE. For a discussion of how to create and use SPL routines, see the IBM Informix Guide to SQL: Tutorial. For a discussion of how to create and use external routines, see IBM Informix User-Defined Routines and Data Types Developer's Guide. For information about how to create C UDRs, see the IBM Informix DataBlade API Programmer's Guide.
2-39
ALTER SEQUENCE
Use the ALTER SEQUENCE statement to modify the definition of a sequence object. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
ALTER SEQUENCE owner. sequence
(1) (1)
NOCYCLE CYCLE CACHE size NOCACHE ORDER NOORDER BY INCREMENT step WITH RESTART NOMAXVALUE MAXVALUE max NOMINVALUE MINVALUE min restart
Notes: 1
Element max min owner restart sequence size step Description New upper limit on values New lower limit on values Owner of sequence New first value in sequence Name of existing sequence New number of values to preallocate in memory New interval between successive values
2-40
ALTER SEQUENCE
Usage
ALTER SEQUENCE redefines an existing sequence object. It only affects subsequently generated values (and any unused values in the sequence cache). You cannot use the ALTER SEQUENCE statement to rename a sequence nor to change the owner of a sequence. You must be the owner, or the DBA, or else have the Alter privilege on the sequence to modify its definition. Only elements of the sequence definition that you specify explicitly in the ALTER SEQUENCE statement are modified. An error occurs if you make contradictory changes, such as specifying both MAXVALUE and NOMAXVALUE, or both the CYCLE and NOCYCLE options.
INCREMENT BY Option
Use the INCREMENT BY option to specify a new interval between successive numbers in a sequence. The interval, or step value, can be a positive whole number (for ascending sequences) or a negative whole number (for descending sequences) in the INT8 range. The BY keyword is optional.
2-41
ALTER SEQUENCE
NOCYCLE attribute. After an ascending sequence reaches max, it generates the min value for the next value. After a descending sequence reaches min, it generates the max value for the next sequence value. Use the NOCYCLE option to prevent the sequence from generating more values after reaching the declared limit. Once the sequence reaches the limit, the next reference to sequence.NEXTVAL returns an error message.
Related Information
Related statements: CREATE SEQUENCE, DROP SEQUENCE, RENAME SEQUENCE, CREATE SYNONYM, DROP SYNONYM, GRANT, REVOKE, INSERT, UPDATE, and SELECT For information about the syssequences system catalog table in which sequence objects are registered, see the IBM Informix Guide to SQL: Reference. For information about initializing, generating, or reading values from a sequence, see NEXTVAL and CURRVAL Operators (IDS) on page 4-61.
2-42
ALTER TABLE
Use the ALTER TABLE statement to modify the definition of an existing table.
Syntax
(1) ALTER TABLE table synonym Basic Table Options (2) Logging TYPE Options
Notes: 1 2
Element synonym table Description Synonym for the table to be altered Name of table to be altered
Usage
In Dynamic Server, the database server performs the actions in the ALTER TABLE statement in the order that you specify. If any action fails, the entire operation is cancelled. Altering a table on which a view depends might invalidate the view. Warning: The clauses available with this statement have varying performance implications. Before you undertake alter operations, check corresponding sections in your IBM Informix Performance Guide to review effects and strategies. You cannot alter a temporary table. You also cannot alter a violations or diagnostics table. In addition, you cannot add, drop, or modify a column if the table that contains the column has a violation table or a diagnostics table associated with it. If the USETABLENAME environment variable is set, you cannot specify a synonym for the table in the ALTER TABLE statement. In Extended Parallel Server, if a table has range fragmentation, only the Logging TYPE options and LOCK MODE clause are valid. All other ALTER TABLE options return an error. For a RAW or (in Extended Parallel Server) a STATIC table, the Logging TYPE options are the only part of the ALTER TABLE statement that you can use. To use ALTER TABLE, you must meet one of the following conditions: v You must have DBA privilege on the database containing the table. v You must own the table (and for Extended Parallel Server, you must also have the Resource privilege). v You must have the Alter privilege on the specified table and the Resource privilege on the database where the table resides.
Chapter 2. SQL Statements
2-43
ALTER TABLE
v To add a referential constraint, you must have the DBA or References privilege on either the referenced columns or the referenced table. v To drop a constraint, you must have the DBA privilege or be the owner of the constraint. If you are the owner of the constraint but not the owner of the table, you must have Alter privilege on the specified table. You do not need the References privilege to drop a constraint.
(7)
Notes: 1 2 3 4 5 6 7 8 9 10 11 See page 2-45 See page 2-59 See page 2-53 See page 2-61 See page 2-52 Use path once See page 2-61 See page 2-62 Dynamic Server only See page 2-58 See page 2-64
You can use the Basic Table Options segment to modify the schema of a table by adding, modifying, or dropping columns and constraints, or changing the extent
2-44
ALTER TABLE
size or locking granularity of a table. The database server performs alterations in the order that you specify. If any of the actions fails, the entire operation is cancelled. With Dynamic Server, you can also associate a table with a named ROW type or specify a new storage space to store large-object data. You can also add or drop rowid columns and shadow columns for Enterprise Replication. You cannot, however, specify these options in conjunction with any other alterations.
ADD Clause
Use the ADD Column clause to add a column to a table. ADD Column Clause:
, ADD ( New Column New Column )
New Column:
2-45
ALTER TABLE
(1) new_column Data Type (2) DEFAULT Clause
BEFORE column
Notes: 1 2 3
Element column new_column Description Name of column before which new_column is to be placed Name of column that you are adding
The following restrictions apply to the ADD clause: v You cannot add a serial column to a table that contains data. v In Extended Parallel Server, you cannot add a column to a table that has a bit-mapped index. v You cannot add columns beyond the maximum row size of 32,767 bytes.
If you do not include the BEFORE option, the database server adds the new column or list of columns to the end of the table definition by default.
DEFAULT Clause
Use the DEFAULT clause to specify value that the database server should insert in a column when an explicit value for the column is not specified.
2-46
ALTER TABLE
DEFAULT Clause:
DEFAULT literal NULL USER (1) CURRENT (2) DATETIME Field Qualifier TODAY SITENAME DBSERVERNAME
Notes: 1 2
Element literal Description Literal default value for the column
You cannot specify a default value for serial columns. If the table that you are altering already has rows in it when you add a column that contains a default value, the database server inserts the default value for all pre-existing rows. The following example adds a column to the items table. In items, the new column item_weight has a literal default value:
ALTER TABLE items ADD item_weight DECIMAL (6, 2) DEFAULT 2.00 BEFORE total_price
In this example, each existing row in the items table has a default value of 2.00 for the item_weight column. For more information about the options of the DEFAULT clause, refer to DEFAULT Clause on page 2-174.
2-47
ALTER TABLE
UNIQUE (1) DISTINCT PRIMARY KEY (3) REFERENCES Clause (4) CHECK Clause
(2)
Notes: 1 2 3 4 Informix extension See page 2-49 See page 2-49 See page 2-51
You cannot specify a primary-key or unique constraint on a new column if the table contains data. In the case of a unique constraint, however, the table can contain a single row of data. When you want to add a column with a primary-key constraint, the table must be empty when you issue the ALTER TABLE statement. The following rules apply when you place primary-key or unique constraints on existing columns: v When you place a primary-key or unique constraint on a column or set of columns, the database server creates an internal B-tree index on the constrained column or set of columns unless a user-created index was defined on the column or set of columns. v When you place a primary-key or unique constraint on a column or set of columns, and a unique index already exists on that column or set of columns, the constraint shares the index. If the existing index allows duplicates, however, the database server returns an error. You must then drop the existing index before you add the constraint. v When you place a primary-key or unique constraint on a column or set of columns, and a referential constraint already exists on that column or set of columns, the duplicate index is upgraded to UNIQUE (if possible), and the index is shared. You cannot place a unique constraint on a BYTE or TEXT column, nor can you place referential constraints on columns of these types. A check constraint on a BYTE or TEXT column can check only for IS NULL, IS NOT NULL, or LENGTH. When you place a referential constraint on a column or set of columns, and an index already exists on that column or set of columns, the index is upgraded to UNIQUE (if possible) and the index is shared.
2-48
ALTER TABLE
Constraint Definition
In Dynamic Server, use the Constraint Definition portion of the ALTER TABLE statement to declare the name of a constraint and to set the mode of the constraint to disabled, enabled, or filtering. In Extended Parallel Server, use the Constraint Definition portion of the ALTER TABLE statement to declare the name of a constraint. Constraint Definition:
CONSTRAINT constraint
Notes: 1
Element constraint Description Name declared here for the constraint
For more information about constraint-mode options, see Choosing a Constraint-Mode Option (IDS) on page 2-183.
REFERENCES Clause
The REFERENCES clause has the following syntax. REFERENCES Clause:
REFERENCES table , ( column ) (1) ON DELETE CASCADE
Notes: 1
Element column table Description
Informix extension
Restrictions Syntax
Referenced column in the See Restrictions on Referential Constraints Identifier, p. 5-22 referenced table on page 2-50. The referenced table The referenced and the referencing tables must reside in the same database Database Object Name, p. 5-17
The REFERENCES clause allows you to place a foreign-key reference on a column. The referenced column can be in the same table as the referencing column, or in a different table in the same database.
2-49
ALTER TABLE
If the referenced table is different from the referencing table, the default column is the primary-key column. If the referenced table is the same as the referencing table, there is no default.
When you place a referential constraint on a column or set of columns, and a duplicate or unique index already exists on that column or set of columns, the index is shared.
2-50
ALTER TABLE
For example, in the stores_demo database, the stock table contains the stock_num column as a primary key. The catalog table refers to the stock_num column as a foreign key. The following ALTER TABLE statements drop an existing foreign-key constraint (without cascading delete) and add a new constraint that specifies cascading deletes:
ALTER TABLE catalog DROP CONSTRAINT aa ALTER TABLE catalog ADD CONSTRAINT (FOREIGN KEY (stock_num, manu_code) REFERENCES stock ON DELETE CASCADE CONSTRAINT ab)
With cascading deletes specified on the child table, in addition to deleting a stock item from the stock table, the delete cascades to the catalog table that is associated with the stock_num foreign key. This cascading delete works only if the stock_num that you are deleting was not ordered; otherwise, the constraint from the items table would disallow the cascading delete. For more information, see Restrictions on DELETE When Tables Have Cascading Deletes on page 2-277. If a table has a trigger with a DELETE trigger event, you cannot define a cascading-delete referential constraint on that table. You receive an error when you attempt to add a referential constraint that specifies ON DELETE CASCADE to a table that has a delete trigger. For information about syntax restrictions and locking implications when you delete rows from tables that have cascading deletes, see Considerations When Tables Have Cascading Deletes on page 2-276.
CHECK Clause
A check constraint designates a condition that must be met before data can be inserted into a column. CHECK Clause:
(1) CHECK ( Condition )
During an insert or update, if a row returns false for any check constraint defined on a table, the database server returns an error. No error is returned, however, if a row returns NULL for a check constraint. In some cases, you might want to use both a check constraint and a NOT NULL constraint. Check constraints are defined using search conditions. The search condition cannot contain user-defined routines, subqueries, aggregates, host variables, or rowids. In addition, the condition cannot contain the variant built-in functions CURRENT, USER, SITENAME, DBSERVERNAME, or TODAY.
2-51
ALTER TABLE
The check constraint cannot include columns in different tables. When you are using the ADD or MODIFY clause, the check constraint cannot depend upon values in other columns of the same table. The next example adds a new unit_price column to the items table and includes a check constraint to ensure that the entered value is greater than 0:
ALTER TABLE items ADD (unit_price MONEY (6,2) CHECK (unit_price > 0) )
To create a constraint that checks values in more than one column, use the ADD CONSTRAINT clause. The following example builds a constraint on the column that was added in the previous example. The check constraint now spans two columns in the table.
ALTER TABLE items ADD CONSTRAINT CHECK (unit_price < total_price)
DROP Clause
Use the DROP clause to drop one or more columns from a table. DROP Clause:
, DROP ( column column )
Element column
Restrictions Must exist in the table. No fragment expression can reference column, and it cannot be the last column in the table.
You cannot issue an ALTER TABLE DROP statement that would drop every column from the table. At least one column must remain in the table. You cannot drop a column that is part of a fragmentation strategy. In Extended Parallel Server, you cannot use the DROP clause if the table has a dependent GK index.
2-52
ALTER TABLE
After the ALTER TABLE statement, tab2 has only one column. The col1trig trigger is invalidated because the action clause as it is currently defined with values for two columns cannot occur. If you drop a column that occurs in the triggering column list of an UPDATE trigger, the database server drops the column from the triggering column list. If the column is the only member of the triggering column list, the database server drops the trigger from the table. For more information on triggering columns in an UPDATE trigger, see CREATE TRIGGER on page 2-216. If a trigger is invalidated when you alter the underlying table, drop and then re-create the trigger.
2-53
ALTER TABLE
Modify Column Clause:
(1) column Data Type (2) DEFAULT Clause
Notes: 1 2 3
Element column Description Column to modify
In Extended Parallel Server, you cannot use the MODIFY clause if the table has a dependent GK index. You cannot change the data type of a column to a COLLECTION or a ROW type. When you modify a column, all attributes previously associated with the column (that is, default value, single-column check constraint, or referential constraint) are dropped. When you want certain attributes of the column to remain, such as PRIMARY KEY, you must re-specify those attributes. For example, if you are changing the data type of an existing column, quantity, to SMALLINT, but you want to keep the default value (in this case, 1) and the NOT NULL column attribute, you can issue this statement:
ALTER TABLE items MODIFY (quantity SMALLINT DEFAULT 1 NOT NULL)
Tip: Both attributes are specified again in the modify clause. When you change the data type of a column, the database server does not perform the modification in place. The next example (for Dynamic Server only) changes a VARCHAR(15) column to an LVARCHAR(3072) column:
ALTER TABLE stock MODIFY (description LVARCHAR(3072))
When you modify a column that has column constraints associated with it, the following constraints are dropped: v All single-column constraints are dropped. v All referential constraints that reference the column are dropped. v If the modified column is part of a multiple-column primary-key or unique constraint, all referential constraints that reference the multiple columns also are dropped. For example, if you modify a column that has a unique constraint, the unique constraint is dropped. If this column was referenced by columns in other tables,
2-54
ALTER TABLE
those referential constraints are also dropped. In addition, if the column is part of a multiple-column primary-key or unique constraint, the multiple-column constraints are not dropped, but any referential constraints placed on the column by other tables are dropped. For another example, suppose that a column is part of a multiple-column primary-key constraint. This primary key is referenced by foreign keys in two other tables. When this column is modified, the multiple-column primary-key constraint is not dropped, but the referential constraints placed on it by the two other tables are dropped.
As an alternative, you can use the INSERT statement to create a gap in the series of serial values in the column. For more information, see Inserting Values into Serial Columns on page 2-400. Altering the Next Serial Value in a Typed Table (IDS): You can set the initial serial number or modify the next serial number for a ROW-type field with the MODIFY clause of the ALTER TABLE statement. (You cannot set the initial number for a serial field when you create a ROW data type.) Suppose you have ROW types parent, child1, child2, and child3.
2-55
ALTER TABLE
CREATE CREATE CREATE CREATE ROW ROW ROW ROW TYPE TYPE TYPE TYPE parent child1 child2 child3 (a (s (b (d int); serial) UNDER parent; float, s8 serial8) UNDER child1; int) UNDER child2;
To change the next SERIAL and SERIAL8 numbers to 75, you can issue the following statement:
ALTER TABLE child3 MODIFY (s serial(75), s8 serial8(75))
When the ALTER TABLE statement executes, the database server updates corresponding serial columns in the child1, child2, and child3 tables.
When a primary-key or unique constraint exists, however, conversion takes place only if it does not violate the constraint. If a data type conversion would result in duplicate values (by changing FLOAT to SMALLFLOAT, for example, or by truncating CHAR values), the ALTER TABLE statement fails.
2-56
ALTER TABLE
Issue the ALTER TABLE statement again, but this time specify the DISABLED keyword in the MODIFY clause. Start a violations and diagnostics table for the target table with the START VIOLATIONS TABLE statement. Issue the SET CONSTRAINTS statement to switch the database object mode of the constraint to the enabled mode. When you issue this statement, existing rows in the target table that violate the constraint are duplicated in the violations table; however, you receive an integrity-violation error message, and the constraint remains disabled. Issue a SELECT statement on the violations table to retrieve the nonconforming rows that are duplicated from the target table. You might need to join the violations and diagnostics tables to get all the necessary information. Take corrective action on the rows in the target table that violate the constraint. After you fix all the nonconforming rows in the target table, issue the SET statement again to enable the constraint that was disabled. Now the constraint is enabled, and no integrity-violation error message is returned because all rows in the target table now satisfy the new constraint.
2. 3.
4.
5. 6.
After the ALTER TABLE statement, column i4 accepts only character values. Because character columns accept only values enclosed in quotation marks, the action clause of the col1trig trigger is invalidated. If a trigger is invalidated when you modify the underlying table, drop and then re-create the trigger.
2-57
ALTER TABLE
, ( EXTENT SIZE kilobytes NO LOG LOG HIGH INTEG NO KEEP ACCESS TIME KEEP ACCESS TIME )
Description Column to store in the specified sbspace Number of kilobytes to allocate for the extent size
Restrictions Must be a UDT, or a complex, BLOB, or CLOB data type Must be an integer value
Name of an area of storage for The sbspace must exist smart large objects
When you modify the storage characteristics of a column, all attributes previously associated with the storage space for that column are dropped. When you want certain attributes to remain, you must specify those attributes again. For example, to retain logging, you must specify the log keyword again. The format column.field is not valid here. That is, the smart large object that you are storing cannot be one field of a row type. When you modify the storage characteristics of a column that holds smart large objects, the database server does not alter smart large objects that already exist, but applies the new storage characteristics only to those smart large objects that are inserted after the ALTER TABLE statement takes effect. For more information on the available storage characteristics, refer to the counterpart of this section in the CREATE TABLE statement, PUT Clause (IDS) on page 2-199. For a discussion of large-object characteristics, refer to Large-Object Data Types on page 4-25.
2-58
ALTER TABLE
For example, to add a unique constraint to the fname and lname columns of the customer table, use the following statement:
ALTER TABLE customer ADD CONSTRAINT UNIQUE (lname, fname)
When you do not specify a name for a new constraint, the database server provides one. You can find the name of the constraint in the sysconstraints system catalog table. For more information about the sysconstraints system catalog table, see the IBM Informix Guide to SQL: Reference. When you add a constraint, the collating order must be the same as when the table was created.
(5)
2-59
ALTER TABLE
Notes: 1 2 3 4 5
Element column Description A column on which the constraint is placed
Informix extension Use path no more than 16 times See page 2-51 See page 2-49 See page 2-49
Restrictions No more than 16 columns Syntax Identifier, p. 5-22
A multiple-column constraint has these restrictions: v It can include no more than 16 column names. v In Dynamic Server, the total length of the list of columns cannot exceed 390 bytes. v In Extended Parallel Server, the total length of the list of columns cannot exceed 255 bytes. You can declare a name for the constraint and set its mode by means of Constraint Definition on page 2-49.
2-60
ALTER TABLE
Element constraint
To drop an existing constraint, specify the DROP CONSTRAINT keywords and the name of the constraint. Here is an example of dropping a constraint:
ALTER TABLE manufact DROP CONSTRAINT con_name
If no name is specified when the constraint is created, the database server generates the name. You can query the sysconstraints system catalog table for the name and owner of a constraint. For example, to find the name of the constraint placed on the items table, you can issue the following statement:
SELECT constrname FROM sysconstraints WHERE tabid = (SELECT tabid FROM systables WHERE tabname = items)
When you drop a primary-key or unique constraint that has a corresponding foreign key, the referential constraints are dropped. For example, if you drop the primary-key constraint on the order_num column in the orders table and order_num exists in the items table as a foreign key, that referential relationship is also dropped. You cannot use the MODIFY NEXT SIZE clause of the ALTER TABLE statement to change the size of the next extent of any system catalog table, even if you are user informix.
Element kilobytes
Description Length (in kilobytes) assigned here to the next extent for this table
Restrictions Specification cannot be a variable, and (4(page size)) kilobytes (chunk size)
The minimum extent size is 4 times the disk-page size. For example, on a system with 2-kilobyte pages, the minimum length is 8 kilobytes. The maximum length is equal to the chunk size. This example specifies an extent size of 32 kilobytes:
ALTER TABLE customer MODIFY NEXT SIZE 32
2-61
ALTER TABLE
This clause cannot change the size of existing extents. You cannot change the size of existing extents without unloading all of the data. To change the size of existing extents, you must unload all the data, drop the table, modify the first-extent and next-extent sizes in the CREATE TABLE definition in the database schema, re-create the database, and reload the data. For information about how to optimize extent sizes, see your IBM Informix Administrator's Guide.
The following table describes the locking-granularity options available. Granularity PAGE Effect Obtains and releases one lock on a whole page of rows This is the default locking granularity. Page-level locking is especially useful when you know that the rows are grouped into pages in the same order that you are using to process all the rows. For example, if you are processing the contents of a table in the same order as its cluster index, page locking is especially appropriate. ROW Obtains and releases one lock per row Row-level locking provides the highest level of concurrency. If you are using many rows at one time, the lock-management overhead can become significant. You can also exceed the maximum number of locks available, depending on the configuration of your database server. TABLE (XPS only) Places a lock on the entire table This type of lock reduces update concurrency in comparison to row and page locks. A table lock reduces the lock-management overhead for a table. Multiple read-only transactions can still access the table.
2-62
ALTER TABLE
Guide to SQL: Reference. For information about the DEF_TABLE_LOCKMODE configuration parameter, refer to the IBM Informix Dynamic Server Administrator's Reference.
Here STANDARD, the default option of the CREATE TABLE statement, is used for online transaction processing (OLTP) databases. The OPERATIONAL and STATIC options are used primarily to improve performance in data warehousing databases. A table can have any of the following logging characteristics. Option STANDARD Effect Logging table that allows rollback, recovery, and restoration from archives. This is the default. Use this type for recovery and constraints functionality on OLTP databases. Nonlogging table that cannot have indexes or referential constraints but can be updated. Use this type for quickly loading data. In XPS, raw tables take advantage of light appends and avoid the overhead of logging, checking constraints, and building indexes. Logging table that uses light appends and cannot be restored from archive. Use this type on tables that are refreshed frequently. Light appends allow the quick addition of many rows. Nonlogging table that can contain index and referential constraints but cannot be updated. Use this type for read-only operations because there is no logging or locking overhead.
RAW
Warning: Use raw tables for fast loading of data. It is recommended that you alter the logging type to STANDARD and perform a level-0 backup before you use the table in a transaction or modify the data within the table. If you must use a raw table within a transaction, either set the isolation level to Repeatable Read or lock the table in exclusive mode to prevent concurrency problems.
2-63
ALTER TABLE
The Logging TYPE option can convert a non-logging table, such as a RAW table, to a STANDARD table that supports transaction logging. If you use this feature, you should be aware that the database server does not check to see whether a level 0 archive has been performed on the table. Operations on a RAW table are not logged and are not recoverable, so RAW tables are always at risk. When the database server converts a table that is not logged to a STANDARD table type, it is your responsibility to perform a level-0 backup before using the table in a transaction or modifying data in the table. Failure to do this might lead to recovery problems in the event of a server crash. For more information on these logging types of tables, refer to your IBM Informix Administrator's Guide. The Logging TYPE options have the following restrictions: v You must perform a level-0 archive before the logging type of a table can be altered to STANDARD from any other logging type. v If you want to change the logging type of a table to RAW, you must drop all indexes on the table before you do so. v If you have triggers defined on the table, you cannot change the logging type to RAW or STATIC. These nonlogging table types do not support triggers. v The table cannot be a SCRATCH or TEMP table, and you cannot change any of these types of tables to a SCRATCH or TEMP table. v The table cannot have a dependent generalized-key (GK) index.
Element row_type
Description Identifier of an existing named ROW data type for the table
Restrictions
Syntax
The row_type fields must match the column Identifier, p. 5-22 data type in their order and number
When you use the ADD TYPE clause, you assign the specified named ROW data type to a table whose columns match the fields of that data type. In addition to the requirements common to all ALTER TABLE operations (namely DBA privilege on the database, Alter privilege on the table, and ownership of the table), all of the following must be also true when you use the ADD TYPE clause to convert an untyped table to the specified named ROW data type: v The named ROW data type is already registered in the database. v You hold the Usage privilege on the named ROW data type. v There must be a 1-to-1 correspondence between the ordered set of column data types of the untyped table and the ordered set of field data types of the named ROW data type. v The table cannot be a fragmented table that has rowid values.
2-64
ALTER TABLE
You cannot combine the ADD TYPE clause with any clause that changes the schema of the table. No other ADD, DROP, or MODIFY clause is valid in the same ALTER TABLE statement that has the ADD TYPE clause. The ADD TYPE clause does not allow you to change column data types. (To change the data type of a column, use the MODIFY clause.)
(1) ADD CONSTRAINT Clause (2) DROP CONSTRAINT Clause (3) MODIFY NEXT SIZE Clause (3) (5) LOCK MODE Clause
(4)
Notes: 1 2 3 4 5 See page 2-59 See page 2-61 Use path no more than once See page 2-61 See page 2-62
Related Infrmation
Related statements: CREATE TABLE, DROP TABLE, LOCK TABLE, and SET Database Object Mode. For discussions of data-integrity constraints and the ON DELETE CASCADE option, see the IBM Informix Guide to SQL: Tutorial. For a discussion of database and table creation, see the IBM Informix Database Design and Implementation Guide. For information on how to maximize performance when you make table modifications, see your IBM Informix Performance Guide.
2-65
BEGIN WORK
Use the BEGIN WORK statement to start a transaction (a series of database operations that the COMMIT WORK or ROLLBACK WORK statement terminates). This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
WORK BEGIN (1) WITHOUT REPLICATION
Usage
The BEGIN WORK statement is valid only in a database that supports transaction logging. Each row that an UPDATE, DELETE, or INSERT statement affects during a transaction is locked and remains locked throughout the transaction. A transaction that contains many such statements or that contains statements that affect many rows can exceed the limits that your operating system or the database server configuration imposes on the number of simultaneous locks. If no other user is accessing the table, you can avoid locking limits and reduce locking overhead by locking the table with the LOCK TABLE statement after you begin the transaction. Like other locks, this table lock is released when the transaction terminates. The example of a transaction on Example of BEGIN WORK on page 2-67 includes a LOCK TABLE statement. Important: Issue the BEGIN WORK statement only if a transaction is not in progress. If you issue a BEGIN WORK statement while you are in a transaction, the database server returns an error. The WORK keyword is optional. The following two statements are equivalent:
BEGIN; BEGIN WORK;
In ESQL/C, if you use the BEGIN WORK statement within a UDR called by a WHENEVER statement, specify WHENEVER SQLERROR CONTINUE and WHENEVER SQLWARNING CONTINUE before the ROLLBACK WORK statement. These statements prevent the program from looping endlessly if the ROLLBACK WORK statement encounters an error or a warning.
2-66
BEGIN WORK
v CREATE DATABASE v ROLLBACK WORK The database server returns an error when you use a BEGIN WORK statement after any other statement in an ANSI-compliant database.
The database server must perform this sequence of operations either completely or not at all. When you include all of these operations within a single transaction, the database server guarantees that all the statements are completely and perfectly committed to disk, or else the database is restored to the same state that it was in before the transaction began.
Related Information
Related statements: COMMIT WORK and SAVE EXTERNAL DIRECTIVES For discussions of transactions and locking, see the IBM Informix Guide to SQL: Tutorial.
2-67
CLOSE
Use the CLOSE statement when you no longer need to refer to the rows that a select or function cursor retrieved, or to flush and close an insert cursor. Use this statement with ESQL/C.
Syntax
CLOSE cursor_id (1) cursor_id_var
Notes: 1
Element cursor_id cursor_id_var Description Name of cursor to be closed Host variable that contains the value of cursor_id
Informix extension
Restrictions Must have been declared Must be of a character data type Syntax Identifier on page 5-22 Must conform to language-specific rules for names.
Usage
Closing a cursor makes the cursor unusable for any statements except OPEN or FREE and releases resources that the database server had allocated to the cursor. A CLOSE statement treats a cursor that is associated with an INSERT statement differently from one that is associated with a SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement. In a database that is not ANSI-compliant, you can close a cursor that has not been opened or that has already been closed. No action is taken in these cases. In an ANSI-compliant database, the database server returns an error if you close a cursor that was not open.
2-68
CLOSE
rows that were successfully inserted into the database is returned in the third element of the sqlerrd array, sqlca.sqlerrd[2], in the sqlca structure. For information on how to use SQLERRD to count the total number of rows that were inserted, see Error Checking on page 2-447. The SQLCODE field of the sqlca structure, sqlca.sqlcode, indicates the result of the CLOSE statement for an insert cursor. If all buffered rows are successfully inserted, SQLCODE is set to zero. If an error is encountered, the sqlca.sqlcode field in the SQLCODE is set to a negative error message number. When SQLCODE is zero, the row buffer space is released, and the cursor is closed; that is, you cannot execute a PUT or FLUSH statement that names the cursor until you reopen it. Tip: When you encounter an SQLCODE error, a corresponding SQLSTATE error value also exists. For information about how to get the message text, check the GET DIAGNOSTICS statement. If the insert is not successful, the number of successfully inserted rows is stored in sqlerrd. Any buffered rows that follow the last successfully inserted row are discarded. Because the insert fails, the CLOSE statement fails also, and the cursor is not closed. For example, a CLOSE statement can fail if insufficient disk space prevents some of the rows from being inserted. In this case, a second CLOSE statement can be successful because no buffered rows exist. An OPEN statement can also be successful because the OPEN statement performs an implicit close.
2-69
CLOSE
Related Information
Related statements: DECLARE, FETCH, FLUSH, FREE, OPEN, PUT, and SET AUTOFREE For an introductory discussion of cursors, see the IBM Informix Guide to SQL: Tutorial. For a more advanced discussion of cursors, see the IBM Informix ESQL/C Programmer's Manual.
2-70
CLOSE DATABASE
Use the CLOSE DATABASE statement to close the current database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
CLOSE DATABASE
Usage
When you issue a CLOSE DATABASE statement, you can issue only the following SQL statements immediately after it: v v v v v CONNECT CREATE DATABASE DATABASE DROP DATABASE DISCONNECT (The DISCONNECT statement is valid here only if an explicit connection existed before CLOSE DATABASE was executed.)
Issue the CLOSE DATABASE statement before you drop the current database. If your current database supports transaction logging, and if you have started a transaction, you must issue a COMMIT WORK or ROLLBACK WORK statement before you can use the CLOSE DATABASE statement. The following example shows how to use the CLOSE DATABASE statement to drop the current database:
DATABASE stores_demo . . . CLOSE DATABASE DROP DATABASE stores_demo
In ESQL/C, the CLOSE DATABASE statement cannot appear in a multistatement PREPARE operation. If a previous CONNECT statement has established an explicit connection to a database, and that connection is still your current connection, you cannot use the CLOSE DATABASE statement to close that explicit connection. (You can use the DISCONNECT statement to close the explicit connection.) If you use the CLOSE DATABASE statement within a UDR called by a WHENEVER statement, specify WHENEVER SQLERROR CONTINUE and WHENEVER SQLWARNING CONTINUE before the ROLLBACK WORK statement. This action prevents the program from looping if the ROLLBACK WORK statement encounters an error or a warning. When you issue the CLOSE DATABASE statement, any declared cursors are no longer valid. You must re-declare any cursors that you want to use.
2-71
CLOSE DATABASE
In an ANSI-compliant database, if no error is encountered while you exit from DBAccess in non-interactive mode without issuing the CLOSE DATABASE, COMMIT WORK, or DISCONNECT statement, the database server automatically commits any open transaction.
Related Information
Related statements: CONNECT, CREATE DATABASE, DATABASE, DISCONNECT, and DROP DATABASE
2-72
COMMIT WORK
Use the COMMIT WORK statement to commit all modifications made to the database from the beginning of a transaction.
Syntax
COMMIT WORK
Usage
The COMMIT WORK statement informs the database server that you reached the end of a series of statements that must succeed as a single unit. The database server takes the required steps to make sure that all modifications that the transaction makes are completed correctly and saved to disk. Use COMMIT WORK only at the end of a multistatement operation in a database with transaction logging, when you are sure that you want to keep all changes made to the database from the beginning of a transaction. The COMMIT WORK statement releases all row and table locks. The WORK keyword is optional in a COMMIT WORK statement. The following two statements are equivalent:
COMMIT; COMMIT WORK;
The following example shows a transaction bounded by BEGIN WORK and COMMIT WORK statements.
BEGIN WORK; DELETE FROM call_type WHERE call_code = O; INSERT INTO call_type VALUES (S, order status); COMMIT WORK;
In this example, the user first deletes the row from the call_type table where the value of the call_code column is O. The user then inserts a new row in the call_type table where the value of the call_code column is S. The database server guarantees that both operations succeed or else neither succeeds. In ESQL/C, the COMMIT WORK statement closes all open cursors except those that were declared using the WITH HOLD option.
2-73
COMMIT WORK
Explicit DB-Access Transactions: When you use DB-Access in interactive mode with a database that is not ANSI-compliant but that supports transaction logging, if you select the Commit menu but do not issue the COMMIT WORK statement after a transaction has been started by the BEGIN WORK statement, DB-Access automatically commits the data, but issues the following warning:
Warning: Data commit is a result of unhandled exception in TXN PROC/FUNC
The purpose of this warning is to remind you to issue COMMIT WORK explicitly to end a transaction that BEGIN WORK initiated. In non-interactive mode, however, DB-Access rolls back the current transaction if you end a session without issuing the COMMIT WORK statement.
Related Information
Related statements: BEGIN WORK, SAVE EXTERNAL DIRECTIVES, and DECLARE For a discussion of concepts related to transactions, see the IBM Informix Guide to SQL: Tutorial.
2-74
CONNECT
Use the CONNECT statement to connect to a database environment. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
CONNECT TO
(1) Database Environment (2) AS DEFAULT connection connection_var (3) USER Clause (4)
Notes: 1 2 3 4 See Database Environment on page 2-78 ESQL/C only ESQL/C and DB-Access only See USER Clause on page 2-80
Description Restrictions Syntax Quoted String on page 4-142 Language specific
Case-sensitive name that you declare Must be unique among here for a connection connection names Host variable that stores the name of Must be a fixed-length connection character data type
Usage
The CONNECT statement connects an application to a database environment, which can be a database, a database server, or a database and a database server. If the application successfully connects to the specified database environment, the connection becomes the current connection for the application. SQL statements fail if the application has no current connection to a database server. If you specify a database name, the database server opens that database. You cannot include CONNECT within a PREPARE statement. An application can connect to several database environments at the same time, and it can establish multiple connections to the same database environment, provided each connection has a unique connection name. On UNIX, the only restriction on establishing multiple connections to the same database environment is that an application can establish only one connection to each local server that uses the shared-memory connection mechanism. To find out whether a local server uses the shared-memory connection mechanism or the local-loopback connection mechanism, examine the $INFORMIXDIR/etc/sqlhosts file. For more information on the sqlhosts file, refer to your IBM Informix Administrator's Guide.
Chapter 2. SQL Statements
2-75
CONNECT
On Windows, the local connection mechanism is named pipes. Multiple connections to the local server from one client can exist. Only one connection is current at any time; other connections are dormant. The application cannot interact with a database through a dormant connection. When an application establishes a new connection, that connection becomes current, and the previous current connection becomes dormant. You can make a dormant connection current with the SET CONNECTION statement. See also SET CONNECTION on page 2-534.
Connection Identifiers
The optional connection name is a unique identifier that an application can use to refer to a connection in subsequent SET CONNECTION and DISCONNECT statements. If the application does not provide a connection name (or a connection-host variable), it can refer to the connection using the database environment. If the application makes more than one connection to the same database environment, however, each connection must have a unique name. After you associate a connection name with a connection, you can refer to the connection using only that connection name.
Connection Context
Each connection encompasses a set of information that is called the connection context. The connection context includes the name of the current user, the information that the database environment associates with this name, and information on the state of the connection (such as whether an active transaction is associated with the connection). The connection context is saved when an application becomes dormant, and this context is restored when the application becomes current again. (For more information, see Making a Dormant Connection the Current Connection on page 2-534.)
DEFAULT Option
Use the DEFAULT option to request a connection to a default database server, called a default connection. The default database server can be local or remote. To designate the default database server, set its name in the INFORMIXSERVER environment variable. This option of CONNECT does not open a database. If you select the DEFAULT option for the CONNECT statement, you must use the DATABASE statement or the CREATE DATABASE statement to open or create a database in the default database environment.
2-76
CONNECT
v DROP DATABASE If one of these database statements is the first SQL statement in an application, the statement establishes a connection to a database server, which is known as an implicit connection. If the database statement specifies only a database name, the database server name is obtained from the DBPATH environment variable. This situation is described in Specifying the Database Environment on page 2-79. An application that makes an implicit connection can establish other connections explicitly (using the CONNECT statement) but cannot establish another implicit connection unless the original implicit connection is closed. An application can terminate an implicit connection using the DISCONNECT statement. After you create an explicit connection, you cannot use any database statement to create implicit connections until after you close the explicit connection. After any implicit connection is made, that connection is considered to be the default connection, regardless of whether the database server is the default that the INFORMIXSERVER environment variable specifies. This feature allows the application to refer to the implicit connection if additional explicit connections are made, because the implicit connection has no identifier. For example, if you establish an implicit connection followed by an explicit connection, you can make the implicit connection current by issuing the SET CONNECTION DEFAULT statement. This means, however, that once you establish an implicit connection, you cannot use the CONNECT DEFAULT statement, because the implicit connection is now the default connection. The database statements can always be used to open a database or create a new database on the current database server.
2-77
CONNECT
/* Execute SQL statements in connection C , starting a transaction */ EXEC SQL set connection B; -- switch to connection B /* Execute SQL statements starting a transaction in B. Now there are two active transactions, one each in B and C. */ EXEC SQL set connection A; -- switch to connection A /* Execute SQL statements starting a transaction in A. Now there are three active transactions, one each in A, B and C. */ EXEC SQL set connection C; -- ERROR, transaction active in A /* SET CONNECTION C fails (current connection is still A) The transaction in A must be committed or rolled back because connection A was started without the CONCURRENT TRANSACTION clause. */
Now, there are two active transactions, in B and in C, which must be committed or rolled back separately */ EXEC SQL set connection B; -- switch to connection B EXEC SQL commit work; -- commit tx in current connection (B) EXEC SQL set connection C; -- go back to connection C EXEC SQL commit work; -- commit tx in current connection (C) EXEC SQL disconnect all; }
Warning: When an application uses the WITH CONCURRENT TRANSACTION clause to establish multiple connections to the same database environment, a deadlock condition can occur.
Database Environment
Database Environment:
dbname @dbservername dbname@dbservername (1) db_var
2-78
CONNECT
Element db_var Description Restrictions Syntax Language specific
Host variable that contains a Must be a fixed-length character data type, valid database environment (in whose contents are in a format from the one of the formats in the syntax diagram syntax diagram) Database to which to connect Must already exist
dbname dbservername
Name of the database server to Must already exist; blank space is not valid which a connection is made between @ symbol and dbservername. See also Restrictions on dbservername.
If the DELIMIDENT environment variable is set, any quotation ( ) marks in the database environment must be single. If DELIMIDENT is not set, then either single ( ) or double ( ) quotation marks are valid here.
Restrictions on dbservername
If you specify dbservername, it must satisfy the following restrictions. v If the database server that you specify is not online, you receive an error. v On UNIX, the database server that you specify in dbservername must match the name of a database server in the sqlhosts file. v On Windows, dbservername must match the name of a database server in the sqlhosts subkey in the registry. It is recommended that you use the setnet32 utility to update the registry.
2-79
CONNECT
On Windows, choose Start > Programs > Informix > setnet32 from the Task Bar and set the INFORMIXSERVER and DBPATH environment variables:
set INFORMIXSERVER = srvA set DBPATH = //srvA://srvB://srvC
The next example shows the resulting DBPATH that your application uses:
//srvA://srvB://srvC
The application first establishes a connection to the database server that INFORMIXSERVER specifies. The database server uses parameters in the configuration file to locate the database. If the database does not reside on the default database server, or if the default database server is not online, the application connects to the next database server in DBPATH. In the previous example, that database server would be srvB.
USER Clause
The USER clause specifies information that is used to determine whether the application can access the target computer on a remote host. USER Clause:
USER user_id user_id_var USING validation_var
Restrictions See Restrictions on the User Identifier Parameter on page 2-81. Must be a fixed-length character data type; same restrictions as user_id
Host variable that contains a valid Must be a fixed-length character type. See Language specific password for login name in user_id Restrictions on the Validation Variable or user_id_var Parameter on page 2-80.
The USER clause is required when the CONNECT statement connects to the database server on a remote host. Subsequent to the CONNECT statement, all database operations on the remote host use the specified user name. In DB-Access, the USING clause is valid within files executed from DB-Access. In interactive mode, DB-Access prompts you for a password, so the USING keyword and validation_var are not used.
2-80
CONNECT
Related Information
Related Statements: DISCONNECT, SET CONNECTION, DATABASE, and CREATE DATABASE For more information about sqlhosts, refer to your IBM Informix Administrator's Guide.
2-81
CREATE ACCESS_METHOD
Use the CREATE ACCESS_METHOD statement to register a new primary or secondary access method in the sysams system catalog table. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE SECONDARY PRIMARY ACCESS_METHOD access_method
Notes: 1
Element access method Description Name declared here for the new access method
Usage
The CREATE ACCESS_METHOD statement adds a user-defined access method to a database. To create an access method, you specify purpose functions (or purpose methods), purpose flags, or purpose values as attributes of the access method, and you associate keywords (based on column names in the sysams system catalog table) with UDRs. You must have the DBA or Resource privilege to create an access method. For information on setting purpose options, including a list of all the purpose function keywords, refer to Purpose Options on page 5-46. The PRIMARY keyword specifies a user-defined primary-access method for a virtual table. The SECONDARY keyword specifies creating a user-defined secondary-access method for a virtual index. The SECONDARY keyword (and creating virtual indexes) is not supported in the Java Virtual-Table Interface. The following statement creates a secondary-access method named T_tree:
CREATE SECONDARY ACCESS_METHOD T_tree ( am_getnext = ttree_getnext, . . . am_unique, am_cluster, am_sptype = S );
2-82
CREATE ACCESS_METHOD
In the preceding example, the am_getnext keyword in the Purpose Options list is associated with the ttree_getnext( ) UDR as the name of a method to scan for the next item that satisfies a query. This example indicates that the T_tree secondary access method supports unique keys and clustering, and resides in an sbspace. Any UDR that the CREATE ACCESS_METHOD statement associates with the keyword for a purpose function task, such as the association of ttree_getnext( ) with am_getnext in the preceding example, must already have been registered in the database by the CREATE FUNCTION statement (or by a functionally equivalent statement, such as CREATE PROCEDURE FROM). The following statement creates a primary-access method named am_tabprops that resides in an extspace.
CREATE PRIMARY ACCESS_METHOD am_tabprops ( am_open = FS_open, am_close = FS_close, am_beginscan = FS_beginScan, am_create = FS_create, am_scancost = FS_scanCost, am_endscan = FS_endScan, am_getnext = FS_getNext, am_getbyid = FS_getById, am_drop = FS_drop, am_truncate = FS_truncate, am_rowids, am_sptype = x );
Related Information
Related statements: ALTER ACCESS_METHOD and DROP ACCESS_METHOD For the schema of the sysams table, see the IBM Informix Guide to SQL: Reference. For information about how to set purpose-option specifications, see Purpose Options on page 5-46. For more information on primary-access methods, see the IBM Informix Virtual-Table Interface Programmer's Guide. For more information on secondary-access methods, see the IBM Informix Virtual-Index Interface Programmer's Guide and the IBM Informix User-Defined Routines and Data Types Developer's Guide. For a discussion of privileges, see the GRANT or REVOKE statements or the IBM Informix Database Design and Implementation Guide.
2-83
CREATE AGGREGATE
Use the CREATE AGGREGATE statement to create a new aggregate function and register it in the sysaggregates system catalog table. User-defined aggregates extend the functionality of the database server by performing aggregate computations that the user implements. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE AGGREGATE (1) Owner Name . aggregate
, WITH ( Modifiers )
Modifiers:
INIT=init_func ITER=iter_func COMBINE=comb_func FINAL=final_func HANDLESNULLS
Notes: 1
Element aggregate comb_func Description Name of the new aggregate
Function that merges one partial Must specify the combined function result into the other and returns the both for parallel queries and for updated partial result sequential queries Function that converts a partial result into the result type Function that initializes the data structures required for the aggregate computation If this is omitted, then the returned value is the final result of iter_func Must be able to handle NULL arguments
final_func init_func
iter_func
Function that merges a single value Must specify an iterator function. If with a partial result and returns init_func is omitted, iter_func must be updated partial result able to handle NULL arguments
Usage
You can specify the INIT, ITER, COMBINE, FINAL, and HANDLESNULLS modifiers in any order.
2-84
CREATE AGGREGATE
Important: You must specify the ITER and COMBINE modifiers in a CREATE AGGREGATE statement. You do not have to specify the INIT, FINAL, and HANDLESNULLS modifiers in a CREATE AGGREGATE statement. The ITER, COMBINE, FINAL, and INIT modifiers specify the support functions for a user-defined aggregate. These support functions do not have to exist at the time you create the user-defined aggregate. If you omit the HANDLESNULLS modifier, rows with NULL aggregate argument values do not contribute to the aggregate computation. If you include the HANDLESNULLS modifier, you must define all the support functions to handle NULL values as well.
Before you use the average aggregate in a query, you must also use CREATE FUNCTION statements to create the support functions specified in the CREATE AGGREGATE statement. The following table gives an example of the task that each support function might perform for average.
2-85
CREATE AGGREGATE
Support Function average_init average_iter
Effect Allocates and initializes an extended data type storing the current sum and the current row count For each row, adds the value of the expression to the current sum and increments the current row count by one Adds the current sum and the current row count of one partial result to the other and returns the updated result Returns the ratio of the current sum to the current row count and converts this ratio to the result type
COMBINE
average_combine
FINAL
average_final
Parallel Execution
The database server can break up an aggregate computation into several pieces and compute them in parallel. The database server uses the INIT and ITER support functions to compute each piece sequentially. Then the database server uses the COMBINE function to combine the partial results from all the pieces into a single result value. Whether an aggregate is parallel is an optimization decision that is transparent to the user.
Related Information
Related statements: CREATE FUNCTION and DROP AGGREGATE For information about how to invoke a user-defined aggregate, see User-Defined Aggregates on page 4-117 in the Expression segment. For a description of the sysaggregates system catalog table that stores data about user-defined aggregates, see the IBM Informix Guide to SQL: Reference. For a discussion of user-defined aggregates, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
2-86
CREATE CAST
Use the CREATE CAST statement to register a cast that converts data from one data type to another. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE EXPLICIT IMPLICIT CAST ) WITH function
( source_type AS target_type
Element function
Description UDR that you register to implement the cast Data type to be converted
Syntax Database Object Name on page 5-17 Data Type on page 4-18
source_type
Must exist in the database at the time the cast is registered. See also Source and Target Data Types on page 2-87.
target_type
The same restrictions that apply for the source_type (as Data Type on listed above) also apply for the target_type page 4-18
Usage
A cast is a mechanism that the database server uses to convert one data type to another. The database server uses casts to perform the following tasks: v To compare two values in the WHERE clause of a SELECT, UPDATE, or DELETE statement v To pass values as arguments to user-defined routines v To return values from user-defined routines To create a cast, you must have the necessary privileges on both the source data type and the target data type. All users have access privileges to use the built-in data types. To create a cast to or from an OPAQUE, DISTINCT, or named ROW data type, however, requires the Usage privilege on that data type. The CREATE CAST statement registers a cast in the syscasts system catalog table. For more information on syscasts, see the chapter on system catalog tables in the IBM Informix Guide to SQL: Reference.
2-87
CREATE CAST
The following SELECT statement explicitly invokes this explicit cast in its WHERE clause to compare the bond_rate column (of type rate_of_return) to the initial_APR column (of type percent):
SELECT bond_rate FROM bond WHERE bond_rate::percent > initial_APR
Implicit Casts: The database server invokes built-in casts to convert from one built-in data type to another built-in type that is not directly substitutable. For example, the database server performs conversion of a character type such as CHAR to a numeric type such as INTEGER through a built-in cast. An implicit cast is a cast that the database server can invoke automatically when it encounters data types that cannot be compared with built-in casts. This type of cast enables the database server to automatically handle conversions between other data types. To define an implicit cast, specify the IMPLICIT keyword in the CREATE CAST statement. For example, the following CREATE CAST statement specifies that the database server should automatically use the prcnt_to_char( ) function to convert from the CHAR data type to a distinct data type, percent:
CREATE IMPLICIT CAST (CHAR AS percent WITH char_to_prcnt)
This cast only supports automatic conversion from the CHAR data type to percent. For the database server to convert from percent to CHAR, you also need to define another implicit cast, as follows:
CREATE IMPLICIT CAST (percent AS CHAR WITH prcnt_to_char)
The database server automatically invokes the char_to_prcnt( ) function to evaluate the WHERE clause of the following SELECT statement:
SELECT commission FROM sales_rep WHERE commission > "25%"
Users can also invoke implicit casts explicitly. For more information on how to explicitly invoke a cast function, see Explicit Casts on page 2-88. When a built-in cast does not exist for conversion between data types, you can create user-defined casts to make the necessary conversion.
2-88
CREATE CAST
WITH Clause
The WITH clause of the CREATE CAST statement specifies the name of the user-defined function to invoke to perform the cast. This function is called the cast function. You must specify a function name unless the source data type and the target data type have identical representations. Two data types have identical representations when the following conditions are met: v Both data types have the same length and alignment. v Both data types are passed by reference or both are passed by value. The cast function must be registered in the same database as the cast at the time the cast is invoked, but need not exist when the cast is created. The CREATE CAST statement does not check privileges on the specified function name, or even verify that the cast function exists. Each time a user invokes the cast explicitly or implicitly, the database server verifies that the user has the Execute privilege on the cast function.
Related Information
Related statements: CREATE FUNCTION, CREATE DISTINCT TYPE, CREATE OPAQUE TYPE, CREATE ROW TYPE, and DROP CAST For more information about data types, casting, and conversion, see the Data Types segment in this manual and the IBM Informix Guide to SQL: Reference. For examples that show how to create and use casts, see the IBM Informix Database Design and Implementation Guide.
2-89
CREATE DATABASE
Use the CREATE DATABASE statement to create a new database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE DATABASE database IN dbspace
LOG
Description Name that you declare here for the new database that you are creating The dbspace to store the data for this database; default is the root dbspace
Restrictions Must be unique among names of databases of the database server Must already exist
Usage
This statement is an extension to ANSI-standard syntax. (The ANSI/ISO standard for the SQL language does not specify any syntax for construction of a database, the process by which a database comes into existence.) The database that CREATE DATABASE specifies becomes the current database. The database name that you declare must be unique within the database server environment in which you are working. The database server creates the system catalog tables that describe the structure of the new database. When you create a database, you alone can access it. It remains inaccessible to other users until you, as DBA, grant database privileges. For information on how to grant database privileges, see GRANT on page 2-371. If a previous CONNECT statement has established an explicit connection to a database, and that connection is still your current connection, you cannot use the CREATE DATABASE statement (nor any SQL statement that creates an implicit connection) until after you use DISCONNECT to close the explicit connection. In ESQL/C, the CREATE DATABASE statement cannot appear in a multistatement PREPARE operation. If you do not specify the dbspace, the database server creates the system catalog tables in the root dbspace. The following statement creates the vehicles database in the root dbspace:
CREATE DATABASE vehicles
The following statement creates the vehicles database in the research dbspace:
CREATE DATABASE vehicles IN research
2-90
CREATE DATABASE
In Extended Parallel Server, you can create a database in the dbspace of the primary coserver (coserver 1) only.
Logging Options
The logging options of the CREATE DATABASE statement determine the type of logging that is done for the database. In the event of a failure, the database server uses the log to re-create all committed transactions in your database. If you do not specify the WITH LOG option, you cannot use transactions nor the statements that support transaction logging (BEGIN WORK, COMMIT WORK, ROLLBACK WORK, SET IMPLICIT TRANSACTION, SET LOG, and SET ISOLATION). If you are using Extended Parallel Server, the CREATE DATABASE statement always creates a database with logging. If you do not specify the WITH LOG option, the unbuffered log type is used by default.
If you use a buffered log, you marginally enhance the performance of logging at the risk of not being able to re-create the last few transactions after a failure. (See the discussion of buffered logging in the IBM Informix Database Design and Implementation Guide.)
ANSI-Compliant Databases
When you use the LOG MODE ANSI option in the CREATE DATABASE statement, the database that you create is an ANSI-compliant database. The following example creates an ANSI-compliant database:
CREATE DATABASE employees WITH LOG MODE ANSI
ANSI-compliant databases are different from databases that are not ANSI compliant in several ways, including the following features: v All statements are automatically contained in transactions. v All databases use unbuffered logging. v Owner naming is enforced. You must qualify with the owner name any table, view, synonym, index, or constraint, unless you are the owner. Unless you enclose the name between quotation marks, alphabetic characters in owner names default to uppercase. v For databases, the default isolation level is REPEATABLE READ. v Default privileges on objects differ from those in databases that are not ANSI compliant. Users do not receive PUBLIC privilege to tables and synonyms by default. Other slight differences exist between databases that are ANSI compliant and those that are not. These differences are noted with the related SQL statement in this manual. For a detailed discussion of the differences between ANSI compliant databases and databases that are not ANSI-compliant, see the IBM Informix Database Design and Implementation Guide. Creating an ANSI-compliant database does not mean that you automatically receive warnings for Informix extensions to the ANSI/ISO standard for SQL syntax
2-91
CREATE DATABASE
when you run the database. You must also use the -ansi flag or the DBANSIWARN environment variable to receive such warnings. For additional information about -ansi and DBANSIWARN, see the IBM Informix Guide to SQL: Reference.
Related Information
Related statements: CLOSE DATABASE, CONNECT, DATABASE, and DROP DATABASE
2-92
Syntax
CREATE DISTINCT TYPE distinct_type AS source_type
Element distinct_type
Description
Restrictions
Name that you In an ANSI-compliant database, the combination of the declare here for the owner and data type must be unique within the database. In new distinct data type a database that is not ANSI compliant, the name must be unique among names of data types in the database. Name of existing type Must be either a built-in data type or one created with the on which the new CREATE DISTINCT TYPE, CREATE OPAQUE TYPE, or type is based CREATE ROW TYPE statement
source_type
Usage
A distinct type is a data type based on a built-in data type or on an existing opaque data type, a named ROW data type, or another distinct data type. Distinct data types are strongly typed. Although the distinct type has the same physical representation of data as its source type, values of the two types cannot be compared without an explicit cast from one type to the other. To create a distinct data type, you must have the Resource privilege on the database. Any user with the Resource privilege can create a distinct type from one of the built-in data types, which user informix owns. Important: You cannot create a distinct type on the SERIAL or SERIAL8 data types. To create a distinct type from an opaque type, from a named ROW type, or from another distinct type, you must be the owner of the data type or have the Usage privilege on the data type. By default, after a distinct type is defined, only the owner of the distinct type and the DBA can use it. The owner of the distinct type, however, can grant to other users the Usage privilege on the distinct type. A distinct type has the same storage structure as its source type. The following statement creates the distinct type birthday, based on the built-in DATE data type:
CREATE DISTINCT TYPE birthday AS DATE
Although Dynamic Server uses the same storage format for the distinct type as it does for its source type, a distinct type and its source type cannot be compared in an operation unless one type is explicitly cast to the other type.
2-93
To directly compare the distinct type and its source type or assign a value of the source type to a column of the distinct type, you must cast one type to the other, as the following examples show:
INSERT INTO tab (col1) VALUES (3.5::dist_type); SELECT col1, col2 FROM t WHERE (col1::NUMERIC) > col2; SELECT col1, col2, (col1 + col2::dist_type) sum_col FROM tab;
2-94
Related Information
Related statements: CREATE CAST, CREATE FUNCTION, CREATE OPAQUE TYPE, CREATE ROW TYPE, DROP TYPE, and DROP ROW TYPE For information and examples that show how to use and cast distinct types, see the IBM Informix Guide to SQL: Tutorial. For more information on when you might create a distinct type, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
2-95
CREATE DUPLICATE
Use the CREATE DUPLICATE statement to create a duplicate copy of an existing table for read-only use in a specified dbslice or in specified dbspaces across coservers. Only Extended Parallel Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
, CREATE DUPLICATE OF TABLE table IN ( dbspace dbslice )
Description Name of a dbslice in which to duplicate one fragment of table Name of a dbspace in which to duplicate one fragment of table Name of the original table from which to create a duplicate
Restrictions Must exist and must contain at most one dbspace on each target coserver
Must exist; must not contain an original or Identifier on page duplicate fragment of table 5-22 Must exist in the database. See also Supported Operations on page 2-97. Database Object Name on page 5-17
Usage
If the original table resides entirely on a single coserver, you can create duplicate copies of small tables across coservers for read-only use. For each attached index of the original table, a similarly defined index is built on each table duplicate, using the same dbspaces as the table. Because query operators read the local copy of the table, duplicating small tables across coservers might improve the performance of some queries. If a local copy of a duplicated table exists but is not available because the dbspace that stores it is offline (or for some similar reason), a query that requires access to the table fails. The database server does not attempt to access the original table. The location of a duplicated table can be either a dbslice or a comma-separated list of dbspaces. You can combine dbslices and lists of dbspaces in a single CREATE DUPLICATE statement. v If the original table is not fragmented, the dbspace list need provide only a single dbspace on each coserver. For example, if the table tab1 is not fragmented, enter the following statement to create a duplicate on the remaining three of the four coservers. If the original table is stored in the dbspace db1 on coserver 1 and db2 is on coserver 2, db3 is on coserver 3, and db4 is on coserver 4.
CREATE DUPLICATE OF TABLE tab1 IN (db2, db3, db4)
v If the original table is fragmented with one fragment in the first dbspace of several dbslices that contain dbspaces on all coservers, you can create duplicate copies of the table in the remaining dbspaces of the dbslice. For example, you might create the tab3 table in the first dbspace of three dbslices, each of which contains a dbspace on each coserver, as follows:
2-96
CREATE DUPLICATE
CREATE TABLE tab3 (...) FRAGMENT BY HASH (....) IN dbsl1.l, dbsl2.1, dbsl3.1;
To duplicate the tab3 table across the remaining coservers, use the following statement:
CREATE DUPLICATE OF TABLE tab3 IN dbsl1, dbsl2, dbsl3
v You can mix dbslice names and dbspace lists in the same CREATE DUPLICATE statement. For example, instead of using dbspaces in a dbslice, for the previous example, you might enter the following statement in which dbsp2a is on coserver 2, dbsp3a is on coserver 3, and dbsp4a is on coserver 4:
CREATE DUPLICATE OF TABLE tab3 IN dbsl1, dbsl2, (dbsp2a, dbsp3a, dbsp4a)
The first fragment of the original table is duplicated into dbsl1, which contains a dbspace on each coserver, the second fragment into dbsl2, which also contains a dbspace on each coserver, and the third fragment into the list of dbspaces. Only one fragment of a duplicated table can reside in any single dbspace. You cannot list an existing dbspace of the duplicated table in the list of dbspaces into which it is duplicated, but it is not an error for an existing dbspace to be a member of a dbslice that specifies duplication dbspaces. Matching dbspaces in the dbslice are ignored. The CREATE DUPLICATE statement requires the ALTER privilege.
Supported Operations
The following operations are permitted on duplicated tables: v SELECT v UPDATE STATISTICS v LOCK and UNLOCK v SET Residency v DROP TABLE You cannot duplicate a table in certain circumstances. The table must not: v Have GK or detached indexes v Use range fragmentation v v v v Be a temporary table Be a violations or diagnostic table Contain BYTE, TEXT, SERIAL, or SERIAL8 columns Have referential constraints
The CREATE DUPLICATE statement does not support incremental duplication. It also does not support multiple duplicates of the same table on a single coserver, nor duplication of tables that are fragmented across coservers. If you need to take a dbspace offline and it contains a copy of a duplicated table, or if you need to update data in a duplicated table, first drop all duplicates of the table, as described in DROP DUPLICATE on page 2-299.
Related Information
DROP DUPLICATE
2-97
Syntax
(1) CREATE EXTERNAL TABLE table Column Definition (3) USING( (2) Table Options Table Options DATAFILES Clause (2) )
Notes: 1 2 3 See Column Definition See Table Options on page 2-103 See DATAFILES Clause on page 2-102
Description Name declared here for a table to store external data Restrictions Must be unique among names of tables, views, and synonyms in the current database Syntax Database Object Name on page 5-17
Element table
Usage
The first portion of the syntax diagram declares the name of the table and defines its columns and any column-level constraints. The portion that follows the USING keyword identifies external files that the database server opens when you use the external table, and specifies additional options for characteristics of the external table. After executing the CREATE EXTERNAL TABLE statement, you can move data to and from the external source with an INSERT INTO ... SELECT statement. See the section INTO EXTERNAL Clause (XPS) on page 2-523 for more information about loading the results of a query into an external table.
Column Definition
Column Definition:
SAMEAS template , (1) column Data Type Other Optional Clauses
2-98
EXTERNAL
HEX TEXT (1) Data Type 'PACKED(p,s)' 'ZONED(p,s)' 'BINARY(n)' NULL 'null_string'
Notes: 1 2 3 See Data Type on page 4-18 See DEFAULT Clause on page 2-174 See Column-Level Constraints on page 2-101
Description One column name for each column of the external table Number of 8-bit bytes to represent the integer Precision (number of digits) Scale (digits in fractional part) Value to represent NULL Existing table with the same schema as the external table Restrictions Syntax
For each column, you must specify a Identifier on page 5-22 built-in data type For FIXED format binary integers; big-endian byte order For FIXED-format files only For FIXED-format files only See Defining NULL Values on page 2-100. Cannot be subset of columns nor differ in any column data type n=2 for 16-bit integers n=4 for 32-bit integers Literal Number on page 4-137 Literal Number on page 4-137 Quoted String on page 4-142 Database Object Name on page 5-17
2-99
If the packed decimal or zoned decimal is stored with all bits cleared to represent a NULL value, the null_string can be defined as 0x0. The following rules apply to the value assigned to a null_string: v The NULL representation must fit into the length of the external field. v If a bit pattern is defined, the null_string is not case sensitive. v If a bit pattern is defined, the null_string must begin with 0x. v For numeric fields, the left-most fields are assigned zeros by the database server if the bit pattern does not fill the entire field. v If the NULL representation is not a bit pattern, the NULL value must be a valid number for that field. Warning: If a row that contains a NULL value is unloaded into an external table and the column that receives the NULL value has no NULL value defined, the database server inserts a zero into the column. TEXT and HEX External Types: An Informix BYTE or TEXT column can be encoded in either the TEXT or HEX external type. You can use only delimited BYTE and TEXT formats with these external types. Fixed formats are not allowed. In addition, you cannot use these external types with any other type of delimited-format columns (such as character columns).
2-100
Column-Level Constraints
Use column-level constraints to limit the type of data that is allowed in a column. Constraints at the column level are limited to a single column. Column-Level Constraints:
(1) )
2-101
DATAFILES Clause
The DATAFILES clause specifies external files that are opened when you use external tables. DATAFILES Clause:
DATAFILES
Syntax Identifier on page 5-22 Literal Number on page 4-137 Must conform to operating-system rules. Must conform to operating-system rules.
Numeric ID of coserver containing the external data Must exist Pathname for input or output files in the definition of the external table Formatted pathname that uses pattern-matching characters Must exist Must exist
You can use cogroup names and coserver numbers when you describe the input or output files for the external table definition. You can identify the DATAFILES either by coserver number or by cogroup name. A coserver number contains only digits. A cogroup name is a valid identifier that begins with a letter but otherwise contains any combination of letters, digits, and underscore symbols. If you use only some of the available coservers for reading or writing files, you can designate these coservers as a cogroup using onutil and then use the cogroup name, rather than explicitly specifying each coserver and file separately. Whenever you use all coservers to manage external files, you can use the predefined coserver_group. For examples of the DATAFILES clause, see Examples on page 2-106.
2-102
Important: The formatted pathname option does not support the %o formatting string.
Table Options
These options specify additional characteristics that define the table. Table Options:
, DELIMITED FORMAT ' INFORMIX FIXED DEFAULT ESCAPE EXPRESS DELUXE ASCII CODESET ' ' EBCDIC DELIMITER field_delimiter RECORDEND record_delimiter MAXERRORS num_errors REJECTFILE filename SIZE num_rows '
Description
Restrictions
Syntax Quoted String on page 4-142 Must conform to operating-system rules. Literal Number on page 4-137 Literal Number on page 4-137 Quoted String on page 4-142 Quoted String on page 4-142
Character to separate fields. Default For nonprinting characters, is pipe ( | ) character use octal Full pathname for conversion error messages from coservers Errors per coserver before load operations are terminated Approximate number of rows contained in the external table ASCII character specified here as the escape character Character to separate records Default is Newline ( \n ) See Reject Files on page 2-104. Value is ignored unless MAXERRORS is set Cannot be a negative number Only a single character is valid For nonprinting characters, use octal
The num_errors specification is ignored during unload tasks. If the MAXERRORS environment variable is not set, the database server processes all data in load operations, regardless of the number of errors or num_errors value. If the RECORDEND environment variable is not set, record_delimiter defaults to the Newline character ( \n ). To specify a nonprinting character as the record delimiter or field delimiter, you must encode it as the octal representation of the ASCII character. For example, \006 can represent CTRL-F.
2-103
SIZE
Important: Check constraints on external tables are designed to be evaluated only when loading data. The database server cannot enforce check constraints on external tables because the data can be freely altered outside the control of the database server. If you want to restrict rows that are written to an external table during unload, use a WHERE clause to filter the rows.
Reject Files
Rows that have conversion errors during a load or rows that violate check constraints on the external table are written to a reject file on the coserver that
2-104
The following table describes these elements of the reject file: Element coserver-number filename record reason-code field-name Description Number of the coserver from which the file is read Name of the input file Record number in the input file where the error was detected Description of the error External field name where the first error in the line occurred, or <none> if the rejection is not specific to a particular column Line that caused the error (delimited or fixed-position character files only), up to 80 characters
bad-line
The reject file writes the coserver-number, filename, record, field-name, and reason-code in ASCII. The bad-line information varies with the type of input file. v For delimited files or fixed-position character files, up to 80 characters of the bad line are copied directly into the reject file. v For Informix internal data files, the bad line information is not placed in the reject file because you cannot edit the binary representation in a file; but the coserver-number, filename, record, reason-code, and field-name are still reported in the reject file so you can isolate the problem. Use the Table Options clause to specify the format of the external data. Errors that can cause a row to be rejected include the following. Error Text Explanation
CONSTRAINT constraint name This constraint was violated. CONVERT_ERR MISSING_DELIMITER MISSING_RECORDEND NOT NULL ROW_TOO_LONG Any field encounters a conversion error. No delimiter was found. No record end was found. A NULL was found in field-name. The input record is longer than 32 kilobytes.
2-105
Examples
The examples in this section show how to specify the DATAFILES field. Assume that the database server is running on four nodes, and one file is to be read from each node. All files have the same name. The DATAFILES specification can then be as follows:
DATAFILES ("DISK:cogroup_all:/work2/unload.dir/mytbl")
Now, consider a system with 16 coservers where only three coservers have tape drives attached (for example, coservers 2, 5, and 9). If you define a cogroup for these coservers before you run load and unload commands, you can use the cogroup name rather than a list of individual coservers when you execute the commands. To set up the cogroup, run onutil.
% onutil 1> create cogroup tape_group 2> from coserver.2, coserver.5, coserver.9; Cogroup successfully created.
If, instead, you want to process three files on each of two coservers, define the files as follows:
DATAFILES ("DISK:1:/work2/extern.dir/mytbl.%r(1..3)", "DISK:2:/work2/extern.dir/mytbl.%r(4..6)")
Related Information
Related statements: INSERT and SET PLOAD FILE See also the INTO Table Clauses of SELECT. For more information on external tables, refer to your IBM Informix Administrator's Reference.
2-106
CREATE FUNCTION
Use the CREATE FUNCTION statement to create a user-defined function, register an external function, or to write and register an SPL function. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE DBA Routine Parameter List FUNCTION function( (1) )
Notes: 1 2 3 4 5 6 7 8 9 See Routine Parameter List on page 5-61 See Return Clause on page 5-51 See Specific Name on page 5-68 See Routine Modifier on page 5-54 Stored Procedure Language only See Statement Block on page 5-69 External routines only See External Routine Reference on page 5-20 See Quoted String on page 4-142
2-107
CREATE FUNCTION
Element function Description Name of new function that is defined here Restrictions You must have the appropriate language privileges. See GRANT on page 2-371 and Overloading the Name of a Function on page 2-109. The specified pathname must exist on the computer where the database resides Syntax Database Object Name on page 5-17
pathname
Tip: If you are trying to create a function from text of source code that is in a separate file, use the CREATE FUNCTION FROM statement.
Usage
Dynamic Server supports user-defined functions written in these languages: v Stored Procedure Language (SPL) v One of the external languages (C or Java) that Dynamic Server supports (external functions) When the IFX_EXTEND_ROLE configuration parameter of is set to ON, only users to whom the DBSA grants the built-in EXTEND role can create external functions. How many values a function can return is language-dependent. Functions written in SPL can return one or more values. External functions written in the C or Java languages must return exactly one value. But a C function can return a collection type, and external functions in queries can return additional values indirectly from OUT parameters (and for the Java language, from INOUT parameters) that Dynamic Server can process as statement-local variables (SVLs). For information on how this manual uses the terms UDR, function, and procedure as well as recommended usage, see Relationship Between Routines, Functions, and Procedures on page 2-146 and Using CREATE PROCEDURE Versus CREATE FUNCTION on page 2-146, respectively. The entire length of a CREATE FUNCTION statement must be less than 64 kilobytes. This length is the literal length of the statement, including whitespace characters such as blank spaces and tabs. In ESQL/C, you can use a CREATE FUNCTION statement only within a PREPARE statement. If you want to create a user-defined function for which the text is known at compile time, you must put the text in a file and specify this file with the CREATE FUNCTION FROM statement. Functions use the collating order that was in effect when they were created. See SET COLLATION for information about using non-default collation.
2-108
CREATE FUNCTION
By default, Usage privilege on SPL is granted to PUBLIC. You must also have at least Resource privilege on a database to create an SPL function in that database.
DOCUMENT Clause
The quoted string in the DOCUMENT clause provides a synopsis and description of the UDR. The string is stored in the sysprocbody system catalog table and is intended for the user of the UDR. Anyone with access to the database can query the sysprocbody system catalog table to obtain a description of one or all of the UDRs stored in the database. For example, the following query obtains a description of the SPL function update_by_pct, that SPL Functions on page 2-110 shows:
2-109
CREATE FUNCTION
SELECT data FROM sysprocbody b, sysprocedures p WHERE b.procid = p.procid --join between the two catalog tables AND p.procname = update_by_pct -- look for procedure named update_by_pct AND b.datakey = D-- want user document;
A UDR or application program can query the system catalog tables to fetch the DOCUMENT clause and display it for a user. For C and Java language functions, you can include a DOCUMENT clause at the end of the CREATE FUNCTION statement, whether or not you use the END FUNCTION keywords.
SPL Functions
SPL functions are UDRs written in SPL that return one or more values. To write and register an SPL function, use a CREATE FUNCTION statement. Embed appropriate SQL and SPL statements between the CREATE FUNCTION and END FUNCTION keywords. You can also follow the function with the DOCUMENT and WITH FILE IN options. SPL functions are parsed, optimized (as far as possible), and stored in the system catalog tables in executable format. The body of an SPL function is stored in the sysprocbody system catalog table. Other information about the function is stored in other system catalog tables, including sysprocedures, sysprocplan, and sysprocauth. For more information about these system catalog tables, see the IBM Informix Guide to SQL: Reference. The END FUNCTION keywords are required in every SPL function, and a semicolon ( ; ) must follow the clause that immediately precedes the statement block. The following code example creates an SPL function:
CREATE FUNCTION update_by_pct ( pct INT, pid CHAR(10)) RETURNING INT; DEFINE n INT; UPDATE inventory SET price = price + price * (pct/100) WHERE part_id = pid;
2-110
CREATE FUNCTION
LET n = price; RETURN price; END FUNCTION DOCUMENT "USAGE: Update a price by a percentage", "Enter an integer percentage from 1 - 100", "and a part id number" WITH LISTING IN /tmp/warn_file
For more information on how to write SPL functions, see the chapter about SPL routines in IBM Informix Guide to SQL: Tutorial. See also the section Transactions in SPL Routines on page 5-72. You can include valid SQL or SPL language statements in SPL functions. See, however, the following sections in Chapter 5 that describe restrictions on SQL and SPL statements within SPL routines: Subset of SPL Statements Valid in the Statement Block on page 5-69; SQL Statements Not Valid in an SPL Statement Block on page 5-70; and Restrictions on SPL Routines in Data-Manipulation Statements on page 5-71.
Internal representation
External functions are functions you write in an external language (that is, a programming language other than SPL) that Dynamic Server supports. To create a C user-defined function: 1. Write the C function. 2. Compile the function and store the compiled code in a shared library (the shared-object file for C). 3. Register the function in the database server with the CREATE FUNCTION statement. To create a user-defined function written in the Java language: 1. Write a Java static method, which can use the JDBC functions to interact with the database server. 2. Compile the Java source file and create a .jar file (the shared-object file for Java). 3. Execute the install_jar( ) procedure with the EXECUTE PROCEDURE statement to install the jar file in the current database. 4. If the UDR uses user-defined types, create a map between SQL data types and Java classes. Use the setUDTExtName( ) procedure that is explained in EXECUTE PROCEDURE on page 2-336. 5. Register the UDR with the CREATE FUNCTION statement. Rather than storing the body of an external routine directly in the database, the database server stores only the pathname of the shared-object file that contains the compiled version of the routine. When it executes the external routine, the database server invokes the external object code. The database server stores information about an external function in system catalog tables, including sysprocbody and sysprocauth. For more information on the system catalog, see the IBM Informix Guide to SQL: Reference.
2-111
CREATE FUNCTION
returns a single Boolean value. The external routine reference name specifies the path to the C shared library where the function object code is actually stored. This library contains a C function basetype1_equal( ), which is invoked during execution of the equal( ) function.
CREATE FUNCTION equal ( arg1 basetype1, arg2 basetype1) RETURNING BOOLEAN; EXTERNAL NAME "/usr/lib/basetype1/lib/libbtype1.so(basetype1_equal)" LANGUAGE C END FUNCTION
This function returns a single INTEGER value. The EXTERNAL NAME clause specifies that the Java implementation of the sql_explosive_reaction( ) function is a method called explosiveReaction( ), which resides in the Chemistry Java class that resides in the course_jar jar file.
If user joan now executes function func1, user mike, not user joan, is the owner of the newly created table tab1. In the case of a DBA-privileged UDR, however, the user who executes a UDR (rather than the UDR owner) owns any database objects created by the UDR, unless another owner is specified for the database object within the UDR. For example, assume that user mike creates this user-defined function:
CREATE DBA FUNCTION func2 () RETURNING INT; CREATE TABLE tab2 (coly INT); RETURN 1; END FUNCTION;
If user joan now executes function func2, user joan, not user mike, is the owner of the newly created table tab2. See also the section Support for Roles and User Identity on page 5-72.
Related Information
Related statements: ALTER FUNCTION, ALTER ROUTINE, CREATE PROCEDURE, CREATE FUNCTION FROM, DROP FUNCTION, DROP ROUTINE, GRANT, EXECUTE FUNCTION, PREPARE, REVOKE, and UPDATE STATISTICS
2-112
CREATE FUNCTION
Chapter 3 of this manual describes the SPL language. For a discussion of how to create and use SPL routines, see the IBM Informix Guide to SQL: Tutorial. For a discussion of how to create and use external routines, see IBM Informix User-Defined Routines and Data Types Developer's Guide. For information about how to create C UDRs, see the IBM Informix DataBlade API Programmer's Guide.
2-113
Syntax
CREATE FUNCTION FROM file file_var
Element file
Description Path and filename of a file that contains the full CREATE FUNCTION statement text. Default pathname is current directory. Variable storing value of file
Restrictions Must exist and contain exactly one CREATE FUNCTION statement Same as for file
file_var
Usage
Functions written in the C or Java language are called external functions. When the IFX_EXTEND_ROLE configuration parameter is set to ON, only users who have been granted the built-in EXTEND role can create external functions. An ESQL/C program cannot directly create a user-defined function. That is, it cannot contain the CREATE FUNCTION statement. To create these functions within an ESQL/C program: 1. Create a source file with the CREATE FUNCTION statement. 2. Use the CREATE FUNCTION FROM statement to send the contents of this source file to the database server for execution. The file that you specify in the file parameter can contain only one CREATE FUNCTION statement. For example, suppose that the following CREATE FUNCTION statement is in a separate file, called del_ord.sql:
CREATE FUNCTION delete_order( p_order_num int) RETURNING int, int; DEFINE item_count int; SELECT count(*) INTO item_count FROM items WHERE order_num = p_order_num; DELETE FROM orders WHERE order_num = p_order_num; RETURN p_order_num, item_count; END FUNCTION;
In the ESQL/C program, you can access the delete_order( ) SPL function with the following CREATE FUNCTION FROM statement:
EXEC SQL create function from del_ord.sql;
If you are not sure whether the UDR in the file is a user-defined function or a user-defined procedure, use the CREATE ROUTINE FROM statement.
2-114
Related Information
Related statements: CREATE FUNCTION, CREATE PROCEDURE, CREATE PROCEDURE FROM, and CREATE ROUTINE FROM
2-115
CREATE INDEX
Use the CREATE INDEX statement to create an index for one or more columns in a table, or on values returned by a UDR that uses column values as arguments. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE
(1) Index-Type Options (3) GK INDEX index ON static ( GK SELECT Clause Index Scope Index-Key Specs
(5)
(6) ONLINE
Index Scope:
INDEX index ON table synonym
Index Options:
(8)
(10)
Notes: 1 2 3 4 5 6 7 8 9 10 See Index-Type Options on page 2-117 See Index-Key Specification on page 2-118 Extended Parallel Server only See SELECT Clause for Generalized-Key Index on page 2-132 See LOCK MODE Options (XPS) on page 2-132 Dynamic Server only See USING Access-Method Clause (IDS) on page 2-123 See FILLFACTOR Option on page 2-125 See Storage Options on page 2-125 See Index Modes (IDS) on page 2-130
IBM Informix Guide to SQL: Syntax
2-116
CREATE INDEX
Element index static synonym, table Description Name declared here for a new index Table on which a Generalized Key index is created (XPS) Name or synonym of a standard or temporary table to be indexed Restrictions Must be unique among names of indexes in the database Table must exist and be static; it cannot be a virtual table Synonym and its table must exist in the current database Syntax Database Object Name on page 5-17 Database Object Name on page 5-17 Database Object Name on page 5-17
Usage
When you issue the CREATE INDEX statement, the table is locked in exclusive mode. If another process is using the table, CREATE INDEX returns an error. (For an exception, however, see The ONLINE Keyword (IDS) on page 2-302.) Indexes use the collation that was in effect when CREATE INDEX executed. A secondary-access method (sometimes referred to as an index-access method) is a set of database server functions that build, access, and manipulate an index structure such as a B-tree, R-tree, or an index structure that a DataBlade module provides, in order to speed up the retrieval of data. In Dynamic Server, neither synonym nor table can refer to a virtual table. If you are using Extended Parallel Server, use the USING BITMAP keywords to store the list of records in each key of the index as a compressed bitmap. The storage option is not compatible with a bitmap index because bitmap indexes must be fragmented in the same way as the table.
Index-Type Options
The index-type options let you specify attributes of the index. Index-Type Options:
DISTINCT UNIQUE
CLUSTER
A unique index prevents duplicate values in the customer_num column. A column with a unique index can have, at most, one NULL value. The DISTINCT and UNIQUE keywords are synonyms in this context, so the following statement has the same effect as the previous example:
CREATE DISTINCT INDEX c_num_ix ON customer (customer_num)
The index in both examples is maintained in ascending order, which is the default order.
Chapter 2. SQL Statements
2-117
CREATE INDEX
You can also prevent duplicates in a column or set of columns by creating a unique constraint with the CREATE TABLE or ALTER TABLE statement. You cannot specify an R-tree secondary-access method for a UNIQUE index key. For more information on how to create unique constraints, see the CREATE TABLE or ALTER TABLE statements. See also the section Differences Between a Unique Constraint and a Unique Index on page 2-178. How Indexes Affect Primary-Key, Unique, and Referential Constraints: The database server creates internal B-tree indexes for primary-key, unique, and referential constraints. If a primary-key, unique, or referential constraint is added after the table is created, any user-created indexes on the constrained columns are used, if appropriate. An appropriate index is one that indexes the same columns that are used in the primary-key, referential, or unique constraint. If an appropriate user-created index is not available, the database server creates a nonfragmented internal index on the constrained column or columns.
CLUSTER Option
Use the CLUSTER keyword to reorder the rows of the table in the order that the index designates. The CREATE CLUSTER INDEX statement fails if a CLUSTER index already exists on the same table.
CREATE CLUSTER INDEX c_clust_ix ON customer (zipcode)
This statement creates an index on the customer table and physically orders the rows according to their postal code values, in (by default) ascending order. If the CLUSTER option is specified in addition to fragments on, the data values are clustered only within each fragment, and not globally across the entire table. In Dynamic Server, you cannot specify the CLUSTER option and the ONLINE keyword in the same statement. In addition, some secondary-access methods (such as R-tree) do not support clustering. Before you specify CLUSTER for your index, be sure that the index uses an access method that supports clustering. If you are using Extended Parallel Server, you cannot use the CLUSTER option on STANDARD tables. In addition, you cannot specify the CLUSTER option and storage options in the same CREATE INDEX statement (see Storage Options on page 2-125). When you create a clustered index, the constrid of any unique or referential constraints on the associated table changes. The constrid is stored in the sysconstraints system catalog table.
Index-Key Specification
Use the Index-Key Specification of the CREATE INDEX statement to define the key value for the index, to specify whether to sort the index in ascending or descending order, and to identify a default operator class if the secondary access method in the USING clause has no default operator class, or to override its default operator class. Index-Key Specification:
, ( column , (1) function ( func_col ) (1) op_class ASC DESC )
2-118
CREATE INDEX
Notes: 1
Element column function Description
Column or columns used as a See Using a Column or Column List as the Index key to this index Key on page 2-119. User-defined function used as Must be a nonvariant function that does not return a a key to this index large object data type. Cannot be a built-in algebraic, exponential, log, or hex function. Columns whose values are arguments to function
func_col op_class
See Using a Function as an Index Key (IDS) on page Identifier on 2-120. page 5-22 Identifier on page 5-22
Operator class associated with If the secondary-access method in the USING clause column or function for this has no default operator class, you must specify one index here. (See Using an Operator Class (IDS) on page 2-123.)
The index-key value can be one or more columns that contain built-in data types. If you specify multiple columns, the concatenation of values from the set of columns is treated as a single composite column for indexing. In Dynamic Server, the index-key value also can be one of the following: v A column of type LVARCHAR(size), if size is smaller than 387 bytes v One or more columns that contain user-defined data types v One or more values that a user-defined function returns (referred to as a functional index) v A combination of columns and functions The 387-byte LVARCHAR size limit is for dbspaces of the default (2 kilobyte) page size, but dbspaces of larger page sizes can support larger index key sizes, as listed in the following table.
Table 2-1. Maximum Index Key Size for Selected Page Sizes Page Size Maximum Index Key Size 2 kilobytes 4 kilobytes 387 bytes 796 bytes
2-119
CREATE INDEX
the column or column list; you cannot create another unique index on this column or column list with the CREATE INDEX statement. v The number of indexes that you can create on the same column or the same set of columns is restricted. See Restrictions on the Number of Indexes on a Set of Columns on page 2-122. In Dynamic Server, these additional restrictions apply to indexes: v You cannot create an index on a column of an external table. v The column cannot be of a collection data type.
You can create functional indexes within an SPL routine. You can also create an index on a nonvariant user-defined function that does not return a large object. The functional index can be a B-tree index, an R-tree index, or a user-defined secondary-access method. The ONLINE keyword, however, is not valid when you create a functional index; see The ONLINE Keyword (IDS) on page 2-302.
The UNIQUE keyword prevents any duplicates of a given combination of stock_num and manu_code. The index is in ascending order by default. You can include up to 16 columns in a composite index. The total width of all indexed columns in a single composite index cannot exceed 380 bytes. In Dynamic Server, an index key part is either a column in a table, or the result of a user-defined function on one or more columns. A composite index can have up to 16 key parts that are columns, or up to 341 key parts that are values returned by a UDR. This limit is language-dependent and applies to UDRs written in SPL or Java; functional indexes based on C language UDRs can have up to 102 key parts. A composite index can have any of the following items as an index key: v One or more columns v One or more values that a user-defined function returns (referred to as a functional index) v A combination of columns and user-defined functions
2-120
CREATE INDEX
For dbspaces of the default page size of 2 kilobytes, the total width of all indexed columns in a single CREATE INDEX statement cannot exceed 387 bytes, except for functional indexes of Dynamic Server, whose language-dependent limits are described earlier in this section. Whether the index is based directly on column values in the table, or on functions that take column values as arguments, the maximum size of the index key depends only on page size. The maximum index key size for functional indexes in dbspaces larger than 2 kilobytes are the same as for non-functional indexes. The only difference between limits on column indexes and functional indexes is the number of key parts. A column based index can have no more than 16 key parts and a functional index has different language-dependent limits on key parts. For a given page size, the maximum index key size is the same for both column-based and functional indexes.
In this example, the customer_num column has a unique constraint placed on it. The first CREATE INDEX statement places an index sorted in descending order on the customer_num column. The second CREATE INDEX includes the customer_num column as part of a composite index. For more information on composite indexes, see Creating Composite Indexes on page 2-120. Bidirectional Traversal of Indexes: If you do not specify the ASC or DESC keywords when you create an index on a single column, key values are stored in ascending order by default; but the bidirectional-traversal capability of the database server lets you create just one index on a column and use that index for queries that specify sorting of results in either ascending or descending order of the sort column.
2-121
CREATE INDEX
Because of this capability, it does not matter whether you create a single-column index as an ascending or descending index. Whichever storage order you choose for an index, the database server can traverse that index in ascending or descending order when it processes queries. If you create a composite index on a table, however, the ASC and DESC keywords might be required. For example, if you want to enter a SELECT statement whose ORDER BY clause sorts on multiple columns and sorts each column in a different order, and you want to use an index for this query, you need to create a composite index that corresponds to the ORDER BY columns. For example, suppose that you want to enter the following query:
SELECT stock_num, manu_code, description, unit_price FROM stock ORDER BY manu_code ASC, unit_price DESC
This query sorts first in ascending order by the value of the manu_code column and then in descending order by the value of the unit_price column. To use an index for this query, you need to issue a CREATE INDEX statement that corresponds to the requirements of the ORDER BY clause. For example, you can enter either of the following statements to create the index:
CREATE INDEX stock_idx1 ON stock (manu_code ASC, unit_price DESC); CREATE INDEX stock_idx2 ON stock (manu_code DESC, unit_price ASC);
The composite index that was used for this query (stock_idx1 or stock_idx2) cannot be used for queries in which you specify the same sort direction for the two columns in the ORDER BY clause. For example, suppose that you want to enter the following queries:
SELECT stock_num, manu_code, description, unit_price FROM stock ORDER BY manu_code ASC, unit_price ASC; SELECT stock_num, manu_code, description, unit_price FROM stock ORDER BY manu_code DESC, unit_price DESC;
If you want to use a composite index to improve the performance of these queries, you need to enter one of the following CREATE INDEX statements. You can use either one of the created indexes (stock_idx3 or stock_idx4) to improve the performance of the preceding queries.
CREATE INDEX stock_idx3 ON stock (manu_code ASC, unit_price ASC); CREATE INDEX stock_idx4 ON stock (manu_code DESC, unit_price DESC);
You can create no more than one ascending index and one descending index on a column. Because of the bidirectional-traversal capability of the database server, you only need to create one of the indexes. Creating both would achieve exactly the same results for an ascending or descending sort on the stock_num column. After INSERT or DELETE operations are performed on an indexed table, the number of index entries can vary within a page, and the number of index pages that a table requires can depend on whether the index specifies ascending or descending order. For some load and DML operations, a descending single-column or multi-column index might cause the database server to allocate more index pages than an ascending index requires. Restrictions on the Number of Indexes on a Set of Columns: You can create multiple indexes on a set of columns, provided that each index has a unique
2-122
CREATE INDEX
combination of ascending and descending columns. For example, to create all possible indexes on the stock_num and manu_code columns of the stock table, you could create four indexes: v The ix1 index on both columns in ascending order v The ix2 index on both columns in descending order v The ix3 index on stock_num in ascending order and on manu_code in descending order v The ix4 index on stock_num in descending order and on manu_code in ascending order Because of the bidirectional-traversal capability of the database server, you do not need to create these four indexes. You only need to create two indexes: v The ix1 and ix2 indexes achieve the same results for sorts in which the user specifies the same sort direction (ascending or descending) for both columns, so you only need one index of this pair. v The ix3 and ix4 indexes achieve the same results for sorts in which the user specifies different sort directions for the two columns (ascending on the first column and descending on the second column or vice versa). Thus, you only need to create one index of this pair. (See also Bidirectional Traversal of Indexes on page 2-121.) Dynamic Server can also support multiple indexes on the same combination of ascending and descending columns, if each index has a different collating order; see SET COLLATION on page 2-531.
2-123
CREATE INDEX
Element parameter sec_acc _method value Description Secondary-access-method parameter for this index Secondary-access method for this index Value of the specified parameter Restrictions See the user documentation for your user-defined access method Method can be a B-tree, R-tree, or user-defined access method, such as one that a DataBlade module defines Must be a valid literal value for parameter in this secondary-access method Syntax Quoted String on page 4-142 Identifier on page 5-22 Quoted String on page 4-142 or Literal Number on page 4-137
A secondary-access method is a set of routines that perform all of the operations needed for an index, such as create, drop, insert, delete, update, and scan. The database server provides the following secondary-access methods: v The generic B-tree index is the built-in secondary-access method. A B-tree index is good for a query that retrieves a range of data values. The database server implements this secondary-access method and registers it as btree in the system catalog tables. v The R-tree method is a registered secondary-access method. An R-tree index is good for searches on multidimensional data. The database server registers this secondary-access method as rtree in the system catalog tables of a database. An R-tree secondary-access method is not valid for a UNIQUE index key. An R-tree index cannot be clustered, and cannot be stored in a dbspace that has a non-default page size. For more information on R-tree indexes, see the IBM Informix R-Tree Index User's Guide. The access method that you specify must be registered in the sysams system catalog table. The default secondary-access method is B-tree. If the access method is B-tree, you can create only one index for each unique combination of ascending and descending columnar or functional keys with operator classes. (This restriction does not apply to other secondary-access methods.) By default, CREATE INDEX creates a generic B-tree index. If you want to create an index with a secondary-access method other than B-tree, you must specify the name of the secondary-access method in the USING clause. Some user-defined access methods are packaged as DataBlade modules. Some DataBlade modules provide indexes that require specific parameters when you create them. For more information about user-defined access methods, refer to the documentation of your secondary access-method or DataBlade module. The following example (for a database that implements R-tree indexes) creates an R-tree index on the location column that contains an opaque data type, point, and performs a query with a filter on the location column.
CREATE INDEX loc_ix ON TABLE emp (location) USING rtree; SELECT name FROM emp WHERE location N_equator_equals point(500, 0);
The following CREATE INDEX statement creates an index that uses the fulltext secondary-access method, which takes two parameters: WORD_SUPPORT and PHRASE_SUPPORT. It indexes a table t, which has two columns: i, an integer column, and data, a TEXT column.
2-124
CREATE INDEX
CREATE INDEX tx ON t(data) USING fulltext (WORD_SUPPORT=PATTERN, PHRASE_SUPPORT=MAXIMUM);
FILLFACTOR Option
The FILLFACTOR option takes effect only in the following cases: v when you build an index on a table that contains more than 5,000 rows and that uses more than 100 table pages v when you create an index on a fragmented table v when you create a fragmented index on a nonfragmented table. Use the FILLFACTOR option to provide for expansion of an index at a later date or to create compacted indexes. FILLFACTOR Option:
FILLFACTOR percent
Element percent
Description Percentage of each index page that is filled by index data when the index is created. The default is 90.
When the index is created, the database server initially fills only that percentage of the nodes specified with the FILLFACTOR value. The FILLFACTOR can also be set as a parameter in the ONCONFIG file. The FILLFACTOR clause on the CREATE INDEX statement overrides the setting in the ONCONFIG file. For more information about the ONCONFIG file and the parameters you can use, see your IBM Informix Administrator's Guide.
Storage Options
The storage options specify the distribution scheme of an index. You can use the IN clause to specify a storage space for the entire index, or you can use the FRAGMENT BY clause to fragment the index across multiple storage spaces.
Chapter 2. SQL Statements
2-125
CREATE INDEX
Storage Options:
IN dbspace (1) dbslice (2) extspace TABLE FRAGMENT BY Clause for Indexes
(3)
Notes: 1 2 3
Element dbslice dbspace extspace Description The dbslice that contains all of the index fragments The dbspace in which to store the index
Extended Parallel Server only Dynamic Server only See FRAGMENT BY Clause for Indexes on page 2-127
Restrictions Must exist Must exist Syntax Identifier on page 5-22 Identifier on page 5-22 See the documentation for your access method.
Name assigned by the onspaces command to a storage Must exist area outside the database server
If you specify any storage option (except IN TABLE), you create a detached index. Detached indexes are indexes that are created with a specified distribution scheme. Even if the distribution scheme specified for the index is identical to that specified for the table, the index is still considered to be detached. If the distribution scheme of a table changes, all detached indexes continue to use the distribution scheme that the Storage Option clause specified. For information on locally detached and globally detached indexes, see FRAGMENT BY Clause for Indexes on page 2-127. If you are using Extended Parallel Server, you cannot use the CLUSTER option and storage options in the same CREATE INDEX statement. See CLUSTER Option on page 2-118.
IN Clause
Use the IN clause to specify a storage space to hold the entire index. The storage space that you specify must already exist. Storing an Index in a dbspace: Use the IN dbspace clause to specify the dbspace where you want your index to reside. When you use this clause with any option except the TABLE keyword, you create a detached index. The IN dbspace clause allows you to isolate an index. For example, if the customer table is created in the custdata dbspace, but you want to create an index in a separate dbspace called custind, use the following statements:
CREATE TABLE customer . . . IN custdata EXTENT SIZE 16 CREATE INDEX idx_cust ON customer (customer_num) IN custind
Storing an Index Fragment in a Named Partition (IDS): Besides the option of storing a fragment of the index in a dbspace, Dynamic Server supports storing fragments of the index in a named subset of the dbspace, called a partition. Unless you explicitly declare names for the fragments in the PARTITION BY or
2-126
CREATE INDEX
FRAGMENT BY clause, each fragment, by default, has the same name as the dbspace where it resides. This includes all fragmented tables and indexes migrated from earlier releases of Dynamic Server. Storing an Index in a dbslice (XPS): Using Extended Parallel Server, the IN dbslice clause allows you to fragment an index across multiple dbspaces. The database server fragments the table by round-robin in the dbspaces that make up the dbslice when the table is created. Storing Data in an extspace (IDS): In general, use the extspace storage option in conjunction with the USING Access-Method Clause (IDS) on page 2-123. For more information, refer to the user documentation for your custom-access method. Creating an Attached Index with the IN TABLE Keywords (IDS): In some earlier releases of Dynamic Server, if you did not use the storage options to specify a distribution scheme, then, by default, the index used the same distribution scheme as the table on which it was built. Such an index is called an attached index. If you omit the Storage Options clause, Dynamic Server creates new indexes as detached indexes by default, but supports existing attached indexes created by earlier release versions. (The DEFAULT_ATTACH environment variable, however, can override the default behavior so that the new index is attached.) You can also specify IN TABLE as the storage option to create an attached index, even if the DEFAULT_ATTACH environment variable is not set. An attached index is created in the same dbspace (or dbspaces, if the table is fragmented) as the table on which it is built. If the distribution scheme of a table changes, all attached indexes start using the new distribution scheme. Only B-tree indexes that are nonfragmented and that are on nonfragmented tables can be attached. All other indexes, including extensibility related indexes, such as R-trees and functional indexes, must be detached. You cannot create an attached index using a collating order different from that of the table, nor different from what DB_LOCALE specifies. For information about the DB_LOCALE and DEFAULT_ATTACH environment variables, see the IBM Informix Guide to SQL: Reference. Important: Attached indexes are supported in this release for backward compatibility with Dynamic Server 7.x, but IBM does not recommend use of the DEFAULT_ATTACH environment variable nor of the IN TABLE storage option in new applications. Attached indexes are a deprecated feature that might not be supported in some future release of Dynamic Server.
2-127
CREATE INDEX
FRAGMENT BY (1) PARTITION BY EXPRESSION ( (2) HASH ( column ) IN dbslice , ( , HYBRID ( column ) EXPRESSION Clause dbspace ) Expression List , , REMAINDER Clause )
Expression List:
, ( expr ) IN dbspace (1) PARTITION part
REMAINDER Clause:
REMAINDER ( expr ) PARTITION part IN dbspace
(1)
EXPRESSION Clause:
, EXPRESSION expr IN dbslice dbspace , ( dbspace ) , REMAINDER expr IN dbslice dbspace , ( dbspace )
Expression defining which index keys each See Restrictions on fragment stores Fragmentation Expressions on page 2-129. Name that you declare here for a partition of a dbspace Required for any partition in the same dbspace as another partition of the same index
part
2-128
CREATE INDEX
Here the IN keyword introduces the name of a storage space where an index fragment is to be stored. If you list multiple dbspace names after the IN keyword, use parentheses to delimit the dbspace list. The parentheses around the list of fragment definitions that follow the EXPRESSION keyword are optional.
2-129
CREATE INDEX
server creates the index in the first dbspace that the DBSPACETEMP environment variable specifies. For more information on the DBSPACETEMP environment variable, see the IBM Informix Guide to SQL: Reference. For more information on the default storage characteristics of temporary tables, see Where Temporary Tables are Stored on page 2-214.
The following table explains the index modes Mode DISABLED Effect The database server does not update the index after insert, delete, and update operations that modify the base table. The optimizer does not use the index during the execution of queries. The database server updates the index after insert, delete, and update operations that modify the base table. The optimizer uses the index during query execution. If an insert or update operation causes a duplicate key value to be added to a unique index, the statement fails. The database server updates a unique index after insert, delete, and update operations that modify the base table. (This option is not available with duplicate indexes.) The optimizer uses the index during query execution. If an insert or update operation causes a duplicate key value to be added to a unique index in filtering mode, the statement continues processing, but the bad row is written to the violations table associated with the base table. Diagnostic information about the unique-index violation is written to the diagnostics table associated with the base table. If you specify filtering for a unique index, you can also specify one of the following error options. Error Option WITHOUT ERROR Effect A unique-index violation during an insert or update operation returns no integrity-violation error to the user. Any unique-index violation during an insert or update operation returns an integrity-violation error to the user.
ENABLED
FILTERING
WITH ERROR
2-130
CREATE INDEX
v You can set the mode of a unique index to enabled, disabled, or filtering. v If you do not specify a mode, then by default the index is enabled. v For an index set to filtering mode, if you do not specify an error option, the default is WITHOUT ERROR. v When you add a new unique index to an existing base table and specify the disabled mode for the index, your CREATE INDEX statement succeeds even if duplicate values in the indexed column would cause a unique-index violation. v When you add a new unique index to an existing base table and specify the enabled or filtering mode for the index, your CREATE INDEX statement succeeds provided that no duplicate values exist in the indexed column that would cause a unique-index violation. However, if any duplicate values exist in the indexed column, your CREATE INDEX statement fails and returns an error. v When you add a new unique index to an existing base table in the enabled or filtering mode, and duplicate values exist in the indexed column, erroneous rows in the base table are not filtered to the violations table. Thus, you cannot use a violations table to detect the erroneous rows in the base table. Adding a Unique Index When Duplicate Values Exist in the Column: If you attempt to add a unique index in the enabled mode but receive an error message because duplicate values are in the indexed column, take the following steps to add the index successfully: 1. Add the index in the disabled mode. Issue the CREATE INDEX statement again, but this time specify the DISABLED keyword. 2. Start a violations and diagnostics table for the target table with the START VIOLATIONS TABLE statement. 3. Issue a SET Database Object Mode statement to change the mode of the index to enabled. When you issue this statement, existing rows in the target table that violate the unique-index requirement are duplicated in the violations table. You receive an integrity-violation error message, however, and the index remains disabled. 4. Issue a SELECT statement on the violations table to retrieve the nonconforming rows that are duplicated from the target table. You might need to join the violations and diagnostics tables to get all the necessary information. 5. Take corrective action on the rows in the target table that violate the unique-index requirement. 6. After you fix all the nonconforming rows in the target table, issue the SET Database Object Mode statement again to switch the disabled index to the enabled mode. This time the index is enabled, and no integrity violation error message is returned because all rows in the target table now satisfy the new unique-index requirement.
2-131
CREATE INDEX
When an index is disabled, the database server stops updating it and stops using it during queries, but the catalog information about the disabled index is retained. You cannot create a new index on a column or set of columns if a disabled index on that column or set of columns already exists. Similarly, you cannot create an active (enabled) unique, foreign-key, or primary-key constraint on a column or on a set of columns if the indexes on which the active constraint depends are disabled.
LOCK MODE
In COARSE lock mode, index-level locks are acquired on the index instead of item-level or page-level locks. This mode reduces the number of lock calls on the index. Use the coarse-lock mode when you know the index is not going to change, as when only read-only operations are performed on the index. If you specify no lock mode, the default is NORMAL. That is, the database server places item-level or page-level locks on the index as necessary.
2-132
CREATE INDEX
, SELECT ALL DISTINCT (1) UNIQUE (2) Expression * table. synonym. alias.
Notes: 1 2 3 4
Element alias Description Temporary name assigned to the table in the FROM clause
Informix extension See Expression on page 4-34 See FROM Clause for Generalized-Key Index See WHERE Clause for Generalized-Key Index on page 2-134
Restrictions You cannot use an alias for the table on which the index is built Syntax Identifier on page 5-22
The synonym and the table to which it Database Object points must exist Name on page 5-17
The following restrictions apply to expressions in the GK SELECT clause: v It cannot refer to any SPL routine. v It cannot include the USER, TODAY, CURRENT, DBINFO built-in functions, nor any function that refers to a point in time or interval. FROM Clause for Generalized-Key Index: GK FROM Clause:
FROM indexed_table synonym1 , table synonym2 AS
alias
Description Temporary name for a table Table on which the index is being built
The FROM clause must include the Database Object indexed table Name on page 5-17 Database Object Name on page 5-17
Synonym or identifier of table from The synonym and the table to which to retrieve data which it points must exist
2-133
CREATE INDEX
All tables that appear in the FROM clause must be local static tables. That is views, non-static tables, and remote tables are not valid in the FROM clause. Tables that you specify in the FROM clause must be transitively joined on key to the indexed table. Table A is transitively joined on key to table B if A and B are joined with equal joins on the unique-key columns of A. Suppose that tables A, B, and C each have col1 as a primary key. In the following example, B is joined on key to A and C is joined on key to B. C is transitively joined on key to A.
CREATE GK INDEX gki (SELECT A.col1, A.col2 FROM A, B, C WHERE A.col1 = B.col1 AND B.col1 = C.col1)
Notes: 1 2 See Condition on page 4-5 See Specifying a Join in the WHERE Clause on page 2-511
The WHERE clause for a GK index has the following restrictions: v It cannot include USER, TODAY, CURRENT, or DBINFO built-in functions, nor any function that references a time value or a time-interval value. v It cannot refer to any SPL routine. v It cannot include any subquery. v It cannot include any aggregate function. v It cannot include any IN, LIKE, or MATCHES expression.
2-134
CREATE INDEX
this time, the database server briefly locks the table while updating the system catalog with information about the new index. The indexed table in a CREATE INDEX . . . ONLINE statement can be permanent or temporary, logged or unlogged, and fragmented or non-fragmented. You cannot specify the ONLINE keyword, however, when you create a functional index, a clustered index, a virtual index, or an R-tree index. The following statement instructs the database server to create a unique online index called idx_1 on the lname column of the customer table:
CREATE UNIQUE INDEX idx_1 ON customer(lname) ONLINE;
If, while this index is being constructed, other users insert into the customer table new rows in which lname is not unique, the database server issues an error after it has created the new idx_1 index and registered it in the system catalog. The term online index refers to the locking strategy that the database follows in creating or dropping an index with the ONLINE keyword, rather than to properties of the index that persist after its creation (or its destruction) has completed. This term appears in some error messages, however, and in recovery or restore operations, the database server re-creates as an online index any index that you created as an online index. No more than one CREATE INDEX . . . ONLINE or DROP INDEX . . . ONLINE statement can concurrently reference online indexes on the same table, or online indexes that have the same identifier.
Related Information
Related statements: ALTER INDEX, CREATE OPCLASS, CREATE TABLE, DROP INDEX, RENAME INDEX, and SET Database Object Mode For a discussion of the structure of indexes, see your IBM Informix Administrator's Reference. For a discussion of the different types of indexes and information about performance issues with indexes, see your IBM Informix Performance Guide. For a discussion of the GLS aspects of the CREATE INDEX statement, see the IBM Informix GLS User's Guide. For information about operator classes, refer to the CREATE OPCLASS statement and IBM Informix User-Defined Routines and Data Types Developer's Guide. For information about the indexes that DataBlade modules provide, refer to your DataBlade module documentation.
2-135
Syntax
CREATE OPAQUE TYPE type ( INTERNALLENGTH = length VARIABLE , ) , (1) Opaque-Type Modifier
Notes: 1
Element length type Description Number of bytes needed to store a value of this data type Name that you declare here for the new opaque data type
Usage
The CREATE OPAQUE TYPE statement registers a new opaque data type in the sysxtdtypes system catalog table. To create an opaque type, you must have the Resource privilege on the database. When you create the opaque type, only you, the owner, have the Usage privilege on the new opaque data type. You can use the GRANT or REVOKE statements to grant or revoke the Usage privilege of other users of the database. To view the privileges on a data type, check the sysxtdtypes system catalog table for the owner name, and check the sysxtdtypeauth system catalog table for additional type privileges that might have been granted. For details of system catalog tables, see the IBM Informix Guide to SQL: Reference. The DBAccess utility can also display privileges on opaque data types.
2-136
INTERNALLENGTH Modifier
The INTERNALLENGTH modifier specifies the storage size that is required for the opaque data type as fixed length or varying length. Fixed-Length Opaque Types: A fixed-length opaque type has an internal structure of fixed size. To create a fixed-length opaque type, specify the size of the internal structure, in bytes, for the INTERNALLENGTH modifier. The next example creates a fixed-length opaque type called fixlen_typ and allocates 8 bytes for storing this data type.
CREATE OPAQUE TYPE fixlen_typ(INTERNALLENGTH=8, CANNOTHASH)
Varying-Length Opaque Types: A varying-length opaque type has an internal structure whose size might vary from one value to another. For example, the internal structure of an opaque data type might hold the actual value of a string up to a certain size, but beyond this size it might use an LO-pointer to a CLOB to hold the value. To create a varying-length opaque data type, use the VARIABLE keyword with the INTERNALLENGTH modifier. The following statement creates a variable-length opaque data type called varlen_typ:
CREATE OPAQUE TYPE varlen_typ (INTERNALLENGTH=VARIABLE, MAXLEN=1024)
Opaque-Type Modifier
Opaque-Type Modifier:
Element align_value
Description Byte boundary on which to align an opaque type that is passed to a user-defined routine. Default is 4 bytes.
Restrictions Must be 1, 2, 4, or 8, depending on the C definition of the opaque data type and hardware and compiler used to build the object file for the data type
length
Maximum length to allocate for instances Must be a positive integer 32 kilobytes. Literal of varying-length opaque types. Default Do not specify for fixed-length data types. Number is 2 kilobytes. Values that exceed this length return errors. on page 4-137
Modifiers can specify the following optional information for opaque types: v MAXLEN specifies the maximum length for varying-length types. v CANNOTHASH specifies that the database server cannot use the built-in hash function on the opaque type. v ALIGNMENT specifies the byte boundary on which the database server aligns the opaque type. v PASSEDBYVALUE specifies that an opaque type that requires 4 bytes or fewer of storage is passed by value. By default, opaque types are passed to user-defined routines by reference.
Chapter 2. SQL Statements
2-137
output( )
receive( )
send( )
db_receive( )
db_send( )
server_receive( )
2-138
import( )
export ( )
importbinary( )
exportbinary( )
assign( )
destroy( )
Performs any processing necessary before removing a When the database server executes the row that contains the opaque type This support DELETE or DROP TABLE, before it function must be named destroy( ). removes the opaque type from disk Returns a list of the LO-pointer structures (pointers to When the database server must search smart large objects) in an opaque type opaque types for references to smart large objects; when oncheck runs, or an archive is performed Compares two values of the opaque type and returns When the database server encounters an integer value to indicate whether the first value is an ORDER BY, UNIQUE, DISTINCT, or less than, equal to, or greater than the second value UNION clause in a SELECT statement, or when CREATE INDEX creates a B-tree index
lohandles( )
compare( )
After you write the necessary support functions for the opaque type, use the CREATE FUNCTION statement to register these support functions in the same database as the opaque type. Certain support functions convert other data types to or from the new opaque type. After you create and register these support functions, use the CREATE CAST statement to associate each function with a particular cast. The cast must be registered in the same database as the support function. After you have written the necessary C language or Java language source code to define an opaque data type, you then use the CREATE OPAQUE TYPE statement to register the opaque data type in the database.
Related Information
Related statements: CREATE CAST, CREATE DISTINCT TYPE, CREATE FUNCTION, CREATE ROW TYPE, CREATE TABLE, and DROP TYPE For a description of an opaque type, see the IBM Informix Guide to SQL: Reference.
2-139
2-140
CREATE OPCLASS
Use the CREATE OPCLASS statement to create an operator class for a secondary-access method. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE OPCLASS opclass FOR sec_acc_method
Element opclass
Description Name that you declare here for a new operator class Secondary-access method with which the new operator class is associated Support function that the secondary-access method requires
Restrictions Must be unique among operator classes within the database Must already exist and must be registered in the sysams table Must be listed in the order expected by the access method
Syntax Identifier on page 5-22 Identifier on page 5-22 Identifier on page 5-22
sec_acc_method
support_function
Usage
An operator class is the set of operators that support a secondary-access method for query optimization and building the index. A secondary-access method (sometimes referred to as an index access method) is a set of database server functions that build, access, and manipulate an index structure such as a B-tree, R-tree, or an index structure that a DataBlade module provides. The database server provides the B-tree and R-tree secondary-access methods. For more information on the btree secondary-access method, see Default Operator Classes on page 2-144. Define a new operator class when you want one of the following: v An index to use a different order for the data than the sequence that the default operator class provides v A set of operators that is different from any existing operator classes that are associated with a particular secondary-access method You must have the Resource privilege or be the DBA to create an operator class. The actual name of an operator class is an SQL identifier. When you create an operator class, theopclass name must be unique within the database.
Chapter 2. SQL Statements
2-141
CREATE OPCLASS
When you create an operator class in an ANSI-compliant database, the owner.opclass combination must be unique within the database. The owner name is case sensitive. If you do not put quotes around the owner name (or else set the ANSIOWNER environment variable), the name of the operator-class owner is stored in uppercase letters. The following CREATE OPCLASS statement creates a new operator class called abs_btree_ops for the btree secondary-access method:
CREATE OPCLASS abs_btree_ops FOR btree STRATEGIES (abs_lt, abs_lte, abs_eq, abs_gte, abs_gt) SUPPORT (abs_cmp)
An operator class has two kinds of operator-class functions: v Strategy functions Specify strategy functions of an operator class in the STRATEGY clause of the CREATE OPCLASS statement. In the preceding CREATE OPCLASS code example, the abs_btree_ops operator class has five strategy functions. v Support functions Specify support functions of an operator class in the SUPPORT clause. In the preceding CREATE OPCLASS code example, the abs_btree_ops operator class has one support function.
STRATEGIES Clause
Strategy functions are functions that users can invoke within a DML statement to operate on a specific data type. The query optimizer uses the strategy functions to determine whether a given index can be used to process a query. If a query includes a UDF or a column on which an index exists, and if the qualifying operator in the query matches any function in the STRATEGIES clause, then the query optimizer considers using the index for the query. For more information on query plans, see your IBM Informix Performance Guide. When you create a new operator class, the STRATEGIES clause identifies the strategy functions for the secondary-access method. Each strategy specification lists the name of a strategy function (and optionally, the data types of its parameters). You must list these functions in the order that the secondary-access method expects. For the specific order of strategy operators for the default operator classes for a B-tree index and for an R-tree index, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
Strategy Specification
The STRATEGIES keyword introduces a comma-separated list of function names or function signatures for the new operator class. Each element of this list is called a strategy specification and has the following syntax: Strategy Specification:
strategy_function ( input_type , input_type , output_type )
2-142
CREATE OPCLASS
Element input_type Description Data type of an input parameter to the strategy function for which you intend to use a specific secondary-access method Data type of the optional output parameter of the strategy function Strategy function to associate with the specified operator class Restrictions Syntax
A strategy function accepts two Data Type on input parameter and can have one page 4-18 optional output parameter Optional output parameter for side-effect indexes Must be listed in the order that the specified secondary-access method expects Data Type on page 4-18 Database Database Object Name on page 5-17
output_type strategy_function
Each strategy_function is an external function. The CREATE OPCLASS statement does not verify that a user-defined function of the name you specify exists. However, for the secondary-access method to use the strategy function, the external function must be: v Compiled in a shared library v Registered in the database with the CREATE FUNCTION statement Optionally, you can specify the signature of a strategy function in addition to its name. A strategy function requires two input parameters and an optional output parameter. To specify the function signature, specify: v An input data type for each of the two input parameters of the strategy function, in the order that the strategy function uses them v Optionally, one output data type for an output parameter of the strategy function You can specify UDTs as well as built-in data types. If you do not specify the function signature, the database server assumes that each strategy function takes two arguments of the same data type and returns a BOOLEAN value.
SUPPORT Clause
Support functions are functions that the secondary-access method uses internally to build and search the index. Specify these functions for the secondary-access method in the SUPPORT clause of the CREATE OPCLASS statement. You must list the names of the support functions in the order that the secondary-access method expects. For the specific order of support operators for the default operator classes for a B-tree index and an R-tree index, refer to Default Operator Classes on page 2-144. The support function is an external function. CREATE OPCLASS does not verify that a specified support function exists. For the secondary-access method to use a support function, however, the support function must meet these criteria: v Be compiled in a shared library v Be registered in the database with the CREATE FUNCTION statement
2-143
CREATE OPCLASS
For each of the secondary-access methods that Dynamic Server provides, it provides a default operator class, as follows: v The default B-tree operator class is a built-in operator class. The database server implements the operator-class functions for this operator class and registers it as btree_ops in the system catalog tables of a database. v The default R-tree operator class is a registered operator class. The database server registers this operator class as rtree_ops in the system catalog tables. The database server does not implement the operator-class functions for the default R-tree operator class. Important: To use an R-tree index, you must install a spatial DataBlade module such as the Geodetic DataBlade module or any other third-party DataBlade module that implements the R-tree index. These implement the R-tree operator-class functions. DataBlade modules can provide other types of secondary-access methods. If a DataBlade module provides a secondary-access method, it might also provide a default operator class. For more information, refer to your DataBlade module users guide.
Related Information
Related statements: CREATE FUNCTION, CREATE INDEX, and DROP OPCLASS For information on support functions and how to create and extend an operator class, see IBM Informix User-Defined Routines and Data Types Developer's Guide. For more about R-tree indexes, see the IBM Informix R-Tree Index User's Guide. For information about the GLS aspects of the CREATE OPCLASS statement, refer to the IBM Informix GLS User's Guide.
2-144
CREATE PROCEDURE
Use the CREATE PROCEDURE statement to create a user-defined procedure. (To create a procedure from text of source code that is in a separate file, use the CREATE PROCEDURE FROM statement.) This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE DBA PROCEDURE procedure (1) function (1) (3) ( (2) Routine Parameter List (4) (5) SPECIFIC Specific Name ) Return Clause
(1) (7) Statement Block END PROCEDURE (5) (8) (9) External Routine Reference END PROCEDURE , (10) DOCUMENT Quoted String WITH LISTING IN pathname
Notes: 1 2 3 4 5 6 7 8 9 10 Stored Procedure Language only See Routine Parameter List on page 5-61 See Return Clause on page 5-51 See Specific Name on page 5-68 Dynamic Server only See Routine Modifier on page 5-54 See Statement Block on page 5-69 External routines only See External Routine Reference on page 5-20 See Quoted String on page 4-142
2-145
CREATE PROCEDURE
Element function, procedure pathname Description Restrictions Syntax Database Object Name on page 5-17 Operating system specific
Name declared here for a (XPS) The name must be unique among all SPL new SPL procedure or routines in the database. (IDS) See Procedure function Names in Dynamic Server on page 2-147. File to store compile-time Must exist on the computer where the database warnings resides
Usage
The entire length of a CREATE PROCEDURE statement must be less than 64 kilobytes. This length is the literal length of the CREATE PROCEDURE statement, including blank spaces, tabs, and other whitespace characters. In ESQL/C, you can use CREATE PROCEDURE only as text within a PREPARE statement. If you want to create a procedure for which the text is known at compile time, you must use a CREATE PROCEDURE FROM statement. Routines use the collating order that was in effect when they were created. See SET COLLATION statement of Dynamic Server for information about using non-default collation.
2-146
CREATE PROCEDURE
the term stored procedure. When it is necessary to distinguish between an SPL function and an SPL procedure, this manual does so. The term external routine applies to an external procedure or (for Dynamic Server) an external function, both constructs designating UDRs that are written in a programming language other than SPL. When it is necessary to distinguish between an external function and an external procedure, this manual does so. Extended Parallel Server does not support external routines, but the term user-defined routine (UDR) encompasses both SPL routines and external routines. Wherever the term UDR appears, it is applicable to SPL routines.
2-147
CREATE PROCEDURE
For a brief description of the routine signature that uniquely identifies each UDR, see Routine Overloading and Naming UDRs with a Routine Signature (IDS) on page 5-19. Using the SPECIFIC Clause to Specify a Specific Name: You can declare a specific name that is unique in the database for a user-defined procedure. A specific name is useful when you are overloading a procedure.
DOCUMENT Clause
The quoted string in the DOCUMENT clause provides a synopsis and description of a UDR. The string is stored in the sysprocbody system catalog table and is intended for the user of the UDR. Anyone with access to the database can query the sysprocbody system catalog table to obtain a description of one or all the UDRs stored in the database. A UDR or application program can query the system catalog tables to fetch the DOCUMENT clause and display it for a user. For example, to find the description of the SPL procedure raise_prices, shown in SPL Procedures on page 2-148, enter a query such as this example:
SELECT data FROM sysprocbody b, sysprocedures p WHERE b.procid = p.procid --join between the two catalog tables AND p.procname = raise_prices -- look for procedure named raise_prices AND b.datakey = D;-- want user document
For external procedures, you can use a DOCUMENT clause at the end of the CREATE PROCEDURE statement, whether or not you use the END PROCEDURE keywords.
SPL Procedures
SPL procedures are UDRs written in Stored Procedure Language (SPL) that do not return a value. To write and register an SPL routine, use the CREATE PROCEDURE statement. Embed appropriate SQL and SPL statements between the
2-148
CREATE PROCEDURE
CREATE PROCEDURE and END PROCEDURE keywords. You can also follow the UDR definition with the DOCUMENT and WITH FILE IN options. SPL routines are parsed, optimized (as far as possible), and stored in the system catalog tables in executable format. The body of an SPL routine is stored in the sysprocbody system catalog table. Other information about the routine is stored in other system catalog tables, including sysprocedures, sysprocplan, and sysprocauth. If the Statement Block portion of the CREATE PROCEDURE statement is empty, no operation takes place when you call the procedure. You might use such a dummy procedure in the development stage when you intend to establish the existence of a procedure but have not yet coded it. If you specify an optional clause after the parameter list, you must place a semicolon after the clause that immediately precedes the Statement Block. The following example creates an SPL procedure:
CREATE PROCEDURE raise_prices ( per_cent INT ) UPDATE stock SET unit_price = unit_price + (unit_price * (per_cent/100) ); END PROCEDURE DOCUMENT "USAGE: EXECUTE PROCEDURE raise_prices( xxx )", "xxx = percentage from 1 - 100 " WITH LISTING IN /tmp/warn_file
2-149
CREATE PROCEDURE
When the IFX_EXTEND_ROLE configuration parameter is set to ON, only users who have the built-in EXTEND role can create external procedures.
This example registers a user-defined procedure named showusers( ) that is written in the Java language:
CREATE PROCEDURE showusers() WITH (CLASS = "jvp") EXTERNAL NAME admin_jar:admin.showusers LANGUAGE JAVA
The EXTERNAL NAME clause specifies that the Java implementation of the showusers( ) procedure is a method called showusers( ), which resides in the admin Java class that resides in the admin_jar jar file.
2-150
CREATE PROCEDURE
Important: The sysdbopen( ) and sysdbclose( ) procedures are exceptions to the scope rule for stored procedures. In ordinary UDR procedures, the scope of variables and statements is local. SET PDQPRIORITY and SET ENVIRONMENT statement settings do not persist when these SPL procedures exit. In sysdbopen( ) and sysdbclose( ) procedures, however, statements that set the session environment remain in effect until another statement resets the options. For example, you might create the following procedure, which sets the isolation level to Dirty Read and turns on the IMPLICIT_PDQ environment variable, to be executed when any user connect to the database:
create procedure public.sysdbopen() set role engineer; end procedure;
Procedures do not accept arguments or return values. The sysdbopen( ) and sysdbclose( ) procedures must be executed from the connection coserver and must be installed in each database where you want to execute them. You can create the following four SPL procedures. Procedure Name user.sysdbopen( ) public.sysdbopen( ) Description This procedure is executed when the specified user opens the database as the current database. If no user.sysdbopen( ) procedure applies, this procedure is executed when any user opens the database as the current database. To avoid duplicating SPL code, you can call this procedure from a user-specific procedure. This procedure is executed when the specified user closes the database, disconnects from the database server, or the user session ends. If the sysdbclose( ) procedure did not exist when a session opened the database, however, it is not executed when the session closes the database. If no user.sysdbopen( ) procedure applies, this procedure is executed when the specified user closes the database, disconnects from the database server, or the user session ends. If the sysdbclose( ) procedure did not exist when a session opened the database, however, it is not executed when the session closes the database.
user.sysdbclose( )
public.sysdbclose( )
See also the section Transactions in SPL Routines on page 5-72. Make sure that you set permissions appropriately to allow intended users to execute the SPL procedure statements. For example, if the SPL procedure executes a command that writes output to a local directory, permissions must be set to allow users to write to this directory. If you want the procedure to continue if permission failures occur, include an ON EXCEPTION error handler for this condition. See also the section Support for Roles and User Identity on page 5-72. Warning: If a sysdbopen( ) procedure fails, the database cannot be opened. If a sysdbclose( ) procedure fails, the failure is ignored. While you are
Chapter 2. SQL Statements
2-151
CREATE PROCEDURE
writing and debugging a sysdbopen( ) procedure, set the DEBUG environment variable to NODBPROC before you connect to the database. When DEBUG is set to NODBPROC, the procedure is not executed, and failures cannot prevent the database from opening. Failures from these procedures can be generated by the system or simulated by the procedures with the RAISE EXCEPTION statement of SPL. For more information, refer to the description of RAISE EXCEPTION in Chapter 3. Only a user with DBA privileges can install these procedures. For security reasons, non-DBAs cannot prevent execution of these procedures. For some applications, however, such as ad hoc query applications, users can execute commands and SQL statements that subsequently change the environment. For general information about how to write and install SPL procedures, refer to the chapter about SPL routines in IBM Informix Guide to SQL: Tutorial.
Related Information
Related statements: ALTER FUNCTION, ALTER PROCEDURE, ALTER ROUTINE, CREATE FUNCTION, CREATE FUNCTION FROM, CREATE PROCEDURE FROM, DROP FUNCTION, DROP PROCEDURE, DROP ROUTINE, EXECUTE FUNCTION, EXECUTE PROCEDURE, GRANT, PREPARE, REVOKE, and UPDATE STATISTICS. For a discussion of how to create and use SPL routines, see the IBM Informix Guide to SQL: Tutorial. For a discussion of external routines, see IBM Informix User-Defined Routines and Data Types Developer's Guide. For information about how to create C UDRs, see the IBM Informix DataBlade API Programmer's Guide. For more information on the NODEFDAC environment variable and the related system catalog tables (sysprocedures, sysprocplan, sysprocbody and sysprocauth), see the IBM Informix Guide to SQL: Reference.
2-152
Syntax
CREATE PROCEDURE FROM file file_var
Element file
Description Pathname and filename of file that contains full text of a CREATE PROCEDURE statement. Default pathname is the current directory. Name of a program variable that contains file specification
Restrictions Must exist, and can contain only one CREATE PROCEDURE statement. See also Default Directory That Holds the File on page 2-154. Must be of a character data type; its contents have same restrictions as file
file_var
Language specific
Usage
You cannot create a user-defined procedure directly in an ESQL/C program. That is, the program cannot contain the CREATE PROCEDURE statement. To use a user-defined procedure in an ESQL/C program: 1. Create a source file with the CREATE PROCEDURE statement. 2. Use the CREATE PROCEDURE FROM statement to send the contents of this source file to the database server for execution. The file can contain only one CREATE PROCEDURE statement. For example, suppose that the following CREATE PROCEDURE statement is in a separate file, called raise_pr.sql:
CREATE PROCEDURE raise_prices( UPDATE stock -- increase by SET unit_price = unit_price ( unit_price * (per_cent END PROCEDURE; per_cent int ) percentage; + / 100) );
In the ESQL/C program, you can access the raise_prices( ) SPL procedure with the following CREATE PROCEDURE FROM statement:
EXEC SQL create procedure from raise_pr.sql;
In Dynamic Server, if you are not sure whether the UDR in the file returns a value, use the CREATE ROUTINE FROM statement. When the IFX_EXTEND_ROLE configuration parameter is set to ON, only users who have the built-in EXTEND role can create external routines.
Chapter 2. SQL Statements
2-153
Related Information
Related statements: CREATE PROCEDURE, CREATE FUNCTION FROM, and CREATE ROUTINE FROM
2-154
CREATE ROLE
Use the CREATE ROLE statement to declare and register a new role. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE ROLE role role
Element role
Description Name declared here for a role that the DBA creates
Restrictions Must be unique among role and user names in the database. Maximum number of bytes is 32.
Usage
CREATE ROLE declares a new role and registers it in the system catalog. A role can associate a set of authorization identifiers with a set of access privileges on database objects. The system catalog maintains information about the roles (and their corresponding privileges) that are granted to users or to other roles. Only the database administrator (DBA) can use CREATE ROLE to create a new role. The DBA can assign the privileges required for some work task to a role, such as engineer, and then use the GRANT statement to assign that role to specific users, instead of granting that set of privileges to each user individually. The role name is an authorization identifier. It cannot be a user name that is known to the database server or to the operating system of the database server. The role name cannot already be listed in the username column of the sysusers system catalog table, nor in the grantor or grantee columns of the systabauth, syscolauth, sysfragauth, sysprocauth, or sysroleauth system catalog tables. In Dynamic Server, the role name also cannot match the name of any user or role that is already listed in the grantor or grantee columns of the sysxtdtypeauth system catalog table, nor any built-in role, such as EXTEND or NONE. After a role is created, the DBA can use the GRANT statement to assign the role to PUBLIC, to users, or to other roles, and to grant specific privileges to the role. (A role cannot, however, hold database-level privileges.) After a role is granted successfully to a user or to PUBLIC, the user must use the SET ROLE statement to enable the role. Only then can the user exercise the privileges of the role. To create the role engineer, for example, enter the following statement:
CREATE ROLE engineer
To grant access privileges to the role engineer, the DBA can issue GRANT statements that include engineer in the list of grantees:
GRANT USAGE ON LANGUAGE SPL TO engineer
To assign the role engineer to user kaycee, the DBA could issue this statement:
GRANT ROLE engineer TO kaycee
To activate the role engineer, user kaycee must issue the following statement:
Chapter 2. SQL Statements
2-155
CREATE ROLE
SET ROLE engineer
If this SET ROLE statement is successful, user kaycee acquires whatever privileges have been granted to the role engineer, in addition to any other privileges that kaycee already holds as an individual or as PUBLIC. A user can be granted several roles, but no more than one non-default role, as specified by SET ROLE, can be enabled for any user at a given time. An exception to requiring SET ROLE to explicitly enable a role is any default role that the DBA specifies in the GRANT DEFAULT ROLE role TO user statement. If that statement succeeds, the default role is automatically enabled when user connects to the database. Any role can be a default role. (Similarly, users to whom the Dynamic Server DBSA grants the EXTEND role need not execute SET ROLE before they can create and drop external routines and shared libraries.) CREATE ROLE, when used with the GRANT and SET ROLE statements, enables a DBA to create one set of privileges for a role and then grant the role to many users, instead of granting the same set of privileges individually to many users. With the GRANT DEFAULT ROLE and SET ROLE DEFAULT statements, default roles enable a DBA to assign privileges to a role that is activated automatically when any user who holds that default role connects to the database. This feature is useful when an application performs operations that require specific access privileges, but the application does not include SET ROLE statements. The REVOKE statement can cancel access privileges of a role, remove users from a role, or cancel the default status of a role for one or more users. A role exists until either the DBA or a user to whom the role was granted with the WITH GRANT OPTION keywords uses the DROP ROLE statement to drop the role.
Related Information
Related statements: DROP ROLE, GRANT, REVOKE, and SET ROLE For a discussion of how to use roles, see the IBM Informix Database Design and Implementation Guide.
2-156
Syntax
CREATE ROUTINE FROM file file_var
Element file
Description Pathname and filename for the text of a CREATE PROCEDURE or CREATE FUNCTION statement. Default path is the current directory. Name of a program variable that contains file specification
Restrictions Must exist and can contain only one CREATE FUNCTION or CREATE PROCEDURE statement. Must be a character data type; contents must satisfy file restrictions
file_var
Usage
ESQL/C programs cannot use the CREATE FUNCTION or CREATE PROCEDURE statement directly to define a UDR. You must instead do this: 1. Create a source file with the CREATE FUNCTION or CREATE PROCEDURE statement. 2. Execute the CREATE ROUTINE FROM statement from an ESQL/C program to send the contents of this source file to the database server for execution. The file that you specify can contain only one CREATE FUNCTION or CREATE PROCEDURE statement. The file specification that you provide is relative. If you include no pathname, the client application looks for the file in the current directory. If you do not know at compile time whether the UDR in the file is a function or a procedure, use the CREATE ROUTINE FROM statement in the ESQL/C program. If you know whether the UDR is a function or a procedure, you can improve the readability of your code by using the matching SQL statement to access the source file: v To access user-defined functions, use CREATE FUNCTION FROM. v To access user-defined procedures, use CREATE PROCEDURE FROM. When the IFX_EXTEND_ROLE configuration parameter is set to ON, only users to whom the Database System Administrator (DBSA) has granted the built-in EXTEND role can create external routines. Routines use the collating order that was in effect when they were created. See SET COLLATION for information about using non-default collation.
Related Information
Related statements: CREATE FUNCTION, CREATE FUNCTION FROM, CREATE PROCEDURE, and CREATE PROCEDURE FROM
Chapter 2. SQL Statements
2-157
Syntax
CREATE ROW TYPE row_type
UNDER supertype
Notes: 1
Element row_type supertype Description Name that you declare here for a new named ROW data type Name of the supertype within a data type inheritance hierarchy
Usage
The CREATE ROW TYPE statement declares a named ROW data type and registers it in the system catalog. You can assign a named ROW data type to a table or view to create a typed table or typed view. You can also define a column as a named ROW type. Although you can assign a ROW type to a table to define the schema of the table, ROW data types are not the same as table rows. Table rows consist of one or more columns; ROW data types consist of one or more fields, defined using the Field Definition syntax. A named ROW data type is valid in most contexts where you can specify a data type. Named ROW types are said to be strongly typed. No two named ROW types are equivalent, even if they are structurally equivalent. ROW types without identifiers are called unnamed ROW types. Any two unnamed ROW types are considered equivalent if they are structurally equivalent. For more information, see ROW Data Types on page 4-29. Privileges on named ROW type columns are the same as privileges on any column. For more information, see Table-Level Privileges on page 2-374. (To see what privileges you have on a column, check the syscolauth system catalog table, which is described in the IBM Informix Guide to SQL: Reference.)
2-158
For information about Resource and Under privileges and the ALL keyword in the context of privileges, see the GRANT statement. To find out what privileges exist on a ROW type, check the sysxtdtypes system catalog table for the owner name and the sysxtdtypeauth system catalog table for privileges on the ROW type that might have been granted to users or to roles. To find out what privileges you have on a given table, check the systabauth system catalog table. For more information on system catalog tables, see the IBM Informix Guide to SQL: Reference.
Creating a Subtype
In most cases, you add new fields when you create a named ROW type as a subtype of another named ROW type (its supertype). To create the fields of a named ROW type, use the field definition clause, as described in Field Definition on page 2-160. When you create a subtype, you must use the UNDER keyword to associate the supertype with the named ROW type that you want to create. The next example creates the employee_t type under the person_t type:
CREATE ROW TYPE employee_t (salary NUMERIC(10,2), bonus NUMERIC(10,2)) UNDER person_t;
The employee_t type inherits all the fields of person_t and has two additional fields: salary and bonus; but the person_t type is not altered.
Type Hierarchies
When you create a subtype, you create a type hierarchy. In a type hierarchy, each subtype that you create inherits its properties from a single supertype. If you create a named ROW type customer_t under person_t, customer_t inherits all the fields
2-159
Field Definition
Use the Field Definition clause to define a new field in a named ROW type. Field Definition:
field data_type NOT NULL
Restrictions See Restrictions on Serial and Simple-Large-Object Data Types on page 2-161. Must be unique among field names of this ROW type and of its supertype
The NOT NULL constraint on the named ROW type field applies to the corresponding columns when a typed table of the named ROW type is created.
2-160
You cannot create a ROW type that has a BYTE or TEXT value that is stored in a separate storage space. That is, you cannot use the IN clause to specify the storage location. For example, the following example produces an error:
CREATE ROW TYPE row1 (field1 byte IN blobspace1) --INVALID CODE
A table hierarchy can include no more than one SERIAL and no more than one SERIAL8 column. If a supertable has a SERIAL column, none of its subtables can contain a SERIAL column (but a subtable can have a SERIAL8 column if no other subtable contains a SERIAL8 column). Consequently, when you create the named ROW types on which the table hierarchy is to be based, they can contain at most one SERIAL field and one SERIAL8 field among them. You cannot set the starting SERIAL or SERIAL8 value in the CREATE ROW TYPE statement. To modify the value for a serial field, you must use either the MODIFY clause of the ALTER TABLE statement, or else use the INSERT statement to insert a value that is larger than the current maximum (or default) serial value. Serial fields in ROW types have performance implications across a table hierarchy. To insert data into a subtable whose supertable (or its supertable) contains the serial counter, the database server must also open the supertable, update the serial value, and close the supertable, thus adding extra overhead. 3 3 3 In contexts where these restrictions or performance issues for SERIAL and SERIAL8 data types conflict with your design goals, you might consider using sequence objects to emulate the functionality of serial fields or serial columns.
Related Information
Related statements: DROP ROW TYPE, CREATE TABLE, CREATE CAST, GRANT, and REVOKE For a discussion of named ROW types, see the IBM Informix Database Design and Implementation Guide and the IBM Informix Guide to SQL: Reference.
2-161
CREATE SCHEMA
Use the CREATE SCHEMA statement to issue a block of data definition language (DDL) and GRANT statements as a unit. Use this statement with DBAccess.
Syntax
CREATE SCHEMA AUTHORIZATION user
(1) CREATE TABLE Statement (2) CREATE VIEW Statement (3) GRANT Statement (4) CREATE INDEX Statement CREATE SYNONYM Statement (7) CREATE TRIGGER Statement (8) (9) CREATE OPTICAL CLUSTER Statement (10) CREATE SEQUENCE Statement (11) CREATE ROW TYPE Statement (12) CREATE OPAQUE TYPE Statement (13) CREATE DISTINCT TYPE Statement (14) CREATE CAST Statement ;
(5) (6)
Notes: 1 2 3 4 5 6 7 8 9 10 11 12 13 See CREATE TABLE on page 2-171 See CREATE VIEW on page 2-247 See GRANT on page 2-371 Informix extension See CREATE INDEX on page 2-116 See CREATE SYNONYM on page 2-168 See CREATE TRIGGER on page 2-216 Dynamic Server only Optical Subsystem only. See the IBM Informix: Optical Subsystem Guide. See CREATE SEQUENCE on page 2-165 See CREATE ROW TYPE on page 2-158 See CREATE OPAQUE TYPE on page 2-136 See CREATE DISTINCT TYPE on page 2-93
2-162
CREATE SCHEMA
14 See CREATE CAST on page 2-87
Description User who owns the database objects that this statement creates Restrictions Syntax
Element user
If you have DBA privileges, you can specify the name of any Owner user. Otherwise, you must have the Resource privilege, and Name on you must specify your own user name. page 5-43
Usage
The CREATE SCHEMA statement allows the DBA to specify an owner for all database objects that the CREATE SCHEMA statement creates. You cannot issue CREATE SCHEMA until you have created the database that stores the objects. Users with the Resource privilege can create a schema for themselves. In this case, user must be the name of the person with the Resource privilege who is running the CREATE SCHEMA statement. Anyone with the DBA privilege can also create a schema for someone else. In this case, user can specify a user other than the person who is running the CREATE SCHEMA statement. You can put CREATE and GRANT statements in any logical order, as the following example shows. Statements are considered part of the CREATE SCHEMA statement until a semicolon ( ; ) or an end-of-file symbol is reached.
CREATE SCHEMA AUTHORIZATION sarah CREATE TABLE mytable (mytime DATE, mytext TEXT) GRANT SELECT, UPDATE, DELETE ON mytable TO rick CREATE VIEW myview AS SELECT * FROM mytable WHERE mytime > 12/31/2004 CREATE INDEX idxtime ON mytable (mytime);
Related Information
Related statements: CREATE CAST, CREATE DISTINCT TYPE, CREATE INDEX, CREATE OPAQUE TYPE, CREATE OPCLASS, CREATE ROW TYPE, CREATE SEQUENCE, CREATE SYNONYM, CREATE TABLE, CREATE VIEW, and GRANT For a discussion of how to create a database, see the IBM Informix Database Design and Implementation Guide.
2-163
Syntax
CREATE SCRATCH TABLE table Scratch Table Definition
IN
LOCK MODE
Notes: 1 2 3 See Column Definition on page 2-173 See Multiple-Column Constraint Format on page 2-211 See FRAGMENT BY Clause on page 2-190
Description Name of dbslice to store table Name of dbspace to store table. Default is the dbspace that stores the current database. Name that you declare here for a nonlogging temporary table Restrictions Must already exist Must already exist Must be unique in the current session Syntax Identifier on page 5-22 Identifier on page 5-22 Database Object Name on page 5-17
Usage
The CREATE SCRATCH TABLE statement is a special case of the CREATE Temporary TABLE statement. For the complete syntax and semantics of the CREATE SCRATCH TABLE statement, see CREATE Temporary TABLE on page 2-209.
2-164
CREATE SEQUENCE
Use the CREATE SEQUENCE statement to create a sequence database object from which multiple users can generate unique integers. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE SEQUENCE owner . sequence
BY INCREMENT step WITH START origin NOMAXVALUE MAXVALUE max NOMINVALUE MINVALUE min NOCYCLE CYCLE CACHE size NOCACHE ORDER NOORDER
(1)
Notes: 1
Element max min origin owner sequence size step Description Upper limit of values Lower limit of values First number in the sequence Owner of sequence Name that you declare here for the new sequence Number of values that are preallocated in memory
Usage
A sequence (sometimes called a sequence generator or sequence object) returns a monotonically ascending or descending series of unique integers, one at a time. The CREATE SEQUENCE statement defines a new sequence object, declares its identifier, and registers it in the syssequences system catalog table.
2-165
CREATE SEQUENCE
Authorized users of a sequence can request a new value by including the sequence.NEXTVAL expression in DML statements. The sequence.CURRVAL expression returns the current value of the specified sequence. NEXTVAL and CURRVAL expressions are valid only within SELECT, DELETE, INSERT, and UPDATE statements; Dynamic Server returns an error if you attempt to invoke the built-in NEXTVAL or CURRVAL functions in any other context. Generated values logically resemble the SERIAL8 data type, but can be negative, and are unique within the sequence. Because the database server generates the values, sequences support a much higher level of concurrency than a serial column can. The values are independent of transactions; a generated value cannot be rolled back, even if the transaction in which it was generated fails. You can use a sequence to generate primary key values automatically, using one sequence for many tables, or each table can have its own sequence. CREATE SEQUENCE can specify the following characteristics of a sequence: v Initial value v Size and sign of the increment between values. v Maximum and minimum values v Whether the sequence recycles values after reaching its limit v How many values are preallocated in memory for rapid access A database can support multiple sequences concurrently, but the name of a sequence (or in an ANSI-compliant database, the owner.sequence combination) must be unique within the current database among the names of tables, temporary tables, views, synonyms, and sequences. An error occurs if you include contradictory options, such as specifying both the MINVALUE and NOMINVALUE options, or both CACHE and NOCACHE.
INCREMENT BY Option
Use the INCREMENT BY option to specify the interval between successive numbers in the sequence. The BY keyword is optional. The interval, or step value, can be a positive whole number (for an ascending sequence) or a negative whole number (for a descending sequence) in the INT8 range. If you do not specify any step value, the default interval between successive generated values is 1, and the sequence is an ascending sequence.
2-166
CREATE SEQUENCE
If you do not specify a max value, the default is NOMAXVALUE. This default setting supports values that are less than or equal to 2e64 for ascending sequences, or less than or equal to -1 for descending sequences.
Related Information
Related statements: ALTER SEQUENCE, DROP SEQUENCE, RENAME SEQUENCE, CREATE SYNONYM, DROP SYNONYM, GRANT, and REVOKE For information about the syssequences system catalog table in which sequence objects are registered, see the IBM Informix Guide to SQL: Reference. For information about initializing a sequence and generating or reading values from a sequence, see NEXTVAL and CURRVAL Operators (IDS) on page 4-61.
2-167
CREATE SYNONYM
Use the CREATE SYNONYM statement to declare and register an alternative name for an existing table, view, or sequence object. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
PUBLIC CREATE PRIVATE SYNONYM synonym FOR table view (1) sequence
Notes: 1
Element sequence table, view synonym Description Name of a local sequence Name of table or view for which synonym is being created Synonym declared here for the name of a table, view, or sequence
Usage
Users have the same privileges for a synonym that they have for the database object that the synonym references. The syssynonyms, syssyntable, and systables system catalog tables maintain information about synonyms. You cannot create a synonym for a synonym in the same database. The identifier of the synonym must be unique among the names of tables, temporary tables, views, and sequence objects in the same database. (See, however, the section Synonyms with the Same Name on page 2-169.) Once a synonym is created, it persists until the owner executes the DROP SYNONYM statement. (This persistence distinguishes a synonym from an alias that you can declare in the FROM clause of a SELECT statement; the alias is in scope only during execution of that SELECT statement.) If a synonym refers to a table, view, or sequence in the same database, the synonym is automatically dropped if the referenced table, view, or sequence object is dropped.
You can also create a synonym for a table or view that exists in a database of a database server that is not your current database server. Both database servers must be online when you create the synonym. In a network, the remote database
2-168
CREATE SYNONYM
server verifies that the table or view referenced by the synonym exists when you create the synonym. The next example creates a synonym for a table supported by a remote database server:
CREATE SYNONYM mysum FOR payables@phoenix:jean.summary
The identifier mysum now refers to the table jean.summary, which is in the payables database on the phoenix database server. If the summary table is dropped from the payables database, the mysum synonym is left intact. Subsequent attempts to use mysum return the error: Table not found. Dynamic Server, however, does not support synonyms for these external objects: v Typed tables (including any table that is part of a table hierarchy) v Tables or views that contain any extended data types v Sequence objects outside the local database
In a database that is not ANSI-compliant, no two public synonyms can have the same identifier, and the identifier of a synonym must also be unique among the names of tables, views, and sequences in the same database. The owner.synonym combination of a private synonym must be unique among all the synonyms in the database. That is, more than one private synonym with the same name can exist in the same database, but a different user must own each of these synonyms. The same user cannot create both a private and a public synonym that have the same name. For example, the following code generates an error:
CREATE SYNONYM our_custs FOR customer; CREATE PRIVATE SYNONYM our_custs FOR cust_calls;-- ERROR!!!
A private synonym can be declared with the same name as a public synonym only if the two synonyms have different owners. If you own a private synonym, and a public synonym exists with the same name, the database server resolves the unqualified name as the private synonym. (In this case, you must specify owner.synonym to reference the public synonym.) If you use DROP SYNONYM with the unqualified synonym identifier when your private synonym and the public synonym of another user both have the same identifier, only your private
2-169
CREATE SYNONYM
synonym is dropped. If you repeat the same DROP SYNONYM statement, the database server drops the public synonym.
Chaining Synonyms
If you create a synonym for a table or view that is not in the current database, and this table or view is dropped, the synonym stays in place. You can create a new synonym for the dropped table or view with the name of the dropped table or view as the synonym, which points to another external or remote table or view. (Synonyms for external sequence objects are not supported.) In this way, you can move a table or view to a new location and chain synonyms together so that the original synonyms remain valid. (You can chain up to 16 synonyms in this manner.) The following steps chain two synonyms together for the customer table, which will ultimately reside on the zoo database server (the CREATE TABLE statements are not complete): 1. In the stores_demo database on the database server that is called training, issue the following statement:
CREATE TABLE customer (lname CHAR(15)...)
The synonym cust on the accntg database server now points to the customer table on the zoo database server. The following steps show an example of chaining two synonyms together and changing the table to which a synonym points: 1. On the database server called training, issue the following statement:
CREATE TABLE customer (lname CHAR(15)...)
The synonym cust on the accntg database server now points to a new version of the customer table on the training database server.
Related Information
Related statement: DROP SYNONYM. For a discussion of concepts related to synonyms, see the IBM Informix Database Design and Implementation Guide.
2-170
CREATE TABLE
Use the CREATE TABLE statement to create a new permanent table in the current database, to place data-integrity constraints on columns, to designate where the table is stored, to define a fragmentation strategy, to indicate the size of its initial and subsequent extents, and to specify how to lock the new table. You can use the CREATE TABLE statement to create relational-database tables or to create typed tables (object-relational tables). For information on how to create temporary tables, see CREATE Temporary TABLE on page 2-209.
Syntax
CREATE STANDARD RAW (1) STATIC (1) OPERATIONAL TABLE table Table Definition
Table Definition:
, (2) ( Column Definition , (3) , Multiple-Column Constraint (2) Column Definition ) Options (4)
(5)
(7)
Notes: 1 2 3 4 5 6 7 Extended Parallel Server only See page 2-173 See page 2-184 See page 2-187 Informix extension Dynamic Server only See page 2-204
Description Name that you declare here for the new table Restrictions Must be unique among the names of tables, synonyms, views, and sequences in the database Syntax Identifier, p. 5-22
Element table
Usage
The CREATE TABLE statement can include various clauses, some of which are identified in the following list. Clauses whose names are followed by (IDS) are not supported by Extended Parallel Server. Similarly, clauses whose names are followed by (XPS) are not supported by Dynamic Server.
2-171
CREATE TABLE
Clause Logging Options Column Definition DEFAULT Single-Column Constraint REFERENCES CHECK Constraint Definition (IDS) Multiple-Column Constraint WITH CRDCOLS (IDS) Storage Options IN FRAGMENT BY or PARTITION BY WITH ROWIDS (IDS) RANGE METHOD (XPS) PUT (IDS) EXTENT SIZE USING ACCESS METHOD (IDS) LOCK MODE OF TYPE (IDS) UNDER (IDS) Page 2-172 2-173 2-174 2-176 2-179 2-181 2-182 2-184 2-188 2-188 2-189 2-190 2-191 2-195 2-199 2-201 2-202 2-203 2-204 2-205 What the Clause Specifies Logging characteristics of the new table Name and other attributes of an individual column Default value for an individual column Data-integrity constraints on an individual column Referential-integrity constraints with other columns Check constraints with other columns Name and other attributes of a constraint Data-integrity constraints on a set of columns Two shadow columns for Enterprise Replication Attributes of where the table is physically stored Storage object to hold the new table (or part of it) Distribution scheme of a fragmented table Hidden column in a fragmented table Fragmentation strategy based on column values Storage location for BLOB or CLOB column values Size of the first and subsequent extents How to access the new table Locking granularity of the new table Named ROW type of a new typed table Supertable of a new subtable in a table hierarchy
When you create a new table, every column must have a data type associated with it. The table name must be unique among all the names of tables, views, sequences, and synonyms within the same database, but the names of columns need only be unique among the column names of the same table. In an ANSI-compliant database, the combination owner.table must be unique among tables, synonyms, views, and sequence objects within the database. In DBAccess, using CREATE TABLE outside the CREATE SCHEMA statement generates warnings if you use the -ansi flag or if you set DBANSIWARN. In ESQL/C, using CREATE TABLE generates warnings if you compile with the -ansi flag or set DBANSIWARN. For information about the DBANSIWARN environment variable, see the IBM Informix Guide to SQL: Reference.
Logging Options
Use the Logging Type options to specify logging characteristics that can improve performance in various bulk operations on the table. Other than the default option (STANDARD) that is used for OLTP databases, these logging options are used primarily to improve performance in data warehousing databases. A table can have either of the following logging characteristics. Logging Type STANDARD Effect Logging table that allows rollback, recovery, and restoration from archives. This type is the default.
2-172
CREATE TABLE
Use this type of table for all the recovery and constraints functionality that OLTP databases require. RAW Nonlogging table that cannot have indexes or referential constraints but can be updated. Use this type of table for quickly loading data.
By using raw tables with Extended Parallel Server, you can take advantage of light appends and avoid the overhead of logging, checking constraints, and building indexes. Warning: Use raw tables for fast loading of data, but set the logging type to STANDARD and perform a level-0 backup before you use the table in a transaction or modify the data within the table. If you must use a raw table within a transaction, either set the isolation level to Repeatable Read or lock the table in exclusive mode to prevent concurrency problems. Extended Parallel Server supports two additional logging type options. Option OPERATIONAL Effect Logging table that uses light appends; it cannot be restored from archive. Use this type on tables that are refreshed frequently, because light appends allow the quick addition of many rows. Nonlogging table that can contain index and referential constraints but cannot be updated. Use this type for read-only operations, because no logging or locking overhead occurs.
STATIC
For more information on these logging types of tables, refer to your IBM Informix Administrator's Guide.
Column Definition
Use the column definition portion of CREATE TABLE to list the name, data type, default values, and constraints of a single column. Column Definition:
(1) column Data Type (2) DEFAULT Clause
Notes: 1 2 3 See page 4-18 See page 2-174 See page 2-176
Chapter 2. SQL Statements
2-173
CREATE TABLE
Element column Description Name of a column in the table Restrictions Must be unique in this table Syntax Identifier, p. 5-22
Because the maximum row size is 32,767 bytes, no more than approximately 195 columns in the table can be of the data types BYTE, TEXT, ROW, LVARCHAR, NVARCHAR, VARCHAR, and varying-length UDTs. (The upper limit on columns of these data types also depends on other data describing the table that the database server stores in the same partition.) No more than approximately 97 columns can be of COLLECTION data types (SET, LIST, and MULTISET). As with any SQL identifier, syntactic ambiguities (and sometimes error messages or unexpected behavior) can occur if the column name is a keyword, or if it is the same as the table name, or the name of another table that you subsequently join with the table). For information about the keywords of Dynamic Server, see Appendix A, Reserved Words for IBM Informix Dynamic Server, on page A-1. For more information on the keywords of Extended Parallel Server, see Appendix B, Reserved Words for IBM Informix Extended Parallel Server, on page B-1. For more information on the ambiguities that can occur, see Use of Keywords as Identifiers on page 5-23. In Dynamic Server, if you define a column of a table to be of a named ROW type, the table does not adopt any constraints of the named ROW.
DEFAULT Clause
Use the DEFAULT clause to specify the default value for the database server to insert into a column when no explicit value for the column is specified. DEFAULT Clause:
DEFAULT NULL literal USER (1) CURRENT (2) DATETIME Field Qualifier TODAY SITENAME DBSERVERNAME
Notes: 1 2
Element literal Description String of alphabetic or numeric characters
2-174
CREATE TABLE
DATE literals must be of the format that the DBDATE (or else GL_DATE) environment variable specifies. In the default locale, if neither DBDATE nor GL_DATE is set, date literals must be of the mm/dd/yyyy format.
2-175
CREATE TABLE
These column sizes are recommended because, if the column length is too small to store the default value during INSERT or ALTER TABLE operations, the database server returns an error. In Dynamic Server, you cannot designate a built-in function (that is, CURRENT, USER, TODAY, SITENAME, or DBSERVERNAME) as the default value for a column that holds an opaque or distinct data type. In addition, larger column sizes are required if the data values are encrypted, or if they are encoded in the Unicode character set of the UTF-8 locale. (See the description of the SET ENCRYPTION statement later in this chapter for more information about the storage size requirements for encrypted data.) For descriptions of these functions, see Constant Expressions on page 4-53. The following example creates a table called accounts. In accounts, the acc_num,acc_type, and acc_descr columns have literal default values. The acc_id column defaults to the login name of the user.
CREATE TABLE accounts ( acc_num INTEGER DEFAULT 1, acc_type CHAR(1) DEFAULT A, acc_descr CHAR(20) DEFAULT New Account, acc_id CHAR(32) DEFAULT USER)
(1) DISTINCT UNIQUE PRIMARY KEY (3) REFERENCES Clause (4) CHECK Clause (1) Constraint Definition (2)
2-176
CREATE TABLE
4 See page 2-181 The following example creates a standard table with two constraints: num, a primary-key constraint on the acc_num column; and code, a unique constraint on the acc_code column:
CREATE TABLE acc_num acc_code acc_descr accounts ( INTEGER PRIMARY KEY CONSTRAINT num, INTEGER UNIQUE CONSTRAINT code, CHAR(30))
The types of constraints used in this example are defined in sections that follow.
You cannot specify NULL as the explicit default value for a column if you also specify the NOT NULL constraint.
2-177
CREATE TABLE
Opaque data types support a unique constraint only where a secondary-access method supports uniqueness for that type. The default secondary-access method is a generic B-tree, which supports the equal( ) operator function. Therefore, if the definition of the opaque type includes the equal( ) function, a column of that opaque type can have a unique constraint. The following example creates a simple table that has a unique constraint on one of its columns:
CREATE TABLE accounts (acc_name CHAR(12), acc_num SERIAL UNIQUE CONSTRAINT acc_num)
For an explanation of the constraint name, refer to Declaring a Constraint Name on page 2-182.
2-178
CREATE TABLE
CREATE TABLE accounts (acc_name CHAR(12), acc_num SERIAL PRIMARY KEY CONSTRAINT acc_num)
REFERENCES Clause
Use the REFERENCES clause to establish a referential relationship: v Within a table (that is, between two columns of the same table) v Between two tables (in other words, create a foreign key) REFERENCES Clause:
REFERENCES table , ( column ) (1) ON DELETE CASCADE
Notes: 1
Element column table Description A referenced column The referenced table
Informix extension
Restrictions See Restrictions on Referential Constraints on page 2-179. Must reside in the same database as the referencing table Syntax Identifier, p. 5-22 Database Object Name, p. 5-17
The referencing column (the column being defined) is the column or set of columns that refers to the referenced column or set of columns. The referencing column can contain NULL and duplicate values, but values in the referenced column (or set of columns) must be unique. The relationship between referenced and referencing columns is called a parent-child relationship, where the parent is the referenced column (primary key) and the child is the referencing column (foreign key). The referential constraint establishes this parent-child relationship. When you create a referential constraint, the database server automatically creates an internal index on the constrained column or columns.
2-179
CREATE TABLE
v When you use the single-column constraint format, you can reference only one column. v In Dynamic Server, when you use the multiple-column constraint format, the maximum number of columns in the REFERENCES clause is 16, and the total length of the columns cannot exceed 390 bytes if the page size is 2 kilobytes. (The maximum length increases with the page size.) v In Extended Parallel Server, when you use the multiple-column constraint format, the maximum number of columns in the REFERENCES clause is 16, and the total length of the columns cannot exceed 380 bytes.
When you use the single-column constraint format, you do not explicitly specify the ref_num column as a foreign key. To use the FOREIGN KEY keyword, use the Multiple-Column Constraint Format on page 2-184.
2-180
CREATE TABLE
not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it. If you specify this option, later when you delete a row in the parent table, the database server also deletes any rows associated with that row (foreign keys) in a child table. The principal advantage to the cascading-deletes feature is that it allows you to reduce the quantity of SQL statements you need to perform delete actions. For example, the all_candy table contains the candy_num column as a primary key. The hard_candy table refers to the candy_num column as a foreign key. The following CREATE TABLE statement creates the hard_candy table with the cascading-delete option on the foreign key:
CREATE TABLE all_candy (candy_num SERIAL PRIMARY KEY, candy_maker CHAR(25)); CREATE TABLE hard_candy (candy_num INT, candy_flavor CHAR(20), FOREIGN KEY (candy_num) REFERENCES all_candy ON DELETE CASCADE)
Because ON DELETE CASCADE is specified for the dependent table, when a row of the all_candy table is deleted, the corresponding rows of the hard_candy table are also deleted. For information about syntax restrictions and locking implications when you delete rows from tables that have cascading deletes, see Considerations When Tables Have Cascading Deletes on page 2-276.
CHECK Clause
Use the CHECK clause to designate conditions that must be met before data can be assigned to a column during an INSERT or UPDATE statement. CHECK Clause:
(1) CHECK ( Condition )
In Dynamic Server, the condition cannot include a user-defined routine. During an insert or update, if the check constraint of a row evaluates to false, the database server returns an error. The database server does not return an error if a row evaluates to NULL for a check constraint. In some cases, you might want to use both a check constraint and a NOT NULL constraint.
2-181
CREATE TABLE
the condition. When you specify a two-digit year, the DBCENTURY environment variable can produce unpredictable results if the condition depends on an abbreviated year value. For more information about DBCENTURY, see the IBM Informix Guide to SQL: Reference. More generally, the database server saves the settings of environment variables from the time of creation of check constraints. If any of these settings are subsequently changed in a way that can affect the evaluation of a condition in a check constraint, the new settings are disregarded, and the original environment variable settings are used when the condition is evaluated. With a BYTE or TEXT column, you can check for NULL or not-NULL values. This constraint is the only constraint allowed on a BYTE or TEXT column.
Both acct1 and acct2 are columns of MONEY data type whose values must be between 0 and 99999. If, however, you want to test that acct1 has a larger balance than acct2, you cannot use the single-column constraint format. To create a constraint that checks values in more than one column, you must use the Multiple-Column Constraint Format on page 2-184.
Constraint Definition
Use the constraint definition portion of CREATE TABLE for these purposes: v To declare a name for the constraint v To set a constraint to disabled, enabled, or filtering mode (IDS) Constraint Definition:
CONSTRAINT constraint
(1)
Notes: 1
Element constraint Description Name of constraint
2-182
CREATE TABLE
column, but without declaring a constraint name, the database server creates a constraint and adds a row for that constraint in the sysconstraints system catalog table. The database server also generates an identifier and adds a row to the sysindexes system catalog table for each new primary-key, unique, or referential constraint that does not share an index with an existing constraint. Even if you declare a name for a constraint, the database server generates the name that appears in the sysindexes table. If you want, you can specify a meaningful name for the constraint. The name must be unique among the names of constraints and indexes in the database. Constraint names appear in error messages having to do with constraint violations. You can use this name when you use the DROP CONSTRAINT clause of the ALTER TABLE statement. In Dynamic Server, you also specify a constraint name when you change the mode of constraint with the SET Database Object Mode statement or the SET Transaction Mode statement, and in the DROP INDEX statement for constraints that are implemented as indexes with user-defined names. In an ANSI-compliant database, when you declare the name of a constraint of any type, the combination of the owner name and constraint name must be unique within the database. In Dynamic Server, the system catalog table that holds information about indexes is the sysindices table. Constraint Names That the Database Server Generates: If you do not specify a constraint name, the database server generates a constraint name using the following template:
<constraint_type><tabid>_<constraintid>
In this template, constraint_type is the letter u for unique or primary-key constraints, r for referential constraints, c for check constraints, and n for NOT NULL constraints. In the template, tabid and constraintid are values from the tabid and constrid columns of the systables and sysconstraints system catalog tables, respectively. For example, the constraint name for a unique constraint might look like u111_14 (with a leading blank space). If the generated name conflicts with an existing identifier, the database server returns an error, and you must then supply an explicit constraint name. The generated index name in sysindexes (or sysindices) has this format:
[blankspace]<tabid>_<constraintid>
For example, the index name might be something like 111_14 (quotation marks used here to show the blank space).
2-183
CREATE TABLE
ENABLED Enforces the constraint during INSERT, DELETE, and UPDATE operations If a target row causes a violation of the constraint, the statement fails. This mode is the default. Enforces the constraint during INSERT, DELETE, and UPDATE operations If a target row causes a violation of the constraint, the statement continues processing. The database server writes the row in question to the violations table associated with the target table and writes diagnostic information to the associated diagnostics table.
FILTERING
If you choose filtering mode, you can specify the WITHOUT ERROR or WITH ERROR options. The following list explains these options. Error Option WITHOUT ERROR Effect Does not return an integrity-violation error when a filtering-mode constraint is violated during an insert, delete, or update operation. This is the default error option. Returns an integrity-violation error when a filtering-mode constraint is violated during an insert, delete, or update operation
WITH ERROR
To reset the constraint mode of a table, see SET Database Object Mode on page 2-539. For information about where the database server stores rows that violate a constraint set to FILTERING, see START VIOLATIONS TABLE on page 2-609.
2-184
CREATE TABLE
4
Element column Description Columns on which to place constraint
You can include a maximum of 16 columns in a constraint list. For databases where the page size is two kilobytes, the total length of the list of columns cannot exceed 380 bytes. When you define a unique constraint (by using the UNIQUE or DISTINCT keyword), a column cannot appear in the constraint list more than once. Using the multiple-column constraint format, you can perform these tasks: v Create data-integrity constraints for a set of one or more columns v Specify a mnemonic name for a constraint v Specify the constraint-mode option that controls the behavior of a constraint during insert, delete, and update operations. When you use this format, you can create composite primary and foreign keys, or define check constraints that compare data in different columns. See also the section Differences Between a Unique Constraint and a Unique Index on page 2-178.
2-185
CREATE TABLE
You can find detailed discussions of specific constraints in the following sections:
Constraint CHECK DISTINCT For more information, see CHECK Clause on page 2-181 For an example, see Defining Check Constraints Across Columns on page 2-186
Using the UNIQUE or Examples of the Multiple-Column DISTINCT Constraints on page Constraint Format on page 2-186 2-177 Using the FOREIGN KEY Constraint on page 2-186 Using the PRIMARY KEY Constraint on page 2-178 Defining Composite Primary and Foreign Keys on page 2-187 Defining Composite Primary and Foreign Keys on page 2-187
Using the UNIQUE or Examples of the Multiple-Column DISTINCT Constraints on page Constraint Format on page 2-186 2-177
For constraint names, see Declaring a Constraint Name on page 2-182. Defining Check Constraints Across Columns: When you use the multiple-column constraint format to define check constraints, a check constraint can apply to more than one column in the same table. (You cannot, however, create a check constraint whose condition uses a value from a column in another table.) This example compares two columns, acct1 and acct2, in the new table:
CREATE TABLE my_accounts ( chk_id SERIAL PRIMARY KEY, acct1 MONEY, acct2 MONEY, CHECK (0 < acct1 AND acct1 < 99999), CHECK (0 < acct2 AND acct2 < 99999), CHECK (acct1 > acct2) )
2-186
CREATE TABLE
In this example, the acct1 column must be greater than the acct2 column, or the insert or update fails. Defining Composite Primary and Foreign Keys: When you use the multiple-column constraint format, you can create a composite key. A composite key specifies multiple columns for a primary-key or foreign-key constraint. The next example creates two tables. The first table has a composite key that acts as a primary key, and the second table has a composite key that acts as a foreign key.
CREATE TABLE accounts ( acc_num INTEGER, acc_type INTEGER, acc_descr CHAR(20), PRIMARY KEY (acc_num, acc_type)) CREATE TABLE sub_accounts ( sub_acc INTEGER PRIMARY KEY, ref_num INTEGER NOT NULL, ref_type INTEGER NOT NULL, sub_descr CHAR(20), FOREIGN KEY (ref_num, ref_type) REFERENCES accounts (acc_num, acc_type))
In this example, the foreign key of the sub_accounts table, ref_num and ref_type, references the composite key, acc_num and acc_type, in the accounts table. If, during an insert or update, you tried to insert a row into the sub_accounts table whose value for ref_num and ref_type did not exactly correspond to the values for acc_num and acc_type in an existing row in the accounts table, the database server would return an error. A referential constraint must have a one-to-one relationship between referencing and referenced columns. In other words, if the primary key is a set of columns (a composite key), then the foreign key also must be a set of columns that corresponds to the composite key. Because of the default behavior of the database server, when you create the foreign-key reference, you do not have to reference the composite-key columns (acc_num and acc_type) explicitly. You can rewrite the references section of the previous example as follows:
FOREIGN KEY (ref_num, ref_type) REFERENCES accounts
Options Clauses
The Options clauses of the CREATE TABLE statement let you specify storage locations, extent size, locking modes, and user-defined access methods. Options:
(3)
(5)
2-187
CREATE TABLE
Notes: 1 2 3 4 5 Dynamic Server only Informix extension See page 2-188 See page 2-203 See page 2-202
v They do not appear in DBAccess when you ask for information about the columns of the table. v They are not included in the number of columns (ncols) in the systables system catalog table entry for tablename. To view the contents of cdrserver and cdrtime, explicitly specify the columns in the projection list of a SELECT statement, as the following example shows:
SELECT cdrserver, cdrtime FROM tablename
For more information about how to use this option, refer to the IBM Informix Dynamic Server Enterprise Replication Guide.
Storage Options
3 Use these options to specify the storage location, distribution scheme, and extent size for the table. This is an extension to the ANSI/ISO standard for SQL syntax. Storage Options:
IN
(4)
FRAGMENT BY Clause
2-188
CREATE TABLE
2 3 4 5
Element dbslice dbspace extspace Description Dbslice to store the table Dbspace to store the table Name declared in the onspaces command to a storage area outside the database server
Dynamic Server only See page 2-190 See page 2-199 See page 2-201
Restrictions Must already exist Must already exist Must already exist Syntax Identifier, p. 5-22 Identifier, p. 5-22 See documentation for your access method.
If you use the USING Access-Method Clause (IDS) on page 2-202 to specify an access method, that method must support the storage space. You can specify a dbspace for the table that is different from the storage location for the database, or fragment the table among dbspaces, or among partitions of one or more dbspaces. If you specify no IN clause nor fragmentation scheme, the new table resides in the same dbspace where the current database resides. In Dynamic Server, you can use the PUT clause to specify storage options for smart large objects. For more information, see PUT Clause (IDS) on page 2-199. Note: If your table contains simple large objects (TEXT or BYTE), you can specify a separate blobspace for each object. For information on storing simple large objects, refer to Large-Object Data Types on page 4-25.
For more information about how to store and manage your tables in separate dbspaces, see your IBM Informix Administrator's Guide. Storing Data in a Partition of a dbspace (IDS): Besides the option of storing the table (or a fragment of it) in a dbspace, Dynamic Server supports storing fragments of a table in a subset of a dbspace, called a partition. Unless you explicitly declare names for the fragments in the PARTITION BY clause, each fragment, by default, has the same name as the dbspace where it resides. This includes all fragmented tables and indexes migrated from earlier releases of Dynamic Server.
Chapter 2. SQL Statements
2-189
CREATE TABLE
You can store fragments of the same table in multiple partitions of the same dbspace, but each name that you declare after the PARTITION keyword must be unique among partitions of that dbspace. The PARTITION keyword is required when you store more than one fragment of the table in the same dbspace. You can also use the PARTITION keyword to declare a more meaningful name for a dbspace that has only one partition. Storing Data in a dbslice (XPS): If you are using Extended Parallel Server, the IN dbslice clause allows you to fragment a table across a group of dbspaces that share the same naming convention. The database server fragments the table by round-robin in the dbspaces that make up the dbslice at the time the table is created. To fragment a table across a dbslice, you can use either the IN dbslice syntax or the FRAGMENT BY ROUND ROBIN IN dbslice syntax. Storing Data in an extspace (IDS): In general, use the extspace storage option in conjunction with the USING Access-Method Clause (IDS) on page 2-202. For more information, refer to the documentation of your access method.
FRAGMENT BY Clause
Use the FRAGMENT BY clause to create fragmented tables and to specify their distribution scheme. (The keywords PARTITION BY are a synonym for FRAGMENT BY in Dynamic Server.) FRAGMENT BY Clause for Tables:
FRAGMENT (1) PARTITION BY
, (1) EXPRESSION (1) USING opclass , (2) HASH ( column , HYBRID ( column ) (3) RANGE Method Clause EXPRESSION Clause ) IN dbspace dbslice , PARTITION part IN dbspace Fragment List
2-190
CREATE TABLE
Fragment List:
, , ( expr ) (1) PARTITION part IN dbspace
IN dbspace
EXPRESSION Clause:
, EXPRESSION expr IN dbspace dbslice , ( dbspace ) , REMAINDER expr IN dbspace dbslice , ( dbspace ) )
Notes: 1 2 3 Dynamic Server only Extended Parallel Server only See page 2-195
Description Column to which to apply the fragmentation strategy Dbslice or dbspace to store the table fragment Restrictions Must be a column within the table The dbslice must be defined. You can specify no more than 2,048 dbspaces. Syntax Identifier, p. 5-22 Identifier, p. 5-22 Expression, p. 4-34 Identifier, p. 5-22 Identifier, p. 5-22
Expression that defines a table Columns can be from the current table only, and fragment using a range, hash, or data values can be from only a single row. Value arbitrary rule returned must be Boolean (true or false). No default operator class Name that you declare here for a partition of a dbspace Must be defined and must be associated with a B-tree index Required for any partition in the same dbspace as another partition of the same table
opclass part
When you fragment a table, the IN keyword is followed by the name of the storage space where a table fragment is to be stored.
2-191
CREATE TABLE
row and that the database server can use to find the physical location of the row. Each row requires an additional four bytes to store the rowid. Important: This is a deprecated feature. Use primary keys as an access method rather than the rowid column. You cannot use the WITH ROWIDS clause with typed tables.
Fragmenting by EXPRESSION
In an expression-based distribution scheme, each fragment expression in a rule specifies a storage space. Each fragment expression in the rule isolates data and aids the database server in searching for rows. To fragment a table by expression, specify one of the following rules: v Range rule A range rule specifies fragment expressions that use a range to specify which rows are placed in a fragment, as the next example shows:
FRAGMENT BY EXPRESSION c1 < 100 IN dbsp1, c1 >= 100 AND c1 < 200 IN dbsp2, c1 >= 200 IN dbsp3
v Arbitrary rule An arbitrary rule specifies fragment expressions based on a predefined SQL expression that typically uses OR clauses to group data, as the following example shows:
FRAGMENT BY EXPRESSION zip_num = 95228 OR zip_num = 95443 IN dbsp2, zip_num = 91120 OR zip_num = 92310 IN dbsp4, REMAINDER IN dbsp5
Warning: See the note about the DBCENTURY environment variable and date values in fragment expressions in the section Logging Options on page 2-172. The USING Opclass Option (IDS): With the USING operator class option, you can specify a nondefault operator class for the fragmentation strategy. The secondary-access method of the chosen operator class must have a B-tree index structure.
2-192
CREATE TABLE
In the following example, the abs_btree_ops operator class specifies several user-defined strategy functions that order integers based on their absolute values:
CREATE OPCLASS abs_btree_ops FOR btree STRATEGIES (abs_lt, abs_lte, abs_eq, abs_gte, abs_gt) SUPPORT (abs_cmp)
For the fragmentation strategy, you can specify the abs_btree_ops operator class in the USING clause and use its strategy functions to fragment the table, as follows:
FRAGMENT BY EXPRESSION USING abs_btree_ops (abs_lt(x,345)) IN dbsp1, (abs_gte(x,345) AND abs_lte(x,500)) IN dbsp2, (abs_gt(x,500)) IN dbsp3
For information on how to create and extend an operator class, see IBM Informix User-Defined Routines and Data Types Developer's Guide. User-Defined Functions in Fragment Expressions (IDS): For rows that include user-defined data types, you can use comparison conditions or user-defined functions to define the range rules. In the following example, comparison conditions define the range rules for the long1 column, which contains an opaque data type:
FRAGMENT BY EXPRESSION long1 < 3001 IN dbsp1, long1 BETWEEN 3001 AND 6000 IN dbsp2, long1 > 6000 IN dbsp3
An implicit, user-defined cast converts 3001 and 6000 to the opaque type. Alternatively, you can use user-defined functions to define the range rules for the opaque data type of the long1 column:
FRAGMENT BY EXPRESSION (lessthan(long1,3001)) IN dbsp1, (greaterthanorequal(long1,3001) AND lessthanorequal(long,6000)) IN dbsp2, (greaterthan(long1,6000)) IN dbsp3
Explicit user-defined functions require parentheses around the entire fragment expression before the IN clause, as the previous example shows. User-defined functions in a fragment expression can be written in SPL or in the C or Java language. These functions must satisfy four requirements: v They must evaluate to a Boolean value. v They must be nonvariant. v They must reside within the same database as the table. v They must not generate OUT nor INOUT parameters. For information on how to create UDRs for fragment expressions, refer to IBM Informix User-Defined Routines and Data Types Developer's Guide. Using the REMAINDER Keyword: Use the REMAINDER keyword to specify the storage space in which to store valid values that fall outside the specified expression or expressions. If you do not specify a remainder, and a row is inserted or updated with values that do not correspond to any fragment definition, the database server returns an error.
2-193
CREATE TABLE
The following example uses an arbitrary rule to define five fragments for specific values of the c1 column, and a sixth fragment for all other values: 3 3 3 3 3 3 3
CREATE TABLE T1 (c1 INT) FRAGMENT BY EXPRESSION PARTITION PART_1 (c1 = 10) IN dbs1, PARTITION PART_2 (c1 = 20) IN dbs1, PARTITION PART_3 (c1 = 30) IN dbs1, PARTITION PART_4 (c1 = 40) IN dbs2, PARTITION PART_5 (c1 = 50) IN dbs2, PARTITION PART_6 REMAINDER IN dbs2;
Here the first three fragments are stored in partitions of the dbs1 dbspace, and the other fragments, including the remainder, are stored in partitions of the dbs2 dbspace. Explicit fragment names are required in this example, because each dbspace has multiple partitions.
You can also specify a dbslice. When you specify a dbslice, the database server fragments the table across the dbspaces that make up the dbslice. Serial Columns in HASH-Distribution Schemes: If you base table fragmentation on a SERIAL or SERIAL8 column, only a hash-distribution scheme is valid. In addition, the serial column must be the only column in the hashing key. (These restrictions apply only to table distributions. Fragmentation schemes for indexes that are based on SERIAL or SERIAL8 columns are not subject to these restrictions.) The following excerpt is from a CREATE TABLE statement:
2-194
CREATE TABLE
CREATE TABLE customer ( cust_id serial, . . . ) FRAGMENT BY HASH (cust_id) IN customer1_spc, customer2_spc
You might notice a difference between serial-column values in fragmented and nonfragmented tables. The database server assigns serial values round-robin across fragments, so a fragment might contain values from noncontiguous ranges. For example, if there are two fragments, the first serial value is placed in the first fragment, the second serial value is placed in the second fragment, the third value is placed in the first fragment, and so on.
For more information on an expression-based distribution scheme, see Fragmenting by EXPRESSION on page 2-192.
2-195
CREATE TABLE
, (1) RANGE ( column HYBRID Range Definition dbspace dbslice Second Range Specification ) IN REMAINDER IN dbspace
For hybrid strategies with two range definitions, the second column must have a different column name from the first. For hybrid strategies with exactly one range definition, both occurrences of column must specify the same column. If you list more than one dbslice, including a remainder dbslice, each dbslice must contain the same number of dbspaces. Unless you are specifying the dbspace in the REMAINDER option, you must specify at least two dbspaces.
Range Definition
Use the range definition to specify the minimum and maximum values of the entire range. Range Definition:
max_val MIN min_val MAX
2-196
CREATE TABLE
Element max_val min_val Description Maximum value in the range Minimum value in the range; the default is 0. Restrictions Must be an INT or SMALLINT greater than or equal to min_val, if min_val is supplied Must be an INT or SMALLINT less than or equal to max_val Syntax Literal Number, p. 4-137 Literal Number, p. 4-137
You do not need to specify a minimum value. The minimum and maximum values define the exact range of values to allocate for each storage space.
Range IN Clause
Use the IN clause to specify the storage spaces in which to distribute the data. Range IN Clause:
, IN dbslice , ( dbspace ) ( dbspace )
REMAINDER IN
dbslice ,
Description Dbslice that contains the dbspaces to store table fragments Dbspace to store the table fragment
If you specify more than one dbslice, including a remainder dbslice, each dbslice must contain the same number of dbspaces. Unless you are specifying the dbspace in the REMAINDER option, the minimum number of dbspaces that you can specify is two. The maximum number of dbspaces that you can specify is 2,048. When you use a range-fragmentation method, the number of integer values between the minimum and maximum specified values must be equal to or greater than the number of storage spaces specified, so that the database server can allocate non-overlapping contiguous ranges across the dbspaces. For example, the following code returns an error, because the allocations for the range cannot be distributed across all specified dbspaces:
CREATE TABLE Tab1 (Col1 INT...) FRAGMENT BY RANGE (Col1 MIN 5 MAX 7) IN db1, db2, db3, db4, db5, db6 -- returns an error
The error for this example occurs because the specified range contains three values (5, 6, and 7), but six dbspaces are specified; three values cannot be distributed across six dbspaces.
2-197
CREATE TABLE
If you do not specify a remainder and a row is inserted or updated such that it no longer belongs to any storage space, the database server returns an error.
Restrictions
If you fragment a table with range fragmentation, you cannot perform the following operations on the table after it is created: v You cannot change the fragmentation strategy (ALTER FRAGMENT). v You cannot rename the columns of the table (RENAME COLUMN). v You cannot alter the table in any way except to change the table type or to change the lock mode. That is, the Usage-TYPE options and the Lock Mode clause are the only valid options of ALTER TABLE for a table that has range fragmentation.
Examples
The following examples illustrate range fragmentation in its simple and hybrid forms. Simple Range-Fragmentation Strategy: The following example shows a simple range-fragmentation strategy:
CREATE TABLE Tab1 (Col1 INT...) FRAGMENT BY RANGE (Col1 MIN 100 MAX 200) IN db1, db2, db3, db4
In this example, the database server fragments the table according to the following allocations.
Storage Space db1 db2 Holds Values 100 <= Col1 < 125 125 <= Col1 < 150 Storage Space db3 db4 Holds Values 150 <= Col1 < 175 175 <= Col1 < 200
The previous table shows allocations that can also be made with an expression-based fragmentation scheme:
CREATE TABLE ... FRAGMENT Col1 >= 100 AND Col1 < Col1 >= 125 AND Col1 < Col1 >= 150 AND Col1 < Col1 >= 175 AND Col1 < BY EXPRESSION 125 IN db1 150 IN db2 175 IN db3 200 IN db4
As the examples show, the range-fragmentation example requires much less coding to achieve the same results. The same is true for the hybrid-range fragmentation compared to hybrid-expression fragmentation methods. Column-Major-Range Allocation: The following example demonstrates the syntax for column-major-range allocation, a hybrid-range fragmentation strategy:
CREATE TABLE tab2 (col2 INT, colx char (5)) FRAGMENT BY HYBRID ( RANGE (col2 MIN 100 MAX 220)) RANGE (col2) IN dbsl1, dbsl2, dbsl3
This type of fragmentation creates a distribution across dbslices and provides a further subdivision within each dbslice (across the dbspaces in the dbslice) such that when a query specifies a value for col1 (for example, WHERE col1 = 127), the
2-198
CREATE TABLE
query uniquely identifies a dbspace. To take advantage of the additional subdivision, you must specify more than one dbslice. Row-Major-Range Allocation: The following example demonstrates the syntax for row-major-range allocation, a hybrid-range fragmentation strategy:
CREATE TABLE tab3 (col3 INT, colx char (5)) FRAGMENT BY HYBRID ( RANGE (col3) ) RANGE (col3 MIN 100 MAX 220) IN dbsl1, dbsl2, dbsl3
This fragmentation strategy is the counterpart to the column-major-range allocation. The advantages and restrictions are equivalent. Independent-Range Allocation: The following example demonstrates the syntax for an independent-range allocation, a hybrid-range fragmentation strategy:
CREATE TABLE tab4 (col4 INT, colx char (5), col5 INT) FRAGMENT BY HYBRID ( RANGE (col4 MIN 100 MAX 200) ) RANGE (col5 MIN 500 MAX 800) IN dbsl1, dbsl2, dbsl3
In this type of range fragmentation, the two columns are independent, and therefore the range allocations are independent. The range allocation for a dbspace on both columns is the conjunctive combination of the range allocation on each of the two independent columns. This type of fragmentation does not provide subdivisions within either column. With this type of fragmentation, a query that specifies values for both columns (such as, WHERE col4 = 128 and col5 = 650) uniquely identifies the dbspace at the intersection of the two dbslices identified by the columns independently.
2-199
CREATE TABLE
Element column kbytes sbspace Description Column to store in sbspace Number of kilobytes to allocate for the extent size Name of an area of storage Restrictions Must contain a BLOB, CLOB, user-defined, or complex data type Must be an integer value Must exist Syntax Identifier, p. 5-22 Literal Number, p. 4-137 Identifier, p. 5-22
The column cannot be in the form column.field. That is, the smart large object that you are storing cannot be one field of a row type. A smart large object is contained in a single sbspace. The SBSPACENAME configuration parameter specifies the system default in which smart large objects are created, unless you specify another area. Specifying more than one sbspace distributes the smart large objects in a round-robin distribution scheme, so that the number of smart large objects in each space is approximately equal. The syscolattribs system catalog table contains one row for each sbspace that you specify in the PUT clause. When you fragment smart large objects across different sbspaces, you can work with smaller sbspaces. If you limit the size of an sbspace, backup and archive operations can perform more quickly. For an example that uses the PUT clause, see Alternative to Full Logging on page 2-201. Six storage options are available to store BLOB and CLOB data: Option EXTENT SIZE Effect Specifies how many kilobytes in a smart-large-object extent. The database server might round the EXTENT SIZE up so that the extents are multiples of the sbspace page size. Produces user-data pages that contain a page header and a page trailer to detect incomplete writes and data corruption. This is the default data-integrity behavior. Records, in the smart-large-object metadata, the system time when the smart large object was last read or written. Follows the logging procedure used with the current database log for the corresponding smart large object. This option can generate large amounts of log traffic and increase the risk of filling the logical log. (See also Alternative to Full Logging on page 2-201.) Does not record the system time when the smart large object was last read or written. This provides better performance than the KEEP ACCESS TIME option and is the default tracking behavior. Turns off logging. This option is the default behavior.
HIGH INTEG
LOG
NO LOG
2-200
CREATE TABLE
If a user-defined or complex data type contains more than one large object, the specified large-object storage options apply to all large objects in the type unless the storage options are overridden when the large object is created. Important: The PUT clause does not affect the storage of simple-large-object data types (BYTE and TEXT). For information on how to store BYTE and TEXT data, see Large-Object Data Types on page 4-25.
Description Length in kilobytes of the first extent for the table; default is 16. Length in kilobytes of each subsequent extent; default is 16.
Restrictions Must return a positive number; maximum is the chunk size Must return a positive number; maximum is the chunk size
The minimum length of first_kilobytes (and of next_kilobytes) is four times the disk-page size on your system. For example, if you have a 2-kilobyte page system, the minimum length is 8 kilobytes. The next example specifies a first extent of 20 kilobytes and allows the rest of the extents to use the default size:
CREATE TABLE emp_info ( f_name CHAR(20),
Chapter 2. SQL Statements
2-201
CREATE TABLE
l_name CHAR(20), position CHAR(20), start_date DATETIME YEAR TO DAY, comments VARCHAR(255) ) EXTENT SIZE 20
If you need to revise the extent sizes of a table, you can modify the extent and next-extent sizes in the generated schema files of an unloaded table. For example, to make a database more efficient, you might unload a table, modify the extent sizes in the schema files, and then create and load a new table. For information about how to optimize extents, see your IBM Informix Administrator's Guide.
, ( config_keyword =config_value )
Notes: 1
Element config_keyword config_value
Description Configuration keyword associated with the specified access method Value of the specified configuration keyword
A primary-access method is a set of routines to perform DDL and DML operations, such as create, drop, insert, delete, update, and scan, to make a table available to the database server. Dynamic Server provides a built-in primary-access method. You store and manage a virtual table either outside of the database server in an extspace or inside the database server in an sbspace. (See Storage Options on page 2-188.) You can access a virtual table with SQL statements. Access to a virtual table requires a user-defined primary-access method. DataBlade modules can provide other primary-access methods to access virtual tables. When you access a virtual table, the database server calls the routines associated with that access method rather than the built-in table routines. For more information on these other primary-access methods, refer to your access-method documentation.
2-202
CREATE TABLE
You can retrieve a list of configuration values for an access method from a table descriptor (mi_am_table_desc) using the MI_TAB_AMPARAM macro. Not all keywords require configuration values. The access method must already exist. For example, if an access method called textfile exists, you can specify it with the following syntax:
CREATE TABLE mybook (... ) IN myextspace USING textfile (DELIMITER=:)
You can subsequently change the lock mode of the table with the ALTER TABLE statement.
Granularity PAGE Effect Obtains and releases one lock on a whole page of rows This is the default locking granularity. Page-level locking is especially useful when you know that the rows are grouped into pages in the same order that you are using to process all the rows. For example, if you are processing the contents of a table in the same order as its cluster index, page locking is appropriate. ROW Obtains and releases one lock per row Row-level locking provides the highest level of concurrency. If you are using many rows at one time, however, the lock-management overhead can become significant. You might also exceed the maximum number of locks available, depending on the configuration of your database server. TABLE (XPS) Places a lock on the entire table This type of lock reduces update concurrency compared to row and page locks. A table lock reduces the lock-management overhead for the table With table locking, multiple read-only transactions can still access the table.
2-203
CREATE TABLE
You can set the IFX_DEF_TABLE_LOCKMODE environment variable to specify the lock mode of new tables during your current session. v Database server (all sessions on the database server) If you are a DBA, you can set the DEF_TABLE_LOCKMODE configuration parameter in the ONCONFIG file to determine the lock mode of all new tables in the database server. If you are not a DBA, you can set the IFX_DEF_TABLE_LOCKMODE environment variable for the database server, before you run oninit, to specify the lock mode of all new tables of the database server. The LOCK MODE setting in a CREATE TABLE statement takes precedence over the settings of the IFX_DEF_TABLE_LOCKMODE environment variable and the DEF_TABLE_LOCKMODE configuration parameter. If CREATE TABLE specifies no lock mode setting, the default mode depends on the setting of the IFX_DEF_TABLE_LOCKMODE environment variable or the DEF_TABLE_LOCKMODE configuration parameter. For information about IFX_DEF_TABLE_LOCKMODE, refer to the IBM Informix Guide to SQL: Reference. For information about the DEF_TABLE_LOCKMODE configuration parameter, refer to the IBM Informix Administrator's Reference.
If you use the UNDER clause, the row_type must be derived from the ROW type of the supertable. A type hierarchy must already exist in which the named ROW type of the new table is a subtype of the named ROW type of the supertable.
2-204
CREATE TABLE
Jagged rows are any set rows from a table hierarchy in which the number of columns is not fixed among the typed tables within the hierarchy. Some APIs, such as ESQL/C and JDBC, do not support queries that return jagged rows. When you create a typed table, CREATE TABLE cannot specify names for its columns, because the column names were declared when you created the ROW type. Columns of a typed table correspond to the fields of the named ROW type. The ALTER TABLE statement cannot add additional columns to a typed table. For example, suppose you create a named ROW type, student_t, as follows:
CREATE ROW TYPE student_t (name VARCHAR(30), average REAL, birthdate DATETIME YEAR TO DAY)
If a table is assigned the type student_t, the table is a typed table whose columns are of the same name and data type, and in the same order, as the fields of the type student_t. For example, the following CREATE TABLE statement creates a typed table named students whose type is student_t:
CREATE TABLE students OF TYPE student_t
For more information about ROW types, refer to the CREATE ROW TYPE statement on page 1-194.
When you use the UNDER clause, the subtable inherits these properties: v All columns in the supertable v All constraints defined on the supertable
Chapter 2. SQL Statements
2-205
CREATE TABLE
v v v v v All indexes defined on the supertable All triggers defined on the supertable. All referential integrity constraints The access method The storage option specification (including fragmentation strategy) If a subtable defines no fragments, but if its supertable has fragments defined, then the subtable inherits the fragments of the supertable.
Tip: Any heritable attributes that are added to a supertable after subtables have been created are automatically inherited by existing subtables. You do not need to add all heritable attributes to a supertable before you create its subtables. Restrictions on Table Hierarchies: Inheritance occurs in one direction only, namely from supertable to subtable. Properties of subtables are not inherited by supertables. The section System Catalog Information on page 2-207 lists the inherited database objects for which the system catalog maintains no information regarding subtables. No two tables in a table hierarchy can have the same data type. For example, the final line of the next code example is invalid, because the tables tab2 and tab3 cannot have the same row type (rowtype2):
create create create create --Invalid --> row type rowtype1 (...); row type rowtype2 (...) under rowtype1; table tab1 of type rowtype1; table tab2 of type rowtype2 under tab1; create table tab3 of type rowtype2 under tab1;
Privileges on Tables
The privileges on a table describe both who can access the information in the table and who can create new tables. For more information about access privileges, see GRANT on page 2-371. In an ANSI-compliant database, no default table-level privileges exist. You must grant these privileges explicitly. Setting the environment variable NODEFDAC to yes prevents default privileges from being granted to PUBLIC on new tables in a database that is not ANSI compliant, as described in the IBM Informix Guide to SQL: Reference. For more information about privileges, see the IBM Informix Guide to SQL: Tutorial.
2-206
CREATE TABLE
table. Instead, use the CREATE INDEX statement to create a unique index with the desired fragmentation strategy. Then use the ALTER TABLE statement to add the constraint. The new constraint uses the previously defined index. Important: In a database without logging, detached checking is the only kind of constraint checking available. Detached checking means that constraint checking is performed on a row-by-row basis.
Related Information
Related statements: ALTER TABLE, CREATE INDEX, CREATE DATABASE, CREATE EXTERNAL TABLE (XPS), CREATE ROW TYPE, CREATE Temporary TABLE, DROP TABLE, SET Database Object Mode, and SET Transaction Mode For Extended Parallel Server, see also SET Default Table Type and SET Default Table Space. For discussions of database and table creation, including discussions on data types, data-integrity constraints, and tables in hierarchies, see the IBM Informix Database Design and Implementation Guide. For information about the system catalog tables that store information about objects in the database, see the IBM Informix Guide to SQL: Reference. For information about the syschunks table (in the sysmaster database) that contains information about the location of smart large objects, see your IBM Informix Administrator's Reference.
2-207
Syntax
CREATE TEMP TABLE table Table Definition
Table Definition:
, (1) ( Column Definition , (2) , Multiple-Column Constraint (1) Column Definition ) Options (3)
WITH NO LOG
Notes: 1 2 3 See page 2-209 See page 2-211 See page 2-212
Description Name declared here for a temporary table Restrictions Must be unique among the names of temporary tables in the same session Syntax Identifier, p. 5-22
Element table
Usage
The CREATE TEMP TABLE statement is a special case of the CREATE Temporary TABLE statement. The CREATE Temporary TABLE statement can also create a SCRATCH table in an Extended Parallel Server database. For the complete syntax and semantics of the CREATE TEMP TABLE statement, see CREATE Temporary TABLE on page 2-209.
2-208
Syntax
CREATE TEMP (1) SCRATCH TABLE table Table Definition
Table Definition:
, (2) ( Column Definition , (3) , Multiple-Column Constraint Format (2) Column Definition )
Notes: 1 2 3 4 Extended Parallel Server only See page 2-209 See page 2-211 See page 2-212
Description Name declared here for a table Restrictions Must be unique in session Syntax Database Object Name, p. 5-17
Element table
Usage
You must have the Connect privilege on the database to create a temporary table. The temporary table is visible only to the user who created it. In DBAccess, using the CREATE Temporary Table statement outside the CREATE SCHEMA statement generates warnings if you set DBANSIWARN. In ESQL/C, the CREATE Temporary TABLE statement generates warnings if you use the -ansi flag or set the DBANSIWARN environment variable.
2-209
Column Definition
Use the Column Definition segment of CREATE Temporary TABLE to list the name, data type, default value, and constraints of a single column. Column Definition:
(1) column Data Type (2) DEFAULT Clause
2-210
This portion of the CREATE Temporary TABLE statement is almost identical to the corresponding section in the CREATE TABLE statement. The difference is that fewer types of constraints are allowed in a temporary table.
This is a subset of the syntax of Single-Column Constraint Format on page 2-176 that the CREATE TABLE statement supports. You can find detailed discussions of specific constraints in these sections. Constraint CHECK DISTINCT NOT NULL PRIMARY KEY Using the PRIMARY KEY Constraint on page 2-178 UNIQUE Using the UNIQUE or DISTINCT Constraints on page 2-177 For more information, see CHECK Clause on page 2-181 Using the UNIQUE or DISTINCT Constraints on page 2-177 Using the NOT NULL Constraint on page 2-177
2-211
Notes: 1 2
Element column Description Name of column or columns on which the constraint is placed
This is a subset of the syntax of Multiple-Column Constraint Format on page 2-184 that the CREATE TABLE statement supports. This alternative to the single-column constraint segment of CREATE Temporary TABLE can associate multiple columns with a constraint. Constraints that you define on temporary tables are always enabled. The following table indicates where you can find detailed discussions of specific constraints.
Constraint CHECK DISTINCT PRIMARY KEY UNIQUE For more information, see CHECK Clause on page 2-181 Using the UNIQUE or DISTINCT Constraints on page 2-177 Using the PRIMARY KEY Constraint on page 2-178 Using the UNIQUE or DISTINCT Constraints on page 2-177 For an example, see Defining Check Constraints Across Columns on page 2-186 Examples of the Multiple-Column Constraint Format on page 2-186 Defining Composite Primary and Foreign Keys on page 2-187 Examples of the Multiple-Column Constraint Format on page 2-186
See also the section Differences Between a Unique Constraint and a Unique Index on page 2-178.
(3)
2-212
(5)
Notes: 1 2 3 4 5 Dynamic Server only Informix extension See page 2-213 See page 2-203 See page 2-202
This is a subset of the syntax of Options Clauses on page 2-187 that the CREATE TABLE statement supports.
Storage Options
Use the storage-option segment of the CREATE Temporary Table statement to specify the storage location and distribution scheme for the table. This is an extension to the ANSI/ISO standard for SQL syntax. Unlike the corresponding storage-options segment of the CREATE TABLE statement, no EXTENT SIZE options are supported for temporary tables. Storage Options:
IN
(4)
FRAGMENT BY Clause
Notes: 1 2 3 4
Element dbspace dbslice extspace Description Dbspace in which to store the table. Name of the dbslice in which to store the table
Extended Parallel Server only Dynamic Server only See page 2-190 See page 2-199
Restrictions Must already exist Must already exist Syntax Identifier, p. 5-22 Identifier, p. 5-22 See documentation for access method.
Name that onspaces assigned to a storage area outside Must already exist the database server
To create a fragmented, unique index on a temporary table, you must specify an explicit expression-based distribution scheme for a temporary table in the CREATE Temporary TABLE statement. If you are using Extended Parallel Server, you can fragment a temporary table across multiple dbspaces that different coservers manage.
Chapter 2. SQL Statements
2-213
3 3 3 3 3
In Extended Parallel Server, to re-create this example, use the EXECUTE PROCEDURE statement instead of the EXECUTE FUNCTION statement.
2-214
Related Information
Related statements: ALTER TABLE, CREATE TABLE, CREATE DATABASE, DROP TABLE, and SELECT If you use Extended Parallel Server, see also SET Default Table Type and SET Default Table Space. For additional information about the DBANSIWARN and DBSPACETEMP environment variables, refer to the IBM Informix Guide to SQL: Reference. For additional information about the ONCONFIG parameter DBSPACETEMP, see your IBM Informix Administrator's Guide.
2-215
CREATE TRIGGER
CREATE TRIGGER
Use the CREATE TRIGGER statement to define a trigger on a table. This is an extension to the ANSI/ISO standard for SQL. For Dynamic Server, you can also use CREATE TRIGGER to define an INSTEAD OF trigger on a view.
Syntax
CREATE TRIGGER (1) Owner Name trigger
ENABLED DISABLED
Notes: 1 2 3 4
Element trigger Description Name that you declare here for a new trigger
See page 5-43 See page 2-217 Dynamic Server only See page 2-243
Restrictions Must be unique among the names of triggers in the current database Syntax Identifier, p. 5-22
Usage
A trigger, unless disabled, automatically executes a specified set of SQL statements, called the trigger action, when a specified trigger event occurs. The trigger event that initiates the trigger action can be an INSERT, DELETE, UPDATE, or (for triggers on IDS tables only) a SELECT statement. The event must specify the table or view on which the trigger is defined. (SELECT or UPDATE events for triggers on tables can also specify one or more columns.) You can use the CREATE TRIGGER statement in two distinct ways: v You can define a trigger on a table in the current database. v For Dynamic Server, you can also define an INSTEAD OF trigger on a view in the current database. Any SQL statement that is an instance of the trigger event is called a triggering statement. When the event occurs, triggers defined on tables and triggers defined on views differ in whether the triggering statement is executed: v For tables, the trigger event and the trigger action both execute. v For views, only the trigger action executes, instead of the event.
2-216
CREATE TRIGGER
The CREATE TRIGGER statement can support the integrity of data in the database by defining rules by which specified DML operations (the triggering events) cause the database server to take specified actions. The following sections describe the syntax elements. Sections whose names are followed by (IDS) describe features that are not supported by Extended Parallel Server.
Clause Defining a Trigger Event and Actions Trigger Modes (IDS) Insert Events and Delete Events Update Events Select Events (IDS) Action Clause REFERENCING Clause for Delete REFERENCING Clause for Insert REFERENCING Clause for Update REFERENCING Clause for Select (IDS) Correlated Table Action Triggered Action List INSTEAD OF Trigger on Views (IDS) Action Clause of INSTEAD OF Triggers (IDS) Page 2-217 2-219 2-222 2-222 2-223 2-226 2-228 2-229 2-230 2-231 2-231 2-232 2-243 2-244 Effect Associates triggered actions with an event Enables or disables the trigger Defines Insert events and Delete events Defines Update events Defines Select events Defines triggered actions Declares qualifier for deleted values Declares qualifier for inserted values Declares qualifiers for old and new values Declares qualifier for result set values Defines triggered actions Defines triggered actions Defines a trigger on views Triggered actions on views
UPDATE Subclauses
2-217
CREATE TRIGGER
UPDATE Subclauses:
(4) Action Clause (5) OLD Declaration (5) OLD Declaration Correlated Table Action (2) NEW Declaration (3) (3) Correlated Table Action
Trigger on a View:
(1) INSERT ON view REFERENCING NEW AS DELETE ON view REFERENCING OLD AS UPDATE ON view REFERENCING OLD AS REFERENCING NEW AS new OLD AS (6) INSTEAD OF Triggered Action old old NEW AS new old new FOR EACH ROW
Notes: 1 2 3 4 5 6 Dynamic Server only See page 2-229 See page 2-231 See page 2-226 See page 2-230 See page 2-243
Description The name of a column in the triggering table Old or new correlation name that you declare here Name or synonym of the triggering table or view. The table or view can include an owner. qualifier. Restrictions Must exist Unique in this trigger Syntax Identifier, p. 5-22 Identifier, p. 5-22
The left-hand portion of the main diagram (including the table or view) defines the trigger event (sometimes called the triggering event). The rest of the diagram declares correlation names and defines the trigger action (sometimes called the triggered action). (For triggers on tables, see Action Clause on page 2-226 and Correlated Table Action on page 2-231. For INSTEAD OF triggers on views, see The Action Clause of INSTEAD OF Triggers (IDS) on page 2-244.)
2-218
CREATE TRIGGER
Restrictions on Triggers
To create a trigger on a table or a view, you must own the table or view, or have DBA status. For the relationship between the privileges of the trigger owner and those of other users, see Privileges to Execute Trigger Actions on page 2-239. The table on which you create a trigger must exist in the current database. You cannot create a trigger on any of the following types of tables: v A diagnostics table, a violations table, or a table in another database v A raw table, a temporary table, or a system catalog table Extended Parallel Server does not support triggers on a static or scratch tables, and does not support light appends on operational tables on which a trigger is defined. (Light appends are described in the IBM Informix Administrator's Guide.) For additional restrictions on INSTEAD OF triggers on views, see Restrictions on INSTEAD OF Triggers on Views (IDS) on page 2-245. In DBAccess, if you want to define a trigger as part of a schema, place the CREATE TRIGGER statement inside a CREATE SCHEMA statement. If you are embedding the CREATE TRIGGER statement in an ESQL/C program, you cannot use a host variable in the trigger definition. You can use the DROP TRIGGER statement to remove an existing trigger. If you use DROP TABLE or DROP VIEW to remove triggering tables or views from the database, all triggers on those tables or views are also dropped.
You can create triggers on tables or on views in ENABLED or DISABLED mode. v When a trigger is created in ENABLED mode, the database server executes the trigger action when the trigger event is encountered. (If you specify no mode when you create a trigger, ENABLED is the default mode.) v When a trigger is created in DISABLED mode, the trigger event does not cause execution of the trigger action. In effect, the database server ignores the trigger and its action, even though the systriggers system catalog table maintains information about the disabled trigger. You can also use the SET TRIGGERS option of the Database Object Mode statement to set an existing trigger to the ENABLED or DISABLED mode. After a DISABLED trigger is enabled by the SET TRIGGERS statement, the database server can execute the trigger action when the trigger event is encountered, but the trigger does not perform retroactively. The database server does not attempt to execute the trigger for rows that were selected, inserted, deleted, or updated while the trigger was disabled and before it was enabled.
2-219
CREATE TRIGGER
Warning: Because the behavior of a trigger varies according to its ENABLED or DISABLED mode, be cautious about disabling a trigger. If disabling a trigger will eventually destroy the semantic integrity of the database, do not disable the trigger.
SPL variables are not valid in CREATE TRIGGER statements. An SPL routine called by a trigger cannot perform INSERT, DELETE, or UPDATE operations on any table or view that is not local to the current database. See also Rules for SPL Routines on page 2-238 for additional restrictions on SPL routines that are invoked in triggered actions.
Trigger Events
The trigger event specifies what DML statements can initiate the trigger. The event can be an INSERT, DELETE, or UPDATE operation on the table or view, or (for IDS tables only) a SELECT operation that manipulates the table. You must specify exactly one trigger event. Any SQL statement that is an instance of the trigger event is called a triggering statement. For each table, you can define only one trigger that is activated by an INSERT statement and only one trigger that is activated by a DELETE statement. The same table, however, can have multiple triggers that are activated by UPDATE or SELECT statements, provided that each trigger specifies a disjunct set of columns in defining the UPDATE or SELECT event on the table.
2-220
CREATE TRIGGER
You cannot specify a DELETE event if the triggering table has a referential constraint that specifies ON DELETE CASCADE. You are responsible for guaranteeing that the triggering statement returns the same result with and without the trigger action on a table. See also the sections Action Clause on page 2-226 and Triggered-Action List on page 2-232. A triggering statement from an external database server can activate the trigger. As the following example shows, an Insert trigger on newtab, managed by dbserver1, is activated by an INSERT statement from dbserver2. The trigger executes as if the INSERT originated on dbserver1.
-- Trigger on stores_demo@dbserver1:newtab CREATE TRIGGER ins_tr INSERT ON newtab REFERENCING new AS post_ins FOR EACH ROW(EXECUTE PROCEDURE nt_pct (post_ins.mc)); -- Triggering statement from dbserver2 INSERT INTO stores_demo@dbserver1:newtab SELECT item_num, order_num, quantity, stock_num, manu_code, total_price FROM items;
Dynamic Server also supports INSTEAD OF triggers on views, which are initiated when a triggering DML operation references the specified view. The INSTEAD OF trigger replaces the trigger event with the specified trigger action on a view, rather than execute the triggering INSERT, DELETE, or UPDATE operation. A view can have no more than one INSTEAD OF trigger defined for each type of event (INSERT, DELETE, or UPDATE). You can, however, define a trigger on one or more other views, each with its own INSTEAD OF trigger.
2-221
CREATE TRIGGER
The execution time for a trigger event depends on the complexity of the trigger action and whether it initiates other triggers. The time increases as the number of cascading triggers increases. For more information on triggers that initiate other triggers, see Cascading Triggers on page 2-240.
Element table
An Insert trigger is activated when an INSERT statement includes the specified table (or a synonym for table) in its INTO clause. Similarly, a Delete trigger is activated when a DELETE statement includes the specified table (or a synonym for table) in its FROM clause. For triggers on views, the INSTEAD OF keywords must immediately precede the INSERT, DELETE, or UPDATE keyword that specifies the type of trigger event, and the name or synonym of a view (rather than of a table) must follow the ON keyword. The section INSTEAD OF Triggers on Views (IDS) on page 2-243 describes the syntax for defining INSTEAD OF trigger events. No more than one Insert trigger, and no more than one Delete trigger, can be defined on one table. If you define a trigger on a subtable within a table hierarchy, and the subtable supports cascading deletes, then a DELETE operation on the supertable activates the Delete trigger on the subtable. See also the section Re-Entrancy of Triggers on page 2-236 for information about dependencies and restrictions on the actions of Insert triggers and Delete triggers.
UPDATE Event
UPDATE events (and SELECT events) can include an optional column list. UPDATE Event:
UPDATE , OF column ON table
Description Column that activates the trigger Name of the triggering table
Restrictions Must exist in the triggering table Must exist in the database
2-222
CREATE TRIGGER
If you define more than one Update trigger on the same table, the column list is required, and the column lists for each trigger must be mutually exclusive. If you omit the OF column list, updating any column of table activates the trigger. The OF column clause is not valid for an INSTEAD OF trigger on a view. An UPDATE on the triggering table can activate the trigger in two cases: v The UPDATE statement references any column in the column list. v The UPDATE event definition has no OF column list specification. Whether it updates one column or more than one column from the column list, a triggering UPDATE statement activates the Update trigger only once.
When an UPDATE statement updates multiple columns that have different triggers, the column numbers of the triggering columns determine the order of trigger execution. Execution begins with the smallest triggering column number and proceeds in order to the largest triggering column number. The following example shows that table taba has four columns (a, b, c, d):
CREATE TABLE taba (a int, b int, c int, d int)
Define trig1 as an update on columns a and c, and define trig2 as an update on columns b and d, as the following example shows:
CREATE TRIGGER trig1 UPDATE OF a, c ON taba AFTER (UPDATE tabb SET y = y + 1); CREATE TRIGGER trig2 UPDATE OF b, d ON taba AFTER (UPDATE tabb SET z = z + 1);
The following example shows a triggering statement for the Update trigger:
UPDATE taba SET (b, c) = (b + 1, c + 1)
Then trig1 for columns a and c executes first, and trig2 for columns b and d executes next. In this case, the smallest column number in the two triggers is column 1 (a), and the next is column 2 (b).
2-223
CREATE TRIGGER
SELECT , OF column ON table
Description Column that activates the trigger Name of the triggering table
Restrictions Must exist in the triggering table Must exist in the database
If you define more than one Select trigger on the same table, the column list is required, and the column lists for each trigger must be mutually exclusive. A SELECT on the triggering table can activate the trigger in two cases: v The SELECT statement references any column in the column list. v The SELECT event definition has no OF column list specification. (Sections that follow, however, describe additional circumstances that can affect whether or not a SELECT statement activates a Select trigger.) Whether it specifies one column or more than one column from the column list, a triggering SELECT statement activates the Select trigger only once. The action of a Select trigger cannot include an UPDATE, INSERT, or DELETE on the triggering table. The action of a Select trigger can include UPDATE, INSERT, and DELETE actions on tables other than the triggering table. The following example defines a Select trigger on one column of a table:
CREATE TRIGGER mytrig SELECT OF cola ON mytab REFERENCING OLD AS pre FOR EACH ROW (INSERT INTO newtab(for each action))
2-224
CREATE TRIGGER
For example, if a Select trigger is defined to execute whenever column col1 of table tab1 is selected, then both of the following stand-alone SELECT statements activate the Select trigger:
SELECT * FROM tab1; SELECT col1 FROM tab1;
Now suppose that the following SELECT statement invokes the my_rtn UDR in its select list:
SELECT my_rtn() FROM tab2
This SELECT statement activates the Select trigger defined on column col1 of table tab1 when the my_rtn UDR is executed.
Now suppose that the following statement invokes the my_rtn procedure:
EXECUTE PROCEDURE my_rtn()
This statement activates the Select trigger defined on column col1 of table tab1 when the SELECT statement within the statement block is executed.
2-225
CREATE TRIGGER
If you add a Select trigger to a subtable, this Select trigger can override the Select trigger that the subtable inherits from its supertable. For example, if the Select trigger trig1 is defined on column col1 in supertable tab1, the subtable tab2 inherits this trigger. But if you define a Select trigger named trig2 on column col1 in subtable tab2, and a SELECT statement selects from col1 in supertable tab1, this SELECT statement activates trigger trig1 for the rows in table tab1 and trigger trig2 (not trigger trig1) for the rows in table tab2. In other words, the trigger that you add to the subtable overrides the trigger that the subtable inherits from the supertable.
v A SELECT statement that includes the UNION or UNION ALL operator does not activate a Select trigger. v The SELECT clause of INSERT does not activate a Select trigger. v If the Projection clause of a SELECT includes the DISTINCT or UNIQUE keywords, the SELECT statement does not activate a Select trigger. v Select triggers are not supported on scroll cursors. v If a SELECT statement refers to a remote triggering table, the Select trigger is not activated on the remote database server. v Columns in the ORDER BY list of a query activate no Select triggers (nor any other triggers) unless also listed in the Projection clause.
Action Clause
Action Clause:
(1) BEFORE Triggered Action List (1) FOR EACH ROW Triggered Action List FOR EACH ROW Triggered Action List
(1)
2-226
CREATE TRIGGER
Notes: 1 See page Triggered-Action List on page 2-232 The action clause defines trigger actions and can specify when they occur. You must define at least one trigger action, using the keywords BEFORE, FOR EACH ROW, or AFTER to indicate when the action occurs relative to execution of the triggering statement. You can specify actions for any or all of these three options on a single trigger, but any BEFORE action list must be specified first, and any AFTER action list must be specified last. For more information on the action clause when a REFERENCING clause is also specified, see Correlated Table Action on page 2-231.
BEFORE Actions
The list of BEFORE trigger actions execute once before the triggering statement executes. Even if the triggering statement does not process any rows, the database server executes the BEFORE trigger actions.
AFTER Actions
The specified set of AFTER trigger actions executes once after the action of the triggering statement is complete. If the triggering statement does not process any rows, the AFTER trigger actions still execute.
Next, assume that you define trig1 on columns a and c, and trig2 on columns b and d. If both triggers specify BEFORE, FOR EACH ROW, and AFTER actions, then the trigger actions are executed in the following order: 1. BEFORE action list for trigger (a, c) 2. BEFORE action list for trigger (b, d) 3. FOR EACH ROW action list for trigger (a, c) 4. FOR EACH ROW action list for trigger (b, d) 5. AFTER action list for trigger (a, c) 6. AFTER action list for trigger (b, d)
2-227
CREATE TRIGGER
The database server treats all the triggers that are activated by the same triggering statement as a single trigger, and the trigger action is the merged-action list. All the rules that govern a trigger action apply to the merged list as one list, and no distinction is made between the two original triggers.
RERERENCING Clauses
The REFERENCING clause for any event declares a correlation name that can be used to qualify column values in the triggering table. These names enable FOR EACH ROW actions to reference new values in the result of trigger events. They also enable FOR EACH ROW actions to reference old column values that existed in the triggering table prior to modification by trigger events. Correlation names are not valid if the triggered action includes both the INSERT statement and the BEFORE WHEN or AFTER WHEN keywords. This restriction does not affect triggered actions that specify the FOR EACH ROW keywords without the BEFORE or AFTER keywords, or that include no INSERT statement.
2-228
CREATE TRIGGER
Element correlation Description Name declared here to qualify old column value for use within the trigger action Restrictions Syntax
The correlation is a qualifier for the column value in the triggering table before the triggering statement executed. The correlation is in scope in the FOR EACH ROW trigger action list. See Correlated Table Action on page 2-231. To use a correlation name in a trigger action to refer to an old column value, prefix the column name with the correlation name and a period ( . ) symbol. For example, if the NEW correlation name is post, refer to the new value for the column fname as post.fname. If the trigger event is a DELETE statement, using the new correlation name as a qualifier causes an error, because the column has no value after the row is deleted. For the rules that govern the use of correlation names, see Using Correlation Names in Triggered Actions on page 2-234. You can use the REFERENCING clause for Delete only if you define a FOR EACH ROW trigger action. In Extended Parallel Server, the OLD correlation value cannot be a BYTE or TEXT value. That is, it cannot refer to a BYTE or TEXT column.
Element correlation
Description Name that you declare here for a new column value for use within the trigger action
The correlation is a name for the new column value after the triggering statement has executed. Its scope of reference is only the FOR EACH ROW trigger action list; see Correlated Table Action on page 2-231. To use the correlation name, precede the column name with the correlation name, followed by a period ( . ) symbol. Thus, if the NEW correlation name is post, refer to the new value for the column fname as post.fname. If the trigger event is an INSERT statement, using the old correlation name as a qualifier causes an error, because no value exists before the row is inserted. For the rules that govern how to use correlation names, see Using Correlation Names in Triggered Actions on page 2-234. You can use the INSERT REFERENCING clause only if you define a FOR EACH ROW trigger action. The following example illustrates use of the INSERT REFERENCING clause. This example inserts a row into backup_table1 for every row that is inserted into table1. The values that are inserted into col1 and col2 of backup_table1 are an exact copy of the values that were just inserted into table1.
2-229
CREATE TRIGGER
CREATE TABLE table1 (col1 INT, col2 INT); CREATE TABLE backup_table1 (col1 INT, col2 INT); CREATE TRIGGER before_trig INSERT ON table1 REFERENCING NEW AS new FOR EACH ROW ( INSERT INTO backup_table1 (col1, col2) VALUES (new.col1, new.col2) );
As the preceding example shows, the INSERT REFERENCING clause allows you to refer to data values produced by the trigger action.
Notes: 1
Element correlation Description Name that you declare here for an old or new column value within the trigger action
The OLD correlation is the name of the value of the column in the triggering table before execution of the triggering statement; the NEW correlation identifies the corresponding value after the triggering statement executes. The scope of reference of the correlation names that you declare here is only within the FOR EACH ROW trigger action list. See Correlated Table Action on page 2-231. To refer to an old or new column value, prefix the column name with the correlation name and a period ( . ) symbol. For example, if the new correlation name is post, you can refer to the new value in column fname as post.fname. If the trigger event is an UPDATE statement, you can define both old and new correlation names to refer to column values before and after the triggering UPDATE statement. For rules that govern the use of correlation names, see Using Correlation Names in Triggered Actions on page 2-234. You can use the UPDATE REFERENCING clause only if you define a FOR EACH ROW trigger action. In Extended Parallel Server, the OLD correlation value cannot be a BYTE or TEXT value. That is, it cannot refer to a BYTE or TEXT column.
2-230
CREATE TRIGGER
Element correlation
Description Name that you declare here for old column value for use within the trigger action
This has the same syntax as the REFERENCING Clause for Delete on page 2-228. The scope of reference of the correlation name that you declare here is only within the FOR EACH ROW trigger action list. See Correlated Table Action on page 2-231. You use the correlation name to refer to an old column value by preceding the column name with the correlation name and a period ( . ) symbol. For example, if the old correlation name is pre, you can refer to the old value for the column fname as pre.fname. If the trigger event is a SELECT statement, using the new correlation name as a qualifier causes an error because the column does not have a new value after the column is selected. For the rules that govern the use of correlation names, see Using Correlation Names in Triggered Actions on page 2-234. You can use the SELECT REFERENCING clause only if you define a FOR EACH ROW trigger action.
If the CREATE TRIGGER statement contains an INSERT REFERENCING clause, a DELETE REFERENCING clause, an UPDATE REFERENCING clause, or (for Dynamic Server only) a SELECT REFERENCING clause, you must include a FOR EACH ROW triggered-action list in the action clause. You can also include BEFORE and AFTER triggered-action lists, but they are optional.
Chapter 2. SQL Statements
2-231
CREATE TRIGGER
For information on the BEFORE, FOR EACH ROW, and AFTER triggered-action lists, see Action Clause on page 2-226 In Extended Parallel Server, you cannot specify FOR EACH ROW actions on tables that have globally-detached indexes.
Triggered-Action List
Triggered Action List:
, , (2) ( (1) WHEN ( Condition ) DELETE Statement (4) UPDATE Statement (5) EXECUTE PROCEDURE Statement (6) EXECUTE FUNCTION Statement INSERT Statement (3) )
(7)
Notes: 1 2 3 4 5 6 7 See page 4-5 See page 2-395 See page 2-275 See page 2-636 See page 2-336 Dynamic Server only See page 2-329
For a trigger on a table, the trigger action consists of an optional WHEN condition and the action statements. You can specify a triggered-action list for each WHEN clause, or you can specify a single list (of one or more trigger actions) if you include no WHEN clause. Database objects that are referenced explicitly in the trigger action or in the definition of the trigger event, such as tables, columns, and UDRs, must exist when the CREATE TRIGGER statement defines the new trigger. Attention: When you specify a date expression in the WHEN condition or in an action statement, make sure to specify four digits instead of two digits for the year. For more about abbreviated years, see the description of DBCENTURY in the IBM Informix Guide to SQL: Reference, which also describes how the behavior of some database objects can be affected by environment variable settings. Like fragmentation expressions, check constraints, and UDRs, triggers are stored in the system catalog with the creation-time settings of environment variables that can affect the evaluation of expressions like the WHEN condition. The database server ignores any subsequent changes to those settings when evaluating expressions in those database objects.
WHEN Condition
The WHEN condition makes the triggered action dependent on the outcome of a test. When you include a WHEN condition in a triggered action, the statements in
2-232
CREATE TRIGGER
the triggered action list execute only if the condition evaluates to true. If the WHEN condition evaluates to false or unknown, then the statements in the triggered action list are not executed. If the triggered action is in a FOR EACH ROW section, its condition is evaluated for each row. For example, the triggered action in the following trigger executes only if the condition in the WHEN clause is true:
CREATE TRIGGER up_price UPDATE OF unit_price ON stock REFERENCING OLD AS pre NEW AS post FOR EACH ROW WHEN(post.unit_price > pre.unit_price * 2) (INSERT INTO warn_tab VALUES(pre.stock_num, pre.order_num, pre.unit_price, post.unit_price, CURRENT))
An SPL routine that executes inside the WHEN condition carries the same restrictions as a UDR that is called in a data manipulation statement. That is, the SPL routine cannot contain certain SQL statements. For information on which statements are restricted, see Restrictions on SPL Routines in Data-Manipulation Statements on page 5-71.
Action Statements
The triggered-action statements can be INSERT, DELETE, UPDATE, EXECUTE FUNCTION, or EXECUTE PROCEDURE statements. If the action list contains multiple statements, and the WHEN condition is satisfied (or is absent), then these statements execute in the order in which they appear in the list. UDRs as Triggered Actions: User-defined functions and procedures can be triggered actions. In Dynamic Server, you can use the EXECUTE FUNCTION statement to call any user-defined function. Use the EXECUTE PROCEDURE statement to call any user-defined procedure. In Extended Parallel Server, you can use the EXECUTE PROCEDURE statement to execute any SPL routine. For restrictions on using SPL routines as triggered actions, see Rules for SPL Routines on page 2-238 and Triggers and SPL Routines on page 2-220. Achieving a Consistent Result: To guarantee that the triggering statement returns the same result with and without the triggered actions, make sure that the triggered actions in the BEFORE and FOR EACH ROW sections do not modify any table referenced in the following clauses: v WHERE clause v SET clause in the UPDATE statement v SELECT clause v EXECUTE PROCEDURE clause or EXECUTE FUNCTION clause in a multiple-row INSERT statement. Using Reserved Words: If you use the INSERT, DELETE, UPDATE, or EXECUTE reserved words as an identifier in any of the following clauses inside a triggered action list, you must qualify them by the owner name, the table name, or both: v FROM clause of a SELECT statement v INTO clause of the EXECUTE PROCEDURE or EXECUTE FUNCTION statement v GROUP BY clause
Chapter 2. SQL Statements
2-233
CREATE TRIGGER
v SET clause of the UPDATE statement. You get a syntax error if these keywords are not qualified when you use these clauses inside a triggered action. If you use the keyword as a column name, it must be qualified by the table name; for example, table.update. If both the table name and the column name are keywords, they must be qualified by the owner name (for example, owner.insert.update). If the owner name, table name, and column name are all keywords, the owner name must be in quotes; for example, delete.insert.update. (These are general rules regarding reserved words as identifiers, rather than special cases for triggers. Your code will be easier to read and to maintain if you avoid using the keywords of SQL as identifiers.) The only exception is when these keywords are the first table or column name in the list, and you do not have to qualify them. For example, delete in the following statement does not need to be qualified because it is the first column listed in the INTO clause:
CREATE TRIGGER t1 UPDATE OF b ON tab1 FOR EACH ROW (EXECUTE PROCEDURE p2() INTO delete, d)
The following statements show examples in which you must qualify the column name or the table name: v FROM clause of a SELECT statement
CREATE TRIGGER t1 INSERT ON tab1 BEFORE (INSERT INTO tab2 SELECT * FROM tab3, owner1.update)
In Dynamic Server, an INSTEAD OF trigger on a view cannot include the EXECUTE PROCEDURE INTO statement among its triggered actions. v GROUP BY clause of a SELECT statement
CREATE TRIGGER t4 DELETE ON tab1 BEFORE (INSERT INTO tab3 SELECT deptno, SUM(exp) FROM budget GROUP BY deptno, budget.update)
2-234
CREATE TRIGGER
For the statement to be valid, both col_c and col_c2 must be columns from tab1. If col_c2 is intended to be a correlation reference to a column in the triggering table, it must be qualified by either the old or the new correlation name. If col_c2 is not a column in tab1 and is not qualified by either the old or new correlation name, you get an error. In a statement that is valid independent of the triggered action, a column name with no correlation qualifier refers to the current value in the database. In the triggered action for trigger t1 in the next example, mgr in the WHERE clause of the correlated subquery is an unqualified column in the triggering table. In this case, mgr refers to the current column value in empsal because the INSERT statement is valid independent of the triggered action.
CREATE CREATE CREATE CREATE DATABASE db1; TABLE empsal (empno INT, salary INT, mgr INT); TABLE mgr (eno INT, bonus INT); TABLE biggap (empno INT, salary INT, mgr INT);
CREATE TRIGGER t1 UPDATE OF salary ON empsal AFTER (INSERT INTO biggap SELECT * FROM empsal WHERE salary < (SELECT bonus FROM mgr WHERE eno = mgr));
In a triggered action, an unqualified column name from the triggering table refers to the current column value, but only when the triggered statement is valid independent of the triggered action.
Refer to the following key when you read the previous table. Term Original value Meaning Value before the triggering statement
Chapter 2. SQL Statements
2-235
CREATE TRIGGER
Current value (N) (U) Value after the triggering statement Cannot be changed by triggered action Can be updated by triggered actions; updated value might be different from the original value because of preceding triggered actions.
Outside a FOR EACH ROW triggered-action list, you cannot qualify a column from the triggering table with either the old correlation name or the new correlation name; it always refers to the current value in the database. In Dynamic Server, statements in the trigger action list use whatever collating order was in effect when the trigger was created, even if a different collation is in effect when the trigger action is executed. See SET COLLATION for details of how to specify a collating order different from what DB_LOCALE specifies.
Re-Entrancy of Triggers
In some cases a trigger can be re-entrant. In these cases the triggered action can reference the triggering table. In other words, both the trigger event and the triggered action can operate on the same table. The following list summarizes the situations in which triggers can be re-entrant and the situations in which triggers cannot be re-entrant: v The trigger action of an Update trigger cannot be an INSERT or DELETE of the table that the trigger event updated. v Similarly, the trigger action of an Update trigger cannot be an UPDATE of a column that the trigger event updated. (But the trigger action of an Update trigger can update a column that was not updated by the trigger event.) For example, assume that the following UPDATE statement, which updates columns a and b of tab1, is the triggering statement:
UPDATE tab1 SET (a, b) = (a + 1, b + 1)
Now consider the trigger actions in the following example. The first UPDATE statement is a valid trigger action, but the second one is not, because it updates column b again.
UPDATE tab1 SET c = c + 1; UPDATE tab1 SET b = b + 1; -- OK -- INVALID
v If the trigger has an UPDATE event, the trigger action can be an EXECUTE PROCEDURE or EXECUTE FUNCTION statement with an INTO clause that references a column that was updated by the trigger event or any other column in the triggering table. When an EXECUTE PROCEDURE or EXECUTE FUNCTION statement is the trigger action, the INTO clause for an UPDATE trigger is valid only in FOR EACH ROW trigger actions, and column names that appear in the INTO clause must be from the triggering table. The following statement illustrates the appropriate use of the INTO clause:
CREATE TRIGGER upd_totpr UPDATE OF quantity ON items REFERENCING OLD AS pre_upd NEW AS post_upd FOR EACH ROW(EXECUTE PROCEDURE calc_totpr(pre_upd.quantity,post_upd.quantity, pre_upd.total_price) INTO total_price)
The column that follows the INTO keyword must be in the triggering table, but need not have been updated by the trigger event. When the INTO clause appears in the EXECUTE PROCEDURE or EXECUTE FUNCTION statement, the database server updates the specified columns with values returned from the UDR, immediately upon returning from the UDR.
2-236
CREATE TRIGGER
v If the trigger has an INSERT event, the trigger action cannot be an INSERT or DELETE statement that references the triggering table. v If the trigger has an INSERT event, the trigger action can be an UPDATE statement that references a column in the triggering table, but this column cannot be a column for which a value was supplied by the trigger event. If the trigger has an INSERT event, and the trigger action updates the triggering table, the columns in both statements must be mutually exclusive. For example, assume that the triggering statement inserts values for columns cola and colb of table tab1:
INSERT INTO tab1 (cola, colb) VALUES (1,10)
Now consider the following trigger actions. The first UPDATE is valid, but the second one is not, because it updates column colb even though the trigger event already supplied a value for column colb:
UPDATE tab1 SET colc=100; --OK UPDATE tab1 SET colb=100; --INVALID
v If the trigger has an INSERT event, the trigger action can be an EXECUTE PROCEDURE or EXECUTE FUNCTION statement with an INTO clause that references a column that was supplied by the trigger event or a column that was not supplied by the trigger event. When an EXECUTE PROCEDURE or EXECUTE FUNCTION statement is the trigger action, you can specify the INTO clause for an INSERT trigger only when the trigger action occurs in the FOR EACH ROW list. In this case, the INTO clause can contain only column names from the triggering table. The following statement illustrates the valid use of the INTO clause:
CREATE TRIGGER ins_totpr INSERT ON items REFERENCING NEW AS new_ins FOR EACH ROW (EXECUTE PROCEDURE calc_totpr (0, new_ins.quantity, 0) INTO total_price).
The column that follows the INTO keyword can be a column in the triggering table that was supplied by the trigger event, or a column in the triggering table that was not supplied by the trigger event. When the INTO clause appears in the EXECUTE PROCEDURE or (for Dynamic Server only) the EXECUTE FUNCTION statement, the database server immediately updates the specified columns with values returned from the UDR. v If the trigger action is a SELECT statement, the SELECT statement can reference the triggering table. The SELECT statement can be a trigger action in the following instances: The SELECT statement appears in a subquery in the WHEN clause or in a trigger-action statement. The trigger action is a UDR, and the SELECT statement appears inside the UDR.
In the cascading triggers of the next example, trig2 fails at runtime because it references column b, which the triggering UPDATE statement updates:
Chapter 2. SQL Statements
2-237
CREATE TRIGGER
CREATE TRIGGER trig1 UPDATE OF a ON tab1-- Valid AFTER (UPDATE tab2 set e = e + 1); CREATE TRIGGER trig2 UPDATE of e ON tab2-- Invalid AFTER (UPDATE tab1 set b = b + 1);
Now consider the following SQL statements. When the final UPDATE statement is executed, column a is updated and the trigger trig1 is activated. The trigger action again updates column a with an EXECUTE PROCEDURE INTO statement.
CREATE TABLE temp1 (a int, b int, e int); INSERT INTO temp1 VALUES (10, 20, 30); CREATE PROCEDURE proc(val int) RETURNING int,int; RETURN val+10, val+20; END PROCEDURE; CREATE TRIGGER trig1 UPDATE OF a ON temp1 FOR EACH ROW (EXECUTE PROCEDURE proc(50) INTO a, e); CREATE TRIGGER trig2 UPDATE OF e ON temp1 FOR EACH ROW (EXECUTE PROCEDURE proc(100) INTO a, e); UPDATE temp1 SET (a,b) = (40,50);
In Extended Parallel Server, to re-create this example, use the CREATE PROCEDURE statement instead of the CREATE FUNCTION statement. Several questions arise from this example of cascading triggers. First, should the update of column a activate trigger trig1 again? The answer is no. Because the trigger was activated, it is not activated a second time. If the trigger action is an EXECUTE PROCEDURE INTO or EXECUTE FUNCTION INTO statement, the only triggers that are activated are those that are defined on columns that are mutually exclusive from the columns updated until then (in the cascade of triggers) in that table. Other triggers are ignored. Another question that arises from the example is whether trigger trig2 should be activated. The answer is yes. The trigger trig2 is defined on column e. Until now, column e in table temp1 has not been modified. Trigger trig2 is activated. A final question that arises from the example is whether triggers trig1 and trig2 should be activated after the trigger action in trig2 is performed. The answer is no. Neither trigger is activated. By this time columns a and e have been updated once, and triggers trig1 and trig2 have been executed once. The database server ignores and does not activate these triggers. For more about cascading triggers, see Cascading Triggers on page 2-240. In Dynamic Server, as noted earlier, an INSTEAD OF trigger on a view cannot include the EXECUTE PROCEDURE INTO statement among its trigger actions. In addition, an error results if two views each have INSERT INSTEAD OF triggers with actions defined to perform insert operations on the other view.
2-238
CREATE TRIGGER
v You cannot use the old or new correlation name inside the SPL routine. If you need to use the corresponding values in the routine, you must pass them as parameters. The routine should be independent of triggers, and the old or new correlation name does not have any meaning outside the trigger. When you use an SPL routine as a trigger action, the database objects that the routine references are not checked until the routine is executed. See also the SPL restrictions in Triggers and SPL Routines on page 2-220.
2-239
CREATE TRIGGER
can the owner of the view grant privileges to other users. When the trigger on the complex view is dropped, all of these privileges are revoked.
Cascading Triggers
In this section and in sections that follow, references to nonlogging databases do not apply to Extended Parallel Server. (In Extended Parallel Server, all databases support transaction logging.) The database server allows triggers other than Select triggers to cascade, meaning that the trigger actions of one trigger can activate another trigger. (For further information on the restriction against cascading Select triggers, see Circumstances When a Select Trigger is Activated on page 2-224.) The maximum number of triggers in a cascading series is 61: the initial trigger plus a maximum of 60 cascading triggers. When the number of cascading triggers in a series exceeds the maximum, the database server returns error number -748, with the following message:
Exceeded limit on maximum number of cascaded triggers.
The next example illustrates a series of cascading triggers that enforce referential integrity on the manufact, stock, and items tables in the stores_demo database. When a manufacturer is deleted from the manufact table, the first trigger, del_manu, deletes all the items of that manufacturer from the stock table. Each DELETE in the stock table activates a second trigger, del_items, that deletes all items of that manufacturer from the items table. Finally, each DELETE in the items table triggers SPL routine log_order, creating a record of any orders in the orders table that can no longer be filled.
CREATE TRIGGER del_manu DELETE ON manufact REFERENCING OLD AS pre_del FOR EACH ROW(DELETE FROM stock WHERE manu_code = pre_del.manu_code); CREATE TRIGGER del_stock DELETE ON stock REFERENCING OLD AS pre_del FOR EACH ROW(DELETE FROM items WHERE manu_code = pre_del.manu_code); CREATE TRIGGER del_items DELETE ON items REFERENCING OLD AS pre_del FOR EACH ROW(EXECUTE PROCEDURE log_order(pre_del.order_num));
When you are not using logging, referential integrity constraints on both the manufact and stock tables prohibit the triggers in this example from executing. When you use logging, however, the triggers execute successfully because constraint checking is deferred until all the trigger actions are complete, including the actions of cascading triggers. For more information about how constraints are handled when triggers execute, see Constraint Checking on page 2-241. The database server prevents loops of cascading triggers by not allowing you to modify the triggering table in any cascading trigger action, except with an UPDATE statement that does not modify any column that the triggering UPDATE
2-240
CREATE TRIGGER
statement updated, or with an INSERT statement. An INSERT trigger can define UPDATE trigger actions on the same table.
Constraint Checking
When you use logging, the database server defers constraint checking on the triggering statement until after the statements in the triggered-action list execute. This is equivalent to executing a SET CONSTRAINTS ALL DEFERRED statement before executing the triggering statement. After the trigger action is completed, the database server effectively executes a SET CONSTRAINTS constraint IMMEDIATE statement to check the constraints that were deferred. This action allows you to write triggers so that the trigger action can resolve any constraint violations that the triggering statement creates. For more information, see SET Database Object Mode on page 2-539. Consider the following example, in which the table child has constraint r1, which references the table parent. You define trigger trig1 and activate it with an INSERT statement. In the trigger action, trig1 checks to see if parent has a row with the value of the current cola in child; if not, it inserts it.
CREATE TABLE parent (cola INT PRIMARY KEY); CREATE TABLE child (cola INT REFERENCES parent CONSTRAINT r1); CREATE TRIGGER trig1 INSERT ON child REFERENCING NEW AS new FOR EACH ROW WHEN((SELECT COUNT (*) FROM parent WHERE cola = new.cola) = 0) -- parent row does not exist (INSERT INTO parent VALUES (new.cola));
When you insert a row into a table that is the child table in a referential constraint, the row might not exist in the parent table. The database server does not immediately return this error on a triggering statement. Instead, it allows the trigger action to resolve the constraint violation by inserting the corresponding row into the parent table. As the previous example shows, you can check within the trigger action to see whether the parent row exists, and if so, you can provide logic to bypass the INSERT action. For a database without logging, the database server does not defer constraint checking on the triggering statement. In this case, the database server immediately returns an error if the triggering statement violates a constraint. You cannot use the SET Transaction Mode statement in a trigger action. The database server checks this restriction when you activate a trigger, because the statement could occur inside a UDR. In Extended Parallel Server, rows that cause constraint violations might appear in the violations table, even if a subsequent trigger action corrects the violation.
2-241
CREATE TRIGGER
is different from having the database server apply the actions of individual triggers, and it has the following disadvantages: v If the triggering UPDATE statement sets a column to the current value, you cannot detect the UPDATE, so the trigger action is skipped. You might wish to execute the trigger action, even though the value of the column has not changed. v If the trigger has a BEFORE action, it applies to all columns, because you cannot yet detect whether a column has changed.
External Tables
The trigger action can affect tables of other database servers. The following example shows an Update trigger on dbserver1, which triggers an UPDATE to items on dbserver2:
CREATE TRIGGER upd_nt UPDATE ON newtab REFERENCING new AS post FOR EACH ROW(UPDATE stores_demo@dbserver2:items SET quantity = post.qty WHERE stock_num = post.stock AND manu_code = post.mc)
If, however, a statement from an external database server initiates a trigger whose action affects tables in an external database, the trigger actions fail. For example, the following combination of trigger action and triggering statement results in an error when the triggering statement executes:
-- Trigger action from dbserver1 to dbserver3: CREATE TRIGGER upd_nt UPDATE ON newtab REFERENCING new AS post FOR EACH ROW(UPDATE stores_demo@dbserver3:items SET quantity = post.qty WHERE stock_num = post.stock AND manu_code = post.mc); -- Triggering statement from dbserver2: UPDATE stores_demo@dbserver1:newtab SET qty = qty * 2 WHERE s_num = 5 AND mc = ANZ;
2-242
CREATE TRIGGER
ON EXCEPTION IN (-201) INSERT INTO logtab values (errno, errstr); RAISE EXCEPTION -201 END EXCEPTION
When the RAISE EXCEPTION statement returns the error, however, the database server rolls back this INSERT because it is part of the trigger actions. If the UDR is executed outside a trigger action, the INSERT is not rolled back. The UDR that implements a trigger action cannot contain any BEGIN WORK, COMMIT WORK, or ROLLBACK WORK statements. If the database has transaction logging, you must either begin an explicit transaction before the triggering statement, or the statement itself must be an implicit transaction. In any case, no other transaction-related statement is valid inside the UDR. You can use triggers to enforce referential actions that the database server does not currently support. In a database without logging, you are responsible for maintaining data integrity when the triggering statement fails.
Trigger on a View
Trigger on a View:
INSERT ON view REFERENCING NEW AS DELETE ON view REFERENCING OLD AS UPDATE ON view REFERENCING OLD AS REFERENCING NEW AS new OLD AS old old old new FOR EACH ROW
2-243
CREATE TRIGGER
Element old new trigger view Description Name for old value in view column Name for new value in view column Name declared here for the trigger Name or synonym of the triggering view. Can include owner. qualifier. Restrictions Must be unique in this statement Must be unique in this statement Must be unique among the names of triggers in the database The view or synonym must exist in the current database Syntax Identifier, p. 5-22 Identifier, p. 5-22 Database Object Name, p. 5-17 Database Object Name, p. 5-17
You can use the trigger action to update the tables underlying the view, in some cases updating an otherwise non-updatable view. You can also use INSTEAD OF triggers to substitute other actions when INSERT, DELETE, or UPDATE statements reference specific columns within the database. In the optional REFERENCING clause of an INSTEAD OF UPDATE trigger, the new correlation name can appear before or after the old correlation name. The specified view is sometimes called the triggering view. The left-hand portion of this diagram (including the view specification) defines the trigger event. The rest of the diagram defines correlation names and the trigger action.
Notes: 1 2 3 4 See page 2-395 See page 2-275 See page 2-636 See page 2-336
5 See page 2-329 This is not identical to the syntax of the trigger action for a trigger on a table, as described in the section Triggered-Action List on page 2-232. Because no WHEN (condition) is supported, the same trigger action is executed whenever the INSTEAD OF trigger event is encountered, and only one action list can be specified, rather than a separate list for each condition.
2-244
CREATE TRIGGER
Updating Views
INSERT, DELETE, or UPDATE statements can directly modify a view only if all of the following are true of the SELECT statement that defines the view: v All of the columns in the view are from a single table. v No columns in the projection list are aggregate values. v No UNIQUE or DISTINCT keyword is in the SELECT projection list. v No GROUP BY clause nor UNION operator is in the view definition. v The query selects no calculated values and no literal values. By using INSTEAD OF triggers, however, you can circumvent these restrictions on the view, if the trigger action modifies the base table.
The next statement defines manager_info, a view of columns in the dept and emp tables that includes all the managers of each department:
Chapter 2. SQL Statements
2-245
CREATE TRIGGER
CREATE VIEW manager_info AS SELECT d.deptno, d.deptname, e.empno, e.empname FROM emp e, dept d WHERE e.empno = d.manager_num;
The following CREATE TRIGGER statement creates manager_info_insert, an INSTEAD OF trigger that is designed to insert rows into the dept and emp tables through the manager_info view:
CREATE TRIGGER manager_info_insert INSTEAD OF INSERT ON manager_info --defines trigger event REFERENCING NEW AS n --new manager data FOR EACH ROW --defines trigger action (EXECUTE PROCEDURE instab(n.deptno, n.empno)); CREATE PROCEDURE instab (dno INT, eno INT) INSERT INTO dept(deptno, manager_num) VALUES(dno, eno); INSERT INTO emp (empno, deptno) VALUES (eno, dno); END PROCEDURE;
After the tables, view, trigger, and SPL routine have been created, the database server treats the following INSERT statement as a triggering event:
INSERT INTO manager_info(deptno, empno) VALUES (08, 4232);
This triggering INSERT statement is not executed, but this event causes the trigger action to be executed instead, invoking the instab( ) SPL routine. The INSERT statements in the SPL routine insert new values into both the emp and dept base tables of the manager_info view.
Related Information
Related statements: CREATE PROCEDURE, CREATE VIEW,DROP TRIGGER, EXECUTE PROCEDURE, and SET Database Object Mode For a task-oriented discussion of triggers, and for examples of INSTEAD OF DELETE (and UPDATE) triggers on views, see the IBM Informix Guide to SQL: Tutorial. For performance implications of triggers, see your IBM Informix Performance Guide.
2-246
CREATE TRIGGER
CREATE VIEW
Use the CREATE VIEW statement to create a new view that is based on one or more existing tables and views that reside in the database, or in another database of the local database server or of a different database server.
Syntax
CREATE VIEW view , ( (1) OF TYPE row_type column ) AS
Notes: 1 2
Element column Description Name that you declare here for a column in view. Default is a column name from Projection list of SELECT. Named-row type for typed view Name that you declare here for the view
row_type view
Must be unique among view, table, Database Object sequence, and synonym names in the Name, p. 5-17 database.
Usage
A view is a virtual table, defined by a SELECT statement. Except for the statements in the following list, you can specify the name or synonym of a view in any SQL statement where the name of a table is syntactically valid: v ALTER FRAGMENT v CREATE INDEX v CREATE TABLE v CREATE TRIGGER v RENAME TABLE v START VIOLATIONS TABLE v STOP VIOLATIONS TABLE v TRUNCATE v UPDATE STATISTICS You must specify the name of a view when you use the CREATE TRIGGER statement to define an INSTEAD OF trigger on a view, but the syntax and functionality are different from those of a trigger defined on a table.
2-247
CREATE VIEW
Updating Through Views on page 2-251 prohibits non-updatable views in INSERT, DELETE, or UPDATE statements (where other views are valid). To create a view, you must have the Select privilege on all columns from which the view is derived. You can query a view as if it were a table, and in some cases, you can update it as if it were a table; but a view is not a table. The view consists of the set of rows and columns that the SELECT statement in the view definition returns each time you refer to the view in a query. In some cases, the database server merges the SELECT statement of the user with the SELECT statement defining the view and executes the combined statements. In other cases, a query against a view might execute more slowly than expected, if the complexity of the view definition causes the database server to create a temporary table (referred to as a materialized view). For more information on materialized views, see the IBM Informix Performance Guide. The view reflects changes to the underlying tables with one exception. If a SELECT * specification defines the view, the view has only the columns that existed in the underlying tables when the view was defined by CREATE VIEW. Any new columns that are subsequently added to the underlying tables with the ALTER TABLE statement do not appear in the view. The view inherits the data types of the columns in the tables from which the view is derived. The database server determines data types of virtual columns from the nature of the expression. The SELECT statement is stored in the sysviews system catalog table. When you subsequently refer to a view in another statement, the database server performs the defining SELECT statement while it executes the new statement. In DBAccess, if you create a view outside the CREATE SCHEMA statement, you receive warnings if you use the -ansi flag or if you set the DBANSIWARN environment variable. The following statement creates a view that is based on the person table. When you create a view like this, which has no OF TYPE clause, the view is referred to as an untyped view.
CREATE VIEW v1 AS SELECT * FROM person
Typed Views
You can create typed views if you have Usage privileges on the named-ROW type or if you are its owner or the DBA. If you omit the OF TYPE clause, rows in the view are considered untyped and default to an unnamed-ROW type. Typed views, like typed tables, are based on a named-ROW type. Each column in the view corresponds to a field in the named-ROW type. The following statement creates a typed view that is based on the table person.
CREATE VIEW v2 OF TYPE person_t AS SELECT * FROM person
To create a typed view, you must include an OF TYPE clause. When you create a typed view, the named-ROW type that you specify immediately after the OF TYPE keywords must already exist.
2-248
CREATE VIEW
Union Views
A view that contains a UNION or UNION ALL operator in its SELECT statement is known as a union view. Certain restrictions apply to union views: v If a CREATE VIEW statement defines a union view, you cannot specify the WITH CHECK OPTION keywords in the CREATE VIEW statement. v All restrictions that apply to UNION or UNION ALL operations in stand-alone SELECT statements also apply to UNION and UNION ALL operations in the SELECT statement of a union view. For a list of these restrictions, see Restrictions on a Combined SELECT on page 2-525. For an example of a CREATE VIEW statement that defines a union view, see Naming View Columns.
You must specify at least one column name in the following circumstances: v If you provide names for some of the columns in a view, then you must provide names for all the columns. That is, the column list must contain an entry for every column that appears in the view. v If the SELECT statement returns an expression, the corresponding column in the view is called a virtual column. You must provide a name for a virtual column. In the following example, the user must specify the column parameter because the select list of the Projection clause of the SELECT statement contains an aggregate expression:
CREATE VIEW newview (firstcol, secondcol) AS SELECT sum(cola), colb FROM oldtab
v You must also specify column names in cases where any of the selected columns have duplicate column names without the table qualifiers. For example, if both
Chapter 2. SQL Statements
2-249
CREATE VIEW
orders.order_num and items.order_num appear in the SELECT statement, the CREATE VIEW statement, must provide two separate column names to label them:
CREATE VIEW someorders (custnum,ocustnum,newprice) AS SELECT orders.order_num,items.order_num, items.total_price*1.5 FROM orders, items WHERE orders.order_num = items.order_num AND items.total_price > 100.00
Here custnum and ocustnum replace the two identical column names. v The CREATE VIEW statement must also provide column names in the column list when the SELECT statement includes a UNION or UNION ALL operator and the names of the corresponding columns in the SELECT statements are not identical. For example, code in the following CREATE VIEW statement must specify the column list because the second column in the first SELECT statement has a different name from the second column in the second SELECT statement:
CREATE VIEW myview (cola, colb) AS SELECT colx, coly from firsttab UNION SELECT colx, colz from secondtab
You can insert into a view a row that does not satisfy the conditions of the view (that is, a row that is not visible through the view). You can also update a row of a view so that it no longer satisfies the conditions of the view. For example, if the view was created without the WITH CHECK OPTION keywords, you could insert a row through the view where the city is Los Altos, or you could update a row through the view by changing the city from Palo Alto to Los Altos. To prevent such inserts and updates, you can add the WITH CHECK OPTION keywords when you create the view. These keywords ask the database server to test every inserted or updated row to ensure that it meets the conditions that are set by the WHERE clause of the view. The database server rejects the operation with an error if the row does not meet the conditions. Even if the view was created with the WITH CHECK OPTION keywords, however, you can perform inserts and updates through the view to change
2-250
CREATE VIEW
columns that are not part of the view definition. A column is not part of the view definition if it does not appear in the WHERE clause of the SELECT statement that defines the view.
Related Information
Related statements: CREATE TABLE, CREATE TRIGGER, DROP VIEW, GRANT, REVOKE,SELECT, and SET SESSION AUTHORIZATION For a discussion of views, see the IBM Informix Database Design and Implementation Guide. For a discussion of how to use privileges and views to restrict access to the database, see the IBM Informix Guide to SQL: Tutorial.
2-251
3 3 3 3 3 3 3 3 3
CREATE XADATASOURCE
Use the CREATE XADATASOURCE statement to create a new XA-compliant data source and create an entry for it in the sysxadatasources system catalog table. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
CREATE XADATASOURCE xa_source USING xa_type
Description Name that you declare here for the new XA data source Name of an existing XA data source type
Restrictions Must be unique among XA data source names in sysxadatasources Must already exist in the database in the sysxasourcetypes system catalog table
Usage
An XA-compliant data source is an external data source that complies with the X/Open DTP XA Standard for managing interactions between a transaction manager and a resource manager. To register XA-compliant data sources in the database requires two SQL statements: v First create one or more XA-compliant data source types by using the CREATE XADATASOURCE TYPE statement. v Then create one or more instances of XA-compliant data sources with the CREATE XADATASOURCE statement. You can integrate transactions at the XA data source with the Dynamic Server transaction, using a 2-phase commit protocol to ensure that transactions are uniformly committed or rolled back among multiple database servers, and manage multiple external XA data sources within the same global transaction. Any user can create an XA data source, which follows standard owner-naming rules, according to the ANSI-compliance status of the database. Only a database that uses transaction logging can support the X/Open DTP XA Standard. Both XA data source types and instances of XA data sources are specific to one database. To support distributed transactions, they must be created in each database that interacts with the external XA data source. The following statement creates a new XA data source instance called informix.NewYork of type informix.MQSeries.
CREATE XADATASOURCE informix.NewYork USING informix.MQSeries;
Related Information
Related statements: CREATE XADATASOURCE TYPE, DROP XADATASOURCE, DROP XADATASOURCE TYPE For the schema of the sysxadatasources system catalog table, see the documentation notes to the IBM Informix Guide to SQL: Reference. For more information on XA data sources, see the IBM Informix DataBlade API Programmer's Guide.
2-252
3 3 3 3 3 3 3 3
Syntax
, (1) CREATE XADATASOURCE TYPE xa_type ( Purpose Options )
Usage
The CREATE XADATASOURCE TYPE statement adds an XA-compliant data source type to the database. Any user can create an XA data source type, whose owner-naming rules depend on the ANSI-compliance status of the database. Only a database that uses transaction logging can support the X/Open DTP XA Standard. To create a data source type, you must declare its name and specify purpose functions and purpose values as attributes of the XA source type. Most of the purpose options that follow the source type name associate columns in the sysxasourcetypes system catalog table with the name of a UDR. Both XA data source types and instances of XA data sources are specific to one database. To support distributed transactions, they must be created in each database that interacts with the external XA data source. The following statement creates a new XA data source type called MQSeries, owned by user informix.
CREATE XADATASOURCE TYPE informix.MQSeries( xa_flags = 1, xa_version = 0, xa_open = informix.mqseries_open, xa_close = informix.mqseries_close, xa_start = informix.mqseries_start, xa_end = informix.mqseries_end, xa_rollback = informix.mqseries_rollback, xa_prepare = informix.mqseries_prepare, xa_commit = informix.mqseries_commit, xa_recover = informix.mqseries_recover, xa_forget = informix.mqseries_forget, xa_complete = informix.mqseries_complete);
2-253
Related Information
Related statements: CREATE XADATASOURCE, DROP XADATASOURCE, DROP XADATASOURCE TYPE For information about how to enter purpose-option specifications, see Purpose Options on page 5-46. For information on how to use XA data sources, see the IBM Informix DataBlade API Programmer's Guide. For the schema of the sysxasourcetypes table, see the documentation notes to the IBM Informix Guide to SQL: Reference.
2-254
DATABASE
Use the DATABASE statement to open an accessible database as the current database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DATABASE database EXCLUSIVE
Element database
Usage
You can use the DATABASE statement to select any database on your database server. To select a database on another database server, specify the name of the database server with the database name. If you include the name of the current (or another) database server with the database name, the database server name cannot be uppercase. (See Database Name on page 5-15 for the syntax of specifying the database server name.) Issuing a DATABASE statement when a database is already open closes the current database before opening the new one. Closing the current database releases any cursor resources that the database server holds, invalidating any cursors that you have declared up to that point. If the user specification was changed through a SET SESSION AUTHORIZATION statement, the original user name is restored when the new database is opened. If a previous CONNECT statement has established an explicit connection to a database, and that connection is still your current connection, you cannot use the DATABASE statement (nor any SQL statement that creates an implicit connection) until after you use DISCONNECT to close the explicit connection. The current user (or PUBLIC) must have the Connect privilege on the database that is specified in the DATABASE statement. The current user cannot have the same user name as an existing role in the database. DATABASE is not a valid statement in multistatement PREPARE operations.
2-255
DATABASE
v The fifth field is set to W if database converts all floating-point data to DECIMAL format. (System lacks FLOAT and SMALLFLOAT support.) v The seventh field is set to W if database is the secondary server (that is, running in read-only mode) in a data-replication pair. v The eighth field is set to W if database has DB_LOCALE set to a locale different from the DB_LOCALE setting on the client system.
EXCLUSIVE Keyword
The EXCLUSIVE keyword opens the database in exclusive mode and prevents access by anyone but the current user. To allow others to access the database, you must first execute the CLOSE DATABASE statement and then reopen the database without the EXCLUSIVE keyword. The following statement opens the stores_demo database on the training database server in exclusive mode:
DATABASE stores_demo@training EXCLUSIVE
If another user has already opened the specified database, exclusive access is denied, an error is returned, and no database is opened.
Related Information
Related statements: CLOSE DATABASE, CONNECT, CREATE DATABASE, DISCONNECT, and SET CONNECTION For discussions of how to use different data models to design and implement a database, see the IBM Informix Database Design and Implementation Guide. For descriptions of the sqlca structure, see the IBM Informix Guide to SQL: Tutorial or the IBM Informix ESQL/C Programmer's Manual.
2-256
DEALLOCATE COLLECTION
Use the DEALLOCATE COLLECTION statement to release memory for a collection variable that was previously allocated with the ALLOCATE COLLECTION statement. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. Use this statement with ESQL/C.
Syntax
DEALLOCATE COLLECTION :variable
Element variable
Description Name that identifies a typed or untyped collection variable for which to deallocate memory
Restrictions Must be the name of an ESQL/C collection variable that has already been allocated
Usage
The DEALLOCATE COLLECTION statement frees all the memory that is associated with the ESQL/C collection variable that variable identifies. You must explicitly release memory resources for a collection variable with DEALLOCATE COLLECTION. Otherwise, deallocation occurs automatically at the end of the program. The DEALLOCATE COLLECTION statement releases resources for both typed and untyped collection variables. Tip: The DEALLOCATE COLLECTION statement deallocates memory for an ESQL/C collection variable only. To deallocate memory for an ESQL/C row variable, use the DEALLOCATE ROW statement. If you deallocate a nonexistent collection variable or a variable that is not an ESQL/C collection variable, an error results. Once you deallocate a collection variable, you can use the ALLOCATE COLLECTION to reallocate resources and you can then reuse a collection variable. This example shows how to deallocate resources with the DEALLOCATE COLLECTION statement for the untyped collection variable, a_set:
EXEC SQL BEGIN DECLARE SECTION; client collection a_set; EXEC SQL END DECLARE SECTION; . . . EXEC SQL allocate collection :a_set; . . . EXEC SQL deallocate collection :a_set;
Related Information
Related example: refer to the collection variable example in PUT. Related statements: ALLOCATE COLLECTION and DEALLOCATE ROW For a discussion of collection data types, see the IBM Informix ESQL/C Programmer's Manual.
2-257
DEALLOCATE DESCRIPTOR
Use the DEALLOCATE DESCRIPTOR statement to free a previously allocated, system-descriptor area. Use this statement with ESQL/C.
Syntax
DEALLOCATE DESCRIPTOR descriptor descriptor_var
Description
Restrictions
Syntax
Name of a system-descriptor Use single quotes. System-descriptor area Quoted String on area must already be allocated page 4-142 Host variable that contains the name of a system-descriptor area System-descriptor area must already be allocated, and the variable must already have been declared Name must conform to language-specific rules
Usage
The DEALLOCATE DESCRIPTOR statement frees all the memory that is associated with the system-descriptor area that descriptor or descriptor_var identifies. It also frees all the item descriptors (including memory for data items in the item descriptors). You can reuse a descriptor or descriptor variable after it is deallocated. Otherwise, deallocation occurs automatically at the end of the program. If you deallocate a nonexistent descriptor or descriptor variable, an error results. You cannot use the DEALLOCATE DESCRIPTOR statement to deallocate an sqlda structure. You can use it only to free the memory that is allocated for a system-descriptor area. The following examples show valid DEALLOCATE DESCRIPTOR statements. The first line uses an embedded-variable name, and the second line uses a quoted string to identify the allocated system-descriptor area.
EXEC SQL deallocate descriptor :descname; EXEC SQL deallocate descriptor desc1;
Related Information
Related statements: ALLOCATE DESCRIPTOR, DECLARE, DESCRIBE, EXECUTE, FETCH, GET DESCRIPTOR, OPEN, PREPARE, PUT, and SET DESCRIPTOR For more information on system-descriptor areas, refer to the IBM Informix ESQL/C Programmer's Manual.
2-258
DEALLOCATE ROW
Use the DEALLOCATE ROW statement to release memory for a ROW variable. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. Use this statement with ESQL/C.
Syntax
DEALLOCATE ROW :variable
Element variable
Usage
DEALLOCATE ROW frees all the memory that is associated with the ESQL/C typed or untyped row variable that variable identifies. If you do not explicitly release memory resources with DEALLOCATE ROW, deallocation occurs automatically at the end of the program. To deallocate memory for an ESQL/C collection variable, use the DEALLOCATE COLLECTION statement. After you deallocate a ROW variable, you can use the ALLOCATE ROW statement to reallocate resources, and you can then reuse a ROW variable. The following example shows how to deallocate resources for the ROW variable, a_row, using the DEALLOCATE ROW statement:
EXEC SQL EXEC SQL . . . EXEC SQL . . . EXEC SQL BEGIN DECLARE SECTION; row (a int, b int) a_row; END DECLARE SECTION; allocate row :a_row; deallocate row :a_row;
Related Information
Related statements: ALLOCATE ROW and DEALLOCATE COLLECTION For a discussion of ROW data types, see the IBM Informix Guide to SQL: Tutorial. For complex data types, see the IBM Informix ESQL/C Programmer's Manual.
2-259
DECLARE
Use the DECLARE statement to associate a cursor with a set of rows. Use this statement with ESQL/C.
Syntax
DECLARE cursor_id (1) cursor_id_var
(1) FOR Subset of INSERT Statement Select Options (3) FOR SELECT Statement statement_id (1) statement_id_var
(2)
(4) EXECUTE PROCEDURE Statement (5) (6) EXECUTE FUNCTION Statement (3) FOR SELECT Statement statement_id (1) statement_id_var
(4) EXECUTE PROCEDURE Statement (5) (6) EXECUTE FUNCTION Statement (7) SELECT with Collection-Derived Table (8) INSERT with Collection-Derived Table
Select Options:
FOR READ ONLY (1) FOR UPDATE , OF column
Notes: 1 2 3 4 5 6 7 8 Informix extension See Subset of INSERT Statement Associated with a Sequential Cursor on page 2-266 See SELECT on page 2-479 See EXECUTE PROCEDURE on page 2-336 Dynamic Server only See EXECUTE FUNCTION on page 2-329 See Select with a Collection-Derived Table on page 2-271 See Insert with a Collection-Derived Table on page 2-273
IBM Informix Guide to SQL: Syntax
2-260
DECLARE
9 See Subset of SELECT Statement Associated with Cursors on page 2-269
Description Column to update with cursor Name declared here for cursor Variable holding cursor_id Name of prepared statement Variable holding statement_id Restrictions Must exist, but need not be listed in Select list of Projection clause Must be unique among names of cursors and prepared objects Must have a character data type Declared in PREPARE statement Must have a character data type Syntax Identifier on page 5-22 Identifier on page 5-22 Language-specific Identifier on page 5-22 Language-specific
Usage
A cursor is an identifier that you associate with a group of rows. The DECLARE statement associates the cursor with one of the following database objects: v With an SQL statement, such as SELECT, EXECUTE FUNCTION (or EXECUTE PROCEDURE), or INSERT Each of these SQL statements creates a different type of cursor. For more information, see Overview of Cursor Types on page 2-262. v With the statement identifier (statement id or statement id variable) of a prepared statement You can prepare one of the previous SQL statements and associate the prepared statement with a cursor. For more information, see Associating a Cursor with a Prepared Statement on page 2-271. v Using Dynamic Server with a collection variable in an ESQL/C program The name of the collection variable appears in the FROM clause of a SELECT or the INTO clause of an INSERT. For more information, see Associating a Cursor with a Prepared Statement on page 2-271. DECLARE assigns an identifier to the cursor, specifies its uses, and directs the ESQL/C preprocessor to allocate storage for it. DECLARE must precede any other statement that refers to the cursor during program execution. The maximum length of a DECLARE statement is 64 kilobytes. The number of cursors and prepared objects that can exist concurrently in a single program is limited by the available memory. To avoid exceeding the limit, use the FREE statement to release some prepared statements or cursors. A program can consist of one or more source-code files. By default, the scope of reference of a cursor is global to a program, so a cursor that was declared in one source file can be referenced from a statement in another file. In a multiple-file program, if you want to limit the scope of cursor names to the files in which they are declared, you must preprocess all of the files with the -local command-line option. Multiple cursors can be declared for the same prepared statement identifier. For example, the following ESQL/C example does not return an error:
EXEC EXEC EXEC EXEC SQL SQL SQL SQL prepare declare declare declare id1 from x cursor y scroll z cursor select * from customer; for id1; cursor for id1; with hold for id1;
2-261
DECLARE
If you include the -ansi compilation flag (or if DBANSIWARN is set), warnings are generated for statements that use dynamic cursor names or dynamic statement identifiers and (for Dynamic Server only) statements that use collection-derived tables. Some error checking is performed at runtime, such as these typical checks: v Invalid use of sequential cursors as scroll cursors v Use of undeclared cursors v Invalid cursor names or statement names (empty) Checks for multiple declarations of a cursor of the same name are performed at compile time only if the cursor or statement is specified as an identifier. The following example uses a host variable to store the cursor name:
EXEC SQL declare x cursor for select * from customer; . . . stcopy("x", s); EXEC SQL declare :s cursor for select * from customer;
A cursor uses the collating order that was in effect when the cursor was declared, even if this is different from the collation of the session at runtime.
2-262
DECLARE
A select cursor is a data structure that represents a specific location within the active set of rows that the SELECT statement retrieved. v If you associate an EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement with a cursor, the cursor is called a function cursor. The function cursor represents the columns or values that a user-defined function returns. Function cursors behave the same as select cursors that are enabled as update cursors. In Extended Parallel Server, to create a function cursor, you must use the EXECUTE PROCEDURE statement. Extended Parallel Server does not support the EXECUTE FUNCTION statement. In Dynamic Server, for backward compatibility, if an SPL function was created with the CREATE PROCEDURE statement, you can create a function cursor with the EXECUTE PROCEDURE statement. With external functions, you must use the EXECUTE FUNCTION statement. When you associate a SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement with a cursor, the statement can include an INTO clause. However, if you prepare the SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement, you must omit the INTO clause in the PREPARE statement and use the INTO clause of the FETCH statement to retrieve the values from the collection cursor. A select or function cursor can scan returned rows of data and to move data row by row into a set of receiving variables, as the following steps describe: 1. DECLARE Use DECLARE to define a cursor and associate it with a statement. 2. OPEN Use OPEN to open the cursor. The database server processes the query until it locates or constructs the first row of the active set. 3. FETCH Use FETCH to retrieve successive rows of data from the cursor. 4. CLOSE Use CLOSE to close the cursor when its active set is no longer needed. 5. FREE Use FREE to release the resources that are allocated for the cursor.
2-263
DECLARE
In an ANSI-compliant database, the cursor associated with a SELECT statement through the DECLARE statement is an update cursor by default, provided that the SELECT statement conforms to all of the restrictions for update cursors listed in Subset of SELECT Statement Associated with Cursors on page 2-269. If you want a select cursor to be read-only, you must use the FOR READ ONLY keywords when you declare the cursor. The database server can use less stringent locking for a read-only cursor than for an update cursor. The following example creates a read-only cursor:
EXEC SQL declare z_curs cursor for select * from customer_ansi for read only;
In an update cursor, you can update or delete rows in the active set. After you create an update cursor, you can update or delete the currently selected row by using an UPDATE or DELETE statement with the WHERE CURRENT OF clause. The words CURRENT OF refer to the row that was most recently fetched; they take the place of the usual test expressions in the WHERE clause. An update cursor lets you perform updates that are not possible with the UPDATE statement because the decision to update and the values of the new data items can be based on the original contents of the row. Your program can evaluate or manipulate the selected data before it decides whether to update. The UPDATE statement cannot interrogate the table that is being updated. You can specify particular columns that can be updated. The columns need not appear in the Select list of the Projection clause. Using FOR UPDATE with a List of Columns: When you declare an update cursor, you can limit the update to specific columns by including the OF keyword and a list of columns. You can modify only those named columns in subsequent UPDATE statements. The columns need not be in the select list of the SELECT clause. The next example declares an update cursor and specifies that this cursor can update only the fname and lname columns in the customer_notansi table:
EXEC SQL declare name_curs cursor for select * from customer_notansi for update of fname, lname;
2-264
DECLARE
By default, unless declared as FOR READ ONLY, a select cursor in a database that is ANSI compliant is an update cursor, so the FOR UPDATE keywords are optional. If you want an update cursor to be able to modify only some of the columns in a table, however, you must specify these columns in the FOR UPDATE OF column list. The principal advantage to specifying columns is documentation and preventing programming errors. (The database server refuses to update any other columns.) An additional advantage is improved performance, when the SELECT statement meets the following criteria: v The SELECT statement can be processed using an index. v The columns that are listed are not part of the index that is used to process the SELECT statement. If the columns that you intend to update are part of the index that is used to process the SELECT statement, the database server keeps a list of each updated row, to ensure that no row is updated twice. If the OF keyword specifies which columns can be updated, the database server determines whether or not to keep the list of updated rows. If the database server determines that the work of keeping the list is unnecessary, performance improves. If you do not use the OF column list, the database server always maintains a list of updated rows, although the list might be unnecessary. The following example contains ESQL/C code that uses an update cursor with a DELETE statement to delete the current row. Whenever the row is deleted, the cursor remains between rows. After you delete data, you must use a FETCH statement to advance the cursor to the next row before you can refer to the cursor in a DELETE or UPDATE statement.
EXEC SQL declare q_curs cursor for select * from customer where lname matches :last_name for update; EXEC SQL open q_curs; for (;;) { EXEC SQL fetch q_curs into :cust_rec; if (strncmp(SQLSTATE, "00", 2) != 0) break; /* Display customer values and prompt for answer */ printf("\n%s %s", cust_rec.fname, cust_rec.lname); printf("\nDelete this customer? "); scanf("%s", answer); if (answer[0] == y) EXEC SQL delete from customer where current of q_curs; if (strncmp(SQLSTATE, "00", 2) != 0) break; } printf("\n"); EXEC SQL close q_curs;
Locking with an Update Cursor: The FOR UPDATE keywords notify the database server that updating is possible and cause it to use more stringent locking than with a select cursor. You declare an update cursor to let the database server know that the program might update (or delete) any row that it fetches as part of the SELECT statement. The update cursor employs promotable locks for rows that the program fetches. Other programs can read the locked row, but no other program can place a promotable lock (also called a write lock). Before the program modifies the row, the row lock is promoted to an exclusive lock.
Chapter 2. SQL Statements
2-265
DECLARE
It is possible to declare an update cursor with the WITH HOLD keywords, but the only reason to do so is to break a long series of updates into smaller transactions. You must fetch and update a particular row in the same transaction. If an operation involves fetching and updating a large number of rows, the lock table that the database server maintains can overflow. The usual way to prevent this overflow is to lock the entire table that is being updated. If this action is impossible, an alternative is to update through a hold cursor and to execute COMMIT WORK at frequent intervals. You must plan such an application carefully, however, because COMMIT WORK releases all locks, even those that are placed through a hold cursor.
The insert cursor simply inserts rows of data; it cannot be used to fetch data. When an insert cursor is opened, a buffer is created in memory. The buffer receives rows of data as the program executes PUT statements. The rows are written to disk only when the buffer is full. You can flush the buffer (that is, to write its contents into the database) when it is less than full, using the CLOSE, FLUSH, or COMMIT WORK statements. This topic is discussed further under the CLOSE, FLUSH, and PUT statements. You must close an insert cursor to insert any buffered rows into the database before the program ends. You can lose data if you do not close the cursor properly. For a complete description of INSERT syntax and usage, see INSERT on page 2-395.
Insert Cursor
When you associate an INSERT statement with a cursor, the cursor is called an insert cursor. An insert cursor is a data structure that represents the rows that the INSERT statement is to add to the database. The insert cursor simply inserts rows of data; it cannot be used to fetch data. To create an insert cursor, you associate a cursor with a restricted form of the INSERT statement. The INSERT statement must include a VALUES clause; it cannot contain an embedded SELECT statement. Create an insert cursor if you want to add multiple rows to the database in an INSERT operation. An insert cursor allows bulk insert data to be buffered in memory and written to disk when the buffer is full, as these steps describe: 1. Use DECLARE to define an insert cursor for the INSERT statement. 2. Open the cursor with the OPEN statement. The database server creates the insert buffer in memory and positions the cursor at the first row of the insert buffer. 3. Copy successive rows of data into the insert buffer with the PUT statement.
2-266
DECLARE
4. The database server writes the rows to disk only when the buffer is full. You can use the CLOSE, FLUSH, or COMMIT WORK statement to flush the buffer when it is less than full. This topic is discussed further under the PUT and CLOSE statements. 5. Close the cursor with the CLOSE statement when the insert cursor is no longer needed. You must close an insert cursor to insert any buffered rows into the database before the program ends. You can lose data if you do not close the cursor properly. 6. Free the cursor with the FREE statement. The FREE statement releases the resources that are allocated for an insert cursor. Using an insert cursor is more efficient than embedding the INSERT statement directly. This process reduces communication between the program and the database server and also increases the speed of the insertions. In addition to select and function cursors, insert cursors can also have the sequential cursor characteristic. To create an insert cursor, you associate a sequential cursor with a restricted form of the INSERT statement. (For more information, see Insert Cursor on page 2-266.) The following example contains IBM Informix ESQL/C code that declares a sequential insert cursor:
EXEC SQL declare ins_cur cursor for insert into stock values (:stock_no,:manu_code,:descr,:u_price,:unit,:u_desc);
Cursor Characteristics
You can declare a cursor as a sequential cursor (the default), a scroll cursor (by using the SCROLL keyword), or a hold cursor (by using the WITH HOLD keywords). The SCROLL and WITH HOLD keywords are not mutually exclusive. Sections that follow explain these structural characteristics. A select or function cursor can be either a sequential or a scroll cursor. An insert cursor can only be a sequential cursor. Select, function, and insert cursors can optionally be hold cursors.
In addition to select and function cursors, insert cursors can also have the sequential cursor characteristic. To create an insert cursor, you associate a sequential cursor with a restricted form of the INSERT statement. (For more information, see Insert Cursor on page 2-266.) The following example declares a sequential insert cursor:
Chapter 2. SQL Statements
2-267
DECLARE
EXEC SQL declare ins_cur cursor for insert into stock values (:stock_no,:manu_code,:descr,:u_price,:unit,:u_desc);
You can create scroll cursors for select and function cursors but not for insert cursors. Scroll cursors cannot be declared as FOR UPDATE.
You can use a select hold cursor as the following ESQL/C code example shows. This code fragment uses a hold cursor as a master cursor to scan one set of records and a sequential cursor as a detail cursor to point to records that are located in a different table. The records that the master cursor scans are the basis for updating the records to which the detail cursor points. The COMMIT WORK statement at the end of each iteration of the first WHILE loop leaves the hold cursor c_master open but closes the sequential cursor c_detail and releases all locks. This technique minimizes the resources that the database server must devote to locks and unfinished transactions, and it gives other users immediate access to updated rows.
EXEC SQL BEGIN DECLARE SECTION; int p_custnum, int save_status; long p_orddate; EXEC SQL END DECLARE SECTION; EXEC SQL prepare st_1 from select order_date from orders where customer_num = ? for update; EXEC SQL declare c_detail cursor for st_1; EXEC SQL declare c_master cursor with hold for
2-268
DECLARE
select customer_num from customer where city = Pittsburgh; EXEC SQL open c_master; if(SQLCODE==0) /* the open worked */ EXEC SQL fetch c_master into :p_custnum; /* discover first customer */ while(SQLCODE==0) /* while no errors and not end of pittsburgh customers */ { EXEC SQL begin work; /* start transaction for customer p_custnum */ EXEC SQL open c_detail using :p_custnum; if(SQLCODE==0) /* detail open succeeded */ EXEC SQL fetch c_detail into :p_orddate; /* get first order */ while(SQLCODE==0) /* while no errors and not end of orders */ { EXEC SQL update orders set order_date = 08/15/94 where current of c_detail; if(status==0) /* update was ok */ EXEC SQL fetch c_detail into :p_orddate; /* next order */ } if(SQLCODE==SQLNOTFOUND) /* correctly updated all found orders */ EXEC SQL commit work; /* make updates permanent, set status */ else /* some failure in an update */ { save_status = SQLCODE; /* save error for loop control */ EXEC SQL rollback work; SQLCODE = save_status; /* force loop to end */ } if(SQLCODE==0) /* all updates, and the commit, worked ok */ EXEC SQL fetch c_master into :p_custnum; /* next customer? */ } EXEC SQL close c_master;
Use either the CLOSE statement to close the hold cursor explicitly or the CLOSE DATABASE or DISCONNECT statements to close it implicitly. The CLOSE DATABASE statement closes all cursors. Releases earlier than Version 9.40 of Dynamic Server do not support the PDQPRIORITY feature with cursors that were declared WITH HOLD. Using an Insert Cursor with Hold: If you associate a hold cursor with an INSERT statement, you can use transactions to break a long series of PUT statements into smaller sets of PUT statements. Instead of waiting for the PUT statements to fill the buffer and cause an automatic write to the database, you can execute a COMMIT WORK statement to flush the row buffer. With a hold cursor, COMMIT WORK commits the inserted rows but leaves the cursor open for further inserts. This method can be desirable when you are inserting a large number of rows, because pending uncommitted work consumes database server resources.
2-269
DECLARE
v The statement cannot include any aggregate functions. v The statement cannot include any of the following clauses or keywords: DISTINCT, FOR READ ONLY, FOR UPDATE, GROUP BY, INTO TEMP, ORDER BY, UNION, or UNIQUE. v In Extended Parallel Server, the statement cannot include the INTO EXTERNAL and INTO SCRATCH clauses.
If you want to make it clear in the program code that this cursor is a read-only cursor, specify the FOR READ ONLY option as the following example shows:
EXEC SQL declare cust_curs cursor for select * from customer_notansi for read only;
If you want this cursor to be an update cursor, specify the FOR UPDATE option in your DECLARE statement. This example declares an update cursor:
EXEC SQL declare new_curs cursor for select * from customer_notansi for update;
If you want an update cursor to be able to modify only some columns in a table, you must specify those columns in the FOR UPDATE clause. The following example declares an update cursor that can update only the fname and lname columns in the customer_notansi table:
EXEC SQL declare name_curs cursor for select * from customer_notansi for update of fname, lname;
To make it clear in the program documentation that this cursor is an update cursor, you can specify the FOR UPDATE option as in this example:
EXEC SQL declare x_curs cursor for select * from customer_ansi for update;
If you want an update cursor to be able to modify only some of the columns in a table, you must specify these columns in the FOR UPDATE option. The following example declares an update cursor and specifies that this cursor can update only the fname and lname columns in the customer_ansi table:
EXEC SQL declare y_curs cursor for select * from customer_ansi for update of fname, lname;
If you want a cursor to be a read-only cursor, you must override the default behavior of the DECLARE statement by specifying the FOR READ ONLY option in your DECLARE statement. The following example declares a read-only cursor:
EXEC SQL declare z_curs cursor for select * from customer_ansi for read only;
2-270
DECLARE
If you want to use a prepared SELECT statement to modify data, add a FOR UPDATE clause to the statement text that you want to prepare, as the following ESQL/C example shows:
EXEC SQL prepare sel_1 from select * from customer for update; EXEC SQL declare sel_curs cursor for sel_1;
The DECLARE statement allows you to declare a cursor for an ESQL/C collection variable. Such a cursor is called a collection cursor. You use a collection variable to access the elements of a collection (SET, MULTISET, LIST) column. Use a cursor when you want to access one or more elements in a collection variable. The Collection-Derived Table segment identifies the collection variable for which to declare the cursor. For more information, see Collection-Derived Table on page 5-5.
2-271
DECLARE
When you declare a select cursor for a collection variable, the DECLARE statement has the following restrictions: v It cannot include the FOR READ ONLY keywords as cursor mode. The select cursor is an update cursor. v It cannot include the SCROLL or WITH HOLD keywords. The select cursor must be a sequential cursor. In addition, the SELECT statement that you associate with the collection cursor has the following restrictions: v It cannot include the following clauses or options: WHERE, GROUP BY, ORDER BY, HAVING, INTO TEMP, and WITH REOPTIMIZATION. v It cannot contain expressions in the select list. v If the collection contains elements of opaque, distinct, built-in, or other collection data types, the select list must be an asterisk ( * ). v Column names in the select list must be simple column names. These columns cannot use the following syntax:
database@server:table.column --INVALID SYNTAX
v It must specify the name of the collection variable in the FROM clause. You cannot specify an input parameter (the question-mark ( ? ) symbol) for the collection variable. Likewise you cannot use the virtual table format of the Collection-Derived Table segment. Using a SELECT Cursor with a Collection Variable: A collection cursor that includes a SELECT statement with the Collection- Derived Table clause provides access to the elements in a collection variable. To select more than one element: 1. Create a client collection variable in your ESQL/C program. 2. Declare the collection cursor for the SELECT statement with the DECLARE statement. To modify elements of the collection variable, declare the select cursor as an update cursor with the FOR UPDATE keywords. You can then use the WHERE CURRENT OF clause of the DELETE and UPDATE statements to delete or update elements of the collection. 3. Open this cursor with the OPEN statement. 4. Fetch the elements from the collection cursor with the FETCH statement and the INTO clause. 5. If necessary, perform any updates or deletes on the fetched data and save the modified collection variable in the collection column. Once the collection variable contains the correct elements, use the UPDATE or INSERT statement to save the contents of the collection variable in the actual collection column (SET, MULTISET, or LIST). 6. Close the collection cursor with the CLOSE statement. This DECLARE statement declares a select cursor for a collection variable:
EXEC SQL BEGIN DECLARE SECTION; client collection set(integer not null) a_set; EXEC SQL END DECLARE SECTION; ... EXEC SQL declare set_curs cursor for select * from table(:a_set);
2-272
DECLARE
For an extended code example that uses a collection cursor for a SELECT statement, see Fetching from a Collection Cursor (IDS) on page 2-350.
To insert the elements into the collection variable, use the PUT statement with the FROM clause. For a code example that uses a collection cursor for an INSERT statement, see Inserting into a Collection Cursor (IDS) on page 2-445.
2-273
DECLARE
When you update a row within a transaction, the row remains locked until the cursor is closed or the transaction is committed or rolled back. If you update a row when no transaction is in effect, the row lock is released when the modified row is written to disk. If you update or delete a row outside a transaction, you cannot roll back the operation. In a database that uses transactions, you cannot open an insert cursor outside a transaction unless it was also declared with the WITH HOLD keywords.
Related Information
Related statements: CLOSE, DELETE, EXECUTE PROCEDURE, FETCH, FREE, INSERT, OPEN, PREPARE, PUT, SELECT, and UPDATE For discussions of cursors and data modification, see the IBM Informix Guide to SQL: Tutorial. For more advanced issues related to cursors or using cursors with collection variables, see the IBM Informix ESQL/C Programmer's Manual.
2-274
DELETE
Use the DELETE statement to delete one or more rows from a table, or to delete one or more elements in an SPL or ESQL/C collection variable.
Syntax
DELETE (1) (2) (3) Optimizer Directives table view synonym (2) (3) ONLY (table ) (synonym) (4) Collection-Derived Table FROM (2) (3)
(4) WHERE , (2) (5) USING FROM synonym table view alias (6) WHERE CURRENT OF cursor_id Condition
Notes: 1 2 3 4 5 6
Element alias cursor_id synonym, table, view Description Temporary name that you declare here for a table Previously declared cursor Table, view, or synonym with rows to be deleted
See Optimizer Directives on page 5-34 Informix extension Dynamic Server only See Condition on page 4-5 Extended Parallel Server only ESQL/C and Stored Procedure Language only
Restrictions You cannot use an alias for an indexed table Must have been declared FOR UPDATE The table or view (or synonym and the table or view to which it points) must exist Syntax Identifier on page 5-22 Identifier on page 5-22 Database Object Name on page 5-17
Usage
To execute the DELETE statement, you must hold the DBA access privilege on the database, or the Delete access privilege on the table.
2-275
DELETE
In a database with explicit transaction logging, any DELETE statement that you execute outside a transaction is treated as a single transaction. If you specify a view name, the view must be updatable. For an explanation of an updatable view, see Updating Through Views on page 2-251. The database server locks each row affected by a DELETE statement within a transaction for the duration of the transaction. The type of lock that the database server uses is determined by the lock mode of the table, as set by a CREATE TABLE or ALTER TABLE statement, as follows: v If the lock mode is ROW, the database server acquires one lock for each row affected by the delete. v In Extended Parallel Server, if the lock mode is PAGE, the database server acquires one lock for each page affected by the delete. If the number of rows affected is very large and the lock mode is ROW, you might exceed the limits your operating system places on the maximum number of simultaneous locks. If this occurs, you can either reduce the scope of the DELETE statement or lock the table in exclusive mode before you execute the statement. 4 4 4 4 If you use DELETE without a WHERE clause (to specify either a condition or the active set of the cursor), all rows in the table are deleted. It is typically more efficient, however, to use the TRUNCATE statement, rather than the DELETE statement, to remove all rows from a table. In DB-Access, if you omit the WHERE clause while working at the SQL menu, DBAccess prompts you to verify that you want to delete all rows from a table. You do not receive a prompt if you run execute DELETE within a command file. In an ANSI-compliant database, data manipulation language (DML) statements are always in a transaction. You cannot execute a DELETE statement outside a transaction. On Dynamic Server, the FROM keyword that immediately follows DELETE can be omitted if the DELIMIDENT environment variable has been set.
Warning: If you use the DELETE statement on a supertable and omit the ONLY keyword and WHERE clause, all rows of the supertable and its subtables are deleted. You cannot specify the ONLY keyword if you plan to use the WHERE CURRENT OF clause to delete the current row of the active set of a cursor.
2-276
DELETE
the ON DELETE CASCADE option specified. When a delete is performed from the stock table, rows are also deleted in the catalog and items tables, which are referenced through the foreign keys. To have DELETE actions cascade to a table that has a referential constraint on a parent table, you need the Delete privilege only on the parent table that you reference in the DELETE statement. 4 4 4 4 4 If a DELETE operation with no WHERE clause is performed on a table that one or more child tables reference with cascading deletes, Dynamic Server deletes all rows from that table and from any affected child tables. (This resembles the effect of the TRUNCATE statement, but Dynamic Server does not support TRUNCATE operations on any table that has a child table referencing it.) For an example of how to create a referential constraint that uses cascading deletes, see Using the ON DELETE CASCADE Option on page 2-180. Restrictions on DELETE When Tables Have Cascading Deletes: You cannot use a child table in a correlated subquery to delete a row from a parent table. If two child tables reference the same parent table, and one child specifies cascading deletes but the other child does not, then if you attempt to delete a row that applies to both child tables from the parent table, the delete fails, and no rows are deleted from the parent or child tables. Locking and Logging Implications of Cascading Deletes: During deletes, the database server places locks on all qualifying rows of the referenced and referencing tables. Dynamic Server requires transaction logging for cascading deletes. If logging is turned off in a database that is not ANSI-compliant, even temporarily, deletes do not cascade, because you cannot roll back any actions. For example, if a parent row is deleted, but the system fails before the child rows are deleted, the database will have dangling child records, in violation of referential integrity. After logging is turned back on, however, subsequent deletes cascade.
In DB-Access, if you include a WHERE clause that selects all rows in the table, DBAccess gives no prompt and deletes all rows. In Dynamic Server, if you are deleting from a supertable in a table hierarchy, a subquery in the WHERE clause cannot reference a subtable. When deleting from a subtable, a subquery in the WHERE clause can reference the supertable only in SELECT...FROM ONLY (supertable)... syntax.
2-277
DELETE
row exists; you cannot use the cursor to delete or update a row until you reposition the cursor with a FETCH statement. You access the current row of the active set of a cursor with an update cursor. Before you can use the WHERE CURRENT OF clause, you must first create an update cursor by using the FOREACH statement (SPL) or the DECLARE statement with the FOR UPDATE clause (ESQL/C). Unless they are declared with the FOR READ ONLY keywords, all select cursors are potentially update cursors in an ANSI-compliant database. You can use the WHERE CURRENT OF clause with any select cursor that was not declared with the FOR READ ONLY keywords. In Dynamic Server, you cannot use WHERE CURRENT OF if you are selecting from only one table in a table hierarchy. That is, this clause is not valid with the ONLY keyword. The WHERE CURRENT OF clause can be used to delete an element from a collection by deleting the current row of the collection-derived table that a collection variable holds. For more information, see Collection-Derived Table on page 5-5.
2-278
DELETE
DELETE FROM lineitem USING order o, lineitem l WHERE o.qty < 1 AND o.order_num = l.order_num
A delete join makes it easier to incorporate new data into a database. For example, you can: 1. Store new values in a temporary table. 2. Use a delete join (DELETE...USING statement) to remove any records from the temporary table that already exist in the table into which you want to insert the new records. 3. Insert the remaining records into the table. In addition, you can use this syntax instead of deleting from the results of a SELECT statement that includes a join. In ESQL/C, when you use a delete join, the entire operation occurs as a single transaction. For example, if a delete join query is supposed to delete 100 rows and an error occurs after the 50th row, the first 50 rows that are already deleted will reappear in the table.
You can also use a collection variable to delete values in a collection column by deleting one or more individual elements in a collection. For more information, see Collection-Derived Table on page 5-5 and the examples in Database Name on page 5-15 and Example of Deleting from a Collection on page 5-12.
2-279
DELETE
Deletes across databases of the local Dynamic Server instance can also reference UDTs, as well as DISTINCT types based on built-in data types, if all the UDTs and DISTINCT types are explicitly cast to built-in data types, and all the UDTs, DISTINCT types, and casts are defined in each of the participating databases. Deletes cannot access a database of another database server unless both servers define TCP/IP connections in the DBSERVERNAME or DBSERVERALIAS configuration parameters. This applies to any communication between Dynamic Server instances, even if both database servers reside on the same computer.
Related Information
Related Statements: DECLARE, FETCH, GET DIAGNOSTICS, INSERT, OPEN, SELECT, and UPDATE For discussions of the DELETE statement, SPL routines, statement modification, cursors, and the SQLCODE code, see the IBM Informix Guide to SQL: Tutorial. For information on how to access row and collections with ESQL/C host variables, see the chapter on complex data types in the IBM Informix ESQL/C Programmer's Manual. For a discussion of the GLS aspects of the DELETE statement, see the IBM Informix GLS User's Guide.
2-280
DESCRIBE
Use the DESCRIBE statement to obtain information about output parameters and other features of a prepared statement before you execute it. Use this statement with ESQL/C. (See also DESCRIBE INPUT on page 2-286.)
Syntax
DESCRIBE OUTPUT USING SQL DESCRIPTOR INTO SQL DESCRIPTOR sqlda_pointer statement_id_var statement_id descriptor_var descriptor descriptor_var descriptor
Description Name of a system-descriptor area Host variable specifying a system-descriptor area Pointer to an sqlda structure
Restrictions
Syntax
System-descriptor area must already Quoted String on page be allocated 4-142 Must contain the name of an allocated system-descriptor area Cannot begin with dollar ( $ ) sign or colon ( : ). An sqlda structure is required if dynamic SQL is used. Must be defined in a previous PREPARE statement Must be declared in a previous PREPARE statement Language-specific rules for names See the sqlda structure in the IBM Informix ESQL/C Programmer's Manual PREPARE on page 2-433; Identifier on page 5-22 Language-specific rules for names
Statement identifier for a prepared SQL statement Host variable that contains the value of statement_id
Usage
DESCRIBE can provide information at runtime about a prepared statement: v The type of SQL statement that was prepared v Whether an UPDATE or DELETE statement contains a WHERE clause v In Dynamic Server, for a SELECT, EXECUTE FUNCTION (or EXECUTE PROCEDURE), INSERT, or UPDATE statement, the DESCRIBE statement also returns the number, data types, and size of the values, and the name of the column or expression that the query returns. v In Extended Parallel Server, for a SELECT, EXECUTE PROCEDURE, or INSERT statement, DESCRIBE also returns the number, types, and size of the values, and the name of the column or expression that the query returns. With this information, you can write code to allocate memory to hold retrieved values and display or process them after they are fetched.
2-281
DESCRIBE
2-282
DESCRIBE
A system-descriptor area conforms to the X/Open standards.
allocate descriptor desc1 with max 3; prepare curs1 FROM select * from tab; describe curs1 using sql descriptor desc1; describe curs1 using sql descriptor :desc1var;
2-283
DESCRIBE
The DESCRIBE statement sets the sqlda.sqld field to the number of values in the statement list. The sqlda structure also contains an array of data descriptors (sqlvar structures), one for each value in the statement list. After a DESCRIBE statement is executed, the sqlda.sqlvar structure has the sqltype, sqllen, and sqlname fields set. In Dynamic Server, if the column has an opaque data type, DESCRIBE...INTO sets the sqlxid, sqltypename, sqltypelen, sqlownerlen, and sqlownername fields of the item descriptor. The DESCRIBE statement allocates memory for an sqlda pointer once it is declared in a program. The application program, however, must designate the storage area of the sqlda.sqlvar.sqldatafields.
EXEC SQL declare set_curs cursor for set_id; EXEC SQL open set_curs using :a_set; EXEC SQL describe set_id using sql descriptor desc1; do { EXEC SQL fetch set_curs using sql descriptor desc1; ... EXEC SQL get descriptor desc1 :set_count = count; for (i = 1; i <= set_count; i++) { EXEC SQL get descriptor desc1 value :i :element_type = TYPE; switch { case SQLINTEGER: EXEC SQL get descriptor desc1 value :i :element_value = data; ... } /* end switch */ } /* end for */ } while (SQLCODE == 0); EXEC EXEC EXEC EXEC EXEC SQL SQL SQL SQL SQL close set_curs; free set_curs; free set_id; deallocate collection :a_set; deallocate descriptor desc1;
2-284
DESCRIBE
Related Information
Related statements: ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, DECLARE, DESCRIBE INPUT, EXECUTE, FETCH, GET DESCRIPTOR, OPEN, PREPARE, PUT, and SET DESCRIPTOR For a task-oriented discussion of the DESCRIBE statement, see the IBM Informix Guide to SQL: Tutorial. For more information about how to use a system-descriptor area and sqlda, refer to the IBM Informix ESQL/C Programmer's Manual.
2-285
DESCRIBE INPUT
Use the DESCRIBE INPUT statement to return input parameter information before a prepared statement is executed. Only Dynamic Server supports this statement. Use this statement with ESQL/C.
Syntax
DESCRIBE INPUT statement_var statement_id descriptor descriptor_var descriptor descriptor_var
sqlda_pointer
Notes: 1
Element descriptor descriptor_var sqlda_pointer Description Name of a system-descriptor area Host variable specifying a system-descriptor area Pointer to an sqlda structure
Informix extension
Restrictions Syntax
System-descriptor area must already Quoted String on page be allocated 4-142 Must contain the name of an allocated system-descriptor area Cannot begin with dollar ( $ ) sign or colon ( : ). An sqlda structure is required if dynamic SQL is used. Must be defined in a previously executed PREPARE statement Variable and statement_id both must be declared Language-specific rules for names See the sqlda structure in the IBM Informix ESQL/C Programmer's Manual. PREPARE on page 2-433; PREPARE on page 2-433; Identifier on page 5-22 Language-specific rules for names
statement_id
Statement identifier for a prepared SQL statement Host variable that contains the value of statement_id
statement_var
Usage
The DESCRIBE INPUT and the DESCRIBE OUTPUT statements can return information about a prepared statement to an SQL Descriptor Area (sqlda): v For a SELECT, EXECUTE FUNCTION (or PROCEDURE), INSERT, or UPDATE statement, the DESCRIBE statement (with no INPUT keyword) returns the number, data types, and size of the returned values, and the name of the column or expression. v For a SELECT, EXECUTE FUNCTION, EXECUTE PROCEDURE, DELETE, INSERT, or UPDATE statement, the DESCRIBE INPUT statement returns all the input parameters of a prepared statement. Tip: Dynamic Server versions earlier than 9.40 do not support the INPUT keyword. For compatibility with legacy applications, DESCRIBE without INPUT is supported. In new applications, you should use DESCRIBE INPUT statements to provide information about dynamic parameters in the WHERE
2-286
DESCRIBE INPUT
clause, in subqueries, and in other syntactic contexts where the old form of DESCRIBE cannot provide information. With this information, you can write code to allocate memory to hold retrieved values that you can display or process after they are fetched. The IFX_UPDDESC environment variable does not need to be set before you can use DESCRIBE INPUT to obtain information about an UPDATE statement.
2-287
DESCRIBE INPUT
If the database server cannot infer the data type of an expression parameter, the DESCRIBE INPUT statement returns SQLUNKNOWN as the data type. You can specify a destination for the returned informations as a new or existing system-descriptor area, or as a pointer to an sqlda structure.
allocate descriptor desc1 with max 3; prepare curs1 FROM select * from tab; describe curs1 using sql descriptor desc1; describe curs1 using sql descriptor :desc1var;
2-288
DESCRIBE INPUT
EXEC SQL declare set_curs cursor for set_id; EXEC SQL open set_curs using :a_set; EXEC SQL describe set_id using sql descriptor desc1;do { EXEC SQL fetch set_curs using sql descriptor desc1; ... EXEC SQL get descriptor desc1 :set_count = count; for (i = 1; i <= set_count; i++) { EXEC SQL get descriptor desc1 value :i :element_type = TYPE; switch { case SQLINTEGER: EXEC SQL get descriptor desc1 value :i :element_value = data; ... } /* end switch */
Chapter 2. SQL Statements
2-289
DESCRIBE INPUT
} /* end for */ } while (SQLCODE == 0); EXEC EXEC EXEC EXEC EXEC SQL SQL SQL SQL SQL close set_curs; free set_curs; free set_id; deallocate collection :a_set; deallocate descriptor desc1;
Related Information
Related statements: ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, DECLARE, DESCRIBE, EXECUTE, FETCH, GET DESCRIPTOR, OPEN, PREPARE, PUT, and SET DESCRIPTOR For a task-oriented discussion of the DESCRIBE statement, see the IBM Informix Guide to SQL: Tutorial. For more information about how to use a system-descriptor area and sqlda, refer to the IBM Informix ESQL/C Programmer's Manual.
2-290
DISCONNECT
Use the DISCONNECT statement to terminate a connection between an application and a database server.
Syntax
DISCONNECT CURRENT (1) ALL DEFAULT connection connection_var
Notes: 1
Element connection connection_var
ESQL/C only
Restrictions Connection name that the CONNECT statement assigned Must be a fixed-length character data type Syntax Quoted String on page 4-142 Language specific
Description String that specifies a connection to terminate Host variable that holds the name of a connection
Usage
DISCONNECT terminates a connection to a database server. If a database is open, it closes before the connection drops. Even if you made a connection to a specific database only, the connection to the database server is terminated by DISCONNECT. If DISCONNECT does not terminate the current connection, the connection context of the current environment is not changed. DISCONNECT is not valid as statement text in a PREPARE statement. In ESQL/C, if you disconnect with connection or connection_var, DISCONNECT generates an error if the specified connection is not a current or dormant connection.
DEFAULT Option
DISCONNECT DEFAULT disconnects the default connection. The default connection is one of the following connections: v A connection established by the CONNECT TO DEFAULT statement v An implicit default connection established by the DATABASE or CREATE DATABASE statement You can use DISCONNECT to disconnect the default connection. If the DATABASE statement does not specify a database server, as in the following example, the default connection is made to the default database server:
EXEC SQL database stores_demo; . . . EXEC SQL disconnect default;
If the DATABASE statement specifies a database server, as the following example shows, the default connection is made to that database server:
Chapter 2. SQL Statements
2-291
DISCONNECT
EXEC SQL database stores_demo@mydbsrvr; . . . EXEC SQL disconnect default;
In either case, the DEFAULT option of DISCONNECT disconnects this default connection. For more information, see DEFAULT Option on page 2-76.
In ESQL/C, the ALL keyword has the same effect on multithreaded applications that it has on single-threaded applications. Execution of the DISCONNECT ALL statement causes all connections in all threads to be terminated. However, the DISCONNECT ALL statement fails if any of the connections is in use or has an ongoing transaction associated with it. If either of these conditions is true, none of the connections is disconnected.
2-292
DISCONNECT
Related Information
Related statements: CONNECT, DATABASE, and SET CONNECTION For information on multithreaded applications, see the IBM Informix ESQL/C Programmer's Manual.
2-293
DROP ACCESS_METHOD
Use the DROP ACCESS_METHOD statement to remove a previously defined access method from the database. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP ACCESS_METHOD access_method RESTRICT
Restrictions Must be registered in sysams system catalog table; cannot be a built-in access method Must own the access method
Usage
The RESTRICT keyword is required. You cannot drop an access method if virtual tables or indexes exist that use the access method. You must be the owner of the access method or have DBA privileges to drop an access method. If a transaction is in progress, the database server waits to drop the access method until the transaction is committed or rolled back. No other users can execute the access method until the transaction has completed.
Related Information
Related statements: ALTER ACCESS_METHOD and CREATE ACCESS_METHOD For a description of the RESTRICT keyword, see Specifying RESTRICT Mode on page 2-315. For more information on primary-access methods, see the IBM Informix Virtual-Table Interface Programmer's Guide. For more information on secondary-access methods, see the IBM Informix Virtual-Index Interface Programmer's Guide. For a discussion of privileges, see the GRANT statement or the IBM Informix Database Design and Implementation Guide.
2-294
DROP AGGREGATE
Use the DROP AGGREGATE statement to drop a user-defined aggregate that you created with the CREATE AGGREGATE statement. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP AGGREGATE owner . aggregate
Restrictions Must have been previously created with the CREATE AGGREGATE statement Must own the aggregate
Usage
Dropping a user-defined aggregate does not drop the support functions that you defined for the aggregate in the CREATE AGGREGATE statement. The database server does not track dependency of SQL statements on user-defined aggregates that you use in the statements. For example, you can drop a user-defined aggregate that is used in an SPL routine. The following example drops the user-defined aggregate named my_avg:
DROP AGGREGATE my_avg
Related Information
Related statements: CREATE AGGREGATE For information about how to invoke a user-defined aggregate, see the discussion of user-defined aggregates in the Expression segment. For a description of the sysaggregates system catalog table that holds information about user-defined aggregates, see the IBM Informix Guide to SQL: Reference. For a discussion of user-defined aggregates, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
2-295
DROP CAST
Use the DROP CAST statement to remove an existing cast from the database. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP CAST ( source_type AS target_type )
Description Data type that the cast accepts as input Data type returned by the cast
Syntax Identifier on page 5-22; Data Type on page 4-18 Identifier on page 5-22; Data Type on page 4-18
Usage
You must be owner of the cast or have the DBA privilege to use DROP CAST. Dropping a cast removes its definition from the syscasts system catalog table, so the cast cannot be invoked explicitly or implicitly. Dropping a cast has no effect on the user-defined function associated with the cast. Use the DROP FUNCTION statement to remove the user-defined function from the database. Warning: Do not drop built-in casts, which user informix owns. These are required for automatic conversions between built-in data types. A cast defined on a given data type can also be used on any DISTINCT types created from that source type. If you drop the cast, you can no longer invoke it for the DISTINCT types, but dropping a cast that is defined for a DISTINCT type has no effect on casts for its source type. When you create a DISTINCT type, the database server automatically defines an explicit cast from the DISTINCT type to its source type and another explicit cast from the source type to the DISTINCT type. When you drop the DISTINCT type, the database server automatically drops these two casts.
Related Information
Related statements: CREATE CAST and DROP FUNCTION. For more information about data types, refer to the IBM Informix Guide to SQL: Reference.
2-296
DROP DATABASE
Use the DROP DATABASE statement to delete an entire database, including all system catalog tables, objects, and data. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
(1) DROP DATABASE Database Name
Usage
The DROP DATABASE statement is an extension to the ANSI/ISO standard, which does not provide syntax for the destruction of a database. The following statement drops the stores_demo database:
DROP DATABASE stores_demo
You must have the DBA privilege or be user informix to run the DROP DATABASE statement successfully. Otherwise, the database server issues an error message and does not drop the database. You cannot drop the current database or a database that is currently being used by another user. All the current users of the database must first execute the CLOSE DATABASE statement before DROP DATABASE can be successful. The DROP DATABASE statement attempts to create an implicit connection to the database that you intend to drop. If a previous CONNECT statement has established an explicit connection to another database, and that connection is still your current connection, the DROP DATABASE statement fails with error -1811. In this case, you must first use the DISCONNECT statement to close the explicit connection before you can execute the DROP DATABASE statement. The DROP DATABASE statement cannot appear in a multistatement PREPARE statement, nor within an SPL routine. In a DROP DATABASE operation, the database server acquires a lock on each table in the database and holds the locks until the entire operation is complete. Configure your database server with enough locks to accommodate this fact. For example, if the database to be dropped has 2500 tables, but fewer than 2500 locks were configured for your database server, the DROP DATABASE statement fails. For more information on how to configure the number of locks available to the database server, see the discussion of the LOCKS configuration parameter in your IBM Informix Administrator's Reference. In DBAccess, use the DROP DATABASE statement with caution. DBAccess does not prompt you to verify that you want to delete the entire database.
2-297
DROP DATABASE
In ESQL/C, you can use an unqualified database name in a program or host variable, or you can specify the fully-qualified database@server format. For example, the following statement drops the stores_demo database of a database server called gibson95:
EXEC SQL DROP DATABASE stores_demo@gibson95
If this statement executes successfully, the gibson95 database server instance continues to exist, but the stores_demo database of that database server no longer exists. For more information, see Database Name on page 5-15. The DROP DATABASE statement cannot appear in a multistatement PREPARE statement.
Related Information
Related statements: CLOSE DATABASE, CONNECT, CREATE DATABASE, DATABASE, and DISCONNECT
2-298
DROP DUPLICATE
Use the DROP DUPLICATE statement to remove from the database all duplicate copies of a specified existing table that the CREATE DUPLICATE statement created in a specified dbslice or in specified dbspaces across coservers. The original table is not affected by the DROP DUPLICATE statement. Only Extended Parallel Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP DUPLICATE OF TABLE owner . table
Description Owner of the duplicate table Name of the table for which you want to remove all duplicates
Restrictions Must own the duplicate table Must exist and must be a duplicated table
Usage
To drop all duplicate copies of a duplicated table and leave only the original table, enter the DROP DUPLICATE statement. Because duplicate tables are read-only, to update a duplicated table, you must first drop all duplicate copies. Attached indexes on the copies of the duplicate table are also dropped when DROP DUPLICATE is successfully executed.
Related Information
CREATE DUPLICATE
2-299
DROP FUNCTION
Use the DROP FUNCTION statement to remove a user-defined function from the database. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP owner . ( (1) SPECIFIC FUNCTION Specific Name FUNCTION function , parameter_type )
Notes: 1
Element function
Description Name of the user-defined function to be dropped Data type of the parameter
parameter_type
The data type (or list of data types) must be the Identifier on same data types (and specified in the same order) as page 5-22; Data those specified in the CREATE FUNCTION Type on page statement when the function was created 4-18
Usage
Dropping a user-defined function removes the text and executable versions of the function from the database. If you do not know if a UDR is a user-defined function or a user-defined procedure, you can drop the UDR by using the DROP ROUTINE statement. To use the DROP FUNCTION statement, you must be the owner of the user-defined function or have the DBA privilege. To drop an external user-defined function, see also Dropping an External Routine on page 2-309. You cannot drop an SPL function from within the same SPL function. Dynamic Server can resolve a function by its specific name, if the function definition declared a specific name. If you use the specific name in this statement, you must also use the keyword SPECIFIC, as in the following example:
DROP SPECIFIC FUNCTION compare_point
Otherwise, if the function name is not unique within the database, you must specify enough parameter_type information to disambiguate the name. If you use parameter data types to identify a user-defined function, they follow the function name, as in the following example:
DROP FUNCTION compare (int, int)
2-300
DROP FUNCTION
But the database server returns an error if it cannot resolve an ambiguous function name whose signature differs from that of another function only in an unnamed ROW-type parameter. (This error cannot be anticipated by the database server when the ambiguous function is defined.)
Related Information
Related statements: ALTER FUNCTION, CREATE FUNCTION, CREATE FUNCTION FROM, DROP PROCEDURE, DROP ROUTINE, EXECUTE FUNCTION, and GRANT For information on how to create user-defined functions, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
2-301
DROP INDEX
Use the DROP INDEX statement to remove an index. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP INDEX owner . ONLINE index (1)
Notes: 1
Element index owner Description Name of the index to be dropped Name of index owner
Usage
In a typical online transaction processing (OLTP) environment, concurrent applications are connected to the database server to perform DML operations. For every query, the optimizer chooses a plan that is based on existing indexes, statistics, and directives. After numerous OLTP transactions, however, the chosen plan might no longer be the best plan for query execution. In this case, dropping an inefficient index can sometimes improve performance. You must be the owner of the index or have the DBA privilege to use the DROP INDEX statement. The following example drops the index o_num_ix that joed owns. The stores_demo database must be the current database:
DROP INDEX stores_demo:joed.o_num_ix;
You cannot use the DROP INDEX statement to drop a unique constraint, nor to drop an index that supports a constraint; you must use the ALTER TABLE ... DROP CONSTRAINT statement to drop the constraint. When you drop the constraint, the database server automatically drops any index that exists solely to support that constraint. If you attempt to use DROP INDEX to drop an index that is shared by a unique constraint, the database server renames the specified index in the sysindexes system catalog table, declaring a new name in this format:
[space]<tabid>_<constraint_id>
Here tabid and constraint_id are from the systables and sysconstraints system catalog tables, respectively. The sysconstraints.idxname column is then updated to something like: 121_13 (where quotes show the leading blank space). If this index is a unique index with only referential constraints sharing it, the index is downgraded to a duplicate index after it is renamed.
2-302
DROP INDEX
The DBA can reduce the risk of non-exclusive access errors, and can increase the availability of the indexed table, by including the ONLINE keyword as the last specification of the DROP INDEX statement. The ONLINE keyword instructs the database server to drop the index while minimizing the duration of an exclusive locks. The index can be dropped while concurrent users are accessing the table. After you issue the DROP INDEX ... ONLINE statement, the query optimizer does not consider using the specified index in subsequent query plans or cost estimates, and the database server does not support any other DDL operations on the indexed table, until after the specified index has been dropped. Query operations that were initiated prior to the DROP INDEX ... ONLINE statement, however, can continue to access the index until the queries are completed. When no other users are accessing the index, the database server drops the index, and the DROP INDEX . . . ONLINE statement terminates execution. By default, the DROP INDEX ... ONLINE statement does not wait indefinitely for locks to be released. If one or more concurrent sessions hold locks on the table, the statement might fail with error -216 or -113 unless you first issue the SET LOCK MODE TO WAIT statement to specify an indefinite wait. Otherwise, DROP INDEX ... ONLINE uses the waiting period for locks that the DEADLOCK_TIMEOUT configuration parameter specifies, or that a previous SET LOCK MODE statement specified. To avoid locking errors, execute SET LOCK MODE TO WAIT (with no specified limit) before you attempt to drop an index online. You cannot use the CREATE INDEX statement to declare a new index that has the same identifier until after the specified index has been dropped. No more than one CREATE INDEX ... ONLINE or DROP INDEX ... ONLINE statement can concurrently reference indexes on the same table. The indexed table can be permanent or temporary, logged or unlogged, and fragmented or non-fragmented. You cannot specify the ONLINE keyword, however, when you drop a virtual index, a clustered index, or an R-tree index.
Related Information
Related statements: ALTER TABLE, CREATE INDEX, CREATE TABLE. and CREATE Temporary TABLE. For the effect of indexes on performance, see your IBM Informix Performance Guide. For more information on virtual indexes, see the IBM Informix Virtual-Index Interface Programmer's Guide.
2-303
DROP OPCLASS
Use the DROP OPCLASS statement to remove an existing operator class from the database. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP OPCLASS owner . opclass RESTRICT
Restrictions Must have been created by a previous CREATE OPCLASS statement Must own the operator class
Usage
You must be the owner of the operator class or have the DBA privilege to use the DROP OPCLASS statement. The RESTRICT keyword causes DROP OPCLASS to fail if the database contains indexes defined on the operator class that you plan to drop. Therefore, before you drop the operator class, you must use the DROP INDEX statement to drop any dependent indexes. The following DROP OPCLASS statement drops an operator class called abs_btree_ops:
DROP OPCLASS abs_btree_ops RESTRICT
Related Information
Related statement: CREATE OPCLASS, DROP INDEX For information on how to create or extend an operator class, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
2-304
DROP PROCEDURE
Use the DROP PROCEDURE statement to drop a user-defined procedure from the database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP PROCEDURE owner . procedure (1) function (3) SPECIFIC PROCEDURE Specific Name
, (2) ( parameter_type )
(2)
Notes: 1 2 3
Element function owner parameter _type Description Name of a procedure or SPL function to drop Name of UDR owner The data type of the parameter
Stored Procedure Language only Dynamic Server only See Specific Name on page 5-68
Restrictions Must exist (that is, be registered) in the database Must own the procedure or SPL function The data type (or list of data types) must be the same types (and in the same order) as those specified when the procedure was created Must exist (that is, be registered) in the database Syntax Identifier on page 5-22 Owner Name on page 5-43 Identifier on page 5-22; Data Type on page 4-18 Database Object Name on page 5-17
procedure
Usage
Dropping a user-defined procedure removes the text and executable versions of the procedure. You cannot drop an SPL procedure within the same SPL procedure. To use the DROP PROCEDURE statement, you must be the owner of the procedure or have the DBA privilege. In Dynamic Server, for backward compatibility, you can use the DROP PROCEDURE statement to drop an SPL function that was created with the CREATE PROCEDURE statement. In Extended Parallel Server, which does not support the DROP FUNCTION statement, use the DROP PROCEDURE statement to drop any SPL routine. Extended Parallel Server does not support ALTER PROCEDURE, ALTER ROUTINE, or ALTER FUNCTION statements. To change the text of an SPL procedure, you must drop the procedure, modify it, and then re-create it. Make sure to keep a copy of the SPL procedure text somewhere outside the database, in case you need to re-create the procedure after it is dropped.
2-305
DROP PROCEDURE
If the function or procedure name is not unique within the database, you must specify enough parameter_type information to disambiguate the name. If the database server cannot resolve an ambiguous UDR name whose signature differs from that of another UDR only in an unnamed ROW type parameter, an error is returned. (This error cannot be anticipated by the database server when the ambiguous function or procedure is defined.) If you do not know whether a UDR is a user-defined procedure or a user-defined function, you can use the DROP ROUTINE statement. For more information, see DROP ROUTINE on page 2-308. For backward compatibility, in Dynamic Server, you can use this statement to drop an SPL function that CREATE PROCEDURE created. You can include parameter data types after the name of the procedure to identify the procedure:
DROP PROCEDURE compare(int, int);
In Dynamic Server, if you use the specific name for the user-defined procedure, you must also use the keyword SPECIFIC, as in the following example:
DROP SPECIFIC PROCEDURE compare_point;
Related Information
Related statements: CREATE PROCEDURE, CREATE PROCEDURE FROM, DROP FUNCTION, DROP ROUTINE, EXECUTE PROCEDURE, and GRANT For information on how to create user-defined routines, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
2-306
DROP ROLE
Use the DROP ROLE statement to remove a user-defined role from the database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP ROLE role role
Element role
Restrictions
Syntax
Must be registered in the local database. When a role name Owner is enclosed in quotation marks, it is case sensitive. Name on page 5-43
Usage
Either the DBA or a user to whom the role was granted with the WITH GRANT OPTION keywords can issue the DROP ROLE statement. (Like a user name, a role is an authorization identifier, not a database object, so a role has no owner.) After you drop a role, no user can grant or enable the dropped role, and any user who had been assigned the role loses its privileges (such as table-level privileges or routine-level privileges) when the role is dropped, unless the same privileges were granted to PUBLIC or to the user individually. If the dropped role was the default role of a user, the default role for that user becomes NULL. The following statement drops the role engineer:
DROP ROLE engineer
You cannot use the DROP ROLE statement to drop a built-in role, such as the EXTEND or NONE roles of Dynamic Server.
Related Information
Related statements: CREATE ROLE, GRANT, REVOKE, and SET ROLE For a discussion of how to use roles, see the IBM Informix Guide to SQL: Tutorial.
2-307
DROP ROUTINE
Use the DROP ROUTINE statement to remove a user-defined routine (UDR) from the database. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP ROUTINE owner . routine , ( parameter_type (1) SPECIFIC ROUTINE Specific Name )
Notes: 1
Element owner parameter_type
routine
The UDR must exist (that is, be registered) in Identifier on the database page 5-22
Usage
Dropping a UDR removes the text and executable versions of the UDR from the database. If you do not know whether a UDR is a user-defined function or a user-defined procedure, this statement instructs the database server to drop the specified user-defined function or user-defined procedure. To use the DROP ROUTINE statement, you must be the owner of the UDR or have the DBA privilege. You cannot drop an SPL routine from within the same SPL routine.
Restrictions
To use this statement, the type of UDR cannot be ambiguous. The UDR that you specify must refer to either a user-defined function or a user-defined procedure. If either of the following conditions exist, the database server returns an error: v The name (and parameters) that you specify apply to both a user-defined procedure and a user-defined function, v The specific name that you specify applies to both a user-defined procedure and a user-defined function. If the routine name is not unique within the database, you must specify enough parameter_type information to disambiguate the name. If the database server cannot resolve an ambiguous routine name whose signature differs from that of another
2-308
DROP ROUTINE
routine only in an unnamed ROW type parameter, an error is returned. (This error cannot be anticipated by the database server when the ambiguous routine is defined.) If you use parameter data types to identify a UDR, they follow the UDR name, as in the following example:
DROP ROUTINE compare(int, int)
If you use the specific name for the UDR, you must also include the keyword SPECIFIC, as in the following example:
DROP SPECIFIC ROUTINE compare_point
Related Information
Related statements: CREATE FUNCTION, CREATE PROCEDURE, DROP FUNCTION, DROP PROCEDURE, EXECUTE FUNCTION, EXECUTE PROCEDURE, and GRANT
2-309
Syntax
DROP ROW TYPE owner . row_type RESTRICT
Description Name of owner of the ROW type Name of an existing named ROW data type to be dropped
Restrictions Must be the owner of row_type Must exist. See also the Usage section that follows.
Syntax Owner Name on page 5-43 Identifier on page 5-22; Data Type on page 4-18
Usage
3 3 The DROP ROW TYPE statement removes the entry for the specified row_type from the sysxtdtypes system catalog table. You must be the owner of the specified named ROW data type or have the DBA privilege to execute the DROP ROW TYPE statement. You cannot drop a named ROW data type if its name is in use. You cannot drop a named ROW data type when any of the following conditions are true: v Any existing tables or columns are using the named ROW data type. v The named ROW data type is a supertype in an inheritance hierarchy. v A view is defined on a column of the named ROW data type. To drop a named ROW-type column from a table, use ALTER TABLE. The DROP ROW TYPE statement cannot drop unnamed ROW data types.
2-310
Related Information
Related statement: CREATE ROW TYPE For a description of the system catalog tables, see the IBM Informix Guide to SQL: Reference. For a discussion of named ROW data types, see the IBM Informix Guide to SQL: Tutorial. For information on how to create user-defined data types, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
2-311
DROP SEQUENCE
Use the DROP SEQUENCE statement to remove a sequence from the database. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP SEQUENCE owner . sequence
Restrictions Must own the sequence object Must exist in the current database
Usage
This statement removes the sequence entry from the syssequences system catalog table. To drop a sequence, you must be its owner or have the DBA privilege on the database. In an ANSI-compliant database, you must qualify the name of the sequence with the name of its owner (owner.sequence) if you are not the owner. If you drop a sequence, any synonyms for the name of the sequence are also dropped automatically by the database server. You cannot use a synonym to specify the identifier of the sequence in the DROP SEQUENCE statement.
Related Information
Related statements: ALTER SEQUENCE, CREATE SEQUENCE, RENAME SEQUENCE, GRANT, REVOKE, INSERT, UPDATE, and SELECT
2-312
DROP SYNONYM
Use the DROP SYNONYM statement to remove an existing synonym. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP SYNONYM owner . synonym
Restrictions Must own the synonym The synonym and the table, view, or sequence object to which the synonym points must exist in the database
Usage
3 This removes the entries for the synonym from the systables, syssynonyms, and syssyntable system catalog tables. You must own the synonym or have the DBA privilege to execute the DROP SYNONYM statement. Dropping a synonym has no effect on the table, view, or sequence object to which the synonym points. The following statement drops the synonym nj_cust, which cathyg owns:
DROP SYNONYM cathyg.nj_cust
DROP SYNONYM is not the only DDL operation that can unregister a synonym. If a table, view, or sequence is dropped, any synonyms that are in the same database and that refer to that table, view, or sequence object are also dropped. If a synonym refers to an external table or view that is dropped, however, the synonym remains in place until you explicitly drop it using DROP SYNONYM. You can create another table, view, or synonym in place of the dropped table or view and give the new database object the name of the dropped table or view. The old synonym then refers to the new database object. For a complete discussion of synonym chaining, see the CREATE SYNONYM statement.
Related Information
Related statement: CREATE SYNONYM For a discussion of synonyms, see the IBM Informix Guide to SQL: Tutorial.
2-313
DROP TABLE
Use the DROP TABLE statement to remove a table with its associated indexes and data. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP TABLE owner . table synonym CASCADE RESTRICT
Syntax Owner Name on page 5-43 Identifier on page 5-22 Identifier on page 5-22
Local synonym for a table that The synonym and its table must exist, and is to be dropped USERTABLENAME must not be set to 1 Name of a table to drop Must be registered in the systables system catalog table of the local database
Usage
You must be the owner of the table or have the DBA privilege to use the DROP TABLE statement. You cannot drop an Extended Parallel Server table that includes a dependent GK index unless that index is entirely dependent on the affected table. If you issue a DROP TABLE statement, DBAccess does not prompt you to verify that you want to delete an entire table.
2-314
DROP TABLE
The CASCADE mode is the default mode of the DROP TABLE statement. You can also specify this mode explicitly with the CASCADE keyword.
Related Information
Related statements: CREATE TABLE, DROP DATABASE, and TRUNCATE (XPS) on page 2-628. For a discussion of the data integrity of tables, see the IBM Informix Guide to SQL: Tutorial. For a discussion of how to create a table, see the IBM Informix Database Design and Implementation Guide.
2-315
DROP TRIGGER
Use the DROP TRIGGER statement to remove a trigger definition from the database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP TRIGGER owner . trigger
Description Name of the owner of the trigger Name of the trigger to drop
Restrictions Must own the trigger The trigger must exist in the local database
Usage
You must be the owner of the trigger or have the DBA privilege to drop the trigger. Dropping a trigger removes the text of the trigger definition and the executable trigger from the database. The row describing the specified trigger is deleted from the systriggers system catalog table. In Dynamic Server, dropping an INSTEAD OF trigger on a complex view (a view with columns from more than one table) revokes any privileges on the view that the owner of the trigger received automatically when creating the trigger, and also revokes any privileges that the owner of the trigger granted to other users. (Dropping a trigger on a simple view does not revoke any privileges.) The following statement drops the items_pct trigger:
DROP TRIGGER items_pct
If a DROP TRIGGER statement appears inside an SPL routine that is called by a data manipulation (DML) statement, the database server returns an error.
Related Information
Related statements: CREATE TRIGGER, DROP TABLE
2-316
DROP TYPE
Use the DROP TYPE statement to remove a user-defined distinct or opaque data type from the database. (You cannot use this to drop a built-in data type.) Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP TYPE owner . data_type RESTRICT
Element data_type
Description Distinct or opaque data type to be removed Name of data type owner
Restrictions
Syntax
Must be an existing distinct or opaque type Identifier on page 5-22; in the local database; cannot be a built-in Data Type on page data type 4-18 Must own the data type Owner Name on page 5-43
owner
Usage
To drop a distinct or opaque data type with the DROP TYPE statement, you must be the owner of the data type or have the DBA privilege. When you use this statement, you remove the data type definition from the database (in the sysxtdtypes system catalog table). In general, this statement does not remove any definitions of casts or of support functions associated with that data type. Important: When you drop a distinct type, the database server automatically drops the two explicit casts between the distinct type and the type on which it is based. You cannot drop a distinct or opaque type if the database contains any casts, columns, or user-defined functions whose definitions reference the data type. The following statement drops the new_type data type:
DROP TYPE new_type RESTRICT
Related Information
Related statements: CREATE DISTINCT TYPE, CREATE OPAQUE TYPE, CREATE ROW TYPE, DROP ROW TYPE, and CREATE TABLE
2-317
DROP VIEW
Use the DROP VIEW statement to remove a view from the database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP VIEW owner . view synonym CASCADE RESTRICT
Description Name of view owner Synonym for a view that this statement drops Name of a view to drop
The synonym and the view to Identifier on page 5-22 which it points must exist in the local database Must exist in systables Identifier on page 5-22
view
Usage
To drop a view, you must be the owner or have the DBA privilege. When you drop a view, you also drop any other views and INSTEAD OF triggers whose definitions depend on that view. (You can also specify this default behavior explicitly with the CASCADE keyword.) When you include the RESTRICT keyword in the DROP VIEW statement, the drop operation fails if any other existing views are defined on view; otherwise, these dependent views would be unregistered by the DROP VIEW operation. You can query the sysdepend system catalog table to determine which views, if any, depend on another view. The following statement drops the view that is named cust1:
DROP VIEW cust1
Related Information
Related statements: CREATE VIEW and DROP TABLE For a discussion of views, see the IBM Informix Guide to SQL: Tutorial.
2-318
DROP XADATASOURCE
Use the DROP XADATASOURCE statement to drop a previously defined XA-compliant data source from the system catalog of the database. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
DROP XADATASOURCE xa_source RESTRICT
Element xa_source
Restrictions
Syntax
Must be present in the sysxadatasources system catalog Identifier on table page 5-22
Usage
The RESTRICT keyword is required. You must be the owner of the XA data source or hold DBA privileges to drop an access method. The following statement drops the XA data source instance called NewYork that is owned by user informix.
DROP XADATASOURCE informix.NewYork RESTRICT;
You cannot drop an access method if it is being used in a transaction that is currently open. If an XA data source has been registered with a transaction that is not complete, you can drop the data source only after the database is closed or the session ends.
Related Information
Related statements: CREATE XADATASOURCE, CREATE XADATASOURCE TYPE, DROP XADATASOURCE TYPE For a description of the RESTRICT keyword, see Specifying RESTRICT Mode on page 2-315. For information on how to use XA data sources, see the IBM Informix DataBlade API Programmer's Guide.
2-319
Syntax
DROP XADATASOURCE TYPE xa_type RESTRICT
Element xa_type
Restrictions
Syntax
Must be present in the sysxasourcetypes system Identifier on catalog table page 5-22
Usage
The RESTRICT keyword is required. You cannot unregister an XA data source type if virtual tables or indexes exist that use the data source. You must be user informix or have DBA privileges to drop an XA data source type. The following statement drops an XA data source type called MQSeries owned by user informix:
DROP XADATASOURCE TYPE informix.MQSeries RESTRICT;
You cannot drop an XA data source type until after all the XA data source instances that use that data source type have been dropped.
Related Information
Related statements: CREATE XADATASOURCE, CREATE XADATASOURCE TYPE, DROP XADATASOURCE For a description of the RESTRICT keyword, see Specifying RESTRICT Mode on page 2-315. For information on how to use XA data sources, see the IBM Informix DataBlade API Programmer's Guide.
2-320
EXECUTE
Use the EXECUTE statement to run a previously prepared statement or a multiple-statement prepared object. Only Dynamic Server supports this statement. Use this statement with ESQL/C.
Syntax
EXECUTE stmt_id stmt_id_var INTO Clause (1)
Notes: 1 2
Element stmt_id stmt_id_var Description Identifier of a prepared SQL statement Host variable containing the identifier of a prepared statement
See INTO Clause on page 2-322 See USING Clause on page 2-326
Restrictions Must have been declared in a previous PREPARE statement Syntax Identifier on page 5-22
Must exist and must contain a statement identifier PREPARE on that a previous PREPARE statement declared, and page 2-433 must be of a character data type
Usage
The EXECUTE statement passes a prepared SQL statement to the database server for execution. The following example shows an EXECUTE statement within an ESQL/C program:
EXEC SQL PREPARE del_1 FROM DELETE FROM customer WHERE customer_num = 119; EXEC SQL EXECUTE del_1;
Once prepared, an SQL statement can be executed as often as needed. After you release the database server resources (using a FREE statement), you cannot use the statement identifier with a DECLARE cursor or with the EXECUTE statement until you prepare the statement again. If the statement contained question mark ( ? ) placeholders, use the USING clause to provide specific values for them before execution. For more information, see the USING Clause on page 2-326. You can execute any prepared statement except those in the following list: v A prepared SELECT statement that returns more than one row When you use a prepared SELECT statement to return multiple rows of data, you must use a cursor to retrieve the data rows. As an alternative, you can EXECUTE a prepared SELECT INTO TEMP statement to achieve the same result.
Chapter 2. SQL Statements
2-321
EXECUTE
For more information on cursors, see DECLARE on page 2-260. v A prepared EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement for an SPL function that returns more than one row When you prepare an EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement to invoke an SPL function that returns multiple rows, you must use a cursor to retrieve the data rows. For more information on how to execute a SELECT or an EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement, see PREPARE on page 2-433. If you create or drop a trigger after you prepare a triggering INSERT, DELETE, or UPDATE statement, the prepared statement returns an error when you execute it.
INTO Clause
Use the INTO clause to save the returned values of these SQL statements: v A prepared singleton SELECT statement that returns only one row of column values for the columns in the select list v A prepared EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement for an SPL function that returns only one set of values The INTO clause of the EXECUTE statement has the following syntax: INTO Clause:
, INTO output_var (1) : indicator_var INDICATOR SQL DESCRIPTOR descriptor_var descriptor DESCRIPTOR sqlda_pointer
2-322
EXECUTE
Element descriptor descriptor_var indicator_var Description Quoted string that identifies a system-descriptor area Host variable that identifies a system-descriptor area Host variable that receives a return code if corresponding parameter_var is NULL value, or if truncation occurs Host variable whose contents replace a question-mark ( ? ) placeholder in a prepared statement Pointer to an sqlda structure that defines data type and memory location of values to replace a question-mark ( ? ) placeholder in a prepared object Restrictions Must already be allocated. Use single ( ) quotation marks System-descriptor area must already be allocated Cannot be DATETIME or INTERVAL data type Must be a character data type Syntax Quoted String on page 4-142 Language specific Language specific
output_var
Language specific
sqlda_pointer
Cannot begin with a dollar sign ( $ ) or a colon ( : ) symbol. An sqlda structure is required with dynamic SQL
This closely resembles the syntax of the USING Clause on page 2-326. The INTO clause provides a concise and efficient alternative to more complicated and lengthy syntax. In addition, by placing values into variables that can be displayed, the INTO clause simplifies and enhances your ability to retrieve and display data values. For example, if you use the INTO clause, you do not have to use a cursor to retrieve values from a table. You can store the returned values in output variables, in output SQL descriptors, or in output sqlda pointers.
2-323
EXECUTE
v A descriptor that is a pointer to an sqlda structure Sections that follow describe each of these options for specifying parameters.
The COUNT field corresponds to the number of values that the prepared statement returns. The value of COUNT must be less than or equal to the value of the occurrences that were specified when the system-descriptor area was allocated with the ALLOCATE DESCRIPTOR statement. You can obtain the value of a field with the GET DESCRIPTOR statement and set the value with the SET DESCRIPTOR statement. For more information, refer to the discussion of the system-descriptor area in the IBM Informix ESQL/C Programmer's Manual.
2-324
EXECUTE
DESCRIPTOR clause of the EXECUTE statement. Each time the EXECUTE statement is run, the database server places the returns values that the sqlda structure describes into the sqlda structure. The next example uses an sqlda structure to execute a prepared statement:
struct sqlda *pointer2; ... sprintf(sel_stmt, "%s %s %s", "select fname, lname from customer", "where customer_num =", cust_num); EXEC SQL prepare sel1 from :sel_stmt; EXEC SQL describe sel1 into pointer2; EXEC SQL execute sel1 into descriptor pointer2;
The sqlda.sqld value specifies the number of output values that are described in occurrences of sqlvar. This number must correspond to the number of values that the SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement returns. For more information, refer to the sqlda discussion in the IBM Informix ESQL/C Programmer's Manual. This example uses the INTO clause with an EXECUTE statement in ESQL/C:
EXEC SQL prepare sel1 from select fname, lname from customer where customer_num =123; EXEC SQL execute sel1 into :fname, :lname using :cust_num;
The next example uses the INTO clause to return multiple rows of data:
EXEC SQL BEGIN DECLARE SECTION; int customer_num =100; char fname[25]; EXEC SQL END DECLARE SECTION; EXEC SQL prepare sel1 from select fname from customer where customer_num=?; for ( ;customer_num < 200; customer_num++) { EXEC SQL execute sel1 into :fname using customer_num; printf("Customer number is %d\n", customer_num); printf("Customer first name is %s\n\n", fname); }
2-325
EXECUTE
v DELETE ... WHERE v UPDATE ... WHERE In an ANSI-compliant database, if you prepare and execute any of the statements in the preceding list, and no rows are returned, the returned SQLCODE value is SQLNOTFOUND ( = 100).
USING Clause
Use the USING clause to specify the values that are to replace question-mark ( ? ) placeholders in the prepared statement. Providing values in the EXECUTE statement that replace the question-mark ( ? ) placeholders in the prepared statement is sometimes called parameterizing the prepared statement. USING Clause:
, USING parameter_var (1) : indicator_var INDICATOR SQL DESCRIPTOR descriptor_var descriptor DESCRIPTOR sqlda_pointer
Notes: 1
Element descriptor Description Quoted string that identifies a system-descriptor area Host variable that identifies a system-descriptor area Host variable that receives a return code if corresponding parameter_var is NULL value, or if truncation occurs Host variable whose contents replace a question-mark ( ? ) placeholder in a prepared statement Pointer to an sqlda structure that defines data type and memory location of values to replace question-mark ( ? ) placeholder in a prepared object
Informix extension
Restrictions System-descriptor area must already be allocated. Use single ( ) quotation marks. System-descriptor area must already be allocated Cannot be DATETIME or INTERVAL data type Must be a character data type Syntax Quoted String on page 4-142 Language specific Language specific
descriptor_var indicator_var
parameter_var
Language specific
sqlda_pointer
Cannot begin with a dollar sign ( $ ) or a colon ( : ). An sqlda structure is required with dynamic SQL
This closely resembles the syntax of the INTO Clause on page 2-322. If you know the number of parameters to be supplied at runtime and their data types, you can define the parameters that are needed by the EXECUTE statement as host variables in your program. If you do not know the number of parameters to be supplied at runtime or their data types, you can associate input values from a system-descriptor area or an
2-326
EXECUTE
sqlda structure. Both of these descriptor structures describe the data type and memory location of one or more values to replace question-mark ( ? ) placeholders.
Related Information
Related statements: ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, DECLARE, EXECUTE IMMEDIATE, FETCH, GET DESCRIPTOR, PREPARE, PUT, and SET DESCRIPTOR
Chapter 2. SQL Statements
2-327
EXECUTE
For a task-oriented discussion of the EXECUTE statement, see the IBM Informix Guide to SQL: Tutorial. For more information about concepts that relate to the EXECUTE statement, see the IBM Informix ESQL/C Programmer's Manual.
2-328
EXECUTE FUNCTION
Use the EXECUTE FUNCTION statement to execute a user-defined function. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
EXECUTE FUNCTION
(3) (1) (4) function (1) SPL_var (5) (5) (6) (8) IFX_REPLACE_MODULE Function (9) jvpcontrol Function ( , (2) Argument (7) ) INTO Clause
Notes: 1 2 3 4 5 6 7 8 9 Stored Procedure Language only See Arguments on page 5-2 See INTO Clause on page 2-330 ESQL/C only Dynamic Server only C only See IFX_REPLACE_MODULE Function (IDS, C) on page 4-90 Java only See The jvpcontrol Function (Java) on page 2-333
Description Name of a user-defined function to execute Variable that contains the name of an SPL routine to be executed Restrictions Must be registered in the database Must be a CHAR, VARCHAR, NCHAR, or NVARCHAR data type that contains the non-NULL name of an existing SPL function Syntax Database Object Name on page 5-17 Identifier on page 5-22
Usage
The EXECUTE FUNCTION statement invokes a user-defined function (UDF), with arguments, and specifies where the results are to be returned. An external function returns exactly one value. An SPL function can return one or more values. You cannot use the EXECUTE FUNCTION statement to execute any type of user-defined procedure that returns no value. Instead, use the EXECUTE PROCEDURE or EXECUTE ROUTINE statement to execute procedures.
Chapter 2. SQL Statements
2-329
EXECUTE FUNCTION
You must have the Execute privilege on the user-defined function. If a UDF has a companion function, any user who executes the function must have the Execute privilege on both the function and its companion. For example, if a function has a negator function, any user who executes the function must have the Execute privilege on both the function and its negator. For more information, see GRANT on page 2-371.
INTO Clause
INTO Clause:
2-330
EXECUTE FUNCTION
, INTO data_var (1) : (2) $ INDICATOR data_structure indicator_var
Notes: 1 2
Element data_structure Description Structure that was declared as a host variable Variable to receive the value that a user-defined function returns Program variable to store a return code if the corresponding data_var receives a NULL value
Individual elements of structure must Language specific be compatible with the data types of the returned values See Data Variables on page 2-331. Use an indicator variable if the value of the corresponding data_var might be NULL Language specific Language specific
data_var indicator_var
You must include an INTO clause with EXECUTE FUNCTION to specify the variables that receive the values that a user-defined function returns. If the function returns more than one value, the values are returned into the list of variables in the order in which you specify them. If the EXECUTE FUNCTION statement stands alone (that is, it is not part of a DECLARE statement and does not use the INTO clause), it must execute a noncursor function. A noncursor function returns only one row of values. The following example shows a SELECT statement in IBM Informix ESQL/C:
EXEC SQL execute function cust_num(fname, lname, company_name) into :c_num;
Data Variables
If you issue EXECUTE FUNCTION within an ESQL/C program, data_var must be a host variable. Within an SPL routine, data_var must be an SPL variable. If you issue EXECUTE FUNCTION within a CREATE TRIGGER statement, data_var must be a column name in the triggering table or in another table.
2-331
EXECUTE FUNCTION
To return more than one row of values, an external function (one written in the C or Java language) must be defined as an iterator function. For more information on iterator functions, see the IBM Informix DataBlade API Programmer's Guide. In an SPL routine, if a SELECT returns more than one row, you must use the FOREACH statement to access the rows individually. The INTO clause of the SELECT statement can store the fetched values. For more information, see FOREACH on page 3-20. To return more than one row of values, an SPL function must include the WITH RESUME keywords in its RETURN statement. For more information on how to write SPL functions, see the IBM Informix Guide to SQL: Tutorial. In an IBM Informix ESQL/C program, the DECLARE statement can declare a function cursor and the FETCH statement can return rows individually from the cursor. You can put the INTO clause in the EXECUTE FUNCTION or in the FETCH statement, but you cannot put it in both. The following IBM Informix ESQL/C code examples show different ways you can use the INTO clause: v Using the INTO clause in the EXECUTE FUNCTION statement:
EXEC SQL declare f_curs cursor for execute function get_orders(customer_num) into :ord_num, :ord_date; EXEC SQL open f_curs; while (SQLCODE == 0) EXEC SQL fetch f_curs; EXEC SQL close f_curs;
2-332
EXECUTE FUNCTION
the explicit name of an SPL routine, you can use an SPL variable to hold the routine name. For more information about how to execute SPL functions dynamically, see the IBM Informix Guide to SQL: Tutorial.
Element jvp_id
Description Name of the Java Virtual Processor (JVP) class about which you seek information
You must associate this function with the equivalent of a cursor in the Java language.
For additional details of the IFX_REPLACE_MODULE function, see the section IFX_REPLACE_MODULE Function (IDS, C) on page 4-90.
Related Information
Related statements: CALL, CREATE FUNCTION, CREATE FUNCTION FROM, DROP FUNCTION, DROP ROUTINE, EXECUTE PROCEDURE, and FOREACH
2-333
EXECUTE IMMEDIATE
Use the EXECUTE IMMEDIATE statement to perform the functions of the PREPARE, EXECUTE, and FREE statements. Use this statement with ESQL/C.
Syntax
; EXECUTE IMMEDIATE ' statement statement_var '
Description A valid SQL statement Host variable that contains a character string of one or more SQL statements
Restrictions See the same sections of Usage that are listed below for statement_var
Must be a previously declared character-type variable. Language See EXECUTE IMMEDIATE and Restricted Statements specific on page 2-334 and Restrictions on Allowed Statements on page 2-335.
Usage
The EXECUTE IMMEDIATE statement makes it easy to execute dynamically a single simple SQL statement that is constructed during program execution. For example, you can obtain the name of a database from program input, construct the DATABASE statement as a program variable, and then use EXECUTE IMMEDIATE to execute the statement, which opens the database. The quoted string that includes one or more SQL statements, or the contents of statement_var, is parsed and executed if correct; then all data structures and memory resources are released immediately. In the usual method of dynamic execution, these operations require separate PREPARE, EXECUTE, and FREE statements. The maximum length of an EXECUTE IMMEDIATE statement is 64 kilobytes.
2-334
EXECUTE IMMEDIATE
In addition, you cannot use the EXECUTE IMMEDIATE statement to execute the following statements in text that contains multiple statements that are separated by semicolons:
CLOSE DATABASE CREATE DATABASE DATABASE DROP DATABASE SELECT (except SELECT INTO TEMP)
Use the PREPARE statement and either a cursor or the EXECUTE statement to execute a dynamically constructed SELECT statement.
Related Information
Related statements: EXECUTE, FREE, and PREPARE For a discussion of quick execution, see the IBM Informix Guide to SQL: Tutorial.
2-335
EXECUTE PROCEDURE
Use the EXECUTE PROCEDURE statement to invoke a user-defined procedure. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
EXECUTE PROCEDURE procedure ( ) (1) , SPL_var (2) function Argument (3) (4) (5) SQLJ Built-In Procedures (3) (6) (7) IFX_UNLOAD_MODULE Procedure
Notes: 1 2 3 4 5 6 7 Stored Procedure Language only See Arguments on page 5-2 Dynamic Server only Java only See 2-338 C only See IFX_REPLACE_MODULE Function (IDS, C) on page 4-90
Description SPL function to execute Host variable or program variable that receives the returned value from UDR User-defined procedure to execute Variable that contains the name of the SPL routine to execute Restrictions Must exist Syntax Database Object Name on page 5-17
In the context of a CREATE TRIGGER Language specific statement, must contain column names in the triggering table or in another table Must exist Must be a character data type that contains the non-NULL name of an SPL routine. Database Object Name on page 5-17 Identifier on page 5-22
procedure SPL_var
Usage
The EXECUTE PROCEDURE statement invokes the named user-defined procedure and specifies its arguments. In Dynamic Server, for backward compatibility, you can use the EXECUTE PROCEDURE statement to execute an SPL function that you created with the CREATE PROCEDURE statement. In Extended Parallel Server, use the EXECUTE PROCEDURE statement to execute any SPL routine. Extended Parallel Server does not support the EXECUTE FUNCTION statement.
2-336
EXECUTE PROCEDURE
In ESQL/C, if the EXECUTE PROCEDURE statement returns more than one row, it must be enclosed within an SPL FOREACH loop or accessed through a cursor.
Causes of Errors
EXECUTE PROCEDURE returns an error under the following conditions: v It has more arguments than the called procedure expects. v One or more arguments are missing and do not have default values. Unlike Dynamic Server, Extended Parallel Server can invoke procedures with missing arguments, but the procedure might fail if a missing argument causes another error (for example, an undefined value for a variable). v The fully qualified procedure name or the routine signature is not unique. v No procedure with the specified name or signature is found. If the procedure name is not unique within the database, you must specify enough parameter_type information to disambiguate the name. See Arguments on page 5-2 for additional information about how to specify parameters when invoking a procedure. (In Dynamic Server, the specific name of an external UDR is valid in DDL statements, but is not valid in contexts where you invoke the procedure.)
2-337
EXECUTE PROCEDURE
Element module_name Description Full pathname of file to unload Restrictions Syntax
Shared-object file must exist and be unused. Pathname Quoted String can be up to 255 characters long. on page 4-142
The IFX_UNLOAD_MODULE procedure can only unload an unused shared-object file; that is, when no executing SQL statements (in any database) are using any UDRs in the specified shared-object file. If any UDR in the shared-object file is currently in use, then IFX_UNLOAD_MODULE raises an error. On UNIX, for example, suppose you want to unload the circle.so shared library, which contains C UDRs. If this library resides in the /usr/apps/opaque_types directory, you can use the following EXECUTE PROCEDURE statement to execute the IFX_UNLOAD_MODULE procedure:
EXECUTE PROCEDURE ifx_unload_module( /usr/apps/opaque_types/circle.so, C);
On Windows, for example, suppose you want to unload the circle.dll dynamic link library, which contains C UDRs. If this library is in the C:\usr\apps\opaque_types directory, you can use the following EXECUTE PROCEDURE statement to execute the IFX_UNLOAD_MODULE procedure:
EXECUTE PROCEDURE ifx_unload_module( C:\usr\apps\opaque_types\circle.dll, C);
For more information on how to use IFX_UNLOAD_MODULE to unload a shared-object file, see the chapter on how to design a UDR in IBM Informix User-Defined Routines and Data Types Developer's Guide. For information on how to use the IFX_REPLACE_MODULE function, see IFX_REPLACE_MODULE Function (IDS, C) on page 4-90. Use the SQLJ Driver built-in procedures for one of the following tasks: v To install, replace, or remove a set of Java classes v To specify a path for Java class resolution for Java classes that are included in a JAR file v To map or remove the mapping between a user-defined type and the Java type to which it corresponds SQLJ Driver Built-In Procedures:
(1) sqlj.install_JAR (2) sqlj.replace_JAR (3) sqlj.remove_JAR (4) sqlj.alter_java_path (5) sqlj.SetUDTExtName (6) sqlj.unsetUDTExtName
Notes: 1 2 3 See sqlj.install_jar on page 2-339 See sqlj.replace_jar on page 2-340 See sqlj.remove_jar on page 2-340
2-338
EXECUTE PROCEDURE
4 5 6 See sqlj.alter_java_path on page 2-341 See sqlj.setUDTextName on page 2-342 See sqlj.unsetUDTExtName on page 2-342
The SQLJ built-in procedures are stored in the sysprocedures system catalog table. They are grouped under the sqlj schema. Tip: For any Java static method, the first built-in procedure that you execute must be the sqlj.install_jar( ) procedure. You must install the jar file before you can create a UDR or map a user-defined data type to a Java type. Similarly, you cannot use any of the other SQLJ built-in procedures until you have used sqlj.install_jar( ).
sqlj.install_jar
Use the sqlj.install_jar( ) procedure to install a jar file in the current database and assign it a jar identifier. sqlj.install_jar:
(1) sqlj.install_jar ( jar_file , Jar Name , 0 deploy
Notes: 1
Element deploy jar_file Description Integer that causes the procedure to search for deployment descriptor files in the jar file URL of the jar file that contains the UDR written in Java
Maximum length of the Quoted String on URL is 255 bytes page 4-142
For example, consider a Java class Chemistry that contains the following static method explosiveReaction( ):
public static int explosiveReaction(int ingredient);
Here the Chemistry class resides in this jar file on the server computer:
/students/data/Courses.jar
You can install all classes in the Courses.jar jar file in the current database with the following call to the sqlj.install_jar( ) procedure:
EXECUTE PROCEDURE sqlj.install_jar("file://students/data/Courses.jar", "course_jar")
The sqlj.install_jar( ) procedure assigns the jar ID, course_jar, to the Courses.jar file that it has installed in the current database. After you define a jar ID in the database, you can use that jar ID when you create and execute a UDR written in Java. When you specify a nonzero number for the third argument, the database server searches through any included deployment descriptor files. For example, you might want to include descriptor files that include SQL statements to register and grant privileges on UDRs in the jar file.
Chapter 2. SQL Statements
2-339
EXECUTE PROCEDURE
sqlj.replace_jar
Use the sqlj.replace_jar( ) procedure to replace a previously installed jar file with a new version. When you use this syntax, you provide only the new jar file and assign it to the jar ID for which you want to replace the file. sqlj.replace_jar:
(1) sqlj.replace_jar ( jar_file , Jar Name )
Notes: 1
Element jar_file Description URL of the jar file that contains the UDR written in Java
The maximum length of the URL is 255 Quoted String on bytes page 4-142
If you attempt to replace a jar file that is referenced by one or more UDRs, the database server generates an error. You must drop the referencing UDRs before replacing the jar file. For example, the following call replaces the Courses.jar file, which had previously been installed for the course_jar identifier, with the Subjects.jar file:
EXECUTE PROCEDURE sqlj.replace_jar("file://students/data/Subjects.jar", "course_jar")
Before you replace the Course.jar file, you must drop the user-defined function sql_explosive_reaction( ) with the DROP FUNCTION (or DROP ROUTINE) statement.
sqlj.remove_jar
Use the sqlj.remove_jar( ) procedure to remove a previously installed jar file from the current database. sqlj.remove_jar:
(1) sqlj.remove_jar ( Jar Name , 0 deploy
Notes: 1
Element deploy Description Integer that causes the procedure to search for deployment descriptor files in the jar file
If you attempt to remove a jar file that is referenced by one or more UDRs, the database server generates an error. You must drop the referencing UDRs before you replace the jar file. For example, the following SQL statements remove the jar file associated with the course_jar jar id:
DROP FUNCTION sql_explosive_reaction; EXECUTE PROCEDURE sqlj.remove_jar("course_jar")
2-340
EXECUTE PROCEDURE
When you specify a nonzero number for the second argument, the database server searches through any included deployment descriptor files. For example, you might want to include descriptor files that include SQL statements to revoke privileges on UDRs in the associated jar file and drop them from the database.
sqlj.alter_java_path
Use sqlj.alter_java_path( ) to specify the jar-file path to use when the routine manager resolves related Java classes for the jar file of a UDR written in Java. sqlj.alter_java_path:
sqlj.alter_java_path
(1) ))
Notes: 1
Element class_id package_id Description Java class that contains method to implement the UDR
Name of the package that contains The fully qualified identifier of package_id.class_id the Java class must not exceed 255 bytes
The jar IDs that you specify, namely the jar ID for which you are altering the jar-file path and the resolution jar ID, must both have been installed with the sqlj.install_jar procedure. When you invoke a UDR written in the Java language, the routine manager attempts to load the Java class in which the UDR resides. At this time, it must resolve the references that this Java class makes to other Java classes. The three types of such class references are these: 1. References to Java classes that the JVPCLASSPATH configuration parameter specifies (such as Java system classes like java.util.Vector) 2. References to classes that are in the same jar file as the UDR 3. References to classes that are outside the jar file that contains the UDR. The routine manager implicitly resolves classes of type 1 and 2 in the preceding list. To resolve type 3 references, it examines all the jar files in the jar file path that the latest call to sqlj.alter_java_path( ) specified. The routine manager throws an exception if it cannot resolve a class reference. The routine manager checks the jar file path for class references after it performs the implicit type 1 and type 2 resolutions. If you want a Java class to be loaded from the jar file that the jar file path specifies, make sure the Java class is not present in the JVPCLASSPATH configuration parameter. Otherwise, the system loader picks up that Java class first, which might result in a different class being loaded than what you expect.
2-341
EXECUTE PROCEDURE
Suppose that the sqlj.install_jar( ) procedure and CREATE FUNCTION have been executed as the preceding sections describe. The following SQL statement invokes sql_explosive_reaction( ) function in the course_jar jar file:
EXECUTE PROCEDURE alter_java_path("course_jar", "(professor/*, prof_jar)"); EXECUTE FUNCTION sql_explosive_reaction(10000)
The routine manager attempts to load the class Chemistry. It uses the path that the call to sqlj.alter_java_path( ) specifies to resolve any class references. Therefore, it checks the classes that are in the professor package of the jar file that prof_jar identifies.
sqlj.setUDTextName
Use the sqlj.setUDTextName( ) procedure to define the mapping between a user-defined data type and a Java class. sqlj.SetUDTextName:
sqlj.SetUDTextName (
data_type , package_id,
class_id )
Restrictions
Syntax
Qualified name package_id.class_id must Language-specific rules for not exceed 255 bytes Java identifiers Identifier on page 5-22 Language-specific rules for Java identifiers
User-defined type for which to Name must not exceed 255 bytes create a mapping Name of package that contains Same length restrictions as class_id the class_id Java class
You must have registered the user-defined data type in the CREATE DISTINCT TYPE, CREATE OPAQUE TYPE, or CREATE ROW TYPE statement. To look up the Java class for a user-defined data type, the database server searches in the jar-file path, which the sqlj.alter_java_path( ) procedure has specified. For more information on the jar-file path, see sqlj.alter_java_path on page 2-341. On the client side, the driver looks into the CLASSPATH path on the client environment before it asks the database server for the name of the Java class. The setUDTextName procedure is an extension to the SQLJ:SQL Routines using the Java Programming Language specification.
sqlj.unsetUDTExtName
Use the sqlj.unsetUDTextName( ) procedure to remove the mapping from a user-defined data type to a Java class. sqlj.unsetUDTExtName:
sqlj.unsetUDTExtName ( data_type )
2-342
EXECUTE PROCEDURE
Element data_type Description User-defined data type for which to remove the mapping Restrictions Must exist Syntax Identifier on page 5-22
This procedure removes the SQL-to-Java mapping and consequently removes any cached copy of the Java class from database server shared memory. The unsetUDTextName procedure is an extension to the SQLJ:SQL Routines Using the Java Programming Language specification.
Related Information
Related statements: CREATE FUNCTION, CREATE PROCEDURE, EXECUTE FUNCTION, GRANT, CALL, FOREACH, and LET
2-343
FETCH
Use the FETCH statement to move a cursor to a new row in the active set and to retrieve the row values from memory. Use this statement with ESQL/C.
Syntax
(1) FETCH PRIOR PREVIOUS FIRST LAST CURRENT + RELATIVE position_num_var position_num - position_num row_position_var row_position NEXT
ABSOLUTE
(1) cursor_id_var cursor_id (1) USING descriptor descriptor_var DESCRIPTOR sqlda_pointer SQL DESCRIPTOR
2-344
FETCH
Element cursor_id cursor_id_var data_structure descriptor descriptor_var indicator_var output_var position_num position_num_var row_position row_position_var sqlda_pointer Description Cursor to retrieve rows Host variable storing cursor_id Structure as a host variable System-descriptor area Host variable storing descriptor Host variable for return code if output_var can be NULL value Host variable for fetched value Position relative to current row Host variable ( = position_num) Ordinal position in active set Host variable ( = row_ position) Pointer to an sqlda structure Restrictions Must be open Must be character data type Must store fetched values Must have been allocated Must be allocated See Using Indicator Variables on page 2-347. Must store value from row Value 0 fetches current row Value 0 fetches current row Must be an integer >1 Must be 1 or greater Cannot begin with $ nor : Syntax Identifier on page 5-22 Language specific Language specific Quoted String on page 4-142 Language specific Language specific Language specific Literal Number on page 4-137 Language specific Literal Number on page 4-137 Language specific See ESQL/C manual.
Usage
How the database server creates, stores, and fetches members of the active set of rows depends on whether the cursor is a sequential cursor or a scroll cursor. In X/Open mode, if a cursor-direction value (such as NEXT or RELATIVE) is specified, a warning message is issued, indicating that the statement does not conform to X/Open standards.
When the program opens a sequential cursor, the database server processes the query to the point of locating or constructing the first row of data. The goal of the database server is to tie up as few resources as possible. Because the sequential cursor can retrieve only the next row, the database server can frequently create the active set one row at a time. On each FETCH operation, the database server returns the contents of the current row and locates the next row. This one-row-at-a-time strategy is not possible if the database server must create the entire active set to determine which row is the first row (as would be the case if the SELECT statement included an ORDER BY clause).
2-345
FETCH
EXEC SQL fetch previous q_curs into :orders; EXEC SQL fetch last q_curs into :orders; EXEC SQL fetch relative -10 q_curs into :orders; printf("Which row? "); scanf("%d",row_num); EXEC SQL fetch absolute :row_num q_curs into :orders;
A scroll cursor can fetch any row in the active set, either by specifying an absolute row position or a relative offset. Use the following cursor-position options to specify a particular row that you want to retrieve. Keyword NEXT PREVIOUS PRIOR FIRST LAST CURRENT RELATIVE Effect Retrieves next row in active set Retrieves previous row in active set Retrieves previous row in active set (Synonymous with PREVIOUS.) Retrieves the first row in active set Retrieves the last row in active set Retrieves the current row in active set (the same row as returned by the previous FETCH statement from the scroll cursor) Retrieves nth row, relative to the current cursor position in the active set, where position_num (or position_num_var) supplies n. A negative value indicates the nth row prior to the current cursor position. If position_num = 0, the current row is fetched. Retrieves nth row in active set, where row_position_var (or row_position) = n . Absolute row positions are numbered from 1.
ABSOLUTE
Tip: Do not confuse row-position values with rowid values. A rowid value is based on the position of a row in its table and remains valid until the table is rebuilt. A row-position value (a value that the ABSOLUTE keyword retrieves) is the relative position of the row in the current active set of the cursor; the next time the cursor is opened, different rows might be selected.
2-346
FETCH
v Use the INTO clause of an EXECUTE Function (or EXECUTE PROCEDURE) statement. v Use the INTO clause of a FETCH statement. v Use a system-descriptor area. v Use an sqlda structure.
If you prepare a SELECT statement, the SELECT cannot include the INTO clause so you must use the INTO clause of the FETCH statement. When you create a SELECT statement dynamically, you cannot use an INTO clause because you cannot name host variables in a prepared statement. If you are certain of the number and data type of values in the projection list, you can use an INTO clause in the FETCH statement. If user input generated the query, however, you might not be certain of the number and data type of values that are being selected. In this case, you must use either a system descriptor or else a pointer to an sqlda structure.
2-347
FETCH
statement. Therefore, the FETCH statement must include an INTO clause to retrieve data into a set of variables. This method lets you store different rows in different memory locations. You can fetch into a program-array element only by using an INTO clause in the FETCH statement. If you use a program array, you must list both the array name and a specific element of the array in data_structure. When you are declaring a cursor, do not refer to an array element within the SQL statement. Tip: If you are certain of the number and data type of values in the select list of the Projection clause, you can use an INTO clause in the FETCH statement. In the following ESQL/C example, a series of complete rows is fetched into a program array. The INTO clause of each FETCH statement specifies an array element as well as the array name:
EXEC SQL BEGIN DECLARE SECTION; char wanted_state[2]; short int row_count = 0; struct customer_t{ { int c_no; char fname[15]; char lname[15]; } cust_rec[100]; EXEC SQL END DECLARE SECTION; main() { EXEC SQL connect tostores_demo; printf("Enter 2-letter state code: "); scanf ("%s", wanted_state); EXEC SQL declare cust cursor for select * from customer where state = :wanted_state; EXEC SQL open cust; EXEC SQL fetch cust into :cust_rec[row_count]; while (SQLCODE == 0) { printf("\n%s %s", cust_rec[row_count].fname, cust_rec[row_count].lname); row_count++; EXEC SQL fetch cust into :cust_rec[row_count]; } printf ("\n"); EXEC SQL close cust; EXEC SQL free cust; }
2-348
FETCH
The following example shows a valid FETCH...USING SQL DESCRIPTOR statement:
EXEC SQL allocate descriptor desc; ... EXEC SQL declare selcurs cursor for select * from customer where state = CA; EXEC SQL describe selcurs using sql descriptor desc; EXEC SQL open selcurs; while (1) { EXEC SQL fetch selcurs using sql descriptor desc;
4. Use the USING DESCRIPTOR clause of FETCH to specify the sqlda structure as the location into which you fetch the returned values. The following example shows a FETCH USING DESCRIPTOR statement:
struct sqlda *sqlda_ptr; ... EXEC SQL declare selcurs2 cursor for select * from customer where state = CA; EXEC SQL describe selcurs2 into sqlda_ptr; ... EXEC SQL open selcurs2; while (1) { EXEC SQL fetch selcurs2 using descriptor sqlda_ptr; ...
The sqld value specifies the number of output values that are described in occurrences of the sqlvar structures of the sqlda structure. This number must correspond to the number of values returned from the prepared statement.
2-349
FETCH
v When you set the isolation level to Cursor Stability, the current row is locked. v In an ANSI-compliant database, an isolation level of Repeatable Read is the default; you can set it to something else. v When you are fetching through an update cursor (one that is declared FOR UPDATE), each row you fetch is locked with a promotable lock. Other programs can read the locked row, but no other program can place a promotable or write lock; therefore, the row is unchanged if another user tries to modify it using the WHERE CURRENT OF clause of an UPDATE or DELETE statement. When you modify a row, the lock is upgraded to a write lock and remains until the cursor is closed or the transaction ends. If you do not modify the row, the behavior of the database server depends on the isolation level you have set. The database server releases the lock on an unchanged row as soon as another row is fetched, unless you are using Repeatable Read isolation. (See SET ISOLATION on page 2-575.) Important: You can hold locks on additional rows even when Repeatable Read isolation is not in use or is unavailable. Update the row with unchanged data to hold it locked while your program is reading other rows. You must evaluate the effect of this technique on performance in the context of your application, and you must be aware of the increased potential for deadlock. When you use explicit transactions, be sure that a row is both fetched and modified within a single transaction; that is, both the FETCH statement and the subsequent UPDATE or DELETE statement must fall between a BEGIN WORK statement and the next COMMIT WORK statement.
The following ESQL/C code fragment shows how to fetch elements from the child_colors collection variable:
EXEC SQL BEGIN DECLARE SECTION; client collection child_colors; varchar one_favorite[21]; char child_name[31] = "marybeth"; EXEC SQL END DECLARE SECTION;
2-350
FETCH
EXEC SQL allocate collection :child_colors; /* Get structure of fav_colors column for untyped * child_colors collection variable */ EXEC SQL select fav_colors into :child_colors from children where name = :child_name; /* Declare select cursor for child_colors collection * variable */ EXEC SQL declare colors_curs cursor for select * from table(:child_colors); EXEC SQL open colors_curs; do { EXEC SQL fetch colors_curs into :one_favorite; ... } while (SQLCODE == 0) EXEC SQL close colors_curs; EXEC SQL free colors_curs; EXEC SQL deallocate collection :child_colors;
After you fetch a collection element, you can modify the element with the UPDATE or DELETE statements. For more information, see the UPDATE and DELETE statements in this manual. You can also insert new elements into the collection variable with an INSERT statement. For more information, see the INSERT statement.
Related Information
Related statements: ALLOCATE DESCRIPTOR, CLOSE, DEALLOCATE DESCRIPTOR, DECLARE, DESCRIBE, GET DESCRIPTOR, OPEN, PREPARE, SET DEFERRED_PREPARE, and SET DESCRIPTOR
2-351
FETCH
For a task-oriented discussion of the FETCH statement, see the IBM Informix Guide to SQL: Tutorial. For more information about concepts that relate to the FETCH statement, see the IBM Informix ESQL/C Programmer's Manual.
2-352
FLUSH
Use the FLUSH statement to force rows that a PUT statement buffered to be written to the database. Use this statement, which is an extension to the ANSI/ISO standard for SQL, with ESQL/C.
Syntax
FLUSH cursor_id cursor_id_var
Description Name of a cursor Host variable that holds the value of cursor_id
Usage
The PUT statement adds a row to a buffer, whose content is written to the database when the buffer is full. Use the FLUSH statement to force the insertion when the buffer is not full. If the program terminates without closing the cursor, the buffer is left unflushed. Rows placed into the buffer since the last flush are lost. Do not expect the end of the program to close the cursor and flush the buffer automatically. The following example shows a FLUSH statement that operates on a cursor called icurs:
FLUSH icurs
2-353
FLUSH
Tip: When you encounter an SQLCODE error, a corresponding SQLSTATE error value also exists. For information about how to get the message text, check the GET DIAGNOSTICS statement. To count the number of rows actually inserted into the database as well as the number not yet inserted: 1. Prepare two integer variables, for example, total and pending. 2. When the cursor opens, set both variables to 0. 3. Each time a PUT statement executes, increment both total and pending. 4. Whenever a FLUSH statement executes or the cursor is closed, subtract the third field of the SQLERRD array from pending.
Related Information
Related statements: CLOSE, DECLARE, OPEN, and PREPARE For a task-oriented discussion of FLUSH, see the IBM Informix Guide to SQL: Tutorial. For information about the sqlca structure, see the IBM Informix ESQL/C Programmer's Manual.
2-354
FREE
Use the FREE statement to release resources that are allocated to a prepared statement or to a cursor. Use this statement, which is an extension to the ANSI/ISO standard for SQL, with ESQL/C.
Syntax
FREE cursor_id cursor_id_var statement_id statement_id_var
Description Name of a cursor Host variable that holds the value of cursor_id String that identifies an SQL statement Host variable that identifies an SQL statement
Restrictions Must have been declared Must be a character data type Must be defined in a previous PREPARE statement Same restrictions as statement_id. Must be a character data type.
Syntax Identifier on page 5-22 Language specific PREPARE on page 2-433 PREPARE on page 2-433
Usage
FREE releases the resources that the database server and application-development tool allocated for a prepared statement or for a declared cursor. If you declared a cursor for a prepared statement, FREE statement_id (or statement_id_var) releases only the resources in the application development tool; the cursor can still be used. The resources in the database server are released only when you free the cursor. If you prepared a statement (but did not declare a cursor for it), FREE statement_id (or FREE statement_id_var) releases the resources in both the application development tool and the database server. After you free a statement, you cannot execute it or declare a cursor for it until you prepare it again. The following ESQL/C example shows the sequence of statements that is used to free an implicitly prepared statement:
EXEC SQL prepare sel_stmt from select * from orders; ... EXEC SQL free sel_stmt;
The following ESQL/C example shows the sequence of statements that are used to release the resources of an explicitly prepared statement. The first FREE statement in this example frees the cursor. The second FREE statement in this example frees the prepared statement.
2-355
FREE
sprintf(demoselect, "%s %s", "select * from customer ", "where customer_num between 100 and 200"); EXEC SQL prepare sel_stmt from :demoselect; EXEC SQL declare sel_curs cursor for sel_stmt; EXEC SQL open sel_curs; ... EXEC SQL close sel_curs; EXEC SQL free sel_curs; EXEC SQL free sel_stmt;
If you declared a cursor for a prepared statement, freeing the cursor releases only the resources in the database server. To release the resources for the statement in the application-development tool, use FREE statement_id (or FREE statement_id_var). If a cursor is not declared for a prepared statement, freeing it releases the resources in both the application-development tool and the database server. After a cursor is freed, it cannot be opened until it is declared again. The cursor should be explicitly closed before it is freed. For an example of a FREE statement that frees a cursor, see the previous example.
Related Information
Related statements: CLOSE, DECLARE, EXECUTE, EXECUTE IMMEDIATE, OPEN, PREPARE, and SET AUTOFREE For a task-oriented discussion of the FREE statement, see the IBM Informix Guide to SQL: Tutorial.
2-356
GET DESCRIPTOR
Use the GET DESCRIPTOR statement to read from a system descriptor area. Use this statement with ESQL/C.
Syntax
GET DESCRIPTOR descriptor_var descriptor = COUNT , VALUE item_num_var item_num Described Item Information
total_items_var
2-357
GET DESCRIPTOR
Element descriptor descriptor_var field_var item_num item_num_ var total_items_var Description Quoted string that identifies a system-descriptor area (SDA) Variable that stores descriptor value Host variable to receive the contents of a field from an SDA Unsigned ordinal number of an item described in the SDA Host variable storing item_num Host variable storing the number of items described in the SDA Restrictions System-descriptor area must already have been allocated Same restrictions as descriptor Must be of type that can receive value of a specified SDA field 0 item_num (number of item descriptors in the SDA) Must be an integer data type Must be an integer data type Syntax Quoted String on page 4-142 Language specific Language specific Literal Number on page 4-137 Language specific Language specific
Usage
Use the GET DESCRIPTOR statement to accomplish any of the following tasks: v Determine how many items are described in a system-descriptor area. v Determine the characteristics of each column or expression that is described in the system-descriptor area. v Copy a value from the system-descriptor area into a host variable after a FETCH statement. With Dynamic Server, you can use GET DESCRIPTOR after you describe EXECUTE FUNCTION, INSERT, SELECT, or UPDATE statements with the DESCRIBE ... USING SQL DESCRIPTOR statement. With Extended Parallel Server, you can use GET DESCRIPTOR after you describe EXECUTE PROCEDURE, INSERT, or SELECT statements with the DESCRIBE ... USING SQL DESCRIPTOR statement. The host variables that you reference in the GET DESCRIPTOR statement must be declared in the BEGIN DECLARE SECTION of a program. If an error occurs during the assignment of a value to any specified host variable, the contents of the host variable are undefined.
2-358
GET DESCRIPTOR
The value that the database server returns into the TYPE field is a defined integer. To evaluate the data type that is returned, test for a specific integer value. For additional information about integer data type values, see Setting the TYPE or ITYPE Field on page 2-556. In X/Open mode, the X/Open code is returned to the TYPE field. You cannot mix the two modes because errors can result. For example, if a particular data type is not defined under X/Open mode but is defined for IBM Informix products, executing a GET DESCRIPTOR statement can result in an error. In X/Open mode, a warning message appears if ILENGTH, IDATA, or ITYPE is used. It indicates that these fields are not standard X/Open fields for a system-descriptor area. If the TYPE of a fetched value is DECIMAL or MONEY, the database server returns the precision and scale information for a column into the PRECISION and SCALE fields after a DESCRIBE statement is executed. If the TYPE is not DECIMAL or MONEY, the SCALE and PRECISION fields are undefined. Using the VALUE Clause After a FETCH: Each time your program fetches a row, it must copy the fetched value into host variables so that the data can be used. To accomplish this task, use a GET DESCRIPTOR statement after each fetch of each value in the select list. If three values exist in the select list, you need to use three
2-359
GET DESCRIPTOR
GET DESCRIPTOR statements after each fetch (assuming you want to read all three values). The item numbers for each of the three GET DESCRIPTOR statements are 1, 2, and 3. The following ESQL/C example shows how you can copy data from the DATA field into a host variable (result) after a fetch. For this example, it is predetermined that all returned values are the same data type:
EXEC SQL get descriptor demodesc :desc_count = count; .. . EXEC SQL fetch democursor using sql descriptor demodesc; for (i = 1; i <= desc_count; i++) { if (sqlca.sqlcode != 0) break; EXEC SQL get descriptor demodesc value :i :result = DATA; printf("%s ", result); } printf("\n");
Fetching a NULL Value: When you use GET DESCRIPTOR after a fetch, and the fetched value is NULL, the INDICATOR field is set to -1 to indicate the NULL value. The value of DATA is undefined if INDICATOR indicates a NULL value. The host variable into which DATA is copied has an unpredictable value.
2-360
GET DESCRIPTOR
Use these field names with the GET DESCRIPTOR statement to obtain information about an opaque column.
Related Information
Related statements: ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, DECLARE, DESCRIBE, EXECUTE, FETCH, OPEN, PREPARE, PUT, and SET DESCRIPTOR For more information on concepts that relate to the GET DESCRIPTOR statement, see the IBM Informix ESQL/C Programmer's Manual. For more information on the sysxtdtypes system catalog table, see the IBM Informix Guide to SQL: Reference.
2-361
GET DIAGNOSTICS
Use the GET DIAGNOSTICS statement to return diagnostic information about the most recently executed SQL statement. Use this statement with ESQL/C.
Syntax
(1) GET DIAGNOSTICS Statement Clause (2) EXCEPTION Clause
Notes: 1 2 See Statement Clause on page 2-365 See EXCEPTION Clause on page 2-366
Usage
The GET DIAGNOSTICS statement retrieves specified status information that the database server records in a structure called the diagnostics area. Using GET DIAGNOSTICS does not change the contents of the diagnostics area. The GET DIAGNOSTICS statement uses one of the following two clauses: v The Statement clause returns count and overflow information about errors and warnings that the most recent SQL statement generates. v The EXCEPTION clause provides specific information about errors and warnings that the most recent SQL statement generates.
Class code
Subclass code
The following table is a quick reference for interpreting class code values.
2-362
GET DIAGNOSTICS
SQLSTATE Class Code Value Outcome 00 01 02 > 02 Success Success with warning No data found Error or warning
Support for the ANSI/ISO Standard for SQL: All status codes returned to the SQLSTATE variable are ANSI-compliant except in the following cases: v SQLSTATE codes with a class code of 01 and a subclass code that begins with an I are Informix-specific warning messages. v SQLSTATE codes with a class code of IX and any subclass code are Informix-specific error messages. v SQLSTATE codes whose class code begins with a digit in the range 5 to 9 or with an uppercase letter in the range I to Z indicate conditions that are currently undefined by the ANSI/ISO standard for SQL. The only exception is that SQLSTATE codes whose class code is IX are Informix-specific error messages. List of SQLSTATE Codes: This table describes the class codes, subclass codes, and the meaning of all valid warning and error codes associated with the SQLSTATE variable.
Class 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 07 07 07 07 07 07 07 07 07 Subclass 000 000 002 003 004 005 006 007 I01 I03 I04 I05 I06 I07 I08 I09 I10 I11 000 000 001 002 003 004 005 006 008 009 Meaning Success Success with warning Disconnect error. Transaction rolled back NULL value eliminated in set function String data, right truncation Insufficient item descriptor areas Privilege not revoked Privilege not granted Database has transactions ANSI-compliant database selected IBM Informix database server selected Float to decimal conversion was used Informix extension to ANSI-compliant syntax UPDATE or DELETE statement does not have a WHERE clause An ANSI keyword was used as a cursor name Cardinalities of the projection list and of the INTO list are not equal Database server running in secondary mode Dataskip is turned on No data found Dynamic SQL error USING clause does not match dynamic parameters USING clause does not match target specifications Cursor specification cannot be executed USING clause is required for dynamic parameters Prepared statement is not a cursor specification Restricted data type attribute violation Invalid descriptor count Invalid descriptor index
2-363
GET DIAGNOSTICS
Class 08 08 08 08 08 08 08 08 0A 0A 21 21 21 22 22 22 22 22 22 22 22 22 22 23 24 25 2B 2D 26 2E 28 33 34 35 37 3C 40 40 42 S0 S0 S0 S0 S0 S1 IX Subclass 000 001 002 003 004 006 007 S01 000 001 000 S01 S02 000 001 002 003 005 027 012 019 024 025 000 000 000 000 000 000 000 000 000 000 000 000 000 000 003 000 000 001 002 011 021 001 000 Meaning Connection exception Database server rejected the connection Connection name in use Connection does not exist Client unable to establish connection Transaction rolled back Transaction state unknown Communication failure Feature not supported Multiple server transactions Cardinality violation Insert value list does not match column list Degree of derived table does not match column list Data exception String data, right truncation NULL value, no indicator parameter Numeric value out of range Error in assignment Data exception trim error Division by zero (0) Invalid escape character Unterminated string Invalid escape sequence Integrity constraint violation Invalid cursor state Invalid transaction state Dependent privilege descriptors still exist Invalid transaction termination Invalid SQL statement identifier Invalid connection name Invalid user-authorization specification Invalid SQL descriptor name Invalid cursor name Invalid exception number Syntax error or access violation in PREPARE or EXECUTE IMMEDIATE Duplicate cursor name Transaction rollback Statement completion unknown Syntax error or access violation Invalid name Base table or view table already exists Base table not found Index already exists Column already exists Memory allocation failure Informix reserved error message
2-364
GET DIAGNOSTICS
Using SQLSTATE in Applications: You can use a built-in variable, called SQLSTATE, which you do not have to declare in your program. SQLSTATE contains the status code, essential for error handling, which is generated every time your program executes an SQL statement. SQLSTATE is created automatically. You can examine the SQLSTATE variable to determine whether an SQL statement was successful. If the SQLSTATE variable indicates that the statement failed, you can execute a GET DIAGNOSTICS statement to obtain additional error information. For an example of how to use an SQLSTATE variable in a program, see Using GET DIAGNOSTICS for Error Checking on page 2-369.
Statement Clause
Statement Clause:
, status_var = ROW_COUNT NUMBER MORE
Element status_var
Description Host variable to receive status information about the most recent SQL statement for the specified status field name
When retrieving count and overflow information, GET DIAGNOSTICS can deposit the values of the three statement fields into a corresponding host variable. The host-variable data type must be the same as that of the requested field. The following keywords represent these three fields.
Field Name Keyword MORE NUMBER ROW_COUNT Field Data Type Field Contents Character Integer Integer Y or N 1 to 35,000 0 to 999,999,999 ESQL/C Host Variable Data Type char[2] int int
Using the MORE Keyword: Use the MORE keyword to determine if the most recently executed SQL statement resulted in the following actions by the database server: v Stored all the exceptions that it detected in the diagnostics area If so, GET DIAGNOSTICS returns a value of N. v Detected more exceptions than it stored in the diagnostics area If so, GET DIAGNOSTICS returns a value of Y. (The value of MORE is always N.) Using the ROW_COUNT Keyword: The ROW_COUNT keyword returns the number of rows the most recently executed DML statement processed. ROW_COUNT counts these rows: v Inserted into a table v Updated in a table v Deleted from a table
Chapter 2. SQL Statements
2-365
GET DIAGNOSTICS
Using the NUMBER Keyword: The NUMBER keyword returns the number of exceptions that the most recently executed SQL statement raised. The NUMBER field can hold a value from 1 to 35,000, depending on how many exceptions are counted.
EXCEPTION Clause
Exception Clause:
, EXCEPTION exception_num exception_var information = CLASS_ORIGIN CONNECTION_ALIAS MESSAGE_LENGTH MESSAGE_TEXT RETURNED_SQLSTATE SERVER_NAME SUBCLASS_ORIGIN
Description Number of exceptions Variable storing exception_num Host variable to receive the value of a specified exception field
Restrictions Integer in range 1 to 35,000 Must be SMALLINT or INT Data type must match that of the specified field
The exception_num literal indicates one of the exception values from the number of exceptions that the NUMBER field in the Statement clause returns. When retrieving exception information, GET DIAGNOSTICS writes the values of each of the seven fields into corresponding host variables. These fields are located in the diagnostics area and are derived from an exception raised by the most recent SQL statement. The host-variable data type must be the same as that of the requested field. The following table describes the seven exception information fields.
ESQL/C Host Variable Data Type char[6] char[255] char[255] char[255] int char[255] char[255]
Field Name Keyword RETURNED_SQLSTATE CLASS_ORIGIN SUBCLASS_ORIGIN MESSAGE_TEXT MESSAGE_LENGTH SERVER_NAME CONNECTION_NAME
Field Data Type Field Contents Character Character Character Character Integer Character Character SQLSTATE value String String String Numeric value String String
The application specifies the exception by number, using either an unsigned integer or an integer host variable (an exact numeric with a scale of 0). An exception with a value of 1 corresponds to the SQLSTATE value set by the most recent SQL statement other than GET DIAGNOSTICS. The association between
2-366
GET DIAGNOSTICS
other exception numbers and other exceptions raised by that SQL statement is undefined. Thus, no set order exists in which the diagnostic area can be filled with exception values. You always get at least one exception, even if the SQLSTATE value indicates success. If an error occurs within the GET DIAGNOSTICS statement (that is, if an invalid exception number is requested), the Informix internal SQLCODE and SQLSTATE variables are set to the value of that exception. In addition, the GET DIAGNOSTICS fields are undefined. Using the RETURNED_SQLSTATE Keyword: The RETURNED_SQLSTATE keyword returns the SQLSTATE value that describes the exception. Using the CLASS_ORIGIN Keyword: Use the CLASS_ORIGIN keyword to retrieve the class portion of the RETURNED_SQLSTATE value. If the ISO standard for SQL defines the class, the value of CLASS_ORIGIN is equal to ISO 9075. Otherwise, the value returned by CLASS_ORIGIN is defined by Informix and cannot be ISO 9075. The terms ANSI SQL and ISO SQL are synonymous. Using the SUBCLASS_ORIGIN Keyword: The SUBCLASS_ORIGIN keyword returns data on the RETURNED_SQLSTATE subclass. (This value is ISO 9075 if the ISO standard defines the subclass.) Using the MESSAGE_TEXT Keyword: The MESSAGE_TEXT keyword returns the message text of the exception (for example, an error message). Using the MESSAGE_LENGTH Keyword: The MESSAGE_LENGTH keyword returns the length in bytes of the current message text string. Using the SERVER_NAME Keyword: The SERVER_NAME keyword returns the the name of the database server associated with a CONNECT or DATABASE statement. GET DIAGNOSTICS updates the SERVER_NAME field when any of the following events occur: v A CONNECT statement successfully executes. v A SET CONNECTION statement successfully executes. v A DISCONNECT statement successfully terminates the current connection. v A DISCONNECT ALL statement fails. The SERVER_NAME field is not updated, however, after these events: v A CONNECT statement fails. v A DISCONNECT statement fails (but this does not include the DISCONNECT ALL statement). v A SET CONNECTION statement fails. The SERVER_NAME field retains the value set in the previous SQL statement. If any of the preceding conditions occur on the first SQL statement that executes, the SERVER_NAME field is blank.
2-367
GET DIAGNOSTICS
you connect or fail to connect Field is blank if you do not have a current connection or if you make a default connection. SET CONNECTION DISCONNECT Contains the name of the database server to which you switch or fail to switch Contains the name of the database server from which you disconnect or fail to disconnect If you disconnect and then you execute a DISCONNECT statement for a connection that is not current, the SERVER_NAME field remains unchanged. Sets the field to blank if the statement executes successfully If the statement fails, SERVER_NAME contains the names of all the database servers from which you did not disconnect. (This information does not mean that the connection still exists.)
DISCONNECT ALL
If CONNECT succeeds, SERVER_NAME is set to one of the following values: v The INFORMIXSERVER value (if the connection is to a default database server; that is, if CONNECT specified no database server) v The name of the database server (if the connection is to a specific database server) The DATABASE Statement: When you execute a DATABASE statement, the SERVER_NAME field contains the name of the database server on which the database resides. Using the CONNECTION_NAME Keyword: Use the CONNECTION_NAME keyword to return the name of the connection specified in your CONNECT or SET CONNECTION statement. When the CONNECTION_NAME Keyword Is Updated: GET DIAGNOSTICS updates the CONNECTION_NAME field when the following situations occur: v A CONNECT statement successfully executes. v A SET CONNECTION statement successfully executes. v A DISCONNECT statement successfully executes at the current connection. GET DIAGNOSTICS fills the CONNECTION_NAME field with blanks because no current connection exists. v A DISCONNECT ALL statement fails. When the CONNECTION_NAME Is Not Updated: The CONNECTION_NAME field is not updated in the following cases: v A CONNECT statement fails. v A DISCONNECT statement fails (but this does not include the DISCONNECT ALL statement). v A SET CONNECTION statement fails. The CONNECTION_NAME field retains the value set in the previous SQL statement. If any of the preceding conditions occurs on the first SQL statement that executes, the CONNECTION_NAME field is blank. An implicit connection has no name. After a DATABASE statement successfully creates an implicit connection, the CONNECTION_NAME field is blank.
2-368
GET DIAGNOSTICS
SET CONNECTION
DISCONNECT
DISCONNECT ALL
If CONNECT is successful, CONNECTION_NAME takes one of these values: v The name of the database environment as specified in the CONNECT statement if the CONNECT statement does not include the AS clause v The name of the connection (the identifier that was declared after the AS keyword) if the CONNECT statement includes the AS clause
2-369
GET DIAGNOSTICS
printf("SQLCODE: %d\n", SQLCODE); printf("\n"); EXEC SQL get diagnostics :exception_count = NUMBER, :overflow = MORE; printf("EXCEPTIONS: Number=%d\t", exception_count); printf("More? %s\n", overflow); for (i = 1; i <= exception_count; i++) { EXEC SQL get diagnostics exception :i :sqlstate_code = RETURNED_SQLSTATE, :class_id = CLASS_ORIGIN, :subclass_id = SUBCLASS_ORIGIN, :message = MESSAGE_TEXT, :messlen = MESSAGE_LENGTH; printf("- - - - - - - - - - - - - - - - - - - -\n"); printf("EXCEPTION %d: SQLSTATE=%s\n", i, sqlstate_code); message[messlen-1] =\0; printf("MESSAGE TEXT: %s\n", message); j = stleng(class_id); while((class_id[j] == \0) || (class_id[j] == )) j--; class_id[j+1] = \0; printf("CLASS ORIGIN: %s\n",class_id); j = stleng(subclass_id); while((subclass_id[j] == \0) || (subclass_id[j] == )) j--; subclass_id[j+1] = \0; printf("SUBCLASS ORIGIN: %s\n",subclass_id); } printf("---------------------------------"); printf("-------------------------\n"); }
Related Information
For a task-oriented discussion of error handling and the SQLSTATE variable, see the IBM Informix Guide to SQL: Tutorial. For a discussion of concepts related to the GET DIAGNOSTICS statement and the SQLSTATE variable, see the IBM Informix ESQL/C Programmer's Manual.
2-370
GRANT
The GRANT statement can assign privileges and roles to users of the database and to other roles.
Syntax
(1) GRANT Database-Level Privileges (3) DEFAULT ROLE (3) Role Name TO Options (4) TO Options Role Name user (2) TO PUBLIC ,
Table-Level Privileges (1) (5) Routine-Level Privileges (6) (7) Language-Level Privileges (8) Type-Level Privileges (9) Sequence-Level Privileges
TO Options:
TO PUBLIC , user , role user
AS
grantor
Notes: 1 2 3 4 5 6 7 8 9 Informix extension See page 2-373 See page 2-383 See page 2-374 See page 2-380 Dynamic Server only See page 2-382 See page 2-378 See page 2-382
2-371
GRANT
Element grantor Description Restrictions Syntax
Authorization identifier of a user who can use REVOKE to cancel Must be valid Owner Name, the effects of this GRANT statement. If AS clause is omitted, user name (not a p. 5-43 default is login name of user issuing this statement role name) Name of an existing role to which you grant one or more access privileges, or to which you assign another role Authorization identifier of a user to whom you grant one or more access privileges, or to whom you assign a role Must exist in the Owner Name, database p. 5-43 Same as for grantor Owner Name, p. 5-43
role user
Usage
The GRANT statement extends access privileges to other users that would normally accrue only to the DBA or to the creator of an object. Subsequent GRANT statements do not affect privileges that have already been granted to a user. You can use the GRANT statement for operations like the following: v Authorize others to use or administer a database that you create v Allow others to view, alter, or drop a table, synonym, view or (for Dynamic Server only) a sequence object that you create v Allow others to use a data type or the SPL language, or (for Dynamic Server only) to execute a user-defined routine (UDR) that you create v Assign a role and its privileges to users, or to PUBLIC, or to another role v Assign a default role to one or more users or to PUBLIC You can grant privileges to a previously created role or to a built-in role. You can grant a role to PUBLIC, to individual users, or to another role. If you enclose grantor, role, or user in quotation marks, the name is case sensitive and is stored exactly as you typed it. In an ANSI-compliant database, if you do not use quotation marks as delimiters, the name is stored in uppercase letters. Privileges that you grant remain in effect until you cancel them with a REVOKE statement. Only the grantor of a privilege can revoke that privilege. The grantor is the person who issues the GRANT statement, unless the AS grantor clause transfers the right to revoke those privileges to another user. Only the owner of an object or a user to whom privileges were explicitly granted with the WITH GRANT OPTION keywords can grant privileges on an object. Having DBA privileges is not sufficient. As DBA, however, you can grant a privilege on behalf of another user by using the AS grantor clause. For privileges on database objects whose owner is not a user recognized by the operating system (for example, user informix), the AS grantor clause is useful. The keyword PUBLIC extends the specified privilege or role to all users. If you intend to restrict privileges that PUBLIC already holds to only a subset of users, you must first revoke those privileges from PUBLIC. When database-level privileges conflict with table-level privileges, the more restrictive privileges take precedence. To grant privileges on one or more fragments of a table that has been fragmented by expression, see GRANT FRAGMENT on page 2-388.
2-372
GRANT
Database-Level Privileges
Database-Level Privileges:
CONNECT RESOURCE DBA
When you create a database with the CREATE DATABASE statement, you are the owner and automatically receive all database-level privileges. The database remains inaccessible to any other users until you, as DBA, grant database privileges to them. As database owner, you also receive table-level privileges on all tables in the database automatically. For more information about table-level privileges, see Table-Level Privileges on page 2-374. Recommendation: Only user informix can modify system catalog tables directly. Except as noted specifically in your database server documentation, however, do not use DML statements to insert, delete, or update rows of system catalog tables directly, because modifying data in these tables can destroy the integrity of the database. Database access levels are, from lowest to highest, Connect, Resource, and DBA. Use the corresponding keyword to grant a level of access privilege.
Privilege CONNECT Effect Lets you query and modify data You can modify the database schema if you own the database object that you intend to modify. Any user with the Connect privilege can perform the following operations: v Connect to the database with the CONNECT statement or another connection statement v Execute SELECT, INSERT, UPDATE, and DELETE statements, provided the user has the necessary table-level privileges v Create views, provided the user has the Select privilege on the underlying tables v Create synonyms v Create temporary tables and create indexes on the temporary tables v Alter or drop a table or an index, provided the user owns the table or index (or has Alter, Index, or References privileges on the table) v Grant privileges on a table or view, provided the user owns the table (or was given privileges on the table with the WITH GRANT OPTION keywords)
2-373
GRANT
Privilege RESOURCE Effect Lets you extend the structure of the database In addition to the capabilities of the Connect privilege, the holder of the Resource privilege can perform the following functions: v Create new tables v Create new indexes v Create new UDRs v Create new data types DBA Has all the capabilities of the Resource privilege and can perform the following additional operations: v Grant any database-level privilege, including the DBA privilege, to another user v Grant any table-level privilege to another user or to a role v Grant a role to a user or to another role v Revoke a privilege whose grantor you specify as the revoker in the AS clause of the REVOKE statement v Restrict the Execute privilege to DBAs when registering a UDR v Execute the SET SESSION AUTHORIZATION statement v Create any database object v Create tables, views, and indexes, designating another user as owner of these objects v Alter, drop, or rename database objects, regardless of who owns them v Execute the DROP DISTRIBUTIONS option of the UPDATE STATISTICS statement
User informix has the privilege required to alter the tables of the system catalog, including the systables table. The following example uses the PUBLIC keyword to grant the Connect privilege on the currently active database to all users:
GRANT CONNECT TO PUBLIC
You cannot grant database-level privileges to a role. Only individual users or PUBLIC can hold database-level privileges.
Table-Level Privileges
When you create a table with the CREATE TABLE statement, you are the table owner and automatically receive all table-level privileges. You cannot transfer ownership to another user, but you can grant table-level privileges to another user or to a role. (See, however, RENAME TABLE on page 2-454, which can change both the name and the ownership of a table.) A user with the database-level DBA privilege automatically receives all table-level privileges on every table in that database. Table-Level Privileges:
2-374
GRANT
PRIVILEGES ALL , INSERT DELETE (1) UPDATE (1) SELECT REFERENCES (1) ALTER INDEX (2) UNDER table view synonym ( , column ) ON
owner
Notes: 1 2
Element column Description Column on which the References, Select, or Update privilege is granted. Default scope is all columns of table, view, or synonym. Name of the user who owns the table, view, or synonym Synonym, table, or view on which privileges are granted. (This can be qualified by owner. name.)
The GRANT statement can list one or more of the following keywords to specify the table privileges that you grant to the same users or roles.
2-375
GRANT
Privilege INSERT DELETE SELECT UPDATE REFERENCES Effect Lets you insert rows Lets you delete rows Lets you access any column in SELECT statements. You can restrict the Select privilege to one or more columns by listing the columns. Lets you access any column in UPDATE statements. You can restrict the Update privilege to one or more columns by listing the columns. Lets you define referential constraints on columns. You must have the Resource privilege to take advantage of the References privilege. (You can add, however, a referential constraint during an ALTER TABLE statement without holding the Resource privilege on the database.) You need only the References privilege to indicate cascading deletes. You do not need the Delete privilege to place cascading deletes on a table. You can restrict the References privilege to one or more columns by listing the columns. Lets you create permanent indexes. You must have the Resource privilege to use the Index privilege. (Any user with the Connect privilege can create an index on temporary tables.) Lets you add or delete columns, modify column data types, add or delete constraints, change the locking mode of the table from PAGE to ROW, or add or drop a corresponding ROW data type for your table. It also lets you enable or disable indexes, constraints and triggers, as described in SET Database Object Mode on page 2-539. You must have the Resource privilege to use the Alter privilege. In addition, you also need the Usage privilege for any user-defined data type affected by the ALTER TABLE statement. UNDER ALL Lets you create subtables under a typed table (for IDS only) Provides all privileges listed above. The PRIVILEGES keyword is optional.
INDEX
ALTER
You can narrow the scope of a Select, Update, or References privilege by specifying the columns to which the privilege applies. Specify the keyword PUBLIC as user if you intend the GRANT statement to apply to all users. Some simple examples that follow illustrate how to give table-level privileges with the GRANT statement. The following statement grants the privilege to delete and select values in any column in the table customer to users mary and john. It also grants the Update privilege, but only for columns customer_num, fname, and lname:
GRANT DELETE, SELECT, UPDATE (customer_num, fname, lname) ON customer TO mary, john;
To grant the same privileges as those above to all authorized users, use the keyword PUBLIC as the following example shows:
GRANT DELETE, SELECT, UPDATE (customer_num, fname, lname) ON customer TO PUBLIC;
For a Dynamic Server database, suppose a user named mary has created a typed table named tab1. By default, only user mary can create subtables under the tab1 table. If mary wants to grant the ability to create subtables under the tab1 table to a user named john, mary must enter the following GRANT statement:
2-376
GRANT
GRANT UNDER ON tab1 TO john
After receiving the Under privilege on table tab1, user john can create one or more subtables under tab1.
For example, assume that user ted has the Select and Insert privileges on the customer table with the authority to grant those privileges to other users. User ted wants to grant all table-level privileges to user tania. So user ted issues the following GRANT statement:
GRANT ALL ON customer TO tania;
This statement executes successfully but returns SQLSTATE code 01007 for the following reasons: v The statement succeeds in granting the Select and Insert privileges to user tania because user ted has those privileges and the right to grant those privileges to other users. v The other privileges implied by the ALL keyword were not grantable by user ted and, therefore, were not granted to user tania. With Dynamic Server, if you grant all table-level privileges with the ALL keyword, the privileges includes the Under privilege only if the table is a typed table. The grant of ALL privileges does not include the Under privilege if the table is not based on a ROW type. If the table owner grants ALL privileges on a traditional relational table and later changes that table to a typed table, the table owner must explicitly grant the Under privilege to allow other users to create subtables under it.
Table Reference
You grant table-level privileges directly by specifying the name or an existing synonym of a table or of a view, which you can qualify with the owner name. Table Reference:
view table synonym
owner
Description Name of the user who owns the table, view, or synonym Synonym, table, or view on which privileges are granted
Restrictions Must be a valid authorization identifier The table, view, or synonym must exist in the database
2-377
GRANT
The object on which you grant privileges must reside in the current database. In an ANSI-compliant database, if owner is not enclosed between quotation marks, the database stores the owner name in lowercase letters.
Privileges on a View
You must have at least the Select privilege on a table or columns to create a view on that table. For views that reference only tables in the current database, if the owner of a view loses the Select privilege on any base table underlying the view, the view is dropped. You have the same privileges for the view that you have for the table or tables contributing data to the view. For example, if you create a view from a table to which you have only Select privileges, you can select data from your view but you cannot delete or update data. For information on how to create a view, see CREATE VIEW on page 2-247. When you create a view, PUBLIC does not automatically receive any privileges for a view that you create. Only you have access to table data through that view. Even users who have privileges on the base table of the view do not automatically receive privileges for the view. You can grant (or revoke) privileges on a view only if you are the owner of the underlying base tables, or if you received these privileges on the base table with the right to grant them (specified by the WITH GRANT OPTION keywords). You must explicitly grant those privileges within your authority, because PUBLIC does not automatically receive any privileges on a view when it is created. The creator of a view can explicitly grant Select, Insert, Delete, and Update privileges for the view to other users or to a role. You cannot grant Index, Alter, Under, or References privileges on a view (nor can you specify the ALL keyword for views, because ALL confers Index, References, and Alter privileges).
2-378
GRANT
v The Under privilege on a named ROW type Type-Level Privileges:
USAGE ON TYPE type_name UNDER ON TYPE row_type_name
Description Named ROW type on which the Under privilege is granted User-defined type on which the Usage privilege is granted
Restrictions Named ROW data type must exist User-defined data type must exist.
Syntax Identifier, p. 5-22; Data Type, p. 4-18 Identifier, p. 5-22; Data Type, p. 4-18
To see what privileges exist on user-defined data types, check the sysxtdtypes system catalog table for the owner of each UDT, and the sysxtdtypeauth system catalog table for any other users or roles that hold privileges on UDTs. See the IBM Informix Guide to SQL: Reference for information on the system catalog tables. For all the built-in data types, however, these access privileges are automatically available to PUBLIC and cannot be revoked.
USAGE Privilege
You own any user-defined data type (UDT) that you create. As owner, you automatically receive the Usage privilege on that data type and can grant the Usage privilege to others so that they can reference the type name or data of that type in SQL statements. DBAs can also grant the Usage privilege for UDTs. If you grant Usage privilege to a user (or to a role) that has Alter privileges, that grantee can add a column to a table that contains values of your UDT. Without privileges from the GRANT statement, any user can issue SQL statements that reference built-in data types. In contrast, a user must receive an explicit Usage privilege from a GRANT statement to use a distinct data type, even if the distinct type is based on a built-in type. For more information about user-defined types, see CREATE OPAQUE TYPE on page 2-136, CREATE DISTINCT TYPE on page 2-93, the discussion of data types in the IBM Informix Guide to SQL: Reference and the IBM Informix Database Design and Implementation Guide.
UNDER Privilege
You own any named ROW type that you create. If you want other users to be able to create subtypes under this named ROW type, you must grant to these users the Under privilege on your named ROW type. For example, suppose that you create a ROW type named rtype1:
CREATE ROW TYPE rtype1 (cola INT, colb INT)
If you want another user named kathy to be able to create a subtype under this named ROW type, you must grant the Under privilege on this named ROW type to user kathy:
GRANT UNDER ON ROW TYPE rtype1 TO kathy
2-379
GRANT
Now user kathy can create another ROW type under the rtype1 ROW type, even though kathy is not the owner of the rtype1 ROW type:
CREATE ROW TYPE rtype2 (colc INT, cold INT) UNDER rtype1
For more about named ROW types, see CREATE ROW TYPE on page 2-158, and the discussion of data types in the IBM Informix Guide to SQL: Reference and the IBM Informix Database Design and Implementation Guide.
Routine-Level Privileges
When you create a user-defined routine (UDR), you become owner of the UDR and you automatically receive the Execute privilege on that UDR. The Execute privilege allows you to invoke the UDR with an EXECUTE FUNCTION or EXECUTE PROCEDURE statement, whichever is appropriate, or with a CALL statement in an SPL routine. The Execute privilege also allows you to use a user-defined function in an expression, as in this example:
SELECT * FROM table WHERE in_stock(partnum) < 20
Routine-Level Privileges:
GRANT ON SPL_routine (1) PROCEDURE FUNCTION ROUTINE SPECIFIC ROUTINE FUNCTION PROCEDURE
Notes: 1 2 3
Element routine SPL_routine Description A user-defined routine An SPL routine
Whether you must grant the Execute privilege explicitly depends on the following conditions: v If you have DBA-level privileges, you can use the DBA keyword of CREATE FUNCTION or CREATE PROCEDURE to restrict the default Execute privilege to users with the DBA privilege. You must explicitly grant the Execute privilege on that UDR to users who do not have the DBA privilege. v If you have the Resource database-level privilege but not the DBA privilege, you cannot use the DBA keyword when you create a UDR: When you create a UDR in a database that is not ANSI compliant, PUBLIC can execute that UDR. You do not need to issue a GRANT statement for other users to receive the Execute privilege.
2-380
GRANT
Setting the NODEFDAC environment variable to yes prevents PUBLIC from executing the UDR until you explicitly grant the Execute privilege. v In an ANSI-compliant database, the creator of a UDR must explicitly grant the Execute privilege on the UDR for other users to be able to execute it. In Dynamic Server, if two or more UDRs have the same name, use a keyword from this list to specify which of those UDRs a user list can execute. Keyword SPECIFIC FUNCTION UDR that the User Can Execute The UDR identified by specific name Any function with the specified routine name (and parameter types that match routine parameter list, if specified)
PROCEDURE Any procedure with the specified routine name (and parameter types that match routine parameter list, if specified) ROUTINE Functions or procedures with the specified routine name (and parameter types that match routine parameter list, if specified)
If both a user-defined function and a user-defined procedure of Dynamic Server have the same name and the same list of parameter data types, you can grant the Execute privilege to both with the keyword ROUTINE. To limit the Execute privilege to one routine among several that have the same identifier, use the FUNCTION, PROCEDURE, or SPECIFIC keyword. To limit the Execute privilege to a UDR that accepts certain data types as arguments, include the routine parameter list or use the SPECIFIC keyword to introduce the specific name of a UDR. In Dynamic Server, if an external function has a negator function, you must grant the Execute privilege on both the external function and its negator function before users can execute the external function. A user must have the Usage privilege on a language to register a user-defined routine of Dynamic Server that is written in that language.
In this release of Dynamic Server, only the SPL keyword is supported in the USAGE ON LANGUAGE clause. When a user executes the CREATE FUNCTION or CREATE PROCEDURE statement to register a UDR that is written in SPL, the database server verifies that the user has the Usage privilege on the language in which the UDR is written. (In this release of Dynamic Server, the C language and the Java language do not require Usage privilege.)
Chapter 2. SQL Statements
2-381
GRANT
For information on other privileges that these statements require, see CREATE FUNCTION on page 2-107 and CREATE PROCEDURE on page 2-145.
Notes: 1
Element owner sequence synonym Description Owner of sequence (or owner of synonym) Sequence on which to grant privileges Synonym for a sequence object
Informix extension
Restrictions Must be the owner Must exist Must exist Syntax Owner Name, p. 5-43 Identifier, p. 5-22 Identifier, p. 5-22
The sequence object must exist in the current database. You can qualify the sequence or synonym identifier with a valid owner name, but the name of a remote database (or database@server) is not valid as a qualifier. You can include the WITH GRANT OPTION keywords when you grant ALTER, SELECT, or ALL to a user or to PUBLIC (but not to a role) as privileges on a sequence object.
Alter Privilege
You can grant the Alter privilege on a sequence to another user or to a role. The Alter privilege enables a specified user or role to modify the definition of a sequence with the ALTER SEQUENCE statement or to rename the sequence with the RENAME SEQUENCE statement.
Select Privilege
You can grant the Select privilege on a sequence to another user or to a role. The Select privilege enables a specified user or role to use sequence.CURRVAL and sequence.NEXTVAL expressions in SQL statements to read and to increment (respectively) the value of a sequence.
2-382
GRANT
ALL Keyword
You can specify the ALL keyword to grant both Alter and Select privileges on a sequence object to another user or to a role, or to a list of users or roles.
Element user
Description Login name of a user to whom you are granting privilege or granting a role
The following example grants the Insert table-level privilege on table1 to the user mary in a database that is not ANSI-compliant:
GRANT INSERT ON table1 TO mary;
In an ANSI-compliant database, if you do not include quotes as delimiters around user, the name of the user is stored in uppercase letters.
Role Name
You can use the GRANT statement to associate a list of one or more users (or all users, using the PUBLIC keyword) with a role name that can describe what they do. After you declare and grant a role, access privileges that you grant to that role are thereby granted to all users who are currently associated with that role. Role Name:
role role
Element role
Restrictions
Syntax
Must exist. If enclosed between quotation Owner Name, p. marks, role is case sensitive. 5-43
You can also grant an existing role to another role. This action gives whatever privileges the granted role possesses to all users who have the receiving role.
2-383
GRANT
another role. Users keep a role that was granted to them until the REVOKE statement breaks the association between their login names and the role name. Important: The CREATE ROLE and GRANT statements do not activate the role. A non-default role has no effect until SET ROLE enables it. The grantor or the grantee of a role can issue the SET ROLE statement. The following example shows the actions required to grant and activate the role payables to a group of employees who perform account payable functions. First the DBA creates role payables, then grants it to maryf.
CREATE ROLE payables; GRANT payables TO maryf WITH GRANT OPTION;
The DBA or maryf can activate the role with the following statement:
SET ROLE payables;
User maryf has the WITH GRANT OPTION authorization to grant payables to other employees who pay accounts.
GRANT payables TO charly, gene, marvin, raoul;
If you grant privileges for one role to another role, the recipient role has the combined set of privileges that have been granted to both roles. The following example grants the role petty_cash to the role payables:
CREATE ROLE petty_cash; SET ROLE petty_cash; GRANT petty_cash TO payables;
After all of these statements excute successfully, if user raoul uses the SET ROLE statement to make payables his current role, then (aside from the effects of any REVOKE operations) he holds the following combined set of access privileges: v The privileges granted to the payables role v The privileges granted to the petty_cash role v The privileges granted individually to raoul v The privileges granted to PUBLIC If you attempt to grant a role to yourself, either directly or indirectly, the database server generates an error. The database server also generates an error if you include the WITH GRANT OPTION keywords in a GRANT statement that assigns a role to another role.
2-384
GRANT
This example grants Insert privilege on the supplier table to the role payables:
GRANT INSERT ON supplier TO payables;
Anyone who has been granted the payables role, and who successfully activates it by issuing the SET ROLE statement, can now insert rows into supplier.
The last statement provides users mary, asok, and vlad with accounting as their default role. If any of these users connects to a database, that user activates whatever privileges the accounting role holds, in addition to any privileges that the user already possesses as an individual or as PUBLIC. The role must already exist and the user must have the access privileges to set the role. If the role has not previously been granted to a user, it is granted as part of setting the default role. If no default role is defined for a user nor for PUBLIC, then no role is set, and the existing privileges of the user are in effect. The following example shows how the default role can be assigned to all users:
DATABASE hrdb; CREATE ROLE emprole; GRANT CONNECT TO PUBLIC; GRANT SELECT ON emptab TO emprole; GRANT emprole TO PUBLIC; GRANT DEFAULT ROLE emprole TO PUBLIC;
Note: With Extended Parallel Server, using GRANT DEFAULT ROLE is an alternative to issuing the SET ROLE statement in the sysdbopen( ) procedure. Default roles defined using the sysdbopen( ) procedure, however, have precedence when a user establishes a connection. Changing the default role for a user or for PUBLIC only affects new database connections. Existing connection continue to run under currently assigned roles. If one default role was granted to user, and another default role was granted to PUBLIC, the default role granted to user takes precedence at connection time. A default role cannot be assigned to another role. Because roles are not defined across databases, the default role must be assigned for each database. No options besides the user-list are valid after the TO keyword in the GRANT DEFAULT ROLE statement. The database server issues an error if you attempt to include the AS grantor clause or the WITH GRANT OPTION clause.
Chapter 2. SQL Statements
2-385
GRANT
This statement enables user max to create or drop UDRs, without requiring max to issued the SET ROLE EXTEND statement. (Here the quotation marks preserve the lowercase letters in the authorization identifier max.) In databases for which this security feature is not needed, the DBSA can disable this restriction on who can create or drop external UDRs by setting the IFX_EXTEND_ROLE configuration parameter to OFF in the ONCONFIG file. When IFX_EXTEND_ROLE is set to OFF, any user who holds the Resource privilege can create or drop external UDRs. See Database-Level Privileges on page 2-373 for information about the Resource privileges.
User mary uses her privilege to grant users cathy and paul access to the table:
GRANT SELECT, UPDATE ON items TO cathy; GRANT SELECT ON items TO paul
Later you revoke the access privileges for user mary on the items table:
REVOKE SELECT, UPDATE ON items FROM mary
This single statement effectively revokes all privileges on the items table from users mary, cathy, and paul. If you want to create a chain of privileges with another user as the source of the privilege, use the AS grantor clause. The WITH GRANT OPTION keywords are valid only for users. They are not valid when you use GRANT to convey privileges or another role to a role.
2-386
GRANT
AS grantor Clause
When you grant privileges, by default, you are the one who can revoke those privileges. The AS grantor clause lets you establish another user as the source of the privileges you are granting. When you use this clause, the login provided in the AS grantor clause replaces your login in the appropriate system catalog table. You can use this clause only if you have the DBA privilege on the database. After you use this clause, only the specified grantor can REVOKE the effects of the current GRANT. Even a DBA cannot revoke a privilege unless that DBA is listed in the system catalog table as the source who granted the privilege. The following example illustrates this situation. You are the DBA and you grant all privileges on the items table to user tom with the right to grant all privileges:
REVOKE ALL ON items FROM PUBLIC; GRANT ALL ON items TO tom WITH GRANT OPTION
The following example illustrates a different situation. You also grant Select and Update privileges to user jim, but you specify that the grant is made as user tom. (The records of the database server show that user tom is the grantor of the grant in the systabauth system catalog table, rather than you.)
GRANT SELECT, UPDATE ON items TO jim AS tom
Later, you decide to revoke privileges on the items table from user tom, so you issue the following statement:
REVOKE ALL ON items FROM tom
If instead, however, you try to revoke privileges from user jim with a similar statement, the database server returns an error, as the next example shows:
REVOKE SELECT, UPDATE ON items FROM jim 580: Cannot revoke permission.
You receive an error because the database server record shows the original grantor as user tom, and you cannot revoke the privilege. Although you are the DBA, you cannot revoke a privilege that another user granted. In Extended Parallel Server, the AS grantor clause is not valid in the GRANT ROLE statement or the GRANT DEFAULT ROLE statement. In Dynamic Server, the AS grantor clause is not valid in the GRANT DEFAULT ROLE statement.
Related Information
Related statements: GRANT FRAGMENT, REVOKE, and REVOKE FRAGMENT For information about roles, see the following statements: CREATE ROLE, DROP ROLE, and SET ROLE. In the IBM Informix Database Design and Implementation Guide, see the discussion of privileges. For a discussion of how to embed GRANT and REVOKE statements in programs, see the IBM Informix Guide to SQL: Tutorial.
2-387
GRANT FRAGMENT
Use the GRANT FRAGMENT statement to grant to one or more users or roles any or all of the Insert, Update, and Delete access privileges on individual fragments of a table that has been fragmented by expression. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
(1) GRANT FRAGMENT Fragment-Level Privileges ON table
AS grantor
Notes: 1
Element Description Partition or dbspace that stores a fragment of table User who can revoke the privileges Role to receive privileges Fragmented table on which fragment privileges are granted User to whom privileges are to be granted
3 fragment
grantor role table user
Usage
The GRANT FRAGMENT statement is a special case of the GRANT statement for assigning privileges on table fragments. GRANT FRAGMENT is valid only for tables that are fragmented according to an expression-based distribution scheme. For an explanation of this type of fragmentation strategy, see Expression Distribution Scheme on page 2-18.
Fragment-Level Privileges
The keyword or keywords that follow the FRAGMENT keyword specify fragment-level privileges, which are a logical subset of table-level privileges:
2-388
GRANT FRAGMENT
Fragment-Level Privileges:
ALL , INSERT DELETE UPDATE
These keywords correspond to the following fragment-level privileges: Keyword ALL INSERT DELETE UPDATE Effect on Grantee Receives Insert, Delete, and Update privileges on the fragment Can insert rows into the fragment Can delete rows from the fragment Can update rows in the fragment and in any columns.
2-389
GRANT FRAGMENT
1. When a user executes an INSERT, DELETE, or UPDATE statement, the database server first checks whether the user has the table privileges necessary for the operation attempted. If the table privileges exist, the statement continues processing. 2. If the table privileges do not exist, the database server checks whether the table is fragmented by expression. If the table is not fragmented by expression, the database server returns an error to the user. This error indicates that the user does not have the privilege to execute the command. 3. If the table is fragmented by expression, the database server checks whether the user holds the fragment privileges necessary for the attempted operation. If the user holds the required fragment privileges, the database server continues to process the statement. If the fragment privileges do not exist, the database server returns an error to the user. This error indicates that the user does not have the privilege to execute the statement.
Specifying Fragments
3 You can specify one fragment or a comma-separated list of fragments, with the name (or list of names) enclosed between parentheses that immediately follow the ON table specification. You cannot use quotation marks to delimit fragment names. The database server issues an error if you include no fragment, or if no fragment of the specified table matches a fragment that you list. Each fragment must be referenced by the name of the partition where it is stored. If you did not declare an explicit identifier when you created the partition, its name defaults to the name of the dbspace that contains the partition. After a dbspace is renamed successfully by the onspaces utility, only the new name is valid. Dynamic Server automatically updates existing fragmentation strategies in the system catalog to substitute the new dbspace name, but you must specify the new name in GRANT FRAGMENT statement to reference a fragment whose default name is the name of a renamed dbspace.
The TO Clause
The list of one or more users or roles that follows the TO keyword identifies the grantees. You can specify the PUBLIC keyword to grant the specified fragment-level privileges to all users.
2-390
GRANT FRAGMENT
You cannot use GRANT FRAGMENT to grant fragment-level privileges to yourself, either directly or through roles. If you enclose user or role in quotation marks, the name is case sensitive and is stored exactly as you typed it. In an ANSI-compliant database, if you do not use quotes around user or around role, the name is stored in uppercase letters. The following statement grants the Insert, Update, and Delete privileges on the fragment of the customer table in part1 to user larry:
GRANT FRAGMENT ALL ON customer (part1) TO larry
The following statement grants the Insert, Update, and Delete privileges on the fragments of the customer table in part1 and part2 to user millie:
GRANT FRAGMENT ALL ON customer (part1, part2) TO millie
To grant privileges on all fragments of a table to the same user or users, you can use the GRANT statement instead of the GRANT FRAGMENT statement. You can also use the GRANT FRAGMENT statement for this purpose. Assume that the customer table is fragmented by expression into three fragments, and these fragments reside in the dbspaces named part1, part2, and part3. You can use either of the following statements to grant the Insert privilege on all fragments of the table to user helen:
GRANT FRAGMENT INSERT ON customer (part1, part2, part3) TO helen; GRANT INSERT ON customer TO helen;
The following statement grants the Insert, Update, and Delete privileges on the fragment of the customer table in part3 to users jerome and hilda:
GRANT FRAGMENT ALL ON customer (part3) TO jerome, hilda
The following statement grants the Update and Insert privileges on the fragment of the customer table in part1 to user susan:
GRANT FRAGMENT UPDATE, INSERT ON customer (part1) TO susan
The following statement grants the Insert, Update, and Delete privileges on the fragment of the customer table in part1 to user harry:
GRANT FRAGMENT ALL ON customer (part1) TO harry
2-391
GRANT FRAGMENT
AS grantor Clause
The AS grantor clause of the GRANT FRAGMENT statement can specify the grantor of the privilege. You can use this clause only if you have the DBA privilege on the database. hen you include the AS grantor clause, the database server lists the user or role who is specified as grantor as the grantor of the privilege in the grantor column of the sysfragauth system catalog table. In the next example, the DBA grants the Delete privilege on the fragment of the customer table in part3 to user martha, and uses the AS grantor clause to specify that user jack is listed in sysfragauth as the grantor of the privilege:
GRANT FRAGMENT DELETE ON customer (part3) TO martha AS jack
One effect of the AS grantor clause in the previous example is that user jack can execute the REVOKE FRAGMENT statement to cancel the Delete fragment-level privilege that martha holds, if this GRANT FRAGMENT statement were the only source of the fragment authority of martha on the customer rows in part3.
If you omit the AS grantor clause of GRANT FRAGMENT, or if you specify your own login name as the grantor, you can later use the REVOKE FRAGMENT statement to revoke the privilege that you granted to the specified user. For example, if you grant the Delete privilege on the fragment of the customer table in part3 to user martha but specify user jack as the grantor of the privilege, user jack can revoke that privilege from user martha, but you cannot revoke that privilege from user martha. The DBA, or the owner of the fragment, can use the AS clause of the REVOKE FRAGMENT statement to revoke privileges on the fragment.
Related Information
Related statements: GRANT and REVOKE FRAGMENT For a discussion of fragment-level and table-level privileges, see the IBM Informix Database Design and Implementation Guide.
2-392
INFO
Use the INFO statement to display information about databases tables. This statement is an extension to the ANSI/ISO standard for SQL. You can use this statement only with DBAccess.
Syntax
INFO TABLES COLUMNS INDEXES STATUS PRIVILEGES ACCESS FRAGMENTS REFERENCES FOR table
Element table
Usage
You can use the INFO statement with DBAccess to display information about database tables. C-style comments are not valid within the INFO statement. Keywords of the INFO statement can display the following information. INFO Keyword TABLES COLUMNS INDEXES FRAGMENTS ACCESS or PRIVILEGES Information Displayed Table names in the current database Column information for a specified table Index information for a specified table Fragmentation strategy for a specified table Access privileges for a specified table. (The ACCESS and PRIVILEGES keywords are synonyms in this context.) References privileges for columns of a specified table
REFERENCES
STATUS Status information for a specified table v TABLES Keyword Use TABLES to display a list of the tables in the current database, not including system catalog tables, in one of the following formats: If you are the owner of the cust_calls table, it appears as cust_calls. If you are not the owner of the cust_calls table, the name of the owner precedes the table name, such as june.cust_calls. v COLUMNS Keyword Use COLUMNS to display the names and data types of the columns in a specified table, showing for each column whether NULL values are allowed. v INDEXES Keyword
Chapter 2. SQL Statements
2-393
INFO
Use INDEXES to display the name, owner, and type of each index in a specified table, the clustered status, and listing the indexed columns. FRAGMENTS Keyword Use FRAGMENTS to display the names of dbspaces storing fragments of a table. If the table is fragmented with an expression-based distribution scheme, the INFO statement also shows the expressions. ACCESS or PRIVILEGES Keyword Use ACCESS or PRIVILEGES to display user-access privileges for a specified table. (These two keywords are synonyms in this context.) REFERENCES Keyword Use REFERENCES to display the References privilege for users for the columns of a specified table. For database-level privileges, use a SELECT statement to query the sysusers system catalog table. STATUS Keyword Use STATUS to display information about the owner, row length, number of rows and columns, creation date, and status of audit trails for a specified table.
An alternative to using the INFO statement of SQL is to use the Info command of the SQL menu or of the Table menu of DBAccess to display the same and additional information.
Related Information
Related statements: GRANT and REVOKE For a description of the Info option on the SQL menu or the TABLE menu in DBAccess, see the IBM Informix DBAccess User's Guide.
2-394
INSERT
Use the INSERT statement to insert one or more new rows into a table or view, or to insert one or more elements into an SPL or ESQL/C collection variable.
Syntax
INSERT
(1) INTO synonym view table ( (4) external , Subset of SELECT Statement ( INTO (5) (6) ATposition column ) (7) Collection-Derived Table Field Options (8) (9) (3) VALUES Clause , EXECUTE Routine Clause column ) Subset of SELECT Statement (3) (2)
Field Options:
(1) VALUES Clause , Subset of SELECT Statement field (3)
Notes: 1 2 3 4 5 6 7 8 9 See VALUES Clause on page 2-398 See Execute Routine Clause on page 2-404 See Subset of SELECT Statement on page 2-403 Extended Parallel Server only Stored Procedure Language only ESQL/C only See Collection-Derived Table on page 5-5 Informix extension Dynamic Server only
2-395
INSERT
Element column external field position synonym, table, view Description Column to receive new value External table into which to insert data Field of a named or unnamed ROW data type Position at which to insert an element of a LIST data type Table, view, or synonym in which to insert data Restrictions See Specifying Columns on page 2-396. Must exist Must already be defined in the database Literal integer or an INT or SMALLINT type SPL variable. Synonym or view and the table to which it points must exist Syntax Identifier on page 5-22 Database Object Name on page 5-17 Field Definition on page 2-160 Literal Number on page 4-137 Database Object Name on page 5-17
Usage
To insert data into a table, you must either own the table or have the Insert privilege for the table. (see GRANT on page 2-371.) To insert data into a view, you must have the required Insert privilege, and the view must meet the requirements explained in Inserting Rows Through a View on page 2-397. If the table or view has data integrity constraints, the inserted rows must meet the constraint criteria. If they do not, the database server returns an error. If the checking mode is set to IMMEDIATE, all specified constraints are checked at the end of each INSERT statement. If the checking mode is set to DEFERRED, all specified constraints are not checked until the transaction is committed.
Specifying Columns
If you do not explicitly specify one or more columns, data is inserted into columns using the column order that was established when the table was created or last altered. The column order is listed in the syscolumns system catalog table. In ESQL/C, you can use the DESCRIBE statement with an INSERT statement to identify the column order and the data type of the columns in a table. The number of columns specified in the INSERT INTO clause must equal the number of values supplied in the VALUES clause or by the SELECT statement, either implicitly or explicitly. If you specify a column list, the columns receive data in the order in which you list the columns. The first value following the VALUES keyword is inserted into the first column listed, the second value is inserted into the second column listed, and so on. If you omit a column from the column list, and the column does not have a default value associated with it, the database server places a NULL value in the column when the INSERT statement is executed.
2-396
INSERT
CREATE PROCEDURE test3() DEFINE a_list LIST(SMALLINT NOT NULL); SELECT list_col INTO a_list FROM table1 WHERE id = 201; INSERT AT 3 INTO TABLE(a_list) VALUES( 9 ); UPDATE table1 VALUES list_col = a_list WHERE id = 201; END PROCEDURE;
Suppose that before this INSERT, a_list contained the elements {1,8,4,5,2}. After this INSERT, a_list contains the elements {1,8,9,4,5,2}. The new element 9 was inserted at position 3 in the list. For more information on inserting values into collection variables, see Collection-Derived Table on page 5-5.
Columns in the underlying table that are unspecified in the view receive either a default value or a NULL value if no default is specified. If one of these columns has no default value, and a NULL value is not allowed, the INSERT fails. You can use data-integrity constraints to prevent users from inserting values into the underlying table that do not fit the view-defining SELECT statement. For further information, see WITH CHECK OPTION Keywords on page 2-250. With Dynamic Server, you can insert rows through a single-table or a multiple-table view if an INSTEAD OF trigger specifies valid INSERT operations in its Action clause. See INSTEAD OF Triggers on Views (IDS) on page 2-243 for information on how to create INSTEAD OF triggers that insert through views. If several users are entering sensitive information into a single table, the built-in USER function can limit their view to only the specific rows that each user inserted. The following example contains a view and an INSERT statement that achieves this effect:
CREATE VIEW salary_view AS SELECT lname, fname, current_salary FROM salary WHERE entered_by = USER INSERT INTO salary VALUES (Smith, Pat, 75000, USER)
2-397
INSERT
v In a database that is not ANSI-compliant, an OPEN statement implicitly closes and then reopens the cursor. v A COMMIT WORK statement ends the transaction. When the insert buffer is flushed, the client processor performs appropriate data conversion before it sends the rows to the database server. When the database server receives the buffer, it converts any user-defined data types and then begins to insert the rows one at a time into the database. If an error is encountered while the database server inserts the buffered rows into the database, any buffered rows that follow the last successfully inserted rows are discarded.
VALUES Clause
The VALUES clause can specify values to insert into one or more columns. When you use the VALUES clause, you can insert only one row at a time. Each value that follows the VALUES keyword is assigned to the corresponding column listed in the INSERT INTO clause (or in column order, if a list of columns is not specified). If you are inserting a quoted string into a column, the maximum length that can be inserted without error is 256 bytes. VALUES Clause:
2-398
INSERT
, VALUES ( input_var (1) : (2) $ NULL USER (3) Quoted String (4) Literal Number (2) (5) Constant Expression (6) Column Expression (7) (8) Literal Collection (9) Literal Row (10) Expression literal_Boolean literal_opaque indicator_var indicator_var )
Notes: 1 2 3 4 5 6 7 8 9 10
Element indicator_var input_var literal_opaque Description Variable to show if SQL statement returns NULL to input_var Variable that holds value to insert. This can be a collection variable. Literal representation for an opaque data type Literal representation of a BOOLEAN value as a single character
ESQL/C only Informix extension See Quoted String on page 4-142 See Literal Number on page 4-137 See Constant Expressions on page 4-53 See Column Expressions on page 4-44 Dynamic Server only See Literal Collection on page 4-129 See Literal Row on page 4-139 See Expression on page 4-34
Restrictions See the IBM Informix ESQL/C Programmer's Manual. Can contain any value option of VALUES clause Must be recognized by the input support function of the opaque data type Either t (TRUE) or f (FALSE) Syntax Language specific Language specific See documentation of the opaque type. Quoted String on page 4-142
literal_Boolean
2-399
INSERT
In ESQL/C, if you use an input_var variable to specify the value, you can insert character strings longer than 256 bytes into a table. For the keywords and the types of literal values that are valid in the VALUES clause, refer to Constant Expressions on page 4-53.
2-400
INSERT
insert values of opaque UDTs into columns of tables in the local database, or into columns of tables in other databases of the local Dynamic Server instance. Some opaque data types require special processing when they are inserted. For example, if an opaque data type contains spatial or multirepresentational data, it might provide a choice of how to store the data: inside the internal structure or, for large objects, in a smart large object. This is accomplished by calling a user-defined support function called assign( ). When you execute INSERT on a table whose rows contains one of these opaque types, the database server automatically invokes the assign( ) function for the type. The assign( ) function can make the decision of how to store the data. For more information about the assign( ) support function, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
The collection column, list1, in this example, has three elements. Each element is an unnamed row type with an INTEGER field and a CHAR(5) field. The first element is composed of two literal values, an integer (1) and a quoted string (abcde). The second and third elements also use a quoted string to indicate the second field, but specify the value for the first field with an expression. Regardless of what method you use to insert values into a collection column, you cannot insert NULL elements into the column. Thus expressions that you use cannot evaluate to NULL. If the collection that you are attempting to insert contains a NULL element, the database server returns an error. You can also use a collection variable to insert the values of one or more collection elements into a collection column. For more information, see Collection-Derived Table on page 5-5.
2-401
INSERT
city CHAR(15), state CHAR(2), zipcode CHAR(9) ); CREATE TABLE employee ( name ROW ( fname CHAR(20), lname CHAR(20)), address address_t );
The next example inserts literal values in the name and address columns:
INSERT INTO employee VALUES ( ROW(John, Williams), ROW(103 Baker St, Tracy,CA, 94060)::address_t )
INSERT uses ROW constructors to generate values for the name column (an unnamed ROW data type) and the address column (a named ROW data type). When you specify a value for a named ROW data type, you must use the CAST AS keywords or the double colon ( :: ) operator, with the name of the ROW data type, to cast the value to the named ROW data type. For the syntax of ROW constructors, see Constructor Expressions (IDS) on page 4-63 in the Expression segment. For information on literal values for named ROW and unnamed ROW data types, see Literal Row on page 4-139. When you use a ROW variable in the VALUES clause, the ROW variable must contain values for each field value. For more information, see Inserting into a Row Variable (ESQL/C, IDS, SPL) on page 2-405. You can use ESQL/C host variables to insert nonliteral values in two ways: v An entire ROW type into a column. Use a row variable in the VALUES clause to insert values for all fields in a ROW column at one time. v Individual fields of a ROW type. To insert nonliteral values in a ROW-type column, insert the elements into a row variable and then specify the collection variable in the SET clause of an UPDATE statement.
2-402
INSERT
configuration parameters. This applies to any communication between Dynamic Server instances, even if both database servers reside on the same computer.
In this example, a NULL value is explicitly entered in the order_date column, and all other columns of the orders table that are not explicitly listed in the INSERT INTO clause are also filled with NULL values.
The database server truncates the data values in the following INSERT statements to "jo" and "sa" respectively, but does not return a warning:
INSERT INTO tab1 VALUES ("john"); INSERT INTO tab1 VALUES ("sally");
Thus, in a database that is not ANSI-compliant, the semantic integrity of data for a CHAR(n) column or variable is not enforced when the value inserted or updated exceeds the declared length n. (But in an ANSI-compliant database, the database server issues error -1279 when truncation of character data occurs.)
2-403
INSERT
v FIRST and INTO TEMP v ORDER BY and UNION Extended Parallel Server supports the ORDER BY and UNION options, but not FIRST nor INTO TEMP in the INSERT statement. In an ANSI-compliant database, if this statement has a WHERE clause that does not return rows, sqlca returns SQLNOTFOUND (100). If an INSERT statement that is part of a multistatement prepared object inserts no rows, sqlca returns SQLNOTFOUND (100) for both ANSI-compliant databases and databases that are not ANSI-compliant. In databases that are not ANSI-compliant, sqlca returns zero (0) if no rows satisfy the WHERE clause. In Dynamic Server, if you are inserting values into a supertable in a table hierarchy, the subquery can reference a subtable. If you are inserting values into a subtable in a table hierarchy, the subquery can reference the supertable if it references only the supertable. That is, the subquery must use the SELECT...FROM ONLY (supertable)...syntax.
Notes: 1 2
Element function, procedure Description User-defined function or procedure to insert the data
2-404
INSERT
When you use a user-defined function to insert column values, the return values of the function must have a one-to-one correspondence with the listed columns. That is, each value that the function returns must be of the data type expected by the corresponding column in the column list. For backward compatibility, Dynamic Server can use the EXECUTE PROCEDURE keywords to execute an SPL function that was created with the CREATE PROCEDURE statement. If the called SPL routine scans or updates the target table of the INSERT statement, the database returns an error. That is, the SPL routine cannot select data from the table into which you are inserting rows. If a called SPL routine contains certain SQL statements, the database server returns an error. For information on which SQL statements cannot be used in an SPL routine that is called within a data manipulation statement, see Restrictions on SPL Routines in Data-Manipulation Statements on page 5-71.
For more information, see Updating a Row Variable (IDS, ESQL/C) on page 2-647.
2-405
INSERT
you compile. For more information, refer to the dynamic management section of the IBM Informix ESQL/C Programmer's Manual.
Related Information
Related statements: CLOSE, CREATE EXTERNAL TABLE (XPS), DECLARE, DESCRIBE, EXECUTE, FLUSH, FOREACH, OPEN, PREPARE, PUT, and SELECT For a task-oriented discussion of inserting data into tables and for information on how to access row and collections with SPL variables, see the IBM Informix Guide to SQL: Tutorial. For a discussion of the GLS aspects of the INSERT statement, see the IBM Informix GLS User's Guide. For information on how to access row and collection values with ESQL/C host variables, see the chapter on complex data types in the IBM Informix ESQL/C Programmer's Manual.
2-406
LOAD
Use the LOAD statement to insert data from an operating-system file into an existing table or view. This statement is an extension to the ANSI/ISO standard for SQL. You can use this statement only with DBAccess.
Syntax
LOAD FROM filename DELIMITER delimiter table view synonym ( INSERT INTO
, column )
Syntax Identifier on page 5-22 Quoted String on page 4-142 Specific to operating system rules Database Object Name on page 5-17
Character to separate data values in each See DELIMITER Clause on line of the load file. Default delimiter is page 2-411. the pipe ( | ) symbol. Path and filename of file to read. Default See LOAD FROM File on pathname is current directory page 2-407.
filename
synonym, table, Synonym for the table in which to insert Synonym and table or view to view data from filename which it points must exist
Usage
The LOAD statement appends new rows to the table. It does not overwrite existing data. You cannot add a row that has the same key as an existing row. C-style comments are not valid within the LOAD statement. To use the LOAD statement, you must have Insert privileges for the table where you want to insert data. For information on database-level and table-level privileges, see the GRANT statement.
2-407
LOAD
corresponding column. Specify only values that can convert to the data type of the corresponding column. The following table indicates how the database server expects you to represent the data types in the LOAD FROM file (when you use the default locale, U.S. English).
Type of Data blank Input Format One or more blank characters between delimiters You can include leading blanks in fields that do not correspond to character columns. A t or T indicates a TRUE value, and an f or F indicates a FALSE value. Collection must have its values surrounded by braces ( { } ) and a field delimiter separating each element. For more information, see Loading Complex Data Types (IDS) on page 2-411. Character string in the following format: mm/dd/year You must state the month as a two-digit number. You can use a two-digit number for the year if the year is in the 20th century. (You can specify another century algorithm with the DBCENTURY environment variable.) The value must be an actual date; for example, February 30 is illegal. You can use a different date format if you indicate this format with the GL_DATE or DBDATE environment variable. For more information about environment variables, see the IBM Informix Guide to SQL: Reference and the IBM Informix GLS User's Guide. Value that can include a leading and/or trailing currency symbol and thousands and decimal separators Your locale files or the DBMONEY environment variable can specify a currency format. Nothing between the delimiters ROW type must have its values surrounded by parentheses and a field delimiter that separates each element. For more information, see Loading Complex Data Types (IDS) on page 2-411. TEXT and BYTE columns are loaded directly from the LOAD TO file. For more information, see Loading Simple Large Objects on page 2-410. CLOB and BLOB columns are loaded from a separate operating-system file. The field for the CLOB or BLOB column in the LOAD FROM file contains the name of this separate file. For more information, see Loading Smart Large Objects (IDS) on page 2-410. Character string in year-month-day hour:minute:second.fraction format You cannot use data type keywords or qualifiers for DATETIME or INTERVAL values. The year must be a 4-digit number, and the month must be a 2-digit number. The DBTIME or GL_DATETIME environment variable can specify other formats.
BOOLEAN COLLECTIONS
DATE
Simple large objects (TEXT, BYTE) Smart large objects (CLOB, BLOB)
Time
2-408
LOAD
Type of Data Input Format
User-defined data formats Associated opaque type must have an import support function (opaque types) defined if special processing is required to copy the data in the LOAD FROM file to the internal format of the opaque type. An import binary support function might also be required for data in binary format. The LOAD FROM file data must be in the format that the import or import binary support function expects. The associated opaque type must have an assign support function if special processing is required before writing the data in the database. See Loading Opaque-Type Columns (IDS) on page 2-411.
For more information on DB* environment variables, refer to the IBM Informix Guide to SQL: Reference. For more information on GL* environment variables, refer to the IBM Informix GLS User's Guide. If you are using a nondefault locale, the formats of DATE, DATETIME, MONEY, and numeric column values in the LOAD FROM file must be compatible with the formats that the locale supports for these data types. For more information, see the IBM Informix GLS User's Guide. The following example shows the contents of an input file named new_custs:
0|Jeffery|Padgett|Wheel Thrills|3450 El Camino|Suite 10|Palo Alto|CA|94306|| 0|Linda|Lane|Palo Alto Bicycles|2344 University||Palo Alto|CA|94301| (415)323-6440
This data file conveys the following information: v Indicates a serial field by specifying a zero (0) v Uses the pipe ( | ), the default delimiter v Assigns NULL values to the phone field for the first row and the address2 field for the second row The NULL values are shown by two delimiters with nothing between them. The following statement loads the values from the new_custs file into the customer table that jason owns:
LOAD FROM new_custs INSERT INTO jason.customer
If you include any of the following special characters as part of the value of a field, you must precede the character with a backslash ( \ ) escape symbol: v Backslash v Delimiter v Newline character anywhere in the value of a VARCHAR or NVARCHAR column v Newline character at end of a value for a TEXT value Do not use the backslash character ( \ ) as a field separator. It serves as an escape character to inform the LOAD statement that the next character is to be interpreted as part of the data, rather than as having special significance. Fields that correspond to character columns can contain more characters than the defined maximum allows for the field. The extra characters are ignored.
2-409
LOAD
If you are loading files that contain VARCHAR data types, note the following information: v If you give the LOAD statement data in which the character fields (including VARCHAR) are longer than the column size, the excess characters are disregarded. v Use the backslash ( \ ) to escape embedded delimiter and backslash characters in all character fields, including VARCHAR. v Do not use the following characters as delimiting characters in the LOAD FROM file: digits ( 0 to 9), the letters a to f, and A to F, the backslash ( / ) character, or the NEWLINE character.
In this format, start_off is the starting offset (in hexadecimal) of the smart-large-object value within the client file, length is the length (in hexadecimal) of the BLOB or CLOB value, and client_path is the pathname for the client file. No blank spaces can appear between these values. For example, to load a CLOB value that is 512 bytes long and is at offset 256 in the /usr/apps/clob9ce7.318 file, the database server expects the CLOB value to appear as follows in the LOAD FROM file:
|100,200,/usr/apps/clob9ce7.318|
2-410
LOAD
If the whole client file is to be loaded, a CLOB or BLOB column value appears as follows in the LOAD FROM file:
client_path
For example, to load a CLOB value that occupies the entire file /usr/apps/clob9ce7.318, the database server expects the CLOB value to appear as follows in the LOAD FROM file:
|/usr/apps/clob9ce7.318|
In DB-Access, the USING clause is valid within files executed from DB-Access. In interactive mode, DB-Access prompts you for a password, so the USING keyword and validation_var are not used. For CLOB columns, the database server handles any required code-set conversions for the data. See also the IBM Informix GLS User's Guide.
For example, to load the SET values {1, 3, 4} into a column whose data type is SET(INTEGER NOT NULL), the corresponding field of the LOAD FROM file appears as:
|SET{1 , 3 , 4}|
v Row types (named and unnamed) are introduced with the ROW constructor and their fields are enclosed with parentheses and separated with a comma, as follows:
ROW(val1 , val2 , ... )
For example, to load the ROW values (1, abc), the corresponding field of the LOAD FROM file appears as:
|ROW(1 , abc)|
DELIMITER Clause
Use the DELIMITER clause to specify the delimiter that separates the data contained in each column in a row in the input file. You can specify TAB (CTRL-I) or a blank space (= ASCII 32) as the delimiter symbol. You cannot use the following items as the delimiter symbol: v Backslash ( \ )
Chapter 2. SQL Statements
2-411
LOAD
v NEWLINE character (CTRL-J) v Hexadecimal numbers (0 to 9, a to f, A to F) If you omit this clause, the database server checks the DBDELIMITER environment variable. For information about how to set the DBDELIMITER environment variable, see the IBM Informix Guide to SQL: Reference. If the DBDELIMITER environment variable has not been set, the default delimiter is the pipe ( | ). The following example specifies the semicolon ( ; ) as the delimiting character. The example uses Windows file-naming conventions.
LOAD FROM C:\data\loadfile DELIMITER ; INSERT INTO orders
Related Information
Related statements: UNLOAD and INSERT For a task-oriented discussion of the LOAD statement and other utilities for moving data, see the IBM Informix Migration Guide. For a discussion of the GLS aspects of the LOAD statement, see the IBM Informix GLS User's Guide.
2-412
LOCK TABLE
Use the LOCK TABLE statement to control access to a table by other processes. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
LOCK TABLE table synonym IN SHARE EXCLUSIVE MODE
Description
Restrictions
Syntax
Synonym for the table to be Synonym and the table to which it Database Object Name on page locked points must exist 5-17 Table to be locked See first paragraph of Usage. Database Object Name on page 5-17
Usage
You can use LOCK TABLE to lock a table if either of the following is true: v You are the owner of the table. v You have Select privilege on the table or on a column in the table, either from a direct grant or from a grant to PUBLIC. The LOCK TABLE statement fails if the table is already locked in EXCLUSIVE mode by another process, or if you request an EXCLUSIVE lock while another user has locked the same table in SHARE mode. The SHARE keyword locks a table in shared mode. Shared mode gives other processes read access to the table but denies write access. Other processes cannot update or delete data if a table is locked in shared mode. The EXCLUSIVE keyword locks a table in exclusive mode. This mode denies other processes both read and write access to the table. Exclusive-mode locking automatically occurs during the ALTER INDEX, ALTER TABLE, CREATE INDEX, DROP INDEX, RENAME COLUMN, RENAME TABLE, START VIOLATIONS TABLE, STOP VIOLATIONS TABLE, and TRUNCATE statements.
2-413
LOCK TABLE
Warning: It is recommended that you not use nonlogging tables in a transaction. If you need to use a nonlogging table in a transaction, either lock the table in exclusive mode or set the isolation level to Repeatable Read to prevent concurrency problems.
2-414
LOCK TABLE
The following example shows how to change the lock mode of a table in a database that was created without transactions:
LOCK TABLE orders IN EXCLUSIVE MODE . . . UNLOCK TABLE orders . . . LOCK TABLE orders IN SHARE MODE
Locking Granularity
The default granularity for locking a table is at the page level, or whatever you specify (either PAGE or ROW) in the IFX_TABLE_LOCKMODE environment variable, or if that is not set, by setting DEF_TABLE_LOCKMODE in the ONCONFIG file. The LOCKMODE clause of the CREATE TABLE or ALTER TABLE statement can override the default locking granularity by specifying PAGE, ROW, or (for Extended Parallel Server only) TABLE for a specified table. The LOCK TABLE statement, however, always locks the entire table, overriding any other locking granularity specification for the table. For Extended Parallel Server, the LOCK MODE clause of the CREATE INDEX statement can specify COARSE or NORMAL locking granularity for an index. In all of these contexts, the term lock mode means the locking granularity. In the context of the SET LOCK MODE statement, however, lock mode refers to the behavior of the database server when a process attempts to access a row or a table that another process has locked.
Related Information
Related statements: BEGIN WORK, COMMIT WORK, SAVE EXTERNAL DIRECTIVES, SET ISOLATION, SET LOCK MODE, and UNLOCK TABLE For a discussion of concurrency and locks, see the IBM Informix Guide to SQL: Tutorial.
2-415
MERGE
The MERGE statement provides an efficient way to merge two tables by combining UPDATE and INSERT operations into a single statement. Only Extended Parallel Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
MERGE INTO (1) Optimizer Directives target_table target_view target_synonym alias AS
Notes: 1 2 3 4
Element alias
See Optimizer Directives on page 5-34 See Condition on page 4-5 See SET Clause on page 2-639 See VALUES Clause on page 2-398
Restrictions If potentially ambiguous, you must precede alias with the AS keyword Must exist in the target object Syntax Identifier on page 5-22 Identifier on page 5-22 Database Object Name on page 5-17; SELECT, p. SELECT on page 2-479 Database Object Name on page 5-17
Description Temporary name that you declare here for the target table object or source table object Name of a column in the target table object in which to insert source data Name of a table object (or the result of a query) that contains the data that you are relocating
column
Must exist
Name or synonym of a table or updatable view in which to insert the source data
Usage
The MERGE statement enables you to merge data of a source table into a destination table, here called the target table. It provides an efficient way to update and insert the rows of the source table into the target table, based on search conditions that you specify within the statement.
2-416
MERGE
The target object can be in a different database from the source object, but it must be a database managed by the currently running Extended Parallel Server instance. The source object, however, can be in a different database from the target object, and need not be a database managed by the current running Extended Parallel Server instance. Rows in the target object that match the search conditions are updated; otherwise, new rows are inserted into the target object. The MERGE statement combines some of the functionality of the UPDATE and INSERT statements into a single statement. The SET clause of this statement is identical to that of the UPDATE statement. For more information, see SET Clause on page 2-639. The VALUES clause of the MERGE is similar to the INSERT statement, but only accepts column names and constant values, not expressions. For more information, see VALUES Clause on page 2-398. The following example uses the transaction table new_sale to merge rows into the fact table sale, updating sale_count if there are already records in the sale table. If not, the rows are inserted into the sale table.
MERGE INTO sale USING new_sale AS n ON sale.cust_id = n.cust_id WHEN MATCHED THEN UPDATE SET sale.salecount = sale.salecount + n.salecount WHEN NOT MATCHED THEN INSERT (cust_id, salecount) VALUES (n.cust_id,n.salecount);
If an error occurs while MERGE is executing, the entire statement is rolled back. If the constraints checking mode for the target table is set to IMMEDIATE, then unique and referential constraints of the target object are checked after all the UPDATE and INSERT operations are complete. The NOT NULL and check constraints are checked during the UPDATE and INSERT operations. If the checking mode is set to DEFERRED, the constraints are not checked until after the transaction is committed.
2-417
MERGE
Records in sales Table cust_id Tom Julie sale_count 129 230 Records in new_sales Table cust_id Tom Julie Julie sale_count 20 3 10
When merging new_sale into sale using the expression sale.cust_id = new_sale.cust_id, the MERGE statement returns an error, because it attempts to update one of the records in the source table more than once.
Related Information
Related statements: INSERT andUPDATE
2-418
MOVE TABLE
The MOVE TABLE statement moves the schema of a database table to another database of the same database server instance. Only Extended Parallel Server supports this syntax, which is an extension to the ANSI/ISO standard for SQL.
Syntax
MOVE TABLE owner. source_table TO DATABASE database
RENAME new_owner.
new_table
Description Name of the destination database Current table owner Owner designated here for new_table Name declared here for relocated table Current table name
Restrictions
Syntax
Must exist and must have the sane database Database Name on page server as the source database 5-15 Must be the owner of source_table Must be a valid owner name in the destination database Must be unique among table, view, and synonym names in destination database Must exist in the source database Owner Name on page 5-43 Owner Name on page 5-43 Identifier on page 5-22 Identifier on page 5-22
Usage
The MOVE TABLE statement provides a way to move a permanent table from one database to another on the same Extended Parallel Server instance. When the table is moved, many of its objects, including any indexes and fragmentation strategy, are preserved, but the data values in the table are not physically moved to the destination database. The database server transfers only the system catalog information describing source_table to the destination database. For this reason, the performance of MOVE TABLE depends on the time that is required to transfer the system catalog data, but not on the size of source table. If indexes exist on source table, the data values of the index are not moved. Only the system catalog information is moved to the destination database. By default, all roles assigned to source table are dropped from the source database. The destination table has, by default, restricted privileges. Only the owner of source table has privileges on new table. Also by default, all roles assigned to source table are dropped from new table. In the following basic example, the privileges of source table are not preserved in the destination database:
MOVE TABLE employee TO DATABASE payroll;
2-419
MOVE TABLE
You can preserve the privileges and roles of the original table using the WITH clause. For more information, see Preserving Table Privileges on page 2-421. Only users with DBA privileges on both the source and destination databases can use the MOVE TABLE statement. The following types of tables cannot be moved using the MOVE TABLE statement: v System catalog tables v Temporary tables (TEMP or SCRATCH) v Tables not currently in the database v Active violations tables v Tables appearing in the FROM clause of a GK index v System database tables (tables in sysmaster and sysutils) If the destination database already contains a table with the same name as source table, the MOVE TABLE statement fails with an error. Similarly, if any dependent objects of source table have names that already exist in the destination database, the MOVE TABLE statement fails and returns an error. The source table can be either fragmented or non-fragmented. You must keep the dbspace and fragmentation schema in mind, however, because the data values of the destination table still reside in the same dbspace. MOVE TABLE does not provide a mechanism to move data from one dbspace to another. The MOVE TABLE statement can only move tables between databases. Use the RENAME TABLE statement to move a table within the same database.
2-420
MOVE TABLE
All column distributions of source table are preserved in the destination table. Entries for source table in the sysdistrib system catalog table are moved. This ensures that the destination table has the same statistics as the source table. For this reason, you do not need to run the UPDATE STATISTICS statement after moving a table. A source table of the MOVE TABLE statement receives a new tabid as if it were a newly created table. This means that system-defined names of objects that are dependent on source table are also affected. User-defined object names are preserved after the table is moved.
2-421
MOVE TABLE
dropped from the source database. The following MOVE TABLE example preserves the user privileges of source table and changes the owner of new table:
MOVE TABLE customer TO DATABASE sales RENAME mary.customer WITH (PRIVILEGES);
The WITH (PRIVILEGES, ROLES) Option: This clause specifies that all privileges on source table to be moved to new table, including any privileges of roles. All entries for source table in the systabauth and syscolauth system catalog tables are transferred to the destination table. The following example preserves both user and role privileges in new table:
MOVE TABLE mytab TO DATABASE newdb WITH (PRIVILEGES, ROLES);
The MOVE TABLE statement returns an error if there is a conflict between user names and role names in the source and destination databases. If any roles that are preserved in the destination table are not defined in the destination database, an error is returned. It is the responsibility of the DBA to ensure that role names and user names are correctly defined in the source and destination databases.
2-422
MOVE TABLE
Related Information
Related statements: CREATE TABLE, DROP TABLE, INSERT, RENAME TABLE
2-423
OPEN
Use the OPEN statement to activate a cursor. Use this statement with ESQL/C.
Syntax
OPEN cursor_id (1) cursor_id_var
Notes: 1
Element cursor_id cursor_id_var descriptor descriptor_var parameter_var Description Name of a cursor Host variable = cursor_id Name of a system-descriptor area Host variable that identifies the system-descriptor area
Informix extension
Restrictions Must have been declared Must be a character data type Must have been allocated System-descriptor area must have been allocated Syntax Identifier on page 5-22 Language specific Quoted String on page 4-142 Quoted String on page 4-142 Language specific
Host variable whose contents replace Must be a character or collection a question ( ? ) mark placeholder in a data type prepared statement Pointer to sqlda structure defining data type and memory location of values to replace question ( ? ) marks in a prepared statement Cannot begin with a dollar ( $ ) sign nor with a colon ( : ). You must use an sqlda structure with dynamic SQL statements.
sqlda_pointer
Usage
A cursor is a database object that can contain an ordered set of values. The OPEN statement activates a cursor that the DECLARE statement created. Cursor can be classified by their associated SQL statements: v A select cursor: a cursor that is associated with a SELECT statement v A function cursor: a cursor that is associated with the EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement v An insert cursor: a cursor that is associated with the INSERT statement v A collection cursor: in Dynamic Server, a select or insert cursor that operates on a collection variable.
2-424
OPEN
The specific actions that the database server takes differ, depending on the statement with which the cursor is associated. When you associate one of the previous statements with a cursor directly (that is, you do not prepare the statement and associate the statement identifier with the cursor), the OPEN statement implicitly prepares the statement. In an ANSI-compliant database, you receive an error code if you try to open a cursor that is already open.
In Extended Parallel Server, to re-create this example, use the CREATE PROCEDURE statement instead of the CREATE FUNCTION statement.
2-425
OPEN
Even if the values of the variables are unchanged, the values in the active set can be different, in the following situations: v If the user-defined function takes a different execution path from the previous OPEN statement on a function cursor v If data in the table was modified since the previous OPEN statement on a select cursor The database server can process most queries dynamically, without pre-fetching all rows when it opens the select or function cursor. Therefore, if other users are modifying the table at the same time that the cursor is being processed, the active set might reflect the results of these actions. For some queries, the database server evaluates the entire active set when it opens the cursor. These queries include those with the following features: v Queries that require sorting: those with an ORDER BY clause or with the DISTINCT or UNIQUE keyword v Queries that require hashing: those with a join or with the GROUP BY clause For these queries, any changes that other users make to the table while the cursor is being processed are not reflected in the active set.
If the SELECT, SELECT...FOR UPDATE, EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement is valid, but no rows match its criteria, the first FETCH statement returns a value of 100 (SQLNOTFOUND), meaning no rows were found. Tip: When you encounter an SQLCODE error, a corresponding SQLSTATE error value also exists. For information about how to view the message text, refer to the GET DIAGNOSTICS statement.
2-426
OPEN
EXEC SQL prepare s1 from insert into manufact values (npr, napier); EXEC SQL declare in_curs cursor for s1; EXEC SQL open in_curs; EXEC SQL put in_curs; EXEC SQL close in_curs;
Reopening an Insert Cursor: When you reopen an insert cursor that is already open, you effectively flush the insert buffer; any rows that are stored in the insert buffer are written into the database table. The database server first closes the cursor, which causes the flush and then reopens the cursor. For information about how to check errors and count inserted rows, see Error Checking on page 2-447. In an ANSI-compliant database, you receive an error code if you try to open a cursor that is already open.
USING Clause
The USING clause is required when the cursor is associated with a prepared statement that includes question-mark ( ? ) placeholders, as follows: v A SELECT statement with input parameters in its WHERE clause v An EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement with input parameters as arguments of its user-defined function v An INSERT statement with input parameters in its VALUES clause You can supply values for these parameters in one of the following ways: v You can specify one or more host variables. v You can specify a system-descriptor area. v You can specify a pointer to an sqlda structure. For more information, see PREPARE on page 2-433. If you know the number of parameters to be supplied at runtime and their data types, you can define the parameters that are needed by the statement as host variables in your program. You pass parameters to the database server by opening the cursor with the USING keyword, followed by the names of the variables. These variables are matched with the SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement question-mark ( ? ) parameters in a one-to-one correspondence, from left to right. You cannot include indicator variables in the list of variable names. To use an indicator variable, you must include the SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement as part of the DECLARE statement. You must supply one host variable name for each placeholder. The data type of each variable must be compatible with the corresponding type that the prepared
Chapter 2. SQL Statements
2-427
OPEN
statement requires. The following ESQL/C code fragment opens a select cursor and specifies host variables in the USING clause:
sprintf (select_1, "%s %s %s %s %s", "SELECT o.order_num, sum(total price)", "FROM orders o, items i", "WHERE o.order_date > ? AND o.customer_num = ?", "AND o.order_num = i.order_num", "GROUP BY o.order_num"); EXEC SQL prepare statement_1 from :select_1; EXEC SQL declare q_curs cursor for statement_1; EXEC SQL open q_curs using :o_date, :o.customer_num;
The following example illustrates the USING clause of the OPEN statement with an EXECUTE FUNCTION statement in an ESQL/C code fragment:
stcopy ("EXECUTE FUNCTION one_func(?, ?)", exfunc_stmt); EXEC SQL prepare exfunc_id from :exfunc_stmt; EXEC SQL declare func_curs cursor for exfunc_id; EXEC SQL open func_curs using :arg1, :arg2;
In Extended Parallel Server, to re-create this example use the CREATE PROCEDURE statement instead of the CREATE FUNCTION statement.
As the example indicates, the system descriptor area must be allocated before you reference it in the OPEN statement.
2-428
OPEN
The sqlda value specifies the number of input values that are described in occurrences of sqlvar. This number must correspond to the number of dynamic parameters in the prepared statement. Example of Specifying a Pointer to an sqlda Structure: The following example shows an OPEN ... USING DESCRIPTOR statement:
struct sqlda *sdp; ... EXEC SQL open selcurs using descriptor sdp;
The WITH REOPTIMIZATION option forces the database server to optimize the query-design plan before it processes the OPEN cursor statement. The following example uses the WITH REOPTIMIZATION keywords:
EXEC SQL open selcurs using descriptor sdp with reoptimization;
2-429
OPEN
3 3 3 with a cursor, you must prepare the statement again and declare the cursor again before you can open the cursor. You cannot simply reopen a cursor that is based on a prepared statement that is no longer valid.
Related Information
Related statements: ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, DESCRIBE, CLOSE, DECLARE, EXECUTE, FETCH, FLUSH, FREE, GET DESCRIPTOR, PREPARE, PUT, SET AUTOFREE, SET DEFERRED_PREPARE, and SET DESCRIPTOR For a task-oriented discussion of the OPEN statement, see the IBM Informix Guide to SQL: Tutorial. For more information on system-descriptor areas and the sqlda structure, refer to the IBM Informix ESQL/C Programmer's Manual.
2-430
OUTPUT
The OUTPUT statement writes query results in an operating-system file, or pipes query results to another program This statement is an extension to the ANSI/ISO standard for SQL. You can use this statement only with DBAccess.
Syntax
OUTPUT TO filename (1) PIPE WITHOUT HEADINGS program
Notes: 1 2
Element filename Description Path and filename where query results are written. The default path is the current directory. Name of a program to receive the query results as input
program
Usage
You can use the OUTPUT statement to direct the results of a query to an operating-system file or to a program. You can also specify whether column headings should be omitted from the query output. C-style comments are not valid within the OUTPUT statement.
2-431
OUTPUT
Related Information
Related statements: SELECT and UNLOAD
2-432
PREPARE
Use the PREPARE statement to parse, validate, and generate an execution plan for one or more SQL statements at runtime. Use this statement with ESQL/C.
Syntax
PREPARE statement_id statement_id_var FROM statement_text statement_var
Description Identifier declared here for the prepared object Host variable storing statement_id Text of the SQL statement(s) to prepare Host variable storing the text of one or more SQL statements
Restrictions Must be unique in the database among names of cursors and prepared objects Must have been previously declared as a character data type
See Preparing Multiple SQL Statements on Quoted String page 2-439 and Statement Text on page on page 4-142. 2-434. Must be a character data type. Not valid if the SQL statement(s) contains the Collection-Derived-Table segment. Language specific
statement_var
Usage
The PREPARE statement enables your program to assemble the text of one or more SQL statements at runtime (creating a prepared object) and make it executable. This dynamic form of SQL is accomplished in three steps: 1. A PREPARE statement accepts statement text as input, either as a quoted string or stored within a character variable. Statement text can contain question-mark ( ? ) placeholders to represent values that are to be defined when the statement is executed. 2. An EXECUTE or OPEN statement can supply the required input values and execute the prepared statement once or many times. 3. Resources allocated to the prepared statement can be released later using the FREE statement. In Dynamic Server, the same collating order that is current when you create a prepared object is also used when that object is executed, even if the execution-time collation of the session (or of DB_LOCALE) is different.
Restrictions
The number of prepared objects in a single program is limited by available memory. These include statement identifiers declared in PREPARE statements (statement_id or statement_id_var) and declared cursors. To avoid exceeding the limit, use the FREE statement to release some statements or cursors. The maximum length of a PREPARE statement is 64 kilobytes. For restrictions on the statements in the character string, see Restricted Statements in Single-Statement Prepares on page 2-435 and Restricted Statements in Multistatement Prepared Objects on page 2-440.
Chapter 2. SQL Statements
2-433
PREPARE
Statement Text
The PREPARE statement can take statement text either as a quoted string or as text that is stored in a program variable. The following restrictions apply to the statement text: v The text can contain only SQL statements. It cannot contain statements or comments from the host programming language. v The text can contain comments preceded by a double hyphen (--), or that are enclosed in braces ( { } ) or in C-style slash and asterisk ( /* */ ) delimiters. These symbols introduce or enclose SQL comments. For more information on SQL comment symbols, see How to Enter SQL Comments on page 1-3. v The text can contain either a single SQL statement or a series of statements that are separated by semicolon ( ; ) symbols.
2-434
PREPARE
For a list of SQL statements that cannot be prepared, see Restricted Statements in Single-Statement Prepares on page 2-435. For more information on how to prepare multiple SQL statements, see Preparing Multiple SQL Statements on page 2-439. v The text cannot include an embedded SQL statement prefix or terminator, such as a dollar sign ( $ ) or the words EXEC SQL. v Host-language variables are not recognized as such in prepared text. Therefore, you cannot prepare a SELECT (or EXECUTE FUNCTION or EXECUTE PROCEDURE) statement that includes an INTO clause, because the INTO clause requires a host-language variable. v The only identifiers that you can use are names that are defined in the database, such as names of tables and columns. For more information on how to use identifiers in statement text, see Preparing Statements with SQL Identifiers on page 2-437. v Use a question mark ( ? ) as a placeholder to indicate where data is supplied when the statement executes, as in this ESQL/C example:
EXEC SQL prepare new_cust from insert into customer(fname,lname) values(?,?);
For more information on how to use question marks as placeholders, see Preparing Statements That Receive Parameters on page 2-437. In Dynamic Server, if the prepared statement contains the Collection-Derived-Table segment or an ESQL/C collection variable, some additional limitations exist on how you can assemble the text for the PREPARE statement. For information about dynamic SQL, see the IBM Informix ESQL/C Programmer's Manual.
2-435
PREPARE
ALLOCATE COLLECTION ALLOCATE DESCRIPTOR ALLOCATE ROW CLOSE CONNECT CREATE FUNCTION FROM CREATE PROCEDURE FROM CREATE ROUTINE FROM DEALLOCATE COLLECTION DEALLOCATE DESCRIPTOR DEALLOCATE ROW DECLARE DESCRIBE DISCONNECT EXECUTE EXECUTE IMMEDIATE FETCH FLUSH FREE GET DESCRIPTOR GET DIAGNOSTICS INFO LOAD OPEN OUTPUT PREPARE PUT SET AUTOFREE SET CONNECTION SET DEFERRED_PREPARE SET DESCRIPTOR UNLOAD WHENEVER
In Extended Parallel Server, you can prepare any single SQL statement except the following statements:
ALLOCATE DESCRIPTOR CLOSE CONNECT CREATE PROCEDURE FROM DEALLOCATE DESCRIPTOR DECLARE DESCRIBE DISCONNECT EXECUTE EXECUTE IMMEDIATE FETCH FLUSH FREE GET DESCRIPTOR GET DIAGNOSTICS INFO LOAD OPEN OUTPUT PREPARE PUT SET CONNECTION SET DEFERRED_PREPARE SET DESCRIPTOR UNLOAD WHENEVER
You can prepare a SELECT statement. If SELECT includes the INTO TEMP clause, you can execute the prepared statement with an EXECUTE statement. If it does not include the INTO TEMP clause, the statement returns rows of data. Use DECLARE, OPEN, and FETCH cursor statements to retrieve the rows. A prepared SELECT statement can include a FOR UPDATE clause. This clause is used with the DECLARE statement to create an update cursor. The next example shows a SELECT statement with a FOR UPDATE clause in ESQL/C:
sprintf(up_query, "%s %s %s", "select * from customer ", "where customer_num between ? and ? ", "for update"); EXEC SQL prepare up_sel from :up_query; EXEC SQL declare up_curs cursor for up_sel; EXEC SQL open up_curs using :low_cust,:high_cust;
2-436
PREPARE
sprintf(redo_st, "%s %s", "drop table workt1; ", "create table workt1 (wtk serial, wtv float)" ); EXEC SQL prepare redotab from :redo_st;
You can use a placeholder to defer evaluation of a value until runtime only for an expression, but not for an SQL identifier, except as noted in Preparing Statements with SQL Identifiers on page 2-437. The following example of an ESQL/C code fragment prepares a statement from a variable that is named demoquery. The text in the variable includes one question-mark ( ? ) placeholder. The prepared statement is associated with a cursor and, when the cursor is opened, the USING clause of the OPEN statement supplies a value for the placeholder:
EXEC SQL BEGIN DECLARE SECTION; char queryvalue [6]; char demoquery [80]; EXEC SQL END DECLARE SECTION; EXEC SQL connect to stores_demo; sprintf(demoquery, "%s %s", "select fname, lname from customer ", "where lname > ? "); EXEC SQL prepare quid from :demoquery; EXEC SQL declare democursor cursor for quid; stcopy("C", queryvalue); EXEC SQL open democursor using :queryvalue;
The USING clause is available in both OPEN statements that are associated with a cursor and EXECUTE statements (all other prepared statements). You can use a question-mark ( ? ) placeholder to represent the name of an ESQL/C or SPL collection variable.
2-437
PREPARE
v For the dbspace name in the IN dbspace clause of the CREATE DATABASE statement. v For the cursor name in statements that use cursor names.
EXEC SQL connect to stores_demo; printf( "This program does a select * on a table\n" ); printf( "Enter table name: " ); scanf( "%s", tablename ); sprintf(demoselect, "select * from %s", tablename ); EXEC SQL prepare iid from :demoselect; EXEC SQL describe iid into demodesc; /* Print what describe returns */ for ( i = 0; i < demodesc->sqld; i++ ) prsqlda (demodesc->sqlvar + i);
/* Assign the data pointers. */ for ( i = 0; i < demodesc->sqld; i++ ) { switch (demodesc->sqlvar[i].sqltype & SQLTYPE) { case SQLCHAR: demodesc->sqlvar[i].sqltype = CCHARTYPE; /* make room for null terminator */ demodesc->sqlvar[i].sqllen++; demodesc->sqlvar[i].sqldata = malloc( demodesc->sqlvar[i].sqllen );
2-438
PREPARE
break; case SQLSMINT: /* fall through */ case SQLINT: /* fall through */ case SQLSERIAL: demodesc->sqlvar[i].sqltype = CINTTYPE; demodesc->sqlvar[i].sqldata = malloc( sizeof( int ) ); break; /* And so on for each type. */ } } /* Declare and open cursor for select . */ EXEC SQL declare d_curs cursor for iid; EXEC SQL open d_curs; /* Fetch selected rows one at a time into demodesc. */ for( ; ; ) { printf( "\n" ); EXEC SQL fetch d_curs using descriptor demodesc; if ( sqlca.sqlcode != 0 ) break; for ( i = 0; i < demodesc->sqld; i++ ) { switch (demodesc->sqlvar[i].sqltype) { case CCHARTYPE: printf( "%s: \"%s\n", demodesc->sqlvar[i].sqlname, demodesc->sqlvar[i].sqldata ); break; case CINTTYPE: printf( "%s: %d\n", demodesc->sqlvar[i].sqlname, *((int *) demodesc->sqlvar[i].sqldata) ); break; /* And so forth for each type... */ } } } EXEC SQL close d_curs; EXEC SQL free d_curs; /* Free the data memory. */ for ( i = 0; i < demodesc->sqld; i++ ) free( demodesc->sqlvar[i].sqldata ); free( demodesc ); printf ("Program Over.\n"); } prsqlda(sp) struct sqlvar_struct *sp; { printf printf printf printf printf } ("type = %d\n", sp->sqltype); ("len = %d\n", sp->sqllen); ("data = %lx\n", sp->sqldata); ("ind = %lx\n", sp->sqlind); ("name = %s\n", sp->sqlname);
2-439
PREPARE
If a statement in a multistatement prepare returns an error, the whole prepared statement stops executing. The database server does not execute any remaining statements. In most situations, compiled products return error-status information on the error, but do not indicate which statement in the text causes an error. You can use the sqlca.sqlerrd[4] field in the sqlca to find the offset of the errors. In a multistatement prepare, if no rows are returned from a WHERE clause in the following statements, the database server returns SQLNOTFOUND (100): v UPDATE...WHERE... v SELECT INTO TEMP...WHERE... v INSERT INTO...WHERE... v DELETE FROM...WHERE... In the next example, four SQL statements are prepared into a single ESQL/C string called query. Individual statements are delimited with semicolons. A single PREPARE statement can prepare the four statements for execution, and a single EXECUTE statement can execute the statements that are associated with the qid statement identifier:
sprintf (query, "%s %s %s %s %s %s %s", "update account set balance = balance + ? ", "where acct_number = ?;", "update teller set balance = balance + ? ", "where teller_number = ?;", "update branch set balance = balance + ? ", "where branch_number = ?;", "insert into history values (?, ?);"; EXEC SQL prepare qid from :query; EXEC SQL begin work; EXEC SQL execute qid using :delta, :acct_number, :delta, :teller_number, :delta, :branch_number, :timestamp, :values; EXEC SQL commit work;
Here the semicolons ( ; ) are required as SQL statement-terminator symbols between each SQL statement in the text that query holds.
The following types of statements are also not valid in a multistatement prepare: v Statements that can cause the current database to close during the execution of the multistatement sequence v Statements that include references to TEXT or BYTE host variables In general, you cannot use the SELECT statement in a multistatement prepare. The only form of the SELECT statement allowed in a multistatement prepare is a SELECT statement with an INTO temporary table clause.
2-440
PREPARE
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Like the SQL statement cache, prepared statements can reduce how often the same query plan is reoptimized, thereby conserving resources in some contexts. The section Prepared Statements and the Statement Cache on page 2-599 discusses the use of prepared DML statements, cursors, and the SQL statement cache as combined or alternative techniques for improving query performance.
Related Statements
Related statements: CLOSE, DECLARE, DESCRIBE, EXECUTE, FREE, OPEN, SET AUTOFREE, and SET DEFERRED_PREPARE For information about basic concepts that relate to the PREPARE statement, see the IBM Informix Guide to SQL: Tutorial. For information about more advanced concepts that relate to the PREPARE statement, see the IBM Informix ESQL/C Programmer's Manual.
2-441
PUT
Use the PUT statement to store a row in an insert buffer for later insertion into the database. This statement is an extension to the ANSI/ISO standard for SQL. You can use this statement with ESQL/C.
Syntax
PUT cursor_id_var cursor_id
, FROM output_var INDICATOR indicator_var $indicator_var :indicator_var SQL DESCRIPTOR descriptor descriptor_var DESCRIPTOR sqlda_pointer
USING
Description Name of a cursor Host variable = cursor_id Name of a system-descriptor area Host-variable that contains descriptor
Restrictions Must be open Must be a character type; cursor must be open Must already be allocated Must already be allocated
Syntax Identifier on page 5-22 Language specific Quoted String on page 4-142 Quoted String on page 4-142 Language specific
Host variable to receive a return code if Cannot be a DATETIME or corresponding output_var receives a INTERVAL data type NULL value Host variable whose contents replace a question-mark ( ? ) placeholder in a prepared INSERT statement Pointer to an sqlda structure Must be a character data type First character cannot be the ( $ ) or ( : ) symbol
output_var
Language specific
sqlda_pointer
Usage
PUT stores a row in an insert buffer that is created when the cursor is opened. If the buffer has no room for the new row when the statement executes, the buffered rows are written to the database in a block, and the buffer is emptied. As a result, some PUT statement executions cause rows to be written to the database, and some do not. You can use the FLUSH statement to write buffered rows to the database without adding a new row. The CLOSE statement writes any remaining rows before it closes an insert cursor. If the current database uses explicit transactions, you must execute a PUT statement within a transaction.
2-442
PUT
The following example uses a PUT statement in ESQL/C:
EXEC SQL prepare ins_mcode from insert into manufact values(?,?); EXEC SQL declare mcode cursor for ins_mcode; EXEC SQL open mcode; EXEC SQL put mcode from :the_code, :the_name;
The PUT statement is not an X/Open SQL statement. Therefore, you get a warning message if you compile a PUT statement in X/Open mode.
2-443
PUT
The following ESQL/C example illustrates the use of an insert cursor. The code includes the following statements: v The DECLARE statement associates a cursor called ins_curs with an INSERT statement that inserts data into the customer table. The VALUES clause specifies a data structure that is called cust_rec; the ESQL/C preprocessor converts cust_rec to a list of values, one for each component of the structure. v The OPEN statement creates a buffer. v A user-defined function (not defined within this example) obtains customer information from user input and stores it in cust_rec. v The PUT statement composes a row from the current contents of the cust_rec structure and sends it to the row buffer. v The CLOSE statement inserts into the customer table any rows that remain in the row buffer and closes the insert cursor:
int keep_going = 1; EXEC SQL BEGIN DECLARE SECTION struct cust_row { /* fields of a row of customer table */ } cust_rec; EXEC SQL END DECLARE SECTION EXEC SQL declare ins_curs cursor for insert into customer values (:cust_row); EXEC SQL open ins_curs; while ( (sqlca.sqlcode == 0) && (keep_going) )
{ keep_going = get_user_input(cust_rec); /* ask user for new customer */ if (keep_going ) /* user did supply customer info */ { cust_rec.customer_num = 0; /* request new serial value */ EXEC SQL put ins_curs; } if (sqlca.sqlcode == 0) /* no error from PUT */ keep_going = (prompt_for_y_or_n("another new customer") ==Y) } EXEC SQL close ins_curs;
2-444
PUT
gets(u_company); EXEC SQL put ins_curs from :u_company; printf("Enter another customer (y/n) ? "); if (answer = getch() != y) break; } EXEC SQL close ins_curs; EXEC SQL disconnect all; }
Indicator variables are optional, but you should use an indicator variable if the possibility exists that output_var might contain a NULL value. If you specify the indicator variable without the INDICATOR keyword, you cannot put a blank space between output_var and indicator_var.
2-445
PUT
To put elements, one at a time, into the insert cursor, use the PUT statement and the FROM clause. The PUT statement identifies the collection cursor that is associated with the collection variable. The FROM clause identifies the element value to be inserted into the cursor. The data type of any host variable in the FROM clause must match the element type of the collection. Important: The collection variable stores the elements of the collection. However, it has no intrinsic connection with a database column. Once the collection variable contains the correct elements, you must then save the variable into the actual collection column with the INSERT or UPDATE statement. Suppose you have a table called children with the following schema:
CREATE TABLE children ( age SMALLINT, name VARCHAR(30), fav_colors SET(VARCHAR(20)), )
The following ESQL/C program fragment shows how to use an insert cursor to put elements into a collection variable called child_colors:
EXEC SQL BEGIN DECLARE SECTION; client collection child_colors; char *favorites[] ( "blue", "purple", "green", "white", "gold", 0 ); int a = 0; char child_name[21]; EXEC SQL END DECLARE SECTION; EXEC SQL allocate collection :child_colors; /* Get structure of fav_colors column for untyped * child_colors collection variable */ EXEC SQL select fav_colors into :child_colors from children where name = :child_name; /* Declare insert cursor for child_colors collection * variable and open this cursor */ EXEC SQL declare colors_curs cursor for insert into table(:child_colors) values (?); EXEC SQL open colors_curs; /* Use PUT to gather the favorite-color values * into a cursor */ while (fav_colors[a]) { EXEC SQL put colors_curs from :favorites[:a]; a++ ... } /* Flush cursor contents to collection variable */ EXEC SQL flush colors_curs; EXEC SQL update children set fav_colors = :child_colors; EXEC SQL close colors_curs; EXEC SQL deallocate collection :child_colors;
2-446
PUT
After the FLUSH statement executes, the collection variable, child_colors, contains the elements {blue", "purple", "green", "white", "gold}. The UPDATE statement at the end of this program fragment saves the new collection into the fav_colors column of the database. Without this UPDATE statement, the new collection would not be added to the collection column.
Error Checking
The sqlca structure contains information on the success of each PUT statement as well as information that lets you count the rows that were inserted. The result of each PUT statement is contained in the following fields of the sqlca: sqlca.sqlcode, SQLCODE, and sqlca.sqlerrd[2]. Data buffering with an insert cursor means that errors are not discovered until the buffer is flushed. For example, an input value that is incompatible with the data type of the column for which it is intended is discovered only when the buffer is flushed. When an error is discovered, buffered rows that were not inserted before the error are not inserted; they are lost from memory. The SQLCODE field is set to 0 if no error occurs; otherwise, it is set to an error code. The third element of the sqlerrd array is set to the number of rows that were successfully inserted into the database: v If any row is put into the insert buffer, but not written to the database, SQLCODE and sqlerrd are set to 0 (SQLCODE because no error occurred, and sqlerrd because no rows were inserted). v If a block of buffered rows is written to the database during the execution of a PUT statement, SQLCODE is set to 0 and sqlerrd is set to the number of rows that was successfully inserted into the database. v If an error occurs while the buffered rows are written to the database, SQLCODE indicates the error, and sqlerrd contains the number of successfully inserted rows. (The uninserted rows are discarded from the buffer.) Tip: When you encounter an SQLCODE error, a SQLSTATE error value also exists. See the GET DIAGNOSTICS statement for details of how to obtain the message text. To count the number of pending and inserted rows in the database:
Chapter 2. SQL Statements
2-447
PUT
1. 2. 3. 4. Prepare two integer variables (for example, total and pending). When the cursor is opened, set both variables to 0. Each time a PUT statement executes, increment both total and pending. Whenever a PUT or FLUSH statement executes or the cursor closes, subtract the third field of the SQLERRD array from pending.
At any time, (total - pending) represents the number of rows actually inserted. If no statements fail, pending contains zero after the cursor is closed. If an error occurs during a PUT, FLUSH, or CLOSE statement, the value that remains in pending is the number of uninserted (discarded) rows.
Related Statements
Related statements: ALLOCATE DESCRIPTOR, CLOSE, DEALLOCATE DESCRIPTOR, FLUSH, DECLARE, GET DESCRIPTOR, OPEN, PREPARE, and SET DESCRIPTOR For a task-oriented discussion of the PUT statement, see the IBM Informix Guide to SQL: Tutorial. For more information about error checking, the system-descriptor area, and the sqlda structure, see the IBM Informix ESQL/C Programmer's Manual.
2-448
RENAME COLUMN
Use the RENAME COLUMN statement to change the name of a column. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
RENAME COLUMN owner. table . old_column TO new_column
Element new_column
Description Name that you declare here to replace old_column Column to rename Owner of the table Table that contains old_column
Restrictions Must be unique among column names in table. See also How Triggers Are Affected. Must exist within table Must be the owner of table Must exist in the current database
Usage
You can rename a column of a table if any of the following conditions are true: v You own the table or have Alter privilege on the table. v You have the DBA privilege on the database. In Extended Parallel Server, you cannot rename the columns of a fragmented table if the table is fragmented by range. For more information, see RANGE Method Clause (XPS) on page 2-195.
2-449
RENAME COLUMN
Related Information
Related statements: ALTER TABLE, CREATE TABLE, CREATE TRIGGER, CREATE VIEW, and RENAME TABLE
2-450
RENAME DATABASE
Use the RENAME DATABASE statement to change the name of a database. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
RENAME DATABASE owner. old_database TO new_database
Element new_database
Description New name that you declare here for old_database Name that new_database replaces Owner of old_database
Restrictions Must be unique among database names of the current database server; must not be opened by any user when this statement is issued
old_database owner
Must exist on current database server, but it cannot Database Name, p. be the name of the current database 5-15 Must be the owner of the database Owner, p. 5-43
Usage
You can rename a database if either of the following is true: v You created the database. v You have the DBA privilege on the database. The RENAME DATABASE statement fails with error -9874, however, if the specified database contains a virtual table or a virtual index. You can only rename databases of the database server to which you are currently connected. You cannot rename a database from inside an SPL routine.
Related Information
Related statement: CREATE DATABASE For information on how to update the three-part names of JAR files after you rename the database, see the J/Foundation Developer's Guide.
2-451
RENAME INDEX
Use the RENAME INDEX statement to change the name of an existing index. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
RENAME INDEX owner. old_index TO new_index
Element new_index
Description
Restrictions
New name that you Name must be unique to the database (or to the declare here for the session, if old_index is on a temporary table) index Index name that new_index replaces Must exist, but it cannot be any of the following: -- An index on a system catalog table -- A system-generated constraint index -- A Virtual-Index Interface (VII) Must be the owner of old_index
old_index
Identifier, p. 5-22
owner
Owner of index
Usage
You can rename an index if you are the owner of the index or have the DBA privilege on the database. When you rename an index, the database server changes the index name in the sysindexes, sysconstraints, sysobjstate, and sysfragments system catalog tables. (But for an index on a temporary table, no system catalog tables are updated.) Indexes on system catalog tables cannot be renamed. If you want to change the name of a system-generated index that implements a constraint, use the ALTER TABLE ... DROP CONSTRAINT statement to drop the constraint, and then use the ALTER TABLE ... ADD CONSTRAINT statement to define a new constraint that has the same definition as the constraint that you dropped, but for which you declare the new name. SPL routines that use the renamed index are reoptimized on their next use after the index is renamed.
Related Information
Related statements: ALTER INDEX, CREATE INDEX, and DROP INDEX For a discussion of SPL-routine reoptimization, see your IBM Informix Performance Guide.
2-452
RENAME SEQUENCE
Use the RENAME SEQUENCE statement to change the name of a sequence. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
RENAME SEQUENCE owner. old_sequence TO new_sequence
Element new_sequence
Description New name that you declare here for an existing sequence Current name of a sequence Owner of the sequence
Restrictions Must be unique among the names of sequences, tables, views, and synonyms in the database Must exist in the current database Must be the owner of the sequence
old_sequence owner
Usage
To rename a sequence, you must be the owner of the sequence, have the ALTER privilege on the sequence, or have the DBA privilege on the database. You cannot use a synonym to specify the name of the sequence. In a database that is not ANSI compliant, the name of new_sequence (or in an ANSI-compliant database, the combination of owner.new_sequence) must be unique among sequences, tables, views, and synonyms in the database.
Related Information
Related statements: ALTER SEQUENCE, CREATE SEQUENCE, DROP SEQUENCE, CREATE SYNONYM, DROP SYNONYM, GRANT, REVOKE, INSERT, UPDATE, and SELECT For information about generating values from a sequence, see NEXTVAL and CURRVAL Operators (IDS) on page 4-61.
2-453
RENAME TABLE
Use the RENAME TABLE statement to change the name of a table. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
RENAME TABLE owner. old_table TO (1) new_owner. new_table
Notes: 1
Element new_table Description New name for old_table
Name that new_table replaces Current owner of the table New owner of the table (XPS only)
Usage
To rename a table, you must be the owner of the table, or have the ALTER privilege on the table, or have the DBA privilege on the database. An error occurs if old_table is a synonym, rather than the name of a table. The renamed table remains in the current database. You cannot use the RENAME TABLE statement to move a table from the current database to another database, nor to rename a table that resides in another database. In Dynamic Server, you cannot change the table owner by renaming the table. An error occurs if you try to specify an owner. qualifier for the new name of the table. In Extended Parallel Server, a user with DBA privilege on the database can change the owner of a table, if the table is local. Both the table name and owner can be changed by a single statement. The following example uses the RENAME TABLE statement to change the owner of a table:
RENAME TABLE tro.customer TO mike.customer
When the table owner is changed, you must specify both the old owner and new owner. Important: When the owner of a table is changed, the existing privileges granted by the original owner are retained.
2-454
RENAME TABLE
In Extended Parallel Server, you cannot rename a table that contains a dependent GK index. In an ANSI-compliant database, if you are not the owner of old_table, you must specify owner.old_table as the old name of the table. If old_table is referenced by a view in the current database, the view definition is updated in the sysviews system catalog table to reflect the new table name. For further information on the sysviews system catalog table, see the IBM Informix Guide to SQL: Reference. If old_table is a triggering table, the database server takes these actions: v Replaces the name of the table in the trigger definition but does not replace the table name where it appears inside any triggered actions v Returns an error if the new table name is the same as a correlation name in the REFERENCING clause of the trigger definition When the trigger executes, the database server returns an error if it encounters a table name for which no table exists. You can reorganize the items table to move the quantity column from the fifth position to the third position by following these steps: 1. Create a new table, new_table, that contains the column quantity in the third position. 2. Fill the table with data from the current items table. 3. Drop the old items table. 4. Rename new_table with the name items. The following example uses the RENAME TABLE statement as the last step:
CREATE TABLE new_table ( item_num SMALLINT, order_num INTEGER, quantity SMALLINT, stock_num SMALLINT, manu_code CHAR(3), total_price MONEY(8) ); INSERT INTO new_table SELECT item_num, order_num, quantity, stock_num, manu_code, total_price FROM items; DROP TABLE items; RENAME TABLE new_table TO items;
Related Information
Related statements: ALTER TABLE, CREATE TABLE, DROP TABLE, and RENAME COLUMN
2-455
REVOKE
Use the REVOKE statement to cancel access privileges for PUBLIC, or for a list of users or roles, or to cancel a role for PUBLIC or for a list of users or roles.
Syntax
REVOKE
(1) Database-Level Privileges DEFAULT ROLE FROM PUBLIC , (4) Role Name FROM
(3)
(6) Table-Level Privileges (1) (7) Routine-Level Privileges (5) (8) Language-Level Privileges (9) Type-Level Privileges (10) Sequence-Level Privileges
FROM Options:
(3) FROM User List RESTRICT , (1) Role Name user (4) AS revoker CASCADE (5)
Notes: 1 2 3 4 5 6 7 8 Informix extension See page 2-457 See page 2-466 See page 2-466 Dynamic Server only See page 2-459 See page 2-463 See page 2-464
IBM Informix Guide to SQL: Syntax
2-456
REVOKE
9 10 See page 2-462 See page 2-465
Description Authorization identifier of the grantor of the privileges to be revoked Role from which you revoke another role User whose role (or default role) you cancel Restrictions Must be grantor of the specified privileges Must exist Must exist Syntax Owner Name, p. 5-43 Owner Name, p. 5-43 Owner Name, p. 5-43
Usage
To cancel privileges on one or more fragments of a table that has been fragmented by expression, see REVOKE FRAGMENT on page 2-471. You can revoke privileges if any of the following conditions is true for the privileges that you are attempting to revoke on some database object: v You granted them and did not designate another user as grantor. v The GRANT statement specified you as grantor. v You are revoking privileges from PUBLIC on an object that you own, and those privileges were granted by default when you created the object. v You have database-level DBA privileges and you specify in the AS clause the name of a user who was grantor of the privilege. The REVOKE statement can cancel any of the following access privileges or roles that a user, or PUBLIC, or a role currently holds: v Privileges on the database (but a role cannot hold database-level privileges) v Privileges on a table, synonym, view, or sequence object v Privileges on a user-defined data type (UDT), a user-defined routine (UDR), or on the SPL language v A non-default role, or the default role of PUBLIC or of a user. You cannot revoke privileges from yourself. You cannot revoke grantor status from another user. To revoke a privilege that was granted to another user by the AS grantor clause of the GRANT statement, you must have the DBA privilege, and you must use the AS clause to specify that user as revoker. If you enclose revoker, role, or user in quotation marks, the name is case sensitive and is stored exactly as you typed it. In an ANSI-compliant database, if you do not use quotation marks as delimiters, the name is stored in uppercase letters.
Database-Level Privileges
Database-Level Privileges:
DBA RESOURCE CONNECT
Three concentric layers of database-level privileges, Connect, Resource, and DBA, authorize increasing power over database access and control. Only a user with the DBA privilege can grant or revoke database-level privileges.
Chapter 2. SQL Statements
2-457
REVOKE
Because of the hierarchical organization of the privileges (as outlined in the privilege definitions that are described later in this section), if you revoke either the Resource or the Connect privilege from a user with the DBA privilege, the statement has no effect. If you revoke the DBA privilege from a user who has the DBA privilege, the user retains the Connect privilege on the database. To deny database access to a user with the DBA or Resource privilege, you must first revoke the DBA or the Resource privilege and then revoke the Connect privilege in a separate REVOKE statement. Similarly, if you revoke the Connect privilege from a user who has the Resource privilege, the statement has no effect. If you revoke the Resource privilege from a user, the user retains the Connect privilege on the database. Recommendation: Only user informix can modify system catalog tables directly. Except as noted specifically in your database server documentation, however, do not use DML statements to insert, delete, or update rows of system catalog tables directly, because modifying data in these tables can destroy the integrity of the database. Only users or PUBLIC can hold database-level privileges. You cannot revoke these privileges from a role, because a role cannot hold database level privileges.
2-458
REVOKE
The following table lists the keyword for each database-level privilege.
Privilege DBA Effect Has all the capabilities of the Resource privilege and can perform the following additional operations: v Grant any database-level privilege, including the DBA privilege, to another user. v Grant any table-level privilege to another user or to a role. v Grant a role to a user or to another role. v Revoke a privilege whose grantor you specify as the revoker in the AS clause of the REVOKE statement. v Restrict the Execute privilege to DBAs when registering a UDR. v Execute the SET SESSION AUTHORIZATION statement. v Create any database object. v Create tables, views, and indexes, designating another user as owner of these objects. v Alter, drop, or rename database objects, regardless of who owns it. v Execute the DROP DISTRIBUTIONS option of the UPDATE STATISTICS statement.
3
RESOURCE
v Execute DROP DATABASE and RENAME DATABASE statements. Lets you extend the structure of the database. In addition to the capabilities of the Connect privilege, the holder of the Resource privilege can perform the following operations: v Create new tables. v Create new indexes. v Create new user-defined routines. v Create new data types. CONNECT If you have this privilege, you can query and modify data, and modify the database schema if you own the database object that you want to modify. A user holding the Connect privilege can perform the following operations: v Connect to the database with the CONNECT statement or another connection statement. v Execute SELECT, INSERT, UPDATE, and DELETE statements, provided that the user has the necessary table-level privileges. v Create views, provided that the user has the Select privilege on the underlying tables. v Create synonyms. v Create temporary tables and create indexes on temporary tables. v Alter or drop a table or an index, if the user owns the table or index (or has the Alter, Index, or References privilege on the table). v Grant privileges on a table, if the user owns the table (or was given privileges on the table with the WITH GRANT OPTION keyword).
Table-Level Privileges
Table-level privileges, also called table privileges, specify which operations a user or role can perform on a table or view in the database. You can use a synonym to specify the table or view on which you grant or revoke table privileges. Select, Update, and References privileges can be granted on a subset of the columns of a table or view, but can be revoked only for all columns. If Select privileges are revoked from a user for a table that is referenced in the SELECT
Chapter 2. SQL Statements
2-459
REVOKE
statement defining a view that the same user owns, then that view is dropped, unless it also includes columns from tables in another database. Use the following syntax to specifying which table-level privileges to revoke from one or more users or roles: Table-Level Privileges:
PRIVILEGES ALL , INSERT DELETE UPDATE (1) SELECT ALTER INDEX REFERENCES (2) UNDER ON owner . table view synonym
Notes: 1 2
Element owner synonym, table, view Description Name of the user who owns the table, view, or synonym Synonym, table, or view on which privileges are granted
Must be a valid Owner Name, authorization identifier p. 5-43 Must exist in the current database Identifier, p. 5-22
In one REVOKE statement, you can list one or more of the following keywords to specify the privileges on the specified table to be revoked from the users or roles.
Privilege INSERT DELETE SELECT UPDATE INDEX Effect after REVOKE User cannot insert rows. User cannot delete rows. User cannot display data retrieved by a SELECT statement. User cannot change column values. User cannot create permanent indexes. You must have the Resource privilege to take advantage of the Index privilege. (But any user who has the Connect privilege can create indexes on temporary tables.) The holder cannot add or delete columns, modify column data types, add or delete constraints, change the locking mode of a table from PAGE to ROW, nor add or drop a corresponding named-ROW-type table. The user also cannot enable or disable indexes, constraints, nor triggers, as described in SET Database Object Mode on page 2-539.
ALTER
2-460
REVOKE
Privilege REFERENCES Effect after REVOKE User cannot reference columns in referential constraints. You must also have the Resource privilege on the database to take advantage of the References privilege on tables. (You can add, however, a referential constraint during an ALTER TABLE statement. without holding the Resource privilege on the database.) Revoking the References privilege disallows cascading DELETE operations. User cannot create subtables under a typed table. This removes all of the table privileges that are listed above. (Here the PRIVILEGES keyword is optional. )
See also Table-Level Privileges on page 2-374. If a user receives the same privilege from two different grantors and one grantor revokes the privilege, the grantee still has the privilege until the second grantor also revokes the privilege. For example, if both you and a DBA grant the Update privilege on your table to ted, both you and the DBA must revoke the Update privilege to prevent ted from updating your table. If user ted holds the same privileges through a role or as PUBLIC, however, this REVOKE operation does not prevent ted from exercising the Update privilege.
This statement results in ISAM error message 111, No record found, because the system catalog tables (syscolauth or systabauth) contain no table-level privilege entry for a user named ted. This REVOKE operation does not prevent ted from keeping all the table-level privileges given to PUBLIC on the customer table. To restrict table-level privileges, first revoke the privileges with the PUBLIC keyword, then re-grant them to some appropriate list of users and roles. The following statements revoke the Index and Alter privileges from all users for the customer table, and then grant these privileges specifically to user mary:
REVOKE INDEX, ALTER ON customer FROM PUBLIC GRANT INDEX, ALTER ON customer TO mary
Restricting Access to Specific Columns: Unlike GRANT, the REVOKE statement has no syntax to specify privileges on a subset of columns in a table. To revoke the Select, Update, or References privilege on a column from a user, you must revoke the privilege for all the columns of the table. To provide access to some of the columns on which you previously had granted privileges, issue a new GRANT statement to restore the appropriate privilege on specific columns. The next example cancels Select privileges for PUBLIC on certain columns:
Chapter 2. SQL Statements
2-461
REVOKE
REVOKE SELECT ON customer FROM PUBLIC GRANT SELECT (fname, lname, company, city) ON customer TO PUBLIC
In the next example, mary first receives the ability to reference four columns in customer, then the table owner restricts references to two columns:
GRANT REFERENCES (fname, lname, company, city) ON customer TO mary REVOKE REFERENCES ON customer FROM mary GRANT REFERENCES (company, city) ON customer TO mary
For example, assume that user hal has the Select and Insert privileges on the customer table. User jocelyn wants to revoke all seven table-level privileges from user hal. So user jocelyn issues the following REVOKE statement:
REVOKE ALL ON customer FROM hal
This statement executes successfully but returns SQLSTATE code 01006. The SQLSTATE warning is returned with a successful statement, as follows: v The statement succeeds in revoking the Select and Insert privileges from user hal because user hal had those privileges. v SQLSTATE code 01006 is returned because user hal lacked other privileges implied by the ALL keyword, so these privileges were not revoked. The ALL keyword instructs the database server to revoke everything possible, including nothing. If the user from whom privileges are revoked has no privileges on the table, the REVOKE ALL statement still succeeds, because it revokes everything possible from the user (in this case, no privileges at all). Effect of the ALL Keyword on UNDER Privilege (IDS): If you revoke ALL privileges on a typed table, the Under privilege is included in the privileges that are revoked. If you revoke ALL privileges on a table that is not based on a ROW type, the Under privilege is not included among the privileges that are revoked. (The Under privilege can be granted only on a typed table.)
Description Named ROW type for which to revoke Under privilege User-defined type for which to revoke Usage privilege
2-462
REVOKE
Usage Privilege
Any user can reference a built-in data type in an SQL statement, but not a DISTINCT data type that is based on a built-in data type. The creator of a user-defined data type or a DBA must explicitly grant the Usage privilege on the UDT, including a DISTINCT data type based on a built-in data type. REVOKE with the USAGE ON TYPE keywords removes the Usage privilege that you granted earlier to another user, to PUBLIC, or to a role.
Under Privilege
You own any named ROW data type that you create. If you want other users to be able to create subtypes under this named ROW type, you must grant these users the Under privilege on your named ROW type. If you later want to remove the ability of these users to create subtypes under the named ROW type, you must revoke the Under privilege from these users. A REVOKE statement with the UNDER ON TYPE keywords removes the Under privilege that you granted earlier to these users. For example, suppose that you created a ROW type named rtype1:
CREATE ROW TYPE rtype1 (cola INT, colb INT)
If you want another user named kathy to be able to create a subtype under this named ROW type, you must grant the Under privilege on this named ROW type to user kathy:
GRANT UNDER ON TYPE rtype1 TO kathy
Now user kathy can create another ROW type under the rtype1 ROW type even though kathy is not the owner of the rtype1 ROW type:
CREATE ROW TYPE rtype2 (colc INT, cold INT) UNDER rtype1
If you later want to remove the ability of user kathy to create subtypes under the rtype1 ROW type, enter the following statement:
REVOKE UNDER ON TYPE rtype1 FROM kathy
Routine-Level Privileges
If you revoke the Execute privilege on a UDR from a user, that user can no longer execute that UDR in any way. For details of how a user can execute a UDR, see Routine-Level Privileges on page 2-380. Routine-Level Privileges:
EXECUTE ON SPL_routine (1) PROCEDURE FUNCTION ROUTINE SPECIFIC ROUTINE FUNCTION PROCEDURE
2-463
REVOKE
3
Element routine SPL_routine Description A user-defined routine An SPL routine
In an ANSI-compliant database, the owner name must qualify the routine name, unless the user who issues the REVOKE statement is the owner of the routine. In Dynamic Server, any negator function for which you grant the Execute privilege requires a separate, explicit, REVOKE statement. When you create a UDR under any of the following circumstances, PUBLIC will not be granted Execute privilege by default. Therefore you must explicitly grant the Execute privilege before you can revoke it: v You create the UDR in an ANSI-compliant database. v You have DBA privilege and specify DBA after the CREATE keyword to restrict the Execute privilege to users with the DBA database-level privilege. v The NODEFDAC environment variable is set to yes to prevent PUBLIC from receiving any privileges that are not explicitly granted. But if you create a UDR with none of those conditions in effect, PUBLIC can execute your UDR without the GRANT EXECUTE statement. To limit who can executes your UDR, revoke Execute privilege FROM PUBLIC, and grant it to users (see User List on page 2-466) or roles (see Role Name on page 2-466). In Dynamic Server, if two or more UDRs have the same name, use a keyword from this list to specify which of those UDRs a user list can no longer execute. Keyword SPECIFIC FUNCTION UDR for Which Execution by the User is Prevented The UDR identified by specific name Any function with the specified routine name (and parameter types that match routine parameter list, if specified)
PROCEDURE Any procedure with the specified routine name (and parameter types that match routine parameter list, if specified) ROUTINE Functions or procedures with the specified routine name (and parameter types that match routine parameter list, if specified)
When a user registers a UDR that is written in SPL, Dynamic Server verifies that the user has the Usage privilege on the SPL language. If the user does not, the CREATE FUNCTION or CREATE PROCEDURE statement fails with an error. (The C language and the Java language do not require the Usage privilege.)
2-464
REVOKE
To revoke the Usage privilege on the SPL language from a user or role, issue a REVOKE statement that includes the USAGE ON LANGUAGE SPL keywords. If this statement succeeds, any user or role that you specify in the FROM clause can no longer register UDRs that are written in the specified language. For example, if you revoke the default Usage privilege on SPL from PUBLIC, the ability to create SPL routines is taken away from all users:
REVOKE USAGE ON LANGUAGE SPL FROM PUBLIC
You can issue a GRANT USAGE ON LANGUAGE statement to restore Usage privilege on SPL to a restricted group, such as to the role named developers:
GRANT USAGE ON LANGUAGE SPL TO developers
Description Owner of the sequence or of its synonym Sequence on which to revoke privileges Synonym for a sequence object
The sequence must reside in the current database. (You can qualify the sequence or synonym identifier with a valid owner name, but the name of a remote database (or database@server) is not valid as a qualifier.) Syntax to revoke sequence-level privileges is an extension to the ANSI/ISO standard for SQL.
Alter Privilege
You can revoke the Alter privilege on a sequence from another user or from a role. The Alter privilege enables a specified user or role to modify the definition of a sequence with the ALTER SEQUENCE statement or to rename the sequence with the RENAME SEQUENCE statement.
Select Privilege
You can revoke the Select privilege on a sequence from another user or from a role. Select privilege enables a user or role to use the sequence.CURRVAL and sequence.NEXTVAL in SQL statements to access and to increment the value of a sequence.
2-465
REVOKE
ALL Keyword
You can use the ALL keyword to revoke both Alter and Select privileges from another user or from a role.
User List
The authorization identifiers (or the PUBLIC keyword) that follow the FROM keyword of REVOKE specify who loses the revoked privileges or revoked roles. If you use the PUBLIC keyword as the user list, the REVOKE statement revokes the specified privileges or roles from PUBLIC, thereby revoking them from all users to whom the privileges or roles have not been explicitly granted, or who do not hold some other role through which they have received the role or privilege. The user list can consist of the authorization identifier of a single user or of multiple users, separated by commas. If you use the PUBLIC keyword as the user list, the REVOKE statement revokes the specified privileges from all users. User List:
PUBLIC , user user
Element user
Description Login name of a user whose privilege or role you are revoking
Spell the user names in the list exactly as they were spelled in the GRANT statement. You can optionally use quotes around each user name in the list to preserve the lettercase. In an ANSI-compliant database, if you do not use quotes to delimit user, the name of the user is stored in uppercase letters unless the ANSIOWNER environment variable was set to 1 before the database server was initialized. When you specify login names, you can use the REVOKE statement and the GRANT statement to secure various types of database objects selectively. For examples, see When to Use REVOKE Before GRANT on page 2-461.
Role Name
Only the DBA or a user who was granted a role WITH GRANT OPTION can revoke a role or its privileges. Users cannot revoke roles from themselves. Role Name:
role role
Element role
Description A role with one of these attributes: v Loses an existing privilege or role v Is lost by a user or by another role
Restrictions Must exist. If enclosed between quotation marks, role is case sensitive.
2-466
REVOKE
Immediately after the REVOKE keyword, the name of a role specifies a role to be revoked from the user list. After the FROM keyword, however, the name of a role specifies a role from which access privilege (or another role) is to be revoked. The same FROM clause can include both user and role names if no other REVOKE options conflict with the user or role specifications. Syntax to revoke privileges on a role or from a role are extensions to the ANSI/ISO standard for SQL. When you include a role after the FROM keyword of the REVOKE statement, the specified privilege (or another role) is revoked from that role, but users who have that role retain any privileges or roles that were granted to them individually. If you enclose role between quotation marks, the name is case sensitive and is stored exactly as you typed it. In an ANSI-compliant database, if you do not use quotation marks as delimiters, the role is stored in uppercase letters. When you revoke a role that was granted to a user with the WITH GRANT OPTION keywords, you revoke both the role and the option to grant it. The following examples show the effects of REVOKE role: v Remove users or remove another role from inclusion in the specified role:
REVOKE accounting FROM mary REVOKE payroll FROM accounting
When you revoke table-level privileges from a role, you cannot include the RESTRICT or CASCADE keywords.
This statement removes from user mary whatever privileges the default accounting role had conferred. If mary issues the SET ROLE DEFAULT statement, it has no effect until she is granted some new default role. After you execute REVOKE DEFAULT ROLE specifying one or more users or PUBLIC, any privileges that those users held only through the default role are cancelled. (But this statement does not revoke any privileges that were granted to a user individually, or privileges that were granted to a user through another role, or privileges that PUBLIC holds.) After REVOKE DEFAULT ROLE successfully cancels the default role of user, the default role of user becomes NULL, and the default role information is removed from the system catalog. (In this context, NULL and NONE are synonyms.)
2-467
REVOKE
No warning is issued if REVOKE DEFAULT ROLE specifies a user who has not been granted a default role. No options besides the user-list are valid after the FROM keyword in the REVOKE DEFAULT ROLE statement.
This statement prevents user max from creating or dropping external UDRs, even if max is the owner of a UDR that he subsequently attempts to drop. In databases for which this security feature is not needed, the DBSA can disable this restriction on who can create or drop external UDRs by setting the IFX_EXTEND_ROLE configuration parameter to OFF in the ONCONFIG file. When IFX_EXTEND_ROLE is set to OFF, users with at least the Resource privilege on the database can create or drop external UDRs. See Database-Level Privileges on page 2-457 for information about the Resource privilege.
User mary then uses her new privilege to grant users cathy and paul access to the items table:
GRANT SELECT, UPDATE ON items TO cathy GRANT SELECT ON items TO paul
Later you revoke privileges on the items table from user mary:
REVOKE SELECT, UPDATE ON items FROM mary
This single statement effectively revokes all privileges on the items table from users mary, cathy, and paul.
2-468
REVOKE
The CASCADE keyword has the same effect as this default condition.
The AS Clause
Without the AS clause, the user who executes the REVOKE statement must be the grantor of the privilege that is being revoked. The DBA or the owner of the object can use the AS clause to specify another user (who must be the grantor of the privilege) as the revoker of the privileges. The AS clause provides the only mechanism by which privileges can be revoked on a database object whose owner is an authorization identifier, such as informix, that is not also a valid user account known to the operating system. In Extended Parallel Server, the AS revoker clause is not valid in a REVOKE statement that cancels a role.
2-469
REVOKE
In contrast, if user ted does not grant the Select privilege to tania or to any other user, the same REVOKE statement succeeds. Even if ted does grant the Select privilege to another user, either of the following statements succeeds:
REVOKE SELECT ON customer FROM ted CASCADE REVOKE SELECT ON customer FROM ted
Related Information
Related Statements: GRANT, GRANT FRAGMENT, and REVOKE FRAGMENT For information about roles, see the following statements: CREATE ROLE, DROP ROLE, and SET ROLE. For a discussion of privileges, see the IBM Informix Database Design and Implementation Guide. For a discussion of how to embed GRANT and REVOKE statements in programs, see the IBM Informix Guide to SQL: Tutorial.
2-470
REVOKE FRAGMENT
Use the REVOKE FRAGMENT statement to revoke from one or more users or roles the Insert, Update, or Delete fragment-level privileges that were granted on individual fragments of a table that has been fragmented by expression. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
(1) REVOKE FRAGMENT Fragment-Level Privileges FROM , ( fragment ) PUBLIC , user user role role ON table
AS
revoker revoker
Notes: 1
Element fragment revoker role table user Description Name of partition or dbspace that stores one fragment. Default is all fragments of table. User (who is not executing this statement) who was grantor of privileges to be revoked Role from whom privileges are to be revoked Fragmented table whose fragment-level privileges are to be revoked User from whom privileges are to be revoked
Usage
The REVOKE FRAGMENT statement is a special case of the REVOKE statement for assigning privileges on table fragments. Use the REVOKE FRAGMENT statement to revoke the Insert, Update, or Delete privilege on one or more table fragments from one or more users or roles. The DBA can use this statement to revoke privileges on a fragment whose owner is another user. The REVOKE FRAGMENT statement is valid only for tables that are fragmented by an expression-based distribution scheme. For an explanation of this fragmentation strategy, see Expression Distribution Scheme on page 2-18.
Specifying Fragments
If you specify no fragment, the privileges are revoked for all fragments of table. You can specify one fragment or a comma-separated list of fragments enclosed between parentheses that immediately follow the ON table specification.
Chapter 2. SQL Statements
2-471
REVOKE FRAGMENT
Each fragment must be referenced by the name of the partition where it is stored. If you did not declare an explicit identifier when you created the partition, its name defaults to the name of the dbspace that contains the partition. After a dbspace is renamed successfully by the onspaces utility, only the new name is valid. Dynamic Server automatically updates existing fragmentation strategies in the system catalog to substitute the new dbspace name, but you must specify the new name in REVOKE FRAGMENT statement to reference a fragment whose default name is the name of a renamed dbspace.
Fragment-Level Privileges
The keyword or keywords that follow the FRAGMENT keyword specify fragment-level privileges, which are a logical subset of table-level privileges: Fragment-Level Privileges:
ALL , INSERT DELETE UPDATE
You can revoke fragment-level privileges individually or in combination. The following keywords specify the fragment-level privileges that you can revoke. Keyword INSERT DELETE UPDATE ALL Effect Prevents the user from inserting rows in the fragment Prevents the user from deleting rows in the fragment Prevents the user from updating rows in the fragment Cancels Insert, Delete, and Update privileges on a fragment
If you specify the ALL keyword in a REVOKE FRAGMENT statement, the specified users and roles lose all fragment-level privileges that they currently possess on the specified fragments. For example, assume that a user currently has the Update privilege on one fragment of a table. If you use the ALL keyword to revoke all current privileges on this fragment from this user, the user loses the Update privilege that he or she had on this fragment.
2-472
REVOKE FRAGMENT
For the distinction between fragment-level and table-level privileges, see the sections Definition of Fragment-Level Authorization and Effect of Fragment-Level Authorization in Statement Validation on page 2-389.
The AS Clause
Without the AS clause, the user who executes the REVOKE statement must be a grantor of the privilege that is being revoked. The DBA or the owner of the fragment can use the AS clause to specify another user (who must be the grantor of the privilege) as the revoker of privileges on a fragment. The AS clause provides the only mechanism by which privileges can be revoked on a fragment whose owner is an authorization identifier that is not a valid user account known to the operating system.
The following statement revokes the Update and Insert privileges on the fragment of the customer table in part1 from user susan:
REVOKE FRAGMENT UPDATE, INSERT ON customer (part1) FROM susan
The following statement revokes all privileges currently granted to user harry on the fragment of the customer table in part1:
REVOKE FRAGMENT ALL ON customer (part1) FROM harry
Related Statements
Related statements: GRANT FRAGMENT and REVOKE For a discussion of fragment-level and table-level privileges, see the section Fragment-Level Privileges on page 2-388. See also the IBM Informix Database Design and Implementation Guide.
2-473
ROLLBACK WORK
Use the ROLLBACK WORK statement to cancel a transaction deliberately and undo any changes that occurred since the beginning of the transaction. The ROLLBACK WORK statement restores the database to its state that existed before the transaction began.
Syntax
WORK ROLLBACK
Usage
The ROLLBACK WORK statement is valid only in databases that support transaction logging. In a database that is not ANSI compliant, you start a transaction with the BEGIN WORK statement. You can end a transaction with the COMMIT WORK statement or cancel the transaction with a ROLLBACK WORK statement. The ROLLBACK WORK statement restores the database to the state that existed before the transaction began. Use ROLLBACK WORK only at the end of a multistatement operation. The ROLLBACK WORK statement releases all row and table locks that the cancelled transaction holds. If you issue a ROLLBACK WORK statement when no transaction is pending, an error occurs. In an ANSI-compliant database, transactions are implicit. You do not need to mark the beginning of a transaction with a BEGIN WORK statement. You only need to mark the end of each transaction with a COMMIT WORK statement or cancel the transaction with a ROLLBACK WORK statement. If you issue a ROLLBACK WORK statement when no transaction is pending, the statement is accepted but has no effect. In ESQL/C, the ROLLBACK WORK statement closes all open cursors except those that are declared as hold cursors (by including the WITH HOLD keywords). Hold cursors remain open after a transaction is committed or rolled back. If you use the ROLLBACK WORK statement within an SPL routine that a WHENEVER statement calls, specify WHENEVER SQLERROR CONTINUE and WHENEVER SQLWARNING CONTINUE before the ROLLBACK WORK statement. This step prevents the program from looping if the ROLLBACK WORK statement encounters an error or a warning.
WORK Keyword
The WORK keyword is optional in a ROLLBACK WORK statement. The following two statements are equivalent:
ROLLBACK; ROLLBACK WORK;
2-474
ROLLBACK WORK
Related Statements
Related statements: BEGIN WORK and COMMIT WORK For a discussion of transactions and ROLLBACK WORK, see the IBM Informix Guide to SQL: Tutorial.
2-475
Syntax
, SAVE EXTERNAL DIRECTIVES directive ACTIVE INACTIVE TEST ONLY FOR query
Description Optimizer directive valid for query Text of a valid SELECT statement
Usage
SAVE EXTERNAL DIRECTIVES associates one or more optimizer directives with a query, and stores a record of this association in the sysdirectives system catalog table, for subsequent use with queries that match the specified query string. This statement establishes an association between the list of optimizer directives and the text of a query, but it does not execute the specified query. Only the DBA or user informix can execute SAVE EXTERNAL DIRECTIVES. Optimizer directives that it stores in the database are called external directives.
2-476
This example associates AVOID_INDEX and FULL directives with the specified query. The inline INDEX directive is ignored by the query optimizer when the external directives are applied to a query that matches the SELECT statement.
2-477
Related Statements
For information about optimizer directives and their syntax, see the segment Optimizer Directives in Optimizer Directives on page 5-34. For information about the sysdirectives table and the IFX_EXTDIRECTIVES environment variable, see the IBM Informix Guide to SQL: Reference.
2-478
SELECT
3 3 3 3 3 3 3 3 Use the SELECT statement to retrieve values from a database or from an SPL or ESQL/C collection variable. A SELECT operation is called a query. Rows or values that satisfy the specified search criteria of the query are called qualifying rows or values. What the query retrieves to its calling context, after applying any additional logical conditions, is called the result set of the query. This result set can be empty. You need the CONNECT privilege to execute a query, as well as the SELECT privilege on the tables from which the query retrieves rows.
Syntax
SELECT
Select Options UNION ALL SELECT Select Options ORDER BY Clause (1)
(2) (3)
Select Options:
(6) Projection Clause (2) (4) Optimizer Directives (10) FROM Clause (7) (8) INTO Clause (9) WHERE Clause (11) (5)
(13)
Notes: 1 2 3 4 5 6 See ORDER BY Clause on page 2-515 Informix extension See INTO Table Clauses on page 2-520 Dynamic Server only See Optimizer Directives on page 5-34 See Projection Clause on page 2-481
Chapter 2. SQL Statements
2-479
SELECT
7 8 9 10 11 12 13 ESQL/C only Stored Procedure Language only See INTO Clause on page 2-489 See FROM Clause on page 2-492 See WHERE Clause on page 2-507 See GROUP BY Clause on page 2-513 See HAVING Clause on page 2-514
Description Name of a column that can be updated after a FETCH Restrictions Must be in a FROM clause table, but does not need to be in the select list of the Projection clause Syntax Identifier on page 5-22
Element column
Usage
The SELECT statement can return data from tables in the current database, or in another database of the current database server, or in a database of another database server. Only the SELECT keyword, the Projection clause, and the FROM clause are required specifications. The SELECT statement can include various basic clauses, which are identified in the following list.
2-480
SELECT
Clause Optimizer Directives on page 5-34 Projection Clause INTO Clause on page 2-489 FROM Clause on page 2-492 Using the ON Clause on page 2-504 WHERE Clause on page 2-507 GROUP BY Clause on page 2-513 HAVING Clause on page 2-514 ORDER BY Clause on page 2-515 FOR UPDATE Clause on page 2-518 Using the FOR READ ONLY Clause in Read-Only Mode on page 2-520 INTO TEMP Clause on page 2-522 INTO EXTERNAL Clause (XPS) on page 2-523 INTO SCRATCH Clause (XPS) on page 2-524 UNION Operator on page 2-524 UNION Operator on page 2-524 Page Effect Specifies how the query should be implemented Specifies a list of items to be read from the database Specifies variables to receive the result set Specifies the data sources of Projection clause items Specifies join conditions as pre-join filters Sets conditions on qualifying rows and post-join filters Combines groups of rows into summary results Sets conditions on the summary results Sorts qualifying rows according to column values Enables updating of the result set after a FETCH Disables updating of the result set after a FETCH
Puts the result set into a temporary table Stores the result set in an external table
Combines the result sets of two SELECT statements Same as UNION ALL, but discards duplicate rows
Projection Clause
The Projection clause (sometimes called the Select clause) specifies a list of database objects or expressions to retrieve, and can set restrictions on qualifying rows. (The select list is sometimes also called the projection list.) Projection Clause:
2-481
SELECT
(1) (2) SKIP offset off_var FIRST (1) LIMIT (3) MIDDLE max (1) max_var
(2)
Select List:
(4) Expression column AS column table. display_label view. AS synonym. * alias. (3) external. * (1) (5) ( Collection Subquery ) subquery display_label
Notes: 1 2 3 4 5 Dynamic Server only Informix extension Extended Parallel Server only See Expression on page 4-34 See Collection Subquery on page 4-3
2-482
SELECT
Element alias column display _label external max max_var Description Temporary table or view name. See FROM Clause on page 2-492. Column from which to retrieve data Temporary name declared here for a column or for an expression External table from which to retrieve data Integer (> 0) specifying maximum number of rows to return Host variable or local SPL variable storing the value of max Integer (> 0) specifying how many qualifying rows to exclude before the first row of the result set Host variable or local SPL variable storing the value of offset Embedded query Name of a table, view, or synonym from which to retrieve data Restrictions Syntax
Valid only if the FROM clause declares Identifier on page the alias for table or view 5-22 Must exist in a data source that the FROM clause references See Declaring a Display Label on page 2-489 Must exist If max > number of qualifying rows then all matching rows are returned Identifier on page 5-22 Identifier on page 5-22 Database Object Name on page 5-17 Literal Number on page 4-137
Same as max; valid in prepared objects Language dependent and in SPL routines Cannot be negative. If offset > (number Literal Number on of qualifying rows), then no rows are page 4-137 returned Same as offset; valid in prepared objects and in user-defined routines Cannot include the SKIP or FIRST clause nor the ORDER BY clause The synonym and the table or view to which it points must exist Language dependent SELECT on page 2-479 Database Object Name on page 5-17
3 offset 3 3 3 off_var 3
subquery table, view, synonym
The asterisk ( * ) specifies all columns in the table or view in their defined order. To retrieve all columns in another order, or a subset of columns, you must specify individual column names explicitly. A solitary asterisk ( * ) can be a valid Projection clause if the FROM clause specifies only a single data source. 3 3 The SKIP, FIRST, LIMIT, MIDDLE, DISTINCT, and UNIQUE specifications can restrict results to a subset of the qualifying rows, as sections that follow explain.
2-483
SELECT
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 You can also use a host variable to specify how many rows to exclude. In an SPL routine, you can use an input parameter or a local variable to provide this value. When you use the SKIP option in a query with an ORDER BY clause, you can exclude the first offset rows that have the lowest values according to the ORDER BY criteria. You can also use SKIP to exclude rows with the highest values, if the ORDER BY clause includes the DESC keyword. For example, the following query returns all rows of the orders table, except for the fifty oldest orders:
SELECT SKIP 50 * FROM orders ORDER BY order_date
Here the result set is empty if there are fewer than 50 rows in the orders table. An offset = 0 is not invalid, but in that case the SKIP option does nothing. You can also use the SKIP option to restrict the result sets of prepared SELECT statements, of UNION queries, in queries whose result set defines a collection-derived table, and in the events and actions of triggers. You can use the SKIP and the FIRST options together to specify which and how many qualifying rows are in the result set, as illustrated by examples in the section Using the SKIP Option with the FIRST Option (IDS) on page 2-485. The SKIP option is not valid in the following contexts: v In the definition of a view v In nested SELECT statements v In subqueries.
Dynamic Server can use a host variable or the value of an SPL input parameter in a local variable to specify max. With an ORDER BY clause, you can retrieve the first max qualifying rows. For example, the following query finds the ten highest-paid employees:
SELECT FIRST 10 name, salary FROM emp ORDER BY salary DESC
3 3 3 3 3 3 3 3 3 3
You can use the FIRST option in a query whose result set defines a collection-derived table (CDT) within the FROM clause of another SELECT statement. The following query specifies a CDT that has no more than ten rows:
SELECT * FROM TABLE(MULTISET(SELECT FIRST 10 * FROM employees ORDER BY employee_id)) vt(x,y), tab2 WHERE tab2.id = vt.x;
This example applies the FIRST option to the result of a UNION expression:
SELECT FIRST 10 a, b FROM tab1 UNION SELECT a, b FROM tab2
The FIRST option is not valid in any of the following contexts: v In the definition of a view v In nested SELECT statements
2-484
SELECT
v In subqueries v In a singleton SELECT (where max = 1) within an SPL routine v Where embedded SELECT statements are used as expressions 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
The same considerations apply to the SKIP and LIMIT keywords of Dynamic Server, and to the MIDDLE keyword of Extended Parallel Server. If no literal integer nor integer variable follows the LIMIT keyword in the Projection clause, Dynamic Server interprets LIMIT as a column name. If no data source in the FROM clause has a column with that name, the query fails with an error.
The next example uses in a query with SKIP and FIRST to insert no more than five rows from table tab1 into table tab2, beginning with the eleventh row:
INSERT INTO tab2 SELECT SKIP 10 FIRST 5 * FROM tab1;
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
The following collection subquery returns only the eleventh through fifteenth qualifying rows as a collection-derived table, orders these five rows by the value in column a, and stores this result set in a temporary table.
SELECT * FROM TABLE (MULTISET (SELECT SKIP 10 FIRST 5 a FROM tab3 ORDER BY a)) INTO TEMP
The following INSERT statement includes a collection subquery whose results define a collection-derived table. The rows are ordered by the value in column a, and are inserted into table tab1.
INSERT INTO tab1 (a) SELECT * FROM TABLE (MULTISET (SELECT SKIP 10 FIRST 5 a FROM tab3 ORDER BY a));
Queries that combine the FIRST or LIMIT and SKIP options with the ORDER BY clause can impose a unique order on the qualifying rows, so successive queries that increment the offset value by the value of max can partition the qualifying rows into disjunct subsets of max rows. This can support web applications that require a fixed page size, without requiring cursor management.
2-485
SELECT
You can use these features in distributed queries only if all of the participating database servers support the SKIP and FIRST options.
Allowing Duplicates
You can apply the ALL, UNIQUE, or DISTINCT keywords to indicate whether duplicate values are returned, if any exist. If you do not specify any of these keywords in the Projection clause, all qualifying rows are returned by default. Keyword ALL DISTINCT UNIQUE Effect Specifies that all qualifying rows are returned, regardless of whether duplicates exist. (This is the default specification.) Excludes duplicates of qualifying rows from the result set Excludes duplicate. (Here UNIQUE is a synonym for DISTINCT. This is an extension to the ANSI/ISO standard.)
For example, the next query returns all the unique ordered pairs of values from the stock_num and manu_code columns in rows of the items table. If several rows have the same pair of values, that pair appears only once in the result set:
SELECT DISTINCT stock_num, manu_code FROM items
You can specify DISTINCT or UNIQUE no more than once in each level of a query or subquery. The following example uses DISTINCT in both the query and in the subquery:
SELECT DISTINCT stock_num, manu_code FROM items WHERE order_num = (SELECT DISTINCT order_num FROM orders WHERE customer_num = 120)
2-486
SELECT
Queries cannot access a database of another database server unless both servers define TCP/IP connections in DBSERVERNAME or DBSERVERALIAS configuration parameters. This applies to any communication between Dynamic Server instances, even if both database servers reside on the same computer.
Selecting Constants: If you include a constant expression in the select list, the same value is returned for each row that the query returns (except when the constant expression is NEXTVAL). For a complete description of the syntax and use of constant expressions, see Constant Expressions on page 4-53. Examples that follow show constant expressions within a select list:
SELECT SELECT SELECT SELECT SELECT The first name is, fname FROM customer TODAY FROM cust_calls SITENAME FROM systables WHERE tabid = 1 lead_time - 2 UNITS DAY FROM manufact customer_num + LENGTH(string) from customer
Selecting Built-In Function Expressions: A built-in function expression uses a function that is evaluated for each row in the query. All built-in function expressions require arguments. This set of expressions contains the time functions
2-487
SELECT
and the length function when they are used with a column name as an argument. The following examples show built-in function expressions within the select list of the Projection clause:
SELECT SELECT SELECT SELECT EXTEND(res_dtime, YEAR TO SECOND) FROM cust_calls LENGTH(fname) + LENGTH(lname) FROM customer HEX(order_num) FROM orders MONTH(order_date) FROM orders
Selecting Aggregate Function Expressions: An aggregate function returns one value for a set of queried rows. This value depends on the set of rows that the WHERE clause of the SELECT statement qualifies. In the absence of a WHERE clause, the aggregate functions take on values that depend on all the rows that the FROM clause forms. Examples that follow show aggregate functions in a select list:
SELECT SUM(total_price) FROM items WHERE order_num = 1013 SELECT COUNT(*) FROM orders WHERE order_num = 1001 SELECT MAX(LENGTH(fname) + LENGTH(lname)) FROM customer
Selecting User-Defined Function Expressions: User-defined functions extend the range of functions that are available to you and allow you to perform a subquery on each row that you select. The following example calls the get_orders( ) user-defined function for each customer_num and displays the returned value under the n_orders label:
SELECT customer_num, lname, get_orders(customer_num) n_orders FROM customer
If an SPL routine in a SELECT statement contains certain SQL statements, the database server returns an error. For information on which SQL statements cannot be used in an SPL routine that is called within a query, see Restrictions on SPL Routines in Data-Manipulation Statements on page 5-71. For the complete syntax of user-defined function expressions, see User-Defined Functions on page 4-111. Selecting Expressions That Use Arithmetic Operators: You can combine numeric expressions with arithmetic operators to make complex expressions. You cannot combine expressions that contain aggregate functions with column expressions. These examples show expressions that use arithmetic operators within a select list in the Projection clause:
SELECT SELECT SELECT SELECT stock_num, quantity*total_price FROM customer price*2 doubleprice FROM items count(*)+2 FROM customer count(*)+LENGTH(ab) FROM customer
Selecting ROW Fields (IDS): You can select a specific field of a named or unnamed ROW type column with row.field notation, using a period ( . ) as a separator between the row and field names. For example, suppose you have the following table structure:
CREATE ROW TYPE one (a INTEGER, b FLOAT) CREATE ROW TYPE two (c one, d CHAR(10)) CREATE ROW TYPE three (e CHAR(10), f two) CREATE TABLE new_tab OF TYPE two CREATE TABLE three_tab OF TYPE three
The following examples show expressions that are valid in the select list:
2-488
SELECT
SELECT t.c FROM new_tab t SELECT f.c.a FROM three_tab SELECT f.d FROM three_tab
You can also enter an asterisk ( * ) in place of a field name to signify that all fields of the ROW-type column are to be selected. For example, if the my_tab table has a ROW-type column named rowcol that contains four fields, the following SELECT statement retrieves all four fields of the rowcol column:
SELECT rowcol.* FROM my_tab
You can also retrieve all fields from a row-type column by specifying only the column name. This example has the same effect as the previous query:
SELECT rowcol FROM my_tab
You can use row.field notation not only with ROW-type columns but with expressions that evaluate to ROW-type values. For more information, see Column Expressions on page 4-44 in the Expression segment.
For the keywords of SQL, see Appendix A, Reserved Words for IBM Informix Dynamic Server, on page A-1, or Appendix B, Reserved Words for IBM Informix Extended Parallel Server, on page B-1. If you are creating a temporary table, you must supply a display label for any columns that are not simple column expressions. The display label is used as the name of the column in the temporary table. If you are using the SELECT statement to define a view, do not use display labels. Specify the desired label names in the CREATE VIEW column list instead.
INTO Clause
Use the INTO clause in an SPL routine or an ESQL/C program to specify the program variables or host variables to receive data that SELECT retrieves. INTO Clause:
2-489
SELECT
, INTO output_var (1) :indicator_var (2) $indicator_var data_structure
Notes: 1 2
Element data_ structure indicator_ var output_ var Description Structure that was declared as a host variable Program variable to receive a return code if corresponding output_var receives a NULL value Program or host variable to receive value of the corresponding select list item. Can be a collection variable
The INTO clause specifies one or more variables that receive the values that the query returns. If it returns multiple values, they are assigned to the list of variables in the order in which you specify the variables. If the SELECT statement stands alone (that is, it is not part of a DECLARE statement and does not use the INTO clause), it must be a singleton SELECT statement. A singleton SELECT statement returns only one row. The number of receiving variables must be equal to the number of items in the select list of the Projection clause. The data type of each receiving variable should be compatible with the data type of the corresponding column or expression in the select list. For the actions that the database server takes when the data type of the receiving variable does not match that of the selected item, see Warnings in ESQL/C on page 2-492. The following example shows a singleton SELECT statement in ESQL/C:
EXEC SQL select fname, lname, company_name into :p_fname, :p_lname, :p_coname where customer_num = 101;
In an SPL routine, if a SELECT returns more than one row, you must use the FOREACH statement to access the rows individually. The INTO clause of the SELECT statement holds the fetched values. For more information, see FOREACH on page 3-20.
2-490
SELECT
2-491
SELECT
You can also use program variables in the FETCH statement to specify an element of a program array in the INTO clause. The program variables are evaluated at each fetch, rather than when you declare the cursor.
Error Checking
If the data type of the receiving variable does not match that of the selected item, the data type of the selected item is converted, if possible, to the data type of the variable. If the conversion is impossible, an error occurs, and a negative value is returned in the status variable, sqlca.sqlcode, or SQLCODE. In this case, the value in the program variable is unpredictable. In an ANSI-compliant database, if the number of variables that are listed in the INTO clause differs from the number of items in the select list of the Projection clause, you receive an error. Warnings in ESQL/C: In ESQL/C, if the number of variables listed in the INTO clause differs from the number of items in the Projection clause, a warning is returned in the sqlwarn structure: sqlca.sqlwarn.sqlwarn3. The actual number of variables that are transferred is the lesser of the two numbers. For information about the sqlwarn structure, see the IBM Informix ESQL/C Programmer's Manual.
FROM Clause
The FROM clause lists the table objects from which you are selecting the data. FROM Clause:
, FROM Table Reference , (1) (2) Informix-Extension OUTER Clause , , (1) (2) Informix-Extension OUTER Clause , Table Reference , , (3) (4) ANSI Table Reference
Table Reference:
Relation (5) num SAMPLES OF (5) LOCAL AS (2) (7) (2) (4) (8) (9) Collection-Derived Table (4) Iterator (6) (2) alias
Relation:
2-492
SELECT
table view synonym (5) external (5) (subquery) (4) ONLY (synonym) (table)
Notes: 1 2 3 4 5 6 7 8 9 See Informix-Extension Outer Joins on page 2-506 Informix extension See ANSI Table Reference on page 2-501 Dynamic Server only Extended Parallel Server only See Iterator Functions (IDS) on page 2-499 See Collection-Derived Table on page 5-5 Stored Procedure Language only ESQL/C only
Description Temporary name for a table or view in this query External table from which to retrieve data Number of rows to sample Specifies rows to be retrieved Restrictions See The AS Keyword on page 2-494. Must exist but cannot be the outer table in an outer join Unsigned integer > 0 Cannot be a correlated subquery Syntax Identifier on page 5-22 Database Object Name on page 5-17 Literal Number on page 4-137 SELECT on page 2-479 Database Object Name on page 5-17
Synonym for a table from which Synonym and table or view to to retrieve data which it points must exist
If the FROM clause specifies more than one data source, the query is called a join, because its result set can join rows from several table references. For more information about joins, see Queries that Join Tables on page 2-500.
2-493
SELECT
The next example is equivalent to the second query in the preceding example, but it declares aliases in the FROM clause and uses them in the WHERE clause:
SELECT fname, lname, order_num FROM customer c, orders o WHERE c.customer_num = o.customer_num
Aliases (sometimes called correlation names) are especially useful with a self-join. For more information about self-joins, see Self-Joins on page 2-512. In a self-join, you must list the table name twice in the FROM clause and declare a different alias for each of the two instances of table name. The AS Keyword: If you use a potentially ambiguous word as an alias (or as a display label), you must begin its declaration with the keyword AS. This keyword is required if you use any of the keywords ORDER, FOR, AT, GROUP, HAVING, INTO, NOT, UNION, WHERE, WITH, CREATE, or GRANT as an alias for a table or view. The database server would issue an error if the next example did not include the AS keyword to indicate that not is a display label, rather than an operator:
CREATE TABLE t1(a INT); SELECT a AS not FROM t1
If you do not declare an alias for a collection-derived table, the database server assigns an implementation-dependent name to it.
2-494
SELECT
Apart from these restrictions, any valid SQL query can be a table expression. A table expression can be nested within another table expression, and can include tables and views in its definition. You can use table expressions in CREATE VIEW statements to define views. Usability and Performance Considerations: Although equivalent functionality is available through views, subqueries as table expressions simplify the formulation of queries, make the syntax more flexible and intuitive, and support the ANSI/ISO standard for SQL. Performance might be affected, however, if you use table expressions. It is advisable to use subqueries if you really do not need to use table expressions. The following are examples of valid table expressions:
SELECT * FROM (SELECT * FROM t); SELECT * FROM (SELECT * FROM t) AS s; SELECT * FROM (SELECT * FROM t) AS s WHERE t.a = s.b; SELECT * FROM (SELECT * FROM t) AS s, (SELECT * FROM u) AS v WHERE s.a = v.b; SELECT * FROM (SELECT * FROM t WHERE t.a = 1) AS s, OUTER (SELECT * FROM u WHERE u.b = 2 GROUP BY 1) AS v WHERE s.a = v.b; SELECT * FROM (SELECT a AS colA FROM t WHERE t.a = 1) AS s, OUTER (SELECT b AS colB FROM u WHERE u.b = 2 GROUP BY 1) AS v WHERE s.colA = v.colB; CREATE VIEW vu AS SELECT * FROM (SELECT * FROM t); SELECT * FROM ((SELECT * FROM t) AS r) AS s;
2-495
SELECT
When a query involves a join, you must plan carefully if you want to extract data that the client can aggregate. The simplest way to ensure that a join will retrieve data suitable for aggregation is to limit the number of LOCAL tables to one. The client can then aggregate data with respect to that table. The following example shows a query that returns data suitable for aggregation by the client:
SELECT x.col1, y.col2 FROM LOCAL tab1 x, tab2 y INTO TEMP t1 WHERE x.col1 = y.col1; {can be aggregated by client} local} {tab1 is
The following example shows data that the client cannot aggregate:
SELECT x.col1, y.col2 FROM LOCAL tab1 x, LOCAL tab2 INTO SCRATCH s4 WHERE x.col1 = y.col1; {client cannot aggregate} tab2 are local} {tab1 and
The client must submit exactly the same query to each coserver to retrieve data that can be aggregated.
2-496
SELECT
STATISTICS, the SAMPLES OF clause is ignored, and all data values are returned. For better results, run UPDATE STATISTICS MEDIUM before you run the query with the SAMPLES OF option. The results of a sampled query contain a certain amount of deviation from a complete scan of all rows. You can reduce this expected error to an acceptable level by increasing the proportion of sampled rows to actual rows. When you use sampled queries in joins, the expected error increases dramatically; you must use larger samples in each table to retain an acceptable level of accuracy. For example, you might want to generate a list of how many of each part is sold from the parts_sold table, which is known to contain approximately 100,000,000 rows. The following query provides a sampling ratio of one percent and returns an approximate result:
SELECT part_number, COUNT(*) * 100 AS how_many FROM 1000000 SAMPLES OF parts_sold GROUP BY part_number;
2-497
SELECT
For more information, including restrictions on the SELECT statement, see Using a SELECT ... INTO Statement on page 3-21. When one of the tables to be joined is a collection, the FROM clause cannot specify a join. This restriction applies when the collection variable holds your collection-derived table. See also Collection-Derived Table on page 5-5. and the INSERT, UPDATE, and DELETE statement descriptions in this chapter.
The SELECT statement on a row variable has the following restrictions: v No expressions are allowed in the select list of the Projection clause. v ROW columns cannot be in a WHERE clause comparison condition. v The Projection clause must be an asterisk ( * ) if the row-type contains fields of opaque, distinct, or built-in data types. v Columns listed in the Projection clause can have only unqualified names. They cannot use the syntax database@server:table.column. v The following clauses are not allowed: GROUP BY, HAVING, INTO TEMP, ORDER BY, and WHERE. v The FROM clause has no provisions to do a join.
2-498
SELECT
You can modify the row variable with the Collection-Derived-Table segment of the UPDATE statements. (The INSERT and DELETE statements do not support a row variable in the Collection-Derived-Table segment.) The row variable stores the fields of the row. It has no intrinsic connection, however, with a database column. Once the row variable contains the correct field values, you must then save the variable into the ROW column with one of the following SQL statements: v To update the ROW column in the table with the row variable, use an UPDATE statement on a table or view name and specify the row variable in the SET clause. For more information, see Updating ROW-Type Columns (IDS) on page 2-643. v To insert a row into a ROW column, use the INSERT statement on a table or view and specify the row variable in the VALUES clause. See Inserting Values into ROW-Type Columns (IDS) on page 2-401. For examples of how to use SPL row variables, see the IBM Informix Guide to SQL: Tutorial. For information on using ESQL/C row variables, see the discussion of complex data types in the IBM Informix ESQL/C Programmer's Manual.
table AS ( , column )
Notes: 1
Element column iterator table Description Name declared here for a virtual column in table Name of the iterator function
Name declared here for virtual table Cannot include qualifiers holding iterator result set
The table can only be referenced within the context of this query. After the SELECT statement terminates, the virtual table no longer exists.
2-499
SELECT
The number of columns must match the number of values returned by the iterator. An external function can return no more than one value (but that can be of a collection data type). An SPL routine can return multiple values. To reference the virtual table columns in other parts of the SELECT statement, for example, in the WHERE clause or HAVING clause, you must declare its name and the virtual column names in the FROM clause. You do not need to declare the table name or column names in the FROM clause if you use the asterisk notation in the Projection list of the SELECT clause:
SELECT * FROM ...
For more information and examples of using iterator functions in queries, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
The last four categories are collectively called Join Types in the literature of the relational model; a CROSS JOIN ignores the specific data values in joined tables, returning every possible pair of rows, where one row is from each table. In an inner (or simple) join, the result contains only the combination of rows that satisfy the join conditions. Outer joins preserve rows that otherwise would be discarded by inner joins. In an outer join, the result contains the combination of rows that satisfy the join conditions and the rows from the dominant table that would otherwise be discarded. The rows from the dominant table that do not have matching rows in the subordinate table contain NULL values in the columns selected from the subordinate table. Dynamic Server supports the two different syntaxes for left outer joins: v Informix-extension syntax v ANSI-compliant syntax Earlier versions of the database server supported only Informix-extension syntax for outer joins. Dynamic Server continues to support this legacy syntax, but using the ANSI-compliant syntax for joins provides greater flexibility in creating queries. In view definitions, however, the legacy syntax does not require materialized views, so it might offer performance advantages.
2-500
SELECT
If you use ANSI-compliant syntax to specify a join in the FROM clause, you must also use ANSI-compliant syntax for all outer joins in the same query block. Thus, you cannot begin another outer join with only the OUTER keyword. The following query, for example, is not valid:
SELECT * FROM customer, OUTER orders RIGHT JOIN cust_calls ON (customer.customer_num = orders.customer_num) WHERE customer.customer_num = 104);
This returns an error because it attempts to combine the Informix-extension OUTER syntax with the ANSI-compliant RIGHT JOIN syntax for outer joins. See the section Informix-Extension Outer Joins on page 2-506 for the Informix-extension syntax for LEFT OUTER joins.
2-501
SELECT
3 4 5 6
Element alias synonym, table, view Description Temporary name for a table or view within the scope of the SELECT Source from which to retrieve data
ESQL/C only See Collection-Derived Table on page 5-5 See Iterator Functions (IDS) on page 2-499 See ANSI Joined Tables
Restrictions See The AS Keyword on page 2-494. The synonym and the table or view to which it points must exist Syntax Identifier on page 5-22 Database Object Name on page 5-17
Here the ONLY keyword is the same semantically as in the Informix-extension Table Reference segment, as described in The ONLY Keyword (IDS) on page 2-497. The AS keyword is optional when you declare an alias (also called a correlation name) for a table reference, as described in The AS Keyword on page 2-494, unless the alias conflicts with an SQL keyword. Creating an ANSI Join: With ANSI-compliant joined table syntax, as shown in the following diagram, you can specify the INNER JOIN, CROSS JOIN, NATURAL JOIN, LEFT JOIN (or LEFT OUTER JOIN), RIGHT JOIN (and FULL OUTER JOIN keywords. The OUTER keyword is optional in ANSI-compliant outer joins. You must use the same form of join syntax (either Informix extension or ANSI-compliant) for all of the outer joins in the same query block. When you use the ANSI-compliant syntax, you must also specify the join condition in the ON clause, as described in Using the ON Clause on page 2-504. ANSI Joined Tables: This is the ANSI-compliant syntax for specifying inner and outer joins. ANSI Joined Tables:
(1) ANSI Table Reference Join Options (1) (2) CROSS JOIN ( ANSI Joined Tables ) ANSI Table Reference
Join Options:
INNER LEFT (3) RIGHT (3) FULL (1) JOIN OUTER ANSI Table Reference ON Clause
2-502
SELECT
ON Clause:
AND (4) ON Join (5) Function Expression (2) Condition (subquery) (6) (3) ( OR (4) Join (5) Function Expression (2) Condition (subquery) (3) ( Collection Subquery Collection Subquery )
(6) )
Notes: 1 2 3 4 5 6 See ANSI Table Reference on page 2-501 See Condition on page 4-5 Dynamic Server only See Specifying a Join in the WHERE Clause on page 2-511 See Function Expressions on page 4-68 See Collection Subquery on page 4-3
Description Embedded query Restrictions Cannot contain the FIRST or the ORDER BY clause Syntax SELECT on page 2-479
Element subquery
The ANSI-Joined Table segment must be enclosed between parentheses if it is immediately followed by another join specification. For example, the first of the following two queries returns an error; the second query is valid:
SELECT * FROM (T1 LEFT JOIN T2) CROSS JOIN T3 ON (T1.c1 = T2.c5) WHERE (T1.c1 < 100); -- Ambiguous order of operations SELECT * FROM (T1 LEFT JOIN T2 ON (T1.c1 = T2.c5)) CROSS JOIN T3 WHERE (T1.c1 < 100); -- Unambiguous order of operations
ANSI CROSS Joins: The CROSS keyword specifies the Cartesian product, returning all possible paired combinations that include one row from each of the joined tables. ANSI INNER Joins: To create an inner (or simple) join using the ANSI-compliant syntax, specify the join with the JOIN or INNER JOIN keywords. If you specify only the JOIN keyword, the database server creates an implicit inner join by default. An inner join returns all the rows in a table that have one or more matching rows in the other table (or tables). The unmatched rows are discarded.
2-503
SELECT
ANSI LEFT OUTER Joins: The LEFT keyword specifies a join that treats the first table reference as the dominant table in the join. In a left outer join, the subordinate part of the outer join appears to the right of the keyword that begins the outer join specification. The result set includes all the rows that an INNER join returns, plus all rows that would otherwise have been discarded from the dominant table. ANSI RIGHT OUTER Joins: The RIGHT keyword specifies a join that treats the second table reference as the dominant table in the join. In a right outer join, the subordinate part of the outer join appears to the left of the keyword that begins the outer join specification. The result set includes all the rows that an INNER join returns, plus all rows that would otherwise have been discarded from the dominant table. ANSI FULL OUTER Joins: The FULL keyword specifies a join in which the result set includes all the rows from the Cartesian product for which the join condition is true, plus all the rows from each table that do not match the join condition. In an ANSI-compliant join that specifies the LEFT, RIGHT, or FULL keywords in the FROM clause, the OUTER keyword is optional. Optimizer directives that you specify for an ANSI-compliant joined query are ignored, but are listed under Directives Not Followed in sqlexplain.out.
The following table shows part of the joined customer and orders tables.
customer_num 101 102 103 104 company All Sports Supplies Sports Spot Phils Sports Play Ball! phone 408-789-8075 415-822-1289 415-328-4543 415-368-1100 order_date 05/21/1998 NULL NULL 05/20/1998
In an outer join, the join filters (expressions) that you specify in the ON clause determine which rows of the subordinate table join to the dominant (or outer) table. The dominant table, by definition, returns all its rows in the joined table. That is, a join filter in the ON clause has no effect on the dominant table. If the ON clause specifies a join filter on the dominant table, the database server joins only those dominant table rows that meet the criterion of the join filter to rows in the subordinate table. The joined result contains all rows from the dominant table. Rows in the dominant table that do not meet the criterion of the join filter are extended with NULL values for the subordinate columns.
2-504
SELECT
The following example from the stores_demo database illustrates the effect of a join filter in the ON clause:
SELECT c.customer_num, c.company, c.phone, o.order_date FROM customer c LEFT JOIN orders o ON c.customer_num = o.customer_num AND c.company <> "All Sports Supplies"
The row that contains All Sports Supplies remains in the joined result.
customer_num 101 102 103 104 company All Sports Supplies Sports Spot Phils Sports Play Ball! phone 408-789-8075 415-822-1289 415-328-4543 415-368-1100 order_date NULL NULL NULL 05/20/1998
Even though the order date for customer number 101 is 05/21/1998 in the orders table, the effect of placing the join filter (c.company <> "All Sports Supplies") prevents this row in the dominant customer table from being joined to the subordinate orders table. Instead, a NULL value for order_date is extended to the row of All Sports Supplies. Applying a join filter to a base table in the subordinate part of an outer join can improve performance. For more information, see your IBM Informix Performance Guide. Specifying a Post-Join Filter: When you use the ON clause to specify the join, you can use the WHERE clause as a post-join filter. The database server applies the post-join filter of the WHERE clause to the results of the outer join. The following example illustrates the use of a post-join filter. This query returns data from the stores_demo database. Suppose you want to determine which items in the catalog are not being ordered. The next query creates an outer join of the data from the catalog and items tables and then determines which catalog items from a specific manufacturer (HRO) have not sold:
SELECT c.catalog_num, c.stock_num, c.manu_code, i.quantity FROM catalog c LEFT JOIN items i ON c.stock_num = i.stock_num AND c.manu_code = i.manu_code WHERE i.quantity IS NULL AND c.manu_code = "HRO"
The WHERE clause contains the post-join filter that locates the rows of HRO items in the catalog for which nothing has been sold. When you apply a post-join filter to a base table in the dominant or subordinate part of an outer join, you might improve performance. For more information, see your IBM Informix Performance Guide. Using a Join as the Dominant or Subordinate Part of an Outer Join: With the ANSI join syntax, you can nest joins. You can use a join as the dominant or subordinate part of an outer or inner join. Suppose you want to modify the previous query (the post-join filter example) to get more information that will help you determine whether to continue carrying
2-505
SELECT
each unsold item in the catalog. You can modify the query to include information from the stock table so that you can see a short description of each unsold item with its cost:
SELECT c.catalog_num, c.stock_num, s.description, s.unit_price, s.unit_descr, c.manu_code, i.quantity FROM (catalog c INNER JOIN stock s ON c.stock_num = s.stock_num AND c.manu_code = s.manu_code) LEFT JOIN items i ON c.stock_num = i.stock_num AND c.manu_code = i.manu_code WHERE i.quantity IS NULL AND c.manu_code = "HRO"
In this example, an inner join between the catalog and stock tables forms the dominant part of an outer join with the items table. Additional Examples of Outer Joins: For additional examples of outer joins, see the IBM Informix Guide to SQL: Tutorial.
, (2) ) (1)
Notes: 1 2 See FROM Clause on page 2-492 Informix extension If you use this syntax for an outer join, you must use Informix-extension syntax for all outer joins in a single query block, and you must include the join condition in the WHERE clause. You cannot begin another outer join with the LEFT JOIN or the LEFT OUTER JOIN keywords. This example uses the OUTER keyword to create an outer join that lists all customers and their orders, regardless of whether they have placed orders:
SELECT c.customer_num, c.lname, o.order_num FROM customer c, OUTER orders o WHERE c.customer_num = o.customer_num
This example returns all the rows from the customer table with the rows that match in the orders table. If no record for a customer appears in the orders table, the returned order_num column for that customer has a NULL value. If you have a complex outer join, that is, the query has more than one outer join, you must either embed the additional outer join or joins in parentheses, as the
2-506
SELECT
syntax diagram shows, or establish join conditions, or relationships, between the dominant table and each subordinate table in the WHERE clause. When an expression or a condition in the WHERE clause relates two subordinate tables, you must use parentheses around the joined tables in the FROM clause to enforce dominant-subordinate relationships, as in this example:
SELECT c.company, o.order_date, i.total_price, m.manu_name FROM customer c, OUTER (orders o, OUTER (items i, OUTER manufact m)) WHERE c.customer_num = o.customer_num AND o.order_num = i.order_num AND i.manu_code = m.manu_code;
When you omit parentheses around the subordinate tables in the FROM clause, you must establish join conditions between the dominant table and each subordinate table in the WHERE clause. If a join condition is between two subordinate tables, the query will fail, but the following example successfully returns a result:
SELECT c.company, o.order_date, c2.call_descr FROM customer c, OUTER orders o, OUTER cust_calls c2 WHERE c.customer_num = o.customer_num AND c.customer_num = c2.customer_num;
The IBM Informix Guide to SQL: Tutorial has examples of complex outer joins.
WHERE Clause
The WHERE clause can specify join conditions for Informix-extension joins, post-join filters for ANSI-compliant joins, and search criteria on data values. WHERE Clause:
Logical_Operator (1) Condition (2) Join (3) Function Expression (subquery) (4) ( Collection Subquery
WHERE
(5) ) (6)
Notes: 1 2 3 4 5 6 See Condition on page 4-5 See Specifying a Join in the WHERE Clause on page 2-511 See Function Expressions on page 4-68 Dynamic Server only See Collection Subquery on page 4-3 See Statement-Local Variable Expressions (IDS) on page 4-114
2-507
SELECT
Element Logical_ Operator subquery Description Combines two conditions Embedded query Restrictions Valid options are logical union (= OR or OR NOT) or logical intersection ( = AND or AND NOT) Cannot include the FIRST or ORDER BY keywords Syntax Conditions with AND or OR on page 4-17 SELECT on page 2-479
You also can use a SELECT statement within the WHERE clause; this is called a subquery. The following WHERE clause operators are valid in a subquery: v IN or EXISTS v ALL, ANY, or SOME For more information, see Condition on page 4-5. In the WHERE clause, an aggregate function is not valid unless it is part of a subquery or is on a correlated column originating from a parent query, and the WHERE clause is in a subquery within a HAVING clause. Relational-Operator Condition: A relational-operator condition is satisfied if the expressions on each side of the operator fulfill the relation that the operator specifies. The following SELECT statements use the greater than ( > ) and equal ( = ) relational operators:
SELECT order_num FROM orders WHERE order_date > 6/04/98 SELECT fname, lname, company FROM customer WHERE city[1,3] = San
Quotes are required around San because the substring is from a character column. See the Relational-Operator Condition on page 4-8. IN Condition: The IN condition is satisfied when the expression to the left of the IN keyword is included in the list of values to the right of the keyword. The following examples show the IN condition:
SELECT lname, fname, company FROM customer WHERE state IN (CA,WA, NJ) SELECT * FROM cust_calls WHERE user_id NOT IN (USER )
For more information, see the IN Subquery on page 4-14. BETWEEN Condition: The BETWEEN condition is satisfied when the value to the left of BETWEEN is in the inclusive range of the two values on the right of BETWEEN. The first two queries in the following example use literal values after the BETWEEN keyword. The third query uses the built-in CURRENT function and a literal interval to search for dates between the current day and seven days earlier.
2-508
SELECT
SELECT stock_num, manu_code FROM stock WHERE unit_price BETWEEN 125.00 AND 200.00 SELECT DISTINCT customer_num, stock_num, manu_code FROM orders, items WHERE order_date BETWEEN 6/1/97 AND 9/1/97 SELECT * FROM cust_calls WHERE call_dtime BETWEEN (CURRENT - INTERVAL(7) DAY TO DAY) AND CURRENT
For more information, see the BETWEEN Condition on page 4-8. IS NULL Condition: The IS NULL condition is satisfied if the column contains a NULL value. If you use the NOT option, the condition is satisfied when the column contains a value that is not NULL. The following example selects the order numbers and customer numbers for which the order has not been paid:
SELECT order_num, customer_num FROM orders WHERE paid_date IS NULL
For a complete description, see the IS NULL Condition on page 4-10. LIKE or MATCHES Condition: The LIKE or MATCHES condition is satisfied if either of the following is true: v The value of the column that precedes the LIKE or MATCHES keyword matches the pattern that the quoted string specifies. You can use wildcard characters in the string. v The value of the column that precedes the LIKE or MATCHES keyword matches the pattern that is specified by the column that follows the LIKE or MATCHES keyword. The value of the column on the right serves as the matching pattern in the condition. The following SELECT statement returns all rows in the customer table in which the lname column begins with the literal string Baxter. Because the string is a literal string, the condition is case sensitive.
SELECT * FROM customer WHERE lname LIKE Baxter%
The next SELECT statement returns all rows in the customer table in which the value of the lname column matches the value of the fname column:
SELECT * FROM customer WHERE lname LIKE fname
The following examples use the LIKE condition with a wildcard. The first SELECT statement finds all stock items that are some kind of ball. The second SELECT statement finds all company names that contain a percent ( % ) sign. Backslash ( \ ) is used as the default escape character for the percent ( % ) sign wildcard. The third SELECT statement uses the ESCAPE option with the LIKE condition to retrieve rows from the customer table in which the company column includes a percent ( % ) sign. The z is used as an escape character for the percent ( % ) sign:
SELECT stock_num, manu_code FROM stock WHERE description LIKE %ball SELECT * FROM customer WHERE company LIKE %\%% SELECT * FROM customer WHERE company LIKE %z%% ESCAPE z
The following examples use MATCHES with a wildcard in SELECT statements. The first SELECT statement finds all stock items that are some kind of ball. The second SELECT statement finds all company names that contain an asterisk ( * ). The backslash ( \ ) is used as the default escape character for a literal asterisk ( * ) character. The third statement uses the ESCAPE option with the MATCHES
2-509
SELECT
condition to retrieve rows from the customer table where the company column includes an asterisk ( * ). The z character is specified as an escape character for the asterisk ( * ) character:
SELECT stock_num, manu_code FROM stock WHERE description MATCHES *ball SELECT * FROM customer WHERE company MATCHES *\** SELECT * FROM customer WHERE company MATCHES *z** ESCAPE z
See also the LIKE and MATCHES Condition on page 4-11. IN Subquery: With the IN subquery, more than one row can be returned, but only one column can be returned. This example shows the use of an IN subquery in a SELECT statement:
SELECT DISTINCT customer_num FROM orders WHERE order_num NOT IN (SELECT order_num FROM items WHERE stock_num = 1)
For additional information, see the IN Condition on page 4-9. EXISTS Subquery: With the EXISTS subquery, one or more columns can be returned. The following example of a SELECT statement with an EXISTS subquery returns the stock number and manufacturer code for every item that has never been ordered (and is therefore not listed in the items table). It is appropriate to use an EXISTS subquery in this SELECT statement because you need the correlated subquery to test both stock_num and manu_code in the items table.
SELECT stock_num, manu_code FROM stock WHERE NOT EXISTS (SELECT stock_num, manu_code FROM items WHERE stock.stock_num = items.stock_num AND stock.manu_code = items.manu_code)
The preceding example would work equally well if you use a SELECT * in the subquery in place of the column names, because you are testing for the existence of a row or rows. For additional information, see the EXISTS Subquery on page 4-14. ALL, ANY, SOME Subqueries: The following examples return the order number of all orders that contain an item whose total price is greater than the total price of every item in order number 1023. The first SELECT uses the ALL subquery, and the second SELECT produces the same result by using the MAX aggregate function.
SELECT DISTINCT order_num FROM items WHERE total_price > ALL (SELECT total_price FROM items WHERE order_num = 1023) SELECT DISTINCT order_num FROM items WHERE total_price > SELECT MAX(total_price) FROM items WHERE order_num = 1023)
2-510
SELECT
The following SELECT statements return the order number of all orders that contain an item whose total price is greater than the total price of at least one of the items in order number 1023. The first SELECT statement uses the ANY keyword, and the second SELECT statement uses the MIN aggregate function:
SELECT DISTINCT order_num FROM items WHERE total_price > ANY (SELECT total_price FROM items WHERE order_num = 1023) SELECT DISTINCT order_num FROM items WHERE total_price > (SELECT MIN(total_price) FROM items WHERE order_num = 1023)
You can omit the keywords ANY, ALL, or SOME in a subquery if the subquery returns exactly one value. If you omit ANY, ALL, or SOME, and the subquery returns more than one value, you receive an error. The subquery in the next example returns only one row, because it uses an aggregate function:
SELECT order_num FROM items WHERE stock_num = 9 AND quantity = (SELECT MAX(quantity) FROM items WHERE stock_num = 9)
Data Source:
alias . table . view . synonym . (2) external .
Notes: 1 2 See Relational Operator on page 4-146 Extended Parallel Server only
2-511
SELECT
Element alias column external Description Restrictions Syntax Identifier on page 5-22 Identifier on page 5-22 Database Object Name on page 5-17 Database Object Name on page 5-17
Temporary alternative name declared in the See Self-Joins on page 2-512; FROM clause for a table or view FROM Clause on page 2-492 Column of a table or view to be joined External table from which to retrieve data Must exist in the table or view External table must exist
Rows from the tables or views are joined when there is a match between the values of specified columns. When the columns to be joined have the same name, you must qualify each column name with its data source. Two-Table Joins: You can create two-table joins, multiple-table joins, self-joins, and outer joins (Informix-extension syntax). The following example shows a two-table join:
SELECT order_num, lname, fname FROM customer, orders WHERE customer.customer_num = orders.customer_num
Multiple-Table Joins: A multiple-table join is a join of more than two tables. Its structure is similar to the structure of a two-table join, except that you have a join condition for more than one pair of tables in the WHERE clause. When columns from different tables have the same name, you must qualify the column name with its associated table or table alias, as in table.column. For the full syntax of a table name, see Database Object Name on page 5-17. The following multiple-table join yields the company name of the customer who ordered an item as well as its stock number and manufacturer code:
SELECT DISTINCT company, stock_num, manu_code FROM customer c, orders o, items i WHERE c.customer_num = o.customer_num AND o.order_num = i.order_num
Self-Joins: You can join a table to itself. To do so, you must list the table name twice in the FROM clause and assign it two different table aliases. Use the aliases to refer to each of the two tables in the WHERE clause. The next example is a self-join on the stock table. It finds pairs of stock items whose unit prices differ by a factor greater than 2.5. The letters x and y are each aliases for the stock table.
SELECT x.stock_num, x.manu_code, y.stock_num, y.manu_code FROM stock x, stock y WHERE x.unit_price > 2.5 * y.unit_price
Extended Parallel Server does not support self-joins with an external table. Informix-Extension Outer Joins: The next outer join lists the company name of the customer and all associated order numbers, if the customer has placed an order. If not, the company name is still listed, and a NULL value is returned for the order number.
SELECT company, order_num FROM customer c, OUTER orders o WHERE c.customer_num = o.customer_num
In Extended Parallel Server, you cannot use an external table as the outer table in an outer join.
2-512
SELECT
For more information about outer joins, see the IBM Informix Guide to SQL: Tutorial.
GROUP BY Clause
Use the GROUP BY clause to produce a single row of results for each group. A group is a set of rows that have the same values for each column listed. GROUP BY Clause:
, GROUP BY (1) Data Source (2) select_number column
Notes: 1 2
Element column Description A column (or an expression) from the select list of the Projection clause
See Relationship of GROUP BY and Identifier on Projection Clauses page 5-22 See Using Select Numbers on page Literal Number 2-514. on page 4-137
select _number Integer specifying ordinal position of a column or expression in select list
The SELECT statement with a GROUP BY clause returns a single row of results for each group of rows that have the same value in column, or that have the same value in the column or expression that the select_number specifies.
2-513
SELECT
the argument of an aggregate function. The COUNT and SUM aggregates are applied to each group, not to the whole query set.
SELECT order_num, COUNT(*), SUM(total_price) FROM items GROUP BY order_num
If a column stands alone in a column expression in the select list, you must use it in the GROUP BY clause. If a column is combined with another column by an arithmetic operator, you can choose to group by the individual columns or by the combined expression using the number.
HAVING Clause
Use the HAVING clause to apply one or more qualifying conditions to groups. HAVING Clause:
(1) HAVING Condition
In the following examples, each condition compares one calculated property of the group with another calculated property of the group or with a constant. The first SELECT statement uses a HAVING clause that compares the calculated expression COUNT(*) with the constant 2. The query returns the average total price per item on all orders that have more than two items. The second SELECT statement lists customers and the call months for customers who have made two or more calls in the same month:
SELECT order_num, AVG(total_price) FROM items GROUP BY order_num HAVING COUNT(*) > 2 SELECT customer_num, EXTEND (call_dtime, MONTH TO MONTH) FROM cust_calls GROUP BY 1, 2 HAVING COUNT(*) > 1
You can use the HAVING clause to place conditions on the GROUP BY column values as well as on calculated values. This example returns cust_code and customer_num, call_dtime, and groups them by call_code for all calls that have been received from customers with customer_num less than 120:
2-514
SELECT
SELECT customer_num, EXTEND (call_dtime), call_code FROM cust_calls GROUP BY call_code, 2, 1 HAVING customer_num < 120
The HAVING clause generally complements a GROUP BY clause. If you omit the GROUP BY clause, the HAVING clause applies to all rows that satisfy the query, and all rows in the table make up a single group. The following example returns the average price of all the values in the table, as long as more than ten rows are in the table:
SELECT AVG(total_price) FROM items HAVING COUNT(*) > 10
ORDER BY Clause
The ORDER BY clause sorts query results by specified columns or expressions. ORDER BY Clause:
, ASC ORDER BY (1) Data Source (2) Expression select_number display_label (3) (4) ROWID column (3) [ first, last ] DESC
Notes: 1 2 3 4 See WHERE Clause on page 2-507 See Expression on page 4-34 Informix extension Dynamic Server only
Description Sort rows by value in this column Restrictions Must also be in select list (Extended Parallel Server only). Syntax Identifier on page 5-22 Identifier on page 5-22 Literal Number on page 4-137 Literal Number on page 4-137
Temporary name for a column or for a Must be unique among labels column expression declared in the Projection clause First and last byte in column substring Integers; for BYTE, TEXT, and to sort the result set character data types only Ordinal position of a column in select list of the Projection clause See Using Select Numbers on page 2-514.
2-515
SELECT
SELECT paid_date - ship_date span, customer_num FROM orders ORDER BY span
Dynamic Server supports columns and expressions in the ORDER BY clause that do not appear in the select list of the Projection clause. You can omit a display label for the derived column in the select list and specify the derived column by means of a select number in the ORDER BY clause. The select list of the Projection clause must include any column or expression that the ORDER BY clause specifies, however, if any of the following is true: v The query includes the DISTINCT, UNIQUE, or UNION operator. v The query includes the INTO TEMP table clause. v The distributed query accesses a remote database whose server requires every column or expression in the ORDER BY clause to also appear in the select list of the Projection clause. v An expression in the ORDER BY clause includes a display label for a column substring. (See the next section, Ordering by a Substring on page 2-516.) The next query selects one column from the orders table and sorts the results by the value of another column. (With Extended Parallel Server, order_date must also appear in the Projection clause.) By default, the rows are listed in ascending order.
SELECT ship_date FROM orders ORDER BY order_date
You can order by an aggregate expression only if the query also has a GROUP BY clause. This query declares the display label maxwgt for an aggregate in the ORDER BY clause:
SELECT ship_charge, MAX(ship_weight) maxwgt FROM orders GROUP BY ship_charge ORDER BY maxwgt
If the current processing locale defines a localized collation, then NCHAR and NVARCHAR column values are sorted in that localized order. 3 3 3 In Dynamic Server, no column in the ORDER BY clause can be a collection type, but a query whose result set defines a collection-derived table can include the ORDER BY clause. For an example, see Collection-Derived Table on page 5-5. You might improve the performance of some non-PDQ queries that use the ORDER BY clause to sort a large set of rows if you increase the setting of the DS_NONPDQ_QUERY_MEM configuration parameter.
Ordering by a Substring
You can order by a substring instead of by the entire length of a character, BYTE, or TEXT column, or of an expression returning a character string. The database server uses the substring to sort the result set. Define the substring by specifying integer subscripts (the first and last parameters), representing the starting and ending byte positions of the substring within the column value. The following SELECT statement queries the customer table and specifies a column substring in the ORDER BY column. This instructs the database server to sort the query results by the portion of the lname column contained in the sixth through ninth bytes of the column value.
SELECT * from customee ORDER BY lname[6,9]
2-516
SELECT
Assume that the value of lname in one row of the customer table is Greenburg. Because of the column substring in the ORDER BY clause, the database server determines the sort position of this row by using the value burg, rather than the complete column value Greenburg. When ordering by an expression, you can specify substrings only for expressions that return a character data type. If you specify a column substring in the ORDER BY clause, the column must have one of the following data types: BYTE, CHAR, NCHAR, NVARCHAR, TEXT, or VARCHAR. Dynamic Server can also support LVARCHAR column substrings in the ORDER BY clause, if the column is in a database of the local database server. For information on the GLS aspects of using column substrings in the ORDER BY clause, see the IBM Informix GLS User's Guide.
Nested Ordering
If you list more than one column in the ORDER BY clause, your query is ordered by a nested sort. The first level of sort is based on the first column; the second column determines the second level of sort. The following example of a nested sort selects all the rows in the cust_calls table and orders them by call_code and by call_dtime within call_code:
SELECT * FROM cust_calls ORDER BY call_code, call_dtime
Select numbers are required in the ORDER BY clause when SELECT statements are joined by the UNION or UNION ALL keywords, or when compatible columns in the same position have different names.
2-517
SELECT
Ordering by Rowids
You can specify the ROWID keyword in the ORDER BY clause. This specifies the rowid column, a hidden column in nonfragmented tables and in fragmented tables that were created with the WITH ROWIDS clause. The rowid column contains a unique internal record number that is associated with a row in a table. (It is recommended, however, that you utilize primary keys as your access method, rather than exploiting the rowid column.) The ORDER BY clause cannot specify the rowid column if the table from which you are selecting is a fragmented table that has no rowid column. In Extended Parallel Server, you cannot specify the ROWID keyword in the ORDER BY clause unless you also included ROWID in the Projection clause. In Dynamic Server, you do not need to include the ROWID keyword in the Projection clause when you specify ROWID in the ORDER BY clause. For further information about rowid values and how to use the rowid column in column expressions, see WITH ROWIDS Option (IDS) on page 2-21 and Using Rowids (IDS) on page 4-48.
Element column
Restrictions
Syntax
Must be in the FROM clause table, but it does not need Identifier to be in the select list of the Projection clause on page 5-22
2-518
SELECT
The FOR UPDATE keyword notifies the database server that updating is possible, causing it to use more stringent locking than it would with a select cursor. You cannot modify data through a cursor without this clause. You can specify which columns can be updated. After you declare a cursor for a SELECT ... FOR UPDATE statement, you can update or delete the currently selected row using an UPDATE or DELETE statement with the WHERE CURRENT OF clause. The words CURRENT OF refer to the row that was most recently fetched; they replace the usual test expressions in the WHERE clause. To update rows with a specific value, your program might contain statements such as those in the following example:
EXEC SQL char char EXEC . . . BEGIN DECLARE SECTION; fname[ 16]; lname[ 16]; SQL END DECLARE SECTION;
EXEC SQL connect to stores_demo; /* select statement being prepared contains a for update clause */ EXEC SQL prepare x from select fname, lname from customer for update; EXEC SQL declare xc cursor for x; for (;;) { EXEC SQL fetch xc into $fname, $lname; if (strncmp(SQLSTATE, 00, 2) != 0) break; printf("%d %s %s\n",cnum, fname, lname ); if (cnum == 999) --update rows with 999 customer_num EXEC SQL update customer set fname = rosey where current of xc; } EXEC SQL close xc; EXEC SQL disconnect current;
A SELECT...FOR UPDATE statement, like an update cursor, allows you to perform updates that are not possible with the UPDATE statement alone, because both the decision to update and the values of the new data items can be based on the original contents of the row. The UPDATE statement cannot query the table that is being updated.
2-519
SELECT
v Syntax restrictions on a SELECT statement that uses a FOR READ ONLY clause
The solution is to include the FOR READ ONLY clause in your select cursors. The read-only cursor that this clause specifies is compatible with the read-only mode of the database. For example, the following SELECT FOR READ ONLY statement against the customer_ansi table succeeds:
EXEC SQL declare ansi_read cursor for select * from customer_ansi for read only;
DBAccess executes all SELECT statements with select cursors, so you must specify FOR READ ONLY in all queries that access data in a read-only ANSI-compliant database. The FOR READ ONLY clause causes DBAccess to declare the cursor for the SELECT statement as a read-only cursor. For more information on level-0 backups, see your IBM Informix Backup and Restore Guide. For more information on select cursors, read-only cursors, and update cursors, see DECLARE on page 2-260. For more information on the express mode of the HPL of Dynamic Server, see the IBM Informix High-Performance Loader User's Guide.
2-520
SELECT
INTO TEMP table WITH NO LOG (1) RAW STATIC STANDARD OPERATIONAL (1) SCRATCH table EXTERNAL table USING USING Options table (2) Storage Options Lock Mode Options (3)
USING Options:
(5) ( (4) Table Options , Table Options DATAFILES Clause (4) )
Notes: 1 2 3 4 5 Extended Parallel Server only See Storage Options on page 2-188 See LOCK MODE Options on page 2-203 See Table Options on page 2-523 See DATAFILES Clause on page 2-102
Description Table to receive the results of the query Restrictions Must be unique among names of tables, views, and synonyms that you own in the current database Syntax Database Object Name on page 5-17
Element table
You must have the Connect privilege on a database to create a temporary or external table in that database. The name of a temporary table need not be unique among the names of temporary tables of other users. Column names in the temporary or external table must be specified in the Projection clause, where you must supply a display label for all expressions that are not simple column expressions. The display label becomes the column name in the temporary or external table. If you do not declare a display label for a column expression, the table uses the column name from the select list of the Projection clause. The following INTO TEMP example creates the pushdate table with two columns, customer_num and slowdate:
SELECT customer_num, call_dtime + 5 UNITS DAY slowdate FROM cust_calls INTO TEMP pushdate
2-521
SELECT
This release of Dynamic Server continues to process the remaining statements of a multistatement prepared object after encountering the SQLNOTFOUND value of 100. You can maintain the legacy behavior, however, of not executing the remaining prepared statements by setting the IFX_MULTIPREPSTMT environment variable to 1.
2-522
SELECT
has no display label, the table uses the column name. If there are duplicate labels or column names in the select list, an error will be returned.
Notes: 1
Element field_delimiter record_delimiter
Description Character to separate fields. Default is pipe ( | ) character Character to separate records
The following table describes the keywords that apply to unloading data. If you want to specify additional table options in the external-table description for the purpose of reloading the table later, see Table Options on page 2-103.
Chapter 2. SQL Statements
2-523
SELECT
In the SELECT ... INTO EXTERNAL statement, you can specify all table options that are discussed in the CREATE EXTERNAL TABLE statement except the fixed-format option. You can use the INTO EXTERNAL clause when the format type of the created data file is either delimited text (if you use the DELIMITED keyword) or text in Informix internal data format (if you use the INFORMIX keyword). You cannot use it for a fixed-format unload. Keyword CODESET DELIMITER ESCAPE Effect Specifies the type of code set. Options are EBCDIC or ASCII. Specifies the character that separates fields in a delimited text file Directs the database server to recognize ASCII special characters embedded in ASCII-text-based data files If you do not specify ESCAPE when you load data, the database server does not check the character fields in text data files for embedded special characters. If you do not specify ESCAPE when you unload data, the database server does not create embedded hexadecimal characters in text fields. FORMAT RECORDEND Specifies the format of the data in the data files Specifies the character that separates records in a delimited text file
Specifying Delimiters: If you do not set the RECORDEND environment variable, the default value for record_delimiter is the newline character (CTRL-J). If you use a non-printing character as a delimiter, encode it as the octal representation of the ASCII character. For example, \006 can represent CTRL-F. For more information on external tables, see CREATE EXTERNAL TABLE (XPS) on page 2-98.
UNION Operator
Place the UNION operator between two SELECT statements to combine the queries into a single query. You can string several SELECT statements together
2-524
SELECT
using the UNION operator. Corresponding items do not need to have the same name. Omitting the ALL keyword excludes duplicate rows.
If you want to remove duplicates, use the UNION operator without the keyword ALL in the query. In the preceding example, if the combination 101 B were returned in both SELECT statements, a UNION operator would cause the combination to be listed once. (If you want to remove duplicates within each SELECT statement, use the DISTINCT keyword in the Projection clause, as described in Projection Clause on page 2-481.)
UNION in Subqueries
You can use the UNION and UNION ALL operators in subqueries of SELECT statements within the WHERE clause, the FROM clause, and in collection subqueries. In this release of Dynamic Server, however, subqueries that include UNION or UNION ALL are not supported in the following contexts: v In the definition of a view v In the event or in the Action clause of a trigger v With the FOR UPDATE clause or with an Update cursor
Chapter 2. SQL Statements
2-525
SELECT
v In a distributed query (accessing tables outside the local database) For more information about collection subqueries, see Collection Subquery on page 4-3. For more information about the FOR UPDATE clause, see FOR UPDATE Clause on page 2-518. In a combined subquery, the database server can resolve a column name only within the scope of its qualifying table reference. The following query, for example, returns an error:
SELECT * FROM t1 WHERE EXISTS (SELECT a FROM t2 UNION SELECT b FROM t3 WHERE t3.c IN (SELECT t4.x FROM t4 WHERE t4.4 = t2.z))
Here t2.z in the innermost subquery cannot be resolved, because z occurs outside the scope of reference of the table reference t2. Only column references that belong to t4, t3, or t1 can be resolved in the innermost subquery. The scope of a table reference extends downwards through subqueries, but not across the UNION operator to sibling SELECT statements.
Related Information
3 3 3 Because the SELECT statement is the Q in SQL, most features of the database server directly or indirectly support SELECT operations, which are central to relational and object-relational databases. (So this section is not comprehensive.) For task-oriented discussions of the SELECT statement, see the IBM Informix Guide to SQL: Tutorial. For a discussion of the GLS aspects of the SELECT statement, see the IBM Informix GLS User's Guide. For information on how to access row and collection values with ESQL/C host variables, see the discussion of complex data types in the IBM Informix ESQL/C Programmer's Manual.
2-526
SET ALL_MUTABLES
Use the SET ALL_MUTABLES statement to specify the modifiability of all mutable environment variables in a user session. Only Extended Parallel Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
SET ALL_MUTABLES TO MUTABLE IMMUTABLE
Usage
By default, users can reset the values of some environment variables during a session to support their usage requirements. This capability can sometimes conflict, however, with the resource usage policy set by the DBA. By changing the mutability property of environment variables, a DBA can have more accurate control over resource allocation during a user session. v SET ALL_MUTABLES TO MUTABLE causes all environment settings that have the mutability property to be modifiable during a user session. v SET ALL_MUTABLES TO IMMUTABLE prevents modification of any environment settings that have the mutability property during a user session. Settings of the following features frequently have the most impact on resource utilization within a user session, and have the mutability property defined: v BOUND_IMPL_PDQ v CLIENT_TZ v COMPUTE_QUOTA v DEFAULT TABLE_SPACE v DEFAULT TABLE_TYPE v IMPLICIT_PDQ v MAXSCAN v PDQPRIORITY v TEMP_TAB_EXT_SIZE v TEMP_TAB_NEXT_SIZE v TMPSPACE_LIMIT Users can reset the mutability of specific features by using the corresponding SET statement. For example, PDQPRIORITY can be made immutable by the SET PDQPRIORITY IMMUTABLE statement. SET DEFAULT TABLE_TYPE MUTABLE can change the default table type. SET ALL_MUTABLES can reset the mutability all of the above features in a single statement. Typically, the mutability property is set by the DBA in a sysdbopen procedure to control the usage of resources. For a discussion of setting the mutability property in sysdbopen procedure, see IBM Informix Performance Guide. The DBA can modify all variables, irrespective of their mutability setting. The command
onstat -g mut session_id
Chapter 2. SQL Statements
2-527
SET ALL_MUTABLES
displays the current mutability settings for the specified session. The command
onmode -q
can change the mutability of a specified variable associated with an existing session. See the IBM Informix Administrator's Reference for details of how to use the onstat and onmode utilities. By default, all environment variables are MUTABLE.
Related Information
Related statements: SET Default Table Space, SET Default Table Type, SET ENVIRONMENT, and SET PDQPRIORITY See also the section Setting Environment Variables in SYSTEM Commands on page 3-40, which describes how to use the SYSTEM statement of SPL to change the settings of environment variables for the current user session.
2-528
SET AUTOFREE
Use the SET AUTOFREE statement to instruct the database server to enable or disable a memory-management feature that can free the memory allocated for a cursor automatically, as soon as the cursor is closed. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. You can use this statement only with ESQL/C.
Syntax
ENABLED SET AUTOFREE DISABLED FOR cursor_id cursor_id_var
Description Name of a cursor for which Autofree is to be reset Host variable that holds the value of cursor_id
Restrictions Must already be declared within the program Must store a cursor_id already declared in the program
Usage
When the Autofree feature is enabled for a cursor, and the cursor is subsequently closed, you do not need to explicitly use the FREE statement to release the memory that the database server allocated for the cursor. If you issue SET AUTOFREE but specify no option, the default is ENABLED. The SET AUTOFREE statement that enables the Autofree feature must appear before the OPEN statement that opens a cursor. The SET AUTOFREE statement does not affect the memory allocated to a cursor that is already open. After a cursor is Autofree enabled, you cannot open that cursor a second time.
The next example disables the Autofree feature for all subsequent cursors:
EXEC SQL set autofree disabled;
2-529
SET AUTOFREE
In the following example, the first statement enables the Autofree feature for all cursors, while the second statement disables the Autofree feature for the cursor named x1:
EXEC SQL set autofree enabled; EXEC SQL set autofree disabled for x1;
Here the x1 cursor must have been declared but not yet opened.
When the database server closes the sel_curs2 cursor, it automatically performs the equivalent of the following FREE statements:
FREE sel_curs2; FREE sel_stmt;
Because memory for the sel_stmt statement is freed automatically, you cannot declare a new cursor on it unless you prepare the statement again.
Related Information
Related statements: CLOSE, DECLARE, FETCH, FREE, OPEN, and PREPARE For more information on the Autofree feature, see the IBM Informix ESQL/C Programmer's Manual.
2-530
SET COLLATION
Use the SET COLLATION statement to specify a new collating order for the session, superseding the default collation of the DB_LOCALE environment variable setting. SET NO COLLATION restores the default collation. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. You can use this statement with ESQL/C.
Syntax
SET COLLATION locale NO COLLATION
Element locale
Restrictions Must be the name of a locale that the database server can access
Usage
As the IBM Informix GLS User's Guide explains, the database server uses locale files to specify the character set, the collating order, and other conventions of some natural language to display and manipulate character strings and other data values. The collating order of the database locale is the sequential order in which the database server sorts character strings. If you set no value for DB_LOCALE, the default locale, based on United States English, is en_us.8859-1 for UNIX, or Code Page 1252 for Windows systems. Otherwise, the database server uses the DB_LOCALE setting as its locale. SET COLLATION overrides the collating order of DB_LOCALE at runtime for all database servers previously accessed in the same session. The new collating order remains in effect for the rest of the session, or until you issue another SET COLLATION statement. Other sessions are not affected, but database objects that you created with a non-default collation will use whatever collating order was in effect at their time of their creation. By default, the collating order is the code-set order, but some locales also support a locale-specific order. In most contexts, only NCHAR and NVARCHAR data values can be sorted according to a locale-specific collating order.
If the next action of a database server in this session sorted NCHAR or NVARCHAR values, this would follow the German collating order. Suppose that, in the same session, the following SET NO COLLATION statement restores the DB_LOCALE setting for the collating order:
EXEC SQL set no collation;
2-531
SET COLLATION
After SET NO COLLATION executes, subsequent collation in the same session is based on the DB_LOCALE setting. Any database objects that you created using the German collating order, however, such as check constraints, indexes, prepared objects, triggers, or UDRs, will continue to apply German collation to NCHAR and NVARCHAR data types.
2-532
SET COLLATION
When synonyms are created for remote tables or views, the participating databases must have the same collating order. Existing synonyms, however, can be used in other databases that support SET COLLATION and the collating order of the synonym, regardless of the DB_LOCALE setting. Check constraints, cursors, prepared objects, triggers, and SPL routines that sort NCHAR or NVARCHAR values use the collation that was in effect at the time of their creation, if this is different from the DB_LOCALE setting. The effect on performance is sensitive to how many different collations are used when creating database objects that sort in a localized order.
Related Information
For information on locales, see the IBM Informix GLS User's Guide.
2-533
SET CONNECTION
Use the SET CONNECTION statement to reestablish a connection between an application and a database environment and to make the connection current. You can also use this statement with the DORMANT option to put the current connection in a dormant state. Use this statement with ESQL/C.
Syntax
SET CONNECTION connection (1) connection_var (1) Database Environment DEFAULT (1) CURRENT DORMANT
Notes: 1 2
Element connection connection_var
Description Name of the initial connection that the CONNECT statement made Host variable that contains the value of connection
Usage
You can use SET CONNECTION to make a dormant connection current or to make the current connection dormant. SET CONNECTION is not valid as a prepared statement.
2-534
SET CONNECTION
A dormant connection has a connection context associated with it. When an application makes a dormant connection current, it reestablishes that connection to a database environment and restores its connection context. (For more information on connection context, see the CONNECT statement on page 2-75.) Reestablishing a connection is comparable to establishing the initial connection, except that it typically avoids authenticating the permissions for the user again, and it avoids reallocating resources associated with the initial connection. For example, the application does not need to reprepare any statements that have previously been prepared in the connection, nor does it need to redeclare any cursors.
The SET CONNECTION statement with the DORMANT option generates an error if you specify a connection that is already dormant. For example, if connection con1 is current and connection con2 is dormant, the following SET CONNECTION statement returns an error message:
SET CONNECTION con2 DORMANT
2-535
SET CONNECTION
thread_2() { /* Make con2 an active connection */ EXEC SQL connect to db2 as con2; /*Do insert on table t2 in db2*/ EXEC SQL insert into table t2 values(10); /* make con2 available to other threads */ EXEC SQL set connection con2 dormant; }
If a connection to a database environment was initiated using the CONNECT . . . WITH CONCURRENT TRANSACTION statement, any thread that subsequently connects to that database environment can use an ongoing transaction. In addition, if an open cursor is associated with such a connection, the cursor remains open when the connection is made dormant. Threads within a thread-safe ESQL/C application can use the same cursor by making the associated connection current, even though only one thread can use the connection at any given time.
If a connection to a database server was assigned a connection name, however, you must use the connection name to reconnect to the database server. An error is returned if you use a database environment rather than the connection name when a connection name exists.
DEFAULT Option
The DEFAULT option specifies the default connection for a SET CONNECTION statement. The default connection is one of the following connections: v An explicit default connection (a connection established with the CONNECT TO DEFAULT statement) v An implicit default connection (any connection established with the DATABASE or CREATE DATABASE statements) Use SET CONNECTION without a DORMANT option to reestablish the default connection, or with that option to make the default connection dormant. For more information, see DEFAULT Option on page 2-76 and The Implicit Connection with DATABASE Statements on page 2-76.
CURRENT Keyword
Use the CURRENT keyword with the DORMANT option of the SET CONNECTION statement as a shorthand form of identifying the current connection. The CURRENT keyword replaces the current connection name. If the current connection is con1, the following two statements are equivalent:
2-536
SET CONNECTION
SET CONNECTION con1 DORMANT; SET CONNECTION CURRENT DORMANT;
Related Information
Related statements: CONNECT, DISCONNECT, and DATABASE For a discussion of the SET CONNECTION statement and thread-safe applications, see the IBM Informix ESQL/C Programmer's Manual.
2-537
SET CONSTRAINTS
Use the SET CONSTRAINTS statements to specify how some or all of the constraints on a table are processed. Constraint-mode options include these: v Whether constraints are checked at the statement level (IMMEDIATE) or at the transaction level (DEFERRED) v Whether to enable or disable constraints v Whether to change the filtering mode of constraints.
Syntax
SET CONSTRAINTS ALL , constraint IMMEDIATE DEFERRED (1) ENABLED DISABLED WITH ERROR WITHOUT ERROR WITH ERROR IMMEDIATE DEFERRED
FILTERING ENABLED DISABLED WITH ERROR WITHOUT ERROR FILTERING WITH ERROR
Notes: 1
Element constraint table Description Constraint whose mode is to be reset Table whose constraint mode is to be reset for all constraints
Usage
The SET CONSTRAINTS keywords begin the SET Transaction Mode statement, which is described in SET Transaction Mode on page 2-606. Only Dynamic Server supports the SET Transaction Mode statement, which is an extension to the ANSI/ISO standard for SQL. The SET CONSTRAINTS keywords can also begin a special case of the SET Database Object Mode statement, which is an extension to the ANSI/ISO standard for SQL. The SET Database Object Mode statement can also enable or disable a trigger or index, or change the filtering mode of a unique index. For the complete syntax and semantics of the SET INDEX statement, see SET Database Object Mode on page 2-539.
2-538
Syntax
(1) SET Object-List Format (2) Table Format
Usage
In the context of this statement, database object has the restricted meaning of an index, a trigger, or a constraint, rather than the more general meaning of this term that the description of the 5-17 segment defines in Chapter 5. The scope of the SET Database Object Mode statement is restricted to constraints, indexes, or triggers in the local database to which the session is currently connected. After you change the mode of an object, the new mode is in effect for all sessions of that database, and persists until another SET Database Object Mode statement changes it again, or until the object is dropped from the database. Only two object modes are available for triggers and for indexes that allow duplicate values: v enabled v disabled For constraints and for unique indexes, you can also specify two additional modes: v filtering without integrity-violation errors v filtering with integrity-violation errors At any given time, an object must be in exactly one of these modes. These modes, which are sometimes called object states, are described in the section Definitions of Database Object Modes on page 2-541. The sysobjstate system catalog table lists all of the constraint, index, and trigger objects in the database, and the current mode of each object. For information on the sysobjstate table, see the IBM Informix Guide to SQL: Reference.
2-539
Object-List Format
Use the object-list format to change the mode for one or more constraint, index, or trigger. Object-List Format:
, (1) CONSTRAINTS , INDEXES constraint Modes for Constraints and Unique Indexes (1) index Modes for Constraints and Unique Indexes (2) Modes for Triggers and Duplicate Indexes , TRIGGERS trigger (2) Modes for Triggers and Duplicate Indexes
Name of a constraint whose Must be a local constraint, and all constraints in the mode is to be set list must be defined on the same table Name of an index whose mode is to be set Name of a trigger whose mode is to be set Must be a local index, and all indexes in the list must be defined on the same table Must be a local trigger, and all triggers in the list must be defined on the same table or view
For example, to change the mode of the unique index unq_ssn on the cust_subset table to filtering, enter the following statement:
SET INDEXES unq_ssn FILTERING
You can also use the object-list format to change the mode for a list of constraints, indexes, or triggers that are defined on the same table. Assume that four triggers are defined on the cust_subset table: insert_trig, update_trig, delete_trig, and execute_trig. Also assume that all four triggers are enabled. To disable all triggers except execute_trig, enter this statement:
SET TRIGGERS insert_trig, update_trig, delete_trig DISABLED
If my_trig is a disabled INSTEAD OF trigger on a view, the following statement enables that trigger:
SET TRIGGERS my_trig ENABLED
2-540
Table Format
Use the table format to change the mode of all database objects of a specified type that have been defined on the same table. Table Format:
, CONSTRAINTS INDEXES TRIGGERS FOR table
(1) Modes for Constraints and Unique Indexes (2) Modes for Triggers and Duplicate Indexes
Notes: 1 2
Element table Description Table on which objects are defined
Must be a local table. Objects defined on a temporary table Database Object cannot be set to disabled or filtering modes. Name, p. 5-17
In table format, you can change the modes of more than one database object type with a single statement. For example, this enables all constraints, indexes, and triggers that are defined on the cust_subset table:
SET CONSTRAINTS, INDEXES, TRIGGERS FOR cust_subset ENABLED
If you specify no mode for a constraint in a CREATE TABLE, ALTER TABLE, or SET Database Object Mode statement, the constraint is enabled by default. If you do not specify the mode for an index in the CREATE INDEX or SET Database Object Mode statement, the index is enabled by default.
2-541
Enabled Mode
Constraints, indexes, and triggers are enabled by default. The CREATE TABLE, ALTER TABLE, CREATE INDEX, and CREATE TRIGGER statements create database objects in enabled mode, unless you specify another mode. When a database object is enabled, the database server recognizes the existence of the database object and takes the database object into consideration while it executes an INSERT, DELETE, or UPDATE statement (or for Select triggers, a SELECT statement). Thus, an enabled constraint is enforced, an enabled index updated, and an enabled trigger is executed when the trigger event takes place. When you enable constraints and unique indexes, if a violating row exists, the data manipulation statement fails (that is, no rows are changed) and the database server returns an error message.
Disabled Mode
When a database object is disabled, the database server ignores it during the execution of an INSERT, DELETE, SELECT, or UPDATE statement. A disabled constraint is not enforced, a disabled index is not updated, and a disabled trigger is not executed when the trigger event takes place. When you disable constraints and unique indexes, any data manipulation statement that violates the restriction of the constraint or unique index succeeds (that is, the target row is changed), and the database server does not return an error message. You can use the disabled mode to add a new constraint or new unique index to an existing table, even if some rows in the table do not satisfy the new integrity specification. Disabling can also be efficient in LOAD operations. For information on adding a constraint, see Adding a Constraint That Existing Rows Violate (IDS) on page 2-56 in the ALTER TABLE statement. For information on adding a unique index, see Adding a Unique Index When Duplicate Values Exist in the Column in the CREATE INDEX statement.
Filtering Mode
When a constraint or unique index is in filtering mode, the INSERT, DELETE, or UPDATE statement succeeds, but the database server enforces the constraint or the unique-index requirement by writing any failed rows to the violations table associated with the target table. Diagnostic information about the constraint violation or unique-index violation is written to the diagnostics table associated with the target table. In data manipulation operations, filtering mode has the following specific effects on INSERT, UPDATE, and DELETE statements: v A constraint violation during an INSERT statement causes the database server to make a copy of the nonconforming record and write it to the violations table. The database server does not write the nonconforming record to the target table. If the INSERT statement is not a singleton INSERT, the rest of the insert operation proceeds with the next record. v A constraint violation or unique-index violation during an UPDATE statement causes the database server to make a copy of the existing record that was to be updated and write it to the violations table. The database server also makes a copy of the new record and writes it to the violations table, but the actual record
2-542
2-543
If you specify no mode for an index or for a trigger when you create it or in a subsequent SET Database Object Mode statement, the object is enabled by default.
Related Information
Related statements: ALTER TABLE, CREATE TABLE, CREATE INDEX, CREATE TRIGGER, START VIOLATIONS TABLE, and STOP VIOLATIONS TABLE For a discussion of object modes and violation detection and examples that show how database object modes work when users execute data manipulation statements on target tables or add new constraints and indexes to target tables, see the IBM Informix Guide to SQL: Tutorial. For information on the system catalog tables associated with the SET Database Object Mode statement, see the descriptions of the sysobjstate and sysviolations tables in the IBM Informix Guide to SQL: Reference.
2-544
SET DATASKIP
Use the SET DATASKIP statement to control whether the database server skips a dbspace that is unavailable during the processing of a transaction. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
SET DATASKIP ON , dbspace OFF DEFAULT
Element dbspace
Usage
SET DATASKIP allows you to reset at runtime the Dataskip feature, which controls whether the database server skips a dbspace that is unavailable (for example, due to a media failure) in the course of processing a transaction. In ESQL/C, the warning flag sqlca.sqlwarn.sqlwarn6 is set to W if a dbspace is skipped. See also the IBM Informix ESQL/C Programmer's Manual. In Dynamic Server, this statement applies only to tables that are fragmented across dbspaces or partitions. It does not apply to blobspaces nor to sbspaces. Specifying SET DATASKIP ON without including a dbspace instructs the database server to skip any dbspaces in the fragmentation list that are unavailable. You can use the onstat -d or -D options to determine whether a dbspace is down. When you specify SET DATASKIP ON dbspace, you are instructing the database server to skip the specified dbspace if it is unavailable. If you specify SET DATASKIP OFF, the Dataskip feature is disabled. If you specify SET DATASKIP DEFAULT, the database server uses the setting for the Dataskip feature from the ONCONFIG file.
2-545
SET DATASKIP
v Inserts When you try to insert records in a expression-based fragmentation strategy and the dbspace is unavailable, an error is returned. When you try to insert records in a round-robin fragment-based strategy, and a dbspace is down, the database server inserts the rows into any available dbspace. When no dbspace is available, an error is returned. v Indexing When you perform updates that affect the index, such as when you insert or delete rows, or update an indexed column, the index must be available. When you try to create an index, the dbspace you want to use must be available. v Serial keys The first fragment is used to store the current serial-key value internally. This is not visible to you except when the first fragment becomes unavailable and a new serial key value is required, which can happen during INSERT statements.
Related Information
Related statement: SET ALL_MUTABLES For additional information about the Dataskip feature, see your IBM Informix Administrator's Guide.
2-546
Syntax
SET DEBUG FILE TO filename filename_var expression WITH APPEND
Description Expression that returns a filename Pathname of the file that contains the output of the TRACE statement Host variable storing filename string
Restrictions Must be a valid filename See Using the WITH APPEND Option on page 2-547 Must be a character type
filename_var
Language specific
Usage
This statement specifies the file to which the database server writes the output from the TRACE statement in the SPL routine Each time the TRACE statement is executed, the trace information is added to this output file.
2-547
Related Information
Related statement: TRACE For a task-oriented discussion of SPL routines, see the IBM Informix Guide to SQL: Tutorial.
2-548
Syntax
SET TEMP TABLE_SPACE TO dbs_list DEFAULT IMMUTABLE MUTABLE
Element dbs_list
Usage
When the CREATE TABLE statement includes no fragmentation clause, the database server uses the dbspace of the current database as the default storage location. You can use the SET TABLE_SPACE statements to change the default to another dbslice or to a list of one or more dbspaces. Similarly, you can use the SET TEMP TABLE_SPACE statement to change the default storage location for CREATE Temporary TABLE statements that do not include the Storage Options clause. This statement also sets the default dbspace for SELECT statements that include the INTO Table clause. These defaults persist for the rest of the current session, or until the next SET Default Table Space statement. Specifying the TO DEFAULT option restores the default behavior. The DBA can use the MUTABLE or IMMUTABLE keywords to enable (with MUTABLE) or prevent (with IMMUTABLE) changes by users to the default table space. The DBA typically sets the default table space in a sysdbopen procedure, and sets it to IMMUTABLE if the default should not be changed in a user session.
Related Information
Related statements: CREATE TABLE, CREATE Temporary TABLE, SET ALL_MUTABLES, SET Default Table Type.
2-549
Syntax
SET TABLE_TYPE TO STANDARD OPERATIONAL RAW STATIC IMMUTABLE MUTABLE TEMP TABLE_TYPE TO SCRATCH DEFAULT IMMUTABLE MUTABLE
Usage
If CREATE TABLE specifies no table type, the default table type is STANDARD. The SET TABLE_TYPE statement can change this default for subsequent CREATE TABLE statements in the current session. Similarly, you can use the SET TEMP TABLE_TYPE to change the default temporary table type. These statements have no effect on tables for which you explicitly specify a table type in the statement that creates the new table or temporary table. Because the CREATE Temporary TABLE statement requires an explicit table type, the SET TEMP TABLE_TYPE statement only affects SQL operations that create a temporary implicitly, such as in executing join operations, SELECT statements with the GROUP BY or ORDER BY clause, and index builds. The effect of SET Default Table Type persists until the end of the session, or until you issue another SET Default Table Type statement to specify a new default table type. The SET TABLE_TYPE TO STANDARD statement and the SET TEMP TABLE_TYPE TO DEFAULT statements restore the default behavior. The DBA can use the keyword MUTABLE or IMMUTABLE to enable (with MUTABLE) or to prevent (with IMMUTABLE) changes by users to the default table type during a user session. The DBA typically sets the default table type in a sysdbopen procedure and specifies IMMUTABLE if it should not be changed during a user session. Although the scope of these statements is the current session, they can be used to have a database-wide effect. The next example shows how to do this by using SPL routines to establish a default table type at connect time:
CREATE SET SET SET ... PROCEDURE public.sysdbopen() TABLE_TYPE TO RAW; TEMP TABLE_TYPE TO SCRATCH; TABLE_SPACE TO other_tables;
2-550
Related Information
Related statements: CREATE TABLE, CREATE TEMP TABLE, SET ALL_MUTABLES, SET Default Table Space. For more information on table types that can be specified in the CREATE TABLE statement, see CREATE TABLE on page 2-171. For more information about temporary tables, see CREATE Temporary TABLE on page 2-209.
2-551
SET DEFERRED_PREPARE
Use the SET DEFERRED_PREPARE statement to control whether a client process postpones sending a PREPARE statement to the database server until the OPEN or EXECUTE statement is sent. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. You can use this statement only with ESQL/C.
Syntax
ENABLED SET DEFERRED_PREPARE DISABLED
Usage
By default, the SET DEFERRED_PREPARE statement causes the application program to delay sending the PREPARE statement to the database server until the OPEN or EXECUTE statement is executed. In effect, the PREPARE statement is bundled with the other statement so that one round-trip of messages, instead of two, is sent between the client and the server. This Deferred-Prepare feature can affect the following series of Dynamic SQL statement: v PREPARE, DECLARE, OPEN statement blocks that operate with the FETCH or PUT statements v PREPARE followed by the EXECUTE or EXECUTE IMMEDIATE statement You can specify ENABLED or DISABLED options for SET DEFERRED_PREPARE. If you specify no option, the default is ENABLED. The following example enables the Deferred-Prepare feature by default:
EXEC SQL set deferred_prepare;
The ENABLED option enables the Deferred-Prepare feature within the application. The following example explicitly specifies the ENABLED option:
EXEC SQL set deferred_prepare enabled;
After an application issues SET DEFERRED_PREPARE ENABLED, the Deferred-Prepare feature is enabled for subsequent PREPARE statements in the application. The application then exhibits the following behavior: v The sequence PREPARE, DECLARE, OPEN sends the PREPARE statement to the database server with the OPEN statement. If the prepared statement has syntax errors, the database server does not return error messages to the application until the application declares a cursor for the prepared statement and opens the cursor. v The sequence PREPARE, EXECUTE sends the PREPARE statement to the database server with the EXECUTE statement. If a prepared statement contains syntax errors, the database server does not return error messages to the application until the application attempts to execute the prepared statement. If Deferred-Prepare is enabled in a PREPARE, DECLARE, OPEN statement block that contains a DESCRIBE statement, the DESCRIBE statement must follow the OPEN statement rather than the PREPARE statement. If the DESCRIBE follows PREPARE, the DESCRIBE statement results in an error.
2-552
SET DEFERRED_PREPARE
Use the DISABLED option to disable the Deferred-Prepare feature within the application. The following example specifies the DISABLED option:
EXEC SQL set deferred_prepare disabled;
If you specify the DISABLED option, the application sends each PREPARE statement to the database server when the PREPARE statement is executed.
Related Information
Related statements: DECLARE, DESCRIBE, EXECUTE, OPEN, and PREPARE For a task-oriented discussion of the PREPARE statement and dynamic SQL, see the IBM Informix Guide to SQL: Tutorial. For more information about concepts that relate to the SET DEFERRED_PREPARE statement, see the IBM Informix ESQL/C Programmer's Manual.
2-553
SET DESCRIPTOR
The SET DESCRIPTOR statement sets values in a system-descriptor area (SDA). Use this statement with ESQL/C.
Syntax
SET DESCRIPTOR descriptor_var descriptor
COUNT=
VALUE
item_num_var item_num
Item Descriptor
Notes: 1
Element descriptor descriptor_var item_num Description
String that identifies the SDA to which System-descriptor area (SDA) values are assigned must be previously allocated Host variable that stores descriptor Same restrictions as descriptor
Unsigned integer that specifies ordinal 0 < item_num (number of item position of an item descriptor in the descriptors specified when SDA SDA was allocated) Host variable that stores item_num Unsigned integer that specifies how many items the SDA describes Host variable that stores total_items Same restrictions as item_num Same restrictions as item_num Same restrictions as total_items
Usage
The SET DESCRIPTOR statement can be used after you have described SELECT, EXECUTE FUNCTION, EXECUTE PROCEDURE, ALLOCATE DESCRIPTOR, or INSERT statements with the DESCRIBE ... USING SQL DESCRIPTOR statement. SET DESCRIPTOR can assign values to a system-descriptor area in these cases: v To set the COUNT field of a system-descriptor area to match the number of items for which you are providing descriptions in the system-descriptor area v To set the item descriptor for each value for which you are providing descriptions in the system-descriptor area v To modify the contents of an item-descriptor field If an error occurs during the assignment to any identified system-descriptor fields, the contents of all identified fields are set to 0 or NULL, depending on the data type of the variable.
2-554
SET DESCRIPTOR
than you are using, you need to set the COUNT field to the number of items that you are actually using. The following example shows a fragment of an ESQL/C program:
EXEC SQL BEGIN DECLARE SECTION; int count; EXEC SQL END DECLARE SECTION; EXEC SQL allocate descriptor desc_100; /*allocates for 100 items*/ count = 2; EXEC SQL set descriptor desc_100 count = :count;
Item Descriptor
Use the Item Descriptor portion of the SET DESCRIPTOR statement to set value for an individual field in a system-descriptor area. Item Descriptor:
TYPE LENGTH PRECISION SCALE NULLABLE INDICATOR ITYPE ILENGTH DATA IDATA = = literal_int_var literal_int
(1) Literal Number (2) Literal DATETIME (3) Literal INTERVAL (4) Quoted String input_var (4) NAME (5) EXTYPENAME (5) EXTYPEOWNERNAME (5) SOURCEID SOURCETYPE EXTYPEID EXTYPELENGTH EXTYPEOWNERLENGTH = literal_int_var literal_int = Quoted String input_var
2-555
SET DESCRIPTOR
3 4 5
Element input_var literal_int literal_int_var Description Host variable storing data for the specified item descriptor field Integer value ( > 0 ) assigned to the specified item descriptor field Variable having literal_int value
Must be appropriate for the specified Language-specific field Restrictions depend on the keyword to the left of = symbol Same as for literal_int Literal Number, p. 4-137 Language-specific
For information on codes that are valid for the TYPE or ITYPE fields and their meanings, see Setting the TYPE or ITYPE Field on page 2-556. For the restrictions that apply to other field types, see the individual headings for field types under Using the VALUE Clause on page 2-555.
SQL Data Type CHAR SMALLINT INTEGER FLOAT SMALLFLOAT DECIMAL SERIAL DATE
SQL Data Type MONEY DATETIME BYTE TEXT VARCHAR INTERVAL NCHAR NVARCHAR
The following table lists integer values that represent additional data types available with Dynamic Server.
SQL Data Type INT8 SERIAL8 SET MULTISET LIST ROW Integer Value 17 18 19 20 21 22 SQL Data Type COLLECTION Varying-length OPAQUE type Fixed-length OPAQUE type LVARCHAR (client-side only) BOOLEAN Integer Value 23 40 41 43 45
The same TYPE constants can also appear in the syscolumns.coltype column in the system catalog; see IBM Informix Guide to SQL: Reference.
2-556
SET DESCRIPTOR
For code that is easier to maintain, use the predefined constants for these SQL data types instead of their actual integer values. These constants are defined in the $INFORMIX/incl/public/sqltypes.h header file. You cannot, however, use the actual constant name in the SET DESCRIPTOR statement. Instead, assign the constant to an integer host variable and specify the host variable in the SET DESCRIPTOR statement file. The following example shows how you can set the TYPE field in ESQL/C:
main() { EXEC SQL BEGIN DECLARE SECTION; int itemno, type; EXEC SQL END DECLARE SECTION; ... EXEC SQL allocate descriptor desc1 with max 5; ... type = SQLINT; itemno = 3; EXEC SQL set descriptor desc1 value :itemno type = :type; }
This information is identical for ITYPE. Use ITYPE when you create a dynamic program that does not comply with the X/Open standard. Compiling Without the -xopen Option: If you compile without the -xopen option, the normal Informix SQL code is assigned for TYPE. You must be careful not to mix normal and X/Open modes, because errors can result. For example, if a data type is not defined under X/Open mode, but is defined under normal mode, executing a SET DESCRIPTOR statement can result in an error. Setting the TYPE Field in X/Open Programs: In X/Open mode, you must use the X/Open set of integer codes for the data type in the TYPE field. If you use the ILENGTH, IDATA, or ITYPE fields in a SET DESCRIPTOR statement, a warning message appears. The warning indicates that these fields are not standard X/Open fields for a system-descriptor area. For code that is easier to maintain, use the predefined constants for these X/Open SQL data types instead of their actual integer value. These constants are defined in the $INFORMIX/incl/public/sqlxtype.h header file. Using DECIMAL or MONEY Data Types: If you set the TYPE field for a DECIMAL or MONEY data type, and you want to use a scale or precision other than the default values, set the SCALE and PRECISION fields. You do not need to set the LENGTHfield for a DECIMAL or MONEY item; the LENGTH field is set accordingly from the SCALE and PRECISION fields. Using DATETIME or INTERVAL Data Types: If you set the TYPE field for a DATETIME or INTERVAL value, the DATA field can be a DATETIME or INTERVAL literal or a character string. If you use a character string, the LENGTH field must be the encoded qualifier value. To determine the encoded qualifiers for a DATETIME or INTERVAL character string, use the datetime and interval macros in the datetime.h header file. If you set DATA to a host variable of DATETIME or INTERVAL, you do not need to set LENGTH explicitly to the encoded qualifier integer.
2-557
SET DESCRIPTOR
2-558
SET DESCRIPTOR
Related Information
Related statements: ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, DECLARE, DESCRIBE, EXECUTE, FETCH, GET DESCRIPTOR, OPEN, PREPARE, and PUT For more information on system-descriptor areas, refer to the IBM Informix ESQL/C Programmer's Manual.
2-559
Syntax
SET ENCRYPTION PASSWORD password WITH HINT hint
Description
Restrictions
String that GETHINT returns from (0 byte) hint (32 bytes). Do not an encrypted argument include the password in the hint. Password (or a multi-word phrase) (6 bytes) password (120 bytes). for data encryption Do not specify your login password.
Usage
The SET ENCRYPTION PASSWORD statement declares a password to support data confidentiality through built-in functions that use the Triple-DES or AES algorithms for encryption and decryption. These functions enable the database to store sensitive data in an encrypted format that prevents anyone who cannot provide the secret password from viewing, copying, or modifying encrypted data. The password is not stored as plain text in the database, and is not accessible to the DBA. This security feature is independent of the Trusted Facility feature. Important: By default, communication between client systems and Dynamic Server is in plain text. Unless the database is accessible only by a secure network, the DBA must enable the encryption communication support module (ENCCSM) to provide data encryption between the database server and any client system. Otherwise, an attacker might read the password and use it to access encrypted data. If the network is not secure, all of the database servers in a distributed query need ENCCSM enabled, so that the password is not transmitted as plain text. For information about how to enable a communication support module (CSM), see your IBM Informix Administrator's Guide. Operations on encrypted data tend to be slower than corresponding operations on plain text data, but use of this feature has no effect on unencrypted data. The SET ENCRYPTION PASSWORD statements can be prepared, and EXECUTE IMMEDIATE can process a prepared SET ENCRYPTION PASSWORD statement.
2-560
If the encrypted VARCHAR (or NVARCHAR) value is longer than the 255 byte maximum size for those data types, the encryption function returns a CHAR (or NCHAR) value of sufficient size to store the encrypted value. DECRYPT_BINARY and DECRYPT_CHAR both return the same value from encrypted CHAR, NCHAR, VARCHAR, NVARCHAR, or LVARCHAR values. No built-in encryption or decryption functions support BYTE or TEXT data types, but you can use BLOB data types to encrypt very large strings. Warning: If the declared size of a database column in which you intend to store encrypted data is smaller than the encrypted data length, truncation occurs when you insert the encrypted data into the column. The truncated data cannot subsequently be decrypted, because the data length indicated in the header of the encrypted string does not match what the column stores. To avoid truncation, make sure that any column that stored encrypted strings has sufficient length. (See the cross-reference in the next paragraph for details of how to calculate the length of an encrypted string.) Besides the unencrypted data length, the storage required for encrypted data depends on the encoding format, on whether you specify a hint, and on the block size of the encryption function. For a formula to estimate the encrypted size, see Calculating storage requirements for encrypted data on page 4-82.
2-561
Levels of Encryption
You can use SET ENCRYPTION PASSWORD with encryption and decryption functions to support these granularities of encryption in the database. v Column-Level Encryption: All values in a given column of a database table are encrypted using the same password, the same encryption algorithm, and the same encryption mode. (In this case, you can save disk space by storing the hint outside the encrypted column, rather than repeating it in every row.) v Cell-Level Encryption: Values of a given column in different rows of the same database table are encrypted using different passwords, or different encryption algorithms, or different encryption modes. This technique is sometimes necessary to protect personal data. (Row-column level encryption and set-column level encryption are both synonyms for cell-level encryption.)
Protecting Passwords
Passwords and hints that you declare with SET ENCRYPTION PASSWORD are not stored as plain text in any table of the system catalog, which also maintains no record of which columns or tables contain encrypted data. To prevent other users from accessing the plain text of encrypted data or of a password, however, you must avoid actions that might compromise the secrecy of a password: v Do not create a functional index using a decryption function. (This would store plain-text data in the database, defeating the purpose of encryption.) v On a network that is not secure, always work with encrypted data, or use session encryption, because the SQL communication between client and server sends passwords, hints, and the data to be encrypted as plain text. v Do not store passwords in a trigger or in a UDR that exposes the password to the public. v Do not set the session password prior to creating any view, trigger, procedure, or UDR. Set the session password only when you use the object. Otherwise, the password might be visible in the schema to other users, and queries executed by other users might return unencrypted data. Output from the SET EXPLAIN statement always displays the password and hint parameters as XXXXX, rather than displaying actual password or hint values.
Related Information
For more information about built-in functions for encrypting and decrypting data, see Encryption and Decryption Functions on page 4-79. For information on setting the environment variable INFORMIXCONCSMCFG, refer to the IBM Informix Guide to SQL: Reference.
2-562
SET ENVIRONMENT
The SET ENVIRONMENT statement can specify options at runtime that affect subsequent queries submitted within the same routine. This is an extension to the ANSI/ISO standard for SQL. Only Dynamic Server supports the OPTCOMPIND option. All of the other options are valid only with Extended Parallel Server.
Syntax
SET ENVIRONMENT
(1) BOUND_IMPL_PDQ COMPUTE_QUOTA CLIENT_TZ IMPLICIT_PDQ MAXSCAN TMPSPACE_LIMIT (1) TEMP_TAB_EXT_SIZE TEMP_TAB_NEXT_SIZE (2) OPTCOMPIND DEFAULT integer (1) MUTABLE IMMUTABLE ON OFF value DEFAULT MUTABLE IMMUTABLE
Notes: 1 2
Element value integer Description Value to set for the specified environment option, which this statement also enables The extent size for system-generated temporary tables, in kilobytes (XPS). The code 0, 1, or 2 to prioritize nested-loop joins as an optimizer strategy (IDS).
Usage
SET ENVIRONMENT specifies environment options that manage resource use by the routine in which the statement is executed. For example, on Extended Parallel Server, the SET ENVIRONMENT IMPLICIT_PDQ ON statement enables automatic PDQPRIORITY allocation for queries submitted in the routine. The OFF keyword disables the specified option. The ON keyword enables the specified option. The DEFAULT keyword sets the specified option to its default value. The MUTABLE keyword makes the specified option modifiable during a user session.
2-563
SET ENVIRONMENT
The IMMUTABLE keyword prevents modification of the specified option during a user session. The arguments that follow the option name depend on the syntax of the option. The option name and its ON, OFF, and DEFAULT keywords are not quoted and are not case sensitive. All other arguments must be enclosed between single ( ) or double ( ) quotation marks. If a quoted string is a valid argument for an environment option, the argument is case sensitive. If you enter an undefined option name or an invalid value for a defined option, no error is returned. Undefined options are ignored, but they might produce unexpected results, if you intended some effect that a misspelled or unsupported option name cannot produce. The SET ENVIRONMENT statement can enable only the environment options that are described in sections that follow. For information about the performance implications of the SET ENVIRONMENT options, refer to the IBM Informix Performance Guide.
If you set both IMPLICIT_PDQ and BOUND_IMPL_PDQ, then the explicit PDQPRIORITY value determines the upper limit of memory that can be allocated to a query. If PDQPRIORITY is specified as a range, the database server grants memory within the range specified. For detailed information, see the IBM Informix Performance Guide.
Description Number of hours in timezone offset from Greenwich Mean Time (GMT) Additional minutes in timezone offset from Greenwich Mean Time (GMT)
Restrictions Must be an integer in the range -14 < hour <14 Must be a 2-digit integer in the range 00 to 59 inclusive
2-564
SET ENVIRONMENT
A positive ( + ) offset implies a time zone whose location is west of zero longitude ( = GMT). A negative offset implies a time zone east of zero longitude. The following statement specifies an offset of five hours and fifteen minutes:
SET ENVIRONMENT CLIENT_TZ +5:15
If the statement in this statement executed successfully, then subsequent calls to the CURRENT function in the same session return a time-of-day value that is 5 hours and 15 minutes greater than the default value with no offset. If a CLIENT_TZ value has been previously specified, the keyword ON tells the database server to use that value. The CLIENT_TZ value that you specify in the SET ENVIRONMENT statement temporarily overrides any CLIENT_TZ setting of the IBM_XPS_PARAMS environment variable for the duration of the current session. The onstat -g ses command can display the current CLIENT_TZ setting. In a distributed transaction that you initiate from Extended Parallel Server, but in which remote Dynamic Server instances evaluate CURRENT or TODAY values, the CLIENT_TZ setting is ignored by Dynamic Server.
To require the database server to use explicit PDQPRIORITY settings as the upper bound and optional lower bound of memory that it grants to a query, set the BOUND_IMPL_PDQ environment option.
2-565
SET ENVIRONMENT
might want to reduce the number of scan threads on a coserver if the GROUP, JOIN, and other operators above the scan are not producing rows quickly enough to keep the default number of scan threads busy. For some queries, you might want to increase the number of scan threads on each coserver. For example, if each coserver has three CPU VPs, the database server can create nine scan threads on each coserver. To increase the number to four threads for each CPU VP, execute the following statement:
SET ENVIRONMENT MAXSCAN 12
MAXSCAN is automatically set to 1 if the COMPUTE_QUOTA environment option is enabled or if the isolation level is set to Cursor Stability and the database server can use a pipe operator.
To limit queries to 50 percent of DS_TOTAL_TMPSPACE on each coserver, execute the following statement:
SET ENVIRONMENT TMPSPACE_LIMIT "50";
2-566
SET ENVIRONMENT
This example sets the first extent size of a generated temporary table to 64 and the next extent size to 128 kilobytes. The minimum value of these options is four times the page size on your system. If you specify a size below the minimum, the server will default the page size to four pages. For flex inserts, the server will default to 32 pages or 128 kilobytes. The maximum value for TEMP_TAB_EXT_SIZE and TEMP_TAB_NEXT_SIZE is the maximum value of a chunk size. Use the DEFAULT keyword to reset the values of these environments options back to the system defaults. Important: The database server calculates the extent sizes of temporary tables that it creates for hash tables and sorts. The SET ENVIRONMENT statement has no effect on extent sizes for these tables.
Use the DEFAULT keyword to restore the system default value, as described in the OPTCOMPIND section of the IBM Informix Guide to SQL: Reference. For performance implications of the OPTCOMPIND option, see your IBM Informix Performance Guide.
Related Information
Related statements: SET ALL_MUTABLES, SET PDQPRIORITY
Chapter 2. SQL Statements
2-567
SET EXPLAIN
Use the SET EXPLAIN statement to display the query plan of optimizer, an estimate of the number of rows returned, and the relative cost of the query.
Syntax
SET EXPLAIN OFF ON AVOID_EXECUTE (1) FILE TO filename filename_var expr WITH APPEND
Notes: 1
Element expr filename Description Expression that returns a filename specification Path and filename of the file to receive the output. For the default, see Location of the Output File on page 2-547. Host variable that stores filename
Must conform to operating- system Quoted String, rules. If an existing file, see Using p. 4-142 the WITH APPEND Option on page 2-547. Must be a character data type Language specific
filename_var
Usage
Output from a SET EXPLAIN ON statement is directed to the appropriate file until you issue a SET EXPLAIN OFF statement or until the program ends. If you do not enter a SET EXPLAIN statement, then the default behavior is OFF, and the database server does not generate measurements for queries. The SET EXPLAIN statement executes during the database server optimization phase, which occurs when you initiate a query. For queries that are associated with a cursor, if the query is prepared and does not have host variables, optimization occurs when you prepare it. Otherwise, optimization occurs when you open the cursor. The SET EXPLAIN statement provides various measurements of the work involved in performing a query. Option ON Effect Generates measurements for each subsequent query and writes the results to an output file in the current directory. If the file already exists, new output is appended to the existing file. Prevents a SELECT, INSERT, UPDATE, or DELETE statement from executing while the database server prints the query plan to an output file Terminates activity of the SET EXPLAIN statement,
AVOID_EXECUTE
OFF
2-568
SET EXPLAIN
so that measurements for subsequent queries are no longer generated or written to the output file FILE TO Generates measurements for each subsequent query and allows you to specify the location for the explain output file. If the file already exists, new output overwrites the contents of the file unless you use the WITH APPEND option.
for any select, delete, update or insert query operations. From ESQL, the sqlwarn.sqlwarn7 character is set to W. Use the SET EXPLAIN ON or the SET EXPLAIN OFF statement to turn off the AVOID_EXECUTE option. The SET EXPLAIN ON statement turns off the AVOID_EXECUTE option but continues to generate a query plan and writes the results to an output file. If you issue the SET EXPLAIN ON AVOID_EXECUTE statement in an SPL routine, the SPL routine and any DDL statements still execute, but the DML statements inside the SPL routine do not execute. The database server prints the query plan of the SPL routine to an output file. To turn off this option, you must execute the SET EXPLAIN ON or the SET EXPLAIN OFF statement outside the SPL routine. If you execute the SET EXPLAIN ON AVOID_EXECUTE statement before you execute an SPL routine, the DML statements inside the SPL routine do not execute, and the database server does not print a query plan of the SPL routine to an output file. Nonvariant functions in a query are still evaluated when AVOID_EXECUTE is in effect, because the database server calculates these functions before optimization. For example, the func( ) function is evaluated, even though the following SELECT statement is not executed:
SELECT * FROM orders WHERE func(10) > 5
For other performance implications of the AVOID_EXECUTE option, see your IBM Informix Performance Guide. If you execute the SET EXPLAIN ON AVOID_EXECUTE statement before you open a cursor in an ESQL/C program, each FETCH operation returns the message that the row was not found. If you execute SET EXPLAIN ON AVOID_EXECUTE after an ESQL/C program opens a cursor, however, this statement has no effect on the cursor, which continues to return rows.
2-569
SET EXPLAIN
2-570
SET EXPLAIN
Term Query Significance Displays the executed query and indicates whether SET OPTIMIZATION was set to HIGH or LOW. If you SET OPTIMIZATION to LOW, the output displays the following uppercase string as the first line: QUERY:{LOW} If you SET OPTIMIZATION to HIGH, the output of SET EXPLAIN displays the following uppercase string as the first line: QUERY: Directives followed Lists the directives set for the query If the syntax for a directive is incorrect, the query is processed without the directive. In that case, the output shows DIRECTIVES NOT FOLLOWED in addition to DIRECTIVES FOLLOWED. For more information on the directives specified after this term, see the Optimizer Directives on page 5-34 or SET OPTIMIZATION on page 2-584. Estimated cost An estimate of the amount of work for the query The optimizer uses an estimate to compare the cost of one path with another. The estimate is a number the optimizer assigns to the selected access method. This number does not translate directly into time and cannot be used to compare different queries. It can be used, however, to compare changes made for the same query. When data distributions are used, a query with a higher estimate generally takes longer to run than one with a smaller estimate. In the case of a query and a subquery, two estimated cost figures are returned; the query figure also contains the subquery cost. The subquery cost is shown only so you can see the cost that is associated with the subquery. Estimated number of rows returned Numbered list An estimate of the number of rows to be returned This number is based on information in the system catalog tables. The order in which tables are accessed, followed by the access method used (index path or sequential scan) When a query involves table inheritance, all the tables are listed under the supertable in the order in which they were accessed. Index keys The columns used as filters or indexes; the column name used for the index path or filter is indicated. The notation (Key Only) indicates that all the desired columns are part of the index key, so a key-only read of the index could be substituted for a read of the actual table. The Lower Index Filter shows the key value where the index read begins. If the filter condition contains more than one value, an Upper Index Filter is shown for the key value where the index read stops. Join method When the query involves a join between two tables, the join method the optimizer used (Nested Loop or Dynamic Hash) is shown at the bottom of the output for that query. When the query involves a dynamic join of two tables, if the output contains the words Build Outer, the hash table is built on the first table listed (called the build table). If the words Build Outer do not appear, the hash table is built on the second table listed.
2-571
SET EXPLAIN
If the query uses a collating order other than the default for the DB_LOCALE setting, then the DB_LOCALE setting and the name of the other locale that is the basis for the collation in the query (as specified by the SET COLLATION statement) are both included in the output file. Similarly, if an index is not used because of its collation, the output file indicates this.
Related Information
SET OPTIMIZATION, UPDATE STATISTICS For a description of the EXPLAIN and AVOID_EXECUTE optimizer directives, see Explain-Mode Directives on page 5-40. For discussions of SET EXPLAIN and of analyzing the output of the optimizer, see your IBM Informix Performance Guide.
2-572
SET INDEX
Use the SET INDEX statement to specify that one or more fragments of an index be resident in shared memory as long as possible. Only Extended Parallel Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
SET INDEX index , ( dbspace ) MEMORY_RESIDENT NON_RESIDENT
Description Dbspace in which to store the fragment Index for which to change residency state
Usage
This statement was formerly supported by Dynamic Server, but it is ignored in current releases. Beginning with Version 9.40, Dynamic Server determines the residency status of indexes and tables automatically. The SET INDEX statement is a special case of the SET Residency statement. The SET Residency statement can also specify how long a table fragment remains resident in shared memory. For the complete syntax and semantics of the SET INDEX statement, see SET Residency on page 2-590.
2-573
SET INDEXES
Use the SET INDEXES statement to enable or disable an index, or to change the filtering mode of a unique index. Only Dynamic Server supports this statement, which is an extension to the ANSI/ISO standard for SQL. Do not confuse the SET INDEXES statement with the SET INDEX statement.
Syntax
, SET INDEXES index FOR table ENABLED DISABLED FILTERING WITHOUT ERROR WITH ERROR
Description Table whose indexes are all to be enabled, disabled, or changed in their filtering mode Index to be enabled, disabled, or changed in its filtering mode
Usage
The SET INDEXES statement is a special case of the SET Database Object Mode statement. The SET Database Object Mode statement can also enable or disable a trigger or constraint or change the filtering mode of a constraint. For the complete syntax and semantics of the SET INDEXES statement, see SET Database Object Mode on page 2-539.
2-574
SET ISOLATION
Use the SET ISOLATION statement to define the degree of concurrency among processes that attempt to access the same rows simultaneously. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
SET ISOLATION TO REPEATABLE READ COMMITTED READ CURSOR STABILITY DIRTY READ WITH WARNING
Usage
The SET ISOLATION statement is an Informix extension to the ANSI SQL-92 standard. The SET ISOLATION statement can change the enduring isolation level for the session. If you want to set isolation levels through an ANSI-compliant statement, use the SET TRANSACTION statement instead. For a comparison of these two statements, see SET TRANSACTION on page 2-602. The TO keyword is optional, and has no effect. SET ISOLATION provides the same functionality as the ISO/ANSI-compliant SET TRANSACTION statement for isolation levels of DIRTY READ (called UNCOMMITTED in SET TRANSACTION), COMMITTED READ, and REPEATABLE READ (called SERIALIZABLE in SET TRANSACTION). The database isolation_level affects read concurrency when rows are retrieved from the database. The isolation level specifies the phenomena that can occur during execution of concurrent SQL transactions. The following phenomena are possible: v Dirty Read. SQL transaction T1 modifies a row. SQL transaction T2 then reads that row before T1 performs a COMMIT. If T1 then performs a ROLLBACK, T2 will have read a row that was never committed, and therefore can be considered never to have existed. v Non-Repeatable Read. SQL transaction T1 reads a row. SQL transaction T2 then modifies or deletes that row and performs a COMMIT. If T1 then attempts to reread that row, T1 might receive the modified value or discover that the row has been deleted. v Phantom Row. SQL transaction T1 reads the set of rows N that satisfy some search condition. SQL transaction T2 then executes SQL statements that generate one or more new rows that satisfy the search condition used by SQL transaction T1. If T1 then repeats the original read with the same search condition, T1 receives a different set of rows. The database server uses shared locks to support different levels of isolation among processes attempting to access data. The update or delete process always acquires an exclusive lock on the row that is being modified. The level of isolation does not interfere with rows that you are
Chapter 2. SQL Statements
2-575
SET ISOLATION
updating or deleting. If another process attempts to update or delete rows that you are reading with an isolation level of Repeatable Read, that process is denied access to those rows. In ESQL/C, cursors that are open when SET ISOLATION executes might or might not use the new isolation level when rows are retrieved. Any isolation level that was set from the time the cursor was opened until the application fetches a row might be in effect. The database server might have read rows into internal buffers and internal temporary tables using the isolation level that was in effect at that time. To ensure consistency and reproducible results, close any open cursors before you execute the SET ISOLATION statement. You can issue the SET ISOLATION statement from a client computer only after a database is opened.
2-576
SET ISOLATION
The default level remains in effect until you issue a SET ISOLATION statement. After a SET ISOLATION statement executes, the new isolation level remains in effect until one of the following events occurs: v You enter another SET ISOLATION statement. v You open another database that has a default isolation level different from the level that your last SET ISOLATION statement specified. v The program ends.
2-577
SET ISOLATION
In a database with the isolation level set to Dirty Read, Committed Read, or Cursor Stability, the database server places an update lock on a fetched row of a SELECT ... FOR UPDATE statement. When you turn on the RETAIN UPDATE LOCKS option, the database server retains the update lock until the end of the transaction rather than releasing it at the next subsequent FETCH or when the cursor is closed. This option prevents other users from placing an exclusive lock on the updated row before the current user reaches the end of the transaction. You can use this option to achieve the same locking effects but avoid the overhead of dummy updates or the repeatable read isolation level. You can turn this option on or off at any time during the current session. You can turn the option off by resetting the isolation level without using the RETAIN UPDATE LOCKS keywords. For more information on update locks, see Locking Considerations on page 2-639. Turning the Option Off In the Middle of a Transaction: If you set the RETAIN UPDATE LOCKS option to OFF after a transaction has begun, but before the transaction has been committed or rolled back, several update locks might still exist. Switching OFF the feature does not directly release any update lock. When you turn this option off, the database server reverts to normal behavior for the three isolation levels. That is, a FETCH statement releases the update lock placed on a row by the immediately preceding FETCH statement, and a closed cursor releases the update lock on the current row. Update locks placed by earlier FETCH statements are not released unless multiple update cursors are present within the same transaction. In this case, a subsequent FETCH could also release older update locks of other cursors.
2-578
SET ISOLATION
completed, but the scroll cursor with hold remains open beyond the end of the transaction. You can modify released rows as soon as the transaction ends, but retrieved data in the temporary table might be inconsistent with the actual data. Attention: Do not use nonlogging tables within a transaction. If you need to use a nonlogging table within a transaction, either set the isolation level to Repeatable Read or else lock the table in Exclusive mode to prevent concurrency problems.
Related Information
Related statements: CREATE DATABASE, SET LOCK MODE, and SET TRANSACTION For a discussion of how to set the database isolation level, see the IBM Informix Guide to SQL: Tutorial.
2-579
Syntax
SET LOCK MODE TO NOT WAIT WAIT seconds
Element seconds
Description
Restrictions
Syntax
Maximum number of seconds that a process waits for a Valid only if shorter than Literal Number, p. lock to be released before issuing an error system default 4-137
Usage
This statement can direct the response of the database server in the following ways when a process tries to access a locked row or table. Lock Mode NOT WAIT Effect Database server ends the operation immediately and returns an error code. This condition is the default. Database server suspends the process until the lock releases. Database server suspends the process until the lock releases or until the waiting period ends. If the lock remains after the waiting period, the operation ends and an error code is returned.
In the following example, the user specifies that if the process requests a locked row, the operation should end immediately and an error code should be returned:
SET LOCK MODE TO NOT WAIT
In the following example, the user specifies that the process should be suspended until the lock is released:
SET LOCK MODE TO WAIT
The next example sets an upper limit of 17 seconds on the length of any wait:
SET LOCK MODE TO WAIT 17
WAIT Clause
The WAIT clause causes the database server to suspend the process until the lock is released or until a specified number of seconds have passed without the lock being released. The database server protects against the possibility of a deadlock when you request the WAIT option. Before the database server suspends a process, it checks whether suspending the process could create a deadlock. If the database server
2-580
Related Information
Related statements: LOCK TABLE, SET ISOLATION, SET TRANSACTION, and UNLOCK TABLE For a description of the two distinct meanings of the term lock mode in this manual, see the section Locking Granularity on page 2-415. For a discussion of how to set the lock mode behavior of the database server, see the IBM Informix Guide to SQL: Tutorial.
2-581
SET LOG
Use the SET LOG statement to change your database logging mode from buffered transaction logging to unbuffered transaction logging or vice versa. This statement is an extension to the ANSI/ISO standard for SQL. Unlike most extensions, the SET LOG statement is not valid in an ANSI-compliant database.
Syntax
SET BUFFERED LOG
Usage
You activate transaction logging when you create a database or add logging to an existing database. These transaction logs can be buffered or unbuffered. Buffered logging is a type of logging that holds transactions in a memory buffer until the buffer is full, regardless of when the transaction is committed or rolled back. The database server provides this option to speed up operations by reducing the number of disk writes. Attention: You gain a marginal increase in efficiency with buffered logging, but you incur some risk. In the event of a system failure, the database server cannot recover any completed transactions in the memory buffer that were not written to disk. The SET LOG statement in the following example changes the transaction logging mode to buffered logging:
SET BUFFERED LOG
Unbuffered logging is a type of logging that does not hold transactions in a memory buffer. As soon as a transaction ends, the database server writes the transaction to disk. If a system failure occurs when you are using unbuffered logging, you recover all completed transactions, but not those still in the buffer. The default condition for transaction logs is unbuffered logging. The SET LOG statement in the following example changes the transaction logging mode to unbuffered logging:
SET LOG
The SET LOG statement redefines the mode for the current session only. The default mode, which the database administrator sets with the ondblog utility, remains unchanged. The buffering option does not affect retrievals from external tables. For distributed queries, a database with logging can retrieve only from databases with logging, but it makes no difference whether the databases use buffered or unbuffered logging. An ANSI-compliant database cannot use buffered logging.
2-582
SET LOG
You cannot change the logging mode of ANSI-compliant databases. If you created a database with the WITH LOG MODE ANSI keywords, you cannot later use the SET LOG statement to change the logging mode to buffered or unbuffered transaction logging.
Related Information
Related statement: CREATE DATABASE
2-583
SET OPTIMIZATION
Use the SET OPTIMIZATION statement to specify how much time the optimizer spends developing a query plan or specifying optimization goals. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
SET OPTIMIZATION HIGH LOW (1) FIRST_ROWS ALL_ROWS
Usage
You can execute a SET OPTIMIZATION statement at any time. The specified optimization level carries across databases on the current database server. The option that you specify remains in effect until you issue another SET OPTIMIZATION statement or until the program ends. The default database server optimization level for the amount of time that the query optimizer spends determining the query plan is HIGH. On Dynamic Server, the default optimization goal is ALL_ROWS. Although you can set only one option at a time, you can issue two SET OPTIMIZATION statements: one that specifies the time the optimizer spends to determine the query plan and one that specifies the optimization goal of the query.
2-584
SET OPTIMIZATION
This option directs the optimizer to choose the query plan that returns the first result record as soon as possible. v ALL_ROWS This option directs the optimizer to choose the query plan that returns all the records as quickly as possible. You can also specify the optimization goal of a specific query with the optimization-goal directive. For more information, see Optimizer Directives on page 5-34.
Examples
The following example shows optimization across a network. The central database (on the midstate database server) is to have LOW optimization; the western database (on the rockies database server) is to have HIGH optimization.
CONNECT TO central@midstate; SET OPTIMIZATION LOW; SELECT * FROM customer; CLOSE DATABASE; CONNECT TO western@rockies; SET OPTIMIZATION HIGH; SELECT * FROM customer; CLOSE DATABASE; CONNECT TO wyoming@rockies; SELECT * FROM customer;
The wyoming database is to have HIGH optimization because it resides on the same database server as the western database. The code does not need to re-specify the optimization level for the wyoming database because the wyoming database resides on the rockies database server like the western database. The following example directs the Dynamic Server optimizer to use the most time to determine a query plan, and to then return the first rows of the result as soon as possible:
SET OPTIMIZATION LOW; SET OPTIMIZATION FIRST_ROWS; SELECT lname, fname, bonus FROM sales_emp, sales WHERE sales.empid = sales_emp.empid AND bonus > 5,000 ORDER BY bonus DESC
Related Information
Related statements: SET EXPLAIN, SET ENVIRONMENT, and UPDATE STATISTICS For information on other methods by which you can alter the query plan of the Dynamic Server optimizer, see Optimizer Directives on page 5-34. For more information on how to optimize queries, see your IBM Informix Performance Guide.
2-585
SET PDQPRIORITY
The SET PDQPRIORITY statement allows an application to set the query priority level dynamically within a routine. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
SET PDQPRIORITY DEFAULT LOW (1) OFF HIGH resources (2) LOW low HIGH high MUTABLE IMMUTABLE
Notes: 1 2
Element high low resources Description Integer value that specifies the desired resource allocation Integer that specifies the minimum resource allocation Integer that specifies the query priority level and the percent of resources to process the query
Usage
The SET PDQPRIORITY statement overrides the PDQPRIORITY environment variable (but has lower precedence than the MAX_PDQPRIORITY configuration parameter). The scope of SET PDQPRIORITY is local to the routine, and does not affect other routines within the same session. The SET PDQPRIORITY statement is not supported in SPL routines. In Dynamic Server, set PDQ priority to a value less than the quotient of 100 divided by the maximum number of prepared statements. For example, if two prepared statements are active, you should set the PDQ priority to less than 50. In Extended Parallel Server, you can use SET PDQPRIORITY to set PDQ priority at runtime to a value greater than 0 when you need more memory for operations such as sorts, forming groups, and index builds. For guidelines on which values to use, see your IBM Informix Performance Guide. For example, assume that the DBA sets the MAX_PDQPRIORITY parameter to 50. Then a user enters the following SET PDQPRIORITY statement to set the query priority level to 80 percent of resources:
2-586
SET PDQPRIORITY
SET PDQPRIORITY 80
When it processes the query, the database server uses the MAX_PDQPRIORITY value to factor the query priority level set by the user. The database server silently processes the query with a priority level of 40. This priority level represents 50 percent of the 80 percent of resources that the user specifies. The following keywords are supported by the SET PDQPRIORITY statement. Keyword DEFAULT LOW Effect Uses the setting of the PDQPRIORITY environment variable Data values are fetched from fragmented tables in parallel. (In Dynamic Server, when you specify LOW, the database server uses no other forms of parallelism.) PDQ is turned off (Dynamic Server only). The database server uses no parallelism. OFF is the default if you use neither the PDQPRIORITY environment variable nor the SET PDQPRIORITY statement. The database server determines an appropriate PDQPRIORITY value, based on factors that include the number of available processors, the fragmentation of the tables being queried, the complexity of the query, and others. IBM reserves the right to change the performance behavior of queries when HIGH is specified in future releases. PDQPRIORITY can be changed in a user session by the user. PDQPRIORITY cannot be changed in a user session.
OFF
HIGH
MUTABLE IMMUTABLE
The DBA typically specifies the MUTABLE or IMMUTABLE setting, which only Extended Parallel Server supports, in a sysdbopen( ) procedure. Only Extended Parallel Server supports the HIGH and LOW keywords in the same statement, each followed by a value, to specify a range of resource levels. For more information, see Using a Range of Values on page 2-588.
2-587
SET PDQPRIORITY
For Dynamic Server, the following statements are equivalent. The first statement uses the keyword LOW to establish a low query-priority level. The second uses a value of 1 in the resources parameter to establish a low query-priority level.
SET PDQPRIORITY LOW; SET PDQPRIORITY 1;
Related Information
For information about configuration parameters and about the Resource Grant Manager, see your IBM Informix Administrator's Guide and your IBM Informix Performance Guide. For information about the PDQPRIORITY environment variable, see the IBM Informix Guide to SQL: Reference.
2-588
Syntax
SET PLOAD FILE TO filename WITH APPEND
Element filename
Description Name of the log file. If you specify no filename, then log information is written to /dev/null.
Syntax Platformdependent
Usage
The WITH APPEND option allows you to append new log information to the existing log file. Each time a session closes, the log file for that session also closes. If you issue more than one SET PLOAD FILE statement within a session, each new statement closes a previously opened log file and opens a new log file. If you invoke a SET PLOAD FILE statement with a simple filename on a local database, the output file is located in your current directory. If your current database is on a remote database server, then the output file is located in your home directory on the remote database server, on the coserver where the initial connection was made. If you provide a full pathname for the file, it is placed in the directory and file specified on the remote server.
Related Information
Related statement: CREATE EXTERNAL TABLE (XPS)
2-589
SET Residency
Use the SET Residency statement to specify that one or more fragments of a table or index be resident in shared memory as long as possible. Only Extended Parallel Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
SET TABLE INDEX name , ( dbspace ) MEMORY_RESIDENT NON_RESIDENT
Description The dbspace where fragment resides Table or index for which the residency state will be changed
Usage
This statement was formerly supported by Dynamic Server, but it is ignored in current releases. Beginning with Version 9.40, Dynamic Server determines the residency status of indexes and tables automatically. The SET Residency statement allows you to specify the tables, indexes, and data fragments that you want to remain in the buffer as long as possible. When a free buffer is requested, pages that are declared with the MEMORY_RESIDENT keyword are considered last for page replacement. The default state is nonresident. The residency state is persistent while the database server is up. That is, each time the database server is started, you must specify the database objects that you want to remain in shared memory. After a table, index, or data fragment is set to MEMORY_RESIDENT, the residency state remains in effect until one of the following events occurs: v You use SET Residency to set the database object to NON_RESIDENT. v The database object is dropped. v The database server is taken offline. Only user informix can set or change the residency state of a database object.
2-590
SET Residency
Examples
The next example shows how to set the residency status of an entire table:
SET TABLE tab1 MEMORY_RESIDENT
For fragmented tables or indexes, you can specify residency for individual fragments as the following example shows:
SET INDEX index1 (dbspace1, dbspace2) MEMORY_RESIDENT; SET TABLE tab1 (dbspace1) NON_RESIDENT
This example specifies that the tab1 fragment in dbspace1 is not to remain in shared memory while the index1 fragments in dbspace1 and dbspace2 are to remain in shared memory as long as possible.
Related Information
Related statement: ALTER FRAGMENT For information on how to monitor the residency status of tables, indexes, and fragments, refer to your IBM Informix Administrator's Guide.
2-591
SET ROLE
Use the SET ROLE statement to enable the privileges of a role. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
SET ROLE role role NULL NONE DEFAULT
Element role
Restrictions Must already exist in the database. If enclosed between quotation marks, role is case sensitive.
Usage
Any user who is granted a role can enable the role by using the SET ROLE statement. You can only enable one role at a time. If you execute the SET ROLE statement after a role is already set, the new role replaces the old role. All users are assigned their DEFAULT role as granted by the DBA using GRANT DEFAULT ROLE statement for the database instance. If no default role exists for the user in the current database, role NULL or NONE is assigned by default. In this context, NULL and NONE are synonyms. Roles NULL and NONE can have no privileges. To set your role to NULL or NONE disables your current role. When you use SET ROLE to enable a role, you gain the privileges of the role, in addition to the privileges of PUBLIC and your own privileges. If a role is granted to another role that has been assigned to you, you gain the privileges of both roles, in addition to any privileges of PUBLIC and your own privileges. After SET ROLE executes successfully, the specified role remains effective until the current database is closed or the user executes another SET ROLE statement. Only the user, however, not the role, retains ownership of any database objects, such as tables, that were created during the session. A role is in scope only within the current database. You cannot use privileges that you acquire from a role to access data in another database. For example, if you have privileges from a role in the database named acctg, and you execute a distributed query over the databases named acctg and inventory, your query cannot access the data in the inventory database unless you were also granted appropriate privileges in the inventory database. If your database supports explicit transactions, you must issue the SET ROLE statement outside a transaction. If your database is ANSI-compliant, SET ROLE must be the first statement of a new transaction. If the SET ROLE statement is executed while a transaction is active, an error occurs. For more information about SQL statements that initiate an implicit transaction, see SET SESSION AUTHORIZATION and Transactions on page 2-596.
2-592
SET ROLE
If the SET ROLE statement is executed as a part of a trigger or SPL routine, and the owner of the trigger or SPL routine was granted the role with the WITH GRANT OPTION, the role is enabled even if you are not granted the role. For example, this code fragment sets a role and then relinquishes it after a query:
EXEC SQL set role engineer; EXEC SQL select fname, lname, project INTO :efname, :elname, :eproject FROM projects WHERE project_num > 100 AND lname = Larkin; printf ("%s is working on %s\n", efname, eproject); EXEC SQL set role NULL;
If jgould subsequently uses the SET ROLE statement to enable some other role, then by executing the following statement, jgould replaces that role with Engineer as the current role:
SET ROLE DEFAULT;
If you have no default role, SET ROLE DEFAULT makes NONE your current role, leaving only the privileges that have been granted explicitly to your username or to PUBLIC. After GRANT DEFAULT ROLE changes your default role to a new default role, executing SET ROLE DEFAULT restores your most recently granted default role, even if this role was not your default role when you connected to the database. If one default role is granted to PUBLIC, but a different role is granted as the default role to an individual user, the individually-granted default role takes precedence if that user issues SET ROLE DEFAULT or connects to the database.
Related Information
Related statements: CREATE ROLE, DROP ROLE, GRANT, and REVOKE For a discussion of how to use roles, see the IBM Informix Guide to SQL: Tutorial.
2-593
Syntax
SET SCHEDULE LEVEL level
Element level
Restrictions Must be between 1 and 100. If the value falls outside the range of 1 and 100, the database server uses the default value of 50.
Usage
The highest priority level is 100. That is, a query at level 100 is more important than a query at level 1. In general, the Resource Grant Manager (RGM) processes a query with a higher scheduling level before a query with a lower scheduling level. The exact behavior of the RGM is also influenced by the setting of the DS_ADM_POLICY configuration parameter.
Related Information
Related statement: SET PDQPRIORITY For information about the Resource Grant Manager, see your IBM Informix Administrator's Guide. For information about the DS_ADM_POLICY configuration parameter, see your IBM Informix Administrator's Reference.
2-594
Syntax
SET SESSION AUTHORIZATION TO user
Element user
Description
Restrictions
User name by which database operations will Must be a valid user name. be performed in the current session Delimiters ( ) are optional
Usage
This statement allows a user with the DBA privilege to bypass the privileges that protect database objects. You can use this statement to gain access to a table and adopt the identity of a table owner to grant access privileges. You must obtain the DBA privilege before you start a session in which you use this statement. Otherwise, this statement returns an error. The new identity remains in effect in the current database until you execute SET SESSION AUTHORIZATION again, or until you close the current database. When you use this statement, the specified user must have the Connect privilege on the current database. In addition the DBA cannot set the new authorization identifier to PUBLIC, nor to any existing role in the current database. Setting a session to another user causes a change in a user name in the current active database server. The specified user, as far as this database server process is concerned, is completely dispossessed of any privileges while accessing the database server through some administrative utility. Additionally, the new session user is not able to initiate any administrative operation (execute a utility, for example) by virtue of the acquired identity. After the SET SESSION AUTHORIZATION statement successfully executes, any role enabled by a previous user is relinquished. You must use the SET ROLE statement if you wish to assume a role that has been granted to the specified user. The database server does not enable the default role of user automatically. After SET SESSION AUTHORIZATION successfully executes, the database server puts any owner-privileged UDRs that the DBA created while using the new authorization identifier in RESTRICTED mode, which can affect access privileges during operations of the UDR on objects in remote databases. For more information on RESTRICTED mode, see the sysprocedures system catalog table in the IBM Informix Guide to SQL: Reference. When you assume the identity of another user by executing the SET SESSION AUTHORIZATION statement, you can perform operations in the current database only. You cannot perform an operation on a database object outside the current database, such as a remote table. In addition, you cannot execute a DROP DATABASE or RENAME DATABASE statement, even if the database is owned by the real or effective user.
2-595
If you enclose user in quotation marks, the name is case sensitive and is stored exactly as you typed it. In an ANSI-compliant database, if you do not use quotation marks as delimiters, the name is stored in uppercase letters.
Related Information
Related statements: CONNECT, DATABASE, GRANT, and SET ROLE
2-596
Syntax
SET STATEMENT CACHE ON OFF
Usage
You can use the SET STATEMENT CACHE statement to turn caching in the SQL statement cache ON or OFF for the current session. The statement cache stores in a buffer identical statements that are repeatedly executed in a session. Only data manipulation language (DML) statements (DELETE, INSERT, UPDATE, or SELECT) can be stored in the statement cache. This mechanism allows qualifying statements to bypass the optimization stage and parsing stage, and avoid recompiling, which can reduce memory consumption and can improve query processing time.
2-597
In this example, both statements are entered into the SQL statement cache. Host-variable names, however, are insignificant. For example, the following select statements are considered identical:
SELECT * FROM tab1 WHERE x = :x AND y = :y; SELECT * FROM tab1 WHERE x = :p AND y = :q;
In the previous example, although the host names are different, the statements qualify, because the case, query text strings, and white space match. Performance does not improve, however, because each statement has already been parsed and optimized by the PREPARE statement.
Statement Qualification
A statement that can be cached in the SQL statement cache (and consequently, one that can match a statement that already appears in the SQL statement cache) must meet all of the following conditions: v It must be a SELECT, INSERT, UPDATE, or DELETE statement. v It must contain only non-opaque built-in data types (excluding BLOB, BOOLEAN, BYTE, CLOB, LVARCHAR, and TEXT).
2-598
3 3
3 3
Related Information
For information on optimization settings, see SAVE EXTERNAL DIRECTIVES on page 2-476, SET OPTIMIZATION on page 2-584, and Optimizer Directives on page 5-34. For information about the STMT_CACHE environment variable, see the IBM Informix Guide to SQL: Reference. For more information about STMT_CACHE, STMT_CACHE_NUMPOOL, STMT_CACHE_HITS, and other configuration parameters that affect the statement
Chapter 2. SQL Statements
2-599
2-600
SET TABLE
Use the SET TABLE statement to specify that one or more fragments of a table be resident in shared memory as long as possible. Only Extended Parallel Server supports this statement, which is an extension to the ANSI/ISO standard for SQL.
Syntax
SET TABLE table , ( dbspace ) MEMORY_RESIDENT NON_RESIDENT
Description Name of the dbspace in which the fragment resides Name of the table for which you want to change the residency state
Usage
This statement was formerly supported by Dynamic Server, but it is ignored in current releases. Beginning with Version 9.40, Dynamic Server determines the residency status of indexes and tables automatically. In Extended Parallel Server, however, you can use this statement to specify the residency status of table fragments. The SET TABLE statement is a special case of the SET Residency statement. The SET Residency statement can also specify how long an index fragment remains resident in shared memory. For the complete syntax and semantics of the SET TABLE statement, see SET Residency on page 2-590.
2-601
SET TRANSACTION
Use the SET TRANSACTION statement to define the isolation level and to specify whether the access mode of a transaction is read-only or read-write.
Syntax
, (1) SET TRANSACTION (1) ISOLATION LEVEL READ COMMITTED REPEATABLE READ SERIALIZABLE READ UNCOMMITTED READ WRITE READ ONLY
Usage
SET TRANSACTION is valid only in databases with transaction logging. You can issue a this statement from a client computer only after a database is opened. The database isolation level affects concurrency among processes that attempt to access the same rows simultaneously from the database. The database server uses shared locks to support different levels of isolation among processes that are attempting to read data, as the following list shows: v Read Uncommitted v Read Committed v (ANSI) Repeatable Read v Serializable The update or delete process always acquires an exclusive lock on the row that is being modified. The level of isolation does not interfere with such rows, but the access mode does affect whether you can update or delete rows. If another process attempts to update or delete rows that you are reading with an isolation level of Serializable or (ANSI) Repeatable Read, that process will be denied access to those rows.
2-602
SET TRANSACTION
SET TRANSACTION Isolation Level Read Uncommitted Read Committed [ Not supported ] (ANSI) Repeatable Read Serializable SET ISOLATION Isolation Level Dirty Read Committed Read Cursor Stability (Informix) Repeatable Read (Informix) Repeatable Read
Another difference between SET TRANSACTION and SET ISOLATION is the behavior of the isolation levels within transactions. You can issue SET TRANSACTION only once for a transaction. Any cursors that are opened during that transaction are guaranteed that isolation level (or access mode, if you are defining an access mode). With SET ISOLATION, after a transaction is started, you can change the isolation level more than once within the transaction. The following examples illustrate this difference in the behavior of the SET ISOLATION and SET TRANSACTION statements:
EXEC EXEC EXEC EXEC EXEC EXEC SQL SQL SQL SQL SQL SQL BEGIN WORK; SET ISOLATION TO DIRTY READ; SELECT ... ; SET ISOLATION TO REPEATABLE READ; INSERT ... ; COMMIT WORK; -- Executes without error
A additional difference between SET ISOLATION and SET TRANSACTION is the duration of isolation levels. Because SET ISOLATION supports complete-connection level settings, the isolation level specified by SET ISOLATION remains in effect until another SET ISOLATION statement is issued. The isolation level set by SET TRANSACTION only remains in effect until the transaction terminates. Then the isolation level is reset to the default for the database type.
2-603
SET TRANSACTION
row is retrieved. This option does not place a lock on the fetched row. Read Committed is the default level of isolation in a database with logging that is not ANSI compliant. Read Committed is appropriate when each row of data is processed as an independent unit, without reference to other rows in the same or other tables. Using the Repeatable Read and Serializable Options: The Informix implementation of Repeatable Read and of Serializable are equivalent. The Serializable (or Repeatable Read) option places a shared lock on every row that is selected during the transaction. Another process can also place a shared lock on a selected row, but no other process can modify any selected row during your transaction or insert a row that meets the search criteria of your query during your transaction. A phantom row is a row that was not visible when you first read the query set, but that materializes in a subsequent read of the query set in the same transaction. Only this isolation level prevents access to a phantom row. If you repeat the query during the transaction, you reread the same data. The shared locks are released only when the transaction is committed or rolled back. Serializable is the default isolation level in an ANSI-compliant database. Serializable isolation places the largest number of locks and holds them the longest. Therefore, it is the level that reduces concurrency the most.
The default isolation level remains in effect until you issue a SET TRANSACTION statement within a transaction. After a COMMIT WORK statement completes the transaction or a ROLLBACK WORK statement cancels the transaction, the isolation level is reset to the default.
Access Modes
Access modes affect read and write concurrency for rows within transactions. Use access modes to control data modification. SET TRANSACTION can specify that a transaction is read-only or read-write. By default, transactions are read-write. When you specify a read-only transaction, certain limitations apply. Read-only transactions cannot perform the following actions: v Insert, delete, or update rows of a table. v Create, alter, or drop any database object such as schemas, tables, temporary tables, indexes, or SPL routines. v Grant or revoke access privileges. v Update statistics. v Rename columns or tables.
2-604
SET TRANSACTION
You can execute SPL routines in a read-only transaction as long as the SPL routine does not try to perform any restricted statement.
Related Information
Related statements: CREATE DATABASE, SET ISOLATION, and SET LOCK MODE For a discussion of isolation levels and concurrency issues, see the IBM Informix Guide to SQL: Tutorial.
2-605
Syntax
, SET CONSTRAINTS constraint ALL IMMEDIATE DEFERRED
Element constraint
Restrictions All constraints must exist in the same database, which must support logging
Usage
This statement is valid only in a database with transaction logging, and its effect is limited to the transaction in which it is executed. Use the IMMEDIATE keyword to set the transaction mode of constraints to statement-level checking. IMMEDIATE is the default transaction mode of constraints when they are created. Use the DEFERRED keyword to set the transaction mode to transaction-level checking. You cannot change the transaction mode of a constraint to DEFERRED unless the constraint is currently enabled.
Statement-Level Checking
When you set the transaction mode to IMMEDIATE, statement-level checking is turned on, and all specified constraints are checked at the end of each INSERT, UPDATE, or DELETE statement. If a constraint violation occurs, the statement is not executed.
Transaction-Level Checking
When you set the transaction mode of constraints to DEFERRED, statement-level checking is turned off, and all (or the specified) constraints are not checked until the transaction is committed. If a constraint violation occurs while the transaction is being committed, the transaction is rolled back. Tip: If you defer checking a primary-key constraint, checking the not-NULL constraint for that column or set of columns is also deferred.
2-606
The following example specifies that a list of constraints is not checked until the transaction is complete:
BEGIN WORK SET CONSTRAINTS update_const, insert_const DEFERRED ... COMMIT WORK
Related Information
Related statements: ALTER TABLE and CREATE TABLE
Chapter 2. SQL Statements
2-607
SET TRIGGERS
Use the SET TRIGGERS statement to enable or disable all or some of the triggers on a table, or all or some of the INSTEAD OF triggers on a view.
Syntax
, ENABLED SET TRIGGERS trigger FOR table view DISABLED
Description Table whose triggers are all to be enabled or disabled Trigger to be enabled or disabled View whose INSTEAD OF triggers are all to be enabled or disabled
Syntax Database Object Name, p. 5-17 Database Object Name, p. 5-17 Database Object Name, p. 5-17
Usage
The SET TRIGGERS statement is a special case of the SET Database Object Mode statement. The SET Database Object Mode statement can also enable or disable an index or a constraint, or change the filtering mode of a unique index or of a constraint. For the complete syntax and semantics of the SET TRIGGERS statement, see SET Database Object Mode on page 2-539.
2-608
Syntax
START VIOLATIONS TABLE FOR table
Notes: 1 2
Element diagnostics Description Declares the name of a diagnostics table to be associated with the target table. Default name is table_dia. Maximum number of rows database server (IDS) or any single coserver (XPS) can insert into violations when a single statement is executed on table Target table for which violations and (for Dynamic Server only) diagnostics are to be created
num_rows
table
violations
Violations table to be associated with table. Same restrictions as diagnostics Default name is table_vio.
Usage
The database server associates the violations table (and for Dynamic Server only) the diagnostics table) with the target table that you specify after the FOR keyword by recording the relationship among the three tables in the sysviolations system catalog table. In Extended Parallel Server, the START VIOLATIONS TABLE statement creates a violations table, but no diagnostics table is created. A target table must satisfy these requirements: v It cannot be external to the database. v It cannot already be associated with a violations or diagnostics table. v It cannot be a system catalog table.
Chapter 2. SQL Statements
2-609
2-610
USING Clause
You can use the USING clause to declare explicit names for the violations table and (for Dynamic Server) the diagnostics table. If you omit the USING clause, the database server assigns names to the violation table and (for Dynamic Server) the diagnostics table. The system-assigned name of the violations table consists of the name of the target table followed by the string _vio. The name that Dynamic Server assigns to the diagnostics table consists of the name of the target table followed by the string _dia. If you omit the USING clause, the maximum length of the name of the target table is 124 bytes.
2-611
User who issued the statement that created this nonconforming row
2-612
Violations and Diagnostics Tables with Explicit Names: The following statement starts a violations and diagnostics table for the target table named items. The USING clause assigns explicit names to the violations and diagnostics tables. The violations table is to be named exceptions, and the diagnostics table is to be named reasons.
START VIOLATIONS TABLE FOR items USING exceptions, reasons
2-613
Delete
Select
The following rules apply to ownership of the violations table and privileges on the violations table: v When the violations table is created, the owner of the target table becomes the owner of the violations table. v The owner of the violations table automatically receives all table-level privileges on the violations table, including the Alter and References privileges. The
2-614
2-615
The database server grants the following set of initial privileges on the cust_subset_viols violations table: v User alvin is the owner of the violations table, so he has all table-level privileges on the table. v User barbara has the Insert, Delete, and Index privileges on the table. User barbara has the Select privilege on five columns of the violations table: the ssn, the lname, the informix_tupleid, the informix_optype, and the informix_recowner columns. v User carrie has Insert and Delete privileges on the violations table. User carrie has the Update privilege on four columns of the violations table: the city, the informix_tupleid, the informix_optype, and the informix_recowner columns. She cannot, however, update the informix_tupleid column (because this is a SERIAL column). User carrie has the Select privilege on four columns of the violations table: the ssn column, the informix_tupleid column, the informix_optype column, and the informix_recowner column. v User danny has no privileges on the violations table.
2-616
Because you include no USING clause, the violations table is named customer_vio by default. The customer_vio table includes these columns:
customer_num fname lname company address1 address2 city state zipcode phone informix_tupleid informix_optype informix_recowner
The customer_vio table has the same table definition as the customer table except that the customer_vio table has three additional columns that contain information about the operation that caused the nonconforming row.
2-617
objtype
CHAR(1)
Identifies the owner of the constraint or index for which an integrity violation was detected Contains the name of the constraint or index for which an integrity violation was detected
Delete
2-618
2-619
Because your START VIOLATIONS TABLE statement does not include a USING clause, the diagnostics table is named stock_dia by default. The stock_dia table includes the following columns:
informix_tupleid objtype objowner objname
2-620
This list of columns shows an important difference between the diagnostics table and violations table for a target table. Whereas the violations table has a matching column for every column in the target table, the columns of the diagnostics table do not match any columns in the target table. The diagnostics table created by any START VIOLATIONS TABLE statement always has the same columns with the same column names and data types.
Related Information
Related statements: CREATE INDEX, CREATE TABLE, SET Database Object Mode, and STOP VIOLATIONS TABLE For a discussion of object modes and violation detection, see the IBM Informix Guide to SQL: Tutorial.
2-621
Syntax
STOP VIOLATIONS TABLE FOR table
Element table
Description Name of a target table whose association with the violations and diagnostics table is to be dropped. No default value exists.
Restrictions Must be a local table that has an associated violations table and (for IDS) a diagnostics table
Usage
The STOP VIOLATIONS TABLE statement drops the association between the target table and the violations table (and for Dynamic Server, the diagnostics table). After you issue this statement, the former violations and diagnostics tables continue to exist, but they no longer function as violations and diagnostics tables for the target table. They now have the status of regular database tables instead of violations and diagnostics tables for the target table. You must issue the DROP TABLE statement to drop these two tables explicitly. When DML operations (INSERT, DELETE, or UPDATE) cause data-integrity violations for rows of the target table, the nonconforming rows are no longer filtered to the former violations table, and diagnostic information about the data-integrity violations is not placed in the former diagnostics table. In Extended Parallel Server, the diagnostics table does not exist. The STOP VIOLATIONS TABLE statement drops the association between the target table and the violations table.
2-622
Related Information
Related statements: SET Database Object Mode and START VIOLATIONS TABLE For a discussion of database object modes and violation detection, see the IBM Informix Guide to SQL: Tutorial.
2-623
4 4 4 4 4 4 4 4 4 4
TRUNCATE (IDS)
Use the TRUNCATE statement to quickly delete all rows from a local table and free the associated storage space. You can optionally reserve the space for the same table and its indexes. Only Dynamic Server supports this implementation of the TRUNCATE statement, which is an extension to the ANSI/ISO standard for SQL. Extended Parallel Server supports a different implementation of TRUNCATE, as the section TRUNCATE (XPS) on page 2-628describes.
Syntax
TABLE TRUNCATE 'owner.' table synonym DROP STORAGE REUSE STORAGE
Must exist, and Identifier, p. 5-22 USETABLENAME must not be set Must exist in the database Identifier, p. 5-22
Usage
The TRUNCATE statement rapidly deletes from a local table all active data rows and the B-tree structures of indexes on the table. You have the option of releasing the storage space that was occupied by the rows and index extents, or of reusing the same space when the table is subsequently repopulated with new rows. To execute the TRUNCATE statement, you must be the owner of the table, or else hold DBA access privilege on the database. You must also hold Delete privilege on the table. If an enabled Delete trigger is defined on the table, the Alter privilege is also required, even though TRUNCATE does not activate triggers. Although it requires the Delete privilege, TRUNCATE is a data definition language (DDL) statement. Like other DDL statements, TRUNCATE cannot operate on any table outside the database to which you are connected, nor on a table that a concurrent session is reading in Dirty Read isolation mode. Dynamic Server always logs the TRUNCATE operation, even for a non-logging table. In databases that support transaction logging, only the COMMIT WORK or ROLLBACK WORK statement of SQL is valid after the TRUNCATE statement within the same transaction. When you rollback a TRUNCATE statement, no rows are removed from the table, and the storage extents that hold the rows and index partitions continue to be allocated to the table. Only databases with transaction logging can support the ROLLBACK WORK statement. After the TRUNCATE statement successfully executes, Dynamic Server automatically updates the statistics and distributions for the table and for its indexes in the system catalog to show no rows in the table nor in its dbspace
2-624
TRUNCATE (IDS)
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 partitions. It is not necessary to run the UPDATE STATISTICS statement immediately after you commit the TRUNCATE statement. If the table that the TRUNCATE statement specifies is a typed table, a successful TRUNCATE operation removes all the rows and B-tree structures from that table and from all its subtables within the table hierarchy. The TRUNCATE statement does not reset the serial value of SERIAL or SERIAL8 columns. To reset the counter of a serial column, you must do so explicitly by using the MODIFY clause of the ALTER TABLE statement, either before or after you execute the TRUNCATE statement.
2-625
TRUNCATE (IDS)
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 Whether you specify DROP STORAGE or REUSE STORAGE, any out-of-row data values are released for all rows of the table when the TRUNCATE transaction is committed. Storage occupied by any BLOB or CLOB values that become unreferenced in the TRUNCATE transaction is also released.
You can also use the ALTER ACCESS_METHOD statement to add a valid am_truncate purpose function to an existing access method that has no am_truncate purpose function:
ALTER ACCESS_METHOD abc ( ADD AM_TRUNCATE = abc_truncate);
In these examples, the vti_truncate and abc_truncate functions must be routines that support the functionality of the AM_TRUNCATE purpose option keyword, and that were previously registered in the database by the CREATE FUNCTION or CREATE ROUTINE statement.
2-626
TRUNCATE (IDS)
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 v Any simple large object data types stored in blobspaces v Any BLOB, CLOB, complex, or user-defined types stored in sbspaces v Any opaque types for which a destroy support function is defined. Each of these features require the database server to read each row of the table, substantially reducing the speed of TRUNCATE. If a table includes one or more UDTs for which you have registered an am_truncate( ) purpose function, then the performance difference between TRUNCATE and DELETE would reflect the relative costs of invoking the am_truncate interface once for TRUNCATE versus invoking the destroy support function for each row. As listed in the next section, certain conditions cause TRUNCATE to fail with an error. Some of these conditions have no effect on DELETE operations, so in those cases you can remove all rows more efficiently with a DELETE statement, as in the following operation on the customer table:
DELETE customer;
The FROM keyword that immediately follows DELETE can be omitted, as in this example, only if the DELIMIDENT environment variable is set.
Restrictions
The TRUNCATE statement fails if any of the following conditions exist: v The user does not have Delete privilege on the table. v v v v v v v v v v v v v The table has an enabled Delete trigger, but the user lacks the Alter privilege. The specified table or synonym does not exist in the local database. The specified synonym does not reference a table in the local database. The statement specifies a synonym for a local table, but the USETABLENAME environment variable is set. The statement specifies the name of a view or a synonym for a view. The table is a system catalog table or a system-monitoring interface (SMI) table. An R-tree index is defined on the table. The table is a virtual table (or has a virtual-index interface) for which no valid am_truncate access method exists in the database. An Enterprise Replication replicate is defined on the table. A shared or exclusive lock on the table already exists. One or more cursors are open on the table. A concurrent session with Dirty Read isolation level is reading the table. Another table, with at least one row, has an enabled foreign-key constraint on the specified table. (An enabled foreign key constraint of another table that has no rows, however, has no effect on a TRUNCATE operation.)
Related Information
DELETE, DROP TABLE
2-627
TRUNCATE (XPS)
Use the TRUNCATE statement for quick removal of all rows from a table and all corresponding index data. Only Extended Parallel Server supports this implementation of the TRUNCATE statement, which is an extension to the ANSI/ISO standard for SQL. Dynamic Server supports a different implementation of TRUNCATE, as described in the section TRUNCATE (IDS) on page 2-624.
Syntax
TABLE TRUNCATE ONLY owner. table
Element table
Usage
You must be the owner of the table or have DBA privilege to use this statement. The TRUNCATE statement does not automatically reset the serial value of a column. To reset the serial value of a column, you must do so explicitly, either before or after you run the TRUNCATE statement. TRUNCATE is not equivalent to DROP TABLE. After TRUNCATE successfully executes, the specified table (and all its columns, and any synonyms, views, constraints, indexes, triggers, and access privileges) still exists in the database schema, but with no rows of data.
Restrictions
The TRUNCATE statement fails if any of the following conditions exist: v One or more cursors are open on the table. v Referential constraints exist on the table and any of the referencing tables has at least one row. v A shared or exclusive lock on the table already exists. v The statement references a view. v The statement references any of the following types of tables: An external table A system catalog table A violations table v The statement is issued within a transaction.
2-628
TRUNCATE (XPS)
TRUNCATE ONLY TABLE customer TRUNCATE TABLE customer TRUNCATE ONLY customer TRUNCATE customer
Related Information
Related statements: DELETE and DROP TABLE
2-629
UNLOAD
Use UNLOAD to write the rows retrieved by a SELECT statement to an operating-system file. Use UNLOAD only with DBAccess UNLOAD is an extension to the ANSI/ISO standard for SQL.
Syntax
UNLOAD TO filename DELIMITER delimiter
Notes: 1
Element delimiter filename Description Quoted string to specify the field delimiter character in filename file Operating-system file to receive the rows. Default pathname is the current directory.
variable
Host variable that contains the text of Must have been declared as a a valid SELECT statement character data type
Language- specific
Usage
UNLOAD copies to a file the rows retrieved by a query. You must have the Select privilege on all columns specified in the SELECT statement. For information on database-level and table-level privileges, see GRANT on page 2-371. You can specify a literal SELECT statement or a character variable that contains the text of a SELECT statement. (See SELECT on page 2-479.) The following example unloads rows whose value of customer.customer_num is greater than or equal to 138, and writes them to a file named cust_file:
UNLOAD TO cust_file DELIMITER ! SELECT * FROM customer WHERE customer_num> = 138
The resulting output file, cust_file, contains two rows of data values:
138!Jeffery!Padgett!Wheel Thrills!3450 El Camino!Suite 10!Palo Alto!CA!94306!! 139!Linda!Lane!Palo Alto Bicycles!2344 University!!Palo Alto!CA!94301! (415)323-5400
UNLOAD TO File
The UNLOAD TO file, as specified by the filename parameter, receives the retrieved rows. You can use an UNLOAD TO file as input to a LOAD statement. In the default locale, data values have these formats in the UNLOAD TO file.
2-630
UNLOAD
Data Type BOOLEAN Character Output Format BOOLEAN values appear as either t for TRUE or f for FALSE. If a character field contains the delimiter, IBM Informix products automatically escape it with a backslash ( \ ) to prevent interpretation as a special character. (If you use a LOAD statement to insert the rows into a table, backslash escape characters are automatically stripped.) A collection is unloaded with its values between braces ( { } ) and a delimiter between each element. For more information, see Unloading Complex Types (IDS) on page 2-633. DATE values are represented as mm/dd/yyyy (or the default format for the database locale), where mm is the month (January = 1, and so on), dd is the day, and yyyy is the year. If you have set the GL_DATE or DBDATE environment variable, the UNLOAD statement uses the specified date format for DATE values. Literal DATETIME and INTERVAL values appear as digits and delimiters, without keyword qualifiers, in the default format yyyy-mm-dd hh:mi:ss.fff. Time units outside the declared precision are omitted. If the GL_DATETIME or DBTIME environment variable is set, DATETIME values appear in the specified format. Values are unloaded with no leading currency symbol. In the default locale, comma ( , ) is the thousands separator and period ( . ) is the decimal separator. If DBMONEY is set, UNLOAD uses its specified separators and currency format for MONEY values. NULL appears as two delimiters with no characters between them. Values appear as literals, with no leading blanks. INTEGER, INT8, or SMALLINT zero appear as 0, and MONEY, FLOAT, SMALLFLOAT, or DECIMAL zero appears as 0.0. A ROW type is unloaded with its values enclosed between parentheses and a field delimiter separating each element. For more information, see Unloading Complex Types (IDS) on page 2-633. TEXT and BYTE columns are unloaded directly into the UNLOAD TO file. BYTE values appear in ASCII hexadecimal form, with no added whitespace or newline characters. For more information, see Unloading Simple Large Objects on page 2-632. CLOB and BLOB columns are unloaded into a separate operating-system file on the client computer. The CLOB or BLOB field in the UNLOAD TO file contains the name of this file. For more information, see Unloading Smart Large Objects (IDS) on page 2-632. Opaque types must have an export( ) support function defined. They need special processing to copy data from the internal format of the opaque data type to the UNLOAD TO file format. An export binary support function might also be required for data in binary format. The data in the UNLOAD TO file would correspond to the format that the export( ) or exportbinary( ) support function returns.
Collections DATE
DATETIME, INTERVAL
DECIMAL, MONEY NULL Number ROW types (named and unnamed) Simple large objects (TEXT, BYTE) Smart large objects (CLOB, BLOB) User-defined data types (opaque types)
For more information on DB* environment variables, refer to the IBM Informix Guide to SQL: Reference. For more information on GL* environment variables, refer to the IBM Informix GLS User's Guide. In a nondefault locale, DATE, DATETIME, MONEY, and numeric column values have formats that the locale supports for these data types. For more information, see the IBM Informix GLS User's Guide.
2-631
UNLOAD
Some earlier releases of Informix database servers used || (consecutive delimiters) to represent the empty string in LOAD and UNLOAD operations. In this release, however, || only represents NULL values in CHAR, VARCHAR, LVARCHAR, NCHAR, and NVARCHAR columns.
In this format, start_off is the starting offset (in hexadecimal format) of the smart-large-object value within the client file, length is the length (in hexadecimal) of the BLOB or CLOB value, and client_path is the pathname for the client file. No blank spaces can appear between these values. If a CLOB value is 512 bytes long and is at offset 256 in the /usr/apps/clob9ce7.318 file, for example, then the CLOB value appears as follows in the UNLOAD TO file:
|100,200,/usr/apps/clob9ce7.318|
2-632
UNLOAD
If a BLOB or CLOB column value occupies an entire client file, the CLOB or BLOB column value appears as follows in the UNLOAD TO file:
client_path
For example, if a CLOB value occupies the entire file /usr/apps/clob9ce7.318, the CLOB value appears as follows in the UNLOAD TO file:
|/usr/apps/clob9ce7.318|
For locales that support multibyte code sets, be sure that the declared size (in bytes) of any column that receives character data is large enough to store the entire data string. For some locales, this can require up to 4 times the number of logical characters in the longest data string. The database server handles any required code-set conversions for CLOB data. For more information, see the IBM Informix GLS User's Guide.
For example, to unload the SET values {1, 3, 4} from a column of the SET (INTEGER NOT NULL) data type, the corresponding field of the UNLOAD TO file appears as follows:
|SET{1 , 3 , 4}|
v ROW types (named and unnamed) are introduced by the ROW constructor and have their fields enclosed between parentheses and comma-separated:
ROW(val1 , val2 , ... )
For example, to unload the ROW values (1, abc), the corresponding field of the UNLOAD TO file appears as follows:
|ROW(1 , abc)|
DELIMITER Clause
Use the DELIMITER clause to specify the delimiter that separates the data contained in each column in a row in the output file. If you omit this clause, then DBAccess checks the setting of the DBDELIMITER environment variable. If DBDELIMITER has not been set, the default delimiter is the pipe ( | ) symbol. You can specify TAB (CTRL-I) or a blank space (ASCII 32) as the delimiter symbol, but the following characters are not valid in any locale as delimiter symbols: v Backslash ( \ ) v Newline character (CTRL-J) v Hexadecimal digits (0 to 9, a to f, A to F) The backslash ( \ ) is not a valid field separator or record delimiter because it is the default escape character, indicating that the next character is a literal character in the data, rather than a special character. The following statement specifies the semicolon ( ; ) as the delimiter:
UNLOAD TO cust.out DELIMITER ; SELECT fname, lname, company, city FROM customer
C-style comment indicators ( /* . . . */ ) are not valid for comments in the LOAD or UNLOAD statements. Use double hyphen ( -- ) or braces ( { ... } ) instead.
Chapter 2. SQL Statements
2-633
UNLOAD
Related Information
Related statements: LOAD and SELECT For information about how to set environment variables, see the IBM Informix Guide to SQL: Reference. For a discussion of the GLS aspects of the UNLOAD statement, see the IBM Informix GLS User's Guide. For a task-oriented discussion of the UNLOAD statement and other utilities for moving data, see the IBM Informix Migration Guide.
2-634
UNLOCK TABLE
Use the UNLOCK TABLE statement in a database that does not use implicit transactions to unlock a table that you previously locked with the LOCK TABLE statement. The UNLOCK TABLE statement is not valid within a transaction. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
UNLOCK TABLE table synonym
Restrictions
Syntax
The synonym and the table to which it points must exist Database Object Name, p. 5-17 Must be in a database without transactions and must be a table that you previously locked Database Object Name, p. 5-17
Usage
You can lock a table if you own the table or if you have the Select privilege on the table, either from a direct grant to yourself or from a grant to public. You can only unlock a table that you locked. You cannot unlock a table that another process locked. Only one lock can apply to a table at a time. You must specify the name or synonym of the table that you are unlocking. Do not specify the name of a view, or a synonym for a view. To change the lock mode of a table in a database without transactions, use the UNLOCK TABLE statement to unlock the table, then issue a new LOCK TABLE statement. The following example shows how to change the lock mode of a table in a database that was created without transactions:
LOCK TABLE items IN EXCLUSIVE MODE ... UNLOCK TABLE items ... LOCK TABLE items IN SHARE MODE
The UNLOCK TABLE statement fails if it is issued within a transaction. Table locks set within a transaction are released automatically when the transaction completes. If you are using an ANSI-compliant database, do not issue an UNLOCK TABLE statement. The UNLOCK TABLE statement fails if it is issued within a transaction, and a transaction is always in effect in an ANSI-compliant database.
Related Information
Related statements: BEGIN WORK, COMMIT WORK, LOCK TABLE, and ROLLBACK WORK For a discussion of concurrency and locks, see the IBM Informix Guide to SQL: Tutorial.
2-635
UPDATE
Use the UPDATE statement to change the values in one or more columns of one or more existing rows in a table or view. With Dynamic Server, you can also use this statement to change the values in one or more elements in an ESQL/C collection variable.
Syntax
(4) UPDATE (1) (2) (3) (4) (6) (7) WHERE CURRENT OFcursor_id Optimizer Directives (5) Collection-Derived Table SET Clause Table Options SET Clause Where Options
Table Options:
table view synonym (1) (2) ONLY (table ) (synonym)
Where Options:
(8) Subset of FROM Clause WHERE (6) (7) WHERE CURRENT OFcursor_id
(9) Condition
Notes: 1 2 3 4 5 6 7 8 9 Informix extension Dynamic Server only See page 5-34 See page 2-639 See page 5-5 ESQL/C only Stored Procedure Language only See page 2-645 See page 4-5
2-636
UPDATE
Element cursor_id Description Name of a cursor whose current row is to be updated Restrictions You cannot update a row that includes aggregates The synonym and the table or view to which it points must exist Syntax Identifier, p. 5-22 Database Object Name, p. 5-17
synonym, table, Synonym, table, or view that view contains the rows to be updated
Usage
Use the UPDATE statement to update any of the following types of objects: v A row in a table: a single row, a group of rows, or all rows in a table v For Dynamic Server, an element in a collection variable v An ESQL/C row variable: a field or all fields For information on how to update elements of a collection variable, see Collection-Derived Table on page 5-5. Sections that follow in this description of the UPDATE statement describe how to update a row in a table. You must either own the table or have the Update privilege for the table; see GRANT on page 2-371. To update data in a view, you must have the Update privilege, and the view must meet the requirements that are explained in Updating Rows Through a View on page 2-638. The cursor (as defined in the SELECT ... FOR UPDATE portion of a DECLARE statement) can contain only column names. If you omit the WHERE clause, all rows of the target table are updated. If you are using effective checking and the checking mode is set to IMMEDIATE, all specified constraints are checked at the end of each UPDATE statement. If the checking mode is set to DEFERRED, all specified constraints are not checked until the transaction is committed. In Extended Parallel Server, if UPDATE is constructed in such a way that a single row might be updated more than once, the database server returns an error. If the new value is the same in every update, however, the database server allows the update to take place without reporting an error. In DB-Access, if you omit the WHERE clause and are in interactive mode, DBAccess does not run the UPDATE statement until you confirm that you want to change all rows. If the statement is in a command file, however, and you are running at the command line, the statement executes immediately.
Note: If you use the UPDATE statement on a supertable without the ONLY keyword and without a WHERE clause, all rows of the supertable and its subtables are updated. You cannot use the ONLY keyword if you plan to use the WHERE CURRENT OF clause to update the current row of the active set of a cursor.
Chapter 2. SQL Statements
2-637
UPDATE
2-638
UPDATE
In an ANSI-compliant database, transactions are implicit, and all database modifications take place within a transaction. In this case, if an UPDATE statement fails, you can use ROLLBACK WORK to undo the update. If you are within an explicit transaction, and the update fails, the database server automatically undoes the effects of the update.
Locking Considerations
When a row is selected with the intent to update, the update process acquires an update lock. Update locks permit other processes to read, or share, a row that is about to be updated, but they do not allow those processes to update or delete it. Just before the update occurs, the update process promotes the shared lock to an exclusive lock. An exclusive lock prevents other processes from reading or modifying the contents of the row until the lock is released. An update process can acquire an update lock on a row or on a page that has a shared lock from another process, but you cannot promote the update lock from shared to exclusive (and the update cannot occur) until the other process releases its lock. If the number of rows that a single update affects is large, you can exceed the limits placed on the maximum number of simultaneous locks. If this occurs, you can reduce the number of transactions per UPDATE statement, or you can lock the page or the entire table before you execute the statement.
SET Clause
Use the SET clause to identify the columns to update and assign values to each column. The clause supports the following formats: v A single-column format, which pairs each column with a single expression v A multiple-column format, which associates a list of multiple columns with the values returned by one or more expressions SET Clause:
(1) Single-Column Format (2) Multiple-Column Format
SET
(3)
Single-Column Format
Use the single-column format to pair one column with a single expression. Single-Column Format:
2-639
UPDATE
, column = expression ( singleton_select ) NULL (1) collection_var
Notes: 1
Element column collection_var expression singleton _select Description Column to be updated Host or program variable Returns a value for column Subquery that returns exactly one row
You can use this syntax to update a column that has a ROW data type. You can include any number of single column = single expression terms. The expression can be an SQL subquery (enclosed between parentheses) that returns a single row, provided that the corresponding column is of a data type that can store the value (or the set of values) from the row that the subquery returns. To specify values of a ROW-type column in a SET clause, see Updating ROW-Type Columns (IDS) on page 2-643. The following examples illustrate the single-column format of the SET clause.
UPDATE customer SET address1 = 1111 Alder Court, city = Palo Alto, zipcode = 94301 WHERE customer_num = 103; UPDATE stock SET unit_price = unit_price * 1.07;
In Dynamic Server, if you are updating a supertable in a table hierarchy, the SET clause cannot include a subquery that references a subtable. If you are updating a subtable in a table hierarchy, a subquery in the SET clause can reference the supertable if it references only the supertable. That is, the subquery must use the SELECT...FROM ONLY (supertable) syntax.
2-640
UPDATE
UPDATE customer SET address1 = 123 New Street, SET address2 = null, city = Palo Alto, zipcode = 94303 WHERE customer_num = 134
Multiple-Column Format
Use the multiple-column format of the SET clause to list multiple columns and set them equal to corresponding expressions. Multiple-Column Format:
, ( * , ( expression , ( singleton_select ) NULL (1) SPL function ( , (2) Argument ) column ) =
Notes: 1 2
Element column Description Name of a column to be updated Expression that returns a value for a column Subquery that returns exactly one row
SPL routine that returns one Returned values must have a 1-to-1 correspondence Identifier, or more values to columns in the column list p. 5-22
2-641
UPDATE
The multiple-column format of the SET clause offers the following options for listing a set of columns that you intend to update: v Explicitly list each column, placing commas between columns and enclosing the set of columns between parentheses. v Implicitly list all columns in the table by using an asterisk ( * ). You must list each expression explicitly, placing comma ( , ) separators between expressions and enclosing the set of expressions between parentheses. The number of columns must equal the number of values returned by the expression list, unless the expression list includes an SQL subquery. The following examples show the multiple-column format of the SET clause:
UPDATE customer SET (fname, lname) = (John, Doe) WHERE customer_num = 101 UPDATE manufact SET * = (HNT, Hunter) WHERE manu_code = ANZ
The following examples show the use of subqueries in the SET clause:
UPDATE items SET (stock_num, manu_code, quantity) = ( (SELECT stock_num, manu_code FROM stock WHERE description = baseball), 2) WHERE item_num = 1 AND order_num = 1001 UPDATE table1 SET (col1, col2, col3) = ((SELECT MIN (ship_charge), MAX (ship_charge) FROM orders), 07/01/1997) WHERE col4 = 1001
In Dynamic Server, if you are updating a supertable in a table hierarchy, the SET clause cannot include a subquery that references one of its subtables. If you are updating a subtable in a table hierarchy, a subquery in the SET clause can reference the supertable if it references only the supertable. That is, the subquery must use the SELECT... FROM ONLY (supertable) syntax.
2-642
UPDATE
Data-Manipulation Statements on page 5-71. In the next example, the SPL function p2( ) updates the i2 and c2 columns of the t2 table:
CREATE PROCEDURE p2() RETURNING int, char(20); RETURN 3, three; END PROCEDURE; UPDATE t2 SET (i2, c2) = (p2()) WHERE i2 = 2;
In Extended Parallel Server, you create an SPL function with the CREATE PROCEDURE statement. The CREATE FUNCTION statement is not available.
To update an unnamed ROW type, specify the ROW constructor before the parenthesized list of field values. The following statement updates the name column (an unnamed ROW type) of the empinfo table:
UPDATE empinfo SET name = ROW(John,Williams) WHERE emp_id =455
To update a named ROW type, specify the ROW constructor before the list (in parentheses) of field values, and use the cast ( :: ) operator to cast the ROW value as a named ROW type. The following statement updates the address column (a named ROW type) of the empinfo table:
UPDATE empinfo SET address = ROW(103 Baker St,Tracy,CA)::address_t WHERE emp_id = 3568
For more information on the syntax for ROW constructors, see Constructor Expressions (IDS) on page 4-63. See also Literal Row on page 4-139. The ROW-column SET clause can only support literal values for fields. To use an ESQL/C variable to specify a field value, you must select the ROW data into a row variable, use host variables for the individual field values, then update the ROW column with the row variable. For more information, see Updating a Row Variable (IDS, ESQL/C) on page 2-647. You can use ESQL/C host variables to insert non-literal values as: v An entire row type into a column Use a row variable as a variable name in the SET clause to update all fields in a ROW column at one time. v Individual fields of a ROW type To insert non-literal values into a ROW-type column, you can first update the elements in a row variable and then specify the collection variable in the SET clause of an UPDATE statement.
Chapter 2. SQL Statements
2-643
UPDATE
When you use a row variable in the SET clause, the row variable must contain values for each field value. For information on how to insert values into a row variable, see Updating a Row Variable (IDS, ESQL/C) on page 2-647. You can use the UPDATE statement to modify only some of the fields in a row: v Specify the field names with field projection for all fields whose values remain unchanged. For example, the following UPDATE statement changes only the street and city fields of the address column of the empinfo table:
UPDATE empinfo SET address = ROW(23 Elm St, Sacramento, address.state) WHERE emp_id = 433
The address.state field remains unchanged. v Select the row into an ESQL/C row variable and update the desired fields. For more information, see Updating a Row Variable (IDS, ESQL/C) on page 2-647.
Collection column list1 in this example has three elements. Each element is an unnamed ROW type with an INTEGER field and a CHAR(5) field. The first element includes two literal values: an integer ( 2 ) and a quoted string (zyxwv). The second and third elements also use a quoted string to indicate the value for the second field. They each designate the value for the first field with an expression, however, rather than with a literal value.
2-644
UPDATE
This processing is accomplished by calling a user-defined support function called assign( ). When you execute UPDATE on a table whose rows contain one of these opaque types, the database server automatically invokes the assign( ) function for the type. This function can make the decision of how to store the data. For more information about the assign( ) support function, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
UPDATE supports only a subset of the syntax listed in FROM Clause on page 2-492. You cannot include the LOCAL or the SAMPLES OF keywords if you are using Extended Parallel Server.
WHERE Clause
The WHERE clause lets you specify search criteria to limit the rows to be updated. If you omit the WHERE clause, every row in the table is updated. For more information, see the WHERE Clause on page 2-507. The next example uses WHERE and FROM clauses to update three columns (state, zipcode, and phone) in each row of the customer table that has a corresponding entry in a table of new addresses called new_address:
UPDATE customer SET (state, zipcode, phone) = ((SELECT state, zipcode, phone FROM new_address N WHERE N.cust_num = customer.customer_num)) WHERE customer_num IN (SELECT cust_num FROM new_address)
2-645
UPDATE
2-646
UPDATE
receive 10-percent discounts (assume that a new column, discount, is added to the customer table). The UPDATE statement is prepared outside the WHILE loop to ensure that parsing is done only once.
char answer [1] = y; EXEC SQL BEGIN DECLARE SECTION; char fname[32],lname[32]; int low,high; EXEC SQL END DECLARE SECTION; main() { EXEC SQL connect to stores_demo; EXEC SQL prepare sel_stmt from select fname, lname from customer \ where cust_num between ? and ? for update; EXEC SQL declare x cursor for sel_stmt; printf("\nEnter lower limit customer number: "); scanf("%d", &low); printf("\nEnter upper limit customer number: "); scanf("%d", &high); EXEC SQL open x using :low, :high; EXEC SQL prepare u from update customer set discount = 0.1 where current of x; while (1) { EXEC SQL fetch x into :fname, :lname; if ( SQLCODE == SQLNOTFOUND) break; } printf("\nUpdate %.10s %.10s (y/n)?", fname, lname); if (answer = getch() == y) EXEC SQL execute u; EXEC SQL close x; }
2-647
UPDATE
Suppose that after the SELECT statement, the myrect2 variable has the values x=0, y=0, length=8, and width=8. After the UPDATE statement, the myrect2 variable has field values of x=3, y=4, length=8, and width=8. You cannot use a row variable in the Collection-Derived-Table segment of an INSERT statement. You can, however, use the UPDATE statement and the Collection-Derived-Table segment to insert new field values into a row host variable, if you specify a value for every field in the row. For example, the following code fragment inserts new field values into the row variable myrect and then inserts this row variable into the database:
EXEC SQL update table(:myrect) set x=3, y=4, length=12, width=6; EXEC SQL insert into rectangles values (72, :myrect);
If the row variable is an untyped variable, you must use a SELECT statement before the UPDATE so that ESQL/C can determine the data types of the fields. An UPDATE of fields in a row variable cannot include a WHERE clause. The row variable can store the field values of the row, but it has no intrinsic connection with a database column. Once the row variable contains the correct field values, you must then save the variable into the ROW column with one of the following SQL statements: v To update the ROW column in the table with contents of the row variable, use an UPDATE statement on a table or view name and specify the row variable in the SET clause. (For more information, see Updating ROW-Type Columns (IDS) on page 2-643.) v To insert a row into a column, use the INSERT statement on a table or view name and specify the row variable in the VALUES clause. (For more information, see Inserting Values into ROW-Type Columns (IDS) on page 2-401.) For examples of SPL ROW variables, see the IBM Informix Guide to SQL: Tutorial. For more information on using ESQL/C row variables, see the discussion of complex data types in the IBM Informix ESQL/C Programmer's Manual.
Related Information
Related statements: DECLARE, INSERT, OPEN, SELECT, and FOREACH For a task-oriented discussion of the UPDATE statement, see the IBM Informix Guide to SQL: Tutorial. For a discussion of the GLS aspects of the UPDATE statement, see the IBM Informix GLS User's Guide. For information on how to access row and collections with ESQL/C host variables, see the discussion of complex data types in the IBM Informix ESQL/C Programmer's Manual.
2-648
UPDATE STATISTICS
Use the UPDATE STATISTICS statement to perform any of the following tasks: v Calculate the distribution of column values. v Update system catalog tables that the database server uses to optimize queries. v Force reoptimization of SPL routines. v Convert existing indexes when you upgrade the database server. This statement is an extension to the ANSI/ISO standard for SQL.
Syntax
UPDATE STATISTICS
LOW Table and Column Scope MEDIUM HIGH DROP DISTRIBUTIONS (1) RESOLUTION Clause Table and Column Scope (2) Routine Statistics
ONLY
( 'owner.'
Notes: 1 2
Element column synonym Description A column in table or synonym Synonym for a table whose statistics are to be updated Table for which statistics are to be updated
See Resolution Clause on page 2-654 See Routine Statistics on page 2-655
Restrictions Must exist. With MEDIUM or HIGH keywords, column cannot be of BYTE or TEXT data type The synonym and the table to which it points must exist in the current database Must exist in the current database or be a temporary table created in the current session Syntax Identifier on page 5-22 Database Object Name on page 5-17 Database Object Name on page 5-17
table
Usage
Use the UPDATE STATISTICS statement to update system catalog information that the query optimizer uses for operations on objects in the local database.
2-649
UPDATE STATISTICS
You cannot, however, update the statistics for a table or the query plan of a UDR that is external to the current database. That is, the database server ignores remote database objects when executing the UPDATE STATISTICS statement.
2-650
UPDATE STATISTICS
Table Hierarchy employee
sales_rep
us_sales_rep
When the following statement executes, the database server generates statistics on both the sales_rep and us_sales_rep tables:
UPDATE STATISTICS FOR TABLE sales_rep
In contrast, the following example generates statistical data for each column in table sales_rep but does not act on tables employee or us_sales_rep:
UPDATE STATISTICS FOR TABLE ONLY (sales_rep)
Because neither of the previous examples specified the level at which to update the statistical data, the database server uses the LOW mode by default. 3 3 3 3 3 3 3 3 3 3 3
If you include no column name in the FOR TABLE clause, then distributions are calculated for all columns of the specified table, using to the LOW, MEDIUM, or HIGH mode and the RESOLUTION percentage that you request. . Distributions are not calculated for BYTE or TEXT columns. See also Updating Statistics for Columns of User-Defined Types (IDS) on page 2-652 for UPDATE STATISTICS restrictions on columns that store UDTs.
2-651
UPDATE STATISTICS
Requirements
UPDATE STATISTICS collects statistics for opaque data types only if you have defined user-defined routines for statcollect( ), statprint( ), and the selectivity functions. You must have Usage privilege on these routines. In some cases, UPDATE STATISTICS also requires an sbspace as specified by the SYSSBSPACENAME configuration parameter. For information about how to provide statistical data for a column, refer to the IBM Informix DataBlade API Programmer's Guide. For information about SYSSBSPACENAME, refer to your IBM Informix Administrator's Reference.
Because the LOW mode option does not update data in the sysdistrib system catalog table, all distributions associated with the customer table remain intact, even those that already exist on the customer_num column.
2-652
UPDATE STATISTICS
You must have the DBA privilege or be owner of the table to use this option. The following example shows how to remove distributions for the customer_num column in the customer table:
UPDATE STATISTICS LOW FOR TABLE customer (customer_num) DROP DISTRIBUTIONS
As the example shows, you drop the distribution data at the same time you update the statistical data that the low mode option generates.
2-653
UPDATE STATISTICS
tables. The HIGH keyword might scan each table several times (for each column). To minimize processing time, specify a table name and column names within that table. You must have the DBA privilege or be the owner of the table to create HIGH distributions. For more information on the MEDIUM and HIGH mode options, see the Resolution Clause on page 2-654.
Resolution Clause
Use the Resolution clause to adjust the size of the distribution bin, designate whether or not to avoid calculating data on indexes, and with the MEDIUM mode, to adjust the confidence level. RESOLUTION Clause:
RESOLUTION Clause for MEDIUM Mode RESOLUTION Clause for HIGH Mode
Notes: 1
Element confidence_level
Description Estimated fraction of the time that sampling in MEDIUM mode should produce same results as the exact HIGH mode. Default level is 0.95. Percentage of sample in each bin of distribution Default is 2.5 for MEDIUM and 0.5 for HIGH.
percent
A distribution is a mapping of the data in a column into a set of column values, ordered by magnitude or by collation. The range of these sample values is partitioned into disjunct intervals, called bins, each containing an approximately equal portion of the sample of column values. For example, if one bin holds 2 percent of the data, 50 such intervals hold the entire sample. Some statistical texts call these bins equivalence categories. Each contains a subset of the range of the data values that are sampled from the column.
2-654
UPDATE STATISTICS
The optimizer estimates the effect of a WHERE clause by examining, for each column included in the WHERE clause, the proportionate occurrence of data values contained in the column. You cannot create distributions for BYTE or TEXT columns. If you include a BYTE or TEXT column in an UPDATE STATISTICS statement that specifies medium or high distributions, no distributions are created for those columns. Distributions are constructed for other columns in the list, however, and the statement does not return an error. Columns of the VARCHAR data type do not use overflow bins, even when multiple bins are being used for duplicate values. The amount of space that the DBUPSPACE environment variable specifies determines the number of times the database server scans the designated table to construct a distribution.
Routine Statistics
3 3 3 3 Before the database server executes a new SPL routine the first time, it optimizes the statements in the SPL routine. Optimization makes the code depend on the structure of tables referenced by the routine. If a table schema changes after the routine is optimized, but before it is executed, the routine can fail with an error. To avoid this error after DDL operations, or to reoptimize SPL routines after table distributions might have been modified by DML operations, you can use the Routine Statistics segment of UPDATE STATISTICS to update the optimized execution plans for SPL routines in the sysprocplan system catalog table. Routine Statistics:
FOR PROCEDURE routine (1) PROCEDURE FUNCTION ROUTINE routine ( (2) Routine Parameter List (3) Specific Name )
SPECIFIC
2-655
UPDATE STATISTICS
Notes: 1 2 3 Dynamic Server only See Routine Parameter List on page 5-61 See Specific Name on page 5-68
Description Name declared for a SPL routine in a CREATE FUNCTION or CREATE PROCEDURE statement Restrictions Must reside in the database. In ANSI-compliant databases, qualify routine with owner if you are not owner. Syntax Database Object Name on page 5-17
Element routine
The following table explains the keywords of the Routine Statistics segment. Keyword SPECIFIC FUNCTION Which Execution Plan is Reoptimized The plan for the SPL routine called specific name The plan for any SPL function with the specified name (and with parameter types that match routine parameter list, if supplied) The plan for any SPL procedure with the specified name (and parameter types that match routine parameter list, if supplied) The plan for SPL functions and procedures with the specified name (and parameter types that match routine parameter list, if supplied)
PROCEDURE
ROUTINE
If you specify no routine, the execution plans are reoptimized for all SPL routines in the current database. 3 3 3 The database server keeps a list of tables that the SPL routine references explicitly. Whenever an explicitly referenced table is modified, the database server reoptimizes the procedure the next time the procedure is executed. The sysprocplan system catalog table stores execution plans for SPL routines. Two actions can update the sysprocplan system catalog table: v Execution of an SPL routine that uses a modified table v The UPDATE STATISTICS statement If you change a table that an SPL routine references, you can run UPDATE STATISTICS to reoptimize the procedures that reference the table, rather than waiting until the next time an SPL routine that uses the table executes. (If a table that an SPL routine references is dropped, however, running UPDATE STATISTICS cannot prevent the SPL routine from failing with an error.) 3 3 3 3 3 3 3 3
2-656
UPDATE STATISTICS
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 Each SPL routine is optimized the first time that it is run (not when it is created). This behavior means that an SPL routine might succeed the first time it is run but fail later under virtually identical circumstances, if the schema of an indirectly referenced table has been changed. The failure of an SPL routine can also be intermittent, because failure during one execution forces an internal warning to reoptimize the procedure before the next execution. You can use either of two methods to recover from this error: v Issue UPDATE STATISTICS to force reoptimization of the routine. v Rerun the routine. To prevent this error, you can force reoptimization of the SPL routine. To force reoptimization, execute the following statement:
UPDATE STATISTICS FOR PROCEDURE routine
You can add this statement to your program in either of the following ways: v Issue UPDATE STATISTICS after each statement that changes the mode of an object. v Issue UPDATE STATISTICS before each invocation of the SPL routine For efficiency, you can put the UPDATE STATISTICS statement with the action that occurs less frequently in the program (change of object mode or execution of the procedure). In most cases, the action that occurs less frequently in the program is the change of object mode. When you follow this method of recovering from this error, you must execute UPDATE STATISTICS for each procedure that indirectly references the altered tables unless the procedure also references the tables explicitly. You can also recover from error -710 after an indirectly referenced table is altered simply by re-executing the SPL routine. The first time that the stored procedure fails, the database server marks the procedure as in need of reoptimization. The next time that you run the procedure, the database server reoptimizes the procedure before running it. Running the SPL routine twice, however, might be neither practical nor safe. A safer choice is to use the UPDATE STATISTICS statement to force reoptimization of the procedure.
Performance
The more specific you make the list of objects that UPDATE STATISTICS examines, the faster it completes execution. Limiting the number of columns distributed
Chapter 2. SQL Statements
2-657
UPDATE STATISTICS
speeds the update. Similarly, precision affects the speed of the update. If all other keywords are the same, LOW works fastest, but HIGH examines the most data.
Related Statements
Related statements: SET EXPLAIN and SET OPTIMIZATION For a discussion of the performance implications of UPDATE STATISTICS, see your IBM Informix Performance Guide. For a discussion of how to use the dbschema utility to view distributions created with UPDATE STATISTICS, see the IBM Informix Migration Guide.
2-658
WHENEVER
Use the WHENEVER statement to trap exceptions that occur during the execution of SQL statements. Use this statement only with ESQL/C.
Syntax
WHENEVER SQLERROR NOT FOUND (1) SQLWARNING (1) ERROR CONTINUE GOTO GO TO :label (1) label CALL routine STOP
Notes: 1
Element label routine Description Statement label to which program control transfers when an exception occurs Name of a user-defined routine (UDR) to be invoked when an exception occurs
Informix extension
Restrictions Must exist in the same source-code module. No arguments; UDR must exist at compile time. Syntax Language-specific Database Object Name on page 5-17
Usage
The WHENEVER statement is equivalent to placing an exception-checking routine after every SQL statement. The following table summarizes the types of exceptions for which you can check with the WHENEVER statement.
Type of Exception Errors Warnings Not Found or End of Data WHENEVER Keyword SQLERROR or ERROR SQLWARNING Keyword on page 2-661 NOT FOUND Keywords on page 2-661 For More Information SQLERROR Keyword on page 2-661
Programs that do not use the WHENEVER statement do not automatically abort when an exception occurs. Such programs must explicitly check for exceptions and take whatever corrective action their logic specifies. If you do not check for exceptions, the program simply continues running. If errors occur, however, the program might not perform its intended purpose. The first keyword that follows WHENEVER specifies some type of exceptional condition; the last part of the statement specifies some action to take when the exception is encountered (or no action, if CONTINUE is specified). The following table summarizes possible actions that WHENEVER can specify.
2-659
WHENEVER
Type of Action Continue program execution WHENEVER Keyword For More Information CONTINUE Keyword on page 2-661 STOP Keyword on page 2-662 GOTO GO TO CALL Clause on page 2-662 GOTO Keyword on page 2-662
Stop program execution Transfer control to a specified label Transfer control to a UDR
2-660
WHENEVER
SQLERROR Keyword
If you use the SQLERROR keyword, any SQL statement that encounters an error is handled as the WHENEVER SQLERROR statement directs. If an error occurs, the sqlcode variable (sqlca.sqlcode, SQLCODE) is set to a value less than zero (0) and the SQLSTATE variable is set to a class code with a value greater than 02. The next example terminates program execution if an SQL error is detected:
WHENEVER SQLERROR STOP
If you do not include any WHENEVER SQLERROR statements in a program, the default action for WHENEVER SQLERROR is CONTINUE.
ERROR Keyword
Within the WHENEVER statement (and only in this context), the keyword ERROR is a synonym for the SQLERROR keyword.
SQLWARNING Keyword
If you use the SQLWARNING keyword, any SQL statement that generates a warning is handled as the WHENEVER SQLWARNING statement directs. If a warning occurs, the first field (sqlca.sqlwarn.sqlwarn0) of the warning structure in SQLCA is set to W, and the SQLSTATE variable is set to a class code of 01. Besides the first field of the warning structure, a warning also sets an additional field to W. The field that is set indicates what type of warning occurred. The next statement causes execution to stop if a warning condition exists:
WHENEVER SQLWARNING STOP
If you do not use any WHENEVER SQLWARNING statements in a program, the default action for WHENEVER SQLWARNING is CONTINUE.
If you do not use any WHENEVER NOT FOUND statements in a program, the default action for WHENEVER NOT FOUND is CONTINUE.
CONTINUE Keyword
Use the CONTINUE keyword to instruct the program to ignore the exception and to continue execution at the next statement after the SQL statement. The default
2-661
WHENEVER
action for all exceptions is CONTINUE. You can use this keyword to turn off a previously specified action for an exceptional condition.
STOP Keyword
Use the STOP keyword to instruct the program to stop execution when the specified exception occurs. The following statement halts execution of an ESQL/C program each time that an SQL statement generates a warning:
EXEC SQL WHENEVER SQLWARNING STOP;
GOTO Keyword
Use the GOTO clause to transfer control to the statement that the label identifies when a specified exception occurs. The GOTO and GO TO keywords are ANSI-compliant syntax for this feature of embedded SQL languages like ESQL/C. The following ESQL/C code fragment shows a WHENEVER statement that transfers control to the label missing each time that the NOT FOUND condition occurs:
query_data() ... EXEC SQL WHENEVER NOT FOUND GO TO missing; ... EXEC SQL fetch lname into :lname; ... missing: printf("No Customers Found\n");
Within the scope of the WHENEVER GOTO statement, you must define the labeled statement in each routine that contains SQL statements. If your program contains more than one user-defined function, you might need to include the labeled statement and its code in each function. If the preprocessor encounters an SQL statement within the scope of a WHENEVER ... GOTO statement, but within a routine that does not have the specified label, the preprocessor tries to insert the code associated with the labeled statement, but generates an error when it cannot find the label. To correct this error, either put a labeled statement with the same label name in each UDR, or issue another WHENEVER statement to reset the error condition, or use the CALL clause to call a separate function.
CALL Clause
Use the CALL clause to transfer program control to the specified UDR when the specified type of exception occurs. Do not include parentheses after the UDR name. The following WHENEVER statement causes the program to call the error_recovery() function if the program detects an error:
EXEC SQL WHENEVER SQLERROR CALL error_recovery;
When the UDR returns, execution resumes at the next statement after the line that is causing the error. If you want to halt execution when an error occurs, include statements that terminate the program as part of the specified UDR. Observe the following restrictions on the specified routine: v The UDR cannot accept arguments nor can it return values. If it needs external information, use global variables or the WHENEVER ... GOTO option to transfer program control to a label that calls the UDR.
2-662
WHENEVER
v You cannot specify the name of an SPL routine in the CALL clause. To call an SPL routine, use the CALL clause to invoke a UDR that contains the EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement. v Make sure that all functions within the scope of WHENEVER ... CALL statements can find a declaration of the specified function.
Related Statements
Related statements: EXECUTE FUNCTION, EXECUTE PROCEDURE, and FETCH For discussions on exception handling and error checking, see the IBM Informix ESQL/C Programmer's Manual.
2-663
2-664
In This Chapter
You can use Stored Procedure Language (SPL) statements to write SPL routines (formerly referred to as stored procedures), and you can store these routines in the database as user-defined routines (UDRs). SPL routines are effective tools for controlling SQL activity. This chapter contains descriptions of the SPL statements. The description of each statement includes the following information: v A brief introduction that explains the effect of the statement v A syntax diagram that shows how to enter the statement correctly v A syntax table that explains each input parameter in the syntax diagram v Rules of usage, including examples that illustrate these rules If a statement is composed of multiple clauses, the statement description provides information about each clause. For an overview of the SPL language and task-oriented information about creating and using SPL routines, see the IBM Informix Guide to SQL: Tutorial. In Extended Parallel Server, to create an SPL function you must use the CREATE PROCEDURE statement or the CREATE PROCEDURE FROM statement. Extended Parallel Server does not support the CREATE FUNCTION statement nor the CREATE FUNCTION FROM statement. In Dynamic Server, for backward compatibility, you can create an SPL function with the CREATE PROCEDURE or CREATE PROCEDURE FROM statement. For external functions, you must use the CREATE FUNCTION or CREATE FUNCTION FROM statement. It is recommended that you use the CREATE FUNCTION or CREATE FUNCTION FROM statement when you create new user-defined functions. The SPL language does not support dynamic SQL. You cannot include any of the SQL statements that Chapter 1, Overview of SQL Syntax classifies as Dynamic Management Statements within an SPL routine.
3-1
CALL
CALL
Use the CALL statement to execute a user-defined routine (UDR) from within an SPL routine.
Syntax
CALL procedure ( , (1) Argument , function ( , (1) Argument routine_var , RETURNING data_var ) RETURNING data_var )
Notes: 1
Element data_var function, procedure routine_var Description Variable to receive the values function returns User-defined function or procedure Variable that contains the name of a UDR
Usage
The CALL statement invokes a UDR. The CALL statement is identical in behavior to the EXECUTE PROCEDURE and EXECUTE FUNCTION statements, but you can only use CALL from within an SPL routine. You can use CALL in an ESQL/C program or with DBAccess, but only if the statement is in an SPL routine that the program or DBAccess executed. If you CALL a user-defined function, you must specify a RETURNING clause.
Specifying Arguments
If a CALL statement contains more arguments than the UDR expects, you receive an error. If CALL specifies fewer arguments than the UDR expects, the arguments are said to be missing. The database server initializes missing arguments to their corresponding default values. (See CREATE PROCEDURE on page 2-145 and CREATE FUNCTION on page 2-107.) This initialization occurs before the first
3-2
CALL
executable statement in the body of the UDR. If missing arguments do not have default values, they are initialized to the value of UNDEFINED. An attempt to use any variable of UNDEFINED value results in an error. In each UDR call, you have the option of specifying parameter names for the arguments you pass to the UDR. Each of the following examples are valid for a UDR that expects character arguments named t, n, and d, in that order:
CALL add_col (t=customer, n = newint, d =integer); CALL add_col(customer,newint,integer);
The first routine call (no_args) expects no returned values. The second routine call is to a function (yes_args), which expects three returned values. The not_much() procedure declares three integer variables (i, j, and k) to receive the returned values from yes_args.
3-3
CASE
Use the CASE statement when you need to take one of many branches depending on the value of an SPL variable or a simple expression. The CASE statement is a fast alternative to the IF statement. Only Extended Parallel Server supports the CASE statement.
Syntax
CASE value_expr
(1) ELSE Statement Block (1) WHEN constant_expr THEN Statement Block (1) ELSE END CASE Statement Block
Notes: 1
Element constant_expr Description Expression that specifies a literal value Expression that returns a value
value_expr
Usage
You can use the CASE statement to create a set of conditional branches within an SPL routine. The WHEN and the ELSE clauses are optional, but you must include at least one of them. If you specify no WHEN and no ELSE clause, you receive a syntax error. 3 3 3 3 3 Do not confuse the CASE statement with CASE expressions of SQL. (For Extended Parallel Server, CASE expressions support the same keywords as the CASE statement, but use different syntax and semantics to evaluate conditions that you specify and return a single value or NULL, as described in CASE Expressions on page 4-49.)
3-4
CASE
v After the database server executes the statement block that follows the THEN keyword, it executes the statement that follows the CASE statement in the SPL routine. v If the value of the value_expr parameter does not match the literal value specified in the constant_expr parameter of any WHEN clause, and if the CASE statement includes an ELSE clause, the database server executes the statement block that follows the ELSE keyword. v If the value of the value_expr parameter does not match the literal value specified in the constant_expr parameter of any WHEN clause, and if the CASE statement does not include an ELSE clause, the database server executes the statement that follows the CASE statement in the SPL routine. v If the CASE statement includes an ELSE clause but not a WHEN clause, the database server executes the statement block that follows the ELSE keyword. The statement block that follows the THEN or ELSE keywords can include any SQL statement or SPL statement that is valid in the statement block of an SPL routine. For more information, see Statement Block on page 5-69.
Related Statements
IF
3-5
CONTINUE
Use the CONTINUE statement to start the next iteration of the innermost FOR, WHILE, or FOREACH loop.
Syntax
CONTINUE FOR WHILE FOREACH ;
Usage
When control of execution passes to a CONTINUE statement, the SPL routine skips the rest of the statements in the innermost loop of the indicated type. Execution continues at the top of the loop with the next iteration. In the following example, the loop_skip function inserts values 3 through 15 into the table testtable. The function also returns values 3 through 9 and 13 through 15 in the process. The function does not return the value 11 because it encounters the CONTINUE FOR statement. The CONTINUE FOR statement causes the function to skip the RETURN WITH RESUME statement:
CREATE FUNCTION loop_skip() RETURNING INT; DEFINE i INT; ... FOR i IN (3 TO 15 STEP 2) INSERT INTO testtable values(i, null, null); IF i = 11 CONTINUE FOR; END IF; RETURN i WITH RESUME; END FOR; END FUNCTION;
Just as with EXIT (EXIT on page 3-16), the FOR, WHILE, or FOREACH keyword must immediately follow CONTINUE to specify the type of loop. The CONTINUE statement generates errors if it cannot find the specified loop.
3-6
DEFINE
Use the DEFINE statement to declare local variables that an SPL routine uses, or to declare global variables that can be shared by several SPL routines.
Syntax
DEFINE
, (1) GLOBAL , SPL_var data_type REFERENCES LIKE BYTE TEXT . column SPL_var data_type DEFAULT REFERENCES BYTE TEXT Default Value DEFAULT NULL ;
Notes: 1 2 3
Element column data_type distinct_type opaque_type SPL_var synonym, table, view Description Column name Type of SPL_var A distinct type An opaque type New SPL variable Name of a table, view, or synonym
See 3-9 Dynamic Server only See Subset of Complex Data Types (IDS) on page 3-12
Restrictions Must already exist in the table or view See Declaring Global Variables on page 3-8. Must already be defined in the database Must already be defined in the database Must be unique within statement block Synonym and the table or view to which it points must exist when DEFINE is issued Syntax Data Type on page 4-18 Data Type on page 4-18 Identifier on page 5-22 Identifier on page 5-22 Identifier on page 5-22 Database Object Name on page 5-17
Usage
The DEFINE statement is not an executable statement. The DEFINE statement must appear after the routine header and before any other statements. If you
Chapter 3. SPL Statements
3-7
DEFINE
declare a local variable (by using DEFINE without the GLOBAL keyword), its scope of reference is the statement block in which it is defined. You can use the variable within the statement block. Another variable outside the statement block with a different definition can have the same name. A variable with the GLOBAL keyword is global in scope and is available outside the statement block and to other SPL routines. Global variables can be any built-in data type except SERIAL, SERIAL8, TEXT, BYTE, CLOB, or BLOB. Local variables can be any built-in data type except SERIAL, SERIAL8, TEXT, or BYTE. If column is of the SERIAL or SERIAL8 data type, declare an INT or INT8 variable (respectively) to store its value.
Redeclaration or Redefinition
If you define the same variable twice in the same statement block, you receive an error. You can redefine a variable within a nested block, in which case it temporarily hides the outer declaration. This example produces an error:
CREATE PROCEDURE example1() DEFINE n INT; DEFINE j INT; DEFINE n CHAR (1); -- redefinition produces an error
Redeclaration is valid in the following example. Within the nested statement block, n is a character variable. Outside the block, n is an integer variable.
CREATE PROCEDURE example2() DEFINE n INT; DEFINE j INT; ... BEGIN DEFINE n CHAR (1); -- character n masks global integer variable ... END
3-8
DEFINE
Element data_type SPL_var Description Type of SPL_var New SPL variable Restrictions See Declaring Global Variables on page 3-8. Must be unique within statement block Syntax Data Type on page 4-18 Identifier on page 5-22
The GLOBAL keyword indicates that the variables that follow have a scope of reference that includes all SPL routines that run in a given DB-Access or SQL API session. The data types of these variables must match the data types of variables in the global environment. The global environment is the memory that is used by all the SPL routines that run in a given DB-Access or SQL API session. The values of global variables are stored in memory. SPL routines that are running in the current session share global variables. Because the database server does not save global variables in the database, the global variables do not remain when the current session closes. The first declaration of a global variable establishes the variable in the global environment; subsequent global declarations simply bind the variable to the global environment and establish the value of the variable at that point. The following example shows two SPL procedures, proc1 and proc2; each has defined the global variable gl_out: v SPL procedure proc1
CREATE PROCEDURE proc1() ... DEFINE GLOBAL gl_out INT DEFAULT 13; ... LET gl_out = gl_out + 1; END PROCEDURE;
If proc1 is called first, gl_out is set to 13 and then incremented to 14. If proc2 is then called, it sees that gl_out is already defined, so the default value of 23 is not applied. Then, proc2 assigns the existing value of 14 to tmp. If proc2 had been called first, gl_out would have been set to 23, and 23 would have been assigned to tmp. Later calls to proc1 would not apply the default of 13. Databases of different database server instances do not share global variables, but all the databases of the same database server instance can share global SPL variables in a single session. The database server and any application development tools, however, do not share global variables.
Default Value
Global variables can have literal, NULL, or system constant default values.
3-9
DEFINE
Default Value:
(1) Literal Number (2) Quoted String (3) Literal Interval (4) Literal Datetime CURRENT (5) DATETIME Field Qualifier DBSERVERNAME SITENAME TODAY USER NULL
Notes: 1 2 3 4 5 See Literal Number on page 4-137 See Quoted String on page 4-142 See Literal INTERVAL on page 4-135 See Literal DATETIME on page 4-132 See DATETIME Field Qualifier on page 4-32
If you specify a default value, the global variable is initialized with the specified value.
CURRENT
CURRENT is a valid default only for a DATETIME variable. If the YEAR TO FRACTION is its declared precision, no qualifier is needed. Otherwise, you must specify the same DATETIME qualifier when CURRENT is the default, as in the following example of a DATETIME variable:
DEFINE GLOBAL d_var DATETIME YEAR TO MONTH DEFAULT CURRENT YEAR TO MONTH;
USER
If you use the value that USER returns as the default, the variable must be defined as a CHAR, VARCHAR, NCHAR, or NVARCHAR data type. It is recommended that the length of the variable be at least 32 bytes. You risk getting an error message during INSERT and ALTER TABLE operations if the length of the variable is too small to store the default value.
TODAY
If you use TODAY as the default, the variable must be a DATE value. (See Constant Expressions on page 4-53 for descriptions of TODAY and of the other system constants that can appear in the Default Value clause.)
3-10
DEFINE
CREATE PROCEDURE use_text() DEFINE i INT; DEFINE GLOBAL l_blob REFERENCES TEXT DEFAULT NULL; ... END PROCEDURE
Here the REFERENCES keyword is required, because the DEFINE statement cannot declare a BYTE or TEXT data type directly; the l_blob variable is a pointer to a TEXT value that is stored in the global environment.
SITENAME or DBSERVERNAME
If you use the SITENAME or DBSERVERNAME keyword as the default, the variable must be a CHAR, VARCHAR, NCHAR, NVARCHAR, or LVARCHAR data type. Its default value is the name of the database server at runtime. It is recommended that the size of the variable be at least 128 bytes long. You risk getting an error message during INSERT and ALTER TABLE operations if the length of the variable is too small to store the default value. The following example uses the SITENAME keyword to specify a default value. This example also initializes a global BYTE variable to NULL:
CREATE PROCEDURE gl_def() DEFINE GLOBAL gl_site CHAR(200) DEFAULT SITENAME; DEFINE GLOBAL gl_byte REFERENCES BYTE DEFAULT NULL; ... END PROCEDURE
3-11
DEFINE
Notes: 1 2
Element column data_type distinct_type opaque_type SPL_var synonym, table, view Description Column name Type of SPL_var A distinct type An opaque type New SPL variable Name of a table, view, or synonym
Local variables do not support default values. The following example shows some typical definitions of local variables:
CREATE PROCEDURE def_ex() DEFINE i INT; DEFINE word CHAR(15); DEFINE b_day DATE; DEFINE c_name LIKE customer.fname; DEFINE b_text REFERENCES TEXT; END PROCEDURE
Element data_type
Description Type of elements of a collection or of fields of an unnamed ROW type Field of unnamed ROW Named ROW data type
Restrictions
Syntax
Must match the data type of the values that the Data Type on page variable will store. Cannot be SERIAL, SERIAL8, 4-18 TEXT, BYTE, CLOB, or BLOB. Must exist in the database Must exist in the database Identifier on page 5-22 Identifier on page 5-22
field row
3-12
DEFINE
With variable c, both the INTEGER values in the SET and the SET values in the LIST are defined as NOT NULL. You can define collection variables with nested complex types to hold matching nested complex type data. Any type or depth of nesting is allowed. You can nest ROW types within collection types, collection types within ROW types, collection types within collection types, ROW types within collection and ROW types, and so on. If you declare a variable as COLLECTION type, the variable acquires varying data type declarations if it is reassigned within the same statement block, as in the following example:
DEFINE a COLLECTION; LET a = setB; ... LET a = listC;
In this example, varA is a generic collection variable that changes its data type to the data type of the currently assigned collection. The first LET statement makes varA a SET variable. The second LET statement makes varA a LIST variable.
3-13
DEFINE
A named ROW variable holds named ROW types of the same type in the declaration of the variable. To define a variable that will hold data stored in an unnamed ROW type, use the ROW keyword followed by the fields of the ROW type, as in:
DEFINE area ROW ( x int, y char(10) );
Unnamed ROW types are type-checked only by structural equivalence. Two unnamed ROW types are considered equivalent if they have the same number of fields, and if the fields have the same type definitions. Therefore, you could fetch either of the following ROW types into the variable area defined above:
ROW ( a int, b char(10) ) ROW ( area int, name char(10) )
ROW variables can have fields, just as ROW types have fields. To assign a value to a field of a ROW variable, use the qualifier notation variableName.fieldName, followed by an expression, as in the following example:
CREATE ROW TYPE rectangle_t (start point_t, length real, width real); DEFINE r rectangle_t; -- Define a variable of a named ROW type LET r.length = 45.5; -- Assign a value to a field of the variable
When you assign a value to a ROW variable, you can use any valid expression.
3-14
DEFINE
The DEFINE statement does not support a FUNCTION keyword. Use the PROCEDURE keyword, whether you are calling a user-defined procedure or a user-defined function. Declaring a variable as PROCEDURE type indicates that in the current statement scope, the variable is not a call to a built-in function. For example, the following statement defines length as an SPL routine, not as the built-in LENGTH function:
DEFINE length PROCEDURE; ... LET x = length (a,b,c)
This definition disables the built-in LENGTH function within the scope of the statement block. You would use such a definition if you had already created a user-defined routine with the name length. If you create an SPL routine with the same name as an aggregate function (SUM, MAX, MIN, AVG, COUNT) or with the name extend, you must qualify the routine name with the owner name.
If you pass a variable of BYTE or TEXT data type to an SPL routine, the data is passed to the database server and stored in the root dbspace or dbspaces that the DBSPACETEMP environment variable specifies, if it is set. You do not need to know the location or name of the file that holds the data. BYTE or TEXT manipulation requires only the name of the BYTE or TEXT variable as it is defined in the routine.
3-15
EXIT
The EXIT statement terminates execution of a FOR, WHILE, or FOREACH loop.
Syntax
EXIT FOR WHILE FOREACH ;
Usage
The EXIT statement marks the end of a FOR, WHILE, or FOREACH statement, causing the innermost loop of the specified type (FOR, WHILE, or FOREACH) to terminate. Execution resumes at the first statement outside the loop. The FOR, WHILE, or FOREACH keyword must immediately follow EXIT. If the database server cannot find the specified loop, the EXIT statement fails. If EXIT is used outside any FOR, WHILE, or FOREACH loop, it generates errors. The following example uses an EXIT FOR statement. In the FOR loop, when j becomes 6, the IF condition i = 5 in the WHILE loop is true. The FOR loop stops executing, and the SPL procedure continues at the next statement outside the FOR loop (in this case, the END PROCEDURE statement). In this example, the procedure ends when j equals 6:
CREATE PROCEDURE ex_cont_ex() DEFINE i,s,j, INT; FOR j = 1 TO 20 IF j > 10 THEN CONTINUE FOR; END IF LET i,s = j,0; WHILE i > 0 LET i = i -1; IF i = 5 THEN EXIT FOR; END IF END WHILE END FOR END PROCEDURE
3-16
FOR
Use the FOR statement to initiate a controlled (definite) loop when you want to guarantee termination of the loop. The FOR statement uses expressions or range operators to specify a finite number of iterations for a loop.
Syntax
, FOR loop_var IN ( Expression Range , expression Expression Range )
END FOR ;
Expression Range:
left_expression TO right_expression STEP increment_expr
Notes: 1
Element expression increment_expr left_expression loop_var right_expression
Description Value to compare with loop_var Positive or negative value by which loop_var is incremented Starting expression of a range Variable that determines how many times the loop executes Ending expression in the range
Usage
The database server evaluates all expressions before the FOR statement executes. If one or more of the expressions are variables whose values change during the loop, the change has no effect on the iterations of the loop. You can use the output from a SELECT statement as the expression. The FOR loop terminates when loop_var is equal to the values of each element in the expression list or range in succession, or when it encounters an EXIT FOR statement. An error is issued, however, if an assignment within the body of the FOR statement attempts to modify the value of loop_var.
Chapter 3. SPL Statements
3-17
FOR
The size of right_expression relative to left_expression determines if the range is stepped through by positive or negative increments.
If you omit the STEP option, the database server gives increment_expr the value of -1 if right_expression is less than left_expression, or +1 if right_expression is more than left_expression. If increment_expr is specified, it must be negative if right_expression is less than left_expression, or positive if right expression is more than left_expression. The two statements in the following example are equivalent. In the first statement, the STEP increment is explicit. In the second statement, the STEP increment is implicitly 1:
FOR index IN (12 TO 21 STEP 1) -- statement block END FOR FOR index = 12 TO 21 -- statement block END FOR
The database server initializes the value of loop_var to the value of left_expression. In subsequent iterations, the server adds increment_expr to the value of loop_var and checks increment_expr to determine whether the value of loop_var is still between left_expression and right_expression. If so, the next iteration occurs. Otherwise, an exit from the loop takes place. Or, if you specify another range, the variable takes on the value of the first element in the next range. Specifying Two or More Ranges in a Single FOR Statement: The following example shows a statement that traverses a loop forward and backward and uses different increment values for each direction:
FOR index_var IN (15 to 21 STEP 2, 21 to 15 STEP -3) -- statement body END FOR
3-18
FOR
The expressions in the IN list do not need to be numeric values, as long as you do not use range operators in the IN list. The following example uses a character expression list:
FOR c IN (hello, (SELECT name FROM t), world, v1, v2) INSERT INTO t VALUES (c); END FOR
The following FOR statement shows the use of a numeric expression list:
FOR index IN (15,16,17,18,19,20,21) -- statement block END FOR
Related Statements
FOREACH, WHILE
3-19
FOREACH
Use a FOREACH loop to select and manipulate more than one row.
Syntax
(1) FOREACH WITH HOLD cursor WITH HOLD Routine Call , INTO (2) Statement Block END FOREACH ; data_var FOR SELECT ... INTO Statement
Routine Call:
EXECUTE PROCEDURE procedure SPL_var function SPL_var function ( , (4) Argument FUNCTION )
(3)
Notes: 1 2 3 4 See Using a SELECT ... INTO Statement on page 3-21 See Statement Block on page 5-69 Dynamic Server only See Arguments on page 5-2
Description Restrictions Syntax Identifier on page 5-22 Identifier on page 5-22 Database Object Name on page 5-17 Identifier on page 5-22
Identifier that you supply as a name for Each cursor name within a routine this FOREACH loop must be unique SPL variable in the calling routine that receives the returned values SPL function or procedure to execute Data type of data_var must be appropriate for returned value Function or procedure must exist
SPL variable that contains the name of a Must be a CHAR, VARCHAR, routine to execute NCHAR, or NVARCHAR type
Usage
A FOREACH loop is the procedural equivalent of using a cursor. To execute a FOREACH statement, the database server takes these actions: 1. It declares and implicitly opens a cursor. 2. It obtains the first row from the query contained within the FOREACH loop, or else the first set of values from the called routine.
3-20
FOREACH
3. It assigns to each variable in the variable list the value of the corresponding value from the active set that the SELECT statement or the called routine creates. 4. It executes the statement block. 5. It fetches the next row from the SELECT statement or called routine on each iteration, and it repeats steps 3 and 4. 6. It terminates the loop when it finds no more rows that satisfy the SELECT statement or called routine. It closes the implicit cursor when the loop terminates. Because the statement block can contain additional FOREACH statements, cursors can be nested. No limit exists on the number of nested cursors. An SPL routine that returns more than one row, collection element, or set of values is called a cursor function. An SPL routine that returns only one row or value is a noncursor function. This SPL procedure illustrates FOREACH statements with a SELECT ... INTO clause, with an explicitly named cursor, and with a procedure call:
CREATE PROCEDURE foreach_ex() DEFINE i, j INT; FOREACH SELECT c1 INTO i FROM tab ORDER BY 1 INSERT INTO tab2 VALUES (i); END FOREACH FOREACH cur1 FOR SELECT c2, c3 INTO i, j FROM tab IF j > 100 THEN DELETE FROM tab WHERE CURRENT OF cur1; CONTINUE FOREACH; END IF UPDATE tab SET c2 = c2 + 10 WHERE CURRENT OF cur1; END FOREACH FOREACH EXECUTE PROCEDURE bar(10,20) INTO i INSERT INTO tab2 VALUES (i); END FOREACH END PROCEDURE; -- foreach_ex
A select cursor is closed when any of the following situations occur: v The cursor returns no further rows. v The cursor is a select cursor without a HOLD specification, and a transaction completes using COMMIT or ROLLBACK statements. v An EXIT statement executes, which transfers control out of the FOREACH statement. v An exception occurs that is not trapped inside the body of the FOREACH statement. (See ON EXCEPTION on page 3-31.) v A cursor in the calling routine that is executing this cursor routine (within a FOREACH loop) closes for any reason.
3-21
FOREACH
3-22
FOREACH
In this example, the SELECT statement selects one element at a time from the collection variable b into the element variable a. The projection list is an asterisk, because the collection variable b contains a collection of built-in types. The variable b is used with the TABLE keyword as a Collection-Derived Table. For more information, see Collection-Derived Table on page 5-5. The next example also shows how to fill a collection variable and then how to use a cursor to access individual elements. This example, however, uses a list of ROW-type fields in its projection list:
DEFINE employees employee_t; DEFINE n VARCHAR(30); DEFINE s INTEGER; SELECT emp_list into employees FROM dept_table WHERE dept_no = 1057; FOREACH cursor1 FOR SELECT name,salary INTO n,s FROM TABLE( employees ) AS e; ... END FOREACH;
Here the collection variable employees contains a collection of ROW types. Each ROW type contains the fields name and salary. The collection query selects one name and salary combination at a time, placing name into n and salary into s. The AS keyword declares e as an alias for the collection-derived table employees. The alias exists as long as the SELECT statement executes. Modifying Elements in a Collection Variable: To update an element of a collection within an SPL routine, you must first declare a cursor with the FOREACH statement. Then, within the FOREACH loop, select elements one at a time from the collection variable, using the collection variable as a collection-derived table in a SELECT query. When the cursor is positioned on the element to be updated, you can use the WHERE CURRENT OF clause, as follows: v The UPDATE statement with the WHERE CURRENT OF clause updates the value in the current element of the collection variable. v The DELETE statement with the WHERE CURRENT OF clause deletes the current element from the collection variable.
3-23
FOREACH
neither a function nor a procedure, it issues an error message. If you use EXECUTE FUNCTION, the database server looks for a user-defined function of the name you specify. If it does not find a function of that name, the database server issues an error message. An SPL function can return zero (0) or more values or rows. The data type and count of each variable in the variable list must match each value that the function returns.
Related Statements
FOR, WHILE
3-24
IF
Use the IF statement to create a logical branch within an SPL routine.
Syntax
(1) IF Condition THEN (2) IF Statement List
Notes: 1 2 See Condition on page 4-5 See IF Statement List on page 3-26
Usage
The database server processes the IF statement by the following steps: 1. If the condition that follows the IF keyword is true, any statements that follow the first THEN keyword of the IF statement execute, and the IF statement terminates. 2. If the result of the initial IF condition is false, but an ELIF clause exists, the database server evaluates the condition that follows the ELIF keyword. 3. If the result of the ELIF condition is true, any statements that follow the THEN keyword of the ELIF clause execute, and the IF statement terminates. 4. If the result of the condition in the first ELIF clause is also false, but one or more additional ELIF clauses exist, the database server evaluates the condition in the next ELIF clause, and proceeds as in the previous step if it is true. If it is false, the database server evaluates the condition in successive ELIF clauses, until it finds a condition that is true, in which case it executes the statement list that follows the THEN keyword of that ELIF clause, and the IF statement terminates. 5. If no condition in the IF statement is true, but the ELSE clause exists, statements that follow the ELSE keyword execute, and the IF statement terminates. 6. If none of the conditions in the IF statement are true, and no ELSE clause exists, the IF statement terminates without executing any statement list.
3-25
IF
ELIF Clause
Use the ELIF clause to specify one or more additional conditions to evaluate. If the IF condition is false, the ELIF condition is evaluated. If the ELIF condition is true, the statements that follow the THEN keyword in the ELIF clause execute. 3 3 If no statement follows the THEN keyword of the ELIF clause when the ELIF condition is true, program control passes from the IF statement to the next statement.
ELSE Clause
The ELSE clause executes if no true previous condition exists in the IF clause or any of the ELIF clauses. In the following example, the SPL function uses an IF statement with both an ELIF clause and an ELSE clause. The IF statement compares two strings. The function displays 1 to indicate that the first string comes before the second string alphabetically, or -1 if the first string comes after the second string alphabetically. If the strings are the same, a zero (0) is returned.
CREATE FUNCTION str_compare (str1 CHAR(20), str2 CHAR(20)) RETURNING INT; DEFINE result INT; IF str1 > str2 THEN LET result =1; ELIF str2 > str1 THEN LET result = -1; ELSE LET result = 0; END IF RETURN result; END FUNCTION -- str_compare
Conditions in an IF Statement
Just as in the WHILE statement, if any expression in the condition evaluates to NULL, then the condition cannot be true, unless you are explicitly testing for NULL using the IS NULL operator. The following rules summarize NULL values in conditions: 1. If the expression x evaluates to NULL, then x is not true by definition. Furthermore, NOT (x) is also not true . 2. IS NULL is the only operator that can return true for x. That is, x IS NULL is true, and x IS NOT NULL is not true. If an expression in the condition has an UNKNOWN value from an uninitialized SPL variable, the statement terminates and raises an exception.
IF Statement List
IF Statement List:
(1) BEGIN Statement Block (2) Subset of SPL Statements (3) Subset of SQL Statements ; END
3-26
IF
Notes: 1 2 3 See Statement Block on page 5-69 See Subset of SPL Statements Allowed in the IF Statement List See SQL Statements Not Valid in an IF Statement
The Subset of SPL Statements syntax diagram for the IF Statement List refers to the SPL statements that are listed in the preceding table.
Many of these statements are prohibited by the more general rule that the dynamic management statements of SQL are not valid within an SPL routine. You can use a SELECT statement only if you use the INTO TEMP clause to store the result set of the SELECT statement in a temporary table.
Related Statements
CASE, WHILE
3-27
LET
Use the LET statement to assign values to variables or to call a user-defined SPL routine and assign the returned value or values to SPL variables.
Syntax
, LET SPL_var = , function ( , (1) Argument , (2) Expression , (3) ( SELECT Statement ) ) ;
Notes: 1 2 3
Element function SPL_var Description SPL function to be invoked SPL variable to receive a value that the function, expression, or query returns
See Arguments on page 5-2 See Expression on page 4-34 See SELECT on page 2-479
Restrictions Must exist in the database Must be defined and in scope within the statement block Syntax Database Object Name on page 5-17 Identifier on page 5-22;
Usage
The LET statement can assign a value returned by an expression, function, or query to an SPL variable. At runtime, the value to be assigned is calculated first. The resulting value is cast to the data type of SPL_var, if possible, and the assignment occurs. If conversion is not possible, an error occurs, and the value of the variable remains undefined. (A LET operation that assigns a single value to a single SPL variable is called a simple assignment.) A compound assignment assigns multiple expressions to multiple SPL variables. The data types of expressions in the expression list do not need to match the data types of the corresponding variables in the variable list, because the database server automatically converts the data types. (For a detailed discussion of casting, see the IBM Informix Guide to SQL: Reference.) In multiple-assignment operations, the number of variables to the left of the equal ( = ) sign must match the number of values returned by the functions, expressions, and queries listed on the right of the equal ( = ) sign. The following example shows several LET statements that assign values to SPL variables:
3-28
LET
LET LET LET LET LET LET a = c + d ; a,b = c,d ; expire_dt = end_dt + 7 UNITS DAY; name = Brunhilda; sname = DBSERVERNAME; this_day = TODAY;
You cannot use multiple values to the right of the equal ( = ) sign to operate on other values. For example, the following statement is not valid:
LET a,b = (c,d) + (10,15); -- INVALID EXPRESSION
You cannot use a SELECT statement to make multiple values operate on other values. The following example is invalid:
LET a,b = (SELECT c1,c2 FROM t) + (10,15); -- INVALID CODE
Because a LET statement is equivalent to a SELECT ... INTO statement, the two statements in the following example have the same results: a=c and b=d:
CREATE PROCEDURE proof() DEFINE a, b, c, d INT; LET a,b = (SELECT c1,c2 FROM t WHERE id = 1); SELECT c1, c2 INTO c, d FROM t WHERE id = 1 END PROCEDURE
If the SELECT statement returns more than one row, you must enclose the SELECT statement in a FOREACH loop. For a description of SELECT syntax and usage, see SELECT on page 2-479.
3-29
LET
The following LET statement is not valid because it tries to add the output of two functions and then assign the sum to two variables, a and b.
LET a, b = func1() + func2(); -- INVALID CODE
You can easily split this LET statement into two valid LET statements:
LET a = (func1() + func2()); LET b = a; -- VALID CODE
A function called in a LET statement can have an argument of COLLECTION, SET, MULTISET, or LIST. You can assign the value that the function returns to a variable, for example:
LET d = function1(collection1); LET a = function2(set1);
In the first statement, the SPL function function1 accepts collection1 (that is, any collection data type) as an argument and returns its value to the variable d. In the second statement, the SPL function function2 accepts set1 as an argument and returns a value to the variable a.
3-30
ON EXCEPTION
Use the ON EXCEPTION statement to specify actions to be taken for any error, or for a list of one or more specified errors, during execution of a statement block.
Syntax
ON EXCEPTION , IN ( error_number )
(1) Statement Block SET SQL_error_var , ISAM_error_var , error_data_var END EXCEPTION WITH RESUME ;
Notes: 1
Element error_data_var
Description
SPL variable to receive a string returned Must be a character type to receive by an SQL error or by a user-defined the error information. Must be valid exception in current statement block. SQL error number or a number defined Must be of integer type. Must be by a RAISE EXCEPTION statement that valid in current statement block. is to be trapped SPL variable that receives the ISAM error number of the exception raised Same as for error_number
error_number
ISAM_error_var SQL_error_var
SPL variable that receives the SQL error Same as for ISAM_error_var number of the exception raised
Usage
The ON EXCEPTION statement, together with the RAISE EXCEPTION statement, provides an error-trapping and error-recovery mechanism for SPL. ON EXCEPTION can specify the errors that you want to trap as the SPL routine executes, and specifies the action to take if the error occurs within the statement block. ON EXCEPTION can specify an error number list in the IN clause, or can include no IN clause. If the IN clause is omitted, then all errors are trapped. A statement block can include more than one ON EXCEPTION statement. The exceptions that are trapped can be either system-defined or user-defined. The scope of an ON EXCEPTION statement is the statement block that follows the ON EXCEPTION statement, and all the statement blocks that are nested within that statement block. When an exception is trapped, the error status is cleared.
Chapter 3. SPL Statements
3-31
ON EXCEPTION
If you specify a variable to receive an ISAM error, but no accompanying ISAM error exists, a zero (0) is assigned to the variable. If you specify a variable to receive the error text, but none exists, the variable stores an empty string. ON EXCEPTION has no effect within a UDR that is called by a trigger.
When an error occurs, the database server searches for the last ON EXCEPTION statement that traps the error code. If the database server finds no pertinent ON EXCEPTION statement, the error code passes back to the calling context (the SPL routine, application, or interactive user), and execution terminates. In the previous example, the minus sign ( - ) is required in the IN clause that specifies error -206; most error codes are negative integers. The next example uses two ON EXCEPTION statements with the same error number so that error code 691 can be trapped in two levels of nesting. All of the DELETE statements except the one that is marked { 6 } are within the scope of the first ON EXCEPTION statement. The DELETE statements that are marked { 1 } and { 2 } are within the scope of the inner ON EXCEPTION statement:
CREATE PROCEDURE delete_cust (cnum INT) ON EXCEPTION IN (-691) -- children exist BEGIN -- Begin-end so no other DELETEs get caught in here. ON EXCEPTION IN (-691) DELETE FROM another_child WHERE num = cnum; { 1 } DELETE FROM orders WHERE customer_num = cnum; { 2 } END EXCEPTION -- for error -691 DELETE FROM orders WHERE customer_num = cnum; { 3 } END DELETE FROM cust_calls WHERE customer_num = cnum; { 4 } DELETE FROM customer WHERE customer_num = cnum; { 5 } END EXCEPTION DELETE FROM customer WHERE customer_num = cnum; { 6 } END PROCEDURE
3-32
ON EXCEPTION
A summary of the sequence of statements in the previous example would be: 1. Test for an error. 2. If error -210, -211, or -212 occurs, take action A. 3. If error -300 occurs, take action B. 4. If any other error occurs, take action C.
3-33
ON EXCEPTION
v If no statement or block, but only the SPL routine, contains the ON EXCEPTION statement, the routine executes a RETURN statement with no arguments, returning a successful status and no values. To prevent an infinite loop, if an error occurs during execution of the statement block, then the search for another ON EXCEPTION statement to trap the error does not include the current ON EXCEPTION statement.
Related Statements
RAISE EXCEPTION
3-34
RAISE EXCEPTION
Use the RAISE EXCEPTION statement to simulate the generation of an error.
Syntax
RAISE EXCEPTION SQL_error_var , ISAM_error , error_text ;
Element error_text
Description SPL variable or expression that contains the error message text
Restrictions Must be a character data type and be valid in the statement block
Syntax Identifier on page 5-22; Expression on page 4-34 Identifier on page 5-22; Expression on page 4-34 Identifier on page 5-22; Expression on page 4-34
ISAM_error
SPL variable or expression that represents an ISAM error number. The default is 0. SPL variable or expression that represents an SQL error number
Must return a value in SMALLINT range. You can specify a unary minus sign before error number. Same as for ISAM_error
SQL_error
Usage
Use the RAISE EXCEPTION statement to simulate an error or to generate an error with a custom message. An ON EXCEPTION statement can trap the generated error. If you omit ISAM_error, the database server sets the ISAM error code to zero (0) when the exception is raised. If you want to specify error_text but not specify a value for ISAM_error, specify zero (0) as the value of ISAM_error. The RAISE EXCEPTION statement can raise either system-generated exceptions or user-generated exceptions. For example, the following statement raises the error number -208 and inserts the text a missing file into the variable of the system-generated error message:
RAISE EXCEPTION -208, 0, a missing file;
Here the minus ( - ) symbol is required after the EXCEPTION keyword for error -208; most error codes are negative integers. The special error number -746 allows you to produce a customized message. For example, the following statement raises the error number -746 and returns the quoted text:
RAISE EXCEPTION -746, 0, You broke the rules;
3-35
RAISE EXCEPTION
FOREACH SELECT c1 INTO alpha FROM sometable IF alpha < 0 THEN RAISE EXCEPTION -746, 0, a < 0 found -- emergency exit END IF END FOREACH
When the SPL routine executes and the IF condition is met, the database server returns the following error:
-746: a < 0 found.
For more information about the scope and compatibility of exceptions, see ON EXCEPTION on page 3-31.
Related Statements
ON EXCEPTION
3-36
RETURN
Use the RETURN statement to specify what values (if any) the SPL function returns to the calling context.
Syntax
RETURN , (1) Expression WITH RESUME ;
Usage
In Dynamic Server, for backward compatibility, you can use the RETURN statement inside a CREATE PROCEDURE statement to create an SPL function. By only using RETURN in CREATE FUNCTION statements, however, you can maintain the convention of using CREATE FUNCTION to define routines that return a value, and CREATE PROCEDURE for other routines. All RETURN statements in the SPL function must be consistent with the RETURNING clause of the CREATE FUNCTION (or CREATE PROCEDURE) statement that defines the function. Any RETURN list of expressions must match in cardinality (and be of data types compatible with) the ordered list of data types in the RETURNING clause of the function definition. Alternatively, however, the RETURN statement can specify no expressions, even if the RETURNING clause lists one or more data types. In this case, a RETURN statement that specifies no expression is equivalent to returning the expected number of NULL values to the calling context. A RETURN statement without any expressions exits only if the SPL function is declared as not returning any values. Otherwise it returns NULL values. The following SPL function has two valid RETURN statements:
CREATE FUNCTION two_returns (stockno INT) RETURNING CHAR (15); DEFINE des CHAR(15); ON EXCEPTION (-272) -- if user does not have select privilege RETURN; -- return no values. END EXCEPTION; SELECT DISTINCT descript INTO des FROM stock WHERE stocknum = stockno; RETURN des; END FUNCTION
A program that calls the function in the previous example should test whether no values are returned and act accordingly.
3-37
RETURN
within a FOREACH loop, or else in the FROM clause of a query. If an SPL routine executes a RETURN WITH RESUME statement, a FETCH statement in an ESQL/C application can call the SPL routine. The following example shows a cursor function that another UDR can call. After the RETURN WITH RESUME statement returns each value to the calling UDR or program, the next line of series executes the next time series is called. If the variable backwards equals zero (0), no value is returned to the calling UDR or program, and execution of series stops:
CREATE FUNCTION series (limit INT, backwards INT) RETURNING INT; DEFINE i INT; FOR i IN (1 TO limit) RETURN i WITH RESUME; END FOR IF backwards = 0 THEN RETURN; END IF FOR i IN (limit TO 1 STEP -1) RETURN i WITH RESUME; END FOR END FUNCTION -- series
3-38
SYSTEM
Use the SYSTEM statement to issue an operating-system command from within an SPL routine.
Syntax
SYSTEM expression SPL_var ;
Restrictions You cannot specify that the command run in the background Must be of a character data type
Usage
If the specified expression is not a character expression, it is converted to a character expression and passed to the operating system for execution. The command that SYSTEM specifies cannot run in the background. The database server waits for the operating system to complete execution of the command before it continues to the next statement in the SPL routine. The SPL routine cannot use any returned values from the command. If the operating-system command fails (that is, returns a nonzero status for the command), an exception is raised that contains the returned operating-system status as the ISAM error code and an appropriate SQL error code. A rollback does not terminate a system call, so a suspended transaction can wait indefinitely for the call to return. For instructions on recovery from a deadlock during a long transaction rollback, see the IBM Informix Administrator's Guide. The dynamic log feature of Dynamic Server automatically adds log files until the long transaction completes or rolls back successfully. In DBA- and owner-privileged SPL routines that contain SYSTEM statements, the command runs with the access privileges of the user who executes the routine.
You can use a double-pipe symbol ( || ) to concatenate expressions with a SYSTEM statement, as the following example shows:
3-39
SYSTEM
CREATE PROCEDURE sensitive_update2() DEFINE user1 char(15); DEFINE user2 char(15); LET user1 = joe; LET user2 = mary; ... -- code to execute if user tries to execute a specified -- command, then sends email to system administrator SYSTEM mail -s violation ||user1 || || user2 || < violation_file; ... END PROCEDURE; --sensitive_update2
The expressions that follow the SYSTEM statements in this example contain variables %tmp% and %SystemRoot% that are defined by Windows.
3-40
TRACE
Use the TRACE statement to control the generation of debugging output.
Syntax
TRACE ON OFF PROCEDURE (1) Expression ;
Usage
The TRACE statement generates output that is sent to the file that the SET DEBUG FILE TO statement specifies. Tracing writes to the debug file the current values of the following program objects: v SPL variables v Routine arguments v Return values v SQL error codes v ISAM error codes The output of each executed TRACE statement appears on a separate line. If you use the TRACE statement before you specify a DEBUG file to contain the output, an error is generated. Any routine that the SPL routine calls inherits the trace state. That is, a called routine (on the same database server) assumes the same trace state (ON, OFF, or PROCEDURE) as the calling routine. The called routine can set its own trace state, but that state is not passed back to the calling routine. A routine that is executed on a remote database server does not inherit the trace state.
TRACE ON
If you specify the keyword ON, all statements are traced. The values of variables (in expressions or otherwise) are printed before they are used. To turn tracing ON implies tracing both routine calls and statements in the body of the routine.
TRACE OFF
If you specify the keyword OFF, all tracing is turned off.
TRACE PROCEDURE
If you specify the keyword PROCEDURE, only the routine calls and return values, but not the body of the routine, are traced. The TRACE statement supports no ROUTINE or FUNCTION keywords. Use the TRACE PROCEDURE keywords when the SPL routine that you trace is a function.
3-41
TRACE
Displaying Expressions
You can use the TRACE statement with a quoted string or an expression to display values or comments in the output file. If the expression is not a literal expression, the expression is evaluated before it is written to the output file. You can use the TRACE statement with an expression even if you used a TRACE OFF statement earlier in a routine. You must first, however, use the SET DEBUG statement to establish a trace output file. The next example uses a TRACE statement with an expression after using a TRACE OFF statement. The example uses UNIX file naming conventions:
CREATE PROCEDURE tracing () DEFINE i INT; BEGIN ON EXCEPTION IN (1) END EXCEPTION; -- do nothing SET DEBUG FILE TO /tmp/foo.trace; TRACE OFF; TRACE Forloop starts; FOR i IN (1 TO 1000) BEGIN TRACE FOREACH starts; FOREACH SELECT...INTO a FROM t IF <some condition> THEN RAISE EXCEPTION 1 -- emergency exit END IF END FOREACH -- return some value END END FOR -- do something END; END PROCEDURE
3-42
WHILE
Use the WHILE statement to establish a loop with variable end conditions.
Syntax
(1) WHILE Condition Statement Block (2) END WHILE ;
Notes: 1 2 See Condition on page 4-5 See Statement Block on page 5-69
Usage
The condition is evaluated before the statement block first runs and before each subsequent iteration. Iterations continue as long as the condition remains true. The loop terminates when the condition evaluates to not true. If any expression within the condition evaluates to NULL, the condition becomes not true unless you are explicitly testing for NULL with the IS NULL operator. If an expression within the condition has an UNKNOWN value because it references uninitialized SPL variables, an immediate error results. In this case, the loop terminates, raising an exception.
3-43
3-44
In This Chapter
This chapter describes the data types and expressions that Dynamic Server and Extended Parallel Server support. These fundamental syntax segments can appear in data definition language (DDL) and data manipulation language (DML) statements, and in other types of SQL statements. Some SPL statements can also specify data types or expressions. You can use these features of a relational or object-relational database in various contexts, such as to define the schema of a table, to specify the signature and arguments of a routine, or to represent or calculate specific data values.
4-1
v The Syntax column of the table that immediately follows a syntax diagram can list a segment name and the page where the segment description begins. If the syntax diagram for a statement includes a reference to a segment, turn to that segment description to see the complete syntax for the segment. For example, if you want to write a CREATE VIEW statement that includes a database and database server qualifiers of the view name, first look up the syntax diagram for the CREATE VIEW statement in Chapter 2. The table beneath that diagram refers to the Database Object Name segment for the syntax of view. Then use the Database Object Name segment syntax to enter a valid CREATE VIEW statement that also specifies the database and database server name for the view (and for Extended Parallel Server, the coserver identifier). In the following example, the CREATE VIEW statement defines a view called name_only in the sales database on the boston database server:
CREATE VIEW sales@boston:name_only AS SELECT customer_num, fname, lname FROM customer;
Besides the Data Types and Expressions syntax segments that this chapter documents, Chapter 5 provides additional syntax segments that are referenced in the syntax diagrams of this manual.
4-2
Collection Subquery
Collection Subquery
You can use a Collection Subquery to create a MULTISET collection from the results of a subquery. Only Dynamic Server supports this syntax, which is an extension to the ANSI/ISO standard for SQL.
Syntax
Collection Subquery:
(1) MULTISET ( subquery SELECT ITEM singleton_select )
Notes: 1
Element singleton _select subquery Description Subquery returning exactly one row Embedded query
Informix extension
Restrictions Subquery cannot repeat the SELECT keyword, nor include the ORDER BY clause Cannot contain the ORDER BY clause Syntax SELECT, p. 2-479 SELECT, p. 2-479
Usage
The MULTISET and SELECT ITEM keywords have the following significance: v MULTISET specifies a collection of elements that can contain duplicate values, but that has no specific order of elements. v SELECT ITEM supports only one expression in the projection list. You cannot repeat the SELECT keyword in the singleton subquery. You can use a collection subquery in the following contexts: v The Projection clause and WHERE clause of the SELECT statement v The VALUES clause of the INSERT statement v The SET clause of the UPDATE statement v Wherever you can use a collection expression (that is, any expression that evaluates to a single collection) v As an argument passed to a user-defined routine The following restrictions apply to a collection subquery: v The Projection clause cannot contain duplicate column (field) names. v It cannot contain aliases for table names. (But it can use aliases for column (field) names, as in some of the examples that follow. ) v It is read-only. v It cannot be opened twice. v It cannot contain NULL values. v It cannot contain syntax that attempts to seek within the subquery. A collection subquery returns a multiset of unnamed ROW data types. The fields of this ROW type are elements in the projection list of the subquery. Examples that follow access the tables and the ROW types that these statements define:
4-3
Collection Subquery
CREATE CREATE CREATE CREATE CREATE ROW TYPE rt1 (a INT); ROW TYPE rt2 (x int, y rt1); TABLE tab1 (col1 rt1, col2 rt2); TABLE tab2 OF TYPE rt1; TABLE tab3 (a ROW(x INT));
The following examples of collection subqueries return the MULTISET collections that are listed to the right of the subquery.
Collection Subquery MULTISET (SELECT * FROM tab1)... MULTISET (SELECT col2.y FROM tab1)... MULTISET (SELECT * FROM tab2)... MULTISET(SELECT p FROM tab2 p)... MULTISET (SELECT * FROM tab3)... Resulting Collections MULTISET(ROW(col1 rt1, col2 rt2)) MULTISET(ROW(y rt1)) MULTISET(ROW(a int)) MULTISET(ROW(p rt1)) MULTISET(ROW(a ROW(x int)))
4-4
Condition
Use a condition to test whether data meets certain qualifications. Use this segment wherever you see a reference to a condition in a syntax diagram.
Syntax
Condition:
Logical_Operator (1) Comparison Conditions NOT (2) Condition with Subquery (3) (4) User-Defined Function
Notes: 1 2 3 4
Element Logical _Operator Description Combines two conditions
See page 4-6 See page 4-13 Dynamic Server only See page 4-111
Restrictions Valid options are OR ( = logical union) or AND ( = logical intersection) Syntax Condition with AND or OR, p. 4-17
Usage
A condition is a search criterion, optionally connected by the logical operators AND or OR. Conditions can be classified into the following categories: v Comparison conditions (also called filters or Boolean expressions) v Conditions within a subquery v User-defined functions (Dynamic Server only) A condition can contain an aggregate function only if it is used in the HAVING clause of a SELECT statement or in the HAVING clause of a subquery. No aggregate function can appear in a condition in the WHERE clause of a DELETE, SELECT, or UPDATE statement unless both of the following are TRUE: v Aggregate is on a correlated column originating from a parent query. v The WHERE clause appears in a subquery within a HAVING clause. In Dynamic Server, user-defined functions are not valid as conditions in the following contexts: v In the HAVING clause of a SELECT statement v In the definition of a check constraint SPL routines are not valid as conditions in the following contexts: v In the definition of a check constraint v In the ON clause of a SELECT statement
Chapter 4. Data Types and Expressions
4-5
Condition
v In the WHERE clause of a DELETE, SELECT, or UPDATE statement External v In the v In the v In the v In the v In the routines are not valid as conditions in the following contexts: definition of a check constraint ON clause of a SELECT statement WHERE clause of a DELETE, SELECT, or UPDATE statement WHEN clause of CREATE TRIGGER IF, CASE, or WHILE statements of SPL
IN Condition (5) Column Name IS NOT (6) Quoted String (5) Column Name NOT
NULL (6) LIKE (3) MATCHES Column Name Quoted String ESCAPE char (5)
Notes: 1 2 3 4 5 6 See page 4-34 See page 4-146 Informix extension See page 4-9 See page 4-7 See page 4-142
Description An ASCII character to be the nondefault escape character in the quoted string. Single ( ) and double ( ) quotation marks are not valid as char. Restrictions See ESCAPE with LIKE on page 4-12 and ESCAPE with MATCHES on page 4-13. Syntax Quoted String, p. 4-142
Element char
The following sections describe the different types of comparison conditions: v Relational-Operator Condition on page 4-8 v BETWEEN Condition on page 4-8 v IN Condition on page 4-9 v IS NULL Condition on page 4-10 v LIKE and MATCHES Condition on page 4-11.
4-6
Condition
For a discussion of comparison conditions in the context of the SELECT statement, see Using a Condition in the WHERE Clause on page 2-508. Warning: A literal date or DATETIME value in a comparison condition should specify 4 digits for the year. When you specify a 4-digit year, the DBCENTURY environment variable has no effect on the result. When you specify a 2-digit year, DBCENTURY can affect how the database server interprets the comparison condition, which might not work as you intended. For more information, see the IBM Informix Guide to SQL: Reference.
Column Name
Column Name:
column (1) row_column (2) .field
Notes: 1 2
Element alias column field row_column synonym, table, view Description Temporary alternative name for table or view Name of a column A field to compare in a ROW type column A column of type ROW Name of a synonym, table, or view
For more information on the meaning of the column name in these conditions, see the IS NULL Condition on page 4-10 and the LIKE and MATCHES Condition on page 4-11.
4-7
Condition
The following example shows the correct use of quotation marks in comparison conditions. Here the ship_instruct column has a character data type, the order_date column has a date data type, and the ship_weight column has a numeric data type.
SELECT * FROM orders WHERE ship_instruct = express AND order_date > 05/01/98 AND ship_weight < 30
Relational-Operator Condition
The following examples show some relational-operator conditions:
city[1,3] = San o.order_date > 6/12/98 WEEKDAY(paid_date) = WEEKDAY(CURRENT- (31 UNITS DAY)) YEAR(ship_date) < YEAR (TODAY) quantity <= 3 customer_num <> 105 customer_num != 105
If an expression within the condition has an UNKNOWN value because it references an uninitialized variable, the database server raises an exception. If any expression within the condition evaluates to NULL, the condition cannot be true, unless you are explicitly testing for NULL by using the IS NULL operator. For example, if the paid_date column has a NULL value, then neither of the following statements can retrieve that row:
SELECT customer_num, order_date FROM orders WHERE paid_date = SELECT customer_num, order_date FROM orders WHERE NOT (paid_date !=)
The IS NULL operator tests for a NULL value, as the next example shows. The IS NULL operator is described in IS NULL Condition on page 4-10.
SELECT customer_num, order_date FROM orders WHERE paid_date IS NULL
BETWEEN Condition
For a BETWEEN test to be TRUE, the value of the expression on the left of the BETWEEN keyword must be in the inclusive range of the values of the two expressions on the right of the BETWEEN keyword. NULL values do not satisfy the condition, and you cannot use NULL for either expression that defines the range. The following examples show some BETWEEN conditions:
order_date BETWEEN 6/1/97 and 9/7/97 zipcode NOT BETWEEN 94100 and 94199 EXTEND(call_dtime, DAY TO DAY) BETWEEN (CURRENT - INTERVAL(7) DAY TO DAY) AND CURRENT
4-8
Condition
lead_time BETWEEN INTERVAL (1) DAY TO DAY AND INTERVAL (4) DAY TO DAY unit_price BETWEEN loprice AND hiprice
IN Condition
The IN condition is satisfied when the expression to the left of the keyword IN is included in the list of items. IN Condition:
(1) Expression NOT IN
, (2) ( Literal Number (3) Literal DATETIME (4) Quoted String (5) Literal INTERVAL USER TODAY CURRENT (6) DATETIME Field Qualifier SITENAME DBSERVERNAME (7) Literal Row (7) collection_col , (8) ( Literal Collection (8) Literal Collection ) )
(6)
Notes: 1 2 3 4 5 6 7 8 See page 4-34 See page 4-137 See page 4-132 See page 4-142 See page 4-135 See page 4-32 Dynamic Server only See page 4-139
4-9
Condition
Element collection_col Description Restrictions Syntax Identifier, p. 5-22
Name of a collection column that is The column must exist in the used in an IN condition specified table
If you specify the NOT operator, the IN condition is TRUE when the expression is not in the list of items. NULL values do not satisfy the IN condition. The following examples show some IN conditions:
WHERE WHERE WHERE WHERE state IN (CA, WA, OR) manu_code IN (HRO, HSK) user_id NOT IN (USER) order_date NOT IN (TODAY)
In ESQL/C, the built-in TODAY function is evaluated at execution time. The built-in CURRENT function is evaluated when a cursor opens or when the query executes, if it is a singleton SELECT statement. The built-in USER function is case sensitive; for example, it interprets minnie and Minnie as different values. Using the IN Operator with Collection Data Types (IDS): You can use the IN operator to determine if an element is contained in a collection. The collection can be a simple or nested collection. (In a nested collection type, the element type of the collection is also a collection type.) When you use IN to search for an element of a collection, the expression to the left or right of the IN keyword cannot contain a BYTE or TEXT data type. Suppose you create the following table that contains two collection columns:
CREATE TABLE tab_coll ( set_num SET(INT NOT NULL), list_name LIST(SET(CHAR(10) NOT NULL) NOT NULL) );
The following partial examples show how you might use the IN operator for search conditions on the collection columns of the tab_coll table:
WHERE WHERE WHERE WHERE WHERE WHERE 5 IN set_num 5.0::INT IN set_num "5" NOT IN set_num set_num IN ("SET{1,2,3}", "SET{7,8,9}") "SET{john, sally, bill}" IN list_name list_name IN ("LIST{""SET{bill,usha}"", ""SET{ann moshi}""}", "LIST{""SET{bob,ramesh}"", ""SET{bomani ann}""}")
In general, when you use the IN operator on a collection data type, the database server checks whether the value on the left of the IN operator is an element in the set of values on the right of the IN operator.
IS NULL Condition
The IS NULL condition is satisfied if the column contains a NULL value. If you use the IS NOT NULL operator, the condition is satisfied when the column contains a value that is not NULL. This example shows an IS NULL condition:
WHERE paid_date IS NULL
4-10
Condition
Conditions that use the IS NULL operator are exceptions to the usual rule that SQL expressions in which an operator has a NULL operand return FALSE.
LIKE Operator: The LIKE operator supports these wildcard characters in the quoted string. Wildcard % _ \ Effect Matches zero or more characters Matches any single character Removes the special significance of the next character (to match a literal % or _ or \ by specifying \% or \_ or \\ )
Using the backslash ( \ ) symbol as the default escape character is an Informix extension to the ANSI/ISO-standard for SQL. In an ANSI-compliant database, you can only use an escape character to escape a percent sign ( % ), an underscore ( _ ), or the escape character itself. The following condition tests for the string tennis, alone or in a longer string, such as tennis ball or table tennis paddle:
WHERE description LIKE %tennis%
The next example tests for description rows containing an underscore. Here backslash ( \ ) is necessary because underscore ( _ ) is a wildcard character.
WHERE description LIKE %\_%
4-11
Condition
The LIKE operator has an associated operator function called like( ). You can define a like( ) function to handle your own user-defined data types. See also IBM Informix User-Defined Routines and Data Types Developer's Guide. MATCHES Operator: The MATCHES operator supports wildcard characters in the quoted string. Wildcard * ? [...] Effect Matches any string of zero or more characters Matches any single character Matches any of the enclosed characters, including ranges, as in [a-z]. Characters within the brackets cannot be escaped. As first character within the brackets, matches any character that is not listed. Thus, [^abc] matches any character except a, b, or c. Removes the special significance of the next character (to match a literal \ or any other wildcard by specifying \\ or\* or \? and so forth)
^ \
The following condition tests for the string tennis, alone or within a longer string, such as tennis ball or table tennis paddle:
WHERE description MATCHES *tennis*
The following condition is TRUE for the names Frank and frank:
WHERE fname MATCHES [Ff]rank
The following condition is TRUE for any name that begins with either F or f:
WHERE fname MATCHES [Ff]*
The next condition is TRUE for any name that ends with the letters a, b, c, or d:
WHERE fname MATCHES *[a-d]
MATCHES has an associated matches( ) operator function. You can define a matches( ) function for your own user-defined data types. For more information, see IBM Informix User-Defined Routines and Data Types Developer's Guide. If DB_LOCALE or SET COLLATION specifies a nondefault locale supporting a localized collation, and you specify a range for the MATCHES operator using bracket ( [ . . . ] ) symbols, the database server uses the localized collating order, instead of code-set order, to interpret the range and to compare values that have CHAR, CHARACTER VARYING, LVARCHAR, NCHAR, NVARCHAR, and VARCHAR data types. This behavior is an exception to the usual rule that only NCHAR and NVARCHAR data types can be compared in a localized collating order. For more information on the GLS aspects of conditions that include the MATCHES or LIKE operators, see the IBM Informix GLS User's Guide. ESCAPE with LIKE: The ESCAPE clause can specify a nondefault escape character. For example, if you specify z in the ESCAPE clause, then a quoted string operand that included z_ is interpreted as including a literal underscore ( _ ) character, rather than _ as a wildcard. Similarly, z% is interpreted as a literal percent ( % ) sign, rather than % as a wildcard. Finally, the characters zz in a string
4-12
Condition
would be interpreted as single literal z. The following statement retrieves rows from the customer table in which the company column includes a literal underscore character:
SELECT * FROM customer WHERE company LIKE %z_% ESCAPE z
You can also use a host variable that contains a single character. The next statement uses a host variable to specify an escape character:
EXEC SQL BEGIN DECLARE SECTION; char escp=z; char fname[20]; EXEC SQL END DECLARE SECTION; EXEC SQL select fname from customer into :fname where company like %z_% escape :escp;
ESCAPE with MATCHES: The ESCAPE clause can specify a nondefault escape character. Use this as you would the backslash to include a question mark ( ? ), an asterisk ( * ), a caret ( ^ ), or a left ( [ ) or right ( ] ) bracket as a literal character within the quoted string, to prevent them from being interpreted as special characters. If you choose to use z as the escape character, the characters z? in a string stand for a literal question mark ( ? ). Similarly, the characters z* stand for a literal asterisk ( * ). Finally, the characters zz in the string stand for the single character z. The following example retrieves rows from the customer table in which the value of the company column includes the question mark ( ? ):
SELECT * FROM customer WHERE company MATCHES *z?* ESCAPE z
Stand-Alone Condition
A stand-alone condition can be any expression that is not explicitly listed in the syntax for the comparison condition. Such an expression is valid as a condition only if it returns a BOOLEAN value. For example, the following example returns a value of the BOOLEAN data type:
funcname(x)
Notes: 1 2 3 See page 4-14 See page 4-14 See page 4-15
You can include a SELECT statement within a condition; this combination is called a subquery. You can use a subquery in a SELECT statement to perform the following functions: v Compare an expression to the result of another query.
Chapter 4. Data Types and Expressions
4-13
Condition
v Determine if an expression is included in the results of another query. v Ask whether another query selects any rows. The subquery can depend on the current row that the outer SELECT statement is evaluating; in this case, the subquery is called a correlated subquery. The following sections describe subquery conditions and their syntax. For a discussion of types of subquery conditions in the context of the SELECT statement, see Using a Condition in the WHERE Clause on page 2-508. A subquery can return a single value, no value, or a set of values, depending on its context. If a subquery returns a value, it must select only a single column. If the subquery simply checks whether a row (or rows) exists, it can select any number of rows and columns. A subquery cannot contain BYTE or TEXT data types, nor can it contain an ORDER BY clause. For a complete description of SELECT syntax and usage, see SELECT on page 2-479.
IN Subquery
IN Subquery:
(1) Expression NOT IN ( subquery )
Notes: 1
Element subquery Description Embedded query
An IN subquery condition is TRUE if the value of the expression matches one or more of the values from the subquery. (The subquery must return only one row, but it can return more than one column.) The keyword IN is equivalent to the =ANY specification. The keywords NOT IN are equivalent to the !=ALL specification. See the ALL, ANY, and SOME Subqueries on page 4-15. The following example of an IN subquery finds the order numbers for orders that do not include baseball gloves (stock_num = 1):
WHERE order_num NOT IN (SELECT order_num FROM items WHERE stock_num = 1)
Because the IN subquery tests for the presence of rows, duplicate rows in the subquery results do not affect the results of the main query. Therefore, the UNIQUE or DISTINCT keyword in the subquery has no effect on the query results, although not testing duplicates can improve query performance.
EXISTS Subquery
EXISTS Subquery:
4-14
Condition
EXISTS ( NOT subquery )
Element subquery
An EXISTS subquery condition evaluates to TRUE if the subquery returns a row. With an EXISTS subquery, one or more columns can be returned. The subquery always contains a reference to a column of the table in the main query. If you use an aggregate function in an EXISTS subquery that includes no HAVING clause, at least one row is always returned. The following example of a SELECT statement with an EXISTS subquery returns the stock number and manufacturer code for every item that has never been ordered (and is therefore not listed in the items table). You can appropriately use an EXISTS subquery in this SELECT statement because you use the subquery to test both stock_num and manu_code in items.
SELECT stock_num, manu_code FROM stock WHERE NOT EXISTS (SELECT stock_num, manu_code FROM items WHERE stock.stock_num = items.stock_num AND stock.manu_code = items.manu_code)
The preceding example works equally well if you use SELECT * in the subquery in place of the column names, because the existence of the entire row is tested; specific column values are not tested.
Notes: 1 2
Element subquery Description Embedded query
Use the ALL, ANY, and SOME keywords to specify what makes the condition TRUE or FALSE. A search condition that is TRUE when the ANY keyword is used might not be TRUE when the ALL keyword is used, and vice versa. Using the ALL Keyword: The ALL keyword specifies that the search condition is TRUE if the comparison is TRUE for every value that the subquery returns. If the subquery returns no value, the condition is TRUE.
4-15
Condition
In the following example, the first condition tests whether each total_price is greater than the total price of every item in order number 1023. The second condition uses the MAX aggregate function to produce the same results.
total_price > ALL (SELECT total_price FROM items WHERE order_num = 1023) total_price > (SELECT MAX(total_price) FROM items WHERE order_num = 1023)
Using the NOT keyword with an ALL subquery tests whether an expression is not TRUE for at least one element that the subquery returns. For example, the following condition is TRUE when the expression total_price is not greater than all the selected values. That is, it is TRUE when total_price is not greater than the highest total price in order number 1023.
NOT total_price > ALL (SELECT total_price FROM items WHERE order_num = 1023)
Using the ANY or SOME Keywords: The ANY keyword denotes that the search condition is TRUE if the comparison is TRUE for at least one of the values that is returned. If the subquery returns no value, the search condition is FALSE. The SOME keyword is a synonym for ANY. The following conditions are TRUE when the total price is greater than the total price of at least one of the items in order number 1023. The first condition uses the ANY keyword; the second uses the MIN aggregate function:
total_price > ANY (SELECT total_price FROM items WHERE order_num = 1023) total_price > (SELECT MIN(total_price) FROM items WHERE order_num = 1023)
Using the NOT keyword with an ANY subquery tests whether an expression is not TRUE for all elements that the subquery returns. For example, the following condition is TRUE when the expression total_price is not greater than any selected value. That is, it is TRUE when total_price is greater than none of the total prices in order number 1023.
NOT total_price > ANY (SELECT total_price FROM items WHERE order_num = 1023)
Omitting the ANY, ALL, or SOME Keywords: You can omit the keywords ANY, ALL, or SOME in a subquery if you know that the subquery will return exactly one value. If you omit the ANY, ALL, or SOME keywords, and the subquery returns more than one value, you receive an error. The subquery in the following example returns only one row because it uses an aggregate function:
SELECT order_num FROM items WHERE stock_num = 9 AND quantity = (SELECT MAX(quantity) FROM items WHERE stock_num = 9)
NOT Operator
If you preface a condition with the keyword NOT, the test is TRUE only if the condition that NOT qualifies is FALSE. If the condition that NOT qualifies has a NULL or an UNKNOWN value, the NOT operator has no effect. The following truth table shows the effect of NOT. Here T represents a TRUE condition, F represents a FALSE condition, and a question mark (?) represents an UNKNOWN condition. (An UNKNOWN value can occur when an operand is NULL).
4-16
Condition
NOT T ? F F ? T
The left column shows the value of the operand of the NOT operator, and the right column shows the returned value after NOT is applied to the operand.
The following truth tables show the effect of the AND and OR operators. The letter T represents a TRUE condition, F represents a FALSE condition, and the question mark (?) represents an UNKNOWN value. An UNKNOWN value can occur when part of an expression that uses a logical operator is NULL.
OR T ? F T T T T ? T ? ?
F
T ? F
AND T ? F
T T ? F
? ? ? F
F F F F
The marginal values at the left represent the first operand, and values in the top row represent the second operand. Values within each 3x3 matrix show the returned value after the operator is applied to those operands. If the Boolean expression evaluates to UNKNOWN, the condition is not satisfied. Consider the following example within a WHERE clause:
WHERE ship_charge/ship_weight < 5 AND order_num = 1023
The row where order_num = 1023 is the row where ship_weight is NULL. Because ship_weight is NULL, ship_charge/ship_weight is also NULL; therefore, the truth value of ship_charge/ship_weight < 5 is UNKNOWN. Because order_num = 1023 is TRUE, the AND table states that the truth value of the entire condition is UNKNOWN. Consequently, that row is not chosen. If the condition used an OR in place of the AND, the condition would be TRUE.
Related Information
For discussions of comparison conditions in the SELECT statement and of conditions with a subquery, see the IBM Informix Guide to SQL: Tutorial. For information on the GLS aspects of conditions, see the IBM Informix GLS User's Guide.
4-17
Data Type
The Data Type segment specifies the data type of a column, a routine parameter, or a value returned by an expression or by a cast. Use this segment whenever you see a reference to a data type in a syntax diagram.
Syntax
Data Type:
(1) Built-In Data Type (2) (3) User-Defined Data Type (5) Complex Data Type
(4)
Notes: 1 2 3 4 5 See Built-In Data Types Informix extension Dynamic Server only See User-Defined Data Type (IDS) on page 4-27 See Complex Data Type (IDS) on page 4-28
Usage
Sections that follow summarize these data types. For more information, see the chapter about data types in the IBM Informix Guide to SQL: Reference.
(4)
Notes: 1 2 3 4 5 See Character Data Types on page 4-19 See Numeric Data Types on page 4-21 Informix extension See Large-Object Data Types on page 4-25 See Time Data Types on page 4-27
4-18
Data Type
6 Dynamic Server only
These are built into the database server in the sense that the information and support functions required to interpret and transfer these data types is part of the database server software, which supports character, numeric, large-object, and time categories of built-in data types. These are described in sections that follow.
4-19
Data Type
Element max Description Maximum size in bytes. For VARCHAR, this is required. LVARCHAR default is 2048 Bytes reserved. Default is 0. Size in bytes. Default is 1. Restrictions VARCHAR: Integer; 1 max 255 (or 1 max 254, if indexed) LVARCHAR: 1 max 32,739 Integer; 0 reserve max Integer; 1 size 32,767 Syntax Literal Number on page 4-137 Literal Number on page 4-137 Literal Number on page 4-137
reserve size
The database server issues an error if the data type declaration includes empty parentheses, such as LVARCHAR( ). To declare a CHAR or LVARCHAR data type of the default length, simply omit any (size) or (max) specification. The CREATE TABLE statement of Dynamic Server accepts VARCHAR and NVARCHAR column declarations that have no (max) nor (max, reserve) specifications, using ( 1, 0 ) as the (max, reserve) defaults for the column. The following table summarizes the built-in character data types.
Data Type CHAR CHARACTER CHARACTER VARYING LVARCHAR (IDS) NCHAR NVARCHAR VARCHAR Description Stores single-byte or multibyte text strings of fixed length (up to 32,767 bytes); supports code-set order in collation of text data. Default size is 1 byte. Synonym for CHAR ANSI-compliant synonym for VARCHAR Stores single-byte or multibyte text strings of varying length (up to 32,739 bytes). The size of other columns in the same table can further reduce this upper limit. Default size is 2,048 bytes. Stores single-byte or multibyte text strings of fixed length (up to 32,767 bytes); supports localized collation of text data. Stores single-byte or multibyte text strings of varying length (up to 255 bytes); supports localized collation of text data. Stores single-byte or multibyte text strings of varying length (up to 255 bytes); supports code-set order collation of text data.
Single-Byte and Multi-Byte Characters and Locales: All built-in character data types can support single- and multibyte characters in the code set that the DBLOCALE setting specifies. Locales for most European and Middle Eastern languages support only single-byte code sets, but some East Asian locales (and the UTF-8 Unicode locale) support multibyte characters. The TEXT and CLOB data types also support single-byte or multibyte character data, but most built-in functions for manipulating character strings do not support TEXT nor CLOB data. For more information, see Large-Object Data Types on page 4-25. Fixed- and Varying-Length Character Data Types: The database server supports storage of fixed-length and varying-length character data. A fixed-length column requires the defined number of bytes regardless of the actual size of the data. The CHAR data type is of fixed-length. For example, a CHAR(25) column requires 25 bytes of storage for all values, so the string This is a text string uses 25 bytes of storage.
4-20
Data Type
A varying-length column size can be the number of bytes occupied by its data. NVARCHAR, VARCHAR, and (for Dynamic Server only) the LVARCHAR data types are varying-length character data types. For example, a VARCHAR(25) column reserves up to 25 bytes of storage for the column value, but the character string This is a text string uses only 21 bytes of the reserved 25 bytes. The VARCHAR data type can store up to 255 bytes of data. The LVARCHAR type of Dynamic Server can store up to 32,739 bytes of text. LVARCHAR is a built-in opaque data type. Like other opaque types, it cannot be accessed in a database of a non-local Dynamic Server instance by a distributed query or by other DML operations, nor can LVARCHAR values be returned from a database of another database server by UDRs. For information on accessing LVARCHAR values in other databases of the local server, however, see BOOLEAN and Other Built-In Opaque Data Types (IDS) on page 4-19. A single table cannot be created with more than approximately 195 LVARCHAR columns. (This restriction applies to all varying-length and ROW data types.) Light scans are not supported on tables that include LVARCHAR columns. NCHAR and NVARCHAR Data Types: The character data types CHAR, LVARCHAR, and VARCHAR support code-set order collation of data. The database server collates text data in columns of these types by the order that their characters are defined in the code set. To accommodate locale-specific order of characters, you can use the NCHAR and NVARCHAR data types. The NCHAR data type is the fixed-length character data type that supports localized collation. The NVARCHAR data type is the varying-length character data type that can store up to 255 bytes of text data and supports localized collation. A single table cannot be created with more than approximately 195 NVARCHAR or VARCHAR columns. Dynamic Server does not support light scans on tables that have NVARCHAR columns. In Dynamic Server, if you specify no parameters in CREATE TABLE or ALTER TABLE statements that declare VARCHAR or NVARCHAR columns, then the new columns default to a max size of 1 byte and a reserve size of zero. For more information, see the IBM Informix GLS User's Guide.
Notes: 1 2 See Exact Numeric Data Types on page 4-22 See Approximate Numeric Data Types on page 4-23
Chapter 4. Data Types and Expressions
4-21
Data Type
The values of numbers are stored either as exact numeric data types or as approximate numeric data types. Exact Numeric Data Types: An exact numeric data type stores numbers of a specified precision and scale. Exact Numeric Data Type:
DECIMAL DEC NUMERIC (1) MONEY , 2 ( precision , scale INT INTEGER SMALLINT (1) INT8 (1) SERIAL SERIAL8 ( start ) )
Notes: 1
Element precision scale start Description Significant digits Digits in fractional part Integer starting value
Informix extension
Restrictions Must be an integer; 1 precision 32 Must be an integer; 1 scale precision For SERIAL: 1 start 2,147,483,64; For SERIAL8: 1 start 9,223,372,036,854,775,807 Syntax Literal Number on page 4-137 Literal Number on page 4-137 Literal Number on page 4-137
The precision of a data type is the number of digits that the data type stores. The scale is the number of digits to the right of the decimal separator.
4-22
Data Type
The following table summarizes the exact numeric data types available.
Data Type DEC(p,s) DECIMAL(p,s) Description Synonym for DECIMAL(p,s) Stores fixed-point decimal values of real numbers, with up to 30 significant digits in the fractional part, or up to 32 significant digits to the left of the decimal point. Synonym for INTEGER Stores a 4-byte integer value. These values can be in the range from -((2**31)-1) to (2**31)-1 (the range -2,147,483,647 to 2,147,483,647). Stores an 8-byte integer value. These values can be in the range from -((2**63)-1) to (2**63)-1 (the range -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807). Stores fixed-point currency values. These values have same internal data format as a fixed-point DECIMAL(p,s) value. ANSI-compliant synonym for DECIMAL(p,s) Stores a 4-byte positive integer that the database server generates. Values can range from 1 to (2**31)-1 (that is, from 1 to 2,147,483,647). Stores an 8-byte positive integer value that the database server generates. Values can range from 1 to (2**63)-1 (that is, from 1 to 9,223,372,036,854,775,807). Stores a 2-byte integer value. These values can be in the range from -((2**15)-1) to (2**15)-1 (that is, from -32,767 to 32,767).
SMALLINT
DECIMAL(p,s) Data Types: The first DECIMAL(p, s) parameter (p) specifies the precision (the total number of digits) and the second (s) parameter specifies the scale (the number of digits in the fractional part). If you provide only one parameter, an ANSI-compliant database interprets it as the precision of a fixed-point number and the default scale is 0. If you specify no parameters, and the database is ANSI-compliant, then by default the precision is 16 and the scale is 0. If the database is not ANSI-compliant, and you specify fewer than 2 parameters, you declare a floating-point DECIMAL, which is not an exact number data type. (See instead the section Approximate Numeric Data Types on page 4-23.) DECIMAL(p, s) values are stored internally with the first byte representing a sign bit and a 7-bit exponent in excess-65 format. The other bytes express the mantissa as base-100 digits. This implies that DECIMAL(32,s) data types store only s-1 decimal digits to the right of the decimal point, if s is an odd number. SERIAL and SERIAL8 Data Types: If you want to insert an explicit value into a SERIAL or SERIAL8 column, you can use any nonzero number. You cannot, however, start or reset the value of a SERIAL or SERIAL8 column with a negative number. (For details of an alternative feature of Dynamic Server for generating integer values, see CREATE SEQUENCE on page 2-165.) A SERIAL or SERIAL8 column is not unique unless you set a unique index on the column. (The index can also be in the form of a primary key or unique constraint.) With such an index, values in SERIAL or SERIAL8 columns are guaranteed to be unique, but successive values are not necessarily contiguous. Approximate Numeric Data Types: An approximate numeric data type represents numeric values approximately.
Chapter 4. Data Types and Expressions
4-23
Data Type
Approximate Numeric Data Type:
(1) (16) DECIMAL ( precision ) DEC NUMERIC FLOAT DOUBLE PRECISION (float_precision) (1) SMALLFLOAT REAL
Notes: 1
Element float_precision precision Description The float_precision is ignored, but is ANSI/ISO compliant. Significant digits. Default is 16.
Informix extension
Restrictions Must be a positive integer. Specified value has no effect. An integer; 1 precision 32 Syntax Literal Number on page 4-137 Literal Number on page 4-137
Use approximate numeric data types for very large and very small numbers that can tolerate some degree of rounding during arithmetic operations. The following table summarizes the built-in approximate numeric data types.
Data Type DEC(p) DECIMAL(p) Description Synonym for DECIMAL(p) Stores floating-point decimal values in the approximate range from 1.0E-130 to 9.99E+126 The p parameter specifies the precision. If no precision is specified, the default is 16. This floating-point data type is available as an approximate numeric type only in a database that is not ANSI-compliant. In an ANSI-compliant database, DECIMAL(p) is implemented as a fixed-point DECIMAL; see Exact Numeric Data Types on page 4-22. DOUBLE PRECISION FLOAT ANSI-compliant synonym for FLOAT. The float_precision term is not valid when you use this synonym in data type declarations. Stores double-precision floating-point numbers with up to 16 significant digits. The float-precision parameter is accepted in data-type declarations for compliance with the ANSI/ISO standard for SQL, but this parameter has no effect on the actual precision of values that the database server stores. ANSI-compliant synonym for DECIMAL(p) In an ANSI-compliant database, this is implemented as an exact numeric type, with the specified precision and a scale of zero, rather than an approximate numeric (floating-point) data type. ANSI-compliant synonym for SMALLFLOAT Stores single-precision floating-point numbers with approximately 8 significant digits
NUMERIC(p)
REAL SMALLFLOAT
4-24
Data Type
The built-in number data types of Informix database servers support real numbers. They cannot directly store imaginary or complex numbers. In Dynamic Server, you must create a user-defined data type for applications that support values that can have an imaginary part. No more than nine arguments to an external UDR can be DECIMAL data types of SQL that the UDR declares as BigDecimal data types of the Java language.
IN
Notes: 1 2
Element blobspace family_name
Description Name of an existing blobspace Family name or variable in the optical family
The large object data types can be classified in two categories: v Simple large objects: TEXT and BYTE v Smart large objects: CLOB and BLOB Simple-Large-Object Data Types: These are the simple-large-object data types: Data Type TEXT BYTE Description Stores text data of up to 231 bytes Stores any digitized data of up to 231 bytes
These data types are not recoverable. Do not supply a BYTE value where TEXT is expected. No built-in cast supports BYTE to TEXT data-type conversion. You cannot create a table with more than approximately 195 BYTE or TEXT columns. (This restriction applies to all varying-length and ROW data types.) Storing BYTE and TEXT Data: A simple-large-object data type can store text or binary data in blobspaces or in tables. The database server can access a BYTE or
4-25
Data Type
TEXT value in one piece. When you specify a BYTE or TEXT data type, you can specify the location in which it is stored. You can store data with the table or in a separate blobspace. In Dynamic Server, if you are creating a named ROW data type that has a BYTE or TEXT field, you cannot use the IN clause to specify a separate storage space. The following example shows how blobspaces and dbspaces are specified. The user creates the resume table. The data values are stored in the employ dbspace. The data in the vita column is stored with the table, but the data associated with the photo column is stored in a blobspace named photo_space.
CREATE TABLE resume ( fname CHAR(15), lname CHAR(15), phone CHAR(18), recd_date DATETIME YEAR TO HOUR, contact_date DATETIME YEAR TO HOUR, comments VARCHAR(250, 100), vita TEXT IN TABLE, photo BYTE IN photo_space ) IN employ
Smart-Large-Object Data Types (IDS): A smart-large-object data type stores text or binary data in sbspaces. The database server can provide random access to a smart-large-object value. That is, it can access any portion of the smart-large-object value. These data types are recoverable. The following table summarizes the smart-large-object data types that Dynamic Server supports. Data Type BLOB CLOB Description Stores binary data of up to 4 terabytes (4*240 bytes) Stores text data of up to 4 terabytes (4*240 bytes)
Both of these are built-in opaque data types. Like other opaque types, they cannot be accessed in a database of a non-local database server by a distributed query or by other DML operations, nor can they be returned from a database of another database server by a UDR. For information on accessing BLOB or CLOB values in other databases of the local server, however, see BOOLEAN and Other Built-In Opaque Data Types (IDS) on page 4-19. Smart large object data types are not parallelizable. The PDQ feature of Dynamic Serve has no effect on operations that load or unload BLOB or CLOB values, or that process them in queries or in other DML operations. For more information about these smart blob data types, see the IBM Informix Guide to SQL: Reference. For information on how to create blobspaces, see your IBM Informix Administrator's Guide. For information about optical families, see the IBM Informix Optical Subsystem Guide. For information about the built-in functions that you can use to import, export, and copy smart large objects, see Smart-Large-Object Functions (IDS) on page 4-91 and the IBM Informix Guide to SQL: Tutorial.
4-26
Data Type
(3)
Notes: 1 2 3 See INTERVAL Field Qualifier on page 4-127 Informix extension See DATETIME Field Qualifier on page 4-32
The following table summarizes the built-in time data types. Data Type DATE DATETIME Description Stores a date value as a Julian date in the range from January 1 of the year 1 up to December 31, 9999. Stores a point-in-time date (year, month, day) and time-of-day (hour, minute, second, and fraction of second), in the range of years 1 to 9999. Also supports contiguous subsets of these time units. Stores spans of time, in years and/or months, or in smaller time units (days, hours, minutes, seconds, and/or fractions of second), with up to 9 digits of precision in the largest time unit, if this is not FRACTION. Also supports contiguous subsets of these time units.
INTERVAL
Notes: 1
Element distinct_type opaque_type Description Distinct data type with same structure as an existing data type Name of the opaque data type
Must be unique among data type Identifier on page names in the database 5-22 Must be unique among data type Identifier on page names in the database 5-22
4-27
Data Type
In this manual, user-defined data type is usually abbreviated as UDT.
Notes: 1 2 See CREATE ROW TYPE on page 2-158 See Collection Data Types on page 4-30
A single complex data type can include multiple components. When you create a complex type, you define the components of the complex type. Unlike an opaque
4-28
Data Type
type, however, a complex type is not encapsulated. You can use SQL to access the individual components of a complex data type. The individual components of a complex data type are called elements. Dynamic Server supports the following categories of complex data types: v ROW data types: Named ROW types and unnamed ROW types v COLLECTION data types: SET, MULTISET, and LIST The elements of a COLLECTION data type must all be of the same data type. You can use the keyword COLLECTION in SPL data type declarations to specify an untyped collection variable. NULL values are not supported in elements of COLLECTION data types. The elements of a ROW data type can be of different data types, but the pattern of data types from the first to the last element cannot vary for a given ROW data type. NULL values are supported in elements of ROW data types, unless you specify otherwise in the data type declaration or in a constraint.
Notes: 1 2
Element data_type field row_type Description Data type of field Name of a field within row_type Some ROW data type defined by CREATE ROW TYPE statement
See Owner Name on page 5-43 See CREATE ROW TYPE on page 2-158
Restrictions Any data type except BYTE or TEXT Must be unique among fields of the same ROW type ROW type must exist in the database Syntax Data Type on page 4-18 Identifier on page 5-22 Identifier on page 5-22; Data Type on page 4-18
4-29
Data Type
You can assign a named ROW type to a table, to a column, or to an SPL variable. A named ROW type that you use to create a typed table or to define a column must already exist. For information on how to create a named ROW data type, see CREATE ROW TYPE on page 2-158. To specify a named ROW data type in an ANSI-compliant database, you must qualify the row_type with its owner name, if you are not the owner of row_type. An unnamed ROW data type is identified by its structure, which specifies fields that you create with its ROW constructor. You can define a column or an SPL variable as an unnamed ROW data type. For the syntax to specify values for an unnamed ROW type, see ROW Constructors on page 4-64.
Element data_type
Restrictions Can be any data type except TEXT, BYTE, SERIAL, or SERIAL8
A SET is an unordered collection of elements, each of which has a unique value. Define a column as a SET data type when you want to store collections whose elements contain no duplicate values and have no associated order. A MULTISET is an unordered collection of elements in which elements can have duplicate values. You can define a column as a MULTISET collection type when you want to store collections whose elements might not be unique and have no specific order associated with them. A LIST is an ordered collection of elements that can include duplicate elements. A LIST differs from a MULTISET in that each element in a LIST collection has an ordinal position in the collection. You can define a column as a LIST collection type when you want to store collections whose elements might not be unique but have a specific order associated with them. The keyword COLLECTION can be used in SPL data type declarations to specify an untyped collection variable. Defining the Element Type: The element type can be any data type except TEXT, BYTE, SERIAL, or SERIAL8. You can nest collection types, using elements of a collection type. Every element must be of the same type. For example, if the element type of a collection data type is INTEGER, every element must be of type INTEGER.
4-30
Data Type
An exception to this restriction occurs if the database server determines that some elements of a collection of character strings are VARCHAR data types (whose length is limited to 255 or fewer bytes) but other elements are longer than 255 bytes. In this case, the collection constructor can assign a CHAR(n) data type to all elements, for n the length in bytes of the longest element. If this is undesirable, you can cast the collection to LVARCHAR, to prevent padding extra length in elements of the collection, as in this example:
LIST {first character string longer than 255 bytes . . . , second character string longer than 255 bytes . . . , another character string} ::LIST (LVARCHAR NOT NULL)
See Collection Constructors on page 4-65 for additional information. If the element type of a collection is an unnamed ROW type, the unnamed ROW type cannot contain fields that hold unnamed ROW types. That is, a collection cannot contain nested unnamed ROW data types. The elements of a collection cannot be NULL. When you define a column as a collection data type, you must use the NOT NULL keywords to specify that the elements of the collection cannot be NULL. Privileges on a collection data type are those of the database column. You cannot specify privileges on individual elements of a collection.
Related Information
For more information about choosing a data type for your database, see the IBM Informix Database Design and Implementation Guide. For more information about the specific qualities of individual data types, see the chapter on data types in the IBM Informix Guide to SQL: Reference. For more information about data types for storing character data in multibyte locales, see the discussion of the NCHAR and NVARCHAR data types and the GLS aspects of other character data types in the IBM Informix GLS User's Guide.
4-31
Syntax
DATETIME Field Qualifier:
YEAR TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO TO YEAR MONTH DAY HOUR MINUTE SECOND FRACTION ( scale ) MONTH MONTH DAY HOUR MINUTE SECOND FRACTION ( scale ) DAY DAY HOUR MINUTE SECOND FRACTION ( HOUR HOUR MINUTE SECOND FRACTION ( scale ) MINUTE TO MINUTE TO SECOND TO FRACTION ( scale ) SECOND TO SECOND TO FRACTION ( scale ) FRACTION TO FRACTION ( scale ) scale )
Element scale
Usage
This segment specifies the precision and scale of a DATETIME data type. Specify, as the first keyword, the largest time unit that the DATETIME column will store. After the keyword TO, specify the smallest unit as the last keyword. These
4-32
An error results if the first keyword represents a smaller time unit than the last, or if you use the plural form of a keyword (such as MINUTES). Operations on DATETIME values that do not include YEAR in their qualifier use values from the system clock-calendar to supply any additional precision. If the first term in the qualifier is DAY, and the current month has fewer than 31 days, unexpected results can occur.
Related Information
For an explanation of the DATETIME Field Qualifier, see the discussion of the DATETIME data type in the IBM Informix Guide to SQL: Reference. For important differences between the syntax of DATETIME and INTERVAL field qualifiers, see INTERVAL Field Qualifier on page 4-127.
4-33
Expression
Data values in SQL statements must be represented as expressions. An expression is a specification, which can include operators, operands, and parentheses, that the database server can evaluate to one or more values, or to a reference to some database object. Expressions can refer to values already in a table of the database, or to values derived from such data, but some expressions (such as TODAY, USER, or literal values) can return values that are independent of the database. You can use expressions to specify values in data-manipulation statements, to define fragmentation strategies, and in other contexts. Use the Expression segment whenever you see a reference to an expression in a syntax diagram. In most contexts, however, you are restricted to expressions whose returned value is of some specific data type, or of a data type that can be converted by the database server to some required data type. For an alphabetical listing of the built-in operators and functions that are described in this segment, see List of Expressions on page 4-36.
Syntax
The sections that follow describe SQL expressions, which are specifications that return one or more values or references to database objects. IBM Informix database servers support the following categories of expressions: SQL Expressions:
Binary Operators (1) Cast Expressions + (2) Column Expressions (3) Conditional Expressions (4) Constant Expressions (5) Constructor Expressions (6) Function Expressions (7) Statement-Local Variable Expressions (8) Aggregate Expressions NULL variable (9) SPL_variable ( Expression )
4-34
Expression
Binary Operators:
+ * / ||
Notes: 1 2 3 4 5 6 7 8 9
Element SPL_variable Description In an SPL routine, a variable that contains some expression type that the syntax diagram shows Host or program variable that contains some expression type that the syntax diagram shows
See page 4-42 See page 4-44 See page 4-49 See page 4-53 See page 4-63 See page 4-68 See page 4-114 See page 4-116 Stored Procedure Language only
Restrictions Must conform to the rules for expressions of that type Must conform to the rules for expressions of that type Syntax Identifier, p. 5-22
variable
Usage
The following table lists the types of SQL expressions, as identified in the diagram for Expression on page 4-34, and describes what each type returns.
Expression Type Aggregate functions Arithmetic operators Concatenation operator Cast operators Column expressions Conditional expressions Constant expressions Constructor expressions Function expressions Statement-Local-Variable expressions Description Returns values from built-in or from user-defined aggregates Supports arithmetic operations on one (unary operators) or two (binary operators) numeric operands Concatenates two string values Explicit casts from one data type to another Full or partial column values Returns values that depend on conditional tests Literal values in data manipulation (DML) statements Dynamically creates values for complex data types Returns values from built-in or user-defined functions Specifies how you can use a defined statement-local variable (SLV) elsewhere in an SQL statement
4-35
Expression
You can also use host variables or SPL variables as expressions. For a complete list with page references to this chapter, see the following List of Expressions.
List of Expressions
Each category of SQL expression includes many individual expressions. The following table lists all the SQL expressions (and some operators) in alphabetical order. The columns in this table have the following meanings: v Name gives the name of each expression. v Description gives a short description of each expression. v Syntax lists the page that shows the syntax of the expression. v Usage shows the page that describes the usage of the expression. Each expression listed in the following table is supported on all database servers unless otherwise noted. When an expression is not supported on all database servers, the Name column notes in parentheses the database server or servers that do support the expression.
Name ABS function ACOS function Addition ( + ) operator ASIN function ATAN function ATAN2 function AVG function CARDINALITY function (IDS) CASE expression CAST expression (IDS) Cast ( :: ) operator Description Returns absolute value of a numeric argument Returns the arc cosine of a numeric argument Returns the sum of two numeric operands Returns the arc sine of a numeric argument Returns the arc tangent of numeric argument Computes the angular component of the polar coordinates (r, q) associated with (x, y) Returns the mean of a set of numeric values Syntax p. 4-69 p. 4-100 p. 4-34 p. 4-100 p. 4-100 p. 4-100 p. 4-116 Usage p. 4-70 p. 4-101 p. 4-40 p. 4-101 p. 4-102 p. 4-102 p. 4-118 p. 4-72 p. 4-49 p. 4-42 p. 4-42 p. 4-90 p. 4-90 p. 4-44 p. 4-41 p. 4-53 p. 4-101 p. 4-119 p. 4-120 p. 4-120 p. 4-119 p. 4-119
Returns the number of elements in a collection data p. 4-72 type (SET, MULTISET, or LIST) Returns a value that depends on which of several conditional tests evaluates to true Converts an expression to a specified data type See Double-colon ( :: ) cast operator p. 4-49 p. 4-42 p. 4-42 p. 4-89 p. 4-89 p. 4-44 p. 4-34 p. 4-53 p. 4-100 p. 4-116 p. 4-116 p. 4-116 p. 4-116 p. 4-116
CHARACTER_LENGTH function See CHAR_LENGTH function. (In multibyte locales, this replaces the LENGTH function.) CHAR_LENGTH function Column expression Concatenation ( || ) operator Constant expression COS function COUNT (as a set of functions) COUNT (ALL column) function COUNT (column) function COUNT DISTINCT function COUNT UNIQUE function Returns count of logical characters in a string Complete or partial column value from a table Concatenates the results of two expressions Expression with a literal, fixed, or variant value Returns the cosine of a radian expression Functions that return frequency counts Each form of the COUNT function is listed below. See COUNT (column) function. Returns the number of non-NULL values in a specified column Returns the number of unique non-NULL values in a specified column See COUNT DISTINCT function.
4-36
Expression
Name COUNT (*) function CURRENT operator CURRENT_ROLE operator sequence.CURRVAL (IDS) DATE function DAY function DBINFO (as a set of functions) Description Returns the cardinality of the set of rows that satisfy a query Syntax p. 4-116 Usage p. 4-119 p. 4-58 p. 4-56 p. 4-61 p. 4-97 p. 4-98 p. 4-73
Returns the current time as a DATETIME value that p. 4-53 consists of the date and the time of day Returns the currently enabled role of the user Returns the current value of specified sequence Converts a nondate argument to a DATE value Returns the day of the month as an integer Provides a set of functions for retrieving different types of database information. To invoke each function, specify the appropriate DBINFO option. Each option is listed below. Returns the coserver ID of the coserver where each row of a specified table is located Returns the coserver ID of the coserver to which the user who entered the query is connected Returns the hostname of the database server to which a client application is connected p. 4-53 p. 4-53 p. 4-96 p. 4-96 p. 4-72
DBINFO (coserverid string followed by table. column and the currentrow string) (XPS) DBINFO (coserverid string with no other arguments) (XPS) DBINFO (dbhostname option) DBINFO (dbspace string followed by table.column and the currentrow string) (XPS) DBINFO (dbspace string followed by a tblspace number) DBINFO (serial8 option) DBINFO (sessionid option) DBINFO (sqlca.sqlerrd1 option) DBINFO (sqlca.sqlerrd2 option)
p. 4-72
p. 4-78
p. 4-72 p. 4-72
Returns the name of the dbspace where each row of p. 4-72 a specified table is located Returns the name of a dbspace corresponding to a tblspace number Returns most recently inserted SERIAL8 value Returns the session ID of the current session Returns the last serial value inserted in a table Returns the number of rows processed by DML statements, and by EXECUTE PROCEDURE and EXECUTE FUNCTION statements Returns exact version of the database server to which a client application is connected Returns the name of the database server Evaluates one or more expression pairs and compares the when expression in each pair with a specified value expression p. 4-72 p. 4-72 p. 4-72 p. 4-72 p. 4-72
DECRYPT_CHAR function (IDS) DECRYPT_BINARY function (IDS) DEFAULT_ROLE operator Division ( / ) operator Double-colon ( :: ) cast operator (IDS) Double-pipe ( || ) concatenation operator
Returns a plain-text string or CLOB after processing p. 4-79 an encrypted argument Returns a plain-text BLOB data value after processing an encrypted BLOB argument Returns the default role of the current user Returns the quotient of two numeric operands Converts the value of an expression to a specified data type Returns a string that joins one string operand to another string operand p. 4-79 p. 4-53 p. 4-34 p. 4-42 p. 4-34
4-37
Expression
Name ENCRYPT_AES function (IDS) ENCRYPT_TDES function (IDS) EXP function EXTEND function FILETOBLOB function (IDS) FILETOCLOB function (IDS) GETHINT function (IDS) HEX function Host variable Description Returns an encrypted string or BLOB after processing a plain-text string, BLOB, or CLOB Returns an encrypted string or BLOB after processing a plain-text string, BLOB, or CLOB Returns the exponent of a numeric expression Resets precision of DATETIME or DATE value Creates a BLOB value from data stored in a specified operating-system file Creates a CLOB value from data stored in a specified operating-system file Returns a plain-text hint string after processing an encrypted data-string argument Returns the hexadecimal encoding of a base-10 integer argument See Variable. Syntax p. 4-79 p. 4-79 p. 4-87 p. 4-96 p. 4-91 p. 4-91 p. 4-79 p. 4-88 p. 4-34 p. 4-111 p. 4-90 p. 4-109 p. 4-89 p. 4-65 p. 4-53 p. 4-53 p. 4-53 p. 4-53 p. 4-53 p. 4-53 p. 4-53 p. 4-91 p. 4-87 p. 4-87 p. 4-91 p. 4-109 p. 4-107 p. 4-116 p. 4-96 p. 4-116 p. 4-69 Usage p. 4-86 p. 4-86 p. 4-87 p. 4-98 p. 4-91 p. 4-91 p. 4-87 p. 4-88 p. 4-34 p. 4-111 p. 4-90 p. 4-110 p. 4-89 p. 4-65 p. 4-53 p. 4-63 p. 4-60 p. 4-60 p. 4-55 p. 4-53 p. 4-63 p. 4-95 p. 4-88 p. 4-88 p. 4-93 p. 4-110 p. 4-107 p. 4-122 p. 4-99 p. 4-122 p. 4-70
IFX_ALLOW_NEWLINE function Sets a newline session mode that allows or disallows newline characters in quoted strings IFX_REPLACE_MODULE function (IDS) INITCAP function LENGTH function LIST collection constructor (IDS) Literal BOOLEAN Literal collection (IDS) Literal DATETIME Literal INTERVAL Literal number Literal opaque type (IDS) Literal row (IDS) LOCOPY function (IDS) LOGN function LOG10 function LOTOFILE function (IDS) LOWER function LPAD function MAX function MDY function MIN function MOD function Replaces a loaded shared-object file with a new version that has a different name or location Converts a string argument to a string in which only the initial letter of each word is uppercase Returns the number of bytes in a character column, not including any trailing blank spaces Constructor for collections whose elements are ordered and can contain duplicate values Literal representation of a BOOLEAN value Represents elements in a collection data type Represents a DATETIME value Represents an INTERVAL value Represents a numeric value Represents an opaque data type Represents the elements in a ROW data type Creates a copy of a smart large object Returns the natural log of a numeric argument Returns the base-10 logarithm of an argument Copies a BLOB or CLOB value to a file Converts uppercase letters to lowercase Returns a string that is left-padded by a specified number of pad characters Returns the largest in a specified set of values Returns a DATE value from integer arguments Returns the smallest in a specified set of values Returns the modulus (the integer-division remainder value) from two numeric arguments
4-38
Expression
Name MONTH function Multiplication ( * ) operator MULTISET collection constructor (IDS) sequence.NEXTVAL (IDS) NULL keyword NVL function OCTET_LENGTH function POW function Procedure-call expression Program variable Quoted string RANGE function REPLACE function ROOT function ROUND function ROW constructor (IDS) RPAD function SET collection constructor (IDS) SIN function SITENAME function SPL routine expression SPL variable SQRT function Statement-Local-Variable expression STDEV function SUBSTR function SUBSTRING function Substring ( [ x, y ] ) operator Subtraction ( - ) operator SUM function TAN function TO_CHAR function TO_DATE function TODAY operator TRIM function TRUNC function Description Returns the month value from a DATE or DATETIME argument Returns the product of two numeric operands Constructor for a non-ordered collection of elements that can contain duplicate value Increments the value of the specified sequence Unknown, missing, or logically undefined value Returns the value of a not-NULL argument, or a specified value if the argument is NULL Returns the number of bytes in a character column, including any trailing blank spaces Raises a base value to a specified power See user-defined function. See variable. Literal character string Returns the range of a specified set of values Replaces specified characters in a source string Returns the root value of a numeric argument Returns the rounded value of an argument Constructor for a named ROW data type Returns a string that is right-padded by a specified number of pad characters Syntax p. 4-96 p. 4-34 p. 4-65 p. 4-53 p. 4-66 p. 4-52 p. 4-89 p. 4-69 p. 4-111 p. 4-34 p. 4-53 p. 4-116 p. 4-107 p. 4-69 p. 4-69 p. 4-63 p. 4-108 Usage p. 4-98 p. 4-40 p. 4-65 p. 4-61 p. 4-67 p. 4-52 p. 4-90 p. 4-70 p. 4-111 p. 4-34 p. 4-55 p. 4-122 p. 4-107 p. 4-70 p. 4-71 p. 4-64 p. 4-108 p. 4-65 p. 4-101 p. 4-57 p. 4-111 p. 4-34 p. 4-71 p. 4-114 p. 4-123 p. 4-105 p. 4-104 p. 2-508 p. 4-40 p. 4-122 p. 4-101 p. 4-99 p. 4-100 p. 4-57 p. 4-102 p. 4-71
Constructor for an unordered collection of elements p. 4-65 in which each value is unique Returns the sine of a radian expression See DBSERVERNAME function. See User-defined functions SPL variable that stores an expression Returns the square root of a numeric argument A statement-local variable (SLV) whose scope is restricted to the same SQL statement Returns the standard deviation of a data set Returns a substring of a string argument Returns a substring of a source string Returns a substring of a string operand Returns the difference between two numbers Returns the sum of a specified set of values Returns the tangent of a radian expression Converts a DATE or DATETIME to a string Converts a string to a DATETIME value Returns the current system date Drops pad characters from a string argument Returns a truncated numeric value p. 4-100 p. 4-53 p. 4-111 p. 4-34 p. 4-69 p. 4-113 p. 4-116 p. 4-105 p. 4-104 p. 4-44 p. 4-34 p. 4-116 p. 4-100 p. 4-96 p. 4-96 p. 4-53 p. 4-102 p. 4-69
4-39
Expression
Name Unary minus ( - ) sign Unary plus ( + ) sign UNITS operator UPPER function User-defined aggregate (IDS) User-defined function USER operator Variable VARIANCE function WEEKDAY function YEAR function * symbol + symbol - symbol / symbol :: symbols || symbol [ first, last ] symbols Description Specifies a negative ( < 0 ) numeric value Specifies a positive ( > 0 ) numeric value . Convert an integer to an INTERVAL value Converts lowercase letters to uppercase Aggregate that you define (as opposed to built-in aggregates that Dynamic Server provides) Function that you write (as opposed to built-in functions that the database server provides) Returns the login name of the current user Host or program variable that stores a value Returns the variance for a set of values Returns an integer code for the day of the week Returns a 4-digit integer representing a year See Multiplication ( * ) operator See Addition and Unary plus ( + ) sign See Subtraction and Unary minus ( - ) sign See Division operator See Double-colon ( :: ) cast operator See Double-pipe ( || ) concatenation operator See Substring operator Syntax p. 4-34 p. 4-34 p. 4-53 p. 4-109 p. 4-125 p. 4-111 p. 4-53 p. 4-34 p. 4-116 p. 4-96 p. 4-96 p. 4-34 p. 4-34 p. 4-34 p. 4-34 p. 4-42 p. 4-34 p. 4-34 Usage p. 4-40 p. 4-40 p. 4-60 p. 4-110 p. 4-125 p. 4-111 p. 4-55 p. 4-34 p. 4-123 p. 4-98 p. 4-98 p. 4-40 p. 4-40 p. 4-40 p. 4-40 p. 4-42 p. 4-41 p. 4-40
Sections that follow describe the syntax and usage of each expression that appears in the preceding table.
Arithmetic Operators
Binary arithmetic operators can combine expressions that return numbers.
Arithmetic Operation Addition Subtraction Arithmetic Operator + Operator Function plus( ) minus( ) Arithmetic Operation Multiplication Division Arithmetic Operator * / Operator Function times( ) divide( )
If you combine a DATETIME value with one or more INTERVAL values, all the fields of the INTERVAL value must be present in the DATETIME value; no implicit EXTEND function is performed. In addition, you cannot use YEAR to MONTH intervals with DAY to SECOND intervals. For additional information about binary arithmetic operators, see the IBM Informix Guide to SQL: Reference. The binary arithmetic operators have associated operator functions, as the preceding table shows. Connecting two expressions with a binary operator is equivalent to invoking the associated operator function on the expressions. For
4-40
Expression
example, the following two statements both select the product of the total_price column and 2. In the first statement, the * operator implicitly invokes the times( ) function.
SELECT (total_price * 2) FROM items WHERE order_num = 1001 SELECT times(total_price, 2) FROM items WHERE order_num = 1001
You cannot use arithmetic operators to combine expressions that use aggregate functions with column expressions. The database server provides the operator functions associated with the relational operators for all built-in data types. You can define new versions of these operator functions to handle your own user-defined data types. For more information, see IBM Informix User-Defined Routines and Data Types Developer's Guide. The database server also supports the following unary arithmetic operators.
Sign of Number Positive Negative Unary Arithmetic Operator + Operator Function positive( ) negate( )
The unary arithmetic operators have the associated operator functions that the preceding table shows. You can define new versions of these functions to handle your own user-defined data types. For more information on this topic, see IBM Informix User-Defined Routines and Data Types Developer's Guide. If any value that participates in an arithmetic expression is NULL, the value of the entire expression is NULL, as the following example shows:
SELECT order_num, ship_charge/ship_weight FROM orders WHERE order_num = 1023
If either ship_charge or ship_weight is NULL, the value returned for the expression ship_charge/ship_weight is also NULL. If the NULL expression ship_charge/ship_weight is used in a condition, its truth value cannot be TRUE, and the condition is not satisfied (unless the NULL expression is an operand of the IS NULL operator).
Concatenation Operator
You can use the concatenation operator ( || ) to concatenate two expressions. These examples show some possible concatenated-expression combinations. v The first example concatenates the zipcode column to the first three letters of the lname column. v The second example concatenates the suffix .dbg to the contents of a host variable called file_variable. v The third example concatenates the value that the TODAY operator returns to the string Date.
lname[1,3] || zipcode :file_variable || .dbg Date: || TODAY
Chapter 4. Data Types and Expressions
4-41
Expression
You cannot use the concatenation operator in an embedded-language-only statement. The ESQL/C-only statements of SQL appear in the following list:
ALLOCATE COLLECTION ALLOCATE DESCRIPTOR ALLOCATE ROW CLOSE CREATE FUNCTION FROM CREATE PROCEDURE FROM CREATE ROUTINE FROM DEALLOCATE COLLECTION DEALLOCATE DESCRIPTOR DEALLOCATE ROW DECLARE DESCRIBE DESCRIBE INPUT EXECUTE EXECUTE IMMEDIATE FETCH FLUSH FREE GET DESCRIPTOR GET DIAGNOSTICS OPEN PREPARE PUT SET AUTOFREE SET CONNECTION SET DESCRIPTOR WHENEVER
You can use the concatenation operator in a SELECT, INSERT, EXECUTE FUNCTION, or EXECUTE PROCEDURE statement within the DECLARE statement. You can use the concatenation operator in the SQL statement or statements in the PREPARE statement. The concatenation operator ( || ) has an associated operator function called concat( ). You can define a concat( ) function to handle your own string-based user-defined data types. For more information, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
Notes: 1
Element target_data_type
4-42
Expression
The following examples show two different ways of finding the integer equivalent of the expression expr. Both require the existence of an implicit or explicit cast from the data type of expr to the INTEGER data type:
CAST (expr AS INTEGER) expr::INTEGER
In the following example, the user casts a BYTE column to the BLOB type and copies the BLOB data to an operating-system file:
SELECT LOTOFILE(mybytecol::blob, fname, client) FROM mytab WHERE pkey = 12345
In the following example, the user casts a TEXT column to a CLOB value and then updates a CLOB column in the same table to have the CLOB value derived from the TEXT column:
UPDATE newtab SET myclobcol = mytextcol::clob
The keyword NULL has a global scope of reference within expressions. In SQL, the keyword NULL is the only syntactic mechanism for accessing a NULL value. Any attempt to redefine or restrict the global scope of the keyword NULL (for example,
4-43
Expression
declaring an SPL variable called null) disables any cast expression that involves a NULL value. Make sure that the keyword NULL receives its global scope in all expression contexts.
Column Expressions
A column expression specifies a data value in a column in the database, or a substring of the value, or (for Dynamic Server only) a field within a ROW-type column. This is the syntax for column expressions. Column Expressions:
table. view. synonym. alias. column (1) [ (2) (1) ROWID row_column .* (3) . field_name (2) row_col_expr .* (3) . field_name first, last ]
Notes: 1 2 3
Element alias Description Temporary alternative name for a table or view, declared in the FROM clause of a query Name of a column
Informix extension Dynamic Server only Use path no more than three times
Restrictions Restrictions depend on the clause of the SELECT statement in which alias occurs Restrictions depend on the SQL statement where column occurs Syntax Identifier, p. 5-22 Identifier, p. 5-22 Identifier, p. 5-22 Literal Number, p. 4-137 Expression, p. 4-34 Identifier, p. 5-22
column field_name
Name of a ROW field in the ROW Must be a member of the row that column or ROW-column expression row-column name or row_col_expr or field name (for nested rows) specifies Integers indicating positions of first The column must be of type CHAR, and last characters within column VARCHAR, NCHAR, NVARCHAR, BYTE, or TEXT, and 0 < first last Expression that returns ROW-type values Name of a ROW-type column Table, view, or synonym (for the table or view) that contains column Must return a ROW data type Must be a named ROW data type or an unnamed ROW data type
first, last
Synonym and the table or view to which Database Object it points must exist Name, p. 5-17
4-44
Expression
company items.price cat_advert [1,15]
You must qualify the column name with a table name or alias whenever it is necessary to distinguish between columns that have the same name but are in different tables. The SELECT statements that the following example shows use customer_num from the customer and orders tables. The first example precedes the column names with table names. The second example precedes the column names with table aliases.
SELECT * FROM customer, orders WHERE customer.customer_num = orders.customer_num; SELECT * FROM customer c, orders o WHERE c.customer_num = o.customer_num;
This use of dot notation is called a field projection. For example, suppose you have a column called rect with the following definition:
CREATE TABLE rectangles ( area float, rect ROW(x int, y int, length float, width float) );
The following SELECT statement uses dot notation to access field length of the rect column:
SELECT rect.length FROM rectangles WHERE area = 64;
Selecting All Fields of a Column with Asterisk Notation: If you want to select all fields of a column that has a ROW type, you can specify the column name without dot notation. For example, you can select all fields of the rect column as follows:
SELECT rect FROM rectangles WHERE area = 64;
You can also use asterisk ( * ) notation to project all the fields of a column that has a ROW data type. For example, if you want to use asterisk notation to select all fields of the rect column, you can enter the following statement:
4-45
Expression
SELECT rect.* FROM rectangles WHERE area = 64;
Asterisk notation is easier than specifying each field of the rect column individually:
SELECT rect.x, rect.y, rect.length, rect.width FROM rectangles WHERE area = 64;
Asterisk notation is valid only in the projection list of a SELECT statement. It can specify all fields of a ROW-type column or the data that a ROW-column expression returns. Asterisk notation is not necessary with ROW-type columns because you can specify the column name alone to project all of its fields. Asterisk notation is quite helpful, however, with ROW-type expressions such as subqueries and user-defined functions that return ROW-type values. For more information, see Using Dot Notation with Row-Type Expressions on page 4-47. You can use asterisk notation with columns and expressions of ROW data types in the projection list of a SELECT statement only. You cannot use asterisk notation with columns and expressions of ROW type in any other clause of a SELECT statement. Selecting Nested Fields: When the ROW type that defines a column itself contains other ROW types, the column contains nested fields. Use dot notation to access these nested fields within a column. For example, assume that the address column of the employee table contains the fields: street, city, state, and zip. In addition, the zip field contains the nested fields: z_code and z_suffix. A query on the zip field returns values for the z_code and z_suffix fields. You can specify, however, that a query returns only specific nested fields. The following example shows how to use dot notation to construct a SELECT statement that returns rows for the z_code field of the address column only:
SELECT address.zip.z_code FROM employee;
Rules of Precedence: The database server uses the following precedence rules to interpret dot notation: 1. schema name_a . table name_b . column name_c . field name_d 2. table name_a . column name_b . field name_c . field name_d 3. column name_a . field name_b . field name_c . field name_d When the meaning of an identifier is ambiguous, the database server uses precedence rules to determine which database object the identifier specifies. Consider the following two tables:
CREATE TABLE b (c ROW(d INTEGER, e CHAR(2)); CREATE TABLE c (d INTEGER);
In the following SELECT statement, the expression c.d references column d of table c (rather than field d of column c in table b) because a table identifier has a higher precedence than a column identifier:
SELECT * FROM b,c WHERE c.d = 10;
4-46
Expression
For more information about precedence rules and how to use dot notation with ROW columns, see the IBM Informix Guide to SQL: Tutorial. Using Dot Notation with Row-Type Expressions: Besides specifying a column of a ROW data type, you can also use dot notation with any expression that evaluates to a ROW type. In an INSERT statement, for example, you can use dot notation in a subquery that returns a single row of values. Assume that you created a ROW type named row_t:
CREATE ROW TYPE row_t (part_id INT, amt INT);
Also assume that you created a typed table named tab1 that is based on the row_t ROW type:
CREATE TABLE tab1 OF TYPE row_t;
Assume also that you inserted the following values into table tab1:
INSERT INTO tab1 VALUES (ROW(1,7)); INSERT INTO tab1 VALUES (ROW(2,10));
Now you can use dot notation to insert the value from only the part_id column of table tab1 into the tab2 table:
INSERT INTO tab2 VALUES ((SELECT t FROM tab1 t WHERE part_id = 1).part_id);
The asterisk form of dot notation is not necessary when you want to select all fields of a ROW-type column because you can specify the column name alone to select all of its fields. The asterisk form of dot notation can be quite helpful, however, when you use a subquery, as in the preceding example, or when you call a user-defined function to return ROW-type values. Suppose that a user-defined function named new_row returns ROW-type values, and you want to call this function to insert the ROW-type values into a table. Asterisk notation makes it easy to specify that all the ROW-type values that the new_row( ) function returns are to be inserted into the table:
INSERT INTO mytab2 SELECT new_row (mycol).* FROM mytab1;
References to the fields of a ROW-type column or a ROW-type expression are not allowed in fragment expressions. A fragment expression is an expression that defines a table fragment or an index fragment in SQL statements like CREATE TABLE, CREATE INDEX, and ALTER FRAGMENT.
4-47
Expression
A conditional expression can include a column expression that uses the substring operator ( [ first, last ] ), as in the following example:
SELECT lname FROM customer WHERE phone[5,7] = 356;
Here the quotes are required, to prevent the database server from applying a numeric filter to the digits in the criterion value. For information on the GLS aspects of column subscripts and substrings, see the IBM Informix GLS User's Guide.
The last example shows how to get the page number (the first six digits after 0x) and the slot number (the last two digits) of the location of your row. You cannot use the ROWID keyword in the select list of the Projection clause of a query that contains an aggregate function.
4-48
Expression
When you select a smart-large-object column, you can assign the handle value to any number of columns: all columns with the same handle value share the CLOB or BLOB value. This storage arrangement reduces the amount of disk space that the CLOB or BLOB value, but when several columns share the same smart-large-object value, the following conditions result: v The chance of lock contention on a CLOB or BLOB column increases. If two columns share the same smart-large-object value, the data might be locked by either column that needs to access it. v The CLOB or BLOB value can be updated from a number of points. To remove these constraints, you can create separate copies of the BLOB or CLOB data for each column that needs to access it. You can use the LOCOPY function to create a copy of an existing smart large object. You can also use the built-in functions LOTOFILE, FILETOCLOB, and FILETOBLOB to access smart-large-object values, as described in Smart-Large-Object Functions (IDS) on page 4-91. For more information on the BLOB and CLOB data types, see the IBM Informix Guide to SQL: Reference.
Conditional Expressions
Conditional expressions return values that depend on the outcome of conditional tests. This diagram shows the syntax for Conditional Expressions. Conditional Expressions:
(1) CASE Expressions (2) NVL Function (3) DECODE Function
Notes: 1 2 3 See page 4-49 See page 4-52 See page 4-52
CASE Expressions
The CASE expression allows an SQL statement such as the SELECT statement to return one of several possible results, depending on which of several condition evaluates to true. The CASE expression has two forms: generic CASE expressions and linear CASE expressions. CASE Expressions:
(1) Generic CASE Expression (2) Linear CASE Expression
(3)
4-49
Expression
3 See page 4-51
You must include at least one WHEN clause in the CASE expression. Subsequent WHEN clauses and the ELSE clause are optional. You can use a generic or linear CASE expression wherever you can use a column expression in an SQL statement (for example, in the Projection clause a SELECT statement). Expressions in the search condition or the result value expression can contain subqueries, and you can nest a CASE expression in another CASE expression. When a CASE expression appears in an aggregate expression, you cannot use aggregate functions in the CASE expression. Generic CASE Expressions: A generic CASE expression tests for a true condition in a WHEN clause. If it finds a true condition, it returns the result specified in the THEN clause. Generic CASE Expression:
(1) CASE WHEN Condition THEN expr NULL END ELSE expr NULL
Notes: 1
Element expr Description Expression that returns some data type
The database server processes the WHEN clauses in the order that they appear in the statement. If the search condition of a WHEN clause evaluates to TRUE, the database server uses the value of the corresponding THEN expression as the result, and stops processing the CASE expression. If no WHEN condition evaluates to TRUE, the database server uses the ELSE expression as the overall result. If no WHEN condition evaluates to TRUE, and no ELSE clause was specified, the returned CASE expression value is NULL. You can use the IS NULL condition to handle NULL results. For information on how to handle NULL values, see IS NULL Condition on page 4-10. The next example shows a generic CASE expression in the Projection clause. In this example, the user retrieves the name and address of each customer as well as a calculated number that is based on the number of problems that exist for that customer:
SELECT cust_name, CASE WHEN number_of_problems = 0 THEN 100 WHEN number_of_problems > 0 AND number_of_problems < 4 THEN number_of_problems * 500 WHEN number_of_problems >= 4 and number_of_problems <= 9 THEN number_of_problems * 400 ELSE
4-50
Expression
(number_of_problems * 300) + 250 END, cust_address FROM custtab
In a generic CASE expression, all the results should be of the same data type, or they should evaluate to a common compatible data type. If the results in all the WHEN clauses are not of the same data type, or if they do not evaluate to values of mutually compatible types, an error occurs. Linear CASE Expressions (IDS): A linear CASE expression compares the value of the expression that follows the CASE keyword with an expression in a WHEN clause. Linear CASE Expression:
CASE expr
expr NULL
Element expr
Restrictions Data type of expr that follows the WHEN keyword must be compatible with data type of the expression that follows the CASE keyword. Data type of expr in the THEN clause must be compatible with data types of expressions in other THEN clauses.
The database server evaluates the expression that follows the CASE keyword, and then processes the WHEN clauses sequentially. If an expression after the WHEN keyword returns the same value as the expression that follows the CASE keyword, the database server uses the value of the expression that follows the THEN keyword as the overall result of the CASE expression. Then the database server stops processing the CASE expression. If none of the WHEN expressions return the same value as the expression that follows the CASE keyword, the database server uses the expression of the ELSE clause as the overall result of the CASE expression (or, if no ELSE clause was specified, the returned value of the CASE expression is NULL). The next example shows a linear CASE expression in the projection list of the Projection clause of a SELECT statement. For each movie in a table of movie titles, the query returns the title, the cost, and the type of the movie. The statement uses a CASE expression to derive the type of each movie:
SELECT title, CASE movie_type WHEN 1 THEN HORROR WHEN 2 THEN COMEDY WHEN 3 THEN ROMANCE WHEN 4 THEN WESTERN ELSE UNCLASSIFIED END, our_cost FROM movie_titles
In linear CASE expressions, the data types of WHEN clause expressions must be compatible with that of the expression that follows the CASE keyword.
4-51
Expression
NVL Function
The NVL expression returns different results, depending on whether its first argument evaluates to NULL. NVL Function:
NVL ( expr1 , expr2 )
NVL evaluates expression1. If expression1 is not NULL, then NVL returns the value of expression1. If expression1 is NULL, NVL returns the value of expression2. The expressions expression1 and expression2 can be of any data type, as long as they can be cast to a common compatible data type. Suppose that the addr column of the employees table has NULL values in some rows, and the user wants to be able to print the label Address unknown for these rows. The user enters the following SELECT statement to display the label Address unknown when the addr column has a NULL value:
SELECT fname, NVL (addr, Address unknown) AS address FROM employees
DECODE Function
The DECODE expression is similar to the CASE expression in that it can print different results depending on the values found in a specified column. DECODE Function:
, , NULL DECODE ( expr , when_expr , then_expr NULL ) , else_expr
Description
Restrictions
Expressions whose values Data types of when_expr and expr must be and data types can be compatible, as must then_expr and else_expr. evaluated Value of when_expr cannot be a NULL.
The expressions expr, when_expr, and then_expr are required. DECODE evaluates expr and compares it to when_expr. If the value of when_expr matches the value of expr, then DECODE returns then_expr. The expressions when_expr and then_expr are an expression pair, and you can specify any number of expression pairs in the DECODE function. In all cases, DECODE compares the first member of the pair against expr and returns the second member of the pair if the first member matches expr. If no expression matches expr, DECODE returns else_expr. If no expression matches expr and you specified no else_expr, then DECODE returns NULL. You can specify any data type for the arguments, but two restrictions exist:
4-52
Expression
v All instances of when_expr must have the same data type, or a common compatible type must exist. All instances of when_expr must also have the same (or a compatible) data type as expr. v All instances of then_expr must have the same data type, or a common compatible type must exist. All instances of then_expr must also have the same (or a compatible) data type as else_expr. Suppose that a user wants to convert descriptive values in the evaluation column of the students table to numeric values in the output. The following table shows the contents of the students table.
firstname Edward Joe evaluation Great Not done firstname Mary Jim evaluation Good Poor
The user now enters a query with the DECODE function to convert the descriptive values in the evaluation column to numeric equivalents:
SELECT firstname, DECODE(evaluation, Poor, 0, Fair, 25, Good, 50, Very Good, 75, Great, 100, -1) as grade FROM students
Constant Expressions
Certain expressions that return a fixed value are called constant expressions. Among these are the following operators (or system constants) whose returned values are determined at runtime: v CURRENT returns the current time and date, from the system clock. v CURRENT_ROLE returns the name of the role, if any, whose privileges are enabled for the current user. v DEFAULT_ROLE returns the name of the role, if any, that is the default role for the current user. v DBSERVERNAME returns the name of the current database server. v SITENAME is a synonym for DBSERVERNAME. v TODAY returns the current calendar date, from the system clock. v USER returns the login name (also called the authorization identifier) of the current user. Besides these operators, the term constant expression can also refer to a quoted string, to a literal value, or to the UNITS operator with its operands. The Constant Expression segment has the following syntax.
4-53
Expression
Constant Expressions:
(1) Quoted String (2) Literal Number USER (3) (4) CURRENT_ROLE DEFAULT_ROLE (4) SITENAME DBSERVERNAME TODAY CURRENT precision (5) Literal DATETIME (6) Literal INTERVAL num UNITS time_unit (3) sequence synonym (7) Literal Collection (8) Literal Row literal opaque type literal BOOLEAN owner . . CURRVAL NEXTVAL
Notes: 1 2 3 4 5 6 7 8 See page 4-142 See page 4-137 Dynamic Server only Informix extension See page 4-132 See page 4-135 See page 4-129 See page 4-139
4-54
Expression
Element literal Boolean literal opaque type num owner precision sequence synonym time_unit Description Literal representation of a BOOLEAN value Literal representation of value of an opaque data type How many of specified time units. See UNITS Operator on page 4-60. Name of the owner of sequence Precision of the returned DATETIME expression Name of a sequence Synonym for the name of a sequence Keyword to specify time unit: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, or FRACTION Restrictions Must be either t (TRUE) or f (FALSE) Must be recognized by the input support function of opaque type If num is not an integer, the fractional part is truncated Must own sequence None, but see Syntax reference in next column for valid syntax Must exist in current database Must exist in current database Must be one of the keywords at left. Case insensitive but cannot be enclosed within quotes Syntax Quoted string, p. 4-142 Defined by UDT developer Literal Number, p. 4-137 Owner, p. 5-43 DATETIME Qualifier, p. 4-32 Identifier, p. 5-22 Identifier, p. 5-22 See the Restrictions column.
Quoted String
The following examples show quoted strings as expressions:
SELECT The first name is , fname FROM customer INSERT INTO manufact VALUES (SPS, SuperSport) UPDATE cust_calls SET res_dtime = 1997-1-1 10:45 WHERE customer_num = 120 AND call_code = B
Literal Number
The following examples show literal numbers as expressions:
INSERT INTO items VALUES (4, 35, 52, HRO, 12, 4.00) INSERT INTO acreage VALUES (4, 5.2e4) SELECT unit_price + 5 FROM stock SELECT -1 * balance FROM accounts
USER Operator
The USER operator returns a string containing the login name (also called the authorization identifier) of the current user who is running the process. The following statements show how you might use the USER operator:
INSERT INTO cust_calls VALUES (221,CURRENT,USER,B,Decimal point off, NULL, NULL) SELECT * FROM cust_calls WHERE user_id = USER UPDATE cust_calls SET user_id = USER WHERE customer_num = 220
The USER operator does not change the lettercase of a user ID. If you use USER in an expression and the current user is Robertm, the USER operator returns Robertm, not robertm or ROBERTM.
4-55
Expression
If you specify USER as a default column value, column must be of type CHAR, VARCHAR, NCHAR, NVARCHAR, or (in Dynamic Server) LVARCHAR. If you specify USER as the default value for a column, the size of column should not be less than 32 bytes. You risk getting an error during operations such as INSERT or ALTER TABLE if the column length is too small to store the default value. In an ANSI-compliant database, if you do not enclose the owner name in quotes, the name of the table owner is stored as uppercase letters. If you use the USER operator as part of a condition, you must be sure that the way the user name is stored matches what the USER operator returns with respect to lettercase.
CURRENT_ROLE Operator
The CURRENT_ROLE operator returns a string that contains the name of the currently enabled role of the user who is running the session. This role was either set in the session explicitly, using the SET ROLE statement, or else implicitly as a default role when the current user connected to the database. If the user holds no role, or if no role that was granted to the user is currently enabled, CURRENT_ROLE returns a NULL value. If the user has been granted no role individually, but a default role has been granted to PUBLIC, and this default role has been explicitly or implicitly enabled, CURRENT_ROLE returns the name of this default role. The next statements show how you might use the CURRENT_ROLE operator:
select CURRENT_ROLE from systables where tabid = 1;)
The CURRENT_ROLE operator does not change the lettercase of the identifier of a role. If you use CURRENT_ROLE in an expression and your current role is Czarina, the CURRENT_ROLE operator returns Czarina, not czarina. If you specify CURRENT_ROLE as the default value for a column, the column must have a CHAR, VARCHAR, LVARCHAR, NCHAR, or NVARCHAR data type. Because the name of a role is an authorization identifier, truncation might occur if the column length is less than 32 bytes.
DEFAULT_ROLE Operator
The DEFAULT_ROLE operator evaluates to a string that contains the name of the default role that has been granted to the user who is running the session. This default role need not be currently enabled, but it must not have been revoked since the most recent GRANT DEFAULT ROLE statement that referenced the user or PUBLIC in the TO clause. If has no default role is explicitly defined for the current user, but PUBLIC has a default role, DEFAULT_ROLE returns the default role of PUBLIC. If the user has no default role, or if the default role that was most recently granted to the user explicitly, or as PUBLIC, was subsequently revoked by the REVOKE DEFAULT ROLE statement, DEFAULT_ROLE returns a NULL value. If the user has been granted no default role individually, but a default role has been granted to PUBLIC, the DEFAULT_ROLE operator returns the name of this default role. If no default role is currently defined for the user nor for PUBLIC, however, DEFAULT_ROLE returns NULL.
4-56
Expression
The SET ROLE statement has no effect on the DEFAULT_ROLE operator, but any access privileges of the default role are not necessarily available to the user if SET ROLE has activated some other role, or if SET ROLE specified NULL or NONE as the current role of the user. The next statements show how you might use the DEFAULT_ROLE operator:
select DEFAULT_ROLE from systables where tabid = 1;)
DEFAULT_ROLE does not change the lettercase of the identifier of a role. If you specify DEFAULT_ROLE as the default value for a column, the column must have a CHAR, VARCHAR, LVARCHAR, NCHAR, or NVARCHAR data type. Because the name of a role is an authorization identifier, truncation might occur if the column width is less than 32 bytes. (See Owner Name on page 5-43 for the syntax of authorization identifiers.)
TODAY Operator
Use the TODAY operator to return the system date as a DATE data type. If you specify TODAY as a default column value, the column must be a DATE column. The following examples show how you might use the TODAY operator in an INSERT, UPDATE, or SELECT statement:
Chapter 4. Data Types and Expressions
4-57
Expression
UPDATE orders (order_date) SET order_date = TODAY WHERE order_num = 1005 INSERT INTO orders VALUES (0, TODAY, 120, NULL, N, 1AUE217, NULL, NULL, NULL, NULL) SELECT * FROM orders WHERE ship_date = TODAY
For Extended Parallel Server, the value returned by TODAY defaults to the date from the system clock-calendar, but you can specify instead the date from some other time zone. You can specify a time zone by using the SET ENVIRONMENT CLIENT_TZ statement of SQL (or by setting the IBM_XPS_PARAMS environment variable) to specify an offset from Greenwich Mean Time (GMT). For code examples of setting non-default time zones, see CURRENT Operator on page 4-58. For information on how to set IBM_XPS_PARAMS, see the chapter on environment variables in the IBM Informix Guide to SQL: Reference.
CURRENT Operator
The CURRENT operator returns a DATETIME value with the date and time of day, showing the current instant. If you do not specify a DATETIME qualifier, the default qualifier is YEAR TO FRACTION(3). The USEOSTIME configuration parameter specifies whether or not the database server uses subsecond precision when it obtains the current time from the operating system. For more information on the USEOSTIME configuration parameter, see your IBM Informix Administrator's Reference. You can use CURRENT in any context where a literal DATETIME is valid. See Literal DATETIME on page 4-132). If you specify CURRENT as the default value for a column, it must be a DATETIME column and the qualifier of CURRENT must match the column qualifier, as the following example shows:
CREATE TABLE new_acct (col1 int, col2 DATETIME YEAR TO DAY DEFAULT CURRENT YEAR TO DAY)
If you use the CURRENT operator in more than once in a single statement, identical values might be returned by each instance of CURRENT. You cannot rely on CURRENT to return distinct values each time it executes. The returned value is based on the system clock and is fixed when any SQL statement starts. For example, any call to CURRENT from inside the SPL function that an EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement invokes returns the value of the system clock when the SPL function starts. CURRENT is always evaluated in the database server where the current database is located. If the current database is in a remote database server, the returned value is from the remote host. SQL is not a procedural language, and CURRENT might not execute in the lexical order of its position in a statement. You should not use CURRENT to mark the start, the end, nor a specific point in the execution of an SQL statement. If your platform does not provide a system call that returns the current time with subsecond precision, CURRENT returns a zero for the FRACTION field. In the following example, the first statement uses CURRENT in a WHERE condition. The second statement uses CURRENT as an argument to the DAY
4-58
Expression
function. The last query selects rows whose call_dtime value is within a range from the beginning of 1997 to the current instant:
DELETE FROM cust_calls WHERE res_dtime < CURRENT YEAR TO MINUTE SELECT * FROM orders WHERE DAY(ord_date) < DAY(CURRENT) SELECT * FROM cust_calls WHERE call_dtime BETWEEN 1997-1-1 00:00:00 AND CURRENT
Assume also that the IBM_XPS_PARAMS setting for CLIENT_TZ has the value +1, that the server time zone is at GMT+4 hours, that the current server date is 2005-05-15, and the current server time is 1:10:39. If the system clocks for client and server are both based on the same value for GMT, this implies a client time zone of GMT+1 hour, or 3 hours earlier than the time zone of the server. Now you enter the following SQL statements:
INSERT INTO testtab VALUES (1, TODAY, CURRENT); SELECT * FROM testtab WHERE intcol = 1;
The query returns 1 (from column intcol) and the following TODAY and CURRENT values:
2005-05-15 01:10:39.000 05/15/2005
The query returns 2 (from column intcol) and the following TODAY and CURRENT values:
2005-05-14 14:40:39 05/14/2005
This value is 10.5 hours earlier than the result from the first example. Note that values that have been stored into a database table are not affected by the CLIENT_TZ specification, as the following example illustrates. The CLIENT_TZ setting is used only when evaluating the CURRENT and TODAY operators as specific DATETIME and DATE values.
SET ENVIRONMENT CLIENT_TZ ON SELECT * FROM testtab WHERE intcol = 1;
4-59
Expression
The query returns 1 (from column intcol) and the following TODAY and CURRENT values:
2005-05-15 01:10:39 05/15/2005
It is also important to note that the INSERT statements above do not evaluate to the time on the client system. Instead, they are based on the time of the database server, as offset to the time zone of the client system. These two times might differ, if the server and client system clocks are not synchronized correctly.
Literal DATETIME
The following examples show literal DATETIME as an expression:
SELECT DATETIME (1997-12-6) YEAR TO DAY FROM customer UPDATE cust_calls SET res_dtime = DATETIME (1998-07-07 10:40) YEAR TO MINUTE WHERE customer_num = 110 AND call_dtime = DATETIME (1998-07-07 10:24) YEAR TO MINUTE SELECT * FROM cust_calls WHERE call_dtime = DATETIME (1998-12-25 00:00:00) YEAR TO SECOND
Literal INTERVAL
The following examples show literal INTERVAL as an expression:
INSERT INTO manufact VALUES (CAT, Catwalk Sports, INTERVAL (16) DAY TO DAY) SELECT lead_time + INTERVAL (5) DAY TO DAY FROM manufact
The second statement in the preceding example adds five days to each value of lead_time selected from the manufact table. For more information, see Literal INTERVAL on page 4-135.
UNITS Operator
The UNITS operator specifies an INTERVAL value whose precision includes only one time unit. You can use UNITS in arithmetic expressions that increase or decrease one of the time units in an INTERVAL or DATETIME value. If the n operand is not an integer, it is rounded down to the nearest whole number when the database server evaluates the expression. In the following example, the first SELECT statement uses the UNITS operator to select all the manufacturer lead times, increased by five days. The second SELECT statement finds all the calls that were placed more than 30 days ago. If the expression in the WHERE clause returns a value greater than 99 (maximum number of days), the query fails. The last statement increases the lead time for the ANZA manufacturer by two days:
SELECT lead_time + 5 UNITS DAY FROM manufact SELECT * FROM cust_calls WHERE (TODAY - call_dtime) > 30 UNITS DAY UPDATE manufact SET lead_time = 2 UNITS DAY + lead_time WHERE manu_code = ANZ
4-60
Expression
4-61
Expression
Restrictions: NEXTVAL and CURRVAL are valid only in SQL statements, not directly in SPL statements. (But SQL statements that use NEXTVAL and CURRVAL can be used in SPL routines.) The following restrictions apply to these operators in SQL statements: v You must have Select privilege on the sequence. v In a CREATE TABLE or ALTER TABLE statement, you cannot specify NEXTVAL or CURRVAL in the following contexts: In the Default clause In the definition of a check constraint. v In a SELECT statement, you cannot specify NEXTVAL or CURRVAL in the following contexts: In the projection list when the DISTINCT keyword is used In the WHERE, GROUP BY, or ORDER BY clauses In a subquery When the UNION operator combines SELECT statements. v You also cannot specify NEXTVAL or CURRVAL in these contexts: In fragmentation expressions In reference to a remote sequence object in another database. Examples: In the following examples, it is assumed that no other user is concurrently accessing the sequence and that the user executes the statements consecutively. These examples are based on the following sequence object and table:
CREATE SEQUENCE seq_2 INCREMENT BY 1 START WITH 1 MAXVALUE 30 MINVALUE 0 NOCYCLE CACHE 10 ORDER; CREATE TABLE tab1 (col1 int, col2 int); INSERT INTO tab1 VALUES (0, 0);
You can use NEXTVAL (or CURRVAL) in the Values clause of an INSERT statement, as the following example shows:
INSERT INTO tab1 (col1, col2) VALUES (seq_2.NEXTVAL, seq_2.NEXTVAL)
In the previous example, the database server inserts an incremented value (or the first value of the sequence, which is 1) into the col1 and col2 columns of the table. You can use NEXTVAL (or CURRVAL) in the SET clause of the UPDATE statement, as the following example shows:
UPDATE tab1 SET col2 = seq_2.NEXTVAL WHERE col1 = 1;
In the previous example, the incremented value of the seq_2 sequence, which is 2, replaces the value in col2 where col1 is equal to 1. The following example shows how you can use NEXTVAL and CURRVAL in the Projection clause of the SELECT statement:
SELECT seq_2.CURRVAL, seq_2.NEXTVAL FROM tab1;
4-62
Expression
In the previous example, the database server returns two rows of incremented values, 3 and 4, from both the CURRVAL and NEXTVAL expressions. For the first row of tab1, the database server returns the incremented value 3 for CURRVAL and NEXTVAL; for the second row of tab1, it returns the incremented value 4. For more examples on how to use NEXTVAL and CURRVAL, see the IBM Informix Guide to SQL: Tutorial.
For the syntax of expressions that evaluate to field values of a ROW data type, see ROW Constructors on page 4-64.
For more information, see Literal Collection on page 4-129. For the syntax of element values, see Collection Constructors on page 4-65.
4-63
Expression
Notes: 1 2 See page 4-34 See page 4-65
ROW Constructors
You use ROW constructors to generate values for ROW-type columns. Suppose you create the following named ROW type and a table that contains the named ROW type row_t and an unnamed ROW type:
CREATE ROW TYPE row_t ( x INT, y INT); CREATE TABLE new_tab ( col1 row_t, col2 ROW( a CHAR(2), b INT) )
When you define a column as a named ROW type or unnamed ROW type, you must use a ROW constructor to generate values for the ROW-type column. To create a value for either a named ROW type or unnamed ROW type, you must complete the following steps: v Begin the expression with the ROW keyword. v Specify a value for each field of the ROW type. v Enclose the comma-separated list of field values within parentheses. The format of the value for each field must be compatible with the data type of the ROW field to which it is assigned. You can use any kind of expression as a value with a ROW constructor, including literals, functions, and variables. The following examples show the use of different types of expressions with ROW constructors to specify values:
ROW(5, 6.77, HMO) ROW(col1.lname, 45000) ROW(john davis, TODAY) ROW(USER, SITENAME)
The following statement uses literal numbers and quoted strings with ROW constructors to insert values into col1 and col2 of the new_tab table:
INSERT INTO new_tab VALUES ( ROW(32, 65)::row_t, ROW(CA, 34) )
When you use a ROW constructor to generate values for a named ROW type, you must explicitly cast the ROW value to the appropriate named ROW type. The cast is necessary to generate a value of the named ROW type. To cast the ROW value as a named ROW type, you can use the cast operator ( :: ) or the CAST AS keywords, as the following examples show:
ROW(4,5)::row_t CAST (ROW(3,4) AS row_t)
4-64
Expression
You can use a ROW constructor to generate ROW type values in INSERT, UPDATE, and SELECT statements. In the next example, the WHERE clause of a SELECT statement specifies a ROW type value that is cast as type person_t:
SELECT * FROM person_tab WHERE col1 = ROW(charlie,hunter)::person_t
For more information on using ROW constructors in INSERT and UPDATE statements, see the INSERT and UPDATE statements in this manual. For information on named ROW types, see the CREATE ROW TYPE statement. For information on unnamed ROW types, see the discussion of the ROW data type in the IBM Informix Guide to SQL: Reference. For task-oriented information on named ROW types and unnamed ROW types, see the IBM Informix Database Design and Implementation Guide.
Collection Constructors
Use a collection constructor to specify values for a collection column. Collection Constructors:
SET MULTISET LIST { , (1) Expression }
You can use collection constructors in the WHERE clause of the SELECT statement and the VALUES clause of the INSERT statement. You can also pass collection constructors to UDRs. This table differentiates the types of collections that you can construct.
Keyword SET Description Indicates a collection of elements with the following qualities: v The collection must contain unique values. v Elements have no specific order associated with them. MULTISET Indicates a collection of elements with the following qualities: v The collection can contain duplicate values. v Elements have no specific order associated with them. LIST Indicates a collection of elements with the following qualities: v The collection can contain duplicate values. v Elements have ordered positions.
The element type of the collection can be any built-in or extended data type. You can use any kind of expression with a collection constructor, including literals, functions, and variables. When you use a collection constructor with a list of expressions, the database server evaluates each expression to its equivalent literal form and uses the literal values to construct the collection. You specify an empty collection with a set of empty braces ( { } ).
Chapter 4. Data Types and Expressions
4-65
Expression
Elements of a collection cannot be NULL. If a collection element evaluates to a NULL value, the database server returns an error. The element type of each expression must all be exactly the same data type. To accomplish this, cast the entire collection constructor expression to a collection type, or cast individual element expressions to the same type. If the database server cannot determine that the collection type and the element types are homogeneous, then the collection constructor returns an error. In the case of host variables, this determination is made at bind time when the client declares the element type of the host variable. An exception to this restriction can occur when some elements of a collection are VARCHAR data types but others are longer than 255 bytes. Here the collection constructor can assign a CHAR(n) type to all elements, for n the length in bytes of the longest element. (But see Collection Data Types on page 4-30 for an example based on this exception, where the user avoids fixed-length CHAR elements by an explicit cast to the LVARCHAR data type.) Examples of Collection Constructors: The following example shows that you can construct a collection with various expressions, if the resulting values are of the same data type:
CREATE FUNCTION f (a int) RETURNS int; RETURN a+1; END FUNCTION; CREATE TABLE tab1 (x SET(INT NOT NULL)); INSERT INTO tab1 VALUES ( SET{10, 1+2+3, f(10)-f(2), SQRT(100) +POW(2,3), (SELECT tabid FROM systables WHERE tabname = sysusers), T::BOOLEAN::INT} ) SELECT * FROM tab1 WHERE x=SET{10, 1+2+3, f(10)-f(2), SQRT(100) +POW(2,3), (SELECT tabid FROM systables WHERE tabname = sysusers), T::BOOLEAN::INT} }
This assumes that a cast from BOOLEAN to INT exists. (For a more restrictive syntax to specify collection values , see Literal Collection on page 4-129.)
NULL Keyword
The NULL keyword is valid in most contexts where you can specify a value. What it specifies, however, is the absence of any value (or an unknown or missing value). NULL Keyword:
NULL
Within SQL, the keyword NULL is the only syntactic mechanism for accessing a NULL value. NULL is not equivalent to zero, nor to any specific value. In ascending ORDER BY operations, NULL values precede any non-NULL value; in
4-66
Expression
descending sorts, NULL values follow any non-NULL value. In GROUP BY operations, all NULL values are grouped together. (Such groups may in fact be logically heterogeneous, if they include missing or unknown values.) The keyword NULL is a global symbol in the syntactic context of expressions, meaning that its scope of reference is global. Every data type, whether built-in or user-defined, can represent a NULL value. IBM Informix Dynamic Server supports cast expressions in the projection list. This means that users can write expressions of the form NULL::datatype, in which datatype is any data type known to the database server. IBM Informix Dynamic Server prohibits the redefinition of NULL, because allowing such definition would restrict the global scope of the NULL keyword. For this reason, any mechanism that restricts the global scope or redefines the scope of the keyword NULL will syntactically disable any cast expression involving a NULL value. You must ensure that the occurrence of the keyword NULL receives its global scope in all expression contexts. For example, consider the following SQL code:
CREATE TABLE newtable ( null int ); SELECT null, null::int FROM newtable;
The CREATE TABLE statement is valid, because the column identifiers have a scope of reference that is restricted to the table definition; they can be accessed only within the scope of a table. The SELECT statement in the example, however, poses some syntactic ambiguities. Does the identifier null appearing in the projection list refer to the global keyword NULL, or does it refer to the column identifier null that was declared in the CREATE TABLE statement? v If the identifier null is interpreted as the column name, the global scope of cast expressions with the NULL keyword will be restricted. v If the identifier null is interpreted as the NULL keyword, the SELECT statement must generate a syntactic error for the first occurrence of null because the NULL keyword can appear only as a cast expression in the projection list. A SELECT statement of the following form is valid because the NULL column of newtable is qualified with the table name:
SELECT newtable.null, null::int FROM newtable;
More involved syntactic ambiguities arise in the context of an SPL routine that has a variable named null. An example follows:
CREATE FUNCTION nulltest() RETURNING INT; DEFINE a INT; DEFINE null INT; DEFINE b INT; LET a = 5; LET null = 7; LET b = null;
4-67
Expression
RETURN b; END FUNCTION; EXECUTE FUNCTION nulltest();
When the preceding function executes in DB-Access, in the expressions of the LET statement, the identifier null is treated as the keyword NULL. The function returns a NULL value instead of 7. Using null as a variable of an SPL routine would restrict the use of a NULL value in the body of the SPL routine. Therefore, the preceding SPL code is not valid, and causes IBM Informix Dynamic Server to return the following error:
-947 Declaration of an SPL variable named null conflicts with SQL NULL value.
In ESQL/C, you should use an indicator variable if there is the possibility that a SELECT statement will return a NULL value.
Function Expressions
A function expression can return one or more values from built-in SQL functions or from user-defined functions, as the following diagram shows. Function Expressions:
(1) Algebraic Functions (3) CARDINALITY Function (4) DBINFO Function (5) (6) Encryption and Decryption Functions (7) Exponential and Logarithmic Functions (8) HEX Function (9) Length Functions (10) IFX_REPLACE_MODULE Function (5) (11) Smart-Large-Object Functions (12) Time Functions (13) Trigonometric Functions (14) String-Manipulation Functions (15) IFX_ALLOW_NEWLINE Function (16) User-Defined Functions (2)
4-68
Expression
4 5 6 7 8 9 10 11 12 13 14 15 16 See page 4-68 Dynamic Server only See page 4-79 See page 4-87 See page 4-116 See page 4-89 See page 4-90 See page 4-91 See page 4-96 See page 4-100 See page 4-102 See page 4-111 See page 4-111
Algebraic Functions
Algebraic functions take one or more arguments of numeric data types. Algebraic Functions:
ABS ( num_expression ) MOD ( dividend , divisor ) POW ( base, exponent ) , 2 ROOT ( radicand , index (1) ROUND ( Expression SQRT ( sqrt_radicand ) (1) TRUNC ( Expression , truncate_factor , 0 )
) , , 0 ) rounding_factor
4-69
Expression
Element base dividend divisor exponent index num_expression radicand rounding_factor Description Value to be raised to the power specified in exponent Value to be divided by divisor Value by which to divide dividend Power to which to raise base Root to extract. The default is 2. Number with an absolute value Value whose root is to be returned Position to left ( - ) or right ( + ) of decimal point to which argument is rounded. Default scale is zero. Number with real square roots Position to left ( - ) or right ( + ) of decimal point to which argument is truncated. Default scale is zero. Restrictions Must return a real number Must return a real number Cannot return zero Must return a real number Must return a real number Must return a real number Must return a real number Syntax Expression, p. 4-34 Expression, p. 4-34 Expression, p. 4-34 Expression, p. 4-34 Expression, p. 4-34 Expression, p. 4-34 Expression, p. 4-34
Integer in range +32 to -32; see Literal Number, ROUND Function on page 4-71. p. 4-137 Must be a positive real number Integer in range +32 to -32; see TRUNC Function on page 4-71. Expression, p. 4-34 Literal Number, p. 4-137
sqrt_radicand truncate_factor
ABS Function: The ABS function returns the absolute value of a numeric expression, returning the same data type as its single argument. The following example shows all orders of more than $20 paid in cash ( + ) or store credit ( - ). The stores_demo database does not contain any negative balances, but you might have negative balances in your application.
SELECT order_num, customer_num, ship_charge FROM orders WHERE ABS(ship_charge) > 20
MOD Function: The MOD function returns the remainder from integer division of two real number operands, after the integer part of the first argument (the dividend) is divided by the integer part of the second argument (the divisor) as an INT data type (or INT8 for remainders outside the range of INT). The quotient and any fractional part of the remainder are discarded. The divisor cannot be 0. Thus, MOD (x,y) returns y (modulo x). Make sure that any variable that receives the result is of a data type that can store the returned value. This example tests to see if the current date is within a 30-day billing cycle:
SELECT MOD(TODAY - MDY(1,1,YEAR(TODAY)),30) FROM orders
POW Function: The POW function raises the base to the exponent. This function requires two numeric arguments. The returned data type is FLOAT. The following example returns data for circles whose areas are less than 1,000 square units:
SELECT * FROM circles WHERE (3.1416 * POW(radius,2)) < 1000
To use e, the base of natural logarithms, see EXP Function on page 4-87. ROOT Function: The ROOT function returns the root value of a numeric expression. This function requires at least one numeric argument (the radicand argument) and allows no more than two (the radicand and index arguments). If only the radicand argument is supplied, the value 2 is used as a default value for the index argument. The value 0 cannot be used as the value of index. The value that the ROOT function returns is a FLOAT data type. The first SELECT statement in the following example takes the square root of the expression. The second takes the cube root of the expression.
4-70
Expression
SELECT ROOT(9) FROM angles SELECT ROOT(64,3) FROM angles -- square root of 9 -- cube root of 64
The SQRT function is equivalent to ROOT(x). ROUND Function: The ROUND function returns the rounded value of an expression. The expression must be numeric or must be converted to numeric. If you omit the rounding factor specification, the value is rounded to a scale of zero, or to the units place. The range of 32 ( + and - ) refers to the precision of the returned value, relative to the first argument. Positive-digit values indicate rounding to the right of the decimal point; negative-digit values indicate rounding to the left of the decimal point, as Figure 4-1 shows.
Expression: ROUND (24,536.8746, -2) = 24,500.00 ROUND (24,536.8746, 0) = 24,537.00 -2 ROUND (24,536.8746, 2) = 24,536.87 0 2 24536.8746
The following example shows how you can use the ROUND function with a column expression in a SELECT statement. This statement displays the order number and rounded total price (to zero places) of items whose rounded total price (to zero places) is equal to 124.00.
SELECT order_num , ROUND(total_price) FROM items WHERE ROUND(total_price) = 124.00
If you use a MONEY data type as the argument for the ROUND function and you round to zero places, the value displays with .00. The SELECT statement in the following example rounds an INTEGER value and a MONEY value. It displays 125 and a rounded price in the form xxx.00 for each row in items.
SELECT ROUND(125.46), ROUND(total_price) FROM items
SQRT Function: The SQRT function returns the square root of a numeric expression. The next example returns the square root of 9 for each row of the angles table:
SELECT SQRT(9) FROM angles
TRUNC Function: The TRUNC function returns the truncated value of a numeric expression. The expression must be numeric or a form that can be converted to a numeric expression. If you omit the truncate factor specification, the argument is truncated to a scale of zero, or to the unit place. The range of 32 ( + and - ) refers to the precision of the returned value, relative to the first argument. Positive digit values indicate truncating to the right of the decimal point; negative digit values indicate truncating to the left, as Figure 4-2 shows.
4-71
Expression
If a MONEY data type is the argument for the TRUNC function that specifies zero places, the fractional places are removed. For example, the following SELECT statement truncates a MONEY value and an INTEGER value. It displays 125 and a truncated price in integer format for each row in items.
SELECT TRUNC(125.46), TRUNC(total_price) FROM items
Restrictions Must be declared as a collection data type Must be declared as a collection data type
Suppose that the set_col SET column contains the following value:
{3, 7, 9, 16, 0}
The following SELECT statement returns 5 as the number of elements in the set_col column:
SELECT CARDINALITY(set_col) FROM table1
If the collection contains duplicate elements, CARDINALITY counts each individual element.
DBINFO Function
The following diagram shows the syntax of the DBINFO function. DBINFO Function:
DBINFO
4-72
Expression
( dbspace tblspace_num expression sqlca.sqlerrd1 sqlca.sqlerrd2 (1) sessionid dbhostname version , specifier serial8 (2) coserverid coserverid , table.column dbspace , table.column , , )
, currentrow currentrow
Notes: 1 2
Element column expression Description Name of a column in the table Expression that evaluates to tblspace_num Literal value that specifies which part of version string to return Table for which to display the dbspace name or coserver ID corresponding to each row Tblspace number (partition number) of a table
specifier table
tblspace_ num
DBINFO Options: The DBINFO function is actually a set of functions that return different types of information about the database. To invoke each function, specify a particular option after the DBINFO keyword. You can use any DBINFO option anywhere within SQL statements and within UDRs. The following table shows the different types of database information that you can retrieve with the DBINFO options. The Option column shows the name of each DBINFO option. The Effect column shows the type of database information that the option retrieves. The Page column shows the page where you can find more information about a given option.
4-73
Expression
Option dbspace tblspace_num sqlca.sqlerrd1 sqlca.sqlerrd2 Effect Returns the name of a dbspace corresponding to a tblspace number Returns the last serial value inserted in a table Returns the number of rows processed by selects, inserts, deletes, updates, EXECUTE PROCEDURE statements, and EXECUTE FUNCTION statements Returns the session ID of the current session Returns the hostname of the database server to which a client application is connected Returns the exact version of the database server to which a client application is connected Returns last SERIAL8 value inserted in a table Returns the coserver ID of the coserver to which the user who entered the query is connected Returns the coserver ID of the coserver where each row of a specified table is located Page 4-74 4-74 4-75
sessionid dbhostname version serial8 coserverid (XPS) coserverid table.column currentrow (XPS) dbspace table.column currentrow (XPS)
Returns the name of the dbspace where each row of 4-79 a specified table is located
Using the dbspace Option Followed by a Tblspace Number: The dbspace option returns a character string that contains the name of the dbspace that corresponds to a tblspace number. You must supply an additional parameter, either tblspace_num or an expression that evaluates to tblspace_num. The following example uses the dbspace option. First, it queries the systables system catalog table to determine the tblspace_num for the table customer, then it executes the function to determine the dbspace name.
SELECT tabname, partnum FROM systables where tabname = customer
If the statement returns a partition number of 1048892, you insert that value into the second argument to find which dbspace contains the customer table, as the following example shows:
SELECT DBINFO (dbspace, 1048892) FROM systables where tabname = customer
If the table for which you want to know the dbspace name is fragmented, you must query the sysfragments system catalog table to find out the tblspace number of each table fragment. Then you must supply each tblspace number in a separate DBINFO query to find out all the dbspaces across which a table is fragmented. Using the sqlca.sqlerrd1 Option: The sqlca.sqlerrd1 option returns a single integer that provides the last serial value that is inserted into a table. To ensure valid results, use this option immediately following a singleton INSERT statement that inserts a single row with a serial value into a table. Tip: To obtain the value of the last SERIAL8 value that is inserted into a table, use the serial8 option of DBINFO. For more information, see Using the serial8 Option on page 4-77. The following example uses the sqlca.sqlerrd1 option:
4-74
Expression
EXEC EXEC EXEC EXEC EXEC EXEC SQL SQL SQL SQL SQL SQL create create insert insert insert insert table fst_tab (ordernum serial, partnum int); table sec_tab (ordernum serial); into fst_tab VALUES (0,1); into fst_tab VALUES (0,4); into fst_tab VALUES (0,6); into sec_tab values (dbinfo(sqlca.sqlerrd1));
This example inserts a row that contains a primary-key serial value into the fst_tab table, and then uses the DBINFO function to insert the same serial value into the sec_tab table. The value that the DBINFO function returns is the serial value of the last row that is inserted into fst_tab. Using the sqlca.sqlerrd2 Option: The sqlca.sqlerrd2 option returns a single integer that provides the number of rows that SELECT, INSERT, DELETE, UPDATE, EXECUTE PROCEDURE, and EXECUTE FUNCTION statements processed. To ensure valid results, use this option after SELECT, EXECUTE PROCEDURE, and EXECUTE FUNCTION statements have completed executing. In addition, to ensure valid results when you use this option within cursors, make sure that all rows are fetched before the cursors are closed. The following example shows an SPL routine that uses the sqlca.sqlerrd2 option to determine the number of rows that are deleted from a table:
CREATE FUNCTION del_rows (pnumb int) RETURNING int; DEFINE nrows int; DELETE FROM fst_tab WHERE part_number = pnumb; LET nrows = DBINFO(sqlca.sqlerrd2); RETURN nrows; END FUNCTION
Using the sessionid Option: The sessionid option of the DBINFO function returns the session ID of your current session. When a client application makes a connection to the database server, the database server starts a session with the client and assigns a session ID for the client. The session ID serves as a unique identifier for a given connection between a client and a database server. The database server stores the value of the session ID in a data structure in shared memory that is called the session control block. The session control block for a given session also includes the user ID, the process ID of the client, the name of the host computer, and a variety of status flags. When you specify the sessionid option, the database server retrieves the session ID of your current session from the session control block and returns this value to you as an integer. Some of the System-Monitoring Interface (SMI) tables in the sysmaster database include a column for session IDs, so you can use the session ID that the DBINFO function obtained to extract information about your own session from these SMI tables. For further information on the session control block, see the IBM Informix Administrator's Guide. For further information on the sysmaster database and the SMI tables, see the IBM Informix Administrator's Reference. In the following example, the user specifies the DBINFO function in a SELECT statement to obtain the value of the current session ID. The user poses this query against the systables system catalog table and uses a WHERE clause to limit the query result to a single row.
4-75
Expression
SELECT DBINFO(sessionid) AS my_sessionid FROM systables WHERE tabname = systables
In the preceding example, the SELECT statement queries against the systables system catalog table. You can, however, obtain the session ID of the current session by querying against any system catalog table or user table in the database. For example, you can enter the following query to obtain the session ID of your current session:
SELECT DBINFO(sessionid) AS user_sessionid FROM customer WHERE customer_num = 101
You can use the DBINFO sessionid option not only in SQL statements but also in SPL routines. The following example shows an SPL function that returns the value of the current session ID to the calling program or routine:
CREATE FUNCTION get_sess() RETURNING INT; RETURN DBINFO(sessionid); END FUNCTION;
Using the dbhostname Option: You can use the dbhostname option to retrieve the hostname of the database server to which a database client is connected. This option retrieves the physical computer name of the computer on which the database server is running. In the following example, the user enters the dbhostname option of DBINFO in a SELECT statement to retrieve the hostname of the database server to which DBAccess is connected:
SELECT DBINFO(dbhostname) FROM systables WHERE tabid = 1
Using the version Option: You can use the version option of the DBINFO function to retrieve the exact version number of the database server against which the client application is running. This option retrieves the exact version string from the message log. The value of the full version string is the same as that displayed by the -V option of the oninit utility. Use the specifier parameter of the version option to specify which part of the version string you want to retrieve. The following table lists the values that you can enter in the specifier parameter, shows which part of the version string is returned for each specifier value, and gives an example of what is returned by each value of specifier. Each example returns part of the complete version string Dynamic Server Version 10.00.UC1.
4-76
Expression
Specifier Parameter server-type major minor os
Part of Version String Returned Type of database server Major version number of the current database server version Minor version number of the current database server version Operating-system identifier within the version string: T = U = H = F = Windows UNIX 32-bit running on a 32-bit operating system UNIX 32-bit running on a 64-bit operating system UNIX 64-bit running on a 64-bit operating system
level full
Interim release level of the current database server version Complete version string as it would be returned by oninit -V
Important: Not all UNIX environments fit the word-length descriptions of operating- system (os) codes in the preceding table. For example, some U versions can run on 64-bit operating systems. Similarly, some F versions can run on operating systems with 32-bit kernels that support 64-bit applications. The following example shows how to use the version option of DBINFO in a SELECT statement to retrieve the major version number of the database server that the DBAccess client is connected to:
SELECT DBINFO(version, major) FROM systables WHERE tabid = 1
Using the serial8 Option: The serial8 option returns a single integer that provides the last SERIAL8 value that is inserted into a table. To ensure valid results, use this option immediately following an INSERT statement that inserts a SERIAL8 value. Tip: To obtain the value of the last SERIAL value that is inserted into a table, use the sqlca.sqlerrd1 option of DBINFO( ). For more information, see Using the sqlca.sqlerrd1 Option on page 4-74. The following example uses the serial8 option:
EXEC SQL create table fst_tab (ordernum serial8, partnum int); EXEC SQL create table sec_tab (ordernum serial8);
Chapter 4. Data Types and Expressions
4-77
Expression
EXEC SQL insert into fst_tab VALUES (0,1); EXEC SQL insert into fst_tab VALUES (0,4); EXEC SQL insert into fst_tab VALUES (0,6); EXEC SQL insert into sec_tab select dbinfo(serial8) from fst_tab where partnum = 6;
This example inserts a row that contains a primary-key SERIAL8 value into the fst_tab table and then uses the DBINFO function to insert the same SERIAL8 value into the sec_tab table. The value that the DBINFO function returns is the SERIAL8 value of the last row that is inserted into fst_tab. The subquery in the last line contains a WHERE clause so that a single value is returned. Using the coserverid Option with No Other Arguments (XPS): The coserverid option with no other arguments returns a single integer that corresponds to the coserver ID of the coserver to which the user who entered the query is connected. Suppose that you use the following statement to create the mytab table:
CREATE TABLE mytab (mycol INT) FRAGMENT BY EXPRESSION mycol < 5 in rootdbs.1 mycol > 5 in rootdbs.2
Further, suppose that the dbspace named rootdbs.1 resides on coserver 1, and the dbspace named rootdbs.2 resides on coserver 2. Also suppose that you use the following statements to insert rows into the mytab table:
INSERT INTO mytab VALUES (1); INSERT INTO mytab VALUES (6);
Finally, suppose that you are logged on to coserver 1 when you make the following query, which displays the values of all columns in the row where the value of the mycol column is 1. This query also displays the coserver ID of the coserver to which you are logged on when you enter the query:
SELECT *, DBINFO (coserverid) AS cid FROM mytab WHERE mycol = 1
The following table shows the result of this query. mycol 1 cid 1
Using the coserverid Option Followed by Table and Column Names (XPS): Use the coserverid option followed by the table name and column name and the currentrow string to find out the coserver ID where each row in a specified table is located. This option is especially useful when you fragment a table across multiple coservers. In the following example, the user asks to see all columns and rows of the mytab table as well as the coserver ID of the coserver where each row resides. For a description of the mytab table, see Using the coserverid Option with No Other Arguments (XPS) on page 4-78:
SELECT *, DBINFO (coserverid, mytab.mycol, currentrow) AS dbsp FROM mytab
4-78
Expression
The following table shows the result of this query. mycol 1 6 cid 1 2
The column that you specify in the DBINFO function can be any column in the specified table. Using the dbspace Option Followed by Table and Column Names (XPS): Use the dbspace option followed by the table name and column name and the currentrow string to find out the name of the dbspace where each row in a specified table is located. This option is especially useful when you fragment a table across multiple dbspaces. In the following example, the user asks to see all columns and rows of the mytab table as well as the name of the dbspace where each row resides. For a description of the mytab table, see Using the coserverid Option with No Other Arguments (XPS) on page 4-78.
SELECT *, DBINFO (dbspace, mytab.mycol, currentrow) AS dbsp FROM mytab
The following table shows the result of this query. mycol 1 6 cid rootdbs.1 rootdbs.2
The column arguments to DBINFO can be any columns in the specified table.
4-79
Expression
Element data encrypted _data hint Description A plain text character string, variable, or large object of type BLOB or CLOB to be encrypted A character string or variable containing output from ENCRYPT_AES or from ENCRYPT_TDES A character string that you define here. Default is the value from the WITH HINT clause of the SET ENCRYPTION statement that defined password. A character string that the encryption function defines. Default is the session password value defined by the SET ENCRYPTION statement Restrictions Must be a character or BLOB data type Decryption requires the encryption password No more than 32 bytes Syntax Expression, p. 4-34 Expression, p. 4-34 Quoted string, p. 4-142 Quoted string, p. 4-142
password
You can invoke these encryption and decryption functions from within DML statements or with the EXECUTE FUNCTION statement. For distributed operations over a network, all participating database servers must support these (or equivalent) functions. If the network is not secure, the DBSA must enable the encryption communication support module (ENCCSM) to provide data encryption between the database server and client systems, in order to avoid transmitting passwords as plain text. Encryption or decryption calls slow the performance of the SQL statement within which these functions are invoked, but have no effect on other statements. Column Level and Cell Level Encryption: The encryption and decryption functions can support two ways of using data encryption features, namely column level and cell level encryption. v Column level encryption means that all values in a given column are encrypted with the same password (which can be a word or phrase), the same cipher, and the same cipher mode. Users of this form of encryption should consider not using the hint feature of these functions, but instead store a mnemonic hint for remembering the password in some other location. Otherwise, the same hint will occupy disk space in every row that contains an encrypted value. v Cell level encryption means that within a column of encrypted data many different passwords (or different ciphers or cipher modes) are used. This use of encryption is also called row-column level or set-column level encryption. Compared to column-level encryption, this makes the task of data management more complex, because if different passwords are required for decrypting different rows of the same table, it is not possible to write a single SELECT statement to fetch all the decrypted data. In some situations, however, individual users may need this technique to protect personal data. To protect data security and confidentiality, the database server does not store information in the system catalog to indicate whether any table (or any column or row) includes encrypted data. Similarly, the logical logs of Dynamic Server do not record SET ENCRYPTION statements, nor calls to encryption or decryption functions. (The Trusted Facility feature for secure auditing, however, can use the STEP audit-event mnemonic to record execution of the SET ENCRYPTION statement, and can use the CRPT audit-event mnemonic to record successful or unsuccessful calls to DECRYPT_CHAR or DECRYPT_BINARY.) The Password and Hint Specifications: The SET ENCRYPTION statement or an encryption function can define a password and hint for the current session. The
4-80
Expression
password must be specified as a character expression that returns at least 6 bytes, but no more than 128. The optional hint is specified as a character expression that returns no more than 32 bytes. The purpose of the hint is to help users to remember the password. When you call ENCRYPT_AES or ENCRYPT_TDES with a hint argument, it is encrypted and embedded in the encrypted_data, from which GETHINT can retrieve it. But if you define hint as NULL, or omit hint when SET ENCRYPTION specified no default hint for the session password, no hint is embedded in the encrypted_data. The password used for encryption and decryption is either the password argument to the function, or if you omit this argument, it is the session password specified in the last SET ENCRYPTION statement executed before you invoke the function. The DECRYPT_CHAR, DECRYPT_BINARY, or GETHINT function call fails with an error if the encrypted_data argument is not in an encrypted format, or if the password argument to a decryption function is omitted when no session password value was set by SET ENCRYPTION. An error also results if the password used for decryption is not the same password used for encryption. Encryption key management, which is critical to the secure operation of the database, is delegated entirely to the application. This implementation means that the password itself is not stored in the database. Without help from the user through the application, the database server cannot decrypt the encrypted data. If you invoke any of these functions from a UDR, you might prefer to set a session password in the SET ENCRYPTION statement. Otherwise, password will be visible to users who can view the sysprocbody.data column in the system catalog. Data Types, Encoding, and Size of Encrypted Values: The data and corresponding encrypted_data values can be of any character type (CHAR, LVARCHAR, NCHAR, NVARCHAR, or VARCHAR), or a smart large object of type BLOB or CLOB. Corresponding data and encrypted_data values that the encryption functions return have the same character, BLOB, or CLOB type. (Use CLOB in place of TEXT, which these functions do not support.) Except for original data of BLOB or CLOB data types, the encrypted_data value is encoded in BASE64 format. An encrypted value requires more space than the corresponding plain text, because the database must also store the information (except for the encryption key) that is needed for decryption. If a hint is used, it adds to the length of encrypted_data. The BASE64 encoding scheme stores 6 bits of input data as 8 bits of output. To encode N bytes of data, BASE64 requires at least ((4N+3/3) bytes of storage, where ( / ) represents integer division. Padding and headers can increase BASE64 storage requirements above this ((4N+3)/3) ratio. Example of Column Level Encryption on page 4-82 lists formulae to estimate the size of data values encrypted in BASE64 format. It typically requires changes to the schema of an existing table that will store BASE64 format encrypted data, especially if a hint will also be stored. The following table shows how the data type of the input string corresponds to the data type of the value that ENCRYPT_AES or ENCRYPT_TDES returns:
4-81
Expression
Table 4-1. Data Types for ENCRYPT_AES and ENCRYPT_TDES Functions Plain Text Data Type CHAR NCHAR VARCHAR NVARCHAR LVARCHAR BLOB CLOB Encrypted Data Type CHAR NCHAR VARCHAR or CHAR NVARCHAR or NCHAR LVARCHAR BLOB BLOB Decryption Function DECRYPT_CHAR DECRYPT_CHAR DECRYPT_CHAR DECRYPT_CHAR DECRYPT_CHAR DECRYPT_BINARY DECRYPT_CHAR
Columns of type VARCHAR and NVARCHAR store no more than 255 bytes. If the data string is too long for these data types to store both the encrypted data and encryption overhead, then the value returned by the encryption function is automatically changed from VARCHAR or NVARCHAR into a fixed CHAR or NCHAR value, with no trailing blanks in the encoded encrypted value. Encrypted values of type BLOB or CLOB are not in BASE64 encoding format, and their size increase after encryption is independent of the original data size. For BLOB or CLOB values, the encrypted size (in bytes) has the following formula, where N is the original size of the plain text, and H is the size of the unencrypted hint string, if encryption is performed by ENCRYPT_TDES:
N + H + 24 bytes.
For BLOB or CLOB values that ENCRYPT_AES encrypts, the overhead is larger:
N + H + 32 bytes.
Example of Column Level Encryption: The following example illustrates how to use the built-in encryption and decryption functions of Dynamic Server to create and use a table that stores encrypted credit card numbers in a column that has a character data type. For purposes of this example, assume that the plain text of the values to be encrypted consists of strings of 16 digits. Because encryption functions support character data types, these values will be stored in a CHAR column, rather than in an INT or INT8 column. Calculating storage requirements for encrypted data: The LENGTH function provides a convenient way to calculate the storage requirements of encrypted data directly:
EXECUTE FUNCTION LENGTH(ENCRYPT_TDES("1234567890123456", "simple password"))
4-82
Expression
This returns 119. The required storage size for encrypted data is sensitive to three factors: v N, the number of bytes in the plain text v whether or not a hint is provided v which encryption function you use (ENCRYPT_TDESor ENCRYPT_TDES) The following formulae describe the four possible cases, and are not simplified: v Encryption by ENCRYPT_TDES( ) with no hint:
Encrypted size = (4 x ((8 x((N + 8)/8) + 10)/3) + 11)
The integer divsion ( / ) returns an integer quotient and discards any remainder. Based on these formulae, the following table shows the encrypted size (in bytes) for selected ranges of values of N: 3 3 3 3 3 3 3 3 3 3 3 3 3
N 1 to 7 8 to 15 16 to 23 24 to 31 32 to 39 40 to 47 100 200 500 ENCRYPT_TDES No Hint 35 43 55 67 75 87 163 299 695 ENCRYPT_AES No Hint 43 43 67 67 87 87 171 299 707 ENCRYPT_TDES With Hint 87 99 107 119 131 139 215 355 747 ENCRYPT_AES With Hint 99 99 119 119 139 139 227 355 759
If the column size is smaller than the data size returned by encryption functions, the encrypted value is truncated when it is inserted. In this case, it will not be possible to decrypt the data, because the header will indicate that the length should be longer than the data value that the column contains. These formulae and the values returned by the LENGTH function, however, indicate that the table schema in the next example can store the encrypted form of 16-digit credit card numbers (with a hint). Implementing column-level encryption: The following steps create a table from which a user who knows the password can retrieve rows that include one column of encrypted data. 1. Create a database table containing at least one column of type BLOB, CLOB, or a character data type of sufficient length to store the encrypted values. For example, the following statement creates a table called customer in which the column creditcard can store encrypted credit card numbers:
Chapter 4. Data Types and Expressions
4-83
Expression
CREATE TABLE customer (id CHAR(20), creditcard CHAR(107));
The following query call a decryption function in the WHERE clause, using the session password default, rather than an explicit password argument:
SELECT id FROM customer WHERE DECRYPT_CHAR(creditcard) = 2345678901234567
Column level encryption offers the coding convenience of passing the implicit session password for all rows with encrypted columns, and in multiple encryption and decryption function calls in the same SQL statement. Confidentiality of the data, however, requires users who know the password on encrypted columns to avoid compromising its secrecy. Triggers and UDRs, for example, should always use the session password, rather than explicit password arguments if they invoke the encryption or decryption functions. The DBSA can manage highly confidential data with column level encryption. Dynamic Server does not, however, prevent users with sufficient privileges from entering data encrypted by some other password into a table whose other rows use the designated column level encryption password.
DECRYPT_CHAR Function
The DECRYPT_CHAR function accepts as its first argument an encrypted_data character string that can have any character type (CHAR, LVARCHAR, NCHAR, NVARCHAR, or VARCHAR). You must specify a password as its second argument, unless the SET ENCRYPTION statement has specified for this session the same session password by which the first argument was encrypted. The DECRYPT_CHAR function also accepts as its first argument an encrypted_data large object of type BLOB or CLOB. You must specify a password as its second argument, unless the SET ENCRYPTION statement has specified as the default for this session the same password by which the first argument was encrypted. If the call to DECRYPT_CHAR is successful, it returns a CLOB large object that contains the plain text version of the encrypted_data argument. If the call to DECRYPT_CHAR with an encrypted string argument is successful, it returns a character string that contains the plain text version of the encrypted_data argument. The following example returns a character string containing a decrypted value from the ssid column of the engineers table for the row whose empno value is 287:
SELECT DECRYPT_CHAR (ssid) FROM engineers WHERE empno = 287
If the first argument to DECRYPT_CHAR is not an encrypted value, or if the second argument (or the default password specified by SET ENCRYPTION) is not the password that was used when the first argument was encrypted, Dynamic Server issues an error, and the call to DECRYPT_CHAR fails. (See the description of the GETHINT Function on page 4-87 for one possible action to take when you cannot remember the password that was used for encryption.)
4-84
Expression
Do not use DECRYPT_CHAR (or any other decryption function) to create a functional index on an encrypted column. This would store the decrypted values as plain text data in the database, defeating the purpose of encryption. For additional information about using data encryption in column values of Dynamic Server databases, see Encryption and Decryption Functions on page 4-79, and SET ENCRYPTION PASSWORD on page 2-560.
DECRYPT_BINARY Function
The DECRYPT_BINARY function accepts as its first argument an encrypted_data large object of type BLOB or CLOB. You must specify a password as its second argument, unless the SET ENCRYPTION statement has specified as the default for this session the same password by which the first argument was encrypted. If the call to DECRYPT_BINARY is successful, it returns a BLOB or CLOB large object that contains the plain text version of the encrypted_data argument. If the first argument to DECRYPT_BINARY is an encrypted value of a character data type, Dynamic Server invokes the DECRYPT_CHAR function and attempts to decrypt the specified value. If the first argument to DECRYPT_BINARY is not an encrypted value, or if the second argument (or the default password specified by SET ENCRYPTION) is not the password that was used when the first argument was encrypted, Dynamic Server issues an error, and the call to DECRYPT_BINARY fails. (See the description of the GETHINT Function on page 4-87 for one possible action to take when you cannot remember the password that was used for encryption.) Do not use DECRYPT_BINARY (or any other decryption function) to create a functional index on an encrypted column. This would store the decrypted values as plain text data in the database, defeating the purpose of encryption. For additional information about using data encryption in column values of Dynamic Server databases, see Encryption and Decryption Functions on page 4-79, and SET ENCRYPTION PASSWORD on page 2-560.
ENCRYPT_AES Function
The ENCRYPT_AES function returns an encrypted value that it derives by applying the AES (Advanced Encryption Standard) algorithm to its first argument, which must be an unencrypted character expression or a smart large object (that is, a BLOB or CLOB data type). A character argument can have a length of up to 32640 bytes if an explicit or default hint is used, or 32672 bytes if no hint (or a NULL hint) is specified. Theoretical size limits on BLOB or CLOB arguments are many orders of magnitude larger, but practical limits might be imposed by your hardware, or by time required for encryption and decryption. You must specify a password as its second argument, unless a SET ENCRYPTION statement has specified a session password, which the database server uses by default if you omit the second argument. If a session password has been set, any password that you specify overrides the session password for the returned value of this function call. The explicit or default password will also be required for any subsequent decryption of the returned encrypted value. A valid password must have at least 6 bytes but no more than 128. You can optionally specify a hint as the third argument. If the SET ENCRYPTION statement specified a default hint for this session, and you specify no hint, that
Chapter 4. Data Types and Expressions
4-85
Expression
default hint is stored in an encrypted form within the returned value. Any hint that you specify overrides the default hint. A valid hint can be no longer than 32 bytes. You can use consecutive quotation marks ( ) to specify a NULL hint. If you specify an explicit hint, you must also specify an explicit password. The purpose of the hint is to help users to remember the password. For example, if the password is buggy, you might define the hint as whip. Neither string is restricted to a single word, but the size of the hint contributes to the size of the returned value. If you subsequently cannot remember the hint, use the returned value from ENCRYPT_AES as the argument to GETHINT to retrieve the hint. The following example calls ENCRYPT_AES from the VALUES clause of an INSERT statement that stores in tab1 a plain-text string and an encrypted_data value that ENCRYPT_AES returns from its 12-byte first argument. Here SET ENCRYPTION defines a session password and hint that are used as default second and third arguments to the ENCRYPT_AES function:
EXEC SQL SET ENCRYPTION PASSWORD CHARYBDIS WITH HINT messina EXEC SQL INSERT INTO tab1 VALUES (abcd, ENCRYPT_AES(111-222-3333));
The call to ENCRYPT_AES fails with an error if the password argument is omitted when no session password has been set, or if the length of an explicit password argument is shorter than 6 bytes or longer than 128 bytes. In some contexts, an error is issued if the encrypted returned value is too large to be stored by the data type that receives it. For additional information about using data encryption in column values of Dynamic Server databases, see Encryption and Decryption Functions on page 4-79, and SET ENCRYPTION PASSWORD on page 2-560.
ENCRYPT_TDES Function
The ENCRYPT_TDES function returns a value that is the result of encrypting a character expression, or a BLOB or CLOB value, by applying the TDES (Triple Data Encryption Standard, which is sometimes also called DES3) algorithm to its first argument. This algorithm is slower than the DES algorithm that is used by the ENCRYPT_AES function, but is considered somewhat more secure. The disk space required as encryption overhead resembles that of ENCRYPT_AES, but is somewhat smaller because of the smaller block size of ENCRYPT_TDES. (See Calculating storage requirements for encrypted data on page 4-82 for a discussion of how to estimate the size of encrypted character strings.) Those differences in performance, tamper-resistance, and in the returned encrypted_data size that the previous paragraph lists are the practical differences between the ENCRYPT_TDES and ENCRYPT_AES functions, which otherwise follow the same rules, defaults, and restrictions that appear in the description of ENCRYPT_AES on the previous page in regard to the following features: v The required first argument (the plain text data value to be encrypted) v The explicit or default second argument (the password string that must also be an argument to DECRYPT_CHAR or DECRYPT_BINARY to decrypt the returned encrypted_data value). This must be specified unless a default session password has been set by the SET ENCRYPTION statement v The optional third argument (the hint value) that might assist users who forget the password. If you subsequently cannot remember an explicit or default hint that was defined for password, you can use the returned value from ENCRYPT_TDES as the argument to GETHINT to retrieve the hint.
4-86
Expression
The following example calls ENCRYPT_TDES from the SET clause of an UPDATE statement. Here the session password is PERSEPHONE and the hint string is pomegranate, with column colU of table tabU the data argument. Because the WHERE clause condition of 1=1 is true for all rows of tabU, the effect of this statement is to replace every plain text colU value with encrypted strings returned by the algorithm that ENCRYPT_TDES implements:
EXEC SQL SET ENCRYPTION PASSWORD PERSEPHONE WITH HINT pomegranate EXEC SQL UPDATE tabU SET colU = ENCRYPT_TDES (colU) WHERE 1=1;
This example assumes that the character data type of colU is of sufficient size to store the new encrypted values without truncation. (A more cautious example might execute an appropriate ALTER TABLE statement before the UPDATE.) For additional information about using data encryption in column values of Dynamic Server databases, see Encryption and Decryption Functions on page 4-79, and SET ENCRYPTION PASSWORD on page 2-560.
GETHINT Function
The GETHINT function returns a character string that a previously executed SET ENCRYPTION PASSWORD statement defined for the password that was used when encrypted_data was encrypted by the ENCRYPT_AES function or by the ENCRYPT_TDES function. This hint string typically provides information that helps the user to specify the password needed to return the plain text version of encrypted_data with the DECRYPT_CHAR or DECRYPT_BINARY decryption function. The hint string, however, should not be the same as the password. In the following example, a query returns the hint string into a host variable called myhint:
EXEC SQL SELECT GETHINT(creditcard) INTO :myhint FROM customer WHERE id = :myid;
An error is returned, rather than a hint string, if the encrypted_data argument to the GETHINT function is not an encrypted string or an encrypted large object. For additional information about using data encryption in column values of Dynamic Server databases, see Encryption and Decryption Functions on page 4-79, and SET ENCRYPTION PASSWORD on page 2-560.
Element float_expression
Description An argument to the EXP, LOGN, or LOG10 functions. For the meaning of float_expression in these functions, see the individual heading for each function on the pages that follow.
Restrictions The domain is the set of real numbers, and the range is the set of positive real numbers
EXP Function: The EXP function returns the exponent of a numeric expression. The following example returns the exponent of 3 for each row of the angles table:
Chapter 4. Data Types and Expressions
4-87
Expression
SELECT EXP(3) FROM angles
For this function, the base is always e, the base of natural logarithms, as the following example shows:
e=exp(1)=2.718281828459
When you want to use the base of natural logarithms as the base value, use the EXP function. If you want to specify a particular value to raise to a specific power, see the POW Function on page 4-70. LOG10 Function: The LOG10 function returns the log of a value to base 10. The following example returns the log base 10 of distance for each row of the travel table:
SELECT LOG10(distance) + 1 digits FROM travel
LOG10 Function: The LOG10 function returns the log of a value to base 10. The following example returns the log base 10 of distance for each row of the travel table:
SELECT LOG10(distance) + 1 digits FROM travel
LOGN Function: The LOGN function returns the natural logarithm of a numeric argument. This value is the inverse of the exponential value. The following query returns the natural log of population for each row of the history table:
SELECT LOGN(population) FROM history WHERE country=US ORDER BY date
HEX Function
The HEX function returns the hexadecimal encoding of an integer expression. HEX Function:
HEX ( int_expression )
Element int_expression
Restrictions Must be a literal integer or some other expression that returns an integer
The next example displays the data type and column length of the columns of the orders table in hexadecimal format. For MONEY and DECIMAL columns, you can then determine the precision and scale from the lowest and next-to-the-lowest bytes. For VARCHAR and NVARCHAR columns, you can determine the minimum space and maximum space from the lowest and next-to-the-lowest bytes. For more information about encoded information, see the IBM Informix Guide to SQL: Reference.
SELECT colname, coltype, HEX(collength) FROM syscolumns C, systables T WHERE C.tabid = T.tabid AND T.tabname = orders
The following example lists the names of all the tables in the current database and their corresponding tblspace number in hexadecimal format.
SELECT tabname, HEX(partnum) FROM systables
4-88
Expression
The two most significant bytes in the hexadecimal number constitute the dbspace number. They identify the table in oncheck output (in Dynamic Server) and in onutil check output (in Extended Parallel Server). The HEX function can operate on an expression, as the next example shows:
SELECT HEX(order_num + 1) FROM orders
Length Functions
Use length functions to determine the length of a column, string, or variable. Length Functions:
(1) LENGTH CHAR_LENGTH CHARACTER_LENGTH OCTET_LENGTH ( (2) Quoted String (3) (4) variable_name column table.
Notes: 1 2 3 4
Element column table variable Description Name of a column in table Name of the table in which the specified column occurs Host variable or SPL variable that contains a character string
Each of these functions has a distinct purpose: v LENGTH v OCTET_LENGTH v CHAR_LENGTH (also known as CHARACTER_LENGTH) The LENGTH Function: The LENGTH function returns the number of bytes in a character column, not including any trailing blank spaces. For BYTE or TEXT columns, LENGTH returns the full number of bytes, including any trailing blank spaces. In ESQL/C, LENGTH can also return the length of a character variable. The next example illustrates the use of the LENGTH function:
SELECT customer_num, LENGTH(fname) + LENGTH(lname), LENGTH(How many bytes is this?) FROM customer WHERE LENGTH(company) > 10
See also the discussion of LENGTH in the IBM Informix GLS User's Guide.
4-89
Expression
The OCTET_LENGTH Function: OCTET_LENGTH returns the number of bytes in a character column, including any trailing spaces. See also the IBM Informix GLS User's Guide. The CHAR_LENGTH Function: The CHAR_LENGTH function (also called CHARACTER_LENGTH) returns the number of logical characters (which can be distinct from the number of bytes in some East Asian locales) in a character column value. For a discussion of this function, see the IBM Informix GLS User's Guide.
Element new_module
Description Full pathname of the new shared-object file to replace the shared-object file that old_module specifies
Restrictions The shared-object file must exist with the specified pathname, which can be no more than 255 bytes long
old_module
Full pathname of the shared-object file to The shared-object file must exist with the replace with the shared-object file that specified pathname, which can be no more new_module specifies than 255 bytes long
The IFX_REPLACE_MODULE function returns an integer value to indicate the status of the shared-object-file replacement, as follows: v Zero (0) to indicate success v A negative integer to indicate an error Important: Do not use the IFX_REPLACE_MODULE function to reload a module of the same name. If the full names of the old and new modules that you send to ifx_replace_module( ) are the same, then unpredictable results can occur. After IFX_REPLACE_MODULE completes execution, the database server ages out the old_module shared-object file; that is, all statements subsequent to the IFX_REPLACE_MODULE function will use UDRs in the new_module shared-object file, and the old module will be unloaded when any statements that were using it are complete. Thus, for a brief time, both the old_module and the new_module shared-object files could be resident in memory. If this aging out behavior is undesirable, use the IFX_UNLOAD_MODULE procedure to unload the shared-object file completely. On UNIX, for example, suppose you want to replace the circle.so shared library, which contains UDRs written in the C language. If the old version of this library resides in the /usr/apps/opaque_types directory and the new version in the /usr/apps/shared_libs directory, then the following EXECUTE FUNCTION statement executes the IFX_REPLACE_MODULE function:
4-90
Expression
EXECUTE FUNCTION ifx_replace_module( "/usr/apps/opaque_types/circle.so", "/usr/apps/shared_libs/circle.so", "C")
On Windows, for another example, suppose you want to replace the circle.dll dynamic link library, which contains C UDRs. If the old version of this library resides in the C:\usr\apps\opaque_types directory and the new version in the C:\usr\apps\DLLs directory, then the following EXECUTE FUNCTION statement executes the IFX_REPLACE_MODULE function:
EXECUTE FUNCTION ifx_replace_module( "C:\usr\apps\opaque_types\circle.dll", "C:\usr\apps\DLLs\circle.dll", "C")
To execute the IFX_REPLACE_MODULE function in an IBM Informix ESQL/C application, you must associate the function with a cursor. For more information on how to use IFX_REPLACE_MODULE to replace a shared-object file, see the chapter on how to design a UDR in IBM Informix User-Defined Routines and Data Types Developer's Guide. For information on how to use the IFX_UNLOAD_MODULE procedure, see the section IFX_UNLOAD_MODULE Procedure (IDS, C) on page 2-337.
Description A column of type BLOB; a column of type CLOB Column within table for the copy of the BLOB or CLOB value Name of the system on which to put or get the smart large object Directory path and filename to locate the smart large object Name or synonym of a table that contains column whose storage characteristics are used for the copy of BLOB or CLOB value
Restrictions In table.column, the column must have BLOB or CLOB data type
Must have CLOB or BLOB as its data Quoted String, p. type 4-142 The only valid values are the strings server or client Must exist on file_destination system. See also Pathnames with Commas on page 4-93. Must exist in the database and must contain a CLOB or BLOB column Quoted String, p. 4-142 Quoted String, p. 4-142 Quoted String, p. 4-142
table
FILETOBLOB and FILETOCLOB Functions: The FILETOBLOB function creates a BLOB value for data that is stored in a specified operating-system file. Similarly, the FILETOCLOB function creates a CLOB value for data that is stored in an operating-system file.
4-91
Expression
These functions determine the operating-system file to use from the following parameters: v The pathname parameter identifies the directory path and name of the source file. v The file destination parameter identifies the computer, client or server, on which this file resides: Set file destination to client to identify the client computer as the location of the source file. The pathname can be either a full pathname or relative to the current directory. Set file destination to server to identify the server computer as the location of the source file. The pathname must be a full pathname. The table and column parameters are optional: v If you omit table and column, the FILETOBLOB function creates a BLOB value with the system-specified storage defaults, and the FILETOCLOB function creates a CLOB value with the system-specified storage defaults. These functions obtain the system-specific storage characteristics from either the ONCONFIG file or the sbspace. For more information on system-specified storage defaults, see the IBM Informix Administrator's Guide. v If you specify table and column, the FILETOBLOB and FILETOCLOB functions use the storage characteristics from the specified column for the BLOB or CLOB value that they create. The FILETOBLOB function returns a handle value (a pointer) to the new BLOB value. Similarly, FILETOCLOB returns a handle value to the new CLOB value. Neither function actually copies the smart-large-object value into a database column. You must assign the BLOB or CLOB value to the appropriate column. The FILETOCLOB function performs any code-set conversion that might be required when it copies the file from the client or server computer to the database. The following INSERT statement uses the FILETOCLOB function to create a CLOB value from the value in the smith.rsm file:
INSERT INTO candidate (cand_num, cand_lname, resume) VALUES (2, Smith, FILETOCLOB(smith.rsm, client))
In the preceding example, the FILETOCLOB function reads the smith.rsm file in the current directory on the client computer and returns a handle value to a CLOB value that contains the data in this file. Because the FILETOCLOB function does not specify a table and column name, this new CLOB value has the system-specified storage characteristics. The INSERT statement then assigns this CLOB value to the resume column in the candidate table. The following INSERT statement uses the FILETOBLOB function to create a BLOB value in a remote table, election2006, from the value in the photos.xxx file on the local database server:
INSERT INTO rdb@rserv:election2006 (cand_pic) VALUES (FILETOBLOB(C:\tmp\photos.xxx, server, candidate, cand_photo))
In the preceding example, the FILETOBLOB function reads the photos.xxx file in the specified directory on the local database server and returns a handle value to a BLOB value that contains the data in this file. The INSERT statement then assigns this BLOB value to the cand_pic column in the remote election2006 table. This
4-92
Expression
new BLOB value has the storage characteristics of the cand_photo column in the candidate table on the local database server. In the following example, the new BLOB value has the storage characteristics of the cand_pix column in the election96 table on a remote database server:
INSERT INTO rdb@rserv:election2006 (cand_pic) VALUES (FILETOBLOB(C:\tmp\photos.xxx, server, rdb2@rserv2:election96, cand_pix))
When you qualify the FILETOBLOB or FILETOCLOB function with the name of a remote database and a remote database server, the pathname and the file destination become relative to the remote database server. When you specify server as the file destination, as the following example shows, the FILETOBLOB function looks for the source file (in this case, photos.xxx) on the remote database server:
INSERT INTO rdb@rserv:election (cand_pic) VALUES (rdb@rserv:FILETOBLOB(C:\tmp\photos.xxx, server))
When you specify client as the file destination, however, as in the following example, the FILETOBLOB function looks for the source file (in this case, photos.xxx) on the local client computer:
INSERT INTO rdb@rserv:election (cand_pic) VALUES (rdb@rserv:FILETOBLOB(photos.xxx, client))
Pathnames with Commas: If a comma ( , ) symbol is within the pathname of the function, the database server expects the pathname to have the following format:
"offset, length, pathname"
For pathnames that contain a comma, you must also specify an offset and length, as in the following example:
FILETOBLOB("0,-1,/tmp/blob,x","server")
The first term in the quoted pathname string is an offset of 0, which instructs the database server to begin reading at the start of the file. The second term is a length of -1, which instructs the database server to continue reading until the end of the entire file. The third term is the /tmp/blob,x pathname, specifying which file to read. (Notice the comma symbol that precedes the x.) Because the pathname includes a comma, the comma-separated offset and length specifications are necessary in this example to avoid an error when FILETOBLOB is called. You do not need to specify offset and length for pathnames that include no comma, but including 0,-1, as the initial characters of the pathname string avoids this error for any valid pathname. LOTOFILE Function: The LOTOFILE function copies a smart large object to an operating-system file. The first parameter specifies the BLOB or CLOB column to copy. The function determines what file to create from the following parameters: v The pathname identifies the directory path and the source file name. v The file destination identifies the computer, client or server, on which this file resides:
4-93
Expression
Set file destination to client to identify the client computer as the location of the source file. The pathname can be either a full pathname or a path relative to the current directory. Set file destination to server to identify the server computer as the location of the source file. The full pathname is required. By default, the LOTOFILE function generates a filename of the form:
file.hex_id
In this format, file is the filename you specify in pathname and hex_id is the unique hexadecimal smart-large-object identifier. The maximum number of digits for a smart-large-object identifier is 17; however most smart large objects would have an identifier with significantly fewer digits. For example, suppose that you specify a UNIX pathname value as follows:
/tmp/resume
If the CLOB column has the identifier 203b2, then LOTOFILE creates the file:
/tmp/resume.203b2
For another example, suppose that you specify a Windows pathname value as follows:
C:\tmp\resume
If the CLOB column has an identifier of 203b2, the LOTOFILE function would create the file:
C:\tmp\resume.203b2
To change the default filename, you can specify the following wildcards in the filename of the pathname: v One or more contiguous question mark ( ? ) characters in the filename can generate a unique filename. The LOTOFILE function replaces each question mark with a hexadecimal digit from the identifier of the BLOB or CLOB column. For example, suppose that you specify a UNIX pathname value as follows:
/tmp/resume??.txt
The LOTOFILE function puts 2 digits of the hexadecimal identifier into the name. If the CLOB column has an identifier of 203b2, the LOTOFILE function would create the file:
/tmp/resume20.txt
If you specify more than 17 question marks, LOTOFILE ignores them. v An exclamation ( ! ) point at the end of the filename indicates that the filename does not need to be unique. For example, suppose that you specify a Windows pathname value as follows:
C:\tmp\resume.txt!
The LOTOFILE function does not use the smart-large-object identifier in the filename, so it generates the following file:
C:\tmp\resume.txt
4-94
Expression
The LOTOFILE function performs any code-set conversion that might be required when it copies a CLOB value from the database to a file on the client or server computer. When you qualify LOTOFILE with the name of a remote database and a remote database server, the BLOB or CLOB column, the pathname, and the file destination become relative to the remote database server. When you specify server as the file destination, as in the next example, the LOTOFILE function copies the smart large object from the remote database server to a source file in the specified directory on the remote database server:
rdb@rserv:LOTOFILE(blob_col, C:\tmp\photo.gif!, server)
If you specify client as the file destination, as in the following example, the LOTOFILE function copies the smart large object from the remote database server to a source file in the specified directory on the local client computer:
rdb@rserv:LOTOFILE(clob_col, C:\tmp\essay.txt!, client)
LOCOPY Function: The LOCOPY function creates a copy of a smart large object. The first parameter specifies the BLOB or CLOB column to copy. The table and column parameters are optional. v If you omit table and column, the LOCOPY function creates a smart large object with system-specified storage defaults and copies the data in the BLOB or CLOB column into it. The LOCOPY function obtains the system-specific storage defaults from either the ONCONFIG file or the sbspace. For more information on system-specified storage defaults, see the IBM Informix Administrator's Guide. v When you specify table and column, the LOCOPY function uses the storage characteristics from the specified column for the BLOB or CLOB value that it creates. The LOCOPY function returns a handle value (a pointer) to the new BLOB or CLOB value. This function does not actually store the new smart-large-object value into a column in the database. You must assign the BLOB or CLOB value to the appropriate column. The following ESQL/C code fragment copies the CLOB value in the resume column of the candidate table to the resume column of the interview table:
/* Insert a new row in the interviews table and get the * resulting SERIAL value (from sqlca.sqlerrd[1]) */ EXEC SQL insert into interviews (intrv_num, intrv_time) values (0, 09:30); intrv_num = sqlca.sqlerrd[1]; /* Update this interviews row with the candidate number * and resume from the candidate table. Use LOCOPY to * create a copy of the CLOB value in the resume column * of the candidate table. */ EXEC SQL update interviews SET (cand_num, resume) = (SELECT cand_num, LOCOPY(resume, candidate, resume) FROM candidate WHERE cand_lname = Haven) WHERE intrv_num = :intrv_num;
4-95
Expression
In the preceding example, the LOCOPY function returns a handle value for the copy of the CLOB resume column in the candidate table. Because the LOCOPY function specifies a table and column name, this new CLOB value has the storage characteristics of this resume column. If you omit the table (candidate) and column (resume) names, the LOCOPY function uses the system-defined storage defaults for the new CLOB value. The UPDATE statement then assigns this new CLOB value to the resume column in the interviews table. In the following example, the LOCOPY function executes on the local database server and returns a handle value on the local server for the copy of the BLOB cand_pic column in the remote election2006 table. The INSERT statement then assigns this new BLOB value to the cand_photo column in the local candidate table.
INSERT INTO candidate (cand_photo) SELECT LOCOPY(cand_pic) FROM rdb@rserv:election2006;
When the LOCOPY function executes on the same database server as the original BLOB or CLOB column in a distributed query, it produces two copies of the BLOB or CLOB value, one on the remote database server and the other on the local database server, as the following two examples show. In the first example, the LOCOPY function executes on the remote database server and returns a handle value on the remote server for the copy of the BLOB cand_pic column in the remote election2006 table. The INSERT statement then assigns this new BLOB value to the cand_photo column in the local candidate table:
INSERT INTO candidate (cand_photo) SELECT rdb@rserv:LOCOPY(cand_pic) FROM rdb@rserv:election2006
In the second example, the LOCOPY function executes on the local database server and returns a handle value on the local server for the copy of the BLOB cand_photo column in the local candidate table. The INSERT statement then assigns this new BLOB value to the cand_pic column in the remote election2006 table:
INSERT INTO rdb@rserv:election2006 (cand_pic) SELECT LOCOPY(cand_photo) FROM candidate
Time Functions
Time Functions:
DATE ( non_date_expr ) DAY ( date/dtime_expr ) MONTH WEEKDAY YEAR EXTEND ( date/dtime_expr ) , first TO last MDY ( month , day , year ) (1) TO_CHAR ( source_date TO_DATE ( char_expression , format_string
4-96
Expression
Element char _expression date/dtime _expr day first Description Expression to be converted to a DATE or DATETIME value Expression that returns a DATE or DATETIME value Expression that returns the number of a day of the month Largest time unit in the result. If you omit first and last, the default first is YEAR. String that contains a format mask for the DATE or DATETIME value Smallest time unit in the result Restrictions Syntax
Must be a literal, host variable, expression, or Expression, column of a character type p. 4-34 Must return a DATE or DATETIME value Must return integer > 0 but not greater than number of days in specified month Must be a DATETIME qualifier that specifies a time unit no smaller than last Must be a character data type and contain a valid date format. Can be a column, host variable, expression, or constant Must be a DATETIME qualifier that specifies a time unit no smaller than first Expression, p. 4-34 Expression, p. 4-34 DATETIME Qualifier, p. 4-32 Quoted String, p. 4-142 DATETIME Qualifier, p. 4-32
format_string
last
Expression that represents the number of the month Expression that represents a value to be converted to a DATE data type Date to be converted to a character string Expression that represents the year
Must evaluate to an integer in the range from Expression, 1 to 12, inclusive p. 4-34 Typically an expression that returns a CHAR, Expression, DATETIME, or INTEGER value that can be p. 4-34 converted to a DATE data type Type DATETIME or DATE. Can be host variable, expression, column, or constant. Expression, p. 4-34
Must evaluate to a 4-digit integer. You cannot Expression, use a 2-digit abbreviation. p. 4-34
DATE Function: The DATE function converts a non-DATE expression to a DATE value. The argument can be any expression that can be converted to a DATE value, usually a CHAR, DATETIME, or INTEGER value. The following WHERE clause specifies a CHAR value for the non-DATE expression:
WHERE order_date < DATE(12/31/04)
When the DATE function interprets a CHAR non-DATE expression, it expects this expression to conform to any DATE format that the DBDATE environment specifies. For example, suppose DBDATE is set to Y2MD/ when you execute the following query:
SELECT DISTINCT DATE(02/01/1998) FROM ship_info
This SELECT statement generates an error because the DATE function cannot convert this non-DATE expression. The DATE function interprets the first part of the date string (02) as the year and the second part (01) as the month. For the third part (1998), the DATE function encounters four digits when it expects a two-digit day (valid day values must be between 01 and 31). It therefore cannot convert the value. For the SELECT statement to execute successfully with the Y2MD/ value for DBDATE, the non-DATE expression would need to be 98/02/01. For information on the format of DBDATE, see the IBM Informix Guide to SQL: Reference. When you specify a positive INTEGER value for the non-DATE expression, the DATE function interprets this as the number of days after December 31, 1899.
4-97
Expression
If the integer value is negative, the DATE function interprets the value as the number of days before December 31, 1899. The following WHERE clause specifies an INTEGER value for the non-DATE expression:
WHERE order_date < DATE(365)
The database server searches for rows with an order_date value less than December 31, 1900 (which is 12/31/1899 plus 365 days). DAY Function: The DAY function returns an integer that represents the day of the month. The following example uses the DAY function with the CURRENT function to compare column values to the current day of the month:
WHERE DAY(order_date) > DAY(CURRENT)
MONTH Function: The MONTH function returns an integer corresponding to the month portion of its type DATE or DATETIME argument. The following example returns a number from 1 through 12 to indicate the month when the order was placed:
SELECT order_num, MONTH(order_date) FROM orders
WEEKDAY Function: The WEEKDAY function returns an integer that represents the day of the week; zero (0) represents Sunday, one (1) represents Monday, and so on. The following example lists all the orders that were paid on the same day of the week, which is the current day:
SELECT * FROM orders WHERE WEEKDAY(paid_date) = WEEKDAY(CURRENT)
YEAR Function: The YEAR function returns a four-digit integer that represents the year. The following example lists orders in which the ship_date is earlier than the beginning of the current year:
SELECT order_num, customer_num FROM orders WHERE year(ship_date) < YEAR(TODAY)
Similarly, because a DATE value is a simple calendar date, you cannot add or subtract a DATE value with an INTERVAL value whose last qualifier is smaller than DAY. In this case, convert the DATE value to a DATETIME value. EXTEND Function: The EXTEND function adjusts the precision of a DATETIME or DATE value. The expression cannot be a quoted string representation of a DATE value. If you do not specify first and last qualifiers, the default qualifiers are YEAR TO FRACTION(3). If the expression contains fields that are not specified by the qualifiers, the unwanted fields are discarded. If the first qualifier specifies a larger (that is, more significant) field than what exists in the expression, the new fields are filled in with values returned by the CURRENT function. If the last qualifier specifies a smaller field (that is, less significant) than what exists in the expression, the new fields are filled in with constant values. A missing MONTH or DAY field is filled in with 1, and the missing HOUR to FRACTION fields are filled in with 0.
4-98
Expression
In the following example, the first EXTEND call evaluates to the call_dtime column value of YEAR TO SECOND. The second statement expands a literal DATETIME so that an interval can be subtracted from it. You must use the EXTEND function with a DATETIME value if you want to add it to or subtract it from an INTERVAL value that does not have all the same qualifiers. The third example updates only a portion of the datetime value, the hour position. The EXTEND function yields just the hh:mm part of the datetime. Subtracting 11:00 from the hours and minutes of the datetime yields an INTERVAL value of the difference, plus or minus, and subtracting that from the original value forces the value to 11:00.
EXTEND (call_dtime, YEAR TO SECOND) EXTEND (DATETIME (1989-8-1) YEAR TO DAY, YEAR TO MINUTE) - INTERVAL (720) MINUTE (3) TO MINUTE UPDATE cust_calls SET call_dtime = call_dtime (EXTEND(call_dtime, HOUR TO MINUTE) - DATETIME (11:00) HOUR TO MINUTE) WHERE customer_num = 106
MDY Function: The MDY function returns a type DATE value with three expressions that evaluate to integers that represent the month, day, and year. The first expression must evaluate to an integer that represents the number of the month (1 to 12). The second expression must evaluate to an integer that represents the number of the day of the month (1 to 28, 29, 30, or 31, as appropriate for the month). The third expression must evaluate to a four-digit integer that represents the year. You cannot use a two-digit abbreviation for the third expression. The following example sets the paid_date associated with the order number 8052 equal to the first day of the present month:
UPDATE orders SET paid_date = MDY(MONTH(TODAY), 1, YEAR(TODAY)) WHERE po_num = 8052
TO_CHAR Function (IDS): The TO_CHAR function converts a DATE or DATETIME value to a character string. The character string contains the date that was specified in the source_date parameter and represents this date in the format that was specified in the format_string parameter. Any argument to this function must be of a built-in data type. If the value of the source_date parameter is NULL, the function returns a NULL value. If you omit the format_string parameter, the TO_CHAR function uses the default date format to format the character string. The default date format is specified by environment variables such as GL_DATETIME and GL_DATE. The format_string parameter does not have to imply the same qualifiers as the source_date parameter. When the implied formatting mask qualifier in format_string is different from the qualifier in source_date, the TO_CHAR function extends the DATETIME value as if it had called the EXTEND function. In the following example, the user wants to convert the begin_date column of the tab1 table to a character string. The begin_date column is defined as a DATETIME YEAR TO SECOND data type. The user uses a SELECT statement with the TO_CHAR function to perform this conversion:
Chapter 4. Data Types and Expressions
4-99
Expression
SELECT TO_CHAR(begin_date, %A %B %d, %Y %R) FROM tab1
The symbols in the format_string parameter in this example have the following meanings. For a complete list of format symbols and their meanings, see the GL_DATE and GL_DATETIME environment variables in the IBM Informix GLS User's Guide. Symbol %A %B %d %Y %R Meaning Full weekday name as defined in the locale Full month name as defined in the locale Day of the month as a decimal number Year as a 4-digit decimal number Time in 24-hour notation
The result of applying the specified format_string to the begin_date column is as follows:
Wednesday July 23, 1997 18:45
TO_DATE Function (IDS): The TO_DATE function converts a character string to a DATETIME value. The function evaluates the char_expression parameter as a date according to the date format you specify in the format_string parameter and returns the equivalent date. If char_expression is NULL, then a NULL value is returned. Any argument to the TO_DATE function must be of a built-in data type. If you omit the format_string parameter, the TO_DATE function applies the default DATETIME format to the DATETIME value. The default DATETIME format is specified by the GL_DATETIME environment variable. In the following example, the user wants to convert a character string to a DATETIME value in order to update the begin_date column of the tab1 table with the converted value. The begin_date column is defined as a DATETIME YEAR TO SECOND data type. The user uses an UPDATE statement that contains a TO_DATE function to accomplish this result:
UPDATE tab1 SET begin_date = TO_DATE(Wednesday July 23, 1997 18:45, %A %B %d, %Y %R);
The format_string parameter in this example tells the TO_DATE function how to format the converted character string in the begin_date column. For a table that shows the meaning of each format symbol in this format string, see TO_CHAR Function (IDS) on page 4-99.
Trigonometric Functions
The built-in trigonometric functions have the following syntax.
4-100
Expression
Trigonometric Functions:
COS ( radian_ expr ) SIN TAN ASIN ( numeric_expr ) ACOS ATAN ATAN2 ( y, x )
Description Expression that serves as an argument to the ASIN, ACOS, or ATAN functions
Expression that represents the number of radians Must return a numeric value Expression that represents the x coordinate of the Must return a numeric value rectangular coordinate pair (x, y) Expression that represents the y coordinate of the rectangular coordinate pair (x, y) Must return a numeric value
Formulas for Radian Expressions: The COS, SIN, and TAN functions take the number of radians (radian_expr) as an argument. If you are using degrees and want to convert degrees to radians, use the following formula:
# degrees * p/180= # radians
COS Function: The COS function returns the cosine of a radian expression. The following example returns the cosine of the values of the degrees column in the anglestbl table. The expression passed to the COS function in this example converts degrees to radians.
SELECT COS(degrees*180/3.1416) FROM anglestbl
SIN Function: The SIN function returns the sine of a radian expression. This example returns the sine of the values in the radians column of the anglestbl table:
SELECT SIN(radians) FROM anglestbl
TAN Function: The TAN function returns the tangent of a radian expression. This example returns the tangent of the values in the radians column of the anglestbl table:
SELECT TAN(radians) FROM anglestbl
ACOS Function: The ACOS function returns the arc cosine of a numeric expression. The following example returns the arc cosine of the value (-0.73) in radians:
SELECT ACOS(-0.73) FROM anglestbl
ASIN Function: The ASIN function returns the arc sine of a numeric expression. The following example returns the arc sine of the value (-0.73) in radians:
SELECT ASIN(-0.73) FROM anglestbl
4-101
Expression
ATAN Function: The ATAN function returns the arc tangent of a numeric expression. The following example returns the arc tangent of the value (-0.73) in radians:
SELECT ATAN(-0.73) FROM anglestbl
ATAN2 Function: The ATAN2 function computes the angular component of the polar coordinates (r, q) associated with (x, y). The following example compares angles to q for the rectangular coordinates (4, 5):
WHERE angles > ATAN2(4,5) --determines q for (4,5) and --compares to angles
You can determine the length of the radial coordinate r using the expression that the following example shows:
SQRT(POW(x,2) + POW(y,2)) --determines r for (x,y)
You can determine the length of the radial coordinate r for the rectangular coordinates (4,5) using the expression that the following example shows:
SQRT(POW(4,2) + POW(5,2)) --determines r for (4,5)
String-Manipulation Functions
String-manipulation functions perform various operations on strings of characters. The syntax for string-manipulation functions is as follows. String-Manipulation Functions:
(1) TRIM Function (2) SUBSTRING Function (3) (4) SUBSTR Function (3) (5) REPLACE Function (3) (6) LPAD Function (3) (7) RPAD Function (3) Case-Conversion Functions
(8)
Notes: 1 2 3 4 5 6 7 8 See page 4-102 See page 4-104 Informix extension See page 4-105 See page 4-107 See page 4-107 See page 4-108 See page 4-109
TRIM Function: The TRIM function removes leading or trailing pad characters from a string.
4-102
Expression
TRIM Function:
TRIM ( BOTH TRAILING LEADING FROM trim_expression source_expression)
Description Expression that evaluates to a single character or NULL. Default is a blank space ( = ASCII 32)
Character expression, including a character column Cannot be LVARCHAR nor Quoted String, p. name, or a call to another TRIM function a host variable 4-142
The TRIM function returns a VARCHAR value identical to its character string argument, except that any leading or trailing whitespace characters, if specified, are deleted. If no trim qualifier (LEADING, TRAILING, or BOTH) is specified, BOTH is the default. If no trim_expression is used, a single blank space is assumed. If either the trim_expression or the source_expression evaluates to null, the result of the TRIM function is NULL. The maximum length of the returned string must be 255 bytes or fewer, because the VARCHAR data type supports no more than 255 bytes. The following example shows some generic uses for the TRIM function:
SELECT TRIM (c1) FROM tab; SELECT TRIM (TRAILING # FROM c1) FROM tab; SELECT TRIM (LEADING FROM c1) FROM tab; UPDATE c1=xyz FROM tab WHERE LENGTH(TRIM(c1))=5; SELECT c1, TRIM(LEADING # FROM TRIM(TRAILING % FROM ###abc%%%)) FROM tab;
When you use the DESCRIBE statement with a SELECT statement that uses the TRIM function in the projection list, the described character type of the trimmed column depends on the database server that you are using and on the data type of the source_expression. For further information on the GLS aspects of the TRIM function in ESQL/C, see the IBM Informix GLS User's Guide. Fixed Character Columns: The TRIM function can be specified on fixed-length character columns. If the length of the string is not completely filled, the unused characters are padded with blank space. Figure 4-3 shows this concept for the column entry ##A2T##, where the column is defined as CHAR(10).
1 # 2 # 3 A 4 2 5 T 6 # 7 # 8 9 10
Characters
Blank padded
If you want to trim the sharp sign ( # ) trim_expression from the column, you need to consider the blank padded spaces as well as the actual characters. For example, if you specify the trim specification BOTH, the result from the trim operation is A2T##, because the TRIM function does not match the blank padded
Chapter 4. Data Types and Expressions
4-103
Expression
space that follows the string. In this case, the only sharp signs ( # ) trimmed are those that precede the other characters. The SELECT statement is shown, followed by Figure 4-4, which presents the result.
SELECT TRIM(LEADING # FROM col1) FROM taba;
1 A 2
3 T #
4 #
10
Characters
Blank padded
SUBSTRING Function: The SUBSTRING function returns a subset of a character string. SUBSTRING Function:
SUBSTRING ( source_string FROM start_position FOR length )
Description Number of characters to return from source_string String argument to the SUBSTRING function Position in source_string of first returned character
Restrictions Must be an expression, constant, column, or host variable that returns an integer Must be an expression, constant, column, or host variable whose value can be converted to a character data type Must be an expression, constant, column, or host variable that returns an integer
start_position
Any argument to the SUBSTRING function must be of a built-in data type. The subset begins at the column position that start_position specifies. The following table shows how the database server determines the starting position of the returned subset based on the input value of the start_position.
4-104
Expression
Value of Start_Position Positive How the Database Server Determines the Starting Position of the Return Subset Counts forward from the first character in source_string For example, if start_position = 1, the first character in the source_string is the first character in the returned subset. Zero (0) Counts from one position before (that is, to the left of) the first character in source_string For example, if start_position = 0 and length = 1, the database server returns NULL, whereas if length = 2, the database server returns the first character in source_string. Negative Counts backward from one position after (that is, to the right of) the last character in source_string For example, if start_position = -1, the starting position of the returned subset is the last character in source_string.
In locales for languages with a right-to-left writing direction, such as Arabic, Farsi, or Hebrew, right should replace left in the preceding table. The size of the subset is specified by length. The length parameter refers to the number of logical characters, rather than to the number of bytes. If you omit the length parameter, or if you specify a length that is greater than the number of characters from start_position to the end of source_string, the SUBSTRING function returns the entire portion of source_ string that begins at start_position. The following example specifies that the subset of the source string that begins in column position 3 and is two characters long should be returned:
SELECT SUBSTRING(ABCDEFG FROM 3 FOR 2) FROM mytable
In the following example, the user specifies a negative start_position for the return subset:
SELECT SUBSTRING(ABCDEFG FROM -3 FOR 7) FROM mytable
The database server starts at the -3 position (four positions before the first character) and counts forward for 7 characters. The following table shows the output of this SELECT statement.
(constant) ABC
SUBSTR Function: The SUBSTR function has the same purpose as the SUBSTRING function (to return a subset of a source string), but it uses different syntax.
4-105
Expression
SUBSTR Function:
SUBSTR ( source_string , start_position , length )
Description Number of characters to be returned from source_string String that serves as input to the SUBSTR function Column position in source_string where the SUBSTR function starts to return characters
Restrictions Must be an expression, literal, column, or host variable that returns an integer Must be an expression, literal, column, or host variable of a data type that can be converted to a character data type Must be an integer expression, literal, column, or host variable. Can have a plus sign ( + ), a minus sign ( - ), or no sign.
start_position
Any argument to the SUBSTR function must be of a built-in data type. The SUBSTR function returns a subset of source_string. The subset begins at the column position that start_position specifies. The following table shows how the database server determines the starting position of the returned subset based on the input value of the start_position.
Value of Start_Position Positive Zero (0) Negative How the Database Server Determines the Starting Position of the Returned Subset Counts forward from the first character in source_string Counts forward from the first character in source_string (that is, treats a start_position of 0 as equivalent to 1) Counts backward from an origin that immediately follows the last character in source_string A value of -1 returns the last character in source_string.
The length parameter specifies the number of logical characters (not the number of bytes) in the subset. If you omit the length parameter, the SUBSTR function returns the entire portion of source_string that begins at start_position. If you specify a negative start_position whose absolute value is greater than the number of characters in source_string, or if length is greater than the number of characters from start_position to the end of source_string, SUBSTR returns NULL. (In this case, the behavior of SUBSTR is different from that of the SUBSTRING function, which returns all the characters from start_position to the last character of source_string, rather than returning NULL.) The next example specifies that the string of characters to be returned begins at a starting position 3 characters before the end of a 7-character source_string. This implies that the starting position is the fifth character of source_string. Because the user does not specify a value for length, the database server returns a string that includes all characters from character-position 5 to the end of source_string.
SELECT SUBSTR(ABCDEFG, -3) FROM mytable
4-106
Expression
The following table shows the output of this SELECT statement.
(constant) EFG
REPLACE Function: The REPLACE function replaces specified characters within a source string with different characters. REPLACE Function:
REPLACE ( source_string , old_string , new_string )
Element new_string
Description
Restrictions
Character or characters that replace Must be an expression, constant, column, or old_string in the return string host variable of a data type that can be converted to a character data type Character or characters in Must be an expression, constant, column, or source_string that are to be replaced host variable of a data type that can be by new_string converted to a character data type String of characters argument to the REPLACE function Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type
old_string
source_string
Any argument to the REPLACE function must be of a built-in data type. The REPLACE function returns a copy of source_string in which every occurrence of old_string is replaced by new_string. If you omit the new_string option, every occurrence of old_string is omitted from the return string. In the following example, the user replaces every occurrence of xz in the source string with t:
SELECT REPLACE(Mighxzy xzime, xz, t) FROM mytable
LPAD Function: The LPAD function returns a copy of source_string that is left-padded to the total number of characters specified by length. LPAD Function:
LPAD ( source_string , length , pad_string )
4-107
Expression
Element length Description Integer value that specifies total number of characters in the returned string String that specifies the pad character or characters String that serves as input to the LPAD function Restrictions Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Syntax Literal Number, p. 4-137 Expression, p. 4-34 Expression, p. 4-34
pad_string
source_string
Any argument to the LPAD function must be of a built-in data type. The pad_string parameter specifies the character or characters to be used for padding the source string. The sequence of pad characters occurs as many times as necessary to make the return string the length specified by length. The series of pad characters in pad_string is truncated if it is too long to fit into length. If you specify no pad_string, the default value is a single blank. In the following example, the user specifies that the source string is to be left-padded to a total length of 16 characters. The user also specifies that the pad characters are a series consisting of a hyphen and an underscore ( -_ ).
SELECT LPAD(Here we are, 16, -_) FROM mytable
RPAD Function: The RPAD function returns a copy of source_string that is right-padded to the total number of characters that length specifies. RPAD Function:
RPAD ( source_string , length , pad_string )
Description The number of characters in the returned string String that specifies the pad character or characters String that serves as input to the RPAD function
Restrictions Must be an expression, constant, column, or host variable that returns an integer Must be an expression, column, constant, or host variable of a data type that can be converted to a character data type Same as for pad_stringe
source_string
Any argument to the RPAD function must be of a built-in data type. The pad_string parameter specifies the pad character or characters to be used to pad the source string.
4-108
Expression
The series of pad characters occurs as many times as necessary to make the return string reach the length that length specifies. The series of pad characters in pad_string is truncated if it is too long to fit into length. If you omit the pad_string parameter, the default value is a single blank space. In the following example, the user specifies that the source string is to be right-padded to a total length of 18 characters. The user also specifies that the pad characters to be used are a sequence consisting of a question mark and an exclamation point ( ?! )
SELECT RPAD(Where are you, 18, ?!) FROM mytable
Case-Conversion Functions
The case-conversion functions perform lettercase conversion on alphabetic characters. In the default locale, only the ASCII characters A - Z and a - z can be modified by these functions, which enable you to perform case-insensitive searches in your queries and to specify the format of the output. The case-conversion functions are UPPER, LOWER, and INITCAP. The following diagram shows the syntax of these case-conversion functions. Case-Conversion Functions:
UPPER LOWER INITCAP ( expression )
Element expression
Restrictions Must be a character type. If a host variable, its length must be long enough to store the converted string.
The expression must return a character data type. When the column is described, the data type returned by the database server is that of expression. For example, if the input type is CHAR, the output type is also CHAR. Argument to these functions must be of the built-in data types. In all locales, the byte length returned from the description of a column with a case-conversion function is the input byte length of the source string. If you use a case-conversion function with a multibyte expression argument, the conversion might increase or decrease the length of the string. If the byte length of the result string exceeds the byte length expression, the database server truncates the result string to fit into the byte length of expression. Only characters designated as ALPHA class in the locale file are converted, and this occurs only if the locale recognizes the construct of lettercase. If expression is NULL, the result of a case-conversion function is also NULL.
Chapter 4. Data Types and Expressions
4-109
Expression
The database server treats a case-conversion function as an SPL routine in the following instances: v If it has no argument v If it has one argument, and that argument is a named argument v If it has more than one argument v If it appears in a projection list with a host variable as an argument If none of the conditions in the preceding list are met, the database server treats a case-conversion function as a system function. The following example uses all the case-conversion functions in the same query to specify multiple output formats for the same value:
Input value: SAN Jose Query: SELECT City, LOWER(City), LOWER("City"), UPPER (City), INITCAP(City) FROM Weather; Query output: SAN Jose san jose city SAN JOSE San Jose
UPPER Function: The UPPER function accepts an expression argument and returns a character string in which every lowercase alphabetical character in the expression is replaced by a corresponding uppercase alphabetic character. The following example uses the UPPER function to perform a case-insensitive search on the lname column for all employees with the last name of Curran:
SELECT title, INITCAP(fname), INITCAP(lname) FROM employees WHERE UPPER (lname) = "CURRAN"
Because the INITCAP function is specified in the projection list, the database server returns the results in a mixed-case format. For example, the output of one matching row might read: accountant James Curran. LOWER Function: The LOWER function accepts an expression argument and returns a character string in which every uppercase alphabetic character in the expression is replaced by a corresponding lowercase alphabetic character. The following example shows how to use the LOWER function to perform a case-insensitive search on the City column. This statement directs the database server to replace all instances (that is, any variation) of the words san jose, with the mixed-case format, San Jose.
UPDATE Weather SET City = "San Jose" WHERE LOWER (City) = "san jose";
INITCAP Function: The INITCAP function returns a copy of the expression in which every word in the expression begins with an uppercase letter. With this function, a word begins after any character other than a letter. Thus, in addition to a blank space, symbols such as commas, periods, colons, and so on, introduce a new word. For an example of the INITCAP function, see UPPER Function on page 4-110.
4-110
Expression
IFX_ALLOW_NEWLINE Function
The IFX_ALLOW_NEWLINE function sets a newline mode that allows newline characters in quoted strings or disallows newline characters in quoted strings within the current session. IFX_ALLOW_NEWLINE Function:
IFX_ALLOW_NEWLINE ( t f )
If you enter t as the argument of this function, you enable newline characters in quoted strings in the session. If you enter f as the argument, you disallow newline characters in quoted strings in the session. You can set the newline mode for all sessions by setting the ALLOW_NEWLINE parameter in the ONCONFIG file to a value of 0 (newline characters not allowed) or to a value of 1 (newline characters allowed). If you do not set this configuration parameter, the default value is 0. Each time you start a session, the new session inherits the newline mode set in the ONCONFIG file. To change the newline mode for the session, execute the IFX_ALLOW_NEWLINE function. Once you have set the newline mode for a session, the mode remains in effect until the end of the session or until you execute the IFX_ALLOW_NEWLINE function again within the session. In the following example, assume that you did not specify any value for the ALLOW_NEWLINE parameter in the ONCONFIG file, so, by default, newline characters are not allowed in quoted strings in any session. After you start a new session, you can enable newline characters in quoted strings in that session by executing the IFX_ALLOW_NEWLINE function:
EXECUTE PROCEDURE IFX_ALLOW_NEWLINE(t)
In ESQL/C, the newline mode that is set by the ALLOW_NEWLINE parameter in the ONCONFIG file or by the execution of the IFX_ALLOW_NEWLINE function in a session applies only to quoted-string literals in SQL statements. The newline mode does not apply to quoted strings contained in host variables in SQL statements. Host variables can contain newline characters within string data regardless of the newline mode currently in effect. For example, you can use a host variable to insert data that contains newline characters into a column even if the ALLOW_NEWLINE parameter in the ONCONFIG file is set to 0. For further information on how the IFX_ALLOW_NEWLINE function affects quoted strings, see Quoted String on page 4-142. For further information on the ALLOW_NEWLINE parameter in the ONCONFIG file, see the IBM Informix Administrator's Reference.
User-Defined Functions
A user-defined function is a function that you write in SPL or in a language external to the database, such as C or Java. User-Defined Functions:
4-111
Expression
, (1) function ( parameter = ) (2) , Statement-Local Variable Declaration (3) Expression
Notes: 1 2 3
Element function parameter Description Name of the function Name of an argument that was declared in a CREATE FUNCTION statement
See page Expression on page 4-34 Dynamic Server only See page Statement-Local Variable Declaration (IDS) on page 4-113
Restrictions Function must exist If you use the parameter = option for any argument in the called function, you must use it for all arguments Syntax Database Object Name, p. 5-17 Identifier, p. 5-22
You can call user-defined functions within SQL statements. Unlike built-in functions, user-defined functions can only be used by the creator of the function, the DBA, and the users who have been granted the Execute privilege on the function. For more information, see GRANT on page 2-371. The following examples show some user-defined function expressions. The first example omits the parameter option when it lists the function argument:
read_address(Miller)
This second example uses the parameter option to specify the argument value:
read_address(lastname = Miller)
When you use the parameter option, the parameter name must match the name of the corresponding parameter in the function registration. For example, the preceding example assumes that the read_address( ) function had been registered as follows:
CREATE FUNCTION read_address(lastname CHAR(20)) RETURNING address_t ... ;
In Dynamic Server, a statement-local variable (SLV) enables you to transmit a value from a user-defined function call to another part of the SQL statement. To use an SLV with a call to a user-defined function: 1. Write one or more OUT parameters (and for UDRs written in Java language, INOUT parameters) for the user-defined function. For information on how to write a UDR with OUT or INOUT parameters, see IBM Informix User-Defined Routines and Data Types Developer's Guide. 2. When you register the user-defined function, specify the OUT keyword before each OUT parameter, and the INOUT keyword before each INOUT parameter.
4-112
Expression
For more information, see Specifying INOUT Parameters for a User-Defined Routine (IDS) on page 5-64, and Specifying OUT Parameters for User-Defined Routines on page 5-63. 3. Declare the SLV in a function expression that calls the user-defined function with each OUT and INOUT parameter. The call to the user-defined function must be made within a WHERE clause. For information about the syntax to declare the SLV, see Statement-Local Variable Declaration (IDS) on page 4-113. 4. Use the SLV that the user-defined function has initialized within the SQL statement. Once the call to the user-defined function has initialized the SLV, you can use this value in other parts of the SQL statement. For information about the use of an SLV within an SQL statement, see Statement-Local Variable Expressions (IDS) on page 4-114. Statement-Local Variable Declaration (IDS): The Statement-Local Variable Declaration declares a statement-local variable (SLV) in a call to a user-defined function that defines one or more OUT or INOUT parameters. Statement-Local Variable Declaration:
(1) slv_name # Built-In Data Type opaque_data_type distinct_data_type (2) Complex Data Type
Notes: 1 2
Element distinct_data_type opaque_data_type slv_name
Description Name of a distinct data type Name of an opaque data type Name of a statement local variable you are defining
You declare an SLV in a user-defined function call so that a user-defined function can assign the value of its OUT or INOUT parameter to the SLV. The UDF must be invoked in the WHERE clause of the SQL statement. For example, if you register a function with the following CREATE FUNCTION statement, you can use its y parameter as an SLV in a WHERE clause:
CREATE FUNCTION find_location(a FLOAT, b FLOAT, OUT y INTEGER) RETURNING VARCHAR(20) EXTERNAL NAME "/usr/lib/local/find.so" LANGUAGE C
4-113
Expression
In this example, find_location( ) accepts two FLOAT values that represent a latitude and a longitude and return the name of the nearest city with an extra value of type INTEGER that represents the population rank of the city. You can now call find_location( ) in a WHERE clause:
SELECT zip_code_t FROM address WHERE address.city = find_location(32.1, 35.7, rank # INT) AND rank < 101;
The function expression passes two FLOAT values to find_location( ) and declares an SLV named rank of type INT. In this case, find_location( ) will return the name of the city nearest latitude 32.1 and longitude 35.7 (which might be a heavily populated area) whose population rank is between 1 and 100. The statement then returns the zip code that corresponds to that city. The WHERE clause of the SQL statement must produce an SLV that is used within other parts of the statement. The following SELECT statement is invalid because the projection list of the Projection clause produces the SLV:
-- invalid SELECT statement SELECT title, contains(body, dog and cat, rank # INT), rank FROM documents
The data type you use when you declare the SLV in a statement must be the same as the data type of the corresponding OUT or INOUT parameter in the CREATE FUNCTION statement. If you use different but compatible data types, such as INTEGER and FLOAT, the database server automatically performs the cast between the data types. SLVs share the name space with UDR variables and the column names of the table involved in the SQL statement. Therefore, the database uses the following precedence to resolve ambiguous situations: v UDR variables v Column names v SLVs Once the user-defined function assigns its OUT parameter to the SLV, you can use this SLV value in other parts of the SQL statement. For more information, see Statement-Local Variable Expressions (IDS) on page 4-114.
Element SLV_variable
Description Statement-local variable (SLV) assigned in a call to a user-defined function in the same SQL statement
Restrictions The SLV_variable exists only for the life of the statement. Its name must be unique within the statement
4-114
Expression
You define an SLV in the call to a user-defined function in the WHERE clause of the SQL statement. This user-defined function must be defined with one or more OUT or INOUT parameters. The call to the user-defined function assigns the value of the OUT or INOUT parameters to the SLVs. For more information, see Statement-Local Variable Declaration (IDS) on page 4-113. Once the user-defined function assigns its OUT or INOUT parameters to the SLVs, you can use these values in other parts of the SQL statement, subject to the following scope-of-reference rules: v The SLV is read-only throughout the query (or subquery) in which it is defined. v The scope of an SLV extends from the query in which the SLV is defined down into all nested subqueries. In other words, if a query contains a subquery, an SLV that is visible in the query is also visible to all subqueries of that query. v In nested queries, the scope of an SLV does not extend upwards. In other words, if a query contains a subquery and the SLV is defined in the subquery, it is not visible to the parent query. v In queries that involve UNION, the SLV is only visible in the query in which it is defined. The SLV is not visible to all other queries involved in the UNION. v For INSERT, DELETE, and UPDATE statements, an SLV is not visible outside the SELECT portion of the statement. Within this SELECT portion, all the above scoping rules apply. Important: A statement-local variable is in scope only for the duration of a single SQL statement. The following SELECT statement calls the find_location( ) function in a WHERE clause and defines the rank SLV. Here find_location( ) accepts two values that represent a latitude and a longitude and return the name of the nearest city with an extra value of type INTEGER that represents the population rank of the city.
SELECT zip_code_t FROM address WHERE address.city = find_location(32.1, 35.7, rank # INT) AND rank < 101;
When execution of the find_location() function completes successfully, the function has initialized the rank SLV. The SELECT then uses this rank value in a second WHERE clause condition. In this example, the Statement-Local Variable Expression is the variable rank in the second WHERE clause condition:
rank < 101
The number of OUT and INOUT parameters and SLVs that a UDF can have is not restricted. (Releases of Dynamic Server earlier than Version 9.4 restricted user-defined functions to a single OUT parameter and no INOUT parameters, thereby restricting the number of SLVs to no more than one.) If the user-defined function that initializes the SLVs is not executed in an iteration of the statement, the SLVs each have a value of NULL. Values of SLVs do not persist across iterations of the statement. At the start of each iteration, the database server sets the SLV values to NULL. The following partial statement calls two user-defined functions with OUT parameters, whose values are referenced with the SLV names out1 and out2:
Chapter 4. Data Types and Expressions
4-115
Expression
SELECT... WHERE func_2(x, out1 # INTEGER) < 100 AND (out1 = 12 OR out1 = 13) AND func_3(a, out2 # FLOAT) = "SAN FRANCISCO" AND out2 = 3.1416;
If a function assigns one or more OUT or INOUT parameter values from another database of the local database server to SLVs, the values must be of built-in data types, or DISTINCT data types whose base types are built-in data types (and that you explicitly cast to built-in data types), or must be UDTs that you explicitly cast to built-in data types. All the UDTs, DISTINCT types, and casts must be defined in all of the participating databases. For more information on how to write a user-defined function with OUT or INOUT parameters, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
Aggregate Expressions
An aggregate expression uses an aggregate function to summarize selected database data. The built-in aggregate functions have the following syntax. Aggregate Expressions:
COUNT( *) Aggregate Scope Qualifiers ) ( Aggregate Scope Qualifiers ) ALL (1) Subset of Expression
AVG MAX MIN SUM RANGE STDEV VARIANCE (2) User-Defined Aggregates
(3)
Notes: 1 2 3
Element column synonym, table, view Description Column to which aggregate function is applied
See page Subset of Expressions Valid in an Aggregate Expression on page 4-118 Dynamic Server only See page User-Defined Aggregates on page 4-117
Restrictions See headings for individual keywords on pages that follow Syntax Identifier, p. 5-22 Database Object Name, p. 5-17
Synonym, table, or view that contains Synonym and the table or view to column which it points must exist
4-116
Expression
You cannot use an aggregate expression in a condition that is part of a WHERE clause unless you use the aggregate expression within a subquery. You cannot apply an aggregate function to a BYTE or TEXT column. For other general restrictions, see Subset of Expressions Valid in an Aggregate Expression on page 4-118. An aggregate function returns one value for a set of queried rows. The following examples show aggregate functions in SELECT statements:
SELECT SUM(total_price) FROM items WHERE order_num = 1013 SELECT COUNT(*) FROM orders WHERE order_num = 1001 SELECT MAX(LENGTH(fname) + LENGTH(lname)) FROM customer
If you use an aggregate function and one or more columns in the projection list of the Projection clause, you must put all the column names that are not used as part of an aggregate or time expression in the GROUP BY clause.
For more information on how to extend built-in aggregates, see IBM Informix User-Defined Routines and Data Types Developer's Guide. For information on how to invoke built-in aggregates, see the descriptions of individual built-in aggregates in the following pages. User-Defined Aggregates: A user-defined aggregate is an aggregate that you define to perform an aggregate computation that the database server does not provide. For example, you can create a user-defined aggregate named SUMSQ that returns the sum of the squared values of a specified column. User-defined aggregates can work with built-in data types or extended data types or both, depending on how you define the support functions for the user-defined aggregate. To create a user-defined aggregate, use the CREATE AGGREGATE statement. In this statement you name the new aggregate and specify the support functions for the aggregate. Once you create the new aggregate and its support functions, you can use the aggregate in SQL statements. For example, if you created the SUMSQ aggregate and specified that it works with the FLOAT data type, you can apply the SUMSQ aggregate to a FLOAT column named digits in the test table:
Chapter 4. Data Types and Expressions
4-117
Expression
SELECT SUMSQ(digits) FROM test
For more information on how to create user-defined aggregates, see CREATE AGGREGATE on page 2-84 and the discussion of user-defined aggregates in IBM Informix User-Defined Routines and Data Types Developer's Guide. For information on how to invoke user-defined aggregates, see User-Defined Aggregates (IDS) on page 4-125.
v On a BYTE or TEXT column You cannot use a column that is a collection data type as an argument to the following aggregate functions: v AVG v SUM v MIN v MAX Expression or column arguments to built-in aggregates (except for COUNT, MAX, MIN, and RANGE) must return numeric or INTERVAL data types, but RANGE also accepts DATE and DATETIME arguments. For SUM and AVG, you cannot use the difference between two DATE values directly as the argument to an aggregate, but you can use DATE differences as operands within arithmetic expression arguments. For example:
SELECT . . . AVG(ship_date - order_date)
AVG Function
The AVG function returns the average of all values in the specified column or expression. You can apply the AVG function only to number columns. If you use the DISTINCT keyword, the average (meaning the mean) is calculated from only the distinct values in the specified column or expression. The query in the following example finds the average price of a helmet:
4-118
Expression
SELECT AVG(unit_price) FROM stock WHERE stock_num = 110
NULLs are ignored unless every value in the column is NULL. If every column value is NULL, the AVG function returns a NULL for that column.
COUNT(*) Function
The COUNT (*) function returns the number of rows that satisfy the WHERE clause of a SELECT statement. The following example finds how many rows in the stock table have the value HRO in the manu_code column:
SELECT COUNT(*) FROM stock WHERE manu_code = HRO
The following example queries one of the System Management Interface (SMI) tables to find the number of extents in the customer table:
SELECT COUNT(*) FROM sysextents WHERE dbs_name = stores AND tabname = customer"
You can use COUNT(*) as the Projection clause in queries of this general format to obtain information from the SMI tables. For information about sysextents and other SMI tables, see the IBM Informix Administrator's Reference chapter that describes the sysmaster database. If the SELECT statement does not have a WHERE clause, the COUNT (*) function returns the total number of rows in the table. The following example finds how many rows are in the stock table:
SELECT COUNT(*) FROM stock
If the SELECT statement contains a GROUP BY clause, the COUNT (*) function reflects the number of values in each group. The following example is grouped by the first name; the rows are selected if the database server finds more than one occurrence of the same name:
SELECT fname, COUNT(*) FROM customer GROUP BY fname HAVING COUNT(*) > 1
If the value of one or more rows is NULL, the COUNT (*) function includes the NULL columns in the count unless the WHERE clause explicitly omits them.
NULLs are ignored unless every value in the specified column is NULL. If every column value is NULL, the COUNT DISTINCT function returns zero (0). The UNIQUE keyword has the same meaning as the DISTINCT keyword in COUNT functions. The UNIQUE keyword instructs the database server to return the number of unique non-NULL values in the column or expression. The following example calls the COUNT UNIQUE function, but it is equivalent to the preceding example that calls the COUNT DISTINCT function:
Chapter 4. Data Types and Expressions
4-119
Expression
SELECT COUNT (UNIQUE item_num) FROM items
The ALL keyword can precede the specified column name for clarity, but the query result is the same whether you include the ALL keyword or omit it. The following example shows how to include the ALL keyword in the COUNT column function:
SELECT COUNT (ALL item_num) FROM items
Some examples can help to show the differences among the different forms of the COUNT function. Most of the following examples query against the ship_instruct column of the orders table in the demonstration database. For information on the structure of the orders table and the data in the ship_instruct column, see the description of the demonstration database in the IBM Informix Guide to SQL: Reference. Examples of the COUNT(*) Function: In the following example, the user wants to know the total number of rows in the orders table. So the user calls the COUNT(*) function in a SELECT statement without a WHERE clause:
SELECT COUNT(*) AS total_rows FROM orders
In the following example, the user wants to know how many rows in the orders table have a NULL value in the ship_instruct column. The user calls the COUNT(*) function in a SELECT statement with a WHERE clause, and specifies the IS NULL condition in the WHERE clause:
SELECT COUNT (*) AS no_ship_instruct FROM orders WHERE ship_instruct IS NULL
4-120
Expression
The following table shows the result of this query.
no_ship_instruct 2
In the following example, the user wants to know how many rows in the orders table have the value express in the ship_instruct column. So the user calls the COUNT(*) function in the projection list and specifies the equals ( = ) relational operator in the WHERE clause.
SELECT COUNT (*) AS ship_express FROM ORDERS WHERE ship_instruct = express
Examples of the COUNT DISTINCT Function: In the next example, the user wants to know how many unique non-NULL values are in the ship_instruct column of the orders table. The user calls the COUNT DISTINCT function in the projection list of the SELECT statement:
SELECT COUNT(DISTINCT ship_instruct) AS unique_notnulls FROM orders
Examples of the COUNT column Function: In the following example the user wants to know how many non-NULL values are in the ship_instruct column of the orders table. The user invokes the COUNT(column) function in the Projection list of the SELECT statement:
SELECT COUNT(ship_instruct) AS total_notnullsFROM orders;
A similar query for non-NULL values in the ship_instruct column can include the ALL keyword in the parentheses that follow the COUNT keyword:
SELECT COUNT(ALL ship_instruct) AS all_notnulls FROM orders
The following table shows that the query result is the same whether you include or omit the ALL keyword (because ALL is the default).
all_notnulls 21
4-121
Expression
MAX Function
The MAX function returns the largest value in the specified column or expression. Using the DISTINCT keyword does not change the results. The query in the following example finds the most expensive item that is in stock but has not been ordered:
SELECT MAX(unit_price) FROM stock WHERE NOT EXISTS (SELECT * FROM items WHERE stock.stock_num = items.stock_num AND stock.manu_code = items.manu_code)
NULLs are ignored unless every value in the column is NULL. If every column value is NULL, the MAX function returns a NULL for that column.
MIN Function
The MIN function returns the lowest value in the column or expression. Using the DISTINCT keyword does not change the results. The following example finds the least expensive item in the stock table:
SELECT MIN(unit_price) FROM stock
NULL values are ignored unless every value in the column is NULL. If every column value is NULL, the MIN function returns a NULL for that column.
SUM Function
The SUM function returns the sum of all the values in the specified column or expression, as the following example shows. If you use the DISTINCT keyword, the sum is for only distinct values in the column or expression:
SELECT SUM(total_price) FROM items WHERE order_num = 1013
NULL values are ignored unless every value in the column is NULL. If every column value is NULL, the SUM function returns a NULL for that column. You cannot use the SUM function with a non-numeric column.
RANGE Function
The RANGE function computes the range of returned values. It calculates the difference between the maximum and the minimum values, as follows:
range(expr) = max(expr) - min(expr)
You can apply the RANGE function only to numeric columns. The following query finds the range of ages for a population:
SELECT RANGE(age) FROM u_pop
As with other aggregates, the RANGE function applies to the rows of a group when the query includes a GROUP BY clause, as the next example shows:
SELECT RANGE(age) FROM u_pop GROUP BY birth
Because DATE values are stored internally as integers, you can use the RANGE function on DATE columns. With a DATE column, the return value is the number of days between the earliest and latest dates in the column. NULL values are ignored unless every value in the column is NULL. If every column value is NULL, the RANGE function returns a NULL for that column. Important: All computations for the RANGE function are performed in 32-digit precision, which should be sufficient for many sets of input data. The computation, however, loses precision or returns incorrect results when all of the input data values have 16 or more digits of precision.
4-122
Expression
STDEV Function
The STDEV function computes the standard deviation of a data set, which is the square root of the VARIANCE function. You can apply the STDEV function only to numeric columns. The next query finds the standard deviation:
SELECT STDEV(age) FROM u_pop WHERE u_pop.age > 0
As with the other aggregates, the STDEV function applies to the rows of a group when the query includes a GROUP BY clause, as this example shows:
SELECT STDEV(age) FROM u_pop GROUP BY birth WHERE STDEV(age) > 0
NULL values are ignored unless every value in the specified column is NULL. If every column value is NULL, STDEV returns a NULL for that column. Important: All computations for the STDEV function are performed in 32-digit precision, which should be sufficient for many sets of input data. The computation, however, loses precision or returns incorrect results when all of the input data values have 16 or more digits of precision. You cannot use this function on columns of type DATE. Within a SELECT Statement with GROUP BY clause, STDEV returns a zero variance for a count of 1. You can omit this special case through appropriate query construction (for example, HAVING COUNT(*) > 1). Otherwise, a data set that has only a few cases might block the rest of the query result.
VARIANCE Function
The VARIANCE function returns an estimate of the population variance, as the standard deviation squared. VARIANCE calculates the following value:
(SUM(Xi2) - (SUM(Xi)2)/N)/(N - 1)
In this formula, Xi is each value in the column and N is the total number of non-NULL values in the column (unless all values are NULL, in which case the variance is logically undefined, and the VARIANCE function returns NULL). You can apply the VARIANCE function only to numeric columns. The following query estimates the variance of age values for a population:
SELECT VARIANCE(age) FROM u_pop WHERE u_pop.age > 0
As with the other aggregates, the VARIANCE function applies to the rows of a group when the query includes a GROUP BY clause, as in this example:
SELECT VARIANCE(age) FROM u_pop GROUP BY birth WHERE VARIANCE(age) > 0
As previously noted, VARIANCE ignores NULL values unless every qualified row is NULL for a specified column. If every value is NULL, then VARIANCE returns a NULL result for that column. (This typically indicates missing data, and is not necessarily a good estimate of underlying population variance.) If N, the total number of qualified non-NULL column values, equals one, then the VARIANCE function returns zero (another implausible estimate of the true population variance). To omit this special case, you can modify the query. For example, you might include a HAVING COUNT(*) > 1 clause.
4-123
Expression
Important: All calculations for the VARIANCE function are performed in 32-digit precision, which should be sufficient for many sets of input data. The computation, however, loses precision or returns incorrect results when all of the input data values have 16 or more digits of precision. Although DATE values are stored internally as an integer, you cannot use the VARIANCE function on columns of data type DATE.
You can use aggregate functions to obtain information about the num column and the testtable table. The following query uses the AVG function to obtain the average of all the non-NULL values in the num column:
SELECT AVG(num) AS average_number FROM testtable
You can use the other aggregate functions in SELECT statements that are similar to the preceding example. If you enter a series of SELECT statements that have different aggregate functions in the projection list and do not include a WHERE clause, you receive the results that the following table shows.
4-124
Expression
Function COUNT (*) COUNT (DISTINCT) COUNT (ALL num) COUNT ( num ) AVG AVG (DISTINCT) STDEV VARIANCE Results 7 3 6 6 2.66666666666667 3.00000000000000 0.74535599249993 0.55555555555556 Function MAX MAX(DISTINCT) MIN MIN(DISTINCT) RANGE SUM SUM(DISTINCT) Results 4 4 2 2 2 16 9
Notes: 1
Element aggregate column setup_expr Description Name of the user-defined aggregate to invoke Name of a column within table Set-up expression that customizes aggregate for a specific invocation
Cannot be a lone host variable. Any Expression, columns referenced in setup_expr must be in p. 4-34 the GROUP BY clause of the query The synonym and the table or view to which Database Object it points must exist Name, p. 5-17
Use the DISTINCT or UNIQUE keywords to specify that the user-defined aggregate is to be applied only to unique values in the named column or expression. Use the ALL keyword to specify that the aggregate is to be applied to all values in the named column or expression. If you omit the DISTINCT, UNIQUE, and ALL keywords, ALL is the default. For further information on the DISTINCT, UNIQUE, and ALL keywords, see Including or Excluding Duplicates in the Row Set on page 4-118.
Chapter 4. Data Types and Expressions
4-125
Expression
When you specify a setup expression, this value is passed to the INIT support function that was defined for the user-defined aggregate in the CREATE AGGREGATE statement. In the following example, you apply the user-defined aggregate named my_avg to all values of the quantity column in the items table:
SELECT my_avg(quantity) FROM items
In the following example, you apply the user-defined aggregate named my_sum to unique values of the quantity column in the items table. You also supply the value 5 as a setup expression. This value might specify that the initial value of the sum that my_avg will compute is 5.
SELECT my_sum(DISTINCT quantity, 5) FROM items
In the following example, you apply the user-defined aggregate named my_max to all values of the quantity column in the remote items table:
SELECT my_max(remote.quantity) FROM rdb@rserv:items remote
If the my_max aggregate is defined as EXECUTEANYWHERE, then the distributed query can be pushed to the remote database server, rserv, for execution. If the my_max aggregate is not defined as EXECUTEANYWHERE, then the distributed query scans the remote items table and computes the my_max aggregate on the local database server. You cannot qualify a user-defined aggregate with the name of a remote database server, as the following example shows. In this case, the database server returns an error:
SELECT rdb@rserv:my_max(remote.quantity) FROM rdb@rserv:items remote
For further information on user-defined aggregates, see CREATE AGGREGATE on page 2-84 and the discussion of user-defined aggregates in IBM Informix User-Defined Routines and Data Types Developer's Guide.
Related Information
For a discussion of expressions in the context of the SELECT statement, see the IBM Informix Guide to SQL: Tutorial. For discussions of column expressions, length functions, and the TRIM function, see the IBM Informix GLS User's Guide.
4-126
Syntax
INTERVAL Field Qualifier:
DAY (precision) TO TO TO TO TO TO TO TO TO DAY HOUR MINUTE SECOND FRACTION ( scale ) HOUR (precision) HOUR MINUTE SECOND FRACTION ( scale ) MINUTE (precision) SECOND (precision) FRACTION (precision) YEAR (precision) MONTH (precision) TO MONTH TO YEAR TO MONTH TO FRACTION ( scale ) TO MINUTE TO SECOND TO FRACTION ( scale ) TO SECOND TO FRACTION ( scale )
Description
Restrictions
Syntax
Integer number of digits in FRACTION field. Default is 3. Must be in the range Literal Number from 1 to 5 on page 4-137 Integer number of digits in the largest time unit that the INTERVAL includes. For YEAR, the default is 4. For all other time units, the default is 2. Must be in the range Literal Number from 1 to 9 on page 4-137
Usage
This segment specifies the precision and scale of an INTERVAL data type. A keyword specifying the largest time unit must be the first keyword, and a keyword specifying the smallest time unit must follow the TO keyword. These can be the same keyword. This segment resembles the syntax of a DATETIME Field Qualifier on page 4-32, but with these exceptions: v If the largest time unit keyword is YEAR or MONTH, the smallest time unit keyword cannot specify a time unit smaller than MONTH.
4-127
When you want a value to specify only one kind of time unit, the first and last qualifiers are the same. For example, an interval of whole years is qualified as YEAR TO YEAR or YEAR (5) TO YEAR, for an interval of up to 99,999 years. The following examples show several forms of INTERVAL field qualifiers:
YEAR(5) TO MONTH DAY (5) TO FRACTION(2) DAY TO DAY FRACTION TO FRACTION (4)
Related Information
For information about how to specify INTERVAL field qualifiers and how to use INTERVAL data in arithmetic and relational operations, see the discussion of the INTERVAL data type in the IBM Informix Guide to SQL: Reference.
4-128
Literal Collection
Use the Literal Collection segment to specify values for a collection data type. For the syntax of expressions that return values of individual elements within a collection, see Collection Constructors on page 4-65.
Syntax
Literal Collection:
" SET MULTISET LIST SET MULTISET LIST { Literal Data }."
'
Literal Data
}.'
Literal Data:
, (1) Element Literal Value (2) Nested Quotation Marks Literal Collection Nested Quotation Marks (2)
Notes: 1 2 See Element Literal Value on page 4-130 See Nested Quotation Marks on page 4-130
Usage
You can specify literal collection values for SET, MULTISET, or LIST data types. To specify a single literal-collection value, specify the collection type and the literal values. The following SQL statement inserts four integer values into a column called set_col that was declared as SET(INT NOT NULL):
INSERT INTO table1 (set_col) VALUES (SET{6, 9, 9, 4});
Specify an empty collection with an empty pair of braces ( { } ) symbols. This example inserts an empty list into a column list_col that was declared as LIST(INT NOT NULL):
INSERT INTO table2 (list_col) VALUES (LIST{})
A pair of single ( ) or double ( ) quotes must delimit the collection. In databases where delimited identifiers are enabled, however, double quotes are not valid, except to delimit SQL identifiers. If you are passing a literal collection as an argument to an SPL routine, make sure that there is a blank space between the parentheses that surround the arguments and the quotation marks that indicate the beginning and end of the literal collection. If you specify a collection as a literal value in a row-string literal, you can omit the quotation marks around the collection itself. Only the outermost quotation marks
Chapter 4. Data Types and Expressions
4-129
Literal Collection
that delimit the row-string literal are necessary. No quotation marks need surround the nested collection type. For an example, see Literals for Nested Rows on page 4-141.
Literal INTERVAL on page 4-135 Quoted String on page 4-142. The string literal must be recognized by the input support function for the associated opaque type. Literal Row on page 4-139. When the collection element type is a named ROW type, you do not need to cast the inserted values to the named ROW type.
Row Type
Important: You cannot specify the simple-large-object data types (BYTE and TEXT) as the element type for a collection. Quoted strings must be specified with a different type of quotation mark than the quotation marks that encompass the collection, so that the database server can parse the quoted strings. Thus, if you use double ( ) quotation marks to specify the collection, use single ( ) quotation marks to specify individual, quoted-string elements. (In databases where delimited identifiers are enabled, however, double quotes are not valid, except to delimit SQL identifiers.)
4-130
Literal Collection
The following examples illustrate the case for two levels of nested collection literals, using double ( ) quotation marks. Here table tab5 is a single-column table whose only column, set_col, is a nested collection type. The following statement creates the tab5 table:
CREATE TABLE tab5 (set_col SET(SET(INT NOT NULL) NOT NULL));
For each literal value, the opening quotation mark and the closing quotation mark must match. Thus, if you open a literal with two double quotes, you must close that literal with two double quotes (""a literal value""). To specify nested quotation marks within an SQL statement in an ESQL/C program, use the C escape character for every double quote inside a single-quote string. Otherwise, the ESQL/C preprocessor cannot correctly interpret the literal collection value. For example, the preceding INSERT statement on the tab5 table would appear in an ESQL/C program as follows:
EXEC SQL insert into tab5 values (set{\"set{34, 56, 23, 33}\"});
For more information, see the chapter on complex data types in the IBM Informix ESQL/C Programmer's Manual. If the collection is a nested collection, you must include the collection-constructor syntax for each level of collection type. Suppose you define the following column:
nest_col SET(MULTISET (INT NOT NULL) NOT NULL)
The following statement inserts three elements into the nest_col column:
INSERT INTO tabx (nest_col) VALUES ("SET{MULTISET{1, 2, 3}}")
Related Information
To learn how to use quotation marks in INSERT statements, see Nested Quotation Marks on page 4-130.
4-131
Literal DATETIME
The Literal DATETIME segment specifies a DATETIME value. Use this segment when you see a reference to a literal DATETIME in a syntax diagram.
Syntax
Literal DATETIME:
(1) DATETIME ( Numeric Date and Time ) DATETIME Field Qualifier
4-132
Literal DATETIME
Element dd fffff hh mi mo space ss yyyy Description Day of month, expressed in digits Fraction of a second, in digits Hour of day, expressed in digits Minute of hour, expressed in digits Month of year, expressed in digits Blank space (ASCII 32) Second of minute, in digits Year, expressed in digits Restrictions 1 dd 28, 29, 30, or 31 0 fffff 9999 0 hh 23 0 mi 59 1 mo 11 Exactly 1 blank character 0 ss 59 You can specify up to 4 digits Syntax Literal Number on page 4-137 Literal Number on page 4-137 Literal Number on page 4-137 Literal Number on page 4-137 Literal Number on page 4-137 Literal blank space Literal Number on page 4-137 Literal Number on page 4-137
Usage
You must specify both a numeric date and a DATETIME field qualifier for this date in the Literal DATETIME segment. The DATETIME field qualifier must correspond to the numeric date you specify. For example, if you specify a numeric date that includes a year as the largest unit and a minute as the smallest unit, you must also specify YEAR TO MINUTE as the DATETIME field qualifier. If you specify two digits for the year, the database server uses the setting of the DBCENTURY environment variable to expand the abbreviated year value to four digits. If the DBCENTURY is not set, the first two digits of the current year are used to expand the abbreviated year value. The following examples show literal DATETIME values:
DATETIME (97-3-6) YEAR TO DAY DATETIME (09:55:30.825) HOUR TO FRACTION DATETIME (97-5) YEAR TO MONTH
The following example shows a literal DATETIME value used with the EXTEND function:
EXTEND (DATETIME (1997-8-1) YEAR TO DAY, YEAR TO MINUTE) - INTERVAL (720) MINUTE (3) TO MINUTE
Here the DATETIME value that CURRENT returns is implicitly cast to DATE. You can also cast DATETIME to DATE explicitly:
LET my_date = CURRENT::DATE
4-133
Literal DATETIME
Both of these LET statements assign the year, month, and day information from the DATETIME value to the local SPL variable my_date of type DATE. Similarly, you can explicitly cast a string that has the format of the Numeric Date and Time segment, as defined in the Literal DATETIME syntax diagram, to a DATETIME data type, as in the following example:
LET my_dt = (2005-02-22 05:58:44.000)::DATETIME
There is neither an implicit nor an explicit built-in cast, however, for directly converting a character string that has the Numeric Date and Time format to a DATE value. Both of the following statements, for example, fail with error -1218:
LET my_date = (2005-02-22 05:58:44.000); LET my_date = (2005-02-22 05:58:44.000)::DATE;
To convert a character string that specifies a valid numeric date and time value to a DATE data type, you must first cast the string to DATETIME, and then cast the resulting DATETIME value to DATE, as in this example:
LET my_date = (2005-02-22 05:58:44.000)::DATETIME::DATE;
A direct string-to-DATE cast can succeed only if the string specifies a valid DATE value.
Related Information
For discussions of the DATETIME data type and the DBCENTURY environment variable, see the IBM Informix Guide to SQL: Reference. For a discussion of how to use the GL_DATETIME environment variable to customize the display format of DATETIME values in non-default locales, see the IBM Informix GLS User's Guide.
4-134
Literal INTERVAL
The Literal INTERVAL segment specifies a literal INTERVAL value. Use this whenever you see a reference to a literal INTERVAL in a syntax diagram.
Syntax
Literal INTERVAL:
(1) INTERVAL ( Numeric Time Span ) INTERVAL Field Qualifier
+ -
yyyy mo mo
Notes: 1
Element dd fffff hh mi mo space ss yyyy Description Number of days Fractions of a second Number of hours Number of minutes Number of months Blank space (ASCII 32) Number of seconds Number of years
4-135
Literal INTERVAL
Usage
Unlike DATETIME literals, INTERVAL literals can include the unary plus ( + ) or unary minus ( - ) sign. If you specify no sign, the default is plus. The precision of the first time unit can be specified by the INTERVAL qualifier. Except for FRACTION, which can have no more than 5 digits of precision, the first time unit can have up to 9 digits of precision, if you specified a nondefault precision in the declaration of the INTERVAL column or variable. The following examples show literal INTERVAL values:
INTERVAL INTERVAL INTERVAL INTERVAL (3-6) YEAR TO MONTH (09:55:30.825) HOUR TO FRACTION (40 5) DAY TO HOUR (299995.2567) SECOND(6) TO FRACTION(4)
Only the last of these examples has nondefault precision. For the syntax of declaring the precision of INTERVAL data types and the default values for each time unit, refer to INTERVAL Field Qualifier on page 4-127.
Related Information
For information on how to use INTERVAL data in arithmetic and relational operations, see the discussion of the INTERVAL data type in the IBM Informix Guide to SQL: Reference.
4-136
Literal Number
A literal number is the base-10 representation of a real number as an integer, as a fixed-point decimal number, or in exponential notation. Use the Literal Number segment whenever you see a reference to a literal number in a syntax diagram.
Syntax
Literal Number:
+ -
digit . e E + digit
digit
digit
Element digit
Usage
You cannot include comma ( , ) or blank (ASCII 32) character. The unary plus ( + ) or minus ( - ) sign can precede a literal number, mantissa, or exponent. You cannot include non-ASCII digits in literal numbers, such as the Hindi numbers that some nondefault locales support.
Integer Literals
In many contexts, a literal number is restricted to an integer literal. An integer has no fractional part and cannot include a decimal point. Built-in data types of SQL that can be exactly represented as literal integers include INT8, INT, SMALLINT, SERIAL, SERIAL8, and DECIMAL(p, 0). If you use the representation of a number in a base other than 10 (such as a binary, octal, or hexadecimal) in any context where a literal integer is valid, the database server will attempt to interpret the value as a base-10 literal integer. For most data values, the result will be incorrect. The following examples show some valid literal integers:
10 -27 +25567
Thousands separators (such as comma symbols) are not valid in literal integers, nor in any other literal number.
4-137
Literal Number
The digits to the right of the decimal point in these examples are the fractional portions of the numbers.
The E in the previous examples is the symbol for exponential notation. The digit that follows E is the value of the exponent. For example, the number 3E5 (or 3E+5) means 3 multiplied by 10 to the fifth power, and the number 3E-5 means 3 multiplied by the reciprocal of 10 to the fifth power.
Related Information
For discussions of numeric data types, such as DECIMAL, FLOAT, INTEGER, and MONEY, see the IBM Informix Guide to SQL: Reference.
4-138
Literal Row
The Literal Row segment specifies the syntax for literal values of named and unnamed ROW data types. Only Dynamic Server supports this syntax. For expressions that evaluate to field values within a ROW data type, see ROW Constructors on page 4-64.
Syntax
Literal Row:
, ROW ( , ROW ( Field Literal Value ) Field Literal Value )
Notes: 1 2 3 4 5
Element literal_opaque_type
See Quoted String on page 4-142 See Literal Number on page 4-137 See Literal DATETIME on page 4-132 See Literal INTERVAL on page 4-135 See Literal Collection on page 4-129
Restrictions Must be a literal that is recognized by the input support function for the associated opaque data type Must be either t (= TRUE) or f (= FALSE) specified as a quoted string Syntax Defined by the developer of the opaque data type Quoted String on page 4-142
Description Literal representation for an opaque data type Literal representation of a BOOLEAN value
literal_BOOLEAN
4-139
Literal Row
Usage
You can specify literal values for named ROW and unnamed ROW data types. A ROW constructor introduces a literal ROW value, which can optionally be enclosed between quotation marks. The format of the value for each field of the ROW type must be compatible with the data type of the corresponding field. Important: You cannot specify simple-large-object data types (BYTE or TEXT) as the field type for a row. Fields of a row can be literal values for the data types in the following table.
For a Field of Type BOOLEAN CHAR, VARCHAR, LVARCHAR, NCHAR, NVARCHAR, CHARACTER VARYING, DATE DATETIME DECIMAL, MONEY, FLOAT, INTEGER, INT8, SMALLFLOAT, SMALLINT INTERVAL Opaque data types Literal Value Syntax t or f, representing TRUE or FALSE Quoted String on page 4-142
Literal DATETIME on page 4-132 Literal Number on page 4-137 Literal INTERVAL on page 4-135 Quoted String on page 4-142 The string must be a literal that is recognized by the input support function for the associated opaque type.
Literal Collection on page 4-129 For information on literal collection values as variable or column values, see Nested Quotation Marks on page 4-130. For information on literal collection values for a ROW type, see Literals for Nested Rows on page 4-141.
For information on ROW type values, see Literals for Nested Rows on page 4-141.
The following INSERT statement inserts values into the rect column of the rectangles table:
INSERT INTO rectangles (rect) VALUES ("ROW(7, 3, 6.0, 2.0)")
4-140
Literal Row
The following INSERT statement inserts values into the address column of the employee table:
INSERT INTO employee (address) VALUES ( "ROW(103 Baker St, Tracy,CA, 94060)"::address_t)
Similarly, if the row-string literal contains a nested collection, only the outermost literal row can be enclosed between quotation marks. Do not put quotation marks around an inner, nested collection type.
Related Information
Related statements: CREATE ROW TYPE, INSERT, UPDATE, and SELECT For information on ROW constructors, see the Expression segment. See also the Collection Literal segment.
4-141
Quoted String
A quoted string is a string literal between quotation marks. Use this segment whenever you see a reference to a quoted string in a syntax diagram.
Syntax
Quoted String:
' '
character ""
Notes: 1
Element character Description
Informix extension
Restrictions Syntax Literal value from the keyboard
Code set element within Cannot enclose between double quotes if the quoted string DELIMIDENT environment variable is set
Usage
Use quoted strings to specify string literals in data-manipulation statements and other SQL statements. For example, you can use a quoted string in an INSERT statement to insert a value into a column of a character data type.
4-142
Quoted String
v When you insert a value that is a quoted string, you must observe a number of restrictions. For further information, see Inserting Values as Quoted Strings on page 4-145. 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
DELIMIDENT is also supported on client systems, where it can be set to y, to n, or to no setting. v y specifies that client applications must use single quote ( ) symbols to delimit literal strings, and must use double quote ( ) symbols only around delimited SQL identifiers. Delimited identifiers can support a larger character set than is valid for undelimited identifiers. Letters within delimited strings or delimited identifiers are case-sensitive. v n specifies that client applications can use double quote ( ) or single quote ( ) symbols to delimit character strings, but not to delimit SQL identifiers. If the database server encounters a string delimited by double or single quote symbols in a context where an SQL identifier is required, it issues an error. An owner name, however, that qualifies an SQL identifier can be delimited by single quote ( ) symbols. You must use a pair of the same quote symbols to delimit a character string. v Specifying DELIMIDENT with no value on the client system requires client applications to use the DELIMIDENT setting that is the default for their application programming interface (API). Client APIs of Dynamic Server use the following default DELIMIDENTsettings: v For OLE DB and .NET, the default DELIMIDENT setting is y v For ESQL/C, JDBC, and ODBC, the default DELIMIDENT setting is n v APIs that have ESQL/C as an underlying layer, such as Informix 4GL, the DataBlade API (LIBDMI), and the C++ API, behave as ESQL/C, and use n as the default if no value for DELIMIDENT is specified on the client system. Even if DELIMIDENT is set, you can use single quote ( ) symbols to delimit authorization identifiers as the owner name component of a database object name, as in the following example:
RENAME COLUMN Owner.table2.collum3 TO column3
The general rule, however, is that when DELIMIDENT is set, the SQL parser interprets strings delimited by single quotes as string literals, and interprets character strings delimited by double quotes ( ) as SQL identifiers.
4-143
Quoted String
v To enable newline characters in quoted strings for the current session, execute the built-in function IFX_ALLOW_NEWLINE. This enables newline characters in quoted strings for the current session:
EXECUTE PROCEDURE IFX_ALLOW_NEWLINE(T)
If newline characters in quoted strings are not enabled for a session, the following statement is invalid and returns an error:
SELECT The quick brown fox jumped over the old gray fence FROM customer WHERE customer_num = 101
If you enable newline characters in quoted strings for the session, however, the statement in the preceding example is valid and executes successfully. For more information on the IFX_ALLOW_NEWLINE function, see IFX_ALLOW_NEWLINE Function on page 4-111. For more information on the ALLOW_NEWLINE parameter in the ONCONFIG file, see your IBM Informix Administrator's Reference.
A string delimited by double quotes can include a double quote character by preceding it with another double quote, as the following string shows:
"Enter ""y"" to select this row"
When the DELIMIDENT environment variable is set, double quotes can only delimit SQL identifiers, not strings. For more information on delimited identifiers, see Delimited Identifiers on page 5-23.
The format of the value in the quoted string must exactly match the format specified by the INTERVAL or DATETIME qualifiers of the column. For the first INSERT in the preceding example, the call_dtime column must be defined with the qualifiers YEAR TO SECOND for the INSERT statement to be valid.
4-144
Quoted String
Related Information
For a discussion of the DELIMIDENT environment variable, see the IBM Informix Guide to SQL: Reference. For a discussion of the GLS aspects of quoted strings, see the IBM Informix GLS User's Guide.
4-145
Relational Operator
A relational operator compares two expressions quantitatively. Use the Relational Operator segment whenever you see a reference to a relational operator in a syntax diagram.
Syntax
Relational Operator:
< <= > = == >= <> (1) !=
Usage
The relational operators of SQL have the following meanings. Relational Operator < <= > = or == >= <> or != Meaning Less than Less than or equal to Greater than Equal to Greater than or equal to Not equal to
Usage
For number expressions, greater than means to the right on the real line. For DATE and DATETIME expressions, greater than means later in time. For INTERVAL expressions, greater than means a longer span of time. For CHAR, VARCHAR, and LVARCHAR expressions, greater than means after in code-set order. (For NCHAR and NVARCHAR expressions, greater than means after in the localized collation order, if one exists; otherwise, it means in code-set order.) Locale-based collation order, if defined for the locale, is used for NCHAR and NVARCHAR expressions. So for NCHAR and NVARCHAR expressions, greater than means after in the locale-based collation order. For more information on locale-based collation order and the NCHAR and NVARCHAR data types, see the IBM Informix GLS User's Guide.
4-146
Relational Operator
Connecting two expressions with a relational operator is equivalent to invoking the operator function on the expressions. For example, the next two statements both select orders with a shipping charge of $18.00 or more. The >= operator in the first statement implicitly invokes the greaterthanorequal( ) operator function:
SELECT order_num FROM orders WHERE ship_charge >= 18.00; SELECT order_num FROM orders WHERE greaterthanorequal(ship_charge, 18.00);
The database server provides the operator functions associated with the relational operators for all built-in data types. When you develop a user-defined data type, you must define the operator functions for that type for users to be able to use the relational operator on the type. If you define less_than( ), greater_than( ), and the other operator functions for a user-defined type, then you should also define compare( ). Similarly, if you define compare( ), then you should also define less_than( ), greater_than( ), and the other operator functions. All of these functions must be defined in a consistent manner, to avoid the possibility of incorrect query results when UDT values are compared in the WHERE clause of a SELECT.
4-147
Relational Operator
This table lists the ASCII code set. The Num columns show ASCII code point numbers, and the Char columns display corresponding ASCII characters. In the default locale, ASCII characters are sorted according to their code-set order. Thus, lowercase letters follow uppercase letters, and both follow digits. In this table, ASCII 32 is the blank character, and the caret symbol ( ^ ) stands for the CTRL key. For example, ^X means CONTROL-X.
Num 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Char ^@ ^A ^B ^C ^D ^E ^F ^G ^H ^I ^J ^K ^L ^M ^N ^O ^P ^Q ^R ^S Num 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 ! # $ % & Char ^T ^U ^V ^W ^X ^Y ^Z esc ^\ ^] ^^ ^_ Num 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 Char ( ) * + , . / 0 1 2 3 4 5 6 7 8 9 : ; Num 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 Char < = > ? @ A B C D E F G H I J K L M N O Num 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 Char P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c Num 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 Char d e f g h i j k l m n o p q r s t u v w Num 120 121 122 123 124 125 126 127 Char x y z { | } ~ del
4-148
Relational Operator
Related Information
For a discussion of relational operators in the SELECT statement, see the IBM Informix Guide to SQL: Tutorial. For a discussion of the GLS aspects of relational operators, see the IBM Informix GLS User's Guide.
4-149
4-150
In This Chapter
Syntax segments are language elements, such as database object names or optimizer directives, that appear as a subdiagram reference in the syntax diagrams of some SQL or SPL statements. Most segments that can occur in only one statement are described in Chapter 2 or Chapter 3 within the description of the statement. For the sake of clarity, ease of use, and comprehensive treatment, however, most segments that can occur in various SQL or SPL statements, and that are not data types nor expressions, are discussed separately here. The previous chapter described the syntax segments that specify data types and expressions. This chapter describes additional syntax segments that are neither data types, expressions, nor complete SQL statements or SPL statements. These segments are referenced in various syntax diagrams that appear in Chapter 2 and in other chapters of this manual.
5-1
Arguments
Arguments
Use the Argument segment to pass a specific value as input to a routine. Use this segment wherever you see a reference to an argument in a syntax diagram.
Syntax
Argument:
(1) parameter = Subset of Expression NULL ( singleton_select )
Notes: 1
Element parameter singleton _select Description A parameter whose value you specify Embedded query that returns a single value
Usage
The CREATE PROCEDURE or CREATE FUNCTION statement can define a parameter list for a UDR. If the parameter list is not empty, you must enter arguments when you invoke the UDR. An argument is a specific value whose data type is compatible with that of the corresponding UDR parameter. When you execute a UDR, you can enter arguments in either of two ways: v With a parameter name (in the form parameter name = expression), even if the arguments are not in the same order as the parameters v By position, with no parameter name, where each expression is in the same order as the parameter to which the argument corresponds. (This is sometimes called ordinal format.) You cannot mix these two ways of specifying arguments within a single invocation of a routine. If you specify a parameter name for one argument, for example, you must use parameter names for all the arguments. In the following example, both statements are valid for a user-defined procedure that expects three character arguments, t, d, and n:
EXECUTE PROCEDURE add_col (t =customer, d =integer, n =newint); EXECUTE PROCEDURE add_col (customer,newint,integer) ;
5-2
Arguments
User-defined routines can be overloaded, if different routines have the same identifier, but have different numbers of declared parameters. For more information about overloading, see Routine Overloading and Naming UDRs with a Routine Signature (IDS) on page 5-19. If you attempt to execute a UDR with more arguments than the UDR expects, you receive an error. If you invoke a UDR with fewer arguments than the UDR expects, the omitted arguments are said to be missing. The database server initializes missing arguments to their corresponding default values. This initialization occurs before the first executable statement in the body of the UDR. If missing arguments have no default values, Dynamic Server issues an error. Extended Parallel Server can invoke a routine with missing arguments that have no default values, but the routine might fail if a missing argument causes another error (for example, an undefined value for a variable). Named parameters cannot be used to invoke UDRs that overload data types in their routine signatures. Named parameters are valid in resolving non-unique routine names only if the signatures have different numbers of parameters:
func( x::integer, y ); -- VALID if only these 2 routines func( x::integer, y, z ); -- have the same func identifier func( x::integer, y ); func( x::float, y ; -- NOT VALID if both routines have -- same identifier and 2 parameters
For both ordinal and named parameters, the routine with the fewest parameters is executed if two or more UDR signatures have multiple numbers of defaults:
func( x, y default 1 ) func( x, y default 1, z default 2 )
If two registered UDRs that are both called func have the signatures shown above, then the statement EXECUTE func(100) invokes func(100,1). You cannot supply a subset of default values using named parameters unless they are in the positional order of the routine signature. That is, you cannot skip a few arguments and rely on the database server to supply their default values. For example, given the signature:
func( x, y default 1, z default 2 )
5-3
Arguments
Related Information
Related Statements: ALTER FUNCTION, ALTER PROCEDURE, ALTER ROUTINE, CALL, CREATE FUNCTION, CREATE FUNCTION FROM, CREATE PROCEDURE FROM, EXECUTE FUNCTION on page 2-329, EXECUTE PROCEDURE. Related Segments: Routine Parameter List on page 5-61
5-4
Collection-Derived Table
A collection-derived table is a virtual table in which the values in the rows of the table are equivalent to elements of a collection. Use this segment where you see a reference to Collection-Derived Table in a syntax diagram. Only Dynamic Server supports this syntax, which is an extension to the ANSI/ISO standard for SQL.
Syntax
Collection-Derived Table:
(1) TABLE (
collection_expr) (1) AS alias alias (2) (3) row_var (3) collection_var ) ( derived_column ) ,
Notes: 1 2 3
Element alias Description Temporary name for a collection-derived table whose scope is a SELECT statement. The default is implementation dependent.
collection_expr
Any expression that evaluates to the See Restrictions with the elements of a single collection Collection-Expression Format on page 5-6. Name of a typed or untyped collection variable, or an ESQL/C row variable that holds the collection-derived table Temporary name for a derived column in a table Must have been declared in an ESQL/C program or (for collection_var) in an SPL routine If the underlying collection is not of a ROW data type, you can specify only one derived-column name
Expression on page 4-34 See the IBM Informix ESQL/C Programmer's Manual or DEFINE on page 3-7. Identifier on page 5-22
collection_var, row_var
derived _column
Usage
A collection-derived table can appear where a table name is valid in the UPDATE statement, in the FROM clause of the SELECT or DELETE statement, or in the INTO clause of an INSERT statement. Use the collection-derived-table segment to accomplish these tasks: v Access the elements of a collection as you would the rows of a table.
Chapter 5. Other Syntax Segments
5-5
Collection-Derived Table
v Specify a collection variable to access, instead of a table name. v Specify an ESQL/C row variable to access, instead of a table name. The TABLE keyword converts a collection into a virtual table. You can use the collection expression format to query a collection column, or you can use the collection variable or row variable format to manipulate the data in a collection column.
The following example uses a join query to create a virtual table of no more than twenty rows (beginning with the 41st row), ordered by value in the salary column of the collection-derived table:
SELECT emp_id, emp_name, emp_salary FROM TABLE(MULTISET(SELECT SKIP 40 LIMIT 20 id, name, salary FROM e1, e2 WHERE e1.id = e2.id ORDER BY salary )) AS etab(emp_id, emp_name, emp_salary);
5-6
Collection-Derived Table
For example, the following statement returns an error because the collection-derived table, TABLE (parents.children), refers to the table parents, which is also referenced in the FROM clause:
SELECT COUNT(*) FROM parents, TABLE(parents.children) c_table WHERE parents.id = 1001
To counter this restriction, you might write a query that contains a subquery in the Projection clause:
SELECT (SELECT COUNT(*) FROM TABLE(parents.children) c_table) FROM parents WHERE parents.id = 1001
Additional Restrictions That Apply to ESQL/C: In addition to the previously described restrictions, the following restrictions also apply when you use the collection-expression format with ESQL/C: v You cannot specify an untyped COLLECTION as the host-variable data type. v You cannot use the format TABLE(?). The data type of the underlying collection variable must be determined statically. To counter this restriction, you can explicitly cast the variable to a typed collection data type (SET, MULTISET, or LIST) that the database server recognizes. For example,
TABLE(CAST(? AS type))
v You cannot use the format TABLE(:hostvar). To counter this restriction, you must explicitly cast the variable to a typed collection data type (SET, MULTISET, or LIST) that the database server recognizes. For example,
TABLE(CAST(:hostvar AS type))
5-7
Collection-Derived Table
CREATE ROW TYPE person (name CHAR(255), id INT); CREATE TABLE parents ( name CHAR(255), id INT, children LIST (person NOT NULL) ); CREATE TABLE parents2 ( name CHAR(255), id INT, children_ids LIST (INT NOT NULL) ); Explicit DerivedColumn List No
Code Example SELECT (SELECT c_table.name FROM TABLE(parents.children) c_table WHERE c_table.id = 1002) FROM parents WHERE parents.id = 1001 In this example, the ROW type of c_table is parents.
Yes
Yes
Unnamed ROW type of SELECT (SELECT c_table.c_name FROM which the column type is TABLE(parents.children) c_table(c._name, c_id) Type and the column name is WHERE c_table.c_id = 1002) FROM parents WHERE the name in the parents.id = 1001 derived-column list In this example, the ROW type of c_table is ROW(c_name CHAR(255), c_id INT). Unnamed ROW that contains one column of Type that is assigned an implementation-dependent name Unnamed ROW type that contains one column of Type whose name is in the derived-column list In the following example, if you do not specify c_id, the database server assigns a name to the derived column. In this case, the ROW type of c_table is ROW(server_defined_name INT). SELECT(SELECT c_table.c_id FROM TABLE(parents2.child_ids) c_table (c_id) WHERE c_table.c_id = 1002) FROM parents WHERE parents.id = 1001 Here the ROW type of c_table is ROW(c_id INT).
No
No
No
Yes
The following program fragment creates a collection-derived table using an SPL function that returns a single value:
CREATE TABLE wanted(person_id int); CREATE FUNCTION wanted_person_count (person_set SET(person NOT NULL)) RETURNS INT; RETURN( SELECT COUNT (*) FROM TABLE (person_set) c_table, wanted WHERE c_tabel.id = wanted.person_id); END FUNCTION;
The next program fragment shows the more general case of creating a collection-derived table using an SPL function that returns multiple values:
-- Table of categories and child categories, -- allowing any number of levels of subcategories CREATE TABLE CategoryChild ( categoryId INTEGER, childCategoryId SMALLINT
5-8
Collection-Derived Table
); INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INTO INTO INTO INTO INTO INTO INTO INTO INTO CategoryChild CategoryChild CategoryChild CategoryChild CategoryChild CategoryChild CategoryChild CategoryChild CategoryChild VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES (1, (1, (1, (2, (2, (5, (7, (7, (4, 2); 3); 4); 5); 6); 7); 8); 9); 10);
-- "R" == ROW type CREATE ROW TYPE categoryLevelR ( categoryId INTEGER, level SMALLINT ); -- DROP FUNCTION categoryDescendants ( -INTEGER, SMALLINT ); CREATE FUNCTION categoryDescendants ( pCategoryId INTEGER, pLevel SMALLINT DEFAULT 0 ) RETURNS MULTISET (categoryLevelR NOT NULL) -- "p" -- "l" DEFINE DEFINE DEFINE == Prefix for Parameter names == Prefix for Local variable names lCategoryId LIKE CategoryChild.categoryId; lRetSet MULTISET (categoryLevelR NOT NULL); lCatRow categoryLevelR;
-- TRACE ON; -- Must initialize collection before inserting rows LET lRetSet = MULTISET{} :: MULTISET (categoryLevelR NOT NULL); FOREACH SELECT childCategoryId INTO lCategoryId FROM CategoryChild WHERE categoryId = pCategoryId; INSERT INTO TABLE (lRetSet) VALUES (ROW (lCategoryId, pLevel+1)::categoryLevelR); -------INSERT INTO TABLE (lRetSet); EXECUTE FUNCTION categoryDescendantsR ( lCategoryId, pLevel+1 ); Need to iterate over results and insert into SET. See the SQL Tutorial, pg. 10-52: "Tip: You can only insert one value at a time into a simple collection." FOREACH EXECUTE FUNCTION categoryDescendantsR ( lCategoryId, pLevel+1 ) INTO lCatRow; INSERT INTO TABLE (lRetSet) VALUES (lCatRow); END FOREACH; END FOREACH; RETURN lRetSet; END FUNCTION ; -- "R" == recursive -- DROP FUNCTION categoryDescendantsR (INTEGER, SMALLINT); CREATE FUNCTION categoryDescendantsR ( pCategoryId INTEGER, pLevel SMALLINT DEFAULT 0 ) RETURNS categoryLevelR; DEFINE lCategoryId LIKE CategoryChild.categoryId; DEFINE lCatRow categoryLevelR;
5-9
Collection-Derived Table
FOREACH SELECT childCategoryId INTO lCategoryId FROM CategoryChild WHERE categoryId = pCategoryId RETURN ROW (lCategoryId, pLevel+1)::categoryLevelR WITH RESUME; FOREACH EXECUTE FUNCTION categoryDescendantsR ( lCategoryId, pLevel+1 ) INTO lCatRow RETURN lCatRow WITH RESUME; END FOREACH; END FOREACH; END FUNCTION; -- Test the functions: SELECT lev, col FROM TABLE (( categoryDescendants (1, 0) )) AS CD (col, lev);
5-10
Collection-Derived Table
2. In ESQL/C, allocate memory for the collection; see ALLOCATE COLLECTION on page 2-4. 3. Optionally, use a SELECT statement to select a COLLECTION column into the collection variable. If the variable is an untyped COLLECTION variable, you must perform a SELECT from the COLLECTION column before you use the variable in the collection-derived table segment. The SELECT statement allows the database server to obtain the collection data type. 4. Use the appropriate data manipulation statement with the collection-derived table segment to add, delete, or update elements in the collection variable. To insert more than one element or to update or delete a specific element of a collection, you must use a cursor for the collection variable. v For more information on how to use an update cursor with ESQL/C, see DECLARE on page 2-260. v For more information on how to use an update cursor with SPL, see FOREACH on page 3-20. 5. After the collection variable contains the correct elements, use an INSERT or UPDATE statement on the table or view that holds the actual collection column to save the changes that the collection variable holds. v With UPDATE, specify the collection variable in the SET clause. v With INSERT, specify the collection variable in the VALUES clause. The collection variable stores the elements of the collection. It has no intrinsic connection, however, with a database column. Once the collection variable contains the correct elements, you must then save the variable into the actual collection column of the table with either an INSERT or an UPDATE statement. Example of Deleting from a Collection in ESQL/C: Suppose that the set_col column of a row in the table1 table is defined as a SET and for one row contains the values {1,8,4,5,2}. The following ESQL/C code fragment uses an update cursor and a DELETE statement with a WHERE CURRENT OF clause to delete the element whose value is 4:
EXEC SQL BEGIN DECLARE SECTION; client collection set(smallint not null) a_set; int an_int; EXEC SQL END DECLARE SECTION; ... EXEC SQL allocate collection :a_set; EXEC SQL select set_col into :a_set from table1 where int_col = 6; EXEC SQL declare set_curs cursor for select * from table(:a_set) for update; EXEC SQL open set_curs; while (i<coll_size) { EXEC SQL fetch set_curs into :an_int; if (an_int = 4) { EXEC SQL delete from table(:a_set) where current of set_curs; break; } i++; }
5-11
Collection-Derived Table
EXEC SQL where EXEC SQL EXEC SQL EXEC SQL update table1 set set_col = :a_set int_col = 6; deallocate collection :a_set; close set_curs; free set_curs;
After the DELETE statement executes, this collection variable contains the elements {1,8,5,2}. The UPDATE statement at the end of this code fragment saves the modified collection into the set_col column. Without this UPDATE statement, element 4 of the collection column is not deleted. Example of Deleting from a Collection: Suppose that the set_col column of a row in the table1 table is defined as a SET and one row contains the values {1,8,4,5,2}. The following SPL code fragment uses a FOREACH loop and a DELETE statement with a WHERE CURRENT OF clause to delete the element whose value is 4:
CREATE_PROCEDURE test6() DEFINE a SMALLINT; DEFINE b SET(SMALLINT NOT NULL); SELECT set_col INTO b FROM table1 WHERE id = 6; -- Select the set in one row from the table -- into a collection variable FOREACH cursor1 FOR SELECT * INTO a FROM TABLE(b); -- Select each element one at a time from -- the collection derived table b into a IF a = 4 THEN DELETE FROM TABLE(b) WHERE CURRENT OF cursor1; -- Delete the element if it has the value 4 EXIT FOREACH; END IF; END FOREACH; UPDATE table1 SET set_col = b WHERE id = 6; -- Update the base table with the new collection END PROCEDURE;
This SPL routine declares two SET variables, a and b, each to hold a set of SMALLINT values. The first SELECT statement copies a SET column from one row of table1 into variable b. The routine then declares a cursor called cursor1 that copies one element at a time from b into SET variable a. When the cursor is positioned on the element whose value is 4, the DELETE statement removes that element from SET variable b. Finally, the UPDATE statement replaces the row of table1 with the new collection that is stored in variable b. For information on how to use collection variables in an SPL routine, see the IBM Informix Guide to SQL: Tutorial. Example of Updating a Collection: Suppose that the set_col column of a table called table1 is defined as a SET and that it contains the values {1,8,4,5,2}. The following ESQL/C program changes the element whose value is 4 to a value of 10:
main { EXEC SQL BEGIN DECLARE SECTION; int a; collection b;
5-12
Collection-Derived Table
EXEC SQL END DECLARE SECTION; EXEC SQL allocate collection :b; EXEC SQL select set_col into :b from table1 where int_col = 6; EXEC SQL declare set_curs cursor for select * from table(:b) for update; EXEC SQL open set_curs; while (SQLCODE != SQLNOTFOUND) { EXEC SQL fetch set_curs into :a; if (a = 4) { EXEC SQL update table(:b)(x) set x = 10 where current of set_curs; break; } } EXEC SQL update table1 set set_col = :b where int_col = 6; EXEC SQL deallocate collection :b; EXEC SQL close set_curs; EXEC SQL free set_curs; }
After you execute this ESQL/C program, the set_col column in table1 contains the values {1,8,10,5,2}. This ESQL/C program defines two collection variables, a and b, and selects a SET from table1 into b. The WHERE clause ensures that only one row is returned. Then the program defines a collection cursor, which selects elements one at a time from b into a. When the program locates the element with the value 4, the first UPDATE statement changes that element value to 10 and exits the loop. In the first UPDATE statement, x is a derived-column name used to update the current element in the collection-derived table. The second UPDATE statement updates the base table table1 with the new collection. For information on how to use collection host variables in an ESQL/C program, see the discussion of complex data types in the IBM Informix ESQL/C Programmer's Manual. Example of Inserting a Value into a Multiset Collection: Suppose the ESQL/C host variable a_multiset has the following declaration:
EXEC SQL BEGIN DECLARE SECTION; client collection multiset(integer not null) a_multiset; EXEC SQL END DECLARE SECTION;
The following INSERT statement adds a new MULTISET element of 142,323 to a_multiset:
EXEC SQL EXEC SQL where EXEC SQL EXEC SQL where allocate collection :a_multiset; select multiset_col into :a_multiset from table1 id = 107; insert into table(:a_multiset) values (142323); update table1 set multiset_col = :a_multiset id = 107;
5-13
Collection-Derived Table
When you insert elements into a client-collection variable, you cannot specify a SELECT statement or an EXECUTE FUNCTION statement in the VALUES clause of the INSERT. When you insert elements into a server-collection variable, however, the SELECT and EXECUTE FUNCTION statements are valid in the VALUES clause. For more information on client- and server-collection variables, see the IBM Informix ESQL/C Programmer's Manual.
To access the elements (or fields) of a nested collection, use a collection or row variable that matches the element type (a_list and an_int in the preceding code fragment) and a select cursor.
The following ESQL/C code fragment adds the fields in the a_row variable to the row_col column of the tab_row table:
EXEC SQL update table(:a_row) set x=0, y=0, length=10, width=20; EXEC SQL update rectangles set rect = :a_row;
Related Information
DECLARE, DELETE, DESCRIBE, FETCH, INSERT, PUT, SELECT, UPDATE, DEFINE, and FOREACH. For information on how to use COLLECTION variables in an SPL routine, see the IBM Informix Guide to SQL: Tutorial. For information on how to use collection or row variables in an ESQL/C program, see the chapter on complex data types in the IBM Informix ESQL/C Programmer's Manual.
5-14
Database Name
Use the Database Name segment to specify the name of a database. Use this segment when you see a reference to a database name in a syntax diagram.
Syntax
Database Name:
dbname @dbservername //dbservername/ dbname (1) db_var
Notes: 1
Element dbname dbservername db_var Description Database name (with no pathname nor database server name) Database server on which the database dbname resides Host variable whose value specifies a database environment
ESQL/C only
Restrictions Must be unique among the names of databases of the database server Must exist. No blank space can separate @ from dbservername. Variable must be a fixed-length character data type Syntax Identifier on page 5-22 Identifier on page 5-22 Language specific
Usage
Database names are not case sensitive. You cannot use delimited identifiers for a database name. The identifiers dbname and dbservername can each have a maximum of 128 bytes. 3 3 3 3 If the name of a database server is a delimited identifier or if it includes uppercase letters, that database server cannot participate in cross-server distributed DML operations. To avoid this restriction, use only undelimited names that include no uppercase letters when you declare the name or the alias of a database server. In a nondefault locale, dbname can include alphabetic characters from the code set of the locale. In a locale that supports a multibyte code set, keep in mind that the maximum length of the database name refers to the number of bytes, not the number of characters. For more information on the GLS aspects of naming databases, see the IBM Informix GLS User's Guide.
Examples
You can choose a database on another database server as your current database by specifying a database server name. The database server that dbservername specifies must match the name of a database server that is listed in your sqlhosts information.
5-15
Database Name
The following examples show valid database specifications, qualified by the database server name:
empinfo@personnel empinfo @personnel
In these examples, empinfo is the name of the database and personnel is the name of the database server.
Here empinfo is the dbname and personnel is the name of the database server.
5-16
Syntax
(1) database @dbservername (2) . object (3) Owner Name . . object coserver_num :
Notes: 1 2 3
Element coserver_num database dbservername object Description Coserver where object resides Database where object resides Database server of database Name of a database object
Informix extension Extended Parallel Server only See Owner Name on page 5-43
Restrictions Must exist. Must exist. Must exist. No blankspace after @. See Usage on page 5-17. Syntax Literal Number on page 4-137 Database Name on page 5-15 Identifier on page 5-22 Identifier on page 5-22
Usage
A database object name can include qualifiers and separator symbols to specify a database, a server, a coserver (for XPS only), an owner, and (for some objects) another object of which the current database object is a component. For example, this expression specifies the unit-price column of the stock table, owned by user informix, in the stores_demo database of a database server called butler:
stores_demo@butler:informix.stock.unit_price
If you are creating or renaming a database object, the new name that you declare must be unique among objects of the same type in the database. Thus, the name of a new view must be unique among the names and synonyms of tables, views, and sequence objects that already exist in the same database. (But a view can have the same name as a view in a different database of the same server, or the same name as a trigger, for example, because these are different types of objects.) In an ANSI-compliant database, the ownername.object combination must be unique in the database for the type of object. A database object specification must include the owner name for a database object that you do not own. For example, if you specify a table that you do not own, you must also specify the owner of the table. The owner of all the system catalog tables is informix.
Chapter 5. Other Syntax Segments
5-17
In this example, the name of the external database is corp_db. The name of the owner of the table is hrdirector. The name of the table is executives. Here the colon ( : ) separator is required after the database qualifier. In Dynamic Server, queries and other data manipulation language (DML) operations on other databases of the local database server can access the built-in opaque data types BOOLEAN, BLOB, CLOB, and LVARCHAR. DML operations can also access user-defined data types (UDTs) that can be cast to built-in types, as well as DISTINCT types that are based on built-in types, if each DISTINCT types and UDT is explicitly cast to a built-in type, and if all the DISTINCT types, UDTs, and casts are defined in all of the participating databases. The same data-type restrictions also apply to the arguments and to the returned values of a user-defined routine (UDR) that accesses other databases of the local Dynamic Server instance, if the UDR is defined in all of the participating databases. Specifying a Database Object in a Cross-Server Query: To specify an object in a database of a remote database server, you must use a fully-qualified identifier that specifies the database, database server, and owner (if the external database is ANSI compliant) in addition to the database object name. For example, hr_db@remoteoffice:hrmanager.employees is a fully-qualified table name. Here the database is hr_db, the database server is remoteoffice, the table owner is hrmanager, and the table name is employees. The at ( @ ) separator, with no blank spaces, is required between the database and database server qualifiers. Cross-server queries can only access columns of built-in data types that are not opaque data types. (You cannot access UDTs, nor opaque, complex, or other extended data types in cross-server operations.) In Dynamic Server, if a UDR exists on a remote database server, you must specify a fully-qualified identifier for the UDR. Like cross-server DML operations, a remote UDR is limited to built-in non-opaque data types for its arguments, parameters, and returned values. You can refer to a remote database object in the following statements only. For information on the support in these statements across databases of the local server, or across database servers, refer to the IBM Informix Guide to SQL: Tutorial.
5-18
3 3 3 3
If the name of a database server is a delimited identifier or if it includes uppercase letters, that database server cannot participate in distributed DML operations. To avoid this restriction, use only undelimited names that include no uppercase letters when you declare the name or the alias of a database server.
5-19
Syntax
External Routine Reference:
(1) EXTERNAL NAME Shared-Object Filename LANGUAGE C JAVA
(2)
(3)
Usage
If the IFX_EXTEND_ROLE configuration parameter is set to ON, authorization to use this segment is available only to the Database Server Administrator (DBSA), and to users whom the DBSA has granted the EXTEND role. By default, the DBSA is user informix. This segment specifies the following information about an external routine: v Pathname to the executable object code, stored in a shared-object file For C routines, this file is either a DLL or a shared library, depending on your operating system. For Java routines, this file is a jar file. Before you can create a UDR written in the Java language, you must assign a jar identifier to the external jar file with the sqlj.install_jar procedure. For more information, see sqlj.install_jar on page 2-339. v The name of the programming language in which the UDR is written v The parameter style of the UDR By default, the parameter style is INFORMIX. (This implies that if you specify OUT or INOUT parameters, the OUT or INOUT values are passed by reference.) v The VARIANT or NOT VARIANT option, if you specify one. (This option is not available for SPL routines.)
5-20
The function returns a single value of type BOOLEAN. The external name specifies the path to the C shared-object file where the object code of the function is stored. The external name indicates that the library contains another function, point1_equal( ), which is invoked while equal( ) executes.
5-21
Identifier
An identifier specifies the unqualified name of a database object, such as an access method, aggregate, alias, blobspace, cast, column, constraint, correlation, data type, index, operator class, optimizer directive, partition, procedure, table, trigger, sequence, synonym, or view. Use the Identifier segment whenever you see a reference to an identifier in a syntax diagram.
Syntax
Identifier:
letter underscore letter digit underscore (1) dollar_sign (2) Delimited Identifier
Notes: 1 2
Element digit dollar_sign letter underscore Description Integer in range 0 to 9 Dollar ( $ ) symbol Upper- or lowercase letter of the alphabet Underscore ( _ ) character
Cannot substitute a space, hyphen, or other Literal symbol entered from non-alphanumeric character the keyboard.
Usage
This is a logical subset of Database Object Name on page 5-17, a segment that can specify the owner, database, and database server of external objects. To include other non-alphanumeric symbols, such as a blank space (ASCII 32), in an identifier, you must use a delimited identifier. It is recommended that you do not use the dollar sign ( $ ) in identifiers, because this symbol is a special character whose inclusion in an identifier might cause conflicts with other syntax elements. For more information, see Delimited Identifiers on page 5-23. An identifier must have a length of at least 1 byte, but no more than 128 bytes. For example, employee_information is valid as a table name. If you are using a multibyte code set, keep in mind that the maximum length of an identifier refers to the number of bytes, not to the number of logical characters.
5-22
Identifier
For letter characters in nondefault locales, see Support for Non-ASCII Characters in Identifiers on page 5-23. For further information on the GLS aspects of identifiers, see Chapter 3 of the IBM Informix GLS User's Guide. When you use ESQL/C with Dynamic Server, the database server checks the internal version number of the client application and the setting of the IFX_LONGID environment variable to determine whether a client application supports long identifiers (up to 128 bytes in length). For more information, see the IBM Informix Guide to SQL: Reference.
Delimited Identifiers
By default, the character set of a valid SQL identifier is restricted to letters, digits, underscore, and dollar-sign symbols. If you set the DELIMIDENT environment
Chapter 5. Other Syntax Segments
5-23
Identifier
variable, however, SQL identifiers can also include additional characters from the code set implied by the setting of the DB_LOCALE environment variable. Delimited Identifier:
"
"
Description Integer in the range 0 to 9 Letter that forms part of the delimited identifier Nonalphanumeric character, such as #, $, or blank space Underscore ( _ ) symbol in the delimited identifier
Restrictions Cannot be the first character Letters in delimited identifiers are case-sensitive Must be an element in the code set of the database locale Cannot include more than 128
Syntax Literal Number on page 4-137 Literal value entered from the keyboard. Literal value entered from the keyboard. Literal value entered from the keyboard.
If the database supports delimited identifiers, double quotes ( ) must enclose every SQL identifier in your code, and single ( ) quotes, rather than double ( ) quotes, must delimit all character-string literals. Delimited identifiers enable you to declare names that are otherwise identical to SQL keywords, such as TABLE, WHERE, DECLARE, and so on. The only type of object for which you cannot specify a delimited identifier is a database name. Letters in delimited identifiers are case sensitive. If you are using the default locale, letter must be an upper- or lowercase character in the range a to z or A to Z (in the ASCII code set). If you are using a nondefault locale, letter must be an alphabetic character that the locale supports. For more information, see Support for Non-ASCII Characters in Delimited Identifiers (GLS) on page 5-24. Delimited identifiers are compliant with the ANSI/ISO standard for SQL. When you create a database object, avoid including leading blank spaces or other whitespace characters between the first delimiting quotation mark and the first nonblank character of the delimited identifier. (Otherwise, you might not be able to reference the object in some contexts.) 3 3 3 3 If the name of a database server is a delimited identifier or if it includes uppercase letters, that database server cannot participate in distributed DML operations. To avoid this restriction, use only undelimited names that include no uppercase letters when you declare the name or the alias of a database server. Support for Nonalphanumeric Characters: You can use delimited identifiers to specify nonalphanumeric characters in the names of database objects. You cannot use delimited identifiers, however, to specify nonalphanumeric characters in the names of storage objects such as dbspaces, partitions, and blobspaces. Support for Non-ASCII Characters in Delimited Identifiers (GLS): When you are using a nondefault locale whose code set supports non-ASCII characters, you
5-24
Identifier
can specify non-ASCII characters in most delimited identifiers. The rule is that if you can specify non-ASCII characters in the undelimited form of the identifier, you can also specify non-ASCII characters in the delimited form of the same identifier. For a list of identifiers that support non-ASCII characters and for information on non-ASCII characters in delimited identifiers, see the IBM Informix GLS User's Guide. Effect of DELIMIDENT Environment Variable: To use delimited identifiers, you must set the DELIMIDENT environment variable. While DELIMIDENTis set , strings enclosed in double quotes ( ) are treated as identifiers of database objects, and strings enclosed in single quotes ( ) are treated as literal strings. If the DELIMIDENT environment variable is not set, however, strings enclosed in double quotes are also treated as literal strings. If DELIMIDENT is set, the SELECT statement in the following example must be in single quotes in order to be treated as a quoted string:
PREPARE ... FROM SELECT * FROM customer;
If a delimited identifier is used in the SELECT statement that defines a view, then the DELIMIDENT environment variable must be set in order for the view to be accessed, even if the view name itself contains no special characters. Examples of Delimited Identifiers: The next example shows how to create a table with a case-sensitive name:
CREATE TABLE "Proper_Ranger" (...);
The following example creates a table whose name includes a whitespace character. If the table name were not enclosed by double ( ) quotes, and if DELIMIDENT were not set, you could not use a blank space in the identifier.
CREATE TABLE "My Customers" (...);
The next example creates a table that has a keyword as the table name:
CREATE TABLE "TABLE" (...);
The following example (for Dynamic Server) shows how to delete all the rows from a table that is named FROM when you omit the keyword FROM in the DELETE statement:
DELETE FROM;
Using Double Quotes Within a Delimited Identifier: To include a double quote ( ) in a delimited identifier, you must precede the double quote ( ) with another double quote ( ), as this example shows:
CREATE TABLE "My""Good""Data" (...);
5-25
Identifier
functions (AVG, COUNT, MAX, MIN, SUM) as well as the function expressions (algebraic, exponential and logarithmic, time, hex, length, dbinfo, trigonometric, and trim functions). Using avg as a column name causes the next example to fail because the database server interprets avg as an aggregate function rather than as a column name:
SELECT avg FROM mytab; -- fails
If the DELIMIDENT environment variable is set, you could use avg as a column name as the following example shows:
SELECT "avg" from mytab -- successful
The workaround in the following example removes ambiguity by including a table name with the column name:
SELECT mytab.avg FROM mytab;
If you use the keyword TODAY, CURRENT, or USER as a column name, ambiguity can occur, as the following example shows:
CREATE TABLE mytab (user char(10), CURRENT DATETIME HOUR TO SECOND,TODAY DATE); INSERT INTO mytab VALUES(josh,11:30:30,1/22/1998); SELECT user,current,today FROM mytab;
The database server interprets user, current, and today in the SELECT statement as the built-in functions USER, CURRENT, and TODAY. Thus, instead of returning josh, 11:30:30,1/22/1998, the SELECT statement returns the current user name, the current time, and the current date. If you want to select the actual columns of the table, you must write the SELECT statement in one of the following ways:
SELECT mytab.user, mytab.current, mytab.today FROM mytab;
You must use a workaround to make this SELECT statement execute successfully. If the DELIMIDENT environment variable is set, you can use all as a column name by enclosing all in double quotes. In the following example, the SELECT statement executes successfully because the database server interprets all as a column name:
5-26
Identifier
SELECT "all" from mytab; -- successful
The workaround in the following example uses the keyword ALL with the column name all:
SELECT ALL all FROM mytab;
The examples that follow show workarounds for using the keywords UNIQUE or DISTINCT as a column name in a CREATE TABLE statement. The next example fails to declare a column named unique because the database server interprets unique as a keyword rather than as a column name:
CREATE TABLE mytab (unique INTEGER); -- fails
The following workaround uses two SQL statements. The first statement creates the column mycol; the second statement renames the column mycol to unique:
CREATE TABLE mytab (mycol INTEGER); RENAME COLUMN mytab.mycol TO unique;
The workaround in the following example also uses two SQL statements. The first statement creates the column mycol; the second alters the table, adds the column unique, and drops the column mycol:
CREATE TABLE mytab (mycol INTEGER); ALTER TABLE mytab ADD (unique INTEGER), DROP (mycol);
If the DELIMIDENT environment variable is set, you could use interval as a column name, as the following example shows:
SELECT "interval" from mytab; -- successful
The workaround in the following example removes ambiguity by specifying a table name with the column name:
SELECT mytab.interval FROM mytab;
The workaround in the following example includes an owner name with the table name:
SELECT josh.mytab.interval FROM josh.mytab;
5-27
Identifier
v Renaming a column to rowid You can, however, use the term rowid as a table name.
CREATE TABLE rowid (column INTEGER, date DATE, char CHAR(20));
Important: It is recommended that you use primary keys as an access method, rather than exploiting the rowid column.
Examples
Examples in this section show workarounds that involve owner naming when the keyword STATISTICS or OUTER is a table name. (This workaround also applies to STATISTICS or OUTER as a view name or synonym.) Using statistics as a table name causes the following example to fail because the database server interprets it as part of the UPDATE STATISTICS syntax rather than as a table name in an UPDATE statement:
UPDATE statistics SET mycol = 10;
The workaround in the following example specifies an owner name with the table name, to avoid ambiguity:
UPDATE josh.statistics SET mycol = 10;
Using outer as a table name causes the following example to fail because the database server interprets outer as a keyword for performing an outer join:
SELECT mycol FROM outer; -- fails
5-28
Identifier
The workaround in the following example includes the AS keyword:
SELECT mycol AS units FROM mytab;
The following examples uses the AS or FROM keyword as a column label. Using as as a column label causes the following example to fail because the database server interprets as as identifying from as a column label and thus finds no required FROM clause:
SELECT mycol as from mytab; -- fails
Using from as a column label causes the following example to fail because the database server expects a table name to follow the first from:
SELECT mycol from FROM mytab; -- fails
This example uses the AS keyword to identify the first from as a column label:
SELECT mycol AS from FROM mytab;
The workaround in the following example uses the keyword AS to identify order as a table alias:
SELECT * FROM mytab AS order;
The next two examples show how to use the keyword WITH as a table alias. Using with as a table alias causes the next example to fail because the database server interprets with as part of the WITH CHECK OPTION syntax:
EXEC SQL select * from mytab with; -- fails
The workaround in the following example uses the keyword AS to identify with as a table alias:
EXEC SQL select * from mytab as with; -- succeeds
The next two examples use the keyword CREATE as a table alias. Using create as a table alias causes the next example to fail because the database server interprets the keyword as part of the syntax to create a new database object, such as a table, synonym, or view:
EXEC SQL select * from mytab create; -- fails EXEC SQL select * from mytab as create; -- succeeds
The workaround uses the keyword AS to identify create as a table alias. (Using grant as an alias would similarly fail, but is valid after the AS keyword.)
5-29
Identifier
You can use the variable select in an IN list if you ensure it is not the first element in the list. The workaround in the following example corrects the IF statement that the preceding example shows:
IF x IN (10, select, 12) THEN LET y = 1; -- problem if
5-30
Identifier
No workaround exists to using null as a variable name and attempting to use that variable in an IN condition.
If the DELIMIDENT environment variable is set, you could use global as a variable name, as the following example shows:
DEFINE "global" INT; -- successful
Important: Although workarounds that the preceding sections show can avoid compilation or runtime syntax conflicts from keywords used as identifiers, keep in mind that such identifiers tend to make code more difficult to understand and to maintain.
5-31
Identifier
SELECT co2 FROM tab2; END WHILE; WHILE var2 = var1 BEGIN SELECT col1 INTO var3 FROM TAB -- ok syntax UNION SELECT co2 FROM tab2; END END WHILE;
The following examples show the incorrect and correct use of a SET LOCK MODE statement inside an ON EXCEPTION statement. The following ON EXCEPTION statement returns an error because the SET LOCK MODE statement is not enclosed in a BEGIN ... END statement block:
ON EXCEPTION IN (-107) SET LOCK MODE TO WAIT; -- error, value expected, not lock END EXCEPTION;
The following ON EXCEPTION statement executes successfully because the SET LOCK MODE statement is enclosed in a BEGIN ... END statement block:
ON EXCEPTION IN (-107) BEGIN SET LOCK MODE TO WAIT; -- ok END END EXCEPTION;
Related Information
For a discussion of owner naming, see your IBM Informix Performance Guide. For a discussion of identifiers that support non-ASCII characters and a discussion of non-ASCII characters in delimited identifiers, see the IBM Informix GLS User's Guide.
5-32
Jar Name
Use the Jar Name segment to specify the name of a jar ID. Use this segment whenever you see a reference to Jar Name in a syntax diagram. Only Dynamic Server supports this syntax segment.
Syntax
Jar Name:
jar_id package . database .
Element database
Description Database in which to install or access jar_id. Default is the current database. The .jar file that contains the Java class to be accessed Name of the package
Restrictions Fully qualified database.package.jar_id identifier must not exceed 255 bytes File must exist in database.package Package must exist in database
Syntax Database Name on page 5-15 Identifier on page 5-22 Identifier on page 5-22
jar_id package
If a jar name is specified as a character string argument to the sqlj.install_jar, sqlj.replace_jar, or sqlj.remove_jar procedures, then any identifiers in the jar name that are delimited identifiers will include the surrounding double quote characters. Before you can access a jar_id in any way (including its use in a CREATE FUNCTION or CREATE PROCEDURE statement), it must be defined in the current database with the install_jar( ) procedure. For more information, see EXECUTE PROCEDURE on page 2-336.
Related Information
For information on how to update the three-part names of JAR files after you rename the database, see the J/Foundation Developer's Guide.
5-33
Optimizer Directives
The Optimizer Directives segment specifies keywords that you can use to partially or fully specify the query plan of the optimizer. Use this segment whenever you see a reference to Optimizer Directives in a syntax diagram.
Syntax
Optimizer Directives:
, --+ {+ /*+ (1) Access-Method Directives (2) Join-Order Directive (3) Join-Method Directives (4) (5) Optimization-Goal Directives (6) Explain-Mode Directives (7) (8) Rewrite Method Directives
} */
Notes: 1 2 3 4 5 6 7 8 See Access-Method Directives on page 5-35 See Join-Order Directive on page 5-37 See Join-Method Directives on page 5-38 Dynamic Server only See Optimization-Goal Directives (IDS) on page 5-40 See Explain-Mode Directives on page 5-40 Extended Parallel Server only See Rewrite Method Directive (XPS) on page 5-42
Usage
Optimizer directives are valid in any query of a DELETE, SELECT, or UPDATE statement. Use one or more directives to partially or fully specify the query plan of the optimizer. The scope of the directive is the current query only. Directives are enabled by default. To obtain information about how specified directives are processed, view the output of the SET EXPLAIN statement. To disable directives, set the IFX_DIRECTIVES environment variable to 0 or OFF, or set the DIRECTIVES parameter in the ONCONFIG file to 0.
5-34
Optimizer Directives
comment symbol, the first character in an optimizer directive is always a plus ( + ) sign. No blank space or other whitespace character is allowed between the comment indicator and the plus sign. You can use any of the following comment indicators: v A double hyphen ( -- ) delimiter The double hyphen needs no closing symbol because it specifies only the remainder of the current line as comment. When you use this style, include the optimizer directive on only the current line. v Braces ( { } ) delimiters The comment extends from the left brace ( { ) until the next right ( } ) brace; this can be in the same line or in some subsequent line. v C-language style slash and asterisk ( /* */ ) delimiters The comment extends from the initial slash-asterisk ( /* ) pair until the next asterisk-slash ( */ ) characters in the same line or in some subsequent line. In ESQL/C, the -keep command option to the esql compiler must be specified when you use C-style comments. For additional information, see How to Enter SQL Comments on page 1-3. If you specify multiple directives in the same query, you must separate them with a blank space, a comma, or by any character that you choose. It is recommended that you separate successive directives with a comma. If the query declares an alias for a table, use the alias (rather than the actual table name) in the optimizer directive specification. Because system-generated index names begin with a blank character, use quotation marks to delimit such names. Syntax errors in an optimizer directive do not cause a valid query to fail. You can use the SET EXPLAIN statement to obtain information related to such errors.
Access-Method Directives
Use the access-method directive to specify the manner in which the optimizer should search the tables. Access-Method Directives:
(1) INDEX_ALL INDEX ( Table Reference , index index , AVOID_INDEX ( FULL AVOID_FULL ( Table Reference Table Reference index index )
5-35
Optimizer Directives
comments
Table Reference:
alias synonym table
Notes: 1
Element alias comments index Description Temporary alternative table name declared in the FROM clause Text that documents the optimizer directive Index for which to specify a query plan directive Synonym or table in a query for which to specify a directive
synonym, table
Use commas or blank spaces to separate elements within the parentheses. The following table describes each of the access-method directives and indicates how it affects the query plan of the optimize.
Keywords AVOID_FULL AVOID_INDEX Effect No full-table scan on the listed table Does not use any of the indexes listed Performs a full-table scan Uses the index specified to access the table Access the table using all listed indexes (Multi-index scan) Optimizer Action The optimizer considers the various indexes it can scan. If no index exists, the optimizer performs a full-table scan. The optimizer considers the remaining indexes and a full- table scan. If all indexes for a table are specified, optimizer uses a full-table scan to access the table. Even if an index exists on a column, the optimizer uses a full-table scan to access the table. If more than one index is specified, the optimizer chooses the index that yields the least cost. If no indexes are specified, then all the available indexes are considered. If more than one index is specified, all are used. If only one is specified, optimizer follows an INDEX SKIP SCAN using that index. If no index is specified, then all available indexes are considered.
FULL INDEX
INDEX_ALL
Both the AVOID_FULL and INDEX keywords specify that the optimizer should avoid a full scan of a table. It is recommended, however, that you use the AVOID_FULL keyword to specify the intent to avoid a full scan on the table. In
5-36
Optimizer Directives
addition to specifying that the optimizer not use a full-table scan, the negative directive allows the optimizer to use indexes that are created after the access-method directive is specified. In general, you can specify only one access-method directive per table. You can, however, specify both AVOID_FULL and AVOID_INDEX for the same table. When you specify both of these access-method directives, the optimizer avoids performing a full scan of the table and it avoids using the specified index or indexes. This combination of negative directives allows the optimizer to use indexes that are created after the access-method directives are specified. Suppose that you have a table named emp that contains the following indexes: loc_no, dept_no, and job_no. When you perform a SELECT that uses the table in the FROM clause, you might direct the optimizer to access the table in one of the following ways: v Example using a positive directive:
SELECT {+INDEX(emp dept_no)}
In this example the access-method directive forces the optimizer to scan the index on the dept_no column. v Example using a negative directive:
SELECT {+AVOID_INDEX(emp loc_no, job_no), AVOID_FULL(emp)}
This example includes multiple access-method directives. These access-method directives also force the optimizer to scan the index on the dept_no column. If a new index, emp_no is created for table emp, however, the optimizer can consider it.
Join-Order Directive
Use the ORDERED join-order directive to force the optimizer to join tables or views in the order in which they appear in the FROM clause of the query. Join-Order Directive:
ORDERED comments
Element comments
For example, the following query forces the database server to join the dept and job tables and then join the result with the emp table:
SELECT --+ ORDERED name, title, salary, dname FROM dept, job, emp WHERE title = clerk AND loc = Palo Alto AND emp.dno = dept.dno AND emp.job= job.job;
Because no predicates occur between the dept table and the job table, this query forces the database server to construct a Cartesian product. When your query involves a view, the placement of the ORDERED join-order directive determines whether you are specifying a partial- or total-join order.
Chapter 5. Other Syntax Segments
5-37
Optimizer Directives
v Specifying partial-join order when you create a view If you use the ORDERED directive when you create a view, the base tables are joined contiguously in the order of the view definition. For all subsequent queries on the view, the database server joins the base tables contiguously in the order specified in the view definition. When used in a view, the ORDERED directive does not affect the join order of other tables named in the FROM clause in a query. v Specifying total-join order when you query a view When you specify the ORDERED join-order directive in a query that uses a view, all tables are joined in the order specified, even those tables that form views. If a view is included in the query, the base tables are joined contiguously in the order of the view definition. For examples of ORDERED with views, refer to your IBM Informix Performance Guide.
Join-Method Directives
Use join-method directives to influence how tables are joined in a query. Join-Method Directives:
, (1) USE_NL AVOID_NL ( Table Reference , (1) AVOID_HASH USE_HASH ( Table Reference /BUILD /PROBE /BROADCAST (2) )
comments
Notes: 1 2
Element comments Description Text to documents the directive
This diagram is simplified: /BROADCAST is not valid with AVOID_HASH. Use commas or blank spaces to separate the elements within the parentheses. The following table describes each of the join-method directives. Keyword USE_NL Effect Uses the specified tables as the inner table in a nested-loop join If n tables are specified in the FROM clause, then at most n-1 tables can be specified in the USE_NL join-method directive. USE_HASH Uses a hash join to access the specified table
5-38
Optimizer Directives
You can also choose whether the table will be used to create the hash table or to probe the hash table. AVOID_NL Does not use the specified table as inner table in a nested loop join A table listed with this directive can still participate in a nested loop join as the outer table. AVOID_HASH Does not access the specified table using a hash join You can optionally use a hash join, but impose restrictions on the role of the table within the hash join. A join-method directive takes precedence over the join method forced by the OPTCOMPIND configuration parameter. When you specify the USE_HASH or AVOID_HASH directives (to use or avoid a hash join, respectively), you can also specify the role of each table: v /BUILD With the USE_HASH directive, this keyword indicates that the specified table be used to construct a hash table. With the AVOID_HASH directive, this keyword indicates that the specified table not be used to construct a hash table. v /PROBE With the USE_HASH directive, this keyword indicates that the specified table be used to probe the hash table. With the AVOID_HASH directive, this keyword indicates that the specified table not be used to probe the hash table. You can specify multiple probe tables as long as there is at least one table for which you do not specify PROBE. v /BROADCAST Only Extended Parallel Server supports the BROADCAST directive, which can improve performance when small base tables or derived tables with few records (< 100K in data size) are involved in a join. In such cases, all the records of the small tables are sent to each instance of a given join operation. The BROADCAST directive must also include either /BUILD or /PROBE, but it is not valid with AVOID_HASH. If the /BUILD option is included, the USE_HASH table is broadcast. If the /PROBE option is included, the join in which the USE_HASH table participates is broadcast. For the optimizer to find an efficient join query plan, you must at least run UPDATE STATISTICS LOW for every table that is involved in the join, so as to provide appropriate cost estimates. Otherwise, the optimizer might choose to broadcast the entire table to all instances, even if the table is large. If neither the /BUILD nor the /PROBE keyword is specified, the optimizer uses cost estimates to determine the role of the table. In this example, the USE_HASH directive forces the optimizer to construct a hash table on the dept table and consider only the hash table to join dept with the other tables. Because no other directives are specified, the optimizer can choose the least expensive join methods for the other joins in the query.
5-39
Optimizer Directives
SELECT /*+ USE_HASH (dept /BUILD) The optimizer must use dept to construct a hash table */ name, title, salary, dname FROM emp, dept, job WHERE loc = Phoenix AND emp.dno = dept.dno AND emp.job = job.job;
comments
Element comments
The two optimization-goal directives are: v FIRST_ROWS This tells the optimizer to choose a plan that optimizes the process of finding only the first screenful of rows that satisfies the query. Use this option to decrease initial response time for queries that use an interactive mode or that require the return of only a few rows. v ALL_ROWS This directive tells the optimizer to choose a plan that optimizes the process of finding all rows that satisfy the query. This form of optimization is the default. An optimization-goal directive takes precedence over the OPT_GOAL environment variable setting and over the OPT_GOAL configuration parameter. For information about how to set the optimization goal for an entire session, see the SET OPTIMIZATION statement. You cannot use an optimization-goal directive in the following contexts: v In a view definition v In a subquery The following query returns the names of the employees who earned the top fifty bonuses. The optimization-goal directive directs the optimizer to return the first screenful of rows as fast as possible.
SELECT {+FIRST_ROWS Return the first screenful of rows as fast as possible}
Explain-Mode Directives
Use the explain-mode directives to test and debug query plans and to print information about the query plan to the sqexplain.out file.
5-40
Optimizer Directives
Explain-Mode Directives:
EXPLAIN AVOID_EXECUTE , comments
Element comments
The following table lists the effect of each explain-mode directive. Keyword EXPLAIN AVOID_EXECUTE Effect Turns SET EXPLAIN ON for the specified query Prevents the data manipulation statement from executing; instead, the query plan is printed to the sqexplain.out file
The EXPLAIN directive is primarily useful for testing and debugging query plans. It is redundant when SET EXPLAIN ON is already in effect. It is not valid in a view definition or in a subquery. The next query executes and prints the query plan to the sqexplain.out file:
SELECT {+EXPLAIN} c.customer_num, c.lname, o.order_date FROM customer c, orders o WHERE c.customer_num = o.customer_num;
The AVOID_EXECUTE directive prevents execution of a query on either the local or remote site, if a remote table is part of the query. This directive does not prevent nonvariant functions in a query from being evaluated. The next query does returns no data, but writes its query plan to file sqexplain.out:
SELECT {+EXPLAIN, AVOID_EXECUTE} c.customer_num, c.lname, o.order_date FROM customer c, orders o WHERE c.customer_num = o.customer_num;
You must use both the EXPLAIN and AVOID_EXECUTE directives to see the query plan of the optimizer (in the sqexplain.out file) without executing the query. The comma ( , ) separating these two directives is optional. If you omit the EXPLAIN directive when you specify the AVOID_EXECUTE directive, no error is issued, but no query plan is written to the sqexplain.out file and no DML statement is executed. You cannot use the explain-mode directives in the following contexts: v In a view definition v In a trigger v In a subquery They are valid, however, in a SELECT statement within an INSERT statement.
5-41
Optimizer Directives
Element comments
Related Information
For information about the sqexplain.out file, see SET EXPLAIN. For information about how to create optimizer directives that the database server stores in the sysdirectives system catalog table for use with subsequent queries, see SAVE EXTERNAL DIRECTIVES. For information about how to set optimization settings for an entire session, see the description of SET OPTIMIZATION. For a discussion about optimizer directives and performance, see your IBM Informix Performance Guide. For descriptions of the IFX_DIRECTIVES and IFX_EXTDIRECTIVES environment variables, see the IBM Informix Guide to SQL: Reference. For information on the DIRECTIVES parameter in the onconfig file, see your IBM Informix Administrator's Reference.
5-42
Owner Name
The owner name specifies the owner of a database object. Use this segment whenever you see a reference to Owner Name in a syntax diagram. A synonym for owner name is authorization identifier.
Syntax
Owner Name:
owner owner owner
Element owner
Restrictions
Syntax
Maximum length is 32 bytes Must conform to the rules of your operating system.
Usage
In an ANSI-compliant database, you must specify the owner of any database object that you do not own. The ANSI term for owner name is schema name. In databases that are not ANSI-compliant, the owner name is optional. You do not need to specify owner when you create database objects or use data access statements. If you do not specify owner when you create a database object, the database server assigns your login name as the owner of the object, in most cases. For exceptions to this rule, see Ownership of Created Database Objects on page 2-112 in CREATE FUNCTION and Ownership of Created Database Objects on page 2-150 in CREATE PROCEDURE. When a DDL statement in an owner-privileged UDR creates a new database object, the owner of the routine (rather than the user who executes it, if that user is not the owner of the routine) becomes the owner of the new database object. If you specify owner in data-access statements, the database server checks it for correctness. Without quotation marks, owner is case insensitive. The following four queries all can access data from the table kaths.tab1:
SELECT SELECT SELECT SELECT * * * * FROM FROM FROM FROM tab1; kaths.tab1; KATHS.tab1; Kaths.tab1;
The first query succeeds because the owner name is not required. The second query succeeds because the specified owner name matches the owner name as it is stored in the database.
5-43
Owner Name
User informix is the owner of the system catalog tables. Tip: The USER keyword returns the login name exactly as it is stored on the system. If the owner name is stored differently from the login name (for example, a mixed-case owner name and an all lowercase login name), the owner = USER syntax fails.
If you specify the owner name when you create or rename a database object in an ANSI-compliant database, you must include the owner name in data access statements. You must include the owner name when you access a database object that you do not own. Because the database server automatically shifts owner to uppercase letters if not between quotation marks, case-sensitive errors can cause queries to fail. For example, if you are user nancy and you use the following statement, the resulting view has the name nancy.njcust:
CREATE VIEW nancy.njcust AS SELECT fname, lname FROM customer WHERE state = NJ;
The following SELECT statement fails because it tries to match the name NANCY.njcust to the actual owner and table name of nancy.njcust:
SELECT * FROM nancy.njcust;
In a Dynamic Server distributed query, if the owner name is not between quotation marks, the remote database follows the lettercase convention of the local database. If the local database is ANSI-compliant, then the remote database processes the owner name in uppercase. If the local database is not ANSI- compliant, then the remote database processes the owner name in lowercase.
5-44
Owner Name
Tip: When you use the owner name as one of the selection criteria in a query (for example, WHERE owner = kaths), make sure that the quoted string matches the owner name exactly as it is stored in the database. If the database server cannot find the database object or database, you might need to modify the query so that the quoted string uses uppercase letters (for example, WHERE owner = KATHS). Because owner name is an authorization identifier, rather than an SQL identifier, you can enclose owner between single-quotation marks ( ) in SQL statements of a database where the DELIMIDENT environment variable specifies support for delimited identifiers, thereby requiring double-quotes ( ) around SQL identifiers.
5-45
Purpose Options
The CREATE ACCESS_METHOD, CREATE XADATASOURCE TYPE, and ALTER ACCESS_METHOD statements of Dynamic Server can specify purpose options with the following syntax.
Syntax
Purpose Options:
task = external_routine value = string_value numeric_value flag
Description User-defined function that performs a task Keyword indicating which feature a flag enables
Restrictions Must be registered in the database The interface specifies flag names
Syntax Database Object Name on page 5-17 Flag Purpose Category in the table in Purpose Functions, Methods, Flags, and Values on page 5-47.
A value of a real number A value that is expressed as one or more characters Keyword that identifies a purpose function
Must be within the range of a numeric data Literal Number on page type 4-137 Characters must be from the code set of the Quoted String on page database 4-142. Keywords to which you can assign a function (whose name cannot match the keyword) Predefined configuration keywords to which you can assign values Task Purpose Category in the table in Purpose Functions, Methods, Flags, and Values on page 5-47. Value Purpose Category in the table in Purpose Functions, Methods, Flags, and Values on page 5-47.
value
Usage
Dynamic Server supports purpose options in two contexts: 3 3 3 3 v Defining or modifying primary and secondary access methods for local or remote tables, views, and indexes v Defining access methods for XA-compliant external data sources.
5-46
Purpose Options
Each task, value, or flag keyword corresponds to a column name in the sysams system catalog table. The keywords let you set the following attributes: v Purpose function A purpose-function attribute maps the name of a user-defined function or method to a task keyword, such as am_create, am_beginscan, or am_getnext. For a complete list of these keywords, see the Task category in the table in Purpose Functions, Methods, Flags, and Values. The external_routine specifies the corresponding function (C) that you supply for the access method. Example setting:
am_create = FS_create
v Purpose flag A purpose flag indicates whether an access method supports a given SQL statement or keyword. Example setting:
am_rowids
v Purpose value These string, character, or numeric values provide configuration information that a flag cannot supply. Example setting:
am_sptype = X
To enable a user-defined function or method as a purpose function, you must first register the C function or Java method that performs the appropriate tasks, using the CREATE FUNCTION statement, and then set the purpose keyword equal to the registered function or method name. This creates a new access method. An example on page Example on page 2-9 adds a purpose method to an existing access method. To enable a purpose flag, specify the name without a corresponding value. To clear a purpose-option setting in the sysams table, use the DROP clause of the ALTER ACCESS_METHOD statement.
5-47
Purpose Options
Keyword am_keyscan Explanation A flag that, if set, indicates that am_getnext returns rows of index keys for a secondary-access method. If a query selects only the columns in the index key, the database server uses the row of index keys that the secondary-access method puts in shared memory, without reading the table. A flag to set if a secondary-access method checks for unique keys A flag that you set if a primary- or secondary-access method supports clustering of tables A flag that you set if a primary-access method can retrieve a row from a specified address A flag to set if a primary-access method supports data changes. The default setting, not set, indicates that the virtual data is read-only. For the C Virtual-Table Interface, set this flag if your application will write data, to avoid the following problems: v An INSERT, DELETE, UPDATE, or ALTER FRAGMENT statement causes an SQL error. v Function am_insert, am_delete, or am_update is not executed. am_parallel A flag that the database server sets to indicate which purpose Flag functions or methods can execute in parallel in a primary or secondary-access method. If set, the hexadecimal am_parallel bitmap contains one or more of the following bit settings: v The 1 bit is set for parallelizable scan. v The 2 bit is set for parallelizable delete. v The 4 bit is set for parallelizable update. v The 8 bit is set for parallelizable insert. Insertions, deletions, and updates are not supported in the Java Virtual-Table Interface. am_costfactor A value by which the database server multiplies the cost that the am_scancost purpose function or method returns for a primary or secondary-access method. An am_costfactor value from 0.1 to 0.9 reduces the cost to a fraction of the value that am_scancost calculates. An am_costfactor value of 1.1 or greater increases the am_scancost value. A keyword that you associate with a user-defined function or method (UDR) name that creates a virtual table or virtual index A keyword that you associate with the name of a UDR that drops a virtual table or virtual index A keyword that you associate with the name of a UDR that makes a fragment, extspace, or sbspace available A keyword that you associate with the name of a UDR that reverses the initialization that am_open performs A keyword that you associate with the name of a UDR that inserts a row or an index entry A keyword that you associate with the name of a UDR that deletes a row or an index entry A keyword that you associate with the name of a UDR that changes the values in a row or key A keyword that you associate with the name of a UDR that builds statistics based on the distribution of values in storage spaces Value 1.0 Not set Category Flag Default Not set
5-48
Purpose Options
Keyword am_scancost am_check Explanation A keyword that you associate with the name of a UDR that calculates the cost of qualifying and retrieving data A keyword that you associate with the name of a UDR that tests the physical structure of a table or performs an integrity check on an index Category Task Task Default None None
A keyword that you associate with the name of a UDR that sets up a Task scan A keyword that you associate with the name of a UDR that reverses the setup that am_beginscan initializes Task
A keyword that you associate with the name of a UDR that scans for Task the next item from a previous scan to complete a join or subquery A keyword that you associate with the name of the required UDR that scans for the next item that satisfies a query A keyword that you associate with the name of a UDR that fetches data from a specific physical address; am_getbyid is available only for primary-access methods A keyword that you associate with the name of a UDR that deletes all rows of a virtual table (primary-access method) or that deletes all corresponding keys in a virtual index (secondary-access method) Task Task
am_truncate
Task
None
The following rules apply to the purpose-option specifications in the CREATE ACCESS_METHOD and ALTER ACCESS_METHOD statements: v To specify multiple purpose options in one statement, separate them with commas. v The CREATE ACCESS_METHOD statement must specify a user-defined function or method name that corresponds to the am_getnext keyword. The ALTER ACCESS_METHOD statement cannot drop the function or method name that corresponds to am_getnext but can modify it. v The ALTER ACCESS_METHOD statement cannot add, drop, or modify the am_sptype value. v You can specify the am_defopclass value only with the ALTER ACCESS_METHOD statement. You must first register a secondary-access method with the CREATE ACCESS_METHOD statement before you can assign a default operator class. 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
5-49
Purpose Options
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
xa_start = informix.mqseries_start, xa_end = informix.mqseries_end, xa_rollback = informix.mqseries_rollback, xa_prepare = informix.mqseries_prepare, xa_commit = informix.mqseries_commit, xa_recover = informix.mqseries_recover, xa_forget = informix.mqseries_forget, xa_complete = informix.mqseries_complete);
These values represents the fields in the XA Switch Structure, as listed in the file $INFORMIXDIR/incl/public/xa.h. The order of specifications in this example follows the order of column names in the sysxasourcetypes system catalog table, but they can be listed in any order, provided that no item is repeated. The xa_flags and xa_version values must be numbers; the rest must be names of UDRs that the Transaction Manager can invoke. These UDRs must already exist in the database before you can issue a CREATE XADATASOURCE TYPE statement that references them among its purpose option specifications. The DROP FUNCTION or DROP ROUTINE statement cannot drop a UDR that is listed among the purpose options of a CREATE XADATASOURCE TYPE statement until all of the XA datasource types that were defined using the UDR are dropped. For information about how to use the UDRs in the previous example to coordinate transactions with external XA data sources, see the IBM Informix DataBlade API Programmer's Guide. For information about the MQDataBlade module, see the IBM Informix Built-In DataBlade Modules User's Guide.
Related Information
3 3 Related statements: ALTER ACCESS_METHOD, CREATE ACCESS_METHOD, CREATE OPCLASS, and CREATE XADATASOURCE TYPE For the following topics, see the IBM Informix Virtual-Table Interface Programmer's Guide (for C): v Managing storage spaces, executing in parallel, and calculating statement costs v Registering the access method and purpose functions v Purpose-function reference For the following topics, see the IBM Informix Virtual-Index Interface Programmer's Guide (for C): v Managing storage spaces, executing in parallel, calculating statement costs, bypassing table scans, and enforcing unique-index constraints v Registering the access method and purpose functions v Purpose-function reference
5-50
Return Clause
The Return clause specifies the data type of a value or values that a user-defined function returns. You can use this segment in UDR definitions.
Syntax
Return Clause:
RETURNING (1) RETURNS
(2) Subset of SQL Data Types REFERENCES BYTE TEXT , (3) Subset of SQL Data Types REFERENCES BYTE TEXT
Notes: 1 2 3
Element parameter Description Name that you declare here for a returned parameter of the UDR
Dynamic Server only See Subset of SQL Data Types on page 5-62 Stored Procedure Language only
Restrictions Must be unique among returned parameters of UDRs. If any returned value of the UDR has a name, then all must have names. Syntax Identifier on page 5-22
Usage
In Dynamic Server, for backward compatibility, you can create SPL functions with the CREATE PROCEDURE statement. (That is, you can include a Return clause in CREATE PROCEDURE statements.) Use CREATE FUNCTION, however, to create new SPL routines that return one or more values. After the Return clause has indicated what data types are to be returned, you can use the RETURN statement of SPL at any point in the statement block to return SPL variables that correspond to the values in the Return clause.
5-51
Return Clause
A user-defined function of Extended Parallel Server can return values of any built-in data type except SERIAL, SERIAL8, TEXT, or BYTE. In Dynamic Server, a UDF can return values of any built-in data type except the complex, serial, and large object types that are not blank in the following table.
Data Type BLOB BYTE COLLECTION CLOB LIST MULTISET ROW SET SERIAL SERIAL8 TEXT X X X X X X X X X X X X X X X X C X X X X X Java SPL X X
In Dynamic Server, if you use a complex data type in the Return clause, the calling user-defined routine must define variables of the appropriate complex types to hold the values that the C or SPL user-defined function returns. User-defined functions can return a value of opaque or distinct data types that are defined in the database. The default precision of a DECIMAL that an SPL function returns is 16 digits. For a function to return a DECIMAL with a different number of significant digits, you must specify the returned precision explicitly in the Return clause.
5-52
Return Clause
CREATE FUNCTION sel_text() RETURNING REFERENCES text; DEFINE blob_var REFERENCES text; SELECT blob_col INTO blob_var FROM blob_table WHERE key_col = 10; RETURN blob_var; END FUNCTION;
In Extended Parallel Server, to emulate this example, you can use the CREATE PROCEDURE statement instead of the CREATE FUNCTION statement.
There is no relationship between the names of returned parameters and the names of any variables in the body of the routine. For example, you can define a function to return an INTEGER as xval, but in the body of the same function, a variable declared as xval could be of the data type INTERVAL YEAR TO MONTH.
In the following example, the Return clause can return zero (0) or two values if it occurs in a noncursor function. In a cursor function, however, it returns more than one row from a table, and each returned row contains zero or two values:
RETURNING INT, INT;
In both of the preceding examples, the receiving function or program must be written appropriately to accept the information that the function returns.
5-53
Routine Modifier
A routine modifier specifies characteristics of how a user-defined routine (UDR) behaves. Only Dynamic Server supports routine modifiers.
Syntax
(1) Adding or Modifying a Routine Modifier Dropping a Routine Modifier
Notes: 1 2 3 4
Element parameter Description Name that you declare here for a returned parameter of the UDR
See Adding or Modifying a Routine Modifier on page 5-55 C only SPL only External routines only
Restrictions Must be unique among returned parameters of UDRs. If any returned value of the UDR has a name, then all must have names. Syntax Identifier on page 5-22
Usage
If you drop an existing modifier in an ALTER FUNCTION, ALTER PROCEDURE, or ALTER ROUTINE statement, the database server sets the value of the modifier to the default value, if a default exists. Some modifiers are available only with user-defined functions. For information on whether a specific routine modifier applies only to user-defined functions (that is, if it does not apply to user-defined procedures), see the description of the modifier in the sections that follow. In these sections, as elsewhere in this manual, external refers to UDRs written in the C or Java languages. Features valid for only one language are so designated in the previous diagrams.
5-54
Routine Modifier
Except for VARIANT and NOT VARIANT modifiers, none of the options in this segment are valid for SPL routines.
(1)
NEGATOR =neg_func (3) CLASS =class_name ITERATOR PARALLELIZABLE (1) HANDLESNULLS INTERNAL 0 PERCALL_COST = cost COSTFUNC =cost_func SELFUNC =sel_func SELCONST =selectivity STACK =stack_size
Notes: 1 2 3
Element class_name cost cost_func neg_func sel_func selectivity stack_size Description
Virtual processor (VP) class in which to Any C UDR must run in the CPU VP run the external routine or in a user-defined VP class CPU use cost for each invocation of a C Integer; 1 cost 231-1 (highest cost). language UDR. Default is 0. Name of a companion user-defined cost Must have same owner as the UDR. function to invoke Execute privilege needed to invoke Negator function that can be invoked instead of the UDR Name of a companion user-defined selectivity function to invoke Must have same owner as the UDR. Execute privilege needed to invoke Must have same owner as the UDR. Execute privilege needed to invoke
CPU use cost for each invocation of a C See Concept of Selectivity on page language UDR. Default is 0. 5-59. Size (in bytes) of stack of the thread that executes the C-language UDR Must be a positive integer
You can add these modifiers in any order. If you list the same modifier more than once, the last setting overrides any previous values.
5-55
Routine Modifier
Modifier Descriptions
The following sections describe the modifiers that you can use to help the database server optimally execute a UDR.
CLASS
Use the CLASS modifier to specify the name of a virtual-processor (VP) class in which to run an external routine. A user-defined VP class must be defined before the UDR can be invoked. You can execute C UDRs in the following types of VP classes: v The CPU virtual-processor class (CPU VP) v A user-defined virtual-processor class. If you omit the CLASS modifier to specify a VP class for a UDR written in C, the UDR runs in the CPU VP. User-defined VP classes protect the database server from ill-behaved C UDRs. An ill-behaved C UDR has at least one of the following characteristics: v It runs in the CPU VP for a long time without yielding. v It is not thread safe. v It calls an unsafe operating-system routine. A well-behaved C UDR has none of these characteristics. Execute only well-behaved C UDRs in the CPU VP. Warning: Execution of an ill-behaved C UDR in the CPU VP can cause serious interference with the operation of the database server, and the UDR might not produce correct results. For a discussion of ill-behaved UDRs, see the IBM Informix DataBlade API Programmer's Guide. By default, a UDR written in Java runs in a Java virtual processor class (JVP). Therefore, the CLASS modifier is optional for a UDR written in Java. However, use the CLASS modifier when you register a UDR written in Java to improve readability of your SQL statements.
COSTFUNC
Use the COSTFUNC modifier to specify the cost of a C UDR. The cost of the UDR is an estimate of the time required to execute it. Occasionally, the cost of a UDR depends on its inputs. In that case, you can use a user-defined function to calculate a cost that depends on input values. To execute cost_func, you must have Execute privilege on it and on the UDR.
HANDLESNULLS
Use the HANDLESNULLS modifier to specify that a C UDR can handle NULL values that are passed to it as arguments. If you do not specify HANDLESNULLS for a C language UDR, and if you pass to it an argument that has a NULL value, the UDR does not execute and returns a NULL value. By default, a C language UDR does not handle NULL values. The HANDLESNULLS modifier is not available for SPL routines because SPL routines handle NULL values by default.
5-56
Routine Modifier
INTERNAL
Use the INTERNAL modifier with an external routine to specify that an SQL or SPL statement cannot call the external routine. An external routine that is specified as INTERNAL is not considered during routine resolution. Use the INTERNAL modifier for external routines that define access methods, language managers, and so on. By default, an external routine is not internal; that is, an SQL or SPL statement can call the routine.
ITERATOR
Use the ITERATOR modifier with external functions to specify that the function is an iterator function. An iterator function is a function that returns a single element per function call to return a set of data; that is, it is called with an initial call and zero or more subsequent calls until the set is complete. By default, an external C or Java language function is not an iterator function. An SPL iterator function requires the RETURN WITH RESUME statement, rather than the ITERATOR modifier. In ESQL/C, an iterator function requires a cursor. The cursor allows the client application to retrieve the values one at a time with the FETCH statement. For more information on how to write iterator functions, see IBM Informix User-Defined Routines and Data Types Developer's Guide and the IBM Informix DataBlade API Programmer's Guide. For information about using an iterator function with a virtual table interface in the FROM clause of a query, see Iterator Functions (IDS) on page 2-499.
NEGATOR
Use the NEGATOR modifier with UDRs that return Boolean values. The NEGATOR modifier names a companion user-defined function, called a negator function, to the current function. A negator function takes the same arguments as its companion function, in the same order, but returns the Boolean complement. That is, if a function returns TRUE for a given set of arguments, its negator function returns FALSE when passed the same arguments, in the same order. For example, the following functions are negator functions:
equal(a,b) notequal(a,b)
Both functions take the same arguments, in the same order, but return complementary Boolean values. When it is more efficient to do so, the optimizer can use the negator function instead of the function you specify. To invoke a user-defined function that has a negator function, you must have the Execute privilege on both functions. In addition, the function must have the same owner as its negator function.
PARALLELIZABLE
Use the PARALLELIZABLE modifier to indicate that an external routine can be executed in parallel in the context of a parallelizable data query (PDQ).
Chapter 5. Other Syntax Segments
5-57
Routine Modifier
By default, an external routine is non-parallelizable; that is, it executes in sequence. If your UDR has a complex or smart large object data type as either a parameter or a returned value, you cannot use the PARALLELIZABLE modifier. If you specify the PARALLELIZABLE modifier for an external routine that cannot be parallelizable, the database server returns a runtime error. A C language UDR that calls only PDQ thread-safe DataBlade API functions is parallelizable. These categories of DataBlade API functions are PDQ thread safe: v Data handling An exception in this category is that collection manipulation functions (mi_collection_*) are not PDQ thread safe. v Session, thread, and transaction management v Function execution v Memory management v Exception handling v Callbacks v Miscellaneous For details of the DataBlade API functions that are included in each category, see the IBM Informix DataBlade API Function Reference. If your C language UDR calls a function that is not included in one of these categories, it is not PDQ thread safe and is therefore not parallelizable. To parallelize Java language UDR calls, the database server must have multiple instances of JVPs. UDRs written in the Java language and that open a JDBC connection are not parallelizable.
PERCALL_COST (C)
Use the PERCALL_COST modifier to specify the approximate CPU usage cost that a C language UDR incurs each time it executes. The optimizer uses the cost you specify to determine the order in which to evaluate SQL predicates in the UDR for best performance. For example, the following query has two predicates joined by a logical AND:
SELECT * FROM tab1 WHERE func1() = 10 AND func2() = abc;
In this example, if one predicate returns FALSE, the optimizer need not evaluate the other predicate. The optimizer uses the specified cost to order the predicates so that the least expensive predicate is evaluated first. The CPU usage cost must be an integer between 1 and 231-1, with 1 the lowest cost and 231-1 the most expensive. To calculate an approximate cost per call, add the following two figures: v The number of lines of code executed each time the C UDR is called v The number of predicates that require an I/O access The default cost per execution is 0. When you drop the PERCALL_COST modifier, the cost per execution returns to 0.
5-58
Routine Modifier
SELCONST (C)
Use the SELCONST modifier to specify the selectivity of a C UDR. The selectivity of the UDR is an estimate of the fraction of the rows that the query will select. The value of selectivity constant, selconst, is a floating-point number between 0 and 1 that represents the fraction of the rows for which you expect the UDR to return TRUE.
SELFUNC (C)
Use the SELFUNC modifier with a C UDR to name a companion user-defined function, called a selectivity function, to the current UDR. The selectivity function provides selectivity information about the current UDR to the optimizer. The selectivity of a UDR is an estimate of the fraction of the rows that the query will select. That is, it is an estimate of the number of times the UDR will execute. To execute sel_func, you must have Execute privilege on it and on the UDR. Concept of Selectivity: Selectivity refers to the number of rows that would qualify for a query that does a search based on an equality predicate. The fewer the number of rows that qualify, the more selective the query. For example, the following query has a search condition based on the customer_num column in the customer table:
SELECT * FROM customer WHERE customer_num = 102;
Because each row in the table has a different customer number, this query is highly selective. In contrast, the following query is not selective:
SELECT * FROM customer WHERE state = CA;
Because most of the rows in the customer table are for customers in California, more than half of the rows in the table would be returned. Restrictions on the SELFUNC Modifier: The selectivity function that you specify must satisfy the following criteria: v It must take the same number of arguments as the current C UDR. v The data type of each argument must be SELFUNC_ARG. v It must return a value of type FLOAT between 0 and 1, which represents the percentage of selectivity of the function. (1 is highly selective; 0 is not at all selective.) v It can be written in any language that the database server supports. A user who invokes the C UDR must have the Execute privilege both on that UDR and on the selectivity function that the SELFUNC modifier specifies. Both the C UDR and the selectivity function must have the same owner. For information on how to use the mi_funcarg* functions to extract information about the arguments of a selectivity function, see the IBM Informix DataBlade API Programmer's Guide.
STACK (C)
Use the STACK modifier with a C UDR to override the default stack size that the STACKSIZE configuration parameter specifies.
5-59
Routine Modifier
The STACK modifier specifies the size (in bytes) of the thread stack, which a user thread that executes the UDR uses to hold information such as routine arguments and returned values from functions. A UDR needs to have enough stack space for all its local variables. For a particular UDR, you might need to specify a stack size larger than the default size to prevent stack overflow. When a UDR that includes the STACK modifier executes, the database server allocates a thread-stack size of the specified number of bytes. Once the UDR completes execution, subsequent UDRs execute in threads with a stack size that the STACKSIZE configuration parameter specifies (unless any of these subsequent UDRs have also specified the STACK modifier). For more information about the thread stack, see your IBM Informix Administrator's Guide and the IBM Informix DataBlade API Function Reference.
Related Information
For more information on user-defined routines, see IBM Informix User-Defined Routines and Data Types Developer's Guide and the IBM Informix DataBlade API Programmer's Guide. For more information about how these modifiers can affect performance, see your IBM Informix Performance Guide.
5-60
Syntax
Routine Parameter List:
, Parameter OUT (1) INOUT
Parameter:
(3) Subset of SQL Data Type LIKE table . column REFERENCES BYTE TEXT DEFAULT NULL
parameter (2)
DEFAULT value
Notes: 1 2 3
Element column parameter table value Description Name of a column whose data type is declared for parameter Name of a parameter of the UDR Table that contains column Default used if UDR is called with no value for parameter
Dynamic Server only External routines only See Subset of SQL Data Types on page 5-62
Restrictions Must exist in the specified table Name is required for SPL routines The table must exist in the database Must be a literal, of the same data type as parameter. For opaque types, an input function must be defined Syntax Database Object Name on page 5-17 Identifier on page 5-22 Identifier on page 5-22 Literal Number on page 4-137
Usage
A parameter is a formal argument in the declaration of a UDR. (When you subsequently invoke a UDR that has parameters, you must substitute an actual argument for the parameter, unless the parameter has a default value.) The name of the parameter is optional for external routines of Dynamic Server. When you create a UDR, you declare a name and data type for each parameter. You can specify the data type directly, or use the LIKE or REFERENCES clause to specify the data type. You can optionally specify a default value.
5-61
You cannot create another procedure named cost( ) in the same Dynamic Server database with two arguments. You can, however, create a procedure named cost( ) with a number of arguments other than two. (Another way to circumvent this restriction on the LIKE clause is through user-defined data types.)
5-62
In Extended Parallel Server, to re-create this example, use the CREATE PROCEDURE statement instead of the CREATE FUNCTION statement. Extended Parallel Server supports no more than a single OUT parameter. If one is specified, it must be the last item in the parameter list. Warning: When you specify a date value as the default value for a parameter, make sure to specify 4 digits instead of 2 digits for the year. When you specify a 2-digit year, the DBCENTURY environment variable setting can affect how the database server interprets the date value, so the UDR might not use the default value that you intended. For more information, see the IBM Informix Guide to SQL: Reference.
In the next example, this Java method returns an extra value by passing an array:
public static String allVarchar(String arg1, String[] arg2) throws SQLException { arg2[0] = arg1; return arg1; }
5-63
You can assign INOUT parameters to statement-local variables (SLVs), which the section Statement-Local Variable Expressions (IDS) on page 4-114 describes.
5-64
Shared-Object Filename
Use a shared-object filename to specify a pathname to an executable object file when you register or alter an external routine. Only Dynamic Server supports this syntax segment.
Syntax
Shared-Object File:
(1) C Shared-Object File (3) Java Shared-Object File (4) (2)
Notes: 1 2 3 4 C only See C Shared-Object File Java only See Java Shared-Object File on page 5-66
Usage
If the IFX_EXTEND_ROLE configuration parameter is set to 1 or to ON, only users to whom the DBSA has granted the built-in EXTEND role are authorized to use this segment. The Database Server Administrator should include in the DB_LIBRARY_PATH configuration parameter settings every file system where the security policy authorizes DataBlade modules and UDRs to reside. Unless DB_LIBRARY_PATH is absent or has no setting, the database server cannot access a file that this segment specifies unless its pathname begins with a string that exactly matches one of the values of DB_LIBRARY_PATH. For example, if $INFORMIXDIR/extend is one of the DB_LIBRARY_PATH values on a Linux system, then shared-object files can have pathnames within the $INFORMIXDIR/extend file system or its subdirectories. (This is also the file system where built-in DataBlade modules reside, and the default location where the DataBlade Developer's Kit creates user-defined DataBlade modules.) The syntax by which you specify a shared-object filename depends on whether the external routine is written in the C language or in the Java language. Sections that follow describe each of these external languages.
C Shared-Object File
To specify the location of a C shared-object file, specify the path to the dynamically loaded executable file within a quoted pathname or as a variable.
5-65
Shared-Object Filename
C Shared-Object File:
quote $environment_var / . $variable pathname (symbol) quote
Description Platform-independent indicator Pathname to the file Either single ( ) or double ( ) quotation mark symbol Entry point to the file Platform-independent indicator
Restrictions Must begin with a dollar sign ( $ ) See notes that follow this table Opening and closing quotation mark symbols must match Must be enclosed in parentheses Must begin with a dollar sign ( $ )
Syntax Identifier on page 5-22 Identifier on page 5-22 Literal symbol (either or ) Identifier on page 5-22 Identifier on page 5-22
The following rules affect pathname and filename specifications in C: v A filename (with no pathname) can specify an internal function. v You can omit the period ( . ) symbol if pathname is relative to the current directory when the CREATE or ALTER statement is run. v On UNIX, an absolute pathname must begin with a slash ( / ) symbol, and each directory name must end with a slash ( / ) symbol. v On Windows, an absolute pathname must begin with a backslash ( \ ) symbol, and each directory name must end with a backslash ( \ ) symbol. v The filename at the end of pathname must have the .so file extension and must refer to an executable file in a shared object library. v Use a symbol only if the entry point to the dynamically loadable executable object file has a different name from the UDR that you are registering with CREATE FUNCTION or CREATE PROCEDURE. v If you specify a variable, it must contain the full pathname to the executable file. v You can include whitespace characters, such as blank spaces or tab characters, within a quoted pathname.
5-66
Shared-Object Filename
quote , ( java_type ) RETURNS java_type
Notes: 1
Element class_id java_type method_id package_id quote Description Java class whose method implements the UDR Java data type for a parameter in the Java-method signature Name of the Java method that implements the UDR
Name of package that contains the Must exist Java class Single ( ) or double ( ) quotation mark delimiters Opening and closing quotation marks must match
Before you can create a UDR written in the Java language, you must assign a jar identifier to the external jar file with the sqlj.install_jar procedure. (For more information, see sqlj.install_jar on page 2-339.) You can include the Java signature of the method that implements the UDR in the shared-object filename. v If you do not specify the Java signature, the routine manager determines the implicit Java signature from the SQL signature in the CREATE FUNCTION or CREATE PROCEDURE statement. It maps SQL data types to the corresponding Java data types with the JDBC and SQL-to-Java mappings. For information on mapping user-defined data types to Java data types, see sqlj.setUDTextName on page 2-342. v If you do specify the Java signature, the routine manager uses this explicit Java signature as the name of the Java method to use. For example, if the Java method explosiveReaction( ) implements the Java UDR sql_explosive_reaction( ) as discussed in sqlj.install_jar on page 2-339, its shared-object filename could be:
course_jar:Chemistry.explosiveReaction
The preceding shared-object filename provides an implicit Java signature. The following shared-object filename is the equivalent with an explicit Java signature:
course_jar:Chemistry.explosiveReaction(int)
Related Information
See the External Routine Reference on page 5-20 segment for the context in which a shared-object filename appears within EXTERNAL NAME clause of the ALTER FUNCTION, ALTER PROCEDURE, ALTER ROUTINE, CREATE FUNCTION, and CREATE PROCEDURE statements. For further information on the DB_LIBRARY_PATH parameter in the ONCONFIG file, see the IBM Informix Administrator's Reference.
Chapter 5. Other Syntax Segments
5-67
Specific Name
Use a specific name to declare an identifier for a UDR that is unique in the database or name space. Use the Specific Name segment whenever you see a reference to a specific name in a syntax diagram. Only Dynamic Server supports specific names.
Syntax
Specific Name:
specific_id owner .
Element owner
Restrictions No more than 32 bytes. Must be same as owner of function or procedure name of this UDR. See also Restrictions on the Owner Name on page 5-68. Must be no more than 128 bytes long. See also Restrictions on the Specific Name on page 5-68.
specific_id
Usage
A specific name is a unique identifier that the CREATE PROCEDURE or CREATE FUNCTION statement declares as an alternative name for a UDR. Because you can overload routines, a database can have more than one UDR with the same name and different parameter lists. You can assign a UDR a specific name that uniquely identifies the specific UDR. If you declare a specific name when you create the UDR, you can later use that name when you alter, drop, grant, or revoke privileges, or update statistics on that UDR. Otherwise, you need to include the parameter data types with the UDR name, if the name alone does not uniquely identify the UDR.
5-68
Statement Block
Use a statement block to specify SPL and SQL operations to take place when an SPL statement that includes this segment is executed.
Syntax
Statement Block:
(2)
(3) EXECUTE FUNCTION Statement (5) EXECUTE PROCEDURE Statement (6) Subset of SPL Statements (7) Subset of SQL Statements ; BEGIN Statement Block END
(4)
Notes: 1 2 3 4 5 6 7 See DEFINE on page 3-7 See ON EXCEPTION on page 3-31 Dynamic Server only See EXECUTE FUNCTION on page 2-329 See EXECUTE PROCEDURE on page 2-336 See Subset of SPL Statements Valid in the Statement Block See Subset of SQL Data Types on page 5-62
Usage
SPL and SQL statements can appear in a statement block, a set of zero or more statements that can define the scope of a variable or of the ON EXCEPTION statement of SPL. If the statement block is empty, no operation takes place when control of execution within the SPL routine passes to the empty SPL statement block.
5-69
Statement Block
For example, you cannot close the current database or select a new database within an SPL routine. Likewise you cannot drop the current SPL routine within the same routine. You can, however, drop another SPL routine. You can use a SELECT statement in only two cases: v You can use the INTO TEMP clause to put the results of the SELECT statement into a temporary table. v You can use the SELECT ... INTO form of the SELECT statement to put the resulting values into SPL variables. If an SPL routine is later to be called as part of a data-manipulation language (DML) statement, additional restrictions exist. For more information, see Restrictions on SPL Routines in Data-Manipulation Statements on page 5-71.
5-70
Statement Block
LET var2 = 2; INSERT INTO tracker (who_submitted, value) VALUES (var1 param before sub-block, var1); BEGIN DEFINE var1 INT; -- same name as global parameter. LET var1 = var2; INSERT INTO tracker (who_submitted, value) VALUES (var1 var defined inside the "IF/BEGIN"., var1); END INSERT INTO tracker (who_submitted, value) VALUES (var1 param after sub-block (unchanged!), var1); END PROCEDURE; EXECUTE PROCEDURE demo_local_var(); SELECT sequential_order, who_submitted, value FROM tracker ORDER BY sequential_order;
This example declares three variables, two of which are named var1. (Name conflicts are created here to illustrate which variables are visible. Using the same name for different variables is generally not recommended, because conflicting names of variables can make your code more difficult to read and to maintain.) Because of the statement block, only one var1 variable is in scope at a time. The var1 variable that is declared inside the statement block is the only var1 variable that can be referenced from within the statement block. The var1 variable that is declared outside the statement block is not visible within the statement block. Because it is out of scope, it is unaffected by the change in value to the var1 variable that takes place inside the statement block. After all the statements run, the outer var1 still has a value of 1. The var2 variable is visible within the statement block because it was not superseded by a name conflict with a block-specific variable.
5-71
Statement Block
DROP ACCESS_METHOD DROP AGGREGATE DROP INDEX DROP OPCLASS DROP OPTICAL CLUSTER DROP ROLE DROP ROW TYPE DROP SEQUENCE DROP SYNONYM DROP TABLE DROP TRIGGER DROP TYPE DROP VIEW INSERT RENAME COLUMN RENAME DATABASE RENAME SEQUENCE RENAME TABLE ROLLBACK WORK SET CONSTRAINTS TRUNCATE UPDATE
In Extended Parallel Server, if you call the SPL routine as part of a DML statement (namely, an INSERT, UPDATE, DELETE, MERGE, or SELECT statement), then the routine can execute only the following statements of SQL:
SELECT SET PLOAD FILE SET DEBUG FILE TO SET EXPLAIN SET OPTIMIZATION
For both Dynamic Server and Extended Parallel Server, these restrictions do not apply to an SPL routine that is invoked by a trigger, because in this case the SPL routine is not called by the DML statement, and therefore can include any SQL statement, such as UPDATE, INSERT and DELETE, that is not listed among the SQL Statements Not Valid in an SPL Statement Block on page 5-70.
5-72
A
ABSOLUTE ACCESS ACCESS_METHOD ACTIVE ADD AFTER AGGREGATE ALIGNMENT ALL ALL_ROWS ALLOCATE ALTER AND ANSI ANY APPEND AS ASC AT ATTACH AUDIT AUTHORIZATION AUTO AUTOFREE AVG AVOID_EXECUTE AVOID_SUBQF
A-1
B
BEFORE BEGIN BETWEEN BINARY BOOLEAN BOTH BUFFERED BUILTIN BY BYTE
C
CACHE CALL CANNOTHASH CARDINALITY CASCADE CASE CAST CHAR CHAR_LENGTH CHARACTER CHARACTER_LENGTH CHECK CLASS CLIENT CLOSE CLUSTER CLUSTERSIZE COARSE COBOL CODESET COLLATION COLLECTION COLUMN COMMIT COMMITTED COMMUTATOR CONCURRENT CONNECT CONNECTION CONST CONSTRAINT CONSTRAINTS CONSTRUCTOR CONTINUE COPY COSTFUNC COUNT CRCOLS CREATE CROSS CURRENT CURRENT_ROLE CURSOR CYCLE
D
DATABASE DATAFILES DATASKIP DATE DATETIME DAY DBA DBDATE DBPASSWORD DBSERVERNAME DEALLOCATE DEBUG DEC DEC_T DECIMAL DECLARE DECODE DEFAULT DEFAULT_ROLE DEFERRED DEFERRED_PREPARE DEFINE DELAY DELETE DELIMITER DELUXE DEREF DESC DESCRIBE DESCRIPTOR DETACH DIAGNOSTICS DIRECTIVES DIRTY DISABLED DISCONNECT DISTINCT DISTRIBUTEBINARY DISTRIBUTESREFERENCES DISTRIBUTIONS DOCUMENT DOMAIN DONOTDISTRIBUTE DORMANT DOUBLE DROP DTIME_T
E
EACH ELIF ELSE ENABLED ENCRYPTION END ENUM ENVIRONMENT ERROR ESCAPE EXCEPTION EXCLUSIVE EXEC EXECUTE EXECUTEANYWHERE EXISTS EXIT EXPLAIN EXPLICIT EXPRESS EXPRESSION EXTEND EXTENT EXTERNAL
A-2
F
FALSE FAR FETCH FILE FILLFACTOR FILTERING FIRST FIRST_ROWS FIXCHAR FIXED FLOAT FLUSH FOR FOREACH FOREIGN FORMAT FORTRAN FOUND FRACTION FRAGMENT FREE FROM FULL FUNCTION
G
GENERAL GET GK GLOBAL GO GOTO GRANT GROUP
H
HANDLESNULLS HASH HAVING HIGH HINT HOLD HOUR HYBRID
I
IF IFX_INT8_T IFX_LO_CREATE_SPEC_T IFX_LO_STAT_T IMMEDIATE IMPLICIT IN INACTIVE INCREMENT INDEX INDEXES INDICATOR INFORMIX INIT INITCAP INLINE INNER INOUT INSERT INSTEAD INT INT8 INTEG INTEGER INTERNAL INTERNALLENGTH INTERVAL INTO INTRVL_T IS ISCANONICAL ISOLATION ITEM ITERATOR
J-K
JOIN KEEP KEY
A-3
L
LABELEQ LABELGE LABELGLB LABELGT LABELLE LABELLT LABELLUB LABELTOSTRING LANGUAGE LAST LEADING LEFT LET LEVEL LIKE LIMIT LIST LISTING LOAD LOC_T LOCAL LOCATOR LOCK LOCKS LOG LONG LOW LOWER LVARCHAR
M
MATCHES MAX MAXERRORS MAXLEN MAXVALUE MDY MEDIAN MEDIUM MEMORY_RESIDENT MIDDLE MIN MINUTE MINVALUE MODE MODERATE MODIFY MODULE MONEY MONTH MOUNTING MULTISET
N
NAME NCHAR NEGATOR NEW NEXT NO NOCACHE NOCYCLE NOMAXVALUE NOMIGRATE NOMINVALUE NON_RESIDENT NONE NOORDER NORMAL NOT NOTEMPLATEARG NULL NUMERIC NVARCHAR NVL
O
OCTET_LENGTH OF OFF OLD ON ONLINE ONLY OPAQUE OPCLASS OPEN OPERATIONAL OPTCOMPIND OPTICAL OPTIMIZATION OPTION OR ORDER OUT OUTER OUTPUT
P
PAGE PARALLELIZABLE PARAMETER PARTITION PASCAL PASSEDBYVALUE PASSWORD PDQPRIORITY PERCALL_COST PLI PLOAD PRECISION PREPARE PREVIOUS PRIMARY PRIOR PRIVATE PRIVILEGES PROCEDURE PUBLIC PUT
A-4
R
RAISE RANGE RAW READ REAL RECORDEND REF REFERENCES REFERENCING REGISTER REJECTFILE RELATIVE RELEASE REMAINDER RENAME REOPTIMIZATION REPEATABLE REPLICATION RESERVE RESOLUTION RESOURCE RESTART RESTRICT RESUME RETAIN RETURN RETURNING RETURNS REUSE REVOKE RIGHT ROBIN ROLE ROLLBACK ROLLFORWARD ROUND ROUTINE ROW ROWID ROWIDS ROWS
S
SAMEAS SAMPLES SAVE SCHEDULE SCHEMA SCRATCH SCROLL SECOND SECONDARY SECTION SELCONST SELECT SELFUNC SEQUENCE SERIAL SERIAL8 SERIALIZABLE SERVERUUID SESSION SET SHARE SHORT SIGNED SITENAME SIZE SKALL SKINHIBIT SKIP SKSHOW SMALLFLOAT SMALLINT SOME SPECIFIC SQL SQLCODE SQLCONTEXT SQLERROR SQLSTATE SQLWARNING STABILITY STACK STANDARD START STATIC STATISTICS STDEV STEP STOP STORAGE STRATEGIES STRING STRINGTOLABEL STRUCT STYLE SUBSTR SUBSTRING SUM SUPPORT SYNC SYNONYM SYSTEM
T
TABLE TEMP TEMPLATE TEST TEXT THEN TIME TIMEOUT TO TODAY TRACE TRAILING TRANSACTION TRIGGER TRIGGERS TRIM TRUE TRUNCATE TYPE TYPEDEF TYPEID TYPENAME TYPEOF
A-5
U
UNCOMMITTED UNDER UNION UNIQUE UNITS UNKNOWN UNLOAD UNLOCK UNSIGNED UPDATE UPPER USAGE USE_SUBQF USER USING
V
VALUE VALUES VAR VARCHAR VARIABLE VARIANCE VARIANT VARYING VIEW VIOLATIONS VOID VOLATILE
W-Z
WAIT WARNING WHEN WHENEVER WHERE WHILE WITH WITHOUT WORK WRITE XADATASOURCE XID XLOAD XUNLOAD YEAR
A-6
A
ADD AFTER ALL ALL_MUTABLES ALTER AND ANSI ANY APPEND AS ASC AT ATTACH AUDIT AUTHORIZATION AVG
B-1
B
BEFORE BEGIN BETWEEN BINARY BITMAP BOTH BOUND_IMPL_PDQ BUFFERED BY BYTE
C
CACHE CALL CASCADE CASE CHAR CHAR_LENGTH CHARACTER CHARACTER_LENGTH CHECK CLIENT_TZ CLOSE CLUSTER CLUSTERSIZE COARSE COBOL CODESET COLUMN COMMIT COMMITTED COMPUTE_QUOTA CONNECT CONSTRAINT CONSTRAINTS CONTINUE COPY COUNT CREATE CURRENT CURRENT_ROLE CURSOR
D
DATABASE DATAFILES DATASKIP DATE DATETIME DAY DBA DBDATE DBSERVERNAME DEBUG DEC DECIMAL DECLARE DECODE DEFAULT DEFAULT_ROLE DEFERRED DEFINE DELETE DELIMITER DELUXE DESC DETACH DIRTY DISCONNECT DISTINCT DISTRIBUTIONS DOCUMENT DOUBLE DROP
E
EACH ELIF ELSE END ENVIRONMENT ERROR ESCAPE EXCEPTION EXCLUSIVE EXEC EXECUTE EXISTS EXIT EXPLAIN EXPRESS EXPRESSION EXTEND EXTENT EXTERNAL
F
FETCH FILE FILLFACTOR FILTERING FIRST FLOAT FOR FOREACH FOREIGN FORMAT FORTRAN FOUND FRACTION FRAGMENT FROM FUNCTION
B-2
G
GK GLOBAL GO GOTO GRANT GROUP
H
HASH HAVING HIGH HOLD HOUR HYBRID
I
IF IMMEDIATE IMMUTABLE IMPLICIT_PDQ IN INDEX INDICATOR INIT INITCAP INNER INSERT INT INT8 INTEGER INTERVAL INTO IS ISOLATION
J-K
JOIN KEY
L
LABELEQ LABELGE LABELGT LABELLE LABELLT LANGUAGE LEADING LEFT LET LEVEL LIKE LISTING LOAD LOCAL LOCK LOCKS LOG LOW LOWER
M
MATCHES MAX MAXERRORS MAXSCAN MEDIUM MEMORY_RESIDENT MERGE MIDDLE MIN MINUTE MODE MODIFY MODULE MONEY MONTH MOUNTING MOVE MUTABLE
N
NCHAR NEW NEXT NO NON_RESIDENT NONE NORMAL NOT NULL NUMERIC NVARCHAR NVL
B-3
O
OCTET_LENGTH OF OFF OLD ON ONLY OPEN OPERATIONAL OPTICAL OPTIMIZATION OPTION OR ORDER OUTER
P
PAGE PASCAL PDQPRIORITY PLI PLOAD PRECISION PREPARE PRIMARY PRIVATE PRIVILEGES PROCEDURE PUBLIC
R
RAISE RANGE RAW READ REAL RECORDEND RECOVER REFERENCES REFERENCING REJECTFILE RELEASE REMAINDER RENAME REPEATABLE RESERVE RESOLUTION RESOURCE RESTRICT RESUME RETAIN RETURN RETURNING RETURNS REVOKE RIDLIST ROBIN ROLLBACK ROLLFORWARD ROLE ROUND ROW ROWS
S
SAMEAS SAMPLES SCHEDULE SCHEMA SCRATCH SCROLL SECOND SECTION SELECT SERIAL SERIAL8 SERIALIZABLE SET SHARE SITENAME SIZE SKALL SKINHIBIT SKSHOW SMALLFLOAT SMALLINT SOME SQL SQLCODE SQLERROR SQLSTATE SQLWARNING STANDARD START STATIC STATISTICS STDEV STEP STOP SUBSTR SUBSTRING SUM SYNC SYNONYM SYSTEM
B-4
T
TABLE TEMP TEMP_TABLE_EXT_SIZE TEMP_TABLE_NEXT_SIZE TEXT THEN TIMEOUT TMPSPACE_LIMIT TO TODAY TRACE TRAILING TRANSACTION TRIGGER TRIM TRUNC TRUNCATE TYPE
U
UNCOMMITTED UNION UNIQUE UNITS UNLOAD UNLOCK UPDATE UPPER USER USING
V
VALUES VARCHAR VARIANCE VARIANT VARYING VIEW VIOLATIONS
W
WAIT WARNING WHEN WHENEVER WHERE WHILE WITH WORK WRITE
X-Z
XLOAD XUNLOAD YEAR
B-5
B-6
Appendix C. Accessibility
The syntax diagrams in the HTML version of this manual are available in dotted decimal syntax format, which is an accessible format that is available only if you are using a screen reader.
C-1
by the ? symbol indicates that all the syntax elements with a corresponding dotted decimal number, and any subordinate syntax elements, are optional. If there is only one syntax element with a dotted decimal number, the ? symbol is displayed on the same line as the syntax element (for example, 5? NOTIFY). If there is more than one syntax element with a dotted decimal number, the ? symbol is displayed on a line by itself, followed by the syntax elements that are optional. For example, if you hear the lines 5 ?, 5 NOTIFY, and 5 UPDATE, you know that syntax elements NOTIFY and UPDATE are optional; that is, you can choose one or none of them. The ? symbol is equivalent to a bypass line in a railroad diagram. ! Specifies a default syntax element. A dotted decimal number followed by the ! symbol and a syntax element indicates that the syntax element is the default option for all syntax elements that share the same dotted decimal number. Only one of the syntax elements that share the same dotted decimal number can specify a ! symbol. For example, if you hear the lines 2? FILE, 2.1! (KEEP), and 2.1 (DELETE), you know that (KEEP) is the default option for the FILE keyword. In this example, if you include the FILE keyword but do not specify an option, default option KEEP is applied. A default option also applies to the next higher dotted decimal number. In this example, if the FILE keyword is omitted, default FILE(KEEP) is used. However, if you hear the lines 2? FILE, 2.1, 2.1.1! (KEEP), and 2.1.1 (DELETE), the default option KEEP only applies to the next higher dotted decimal number, 2.1 (which does not have an associated keyword), and does not apply to 2? FILE. Nothing is used if the keyword FILE is omitted. Specifies a syntax element that can be repeated zero or more times. A dotted decimal number followed by the * symbol indicates that this syntax element can be used zero or more times; that is, it is optional and can be repeated. For example, if you hear the line 5.1* data-area, you know that you can include more than one data area or you can include none. If you hear the lines 3*, 3 HOST, and 3 STATE, you know that you can include HOST, STATE, both together, or nothing. Notes: 1. If a dotted decimal number has an asterisk (*) next to it and there is only one item with that dotted decimal number, you can repeat that same item more than once. 2. If a dotted decimal number has an asterisk next to it and several items have that dotted decimal number, you can use more than one item from the list, but you cannot use the items more than once each. In the previous example, you could write HOST STATE, but you could not write HOST HOST. 3. The * symbol is equivalent to a loop-back line in a railroad syntax diagram. + Specifies a syntax element that must be included one or more times. A dotted decimal number followed by the + symbol indicates that this syntax element must be included one or more times. For example, if you hear the line 6.1+ data-area, you must include at least one data area. If you hear the lines 2+, 2 HOST, and 2 STATE, you know that you must include HOST, STATE, or both. As for the * symbol, you can only repeat a particular item if it is the only item with that dotted decimal number. The + symbol, like the * symbol, is equivalent to a loop-back line in a railroad syntax diagram.
C-2
Notices
IBM may not offer the products, services, or features discussed in this document in all countries. Consult your local IBM representative for information on the products and services currently available in your area. Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM product, program, or service may be used. Any functionally equivalent product, program, or service that does not infringe any IBM intellectual property right may be used instead. However, it is the users responsibility to evaluate and verify the operation of any non-IBM product, program, or service. IBM may have patents or pending patent applications covering subject matter described in this document. The furnishing of this document does not give you any license to these patents. You can send license inquiries, in writing, to: IBM Director of Licensing IBM Corporation North Castle Drive Armonk, NY 10504-1785 U.S.A. For license inquiries regarding double-byte (DBCS) information, contact the IBM Intellectual Property Department in your country or send inquiries, in writing, to: IBM World Trade Asia Corporation Licensing 2-31 Roppongi 3-chome, Minato-ku Tokyo 106-0032, Japan The following paragraph does not apply to the United Kingdom or any other country where such provisions are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THIS PUBLICATION AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer of express or implied warranties in certain transactions, therefore, this statement may not apply to you. This information could include technical inaccuracies or typographical errors. Changes are periodically made to the information herein; these changes will be incorporated in new editions of the publication. IBM may make improvements and/or changes in the product(s) and/or the program(s) described in this publication at any time without notice. Any references in this information to non-IBM Web sites are provided for convenience only and do not in any manner serve as an endorsement of those Web sites. The materials at those Web sites are not part of the materials for this IBM product and use of those Web sites is at your own risk. IBM may use or distribute any of the information you supply in any way it believes appropriate without incurring any obligation to you. Licensees of this program who wish to have information about it for the purpose of enabling: (i) the exchange of information between independently created
Copyright IBM Corp. 1996, 2005
D-1
programs and other programs (including this one) and (ii) the mutual use of the information which has been exchanged, should contact: IBM Corporation J46A/G4 555 Bailey Avenue San Jose, CA 95141-1003 U.S.A. Such information may be available, subject to appropriate terms and conditions, including in some cases, payment of a fee. The licensed program described in this information and all licensed material available for it are provided by IBM under terms of the IBM Customer Agreement, IBM International Program License Agreement, or any equivalent agreement between us. Any performance data contained herein was determined in a controlled environment. Therefore, the results obtained in other operating environments may vary significantly. Some measurements may have been made on development-level systems and there is no guarantee that these measurements will be the same on generally available systems. Furthermore, some measurements may have been estimated through extrapolation. Actual results may vary. Users of this document should verify the applicable data for their specific environment. Information concerning non-IBM products was obtained from the suppliers of those products, their published announcements or other publicly available sources. IBM has not tested those products and cannot confirm the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on the capabilities of non-IBM products should be addressed to the suppliers of those products. All statements regarding IBMs future direction or intent are subject to change or withdrawal without notice, and represent goals and objectives only. All IBM prices shown are IBMs suggested retail prices, are current and are subject to change without notice. Dealer prices may vary. This information contains examples of data and reports used in daily business operations. To illustrate them as completely as possible, the examples include the names of individuals, companies, brands, and products. All of these names are fictitious and any similarity to the names and addresses used by an actual business enterprise is entirely coincidental. COPYRIGHT LICENSE: This information contains sample application programs in source language, which illustrate programming techniques on various operating platforms. You may copy, modify, and distribute these sample programs in any form without payment to IBM, for the purposes of developing, using, marketing or distributing application programs conforming to the application programming interface for the operating platform for which the sample programs are written. These examples have not been thoroughly tested under all conditions. IBM, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. You may copy, modify, and distribute these sample programs in any form without payment to IBM for the purposes of developing, using, marketing, or distributing application programs conforming to IBMs application programming interfaces.
D-2
Each copy or any portion of these sample programs or any derivative work, must include a copyright notice as follows: (your company name) (year). Portions of this code are derived from IBM Corp. Sample Programs. Copyright IBM Corp. (enter the year or years). All rights reserved. If you are viewing this information softcopy, the photographs and color illustrations may not appear.
Trademarks
AIX; DB2; DB2 Universal Database; Distributed Relational Database Architecture; NUMA-Q; OS/2, OS/390, and OS/400; IBM Informix; C-ISAM; Foundation.2000; IBM Informix 4GL; IBM InformixDataBladeModule; Client SDK; Cloudscape; Cloudsync; IBM InformixConnect; IBM InformixDriver for JDBC; Dynamic Connect; IBM InformixDynamic Scalable Architecture(DSA); IBM InformixDynamic Server; IBM InformixEnterprise Gateway Manager (Enterprise Gateway Manager); IBM InformixExtended Parallel Server; i.Financial Services; J/Foundation; MaxConnect; Object Translator; Red Brick; IBM Informix SE; IBM Informix SQL; InformiXML; RedBack; SystemBuilder; U2; UniData; UniVerse; wintegrateare trademarks or registered trademarks of International Business Machines Corporation. Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Windows, Windows NT, and Excel are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. UNIX is a registered trademark in the United States and other countries licensed exclusively through X/Open Company Limited. Other company, product, and service names used in this publication may be trademarks or service marks of others.
Notices
D-3
D-4
A
ABS function 4-69, 4-70 ABSOLUTE keyword, in FETCH statement 2-344 Access control See Privilege. ACCESS keyword in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199 in INFO statement 2-393 Access method attributes 5-46 configuring 5-46 default operator class, assigning 5-47 defined 5-46 index 2-117 modifying 2-9 primary 2-202 privileges 2-9, 2-82 privileges needed to drop 2-294 purpose options 5-46 registering 2-82 secondary 2-117 specifying in CREATE TABLE 2-202 sysams system catalog table settings 5-47 Access mode for transactions 2-604 ACCESS_METHOD keyword in ALTER ACCESS_METHOD statement 2-9 in CREATE ACCESS_METHOD statement 2-82 in DROP ACCESS_METHOD statement 2-294 Accessibility xxii dotted decimal format of syntax diagrams C-1 syntax diagrams, reading in a screen reader C-1 ACOS function 4-100, 4-101 Action clause, in CREATE TRIGGER statement action list 2-231 syntax 2-226 Active connection 2-292 ACTIVE keyword, in SAVE EXTERNAL DIRECTIVES statement 2-476 Active set constructing with OPEN statement 2-425 empty 2-351
X-1
Active set (continued) retrieving with FETCH 2-346 sequential cursor 2-267 ADD CONSTRAINT keywords in ALTER TABLE statement 2-59 ADD keyword in ALTER ACCESS_METHOD statement 2-9 in ALTER FRAGMENT statement 2-26 in ALTER FUNCTION statement 2-31 in ALTER PROCEDURE statement 2-35 in ALTER ROUTINE statement 2-37 in ALTER TABLE statement 2-45, 2-64 ADD ROWIDS keywords in ALTER TABLE statement 2-45 ADD TYPE keywords in ALTER TABLE statement 2-64 AFTER keyword in ALTER FRAGMENT statement 2-13, 2-26 in CREATE TRIGGER statement 2-226, 2-231 Aggregate functions arguments 4-118 as arguments 5-3 AVG 4-118 COUNT 4-119 in ESQL 4-124 in EXISTS subquery 4-15 in expressions 2-487 in GROUP BY Clause 2-513 in ORDER BY clause 2-516 in SELECT statement 2-488 MAX 4-122 MIN 4-122 RANGE 4-122 STDEV 4-123 SUM 4-122 summary 4-124 syntax AVG 4-116 COUNT 4-116 MAX 4-116 MIN 4-116 SUM 4-116 using DISTINCT 4-118 VARIANCE 4-123 AGGREGATE keyword in CREATE AGGREGATE statement 2-84 in DROP AGGREGATE statement 2-295 Algebraic functions ABS 4-70 MOD 4-70 POW 4-70 ROOT 4-70 ROUND 4-71 SQRT 4-71 TRUNC 4-71 Alias for a collection in SELECT statement 3-23 for a table in SELECT statement 2-494, 5-5 in optimizer directives 5-35 ALIGNMENT keyword, in CREATE OPAQUE TYPE statement 2-137 ALL keyword beginning a subquery 2-510 in Condition segment 4-15 in CREATE INDEX statement 2-132 in CREATE VIEW statements 2-249 in DISCONNECT statement 2-291
ALL keyword (continued) in Expression segment 4-116, 4-125 in GRANT FRAGMENT statement 2-389 in GRANT statement 2-383 in REVOKE FRAGMENT statement 2-472 in REVOKE statement 2-459, 2-466 in SELECT statement 2-486 in SET Transaction Mode statement 2-538, 2-606 with UNION operator 2-479, 2-525 ALL_MUTABLES keyword, in SET ALL_MUTABLES statement 2-527 ALL_ROWS keyword in Optimizer Directives Segment 5-40 in SET OPTIMIZATION statement 2-584 ALLOCATE COLLECTION statement 2-4 ALLOCATE DESCRIPTOR statement 2-6 ALLOCATE ROW statement 2-8 Allocating memory for a collection variable 2-4 for system-descriptor area 2-6 ALLOW_NEWLINE configuration parameter 4-143 Allowing newline characters in quoted strings 4-143 ALPHA class 4-109 ALS. See Global Language Support. ALTER ACCESS_METHOD statement 2-9 ALTER FRAGMENT statement am_readwrite purpose flag 5-48 hybrid fragmentation 2-20 INIT clause 2-20 privileges required 2-12 restrictions 2-12 reverting to nonfragmented table with DETACH 2-20 with INIT 2-22 syntax 2-11 with generalized-key index 2-21 ALTER INDEX statement reclustering a table 2-33 specifying lock modes 2-34 ALTER keyword in GRANT statement 2-376, 2-382 in REVOKE statement 2-459, 2-465 Alter privilege 2-376 ALTER privilege 2-97, 2-460 ALTER SEQUENCE statement 2-40 ALTER TABLE statement adding rowids 2-45 cascading deletes 2-50 changing column data type 2-53 dropping a column 2-52 privileges needed 2-43 restrictions ADD clause 2-46 DROP clause 2-52 general 2-43 MODIFY clause 2-54 am_cluster purpose flag defined 5-48 am_defopclass purpose value defined 5-47 am_keyscan purpose flag defined 5-48 am_parallel purpose flag defined 5-48 am_readwrite purpose flag defined 5-48
X-2
am_rowids purpose flag defined 5-48 am_sptype purpose value defined 5-47 am_unique purpose flag defined 5-48 American National Standards Institute. See ANSI compliance. AND keyword in Condition segment 4-5, 4-17 in CREATE INDEX statement 2-134 with BETWEEN keyword 2-508 ANSI compliance -ansi compilation flag 2-262 -ansi flag 2-163, 2-172, 2-209 comment symbols 1-4 creating views 2-248 list of SQL statements 1-8 privileges on UDRs 2-381 renaming a table 2-455 table privileges 2-389 unbuffered logging 2-582 ansi flag 2-91 ANSI keyword in CREATE DATABASE statement 2-90 ANSI-compliance escape character 4-11 implicit transactions 2-596 isolation level 2-350, 2-604 owner names 5-17 SQLSTATE codes 2-363 update cursors 2-264, 2-265, 2-278 warning after DELETE 2-280 ANSI-compliant database creating 2-91 database object naming 5-44 implicit transactions 2-276 opaque-type naming 2-136 procedure name 2-656 table privileges 2-206 upshifting owner names 5-45 warning after opening 2-255 with BEGIN WORK 2-66 ANSIOWNER environment variable 1-1, 5-45 ANY keyword in Condition segment 4-15 in SELECT statement 2-510 APPEND keyword in SET DEBUG FILE statement 2-547 in SET EXPLAIN statement 2-568 in SET PLOAD FILE statement 2-589 Application comment indicators 1-5 single-threaded 2-535 thread-safe 2-292, 2-535, 2-537 Application partitioning. 2-495 Arguments 5-2 Arithmetic functions. See Algebraic functions. Arithmetic operators binary 4-40 syntax 4-34 unary 4-41 Array, with FETCH 2-348 AS keyword in ALTER FRAGMENT statement 2-13 in Collection-Derived-Table segment 5-5
AS keyword (continued) in CONNECT statement 2-75 in CREATE CAST statement 2-87 in CREATE DISTINCT TYPE statement 2-93 in CREATE INDEX statement 2-133 in CREATE TRIGGER statement 2-218 Delete triggers 2-229 Insert triggers 2-229 SELECT triggers 2-231 Update triggers 2-230 view column values 2-243 in CREATE VIEW statement 2-247 in DROP CAST statement 2-296 in explicit casts 4-42 in GRANT FRAGMENT statement 2-388 in Iterator segment 2-499 in MERGE statement 2-416 in REVOKE statement 2-457, 2-469 in SELECT statement Projection clause 2-482 with display labels 2-489 with table aliases 2-494 AS PARTITION keywords, in ALTER FRAGMENT statement 2-13 AS REMAINDER keywords, in ALTER FRAGMENT statement 2-13 ASC keyword in CREATE INDEX statement 2-118 in SELECT statement 2-515, 2-517 order with nulls 2-517 Ascending sequence 2-41, 2-166 ASCII code set 4-148 ASCII keyword in CREATE EXTERNAL TABLE statement 2-103 in SELECT statement 2-524 ASIN function 4-100, 4-101 Assign support function 2-139, 2-401, 2-409, 2-411, 2-645 Associated statement 2-530 Asterisk (*) argument to COUNT function 4-119 arithmetic operator 4-34 as wildcard character 4-12 in C-style comment indicators 1-4 in projection list 3-22 Projection clause 2-481 Asterisk (*) symbol in comment indicators 1-4 AT keyword in INSERT statement 2-395 At symbol ( @ ) 5-15 ATAN function 4-100, 4-102 ATAN2 function 4-100, 4-102 ATTACH keyword, in ALTER FRAGMENT statement 2-13 Attached indexes 2-96, 2-127, 2-299, 2-532 Audit-event mnemonics 4-80 Authorization identifier 2-155, 2-307, 2-595, 5-43 AUTHORIZATION keyword in CREATE SCHEMA statement 2-162 in SET SESSION AUTHORIZATION statement 2-595 Autofree feature, in SET AUTOFREE 2-529 AVG function 4-116, 4-118 AVOID HASH keyword, in Optimizer Directives segment 5-38 AVOID_EXECUTE keyword in SET EXPLAIN statement 2-568 optimizer directive 5-41 AVOID_FULL keyword, in Optimizer Directives segment 5-35 Index
X-3
AVOID_INDEX keyword, in Optimizer Directives segment 5-35 AVOID_NL keyword, in Optimizer Directives segment
5-38
B
B-tree cleaner list 2-651 B-tree index 2-48 btree_ops operator class 2-144 default operator class 2-144 detached 2-127 uses 2-124 B-tree secondary-access method 2-124, 2-142 Background mode 3-39 Backslash (\) as escape character 4-12 as wildcard character 4-11 escape character 2-633 Base-100 format 4-23 BASE64 encoding of encrypted data 4-81 BASE64 encryption format 2-560 Batch file 3-40 BEFORE keyword in ALTER FRAGMENT statement 2-13, 2-26 in ALTER TABLE statement 2-45 in CREATE TRIGGER statement 2-226, 2-231 BEGIN keyword in IF statement 3-27 in Statement Block segment 5-69 BEGIN WORK statement 2-66 BETWEEN keyword in Condition segment 4-6, 4-8 Big-endian format 2-100 BigDecimal data type of Java 4-25, 5-62 BINARY keyword in CREATE EXTERNAL TABLE statement 2-99 Binary operator 2-85, 4-40 Binary-format integers 2-100 Bit-hashing function 2-513 BITMAP keyword, in CREATE INDEX statement 2-116 Blank characters DATETIME separator 4-133 in index names 2-302 in literal numbers 4-137 INTERVAL separator 4-135 BLOB data type 4-26 copying to a file 4-93 copying to a smart large object 4-95 creating from a file 4-91 declaration syntax 4-25 default value 2-175 handle values 4-48 size limit 4-26 storing 2-58, 2-199 unloading 2-631, 2-632 BLOB keyword in Data Type segment 4-25 in DEFINE statement 3-7 Blobspace 2-20 Boldface type xiv BOOLEAN data type defined 4-19 literal values 2-175 unloading 2-631 Boolean expression 4-5 BOTH keyword, in TRIM expressions 4-102
BOUND_IMPL_PDQ keyword in SET ENVIRONMENT statement 2-564 Braces ( { } ) collection delimiters 4-129 comment indicator 1-4, 5-35 specifying empty collection 4-65 Brackets ( [ ] ) as range delimiters 4-12 BROADCAST optimizer directive 5-39 BUFFERED keyword in CREATE DATABASE statement 2-90 in SET LOG statement 2-582 BUFFERED LOG keyword in CREATE DATABASE 2-91 Buffered logging 2-582 Built-in aggregates contrasted with user-defined 2-85 defined 4-117 extending 2-85 Built-in data types opaque 4-19 owner 2-93 privileges on 2-379 syntax 4-18 Built-in routines jvpcontrol 2-333 sqlj.alter_java_path 2-341 sqlj.install_jar 2-339 sqlj.remove_jar 2-340 sqlj.replace_jar 2-340 sqlj.unsetUDTExtName 2-342 Built-in secondary-access method 2-124 BY keyword in ALTER FRAGMENT statement 2-24 in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-166 in SELECT statement 2-513, 2-515 BYTE and TEXT columns, fragment storage 2-16, 2-20 BYTE column modifying 2-55 BYTE data effect of isolation on retrieval 2-578, 2-605 loading 2-408 storage location 4-25 unloading 2-631, 2-632 BYTE data type declaration syntax 4-25 default value 2-175 with SET DESCRIPTOR 2-559 with SPL routines 3-8, 3-15 BYTE keyword in Data Type segment 4-25 in DEFINE statement 3-7 in Return Clause segment 5-51
C
C keyword, in External Routine Reference segment C++ API 4-143 Cache cohesion 2-598 CACHE keyword in ALTER SEQUENCE statement 2-42 in CREATE SEQUENCE statement 2-167 in SET STATEMENT CACHE statement 2-597 Calculated expression restrictions with GROUP BY 2-513 Calendar 4-53 5-20
X-4
CALL keyword in WHENEVER statement 2-659 CALL keyword, in WHENEVER statement 2-659 CALL statement 3-2 Callbacks 5-58 CANNOTHASH keyword, in CREATE OPAQUE TYPE statement 2-137 CANNOTHASH modifier 2-513 CARDINALITY function 4-72 Caret (^) as wildcard character 4-12 use with square brackets 4-12 Cartesian product 5-37 CASCADE keyword in ALTER TABLE statement 2-49 in CREATE TABLE statement 2-179 in DROP TABLE statement 2-314 in DROP VIEW statement 2-318 in MOVE TABLE statement 2-419 in REVOKE statement 2-456 Cascading deletes 2-50 CREATE TABLE with 2-50 locking associated with 2-277 logging 2-277 multiple child tables 2-277 Cascading triggers and triggering table 2-237 triggered actions 2-228 Case conversion functions INITCAP 4-109 LOWER 4-109 UPPER 4-109 CASE keyword in Expression segment 4-50, 4-51 CASE statement 3-4 CAST keyword in DROP CAST statement 2-296 in explicit casts 4-42 Casts built-in 2-88, 2-296 creating 2-87 dropping 2-296 explicit 2-88, 4-42 function for 2-89 implicit 2-88, 4-42 operator ( :: ) 2-88, 4-42 privileges 2-87 registering 2-87 symbol 4-42 cdrserver shadow column 2-45 cdrserver, replication column name 2-188 cdrtime shadow column 2-45 cdrtime, replication column name 2-188 Chaining synonyms 2-170 CHAR data type defined 4-19, 4-20 in INSERT 4-145 CHAR_LENGTH function 4-89, 4-90 Character data types fixed and varying length 4-19, 4-20 syntax 4-19 CHARACTER VARYING data type See also $INFORMIXDIR/etc/sqlhostsVARCHAR data type. syntax 4-19 CHARACTER_LENGTH function 4-89, 4-90
Check constraints defining 2-181 moving 2-421 reject files 2-104 unloading data 2-104 CHECK keyword in ALTER TABLE statement 2-51 in CREATE EXTERNAL TABLE statement 2-101 in CREATE TABLE statement 2-181 in CREATE VIEW statement 2-247 Child table 2-421 CLASS keyword, in Routine Modifier segment 5-55 CLASS_ORIGIN keyword, in GET DIAGNOSTICS statement 2-366 Client APIs of Dynamic Server 4-143 CLIENT_TZ keyword in SET ENVIRONMENT statement 2-564, 4-58, 4-59 CLIENTBINVAL data type 4-19 CLOB data type copying to a file 4-93 copying to a smart large object 4-95 creating from a file 4-91 default value 2-175 handle values 4-48 size limit 4-26 unloading 2-631, 2-632 CLOB keyword in Data Type segment 4-25 in DEFINE statement 3-7 CLOSE DATABASE statement prerequisites to close 2-71 syntax 2-71 CLOSE statement closing a collection cursor 2-69 closing a select cursor 2-68 closing an insert cursor 2-68 cursors affected by transaction end 2-69 syntax 2-68 CLUSTER keyword in ALTER INDEX statement 2-33 in CREATE INDEX statement 2-117 Clustered index 2-33, 2-118 Clustering, specifying support for 5-48 Co-locality 2-129 COARSE keyword in ALTER INDEX statement 2-33 in CREATE INDEX statement 2-132 Code points, ASCII 4-148 Code sets, ISO 8859-1 x Code, sample, conventions for xviii CODESET keyword in CREATE EXTERNAL TABLE statement 2-103 in SELECT statement 2-524 Cogroup name 2-102 Collating order 2-531 Collation with relational operators 4-147 COLLATION keyword, in SET COLLATION statement 2-531 Collection constructors example 4-66 restrictions 4-66 Collection cursor closing 2-69 DECLARE for ESQL/C variable 2-271 declaring 3-22 defined 2-271 in SPL 3-22 Index
X-5
Collection cursor (continued) inserting into 2-350, 2-445 INTO clause 2-350 opening 2-427 Collection data type 4-30 allocating memory 2-4 defining a column 4-30 deleting 2-279 element, searching for with IN 4-10 IN operator 4-10 LIST 4-30 loading 2-411 MULTISET 4-30 returning number of elements 4-72 selecting from 2-497 SET 4-30 unloading 2-631 updating 5-10 COLLECTION keyword in ALLOCATE COLLECTION statement 2-4 in DEALLOCATE COLLECTION statement 2-257 in DEFINE statement 3-7 untyped collection variable 2-497 Collection Subquery segment 4-3 Collection variable accessing 5-10 accessing values 5-10 associating cursor with 2-271 cursor for 2-350 deallocating memory for 2-257 in SELECT statement 2-497 manipulating values 5-10 opening a cursor 2-427 selecting from 2-497 selecting, inserting elements 2-271 untyped 2-4, 2-257 updating 2-351, 5-10 with DESCRIBE INPUT statement 2-289 with DESCRIBE statement 2-284 Collection-derived table 5-5 collection cursor 2-273, 2-350, 2-445 collection variables with 5-10 FOREACH statement 3-23 in SELECT statement 5-10 INSERT statement with 2-273, 2-405 row types in 5-7 row variables with 5-14 SELECT statement with 2-497 fields from row variable 2-498 TABLE keyword 5-6, 5-10, 5-14 UPDATE statement with 2-647, 5-10, 5-14 where allowed 5-14 Collections accessing a nested collection 5-14 accessing elements 5-6 allocating memory 2-4 constructors 4-65 deleting elements from 2-279 example of deleting elements 5-11 example of inserting elements 5-13 example of updating 5-12 generating values for 4-65 inserting values into 2-401 nested 4-130 restrictions when accessing elements 5-6 restrictions when defining 4-31
Collections (continued) restrictions with inserting null values 2-401 selecting from 2-497 specifying literal values 4-130 updating 2-644 Colon symbol (:) cast operator ( :: ) 4-42 DATETIME separator 4-133 INTERVAL separator 4-135 with database qualifier 5-18 Column definition clause 2-173 Column expression 2-521 COLUMN keyword, in RENAME COLUMN statement Column level encryption 4-80 Column name dot notation 4-45 using functions as names 5-25, 5-26 using keywords as names 5-26 Column substring 4-47 Column-level encryption 2-562 Columns adding 2-45 adding a NOT NULL constraint 2-56 changing the data type 2-56 check constraints 2-101 defining as primary key 2-178 dropping 2-52 expression 2-487, 4-44 inserting into 2-396 modifying with ALTER TABLE 2-53 number, effect on triggers 2-223 order in Projection list 2-483 primary or foreign key 2-178 privileges 2-376 projection 4-45 referenced and referencing 2-179 removing a NOT NULL constraint 2-56 renaming 2-449 specifying a subscript 2-516, 4-47 specifying check constraint for 2-181 varying-length 4-21 virtual 2-249 COLUMNS keyword, in INFO statement 2-393 COMBINE keyword in CREATE AGGREGATE statement 2-84 Command file 1-4 Command-line conventions how to read xvi sample diagram xvi Comment symbol braces ( { } ) 1-4 double hyphen (--) 1-4 in application programs 1-5 in optimizer directives 5-34 in prepared statements 2-434 slash and asterisk (/* */) 1-4, 2-633 slash-and-asterisk ( / * */ ) 1-4 COMMIT WORK statement in ANSI-compliant databases 2-74 in non-ANSI databases 2-73 syntax 2-73 COMMITTED keyword in SET ISOLATION statement 2-577 in SET TRANSACTION statement 2-603 COMMITTED keyword, in SET TRANSACTION statement 2-602 Committed Read isolation level 2-577
2-449
X-6
COMMITTED READ keywords, in SET ISOLATION statement 2-575 Compacted index 2-125 Compare support function 2-139 Complete-connection level settings of SET EXPLAIN 2-572 of SET ISOLATION 2-576 of SET LOCK MODE 2-581 Complex data type loading element values 2-411 unloading 2-633 Complex numbers 4-25 Complex table expression 2-494 Complex view 2-239 Compliance with industry standards xxiv Composite key 2-187 Compound assignment 3-28 COMPUTE_QUOTA keyword in SET ENVIRONMENT statement 2-565 concat() operator function 4-42 Concatenation operator ( | | ) 4-34, 4-41 Concurrency with CREATE INDEX 2-134 with DROP INDEX 2-303 with SET ISOLATION 2-575 with SET TRANSACTION 2-604 with START VIOLATIONS TABLE 2-610 CONCURRENT keyword, in CONNECT statement 2-75 Condition segment join conditions 2-511 keywords ALL 4-15 ANY 4-15 BETWEEN 4-8 EXISTS 4-15 IS NOT NULL 4-10 IS NULL 4-10 LIKE 4-11 MATCHES 4-11 NOT 4-11 SOME 4-15 null values 4-16 subquery in SELECT 4-13 syntax 4-5 Conditional expressions CASE 4-49 DECODE 4-49 NVL 4-49 Conditions comparison 4-6, 4-7 IN operator 4-9 NOT IN operator 4-9 Configuration parameters ALLOW_NEWLINE 4-143 DATASKIP 2-545 DB_LIBRARY_PATH 5-65 DBSERVERALIAS 2-280, 2-402, 2-487, 2-645 DBSERVERNAME 2-280, 2-402, 2-487, 2-645 DEADLOCK_TIMEOUT 2-303 DEF_TABLE_LOCKMODE 2-62, 2-204, 2-415 DIRECTIVES 5-34 DS_NONPDQ_QUERY_MEM 2-516 EXT_DIRECTIVES 2-476 FILLFACTOR 2-125 IFX_EXTEND_ROLE 2-108, 2-114, 2-386, 2-468, 4-90 JVPCLASSPATH 2-341
Configuration parameters (continued) LOCKS 2-297 OPTCOMPIND 2-598, 5-39 SEQ_CACHE_SIZE 2-42, 2-167 STACKSIZE 5-59 STMT_CACHE 2-597 STMT_CACHE_HITS 2-599 STMT_CACHE_NOLIMIT 2-599 STMT_CACHE_SIZE 2-599 USEOSTIME 4-58 Conflict resolution 2-45 CONNECT keyword in GRANT statement 2-373 in REVOKE statement 2-457 Connect privilege 2-457 granting 2-373 CONNECT statement 2-75 CONNECTION_ALIAS keyword, in GET DIAGNOSTICS statement 2-366 Connections context 2-291, 2-535 current 2-76, 2-292, 2-536 default 2-291, 2-536 dormant 2-76, 2-291, 2-534 explicit 2-71, 2-77, 2-297 implicit 2-77, 2-90, 2-255, 2-291, 2-536 single-threaded applications 2-535 Constant expression 4-53 in SELECT 2-487 inserting with PUT 2-443 Constraint renaming 2-452 CONSTRAINT keyword in ALTER TABLE statement 2-49, 2-59, 2-61 in CREATE TABLE statement 2-182 Constraints adding primary-key 2-60 adding referential 2-60 adding to a column with data 2-56 adding unique 2-60 adding with ALTER TABLE 2-59 affected by dropping a column from table 2-52 B-tree indexes 2-118 checking 2-241, 2-417, 2-606 detached checking 2-207 disabled 2-183, 2-542 dropping with ALTER TABLE 2-61 enabled 2-183 encountering violations while adding 2-60 filtering 2-183 filtering to violations table 2-542 foreign key 2-469 limit on size 2-185 mode 2-183 modifying a column that has constraints 2-53 moving 2-421 multiple-column 2-184 name 2-182 number of columns allowed 2-185 privileges needed to create 2-60 referential 2-179, 2-618 single-column 2-176 system catalog tables 2-182 transaction mode 2-606 CONSTRAINTS keyword in SET CONSTRAINTS statement 2-538 in SET Database Object Mode statement 2-540, 2-541 Index
X-7
CONSTRAINTS keyword (continued) in SET Transaction Mode statement 2-606 constrid column 2-21 Constructors functions collections 4-65 row 4-64 Contact information xxiv CONTINUE keyword in WHENEVER statement 2-659 CONTINUE statement 3-6 Conventions command-line xvi documentation xiv sample-code xviii syntax diagrams xv syntax notation xv typographical xiv Correlated subqueries 2-494 Correlated subquery defined 4-14 Correlation name 2-494 in routines 2-239 qualifying values 2-235 restrictions 2-228 scope of reference 2-234 COS function 4-100, 4-101 Coserver number 2-102 Coserver number 4-78 COSTFUNC keyword, in Routine Modifier segment 5-54, 5-55 COUNT field in ALLOCATE DESCRIPTOR statement 2-6 in GET DESCRIPTOR statement 2-357 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 COUNT function defined 4-119 restriction with CREATE TRIGGER statement 2-234 syntax 4-117 COUNT keyword See also COUNT field. in SET DESCRIPTOR statement 2-554 CPU usage cost 5-58 CPU VP 2-565 CRCOLS keyword in CREATE TABLE statement 2-188 CREATE ACCESS_METHOD statement 2-82 CREATE AGGREGATE statement 2-84 CREATE CAST statement 2-87 CREATE DATABASE statement ANSI compliance 2-91 syntax 2-90 using with PREPARE 2-90 CREATE DISTINCT TYPE statement 2-93 CREATE DUPLICATE statement 2-96 CREATE EXTERNAL TABLE statement 2-98 CREATE FUNCTION statement 2-107 CREATE INDEX statement cluster with fragments 2-118 composite indexes 2-120 disabled indexes 2-131 index-type options 2-117 sort order 2-121 specifying lock modes 2-132
CREATE INDEX statement (continued) specifying object modes 2-130 storage options 2-125 CREATE OPAQUE TYPE statement 2-136 CREATE PROCEDURE FROM statement 2-153 CREATE PROCEDURE statement 2-145 CREATE ROLE statement 2-155 CREATE ROUTINE FROM statement 2-157 CREATE ROW TYPE statement 2-158 CREATE SCHEMA statement defining a trigger 2-219 syntax 2-162 CREATE SCRATCH TABLE statement 2-164 CREATE SEQUENCE statement 2-165 CREATE SYNONYM statement syntax 2-168 CREATE TABLE statement access-method option 2-202 column definition clause 2-173 column-level constraints 2-211 constraints check 2-181 composite keys 2-184, 2-187 defining 2-176 distinct 2-177 example 2-186 NOT NULL 2-175, 2-177 primary key 2-178 referential 2-179 restrictions 2-177 unique 2-177 CRCOLS keyword 2-188 creating composite columns 2-184 creating shadow-replication columns 2-188 defining constraints 2-184 fragmenting by expression 2-192 by hash 2-194 by hybrid 2-195 by range 2-195 round-robin 2-192 syntax 2-190 locking options 2-203 ON DELETE CASCADE keywords 2-50 OPERATIONAL keyword 2-171, 2-173 PUT clause 2-199 RAW keyword 2-171, 2-173 specifying cascading deletes 2-180 specifying column-default values 2-174 specifying storage location 2-188 STANDARD keyword 2-171, 2-172 STATIC keyword 2-171, 2-173 syntax 2-171 WITH CRCOLS keywords 2-188 WITH ROWIDS keywords 2-191 CREATE TEMP TABLE statement 2-208 CREATE TEMP TABLE. See CREATE Temporary TABLE statement. CREATE Temporary TABLE statement storage options 2-213 syntax 2-209 CREATE TRIGGER statement no table expressions 2-494 syntax 2-216 CREATE VIEW statement 2-247, 2-495 CREATE XADATASOURCE statement 2-252
X-8
Creation-time settings of environment variables 2-232 CROCOLS keyword, in CREATE Temporary TABLE statement 2-212 Cross joins 2-500 CROSS keyword in SELECT statement 2-503 Cross-database DML operations 2-280, 2-402, 2-487, 2-645 CRPT audit-event mnemonic 4-80 CSN encryption 4-80 CTRL-J newline preserving in quoted strings 4-143 Current database, specifying with DATABASE 2-255 Current date 4-58, 4-59 CURRENT DORMANT keywords, in SET CONNECTION statement 2-534 CURRENT function as an argument 4-58 as constant expression 4-53 example 2-403 in ALTER TABLE statement 2-46 in Condition segment 4-9 in CREATE TABLE statement 2-174 in DEFINE statement 3-9 in INSERT statement 2-398, 2-403 in WHERE condition 4-58 CURRENT keyword in DELETE statement 2-275 in DISCONNECT statement 2-292 in FETCH statement 2-344 in SET CONNECTION statement 2-534 in UPDATE statement 2-636, 2-646 CURRENT_ROLE operator defined 4-56 syntax 4-53 CURRVAL operator 2-166, 4-61 Cursor activating with OPEN 2-424 affected by transaction end 2-69 characteristics 2-267 closing 2-68 closing with ROLLBACK WORK 2-474 declaring 2-260 for update restricted statements 2-269 using in ANSI-mode databases 2-270 using in non-ANSI databases 2-270 freeing automatically with SET AUTOFREE 2-529 implicit 3-20 manipulation statements 1-7 opening 2-425 prepared statement with 2-271 read-only restricted statements 2-269 using in ANSI-mode databases 2-270 using in non-ANSI databases 2-270 where required 2-520 retrieving values with FETCH 2-344 select hold examples 2-268 sequence of program operations 2-263 stability 2-603 statement identifier with 2-271 types of 2-424 with INTO keyword in SELECT 2-491 with transactions 2-274 Cursor function 3-21, 5-53 CURSOR keyword, in DECLARE statement 2-260 Cursor Stability isolation level 2-577
CURSOR STABILITY keywords, in SET ISOLATION statement 2-575 CYCLE keyword in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-167
D
Dangling child records 2-277 Data access statements 1-7 confidentiality 2-560 data definition statements 1-6 data manipulation statements 1-7 encryption 4-79 inserting with LOAD 2-407 integrity statements 1-7 Data buffering 2-353 data column of sysprocbody table 4-81 Data distributions confidence 2-653 on temporary tables 2-650 RESOLUTION 2-653 DATA field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 DATA keyword. See DATA field. Data replication 2-45 Data type segment 4-18 Data types 4-18 See also each data type listed under its own name. alignment 2-94 casting 2-87, 4-42 changing with ALTER TABLE 2-56 collection 4-30 complex 4-28 considerations for INSERT 2-400, 4-145 distinct 4-28 opaque 2-136 representation 2-94 simple large object 4-25 specifying with CREATE VIEW 2-248 Data-integrity violations 2-610, 2-622 Database administrator 2-374 granting privileges 2-372 Database administrator (DBA) revoking privileges 2-457 DATABASE keyword in DROP DATABASE statement 2-297 in MOVE TABLE statement 2-419 in RENAME DATABASE statement 2-451 Database object naming 5-17 naming owner 5-43 Database object mode for triggers 2-219, 2-541 privileges required 2-539 specifying 2-539 Database Object Name segment 5-17 Database Server Administrator 5-20 Database Server Administrator (DBSA) 2-108, 2-301, 2-306, 2-309, 2-386, 2-468 DATABASE statement determining database type 2-255 Index
X-9
DATABASE statement (continued) exclusive mode 2-256 specifying current database 2-255 SQLWARN after 2-255 syntax 2-255 Database System Administrator (DBSA) 2-156 Database-level privilege 2-386 See also Privilege. not available for roles 2-458 revoking 2-458 Databases ANSI-compliant 2-255 closing with CLOSE DATABASE 2-71 data warehousing 2-172 default isolation levels 2-577, 2-604 dropping 2-297 external 5-18 global variables 3-9 isolation level 2-602 lock 2-256 naming conventions 5-15 nonlogging database 2-240 OLTP 2-172 opening in exclusive mode 2-256 optimizing queries 2-650 read-only mode 2-520 remote 5-15 renaming 2-451 running in secondary mode 2-256 DataBlade API 5-58 DataBlade API (LIBDMI) 4-143 DataBlade Developer's Kit 5-65 DataBlade modules 2-144 DATAFILES keyword in CREATE EXTERNAL TABLE statement 2-102 in SELECT statement 2-520 DATASKIP configuration parameter 2-545 DATASKIP keyword in SET DATASKIP statement 2-545 DATE data type declaration syntax 4-27 functions in 4-96 literal DATE values 2-175 DATE function 4-96, 4-97 DATETIME data type 4-27 as quoted string 4-144 field qualifiers 4-32 in INSERT 4-145 literal values 4-60, 4-132 DATETIME Field Qualifier segment 4-32 DATETIME keyword, in Literal DATETIME 4-132 datetime.h header file 2-557 DAY function 4-96, 4-98 DAY keyword in DATETIME Field Qualifier 4-32, 4-33 in INTERVAL Field Qualifier 4-127, 4-135 in Literal DATETIME 4-132 DB_LIBRARY_PATH configuration parameter 5-65 DB_LOCALE environment variable 2-127, 2-256, 2-531, 2-572, 5-23 DB-Access utility x DBA keyword in CREATE FUNCTION statement 2-107 in CREATE PROCEDURE statement 2-145 in GRANT statement 2-373 in REVOKE statement 2-457
DBA privilege with CREATE ACCESS_METHOD statement 2-82 with CREATE SCHEMA 2-163 with DROP DATABASE 2-297 with DROP TRIGGER statement 2-316 with REVOKE statement 2-458 DBA-privileged UDR 2-147 DBA. See Database administrator. DBANSIWARN environment variable 2-91, 2-163, 2-172, 2-209, 2-248 DBBLOBBUF environment variable 2-410, 2-632 DBCENTURY environment variable 2-18, 2-232, 2-408, 4-133 DBDATE environment variable 2-175, 2-631, 4-145 DBDELIMITER environment variable 2-412, 2-633 dbhostname option of DBINFO 4-76 DBINFO function 4-72 DBMONEY environment variable 2-408, 2-631, 4-138 DBPATH environment variable 2-77, 2-79 DBSA. See Database System Administrator. dbschema utility 2-41 dbsendrecv data type 2-138 DBSERVERALIAS configuration parameter 2-280, 2-402, 2-487, 2-645 DBSERVERNAME configuration parameter 2-280, 2-402, 2-487, 2-645 DBSERVERNAME function constant expression 4-57 in ALTER TABLE statement 2-46 in Condition segment 4-9 in CREATE TABLE statement 2-174 in DEFINE statement 3-9 dbspace renaming with onspaces 2-12 dbspaces number 4-89 skipping if unavailable 2-545 DBSPACETEMP configuration parameter 2-522 DBSPACETEMP environment variable 2-129, 2-214, 2-522, 3-15 DBTIME environment variable 2-408, 2-631 DBUPSPACE environment variable 2-655 DDL (Data Definition Language) statements listed 1-6 Deadlock 2-350, 3-39 Deadlock detection 2-580 DEADLOCK_TIMEOUT setting in ONCONFIG 2-581 DEALLOCATE COLLECTION statement 2-257 DEALLOCATE DESCRIPTOR statement 2-258 DEALLOCATE ROW statement 2-259 DEBUG environment variable 2-151 DEBUG keyword in SET DEBUG FILE statement 2-547 Debugging sysdbopen() routines 2-151 DECIMAL data type 4-23, 4-24 literal values 4-137 Decimal point (.) DATETIME separator 4-132 INTERVAL separator 4-127 literal numbers 4-137, 4-145 Declarative statements 3-32 DECLARE statement cursor characteristics 2-267 CURSOR keyword 2-267 cursors with prepared statements 2-271 cursors with transactions 2-274
X-10
DECLARE statement (continued) FOR UPDATE keywords 2-264 function cursor 2-262 insert cursor 2-266, 2-269 restrictions with SELECT with ORDER BY 2-518 SCROLL keyword 2-268 select cursor 2-263 syntax 2-260 updating specified columns 2-264 WHERE CURRENT OF clause 2-264 WITH HOLD keywords 2-268 with SELECT statement 2-491 DECODE function 4-52 DECRYPT_BINARY function 4-85 DECRYPT_CHAR function 4-84 DEF_TABLE_LOCKMODE configuration parameter 2-62 DEF_TABLE_LOCKMODE setting, in ONCONFIG 2-415 Default escape character 2-633 Default isolation level 2-604 DEFAULT keyword in ALTER TABLE statement 2-46 in CONNECT statement 2-75 in CREATE EXTERNAL TABLE statement 2-103 in CREATE TABLE statement 2-174 in DEFINE statement 3-7 in DISCONNECT statement 2-291 in GRANT statement 2-385 in REVOKE statement 2-467 in SET CONNECTION statement 2-534, 2-536 in SET DATASKIP statement 2-545 in SET Default Table Space statement 2-549 in SET Default Table Type statement 2-550 in SET ENVIRONMENT statement 2-563, 2-567 in SET PDQPRIORITY statement 2-586 in SET ROLE statement 2-593 SET ROLE statement 2-593 Default locale x Default role 2-156, 2-385, 2-467 DEFAULT_ATTACH environment variable 2-127 DEFERRED keyword, in SET Transaction Mode statement 2-606 DEFERRED_PREPARE keyword n SET DEFERRED_PREPARE statement 2-552 Deferred-Prepare feature 2-552 DEFINE keyword, in Statement Block segment 5-69 DEFINE statement default value clause 3-9 syntax 3-7 Delete join 2-279 DELETE keyword in CREATE TABLE statement 2-179 in CREATE TRIGGER statement 2-222 in GRANT FRAGMENT statement 2-389 in GRANT statement 2-376 in REVOKE FRAGMENT statement 2-472 in REVOKE statement 2-459 Delete privilege 2-376, 2-460 DELETE statements and triggers 2-233 cascading 2-276 collection columns with 2-279 cursor with 2-264 distributed 2-280 OUT parameter 4-115 restrictions when using a join 2-278 syntax 2-275 using joins 2-278
DELETE statements (continued) with SELECT... FOR UPDATE 2-519 with update cursor 2-278 within a transaction 2-276 Delete trigger 2-220, 2-244 Deleting from a specific table in a table hierarchy 2-276 DELIMIDENT environment variable 2-79, 4-143, 4-144, 5-23, 5-25 Delimited identifiers multibyte characters 5-24 non-ASCII characters 5-24 syntax 5-24 DELIMITED keyword in CREATE EXTERNAL TABLE statement 2-103 in SELECT statement 2-524 Delimiter for LOAD input file 2-411 specifying with UNLOAD 2-633 DELIMITER keyword in CREATE EXTERNAL TABLE statement 2-103 in LOAD statement 2-407 in SELECT statement 2-523 in UNLOAD statement 2-630 DELUXE keyword in CREATE EXTERNAL TABLE statement 2-103 Demonstration databases x Dependencies, software x DESC keyword in CREATE INDEX statement 2-118 in SELECT statement 2-515, 2-517 order with nulls 2-517 Descending sequence 2-41, 2-166 DESCRIBE INPUT statement 2-286 DESCRIBE statement collection variable with 2-284 distinct data type with 2-361 opaque data type with 2-360 relation to GET DESCRIPTOR 2-359 syntax 2-281 with SET DESCRIPTOR 2-559 DESCRIPTOR keyword in ALLOCATE DESCRIPTOR statement 2-6 in DEALLOCATE DESCRIPTOR statement 2-258 in EXECUTE statement 2-322, 2-326 in FETCH statement 2-344 in GET DESCRIPTOR statement 2-357 in OPEN statement 2-424 in PUT statement 2-442 destroy() support function 2-139, 2-279, 2-315 DETACH keyword, in ALTER FRAGMENT statement 2-19 Detached index 2-97, 2-127 Detached statement 2-530 dev/null output destination 2-589 Diagnostics area 2-362 DIAGNOSTICS keyword, in GET DIAGNOSTICS statement 2-362 Diagnostics table creating 2-609 examples 2-622 filtering mode 2-542 how to stop 2-622 relationship to target table 2-613 relationship to violations table 2-613 restriction on dropping 2-315 restriction on moving 2-422 schema 2-617 Directive 5-34 Index
X-11
DIRECTIVES configuration parameter 5-34 DIRECTIVES keyword, in SAVE EXTERNAL DIRECTIVES statement 2-476 Dirty Read isolation level 2-134, 2-302, 2-576 DIRTY READ keywords, in SET ISOLATION statement 2-575 Disabilities, visual reading syntax diagrams C-1 DISABLED keyword CREATE TRIGGER statement 2-219 in ALTER TABLE statement 2-49 in CREATE INDEX statement 2-130 in CREATE TABLE statement 2-182, 2-183 in CREATE TRIGGER statement 2-219 in SET AUTOFREE statement 2-529 in SET Database Object Mode statement 2-541, 2-544 in SET DEFERRED_PREPARE statement 2-552 DISCONNECT statement 2-291 DISK keyword in CREATE EXTERNAL TABLE statement 2-102 Display labels in CREATE VIEW statement 2-249 in Projection clause 2-489 in SELECT statement 2-489, 2-521 Distinct data types 4-28 casting 2-94 casts and DROP TYPE 2-317 creating with CREATE DISTINCT TYPE 2-93 DESCRIBE with 2-361 distributed queries 2-486 dropping 2-317 dynamic SQL with 2-361 GET DESCRIPTOR with 2-361 in a remote database 3-38 in dynamic SQL 2-559 privileges 2-93, 2-379, 2-463 restrictions on source type 2-93 source data type 2-361, 2-559 Usage privilege 2-379 with SET DESCRIPTOR 2-559 DISTINCT keyword in ALTER TABLE statement 2-47, 2-59 in CREATE DISTINCT TYPE statement 2-93 in CREATE INDEX statement 2-117, 2-132 in CREATE TABLE statement 2-176, 2-184 in CREATE Temporary TABLE statement 2-211 in Expression segment 4-116, 4-125 in SELECT statement 2-486 in subquery 4-14 Distributed DML operations 2-279, 2-402, 2-645 Distributed queries 2-486 Distributions dropping 2-652 medium 2-654 privileges required to create 2-653 DISTRIBUTIONS keyword, in UPDATE STATISTICS statement 2-649, 2-654 divide() operator function 4-40 Division (/) symbol, arithmetic operator 4-34 DML (Data Manipulation Language) statements 1-7 DOCUMENT keyword in CREATE FUNCTION statement 2-107 in CREATE PROCEDURE statement 2-145 Documentation conventions xiv Documentation Notes xx Documentation set of all manuals xxii Documentation, types of xix machine notes xx
Documentation, types of (continued) online manuals xxi printed manuals xxii Dominant table 2-500 DORMANT keyword, in SET CONNECTION statement 2-534 Dot notation 4-45 Dotted decimal format of syntax diagrams C-1 Double hyphen (--) comment indicator 1-4, 5-35 DOUBLE PRECISION data type 4-24 Double quotes () delimiting SQL identifiers 4-143 literal in a quoted string 4-144 quoted string delimiter 4-142, 4-144 DROP ACCESS_METHOD statement 2-294 DROP AGGREGATE statement 2-295 DROP CAST statement 2-296 DROP CONSTRAINT keywords, in ALTER TABLE statement 2-61 DROP DATABASE statement 2-297 DROP DISTRIBUTIONS keywords, in UPDATE STATISTICS statement 2-649 DROP DUPLICATE statement 2-299 DROP FUNCTION statement 2-300 DROP INDEX statement 2-302 DROP keyword in ALTER ACCESS_METHOD statement 2-9 in ALTER FRAGMENT statement 2-27 in ALTER FUNCTION statement 2-31 in ALTER PROCEDURE statement 2-35 in ALTER ROUTINE statement 2-37 in ALTER TABLE statement 2-52 in TRUNCATE statement 2-624 in UPDATE STATISTICS statement 2-649 DROP OPCLASS statement 2-304 DROP PARTITION keywords, in ALTER FRAGMENT statement 2-27 DROP ROLE statement 2-307 DROP ROUTINE statement 2-308 DROP ROW TYPE statement 2-310 DROP SEQUENCE statement 2-312 DROP SYNONYM statement 2-313 DROP TABLE statement 2-314 DROP TRIGGER statement 2-316 DROP TYPE statement 2-317 DROP VIEW statement 2-318 DROP XADATASOURCE statement 2-319 DROP XADATASOURCE TYPE statement 2-320 DS_ADM_POLICY configuration parameter 2-594 DS_NONPDQ_QUERY_MEM configuration parameter 2-516 DS_TOTAL_TMPSPACE configuration parameter 2-566 DUPLICATE keyword in CREATE DUPLICATE statement 2-96 in DROP DUPLICATE statement 2-299 Duplicate table CREATE DUPLICATE 2-96 Duplicate values in a query 2-486 Dynamic cursor names 2-262 Dynamic link library 5-20 Dynamic log feature 3-39 Dynamic management statement 1-7, 3-27 Dynamic parameters 2-287 Dynamic routine-name specification 2-332 of SPL functions 2-332 of SPL procedures 2-337 Dynamic SQL 3-1
X-12
E
EACH keyword, in CREATE TRIGGER statement 2-226, 2-231 East Asian locales 4-90 EBCDIC keyword in CREATE EXTERNAL TABLE statement 2-103 in SELECT statement 2-524 ELECT INTO clause no table expressions 2-494 Element variable 3-22 ELIF keyword, in IF statement 3-25 ELSE keyword in CASE statement 3-4 in Expression segment 4-50, 4-51 in IF statement 3-25 en_us.8859-1 locale x ENABLED keyword in ALTER TABLE statement 2-49 in CREATE INDEX statement 2-130 in CREATE TABLE statement 2-182, 2-184 in CREATE TRIGGER statement 2-219 in SET AUTOFREE statement 2-529 in SET Database Object Mode statement 2-541, 2-544 in SET DEFERRED_PREPARE statement 2-552 ENCRYPT_AES function 4-85 ENCRYPT_TDES function 4-86 Encrypted data 2-176, 4-79 Encryption and decryption functions DECRYPT_BINARY 4-85 DECRYPT_CHAR 4-84 ENCRYPT_AES 4-85 ENCRYPT_TDES 4-86 GETHINT 4-87 syntax 4-79 Encryption communication support module 2-560, 4-80 ENCRYPTION keyword in SET ENCRYPTION PASSWORD statement 2-560 END CASE keywords, in CASE statement 3-4 END EXCEPTION keywords, in ON EXCEPTION statement 3-31 END FOR keywords, in FOR statement 3-17 END FOREACH keywords, in FOREACH statement 3-20 END FUNCTION keyword in CREATE FUNCTION statement 2-107 END IF keywords, in IF statement 3-25 END keyword in Expression segment 4-50, 4-51 in Statement Block segment 5-69 END PROCEDURE keyword in CREATE PROCEDURE statement 2-145 END WHILE keywords, in WHILE statement 3-43 Enterprise Replication 2-188 ENVIRONMENT keyword in SET ENVIRONMENT statement 2-563 Environment variables xiv ANSIOWNER 1-1, 5-45 DB_LOCALE 2-127, 2-256, 2-531, 2-572, 5-23 DBANSIWARN 2-91, 2-163, 2-248, 2-262 DBBLOBBUF 2-410, 2-632 DBCENTURY 2-18, 2-232, 2-408, 4-133 DBDATE 2-175, 2-408, 2-631, 4-145 DBDELIMITER 2-412, 2-633 DBMONEY 2-408, 2-631 DBPATH 2-77 DBSPACETEMP 2-129 DBTIME 2-408, 2-631 DEBUG 2-151
Environment variables (continued) DEFAULT_ATTACH 2-127 DELIMIDENT 2-79, 4-143, 5-23, 5-25, 5-31 GL_DATE 2-175, 2-408, 2-631, 4-145 GL_DATETIME 2-408, 2-631, 4-134 IBM_XPS_PARAMS 2-565, 4-58, 4-59 IFX_DEF_TABLE_LOCKMODE 2-62, 2-204 IFX_DIRECTIVES 5-34 IFX_DIRTY_WAIT 2-413, 2-625 IFX_EXTDIRECTIVES 2-476 IFX_LONGID 5-23 IFX_MULTIPREPSTMT 2-522 IFX_TABLE_LOCKMODE 2-415 IFX_UPDDESC 2-282, 2-287 IMPLICIT_PDQ 2-151 INFORMIXCONCSMCFG 2-562 INFORMIXSERVER 2-77, 4-57 MAXERRORS 2-103 NODEFDAC 2-109, 2-147, 2-464 OPT_GOAL 5-40 OPTCOMPIND 2-567, 2-598 PDQPRIORITY 2-564, 2-587, 2-598 RECORDEND 2-103 setting with SYSTEM statement 3-40 STMT_CACHE 2-597 USETABLENAME 2-43, 2-314, 2-625 Equal sign (=) assignment operator 2-642 relational operator 4-146, 4-147 equal() function 2-178 equal() operator function 4-147 Error checking continuing after error in SPL routine 3-33 error status with ON EXCEPTION 3-31 with SYSTEM 3-39 with WHENEVER 2-661 ERROR keyword in ALTER TABLE statement 2-49 in CREATE INDEX statement 2-130 in CREATE TABLE statement 2-182 in SET CONSTRAINTS statement 2-538 in SET Database Object Mode statement 2-541 in SET INDEXES statement 2-574 in WHENEVER statement 2-659 synonym for SQLERROR 2-661 Error messages xxi ESCAPE keyword in Condition segment 4-6, 4-11 in CREATE EXTERNAL TABLE statement 2-103 in SELECT statement 2-524 with LIKE keyword 2-509, 4-12 with MATCHES keyword 2-509, 4-13 ESQL/C collection cursor with FETCH 2-350 collection cursor with PUT 2-446 cursor example 2-268 deallocating collection-variable memory 2-257 deallocating row-variable memory 2-259 inserting collection variables with 2-401 inserting row variables 2-402, 2-403 statements valid only in ESQL/C 4-42 ESQL/C API 4-143 Exception handler 5-70 EXCEPTION keyword in GET DIAGNOSTICS statement 2-366 in ON EXCEPTION statement 3-31 in RAISE EXCEPTION statement 3-35 Index
X-13
Excess-65 format 4-23 Exclamation point (!) in smart-large-object filename 4-94 EXCLUSIVE keyword in DATABASE statement 2-255 in LOCK TABLE statement 2-413 Exclusive lock mode 2-117, 2-134, 2-302, 2-413 Executable file location 5-65 Executable statements 3-32 EXECUTE FUNCTION keywords in DECLARE statement 2-260 in FOREACH statement 3-20 in INSERT statement 2-404 in Statement Block segment 5-69 EXECUTE FUNCTION statement and triggers 2-233 how it works 2-330 INTO clause 2-331 preparing 2-332 syntax 2-329 EXECUTE IMMEDIATE statement restricted statement types 2-334 syntax 2-334 EXECUTE ON keywords in GRANT statement 2-380 in REVOKE statement 2-463 EXECUTE PROCEDURE keywords in DECLARE statement 2-260 in FOREACH statement 3-20 in INSERT statement 2-404 in Statement Block segment 5-69 EXECUTE PROCEDURE statement in FOREACH 3-20 in triggered action 2-233 syntax 2-336 EXECUTE statement INTO clause 2-322 INTO SQL DESCRIPTOR clause 2-324 parameterizing a statement 2-326 returned SQLCODE values 2-325 syntax 2-321 USING DESCRIPTOR clause 2-327 with USING keyword 2-326 EXISTS keyword beginning a subquery 2-510 in Condition segment 4-14 in Condition subquery 4-15 EXIT statement 3-16 EXP function 4-87 EXPLAIN keyword optimizer directive 5-41 SET EXPLAIN statement 2-568 EXPLICIT keyword in CREATE CAST statement 2-87 Exponential function 4-87 Exponential number 4-138 Export support function 2-139, 2-631 exportbinary support function 2-631 Exportbinary support function 2-139 EXPRESS keyword in CREATE EXTERNAL TABLE statement 2-103 Expression Boolean 4-6, 4-7 casting 4-42 constant 4-53 list of 4-36 ordering by 2-517
Expression (continued) smart large objects in 4-48 EXPRESSION keyword in ALTER FRAGMENT statement 2-22, 2-24 in CREATE INDEX statement 2-127 in CREATE TABLE statement 2-190 Expression segment aggregate expressions 4-116 cast expressions 4-42 column expressions 4-44 combined expressions 4-40 list of expressions 4-36 syntax 4-34 EXT_DIRECTIVES configuration parameter 2-476 EXTEND function 4-96, 4-98 EXTEND keyword in GRANT statement 2-386 in REVOKE statement 2-468 EXTEND role 2-108, 2-114, 2-150, 2-153, 2-155, 2-157, 2-301, 2-306, 2-307, 2-309 Extent counting how many in a table 4-119 EXTENT keyword in ALTER TABLE statement 2-58 EXTENT SIZE keywords in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199, 2-201 Extents revising the size 2-62, 2-566 External function as operator-class support function 2-143 CREATE FUNCTION 2-111 dropping 2-301 executing 2-329, 2-435 limits on return values 5-51 non-variant 5-20 OUT parameter 4-115 registering 2-111 strategy functions 2-143 variant 5-20 EXTERNAL keyword in CREATE EXTERNAL TABLE statement 2-99 in External Routine Reference segment 5-20 in SAVE EXTERNAL DIRECTIVES statement 2-476 in SELECT statement 2-520 External language 2-111 EXTERNAL NAME in CREATE FUNCTION statement 2-107 in CREATE PROCEDURE statement 2-145 EXTERNAL NAME keywords in ALTER FUNCTION statement 2-31 in ALTER PROCEDURE statement 2-35 in ALTER ROUTINE statement 2-37 in CREATE FUNCTION statement 2-107 in CREATE PROCEDURE statement 2-145 External optimizer directives 2-476 External procedure creating body of 2-149 dropping 2-306 executing 2-435 restrictions on optimizing 2-650 External Routine Reference segment 5-20 External routines as triggered action 2-233 CREATE PROCEDURE FROM statement in 2-153 creating a function in 2-114 defined 2-147
X-14
External routines (continued) dropping 2-309 EXTEND role 2-468 pathname syntax 5-65 preparing 2-435 referencing 5-20 External synonym 2-314 External tables creating 2-98 integer data types 2-99 NULL values 2-100 restrictions in joins and subqueries 2-495 restrictions on calculating statistics 2-650 restrictions on optimizer directives 5-35 with SELECT statement 2-523 EXTYPEID field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 EXTYPEID keyword. See EXTYPEID field. EXTYPELENGTH field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 EXTYPELENGTH keyword. See EXTYPELENGTH field. EXTYPENAME field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 EXTYPENAME keyword. See EXTYPENAME field. EXTYPEOWNERLENGTH field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 EXTYPEOWNERLENGTH keyword. See EXTYPEOWNERLENGTH field. EXTYPEOWNERNAME field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 EXTYPEOWNERNAME keyword. See EXTYPEOWNERNAME field.
F
FETCH statement 2-344 as affected by CLOSE 2-68 relation to GET DESCRIPTOR 2-358 with concatenation operator 4-42 Field projection 2-644, 4-45 Field qualifier for DATETIME 4-32 for INTERVAL 4-127, 4-135 FILE TO keywords in SET DEBUG FILE statement 2-547 in SET EXPLAIN statement 2-568 in SET PLOAD FILE statement 2-589 Files sending output with the OUTPUT statement
2-431
FILETOBLOB function 4-91 FILETOCLOB function 4-90, 4-91 FILLFACTOR configuration parameter 2-125 FILLFACTOR keyword in CREATE INDEX statement 2-125 FILTERING keyword in ALTER TABLE statement 2-49 in CREATE INDEX statement 2-130 in CREATE TABLE statement 2-182, 2-184 in SET Database Object Mode statement 2-541 with diagnostics tables 2-620 FINAL keyword in CREATE AGGREGATE statement 2-84 FIRST keyword in FETCH statement 2-344 in SELECT statement 2-484, 2-486 invalid in INSERT 2-404 FIRST_ROWS keyword in Optimizer Directives Segment 5-40 in SET OPTIMIZATION statement 2-584 Fixed and Known Defects File xx Fixed format files 2-99 FIXED keyword in CREATE EXTERNAL TABLE statement 2-103 Fixed-length opaque data type 2-137 Fixed-point numbers 4-137 FLOAT data type 4-24 literal values 4-137 systems not supporting 2-256 Floating-point numbers 4-137 FLUSH statement 2-353 Flushing an insert buffer 2-447 FOR EACH ROW keyword, in CREATE TRIGGER statement 2-231 FOR EACH ROW keywords, in CREATE TRIGGER statement 2-226 FOR keyword in CONTINUE statement 3-6 in CREATE OPCLASS statement 2-141 in CREATE SYNONYM statement 2-168 in CREATE TRIGGER statement 2-226, 2-231 in DECLARE statement 2-260 in EXIT statement 3-16 in FOREACH statement 3-20 in INFO statement 2-393 in SAVE EXTERNAL DIRECTIVES statement 2-476 in SELECT statement 2-519 in SET AUTOFREE statement 2-529 in SET CONSTRAINTS statement 2-538 in SET Database Object Mode statement 2-541 in SET INDEXES statement 2-574 in SET TRIGGERS statement 2-608 in START VIOLATIONS TABLE statement 2-609 in STOP VIOLATIONS TABLE statement 2-622 in UPDATE STATISTICS statement 2-649, 2-655 FOR READ ONLY keywords, in DECLARE statement 2-260 FOR statement 3-17 FOR TABLE keywords, in UPDATE STATISTICS statement 2-649 FOR UPDATE keywords in DECLARE statement 2-260 in SELECT statement 2-479, 2-518 relation to UPDATE 2-646 with column list 2-264 FOREACH keyword in CONTINUE statement 3-6 Index
X-15
FOREACH keyword (continued) in EXIT statement 3-16 FOREACH statement syntax 3-20 Foreign key dropping 2-61 establishing 2-49, 2-179 examples 2-51, 2-187 multiple columns 2-185 Foreign key constraint 2-186, 2-469 FOREIGN KEY keywords in ALTER TABLE statement 2-59, 2-60 in CREATE TABLE statement 2-184, 2-204 FORMAT keyword in CREATE EXTERNAL TABLE statement 2-103 in SELECT statement 2-520, 2-524 FRACTION keyword as INTERVAL field qualifier 4-135 in DATETIME field qualifier 4-132 in DATETIME Field Qualifier segment 4-32 in INTERVAL Field Qualifier 4-127 FRAGMENT BY keywords in ALTER FRAGMENT statement 2-22, 2-24 in CREATE INDEX statement 2-127 in CREATE TABLE statement 2-190 FRAGMENT keyword in GRANT FRAGMENT statement 2-388 Fragment-level privilege granting 2-388 revoking 2-471 Fragmentation adding a fragment 2-26 adding rowids 2-45 altering fragments 2-11 arbitrary rule 2-192 built-in hash distribution scheme 2-194 by expression 2-18 combining tables 2-13 Dataskip feature 2-545 defining a new strategy 2-127 detaching a table fragment 2-19 dropping an existing fragment 2-27 dropping rowids 2-45 hybrid 2-18 insufficient log space or disk space 2-13 list of dbspaces 2-545 modifying an existing fragment expression 2-28 number of rows in fragment 2-13 of indexes 2-127 of tables 2-190 of temporary tables 2-213 reading data from local fragments 2-495 reinitializing strategy 2-24 remainder 2-27 reverting to nonfragmented 2-22 round-robin 2-17 rowid 2-21 rowid columns with 2-191 strategy by expression 2-192, 2-546 by round-robin 2-192, 2-546 range rule 2-192 TEXT and BYTE data types 2-16 Fragmentation strategy, modifying 2-21 FRAGMENTS keyword, in INFO statement 2-393 FREE statement 2-355
FROM keyword in CREATE INDEX statement 2-133 in DELETE statement 2-275 in LOAD statement 2-407 in PREPARE statement 2-433 in PUT statement 2-442 in REVOKE FRAGMENT statement 2-471 in REVOKE statement 2-456 in SELECT statement 2-492 in TRIM expressions 4-102 in UPDATE statement 2-645 FROM ONLY. See ONLY keyword. FULL keyword in SELECT statement 2-504 Optimizer Directives Segment 5-35 Full outer joins 2-500 Function cursor defined 3-21 opening 2-426 reopening 2-425 Function expressions 4-68 FUNCTION keyword in ALTER PROCEDURE statement 2-31 in CREATE FUNCTION statement 2-107 in DROP FUNCTION statement 2-300 in GRANT statement 2-380 in REVOKE statement 2-463 in SELECT statement 2-499 in UPDATE STATISTICS statement 2-655 Functional index 2-119, 2-120, 2-135, 2-562 Functions altering with ALTER FUNCTION 2-31 casting 2-89 collection manipulation 5-58 creating indirectly from a stored file 2-114 creating with CREATE FUNCTION 2-107 creating with CREATE FUNCTION FROM 2-114 cursor 3-21 dropping with DROP FUNCTION 2-300 dropping with DROP ROUTINE 2-308 modifying path to executable file 2-32 modifying routine modifiers 2-32 noncursor 3-21 nonvariant 5-21 shared library 4-90 smart large object 4-91 specific name 5-68 system catalog tables 2-110 thread-safe 5-58 user-defined defined 2-146 variant 5-20 Functions, SQL ABS 4-70 ACOS 4-101 ASIN 4-101 ATAN 4-102 ATAN2 4-102 AVG 4-118 CARDINALITY 4-72 CASE expression 4-49 CHAR_LENGTH 4-90 COS 4-101 COUNT 4-119 CURRVAL 4-61
X-16
Functions, SQL (continued) DAY 4-98 DECODE 4-52 DECRYPT_BINARY 4-85 DECRYPT_CHAR 4-84 ENCRYPT_TDES 4-86 Encryption and decryption 4-79 EXNCRYPT_AES 4-85 EXP 4-87 Exponential 4-87 EXTEND 4-98 FILETOBLOB 4-91 FILETOCLOB 4-91 GETHINT 4-87 HEX 4-88 IFX_REPLACE_MODULE 4-90 INITCAP 4-109 LENGTH 4-89 LOCOPY 4-95 LOG10 4-88 Logarithmic 4-87 LOGN 4-88 LOTOFILE 4-93 LOWER 4-109 LPAD 4-107 MAX 4-122 MDY 4-99 MIN 4-122 MOD 4-70 MONTH 4-98 NEXTVAL 4-61 NVL 4-52 OCTET_LENGTH 4-90 POW 4-70 RANGE 4-122 ROOT 4-70 ROUND 4-71 RPAD 4-108 SIN 4-101 SQRT 4-71 STDEV 4-123 SUBSTR 4-105 SUM 4-122 TAN 4-101 Time 4-96 TO_CHAR 4-99 TRIM 4-102 TRIM function 4-68 TRUNC 4-71 UPPER 4-109 VARIANCE 4-123 WEEKDAY 4-98 Fuzzy index 2-143
GET DESCRIPTOR statement (continued) use with FETCH statement 2-348 GET DIAGNOSTICS statement SQLSTATE codes 2-363 syntax 2-362 GET keyword in GET DESCRIPTOR statement 2-357 in GET DIAGNOSTICS statement 2-362 GETHINT function 4-87 GK See Generalized-key index GK index 2-97, 2-117 See Generalized-key index. GK INDEX keywords, in CREATE INDEX statement 2-116 GL_DATE environment variable 2-175, 2-408, 2-631, 4-145 GL_DATETIME environment variable 2-408, 2-631, 4-134 Global environment 3-9 GLOBAL keyword, in DEFINE statement 3-7 Global Language Support (GLS) x Global variables 3-9 GLS. See Global Language Support. GO TO keywords, in WHENEVER statement 2-659 GOTO keyword, in WHENEVER statement 2-662 GRANT FRAGMENT statement 2-388 GRANT keyword in GRANT FRAGMENT statement 2-388 in GRANT statement 2-371 GRANT statement 2-371 greaterthan() operator function 4-147 greaterthanorequal() operator function 4-147 GROUP BY keywords, in SELECT statement 2-513
H
Handle value 4-48, 4-92 HANDLESNULLS keyword in CREATE AGGREGATE statement 2-84 in Routine Modifier segment 5-55 Hash join 2-567, 2-571, 5-38 HASH keyword in ALTER FRAGMENT statement 2-22 in CREATE INDEX statement 2-127 in CREATE TABLE statement 2-190 HAVING keyword in SELECT statement 2-514 HEADINGS keyword, in OUTPUT statement 2-431 Help xxii HEX function 4-48, 4-88 HEX keyword in CREATE EXTERNAL TABLE statement 2-99 Hexadecimal digits 2-633 Hexadecimal dump format 2-632 Hexadecimal smart-large-object identifier 2-632, 4-94 HIGH INTEG keywords in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199 HIGH keyword in SET OPTIMIZATION statement 2-584 in SET PDQPRIORITY statement 2-586 in UPDATE STATISTICS statement 2-649 High-Performance Loader 2-139, 2-520 HINT keyword in SET ENCRYPTION PASSWORD statement 2-560 Hold cursor defined 2-267 insert cursor with hold 2-269 Index
G
Generalized-key index affected by dropping a column 2-53 affected by modifying a column 2-57 defined 2-132 no renamed table 2-455 no table expressions 2-494 SELECT clause 2-132 WHERE clause 2-134 Generic B-tree index 2-124 GET DESCRIPTOR statement syntax 2-357
X-17
Hold cursor (continued) update cursor with hold 2-266 HOLD keyword in DECLARE statement 2-260 in FOREACH statement 3-20 hosts.equiv file 2-81 HOUR keyword as DATETIME field qualifier 4-132 as INTERVAL field qualifier 4-135 in DATETIME Field Qualifier segment 4-32 in INTERVAL field qualifier 4-127 HYBRID keyword in ALTER FRAGMENT statement 2-22 in CREATE INDEX statement 2-127 in CREATE TABLE statement 2-190, 2-195 Hyphen symbol (-) DATETIME separator 4-132 INTERVAL separator 4-135
I
IBM format binary in external tables 2-99 IBM_XPS_PARAMS environment variable 2-565 IDATA field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with X/Open programs 2-359 IDATA keyword See IDATA field. Identifier column names 5-28 cursor name 5-30, 5-31 defined 5-22 delimited identifiers 5-24 multibyte characters 5-23 non-ASCII characters 5-23 routines 5-30 storage objects 5-24 syntax 5-22 table names 5-28, 5-29, 5-30 uppercase characters 5-23 using keywords 5-23 using keywords as column names 5-26 variable name 5-31 IF statement 3-25 IFX_ALLOW_NEWLINE function effect on quoted strings 4-143 syntax 4-111 IFX_DEF_TABLE_LOCKMODE environment variable 2-62, 2-204 IFX_DIRECTIVES environment variable 5-34 IFX_DIRTY_WAIT environment variable 2-413, 2-625 IFX_EXTDIRECTIVES environment variable 2-476 IFX_EXTEND_ROLE configuration parameter 2-32, 2-36, 2-38, 2-108, 2-114, 2-150, 2-153, 2-157, 2-386, 2-468, 4-90, 5-65 IFX_LO_SPEC data type 4-19 IFX_LO_STAT data type 4-19 IFX_LONGID environment variable 5-23 IFX_MULTIPREPSTMT environment variable 2-522 IFX_REPLACE_MODULE function 2-333, 4-90 IFX_TABLE_LOCKMODE environment variable 2-415 IFX_UPDDESC environment variable 2-282, 2-287 ILENGTH field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555
ILENGTH field (continued) with X/Open programs 2-359 ILENGTH keyword See ILENGTH field. Ill-behaved C UDR 5-56 Imaginary numbers 4-25 IMMEDIATE keyword in SET Transaction Mode statement 2-606 IMMUTABLE keyword in SET ALL_MUTABLES statement 2-527 in SET DEFAULT TABLE_SPACE statement 2-549 in SET DEFAULT TABLE_TYPE statement 2-550 in SET PDQPRIORITY statement 2-587 IMPEX data type 4-19 IMPEXBIN data type 4-19 Implicit cursor 3-20 Implicit inner join 2-503 IMPLICIT keyword in CREATE CAST statement 2-87 Implicit transactions 2-596 IMPLICIT_PDQ environment variable 2-151 IMPLICIT_PDQ keyword in SET ENVIRONMENT statement 2-565 Import support function 2-139, 2-409 Importbinary support function 2-139, 2-409 IN keyword as a condition 4-14 in ALTER FRAGMENT statement 2-24 in ALTER TABLE statement 2-58 in Condition segment 4-9, 4-14 in CREATE DATABASE statement 2-90 in CREATE DUPLICATE statement 2-96 in CREATE INDEX statement 2-125 in CREATE PROCEDURE statement 2-145 in CREATE TABLE statement 2-188, 2-190, 2-195, 2-197, 2-199 in CREATE Temporary TABLE statement 2-213 in Data Type segment 4-25 in FOR statement 3-17 in LOCK TABLE statement 2-413 in ON EXCEPTION statement 3-31 in SELECT statement 2-508 INACTIVE keyword, in SAVE EXTERNAL DIRECTIVES statement 2-476 INCREMENT keyword in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-166 Index access method 2-141 altering table fragmentation 2-16 attached 2-30, 2-127 B-tree 2-120, 2-127 bidirectional traversal 2-121 cleaner list. See B-tree cleaner list. clustered fragments 2-118 compacted 2-125 composite 2-120 converting during upgrade 2-649, 2-657 creating 2-116 delete flag 2-651 detached 2-25, 2-127 disabled 2-131, 2-542 displaying information for 2-394 Dropping with DROP INDEX 2-302 filtering to violations table 2-542 fragmented 2-12, 2-127, 2-206
X-18
Index (continued) functional 2-120, 2-127, 2-135 fuzzy 2-143 generalized-key 2-132 internal 2-206 maximum key size 2-119, 2-121 memory resident 2-573 multilingual index 2-532 nonfragmented 2-22 on ORDER BY columns 2-518 on temporary tables 2-522 online 2-135 privilege 2-12 provide for expansion 2-125 R-Tree 2-120, 2-127 renaming 2-452 residency status 2-573, 2-590 ROOT argument 4-70 shared 2-48, 2-50, 2-60 side-effect 2-143 system-generated 2-206, 2-420, 2-421 system-generated names 5-35 unique 2-130 restrictions 2-25 unique keys specifying support for 5-48 virtual 2-135, 2-303 INDEX keyword in ALTER FRAGMENT statement 2-11 in ALTER INDEX statement 2-33 in CREATE INDEX statement 2-116 in DROP INDEX statement 2-302 in GRANT statement 2-376 in Optimizer Directives Segment 5-35 in REVOKE statement 2-459 in SET INDEX statement 2-573 in SET Residency statement 2-590 Index Name. See Database Object Name. Index privilege 2-376, 2-460 INDEX_ALL optimizer directive 5-36 INDEXES keyword in INFO statement 2-393 in SET Database Object Mode statement 2-540 in SET INDEXES statement 2-574 INDEXKEYARRAY data type 4-19 INDICATOR field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 INDICATOR keyword See also INDICATOR field. in EXECUTE FUNCTION statement 2-331 in EXECUTE statement 2-322, 2-326 in FETCH statement 2-344 in PUT statement 2-442 in SELECT statement 2-491 Indicator variable in expression 4-124 INDICATOR variable See INDICATOR keyword. Indirect typing 3-14 Industry standards, compliance with xxiv INFO statement, syntax 2-393 Informix 4GL 4-143 Informix Dynamic Server documentation set xxii INFORMIX keyword in CREATE EXTERNAL TABLE statement 2-103
INFORMIX keyword (continued) in External Routine Reference segment 5-20 in SELECT statement 2-524 informix user name 2-297, 2-374, 5-17 INFORMIX.JVPCONTROL function 2-333 INFORMIXCONCSMCFG environment variable 2-562 INFORMIXDIR/bin directory xi INFORMIXSERVER environment variable 2-77, 2-79, 4-57 Inheritance hierarchy dropping tables 2-315 named ROW types 2-159, 2-310 INIT keyword in ALTER FRAGMENT statement 2-20 in CREATE AGGREGATE statement 2-84 INITCAP function 4-109 Initial-cap characters, converting to 4-109 Inner joins 2-500 INNER keyword in SELECT statement 2-503 INOUT parameters 5-20, 5-64 Input support function 2-138 Insert buffer 2-447 counting inserted rows 2-354, 2-447 filling with constant values 2-443 inserting rows with a cursor 2-397 storing rows with PUT 2-442 triggering flushing 2-447 Insert cursor 2-266 benefits 2-267 closing 2-68 declaring 2-262 in INSERT 2-397 in PUT 2-443 opening 2-426 reopening 2-427 result of CLOSE in SQLCA 2-68 with hold 2-269 INSERT INTO keywords in INSERT 2-395 in LOAD 2-412 INSERT keyword in CREATE TRIGGER statement 2-222 in DECLARE statement 2-260 in GRANT FRAGMENT statement 2-389 in GRANT statement 2-376 in LOAD statement 2-407 in MERGE statement 2-416 in REVOKE FRAGMENT statement 2-472 in REVOKE statement 2-459 Insert privilege 2-376, 2-460 INSERT statements 2-395 and triggers 2-233 AT clause 2-396 collection-column values 2-401 collection-derived table, with 2-405 distributed 2-402 effect of transactions 2-398 ESQL/C 2-401, 2-402, 2-403 filling insert buffer with PUT 2-442 flex inserts 2-567 in dynamic SQL 2-405 insert cursor compared with 2-267 insert triggers 2-220 inserting rows through a view 2-397 rows with a cursor 2-397 Index
X-19
INSERT statements (continued) into collection cursor 2-445 nulls 2-403 OUT parameter and 4-115 row type field values 2-401 row variables 2-405 SERIAL and SERIAL8 columns 2-400 smart large objects with 4-48 specifying values to insert 2-398 syntax 2-395 using functions 2-403 VALUES clause, expressions with 2-403 with DECLARE statement 2-260 with insert cursor 2-266 with SELECT statement 2-403 Insert trigger 2-220, 2-244 install_jar() procedure 2-111, 2-149 Installation Guides xix INSTEAD OF keywords, in CREATE TRIGGER statement 2-216 INSTEAD OF trigger 2-243 INT8 data type 4-23 INTEG keyword in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199 INTEGER data type 4-23 in external tables 2-99 literal values 4-137 INTERNAL keyword, in Routine Modifier segment 5-55 INTERNALLENGTH keyword, in CREATE OPAQUE TYPE statement 2-136 INTERVAL data type as quoted string 4-144 field qualifier 4-127 in expression 4-60 in INSERT 4-145 literal 4-136 loading 2-408 precision 4-127 syntax 4-27, 4-135 INTERVAL keyword, in Literal INTERVAL 4-135 INTO DESCRIPTOR keywords, in EXECUTE 2-324 INTO EXTERNAL keywords in SELECT statement 2-523 INTO keyword in DESCRIBE INPUT statement 2-286 in DESCRIBE statement 2-281 in EXECUTE FUNCTION statement 2-331 in EXECUTE PROCEDURE statement 2-336 in EXECUTE statement 2-322 in FETCH statement 2-344 in FOREACH statement 3-20 in INSERT statement 2-395 in LOAD statement 2-407 in MERGE statement 2-416 in SELECT statement 2-489 INTO SCRATCH keywords, in SELECT statement 2-524 INTO SQL DESCRIPTOR keywords, in EXECUTE statement 2-324 INTO TEMP clause in SELECT statement 2-522 invalid in INSERT 2-404 with UNION operator 2-525 IS keyword in Condition segment 4-6 in WHERE clause 2-509 IS NOT NULL keywords, in Condition segment 4-10
IS NULL keywords, in Condition segment 4-10 ISAM error code 3-31, 3-35, 3-39 ISO 8859-1 code set x ISOLATION keyword in SET ISOLATION statement 2-575 in SET TRANSACTION statement 2-602 Isolation level defined 2-576, 2-603 with FETCH statement 2-349 Item descriptor 2-6 ITEM keyword in Collection Subquery segment 4-3 ITER keyword in CREATE AGGREGATE 2-84 Iterator functions 2-499, 3-38, 5-57 ITERATOR keyword, in Routine Modifier segment ITYPE field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with X/Open programs 2-359 ITYPE keyword. See ITYPE field.
5-55
J
Jagged rows 2-205 JAR files, renaming 2-451 JAVA keyword, in External Routine Reference segment Java UDRs 2-339, 2-340, 2-341 CLASS routine modifier 5-54 creating functions 2-111 creating procedures 2-149 Getting JVP memory information 2-333 Getting JVP thread information 2-333 installing a Jar file 2-339 Java signature 5-67 jvpcontrol function 2-333 removing a Jar file 2-340 replacing a Jar file 2-340 shared-object file 5-66 sqlj.alter_java_path procedure 2-341 sqlj.replace_jar procedure 2-340 sqlj.setUDTExtName procedure 2-342 sqlj.unsetUDTExtName procedure 2-342 static method 5-66 unmapping a user-defined type 2-342 Java virtual processor class CLASS modifier 5-56 Java Virtual Processor Class getting memory information 2-333 getting thread information 2-333 Java Virtual-Table Interface 2-82 JDBC API 4-143 JDBC connection 5-58 JDBC Driver built-in function 2-333 Join condition 2-500 hash join 2-567, 2-571 in Condition segment 2-511 in DELETE statement 2-278 in UPDATE statement 2-645 multiple-table join 2-512 nested-loop join 2-567, 2-571 outer 2-507 outer, Informix extension syntax 2-512 self-join 2-512 sort-merge join 2-567 5-20
X-20
Join (continued) transitively joined on key 2-134 Join column. See Foreign key. Join filter 2-504 JOIN keyword in SELECT statement 2-500 Join on key query 2-134 Join-method directive 5-38 Join-order directive 5-37 JVP. See Java Virtual Processor Class. JVPCLASSPATH configuration parameter jvpcontrol function 2-333
2-341
K
KEEP ACCESS TIME keyword in ALTER TABLE statement 2-58 KEEP ACCESS TIME keywords in CREATE TABLE statement 2-199 KEEP keyword in ALTER TABLE statement 2-58 keep option of esqlc 5-35 KEY keyword CREATE TABLE statement 2-204 in CREATE TABLE statement 2-185 in CREATE Temporary Table statement 2-212 Key management for encrypted data 4-81 Key-only index scan 2-132 Keywords as identifiers 5-23 in syntax diagrams xvii list for IDS A-1 list for XPS B-1
L
Language privileges on 2-381 LANGUAGE keyword in External Routine Reference segment 5-20 in GRANT statement 2-381 in REVOKE statement 2-464 Language, privileges on 2-464 Large objects constraints 2-177, 2-185 declaration syntax 4-25 pointer structure 2-139 LAST keyword, in FETCH statement 2-344 LEADING keyword, in TRIM expressions 4-102 LEFT keyword in SELECT statement 2-504 Left outer joins 2-500 LENGTH field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DATETIME and INTERVAL types 2-557 with DECIMAL and MONEY types 2-557 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 Length function 4-89 LENGTH function 2-487, 4-89 LENGTH keyword. See LENGTH field. lessthan() operator function 4-147
lessthanorequal() operator function 4-147 LET statement 3-28 Lettercase conversion 4-109 LEVEL keyword in SET SCHEDULE LEVEL statement 2-594 in SET TRANSACTION statement 2-602 Level-0 backup 2-173, 2-520 Library shared 4-90 Light append 2-63, 2-173 Light appends 2-173, 2-219 Light scan 4-21 LIKE keyword in Condition segment 4-6, 4-11 in DEFINE statement 3-7 in Routine Parameter List segment 5-62 in SELECT statement 2-509 wildcard characters 2-509 like() operator function 4-12 LIST data type columns, generating values for 4-65 defined 4-65 deleting elements from 2-279 unloading 2-631 updating elements 2-647 LIST keyword in DEFINE statement 3-12 in Expression segment 4-65 in Literal Collection 4-129 LIST. See Collections. LISTING keyword in CREATE FUNCTION statement 2-107 in CREATE PROCEDURE statement 2-145 Literal BOOLEAN 2-175 DATE 2-175 DATETIME 4-132 in ALTER TABLE statement 2-46 in INSERT statement 2-398 with IN keyword 2-508 INTERVAL 4-135 in expression 4-60 in INSERT statement 2-398 nested row 4-141 number as constant expression 4-55 Number 4-137 in INSERT 2-398 with IN keyword 4-10 Literal collection nested 4-130 syntax 4-129 Literal number, exponential notation 4-138 Literal Row segment 4-139 Literal values, specifying as default values 2-175 Little-endian format 2-100 LOAD statement 2-407 local command-line option 2-261, 2-322 LOCAL keyword in SELECT statement 2-495 Local variable 3-11 Locales default x en_us.8859-1 x Localized collation 2-516 Localized collation order 2-531, 4-12, 4-148 Index
X-21
LOCK keyword in ALTER INDEX statement 2-33 in ALTER TABLE statement 2-62 in CREATE Temporary TABLE statement 2-164 in SET LOCK MODE statement 2-580 LOCK MODE keyword in ALTER TABLE statement 2-62 LOCK MODE keywords in ALTER INDEX statement 2-33 in CREATE INDEX statement 2-132 in CREATE TABLE statement 2-203 Lock table overflow 2-266 LOCK TABLE statement syntax 2-413 use in transactions 2-66 Locking blobspaces 2-20 during inserts 2-398 updates 2-265, 2-639 exclusive lock 2-265 exclusive locks 2-413 in transactions 2-66 overriding row-level 2-414 promotable lock 2-265 releasing with COMMIT WORK statement 2-73, 2-266 releasing with ROLLBACK WORK statement 2-474 shared locks 2-413 types of locks 2-62, 2-203 update cursors effect on 2-265 update locks 2-578, 2-639 waiting period 2-580 when creating a referential constraint 2-51, 2-180 with SET ISOLATION statement 2-575 SET LOCK MODE statement 2-580 UNLOCK TABLE statement 2-635 with FETCH statement 2-349 with SET TRANSACTION statement 2-602 write lock 2-265 Locking granularity 2-132, 2-415 LOCKS configuration parameter 2-297, 2-414 LOCKS keyword, in SET ISOLATION statement 2-577 LOCOPY function 4-91, 4-95 Log files, for load and unload jobs 2-589 LOG keyword ALTER TABLE statement 2-58 in CREATE DATABASE statement 2-90 in CREATE TABLE statement 2-199 in CREATE Temporary TABLE statement 2-209 in SET LOG statement 2-582 LOG10 function 4-87 Logarithmic functions LOG10 function 4-87 LOGN function 4-88 Logging buffered versus unbuffered 2-582 cascading deletes 2-277 changing mode with SET LOG 2-582 in CREATE DATABASE statement 2-90 log space requirements 2-13 table type options 2-172 temporary tables 2-215 with triggers 2-242 Logical operator, in Condition segment 4-17 LOGN function 4-87 Lohandles support function 2-139
LOLIST data type 4-19 Long transaction rollback 3-39 Loop controlled 3-17 indefinite with WHILE 3-43 LOTOFILE function 4-91, 4-93 LOW keyword in SET OPTIMIZATION statement 2-584 in SET PDQPRIORITY statement 2-586 in UPDATE STATISTICS statement 2-649 LOWER function 4-109 Lowercase characters, converting to 4-109 LPAD function 4-107 LVARCHAR data type 4-19, 4-20 syntax 4-19
M
Machine notes xx Mail, sending from SPL routines 3-39 Mantissa 4-137 MATCHED keyword in MERGE statement 2-416 MATCHES keyword in Condition segment 4-6, 4-11 in SELECT statement 2-509 wildcard characters 2-509 matches() operator function 4-12 Materialized table expression 2-494 Materialized view 2-248 MAX function 4-116, 4-122 MAX keyword in ALLOCATE DESCRIPTOR statement 2-6 in CREATE TABLE statement 2-196 in START VIOLATIONS TABLE statement 2-609 MAX ROWS keywords, in START VIOLATIONS TABLE statement 2-609 MAX VIOLATIONS keywords, in START VIOLATIONS TABLE statement 2-609 MAX_PDQPRIORITY configuration parameter 2-586 MAXERRORS environment variable 2-103 MAXERRORS keyword in CREATE EXTERNAL TABLE statement 2-103 MAXLEN keyword, in CREATE OPAQUE TYPE statement 2-137 MAXSCAN keyword in SET ENVIRONMENT statement 2-565 MAXVALUE keyword in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-166 MDY function 4-96, 4-99 MEDIUM keyword, in UPDATE STATISTICS statement 2-649 Membership operator 4-45 Memory allocating for collection variable 2-4 allocating for query 2-564 allocating for ROW variable 2-8 deallocating cursors 2-355 deallocating for collection variable 2-257 deallocating for cursors 2-529 deallocating for row variable 2-259 deallocating prepared objects 2-356, 2-530 MEMORY keyword, in EXECUTE FUNCTION statement 2-333 MEMORY_RESIDENT keyword in SET INDEX statement 2-573 in SET Residency statement 2-574, 2-590
X-22
MEMORY_RESIDENT keyword (continued) in SET TABLE statement 2-601 Memory-resident indexes, setting See SET Residency statement. Memory-resident tables, setting. See SET Residency statement. MERGE statement 2-416 MESSAGE_LENGTH keyword, in GET DIAGNOSTICS statement 2-366 MESSAGE_TEXT keyword, in GET DIAGNOSTICS statement 2-366 mi_collection* functions 5-58 MIN function 4-116, 4-122 MIN keyword, in CREATE TABLE statement 2-196 Minus (-) sign arithmetic operator 4-34 Minus operator (-) unary 4-136, 4-137 Minus sign (-) INTERVAL literals 4-136 unary operator 4-136 minus() operator function 4-40 MINUTE keyword in DATETIME Field Qualifier segment 4-32 in INTERVAL Field Qualifier 4-127 MINVALUE keyword in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-167 Missing arguments 5-3 Mixed-case characters, converting to 4-109 MOD function 4-69, 4-70 MODE keyword in ALTER INDEX statement 2-33 in ALTER TABLE statement 2-62 in CREATE DATABASE statement 2-90 in CREATE INDEX statement 2-132 in CREATE TABLE statement 2-203 in CREATE Temporary TABLE statement 2-164 in LOCK TABLE statement 2-413 in SET LOCK MODE statement 2-580 MODIFY EXTERNAL NAME keywords in ALTER FUNCTION statement 2-32 in ALTER PROCEDURE statement 2-36 in ALTER ROUTINE statement 2-38 MODIFY keyword in ALTER ACCESS_METHOD statement 2-9 in ALTER FRAGMENT statement 2-28 in ALTER FUNCTION statement 2-31 in ALTER PROCEDURE statement 2-35 in ALTER ROUTINE statement 2-37 in ALTER TABLE statement 2-53 MODIFY NEXT SIZE keyword in ALTER TABLE statement 2-61 Modifying routine modifiers with ALTER FUNCTION statement 2-32 with ALTER PROCEDURE statement 2-35 with ALTER ROUTINE statement 2-37 Modulus 4-70 MONEY data type literal values 4-137 loading 2-408 syntax 4-22 MONTH function 4-96, 4-98 MONTH keyword in DATETIME Field Qualifier segment 4-32 in INTERVAL Field Qualifier 4-127 MORE keyword, in GET DIAGNOSTICS statement 2-365
MOVE TABLE statement 2-419 MQ DataBlade module 5-50 Multi-index scan 5-36 Multibyte characters 4-20 Multibyte locales 4-90 Multilingual index 2-532 Multiple triggers example 2-223 preventing overriding 2-241 Multiple-column constraints in ALTER TABLE statement 2-59 in CREATE TABLE statement 2-184 Multiplication sign (*), arithmetic operator 4-34 Multirepresentational data 2-315, 2-401, 2-644 Multirow query 2-346 MULTISET See Collections. MULTISET columns, generating values for 4-65 MULTISET data type collection subqueries 4-3 defined 4-65 deleting elements from 2-279 unloading 2-631 updating elements 2-647 MULTISET keyword in Collection-Subquery segment 4-3 in DEFINE statement 3-12 in Expression segment 4-65 in Literal Collection 4-129 MULTISET, creating from subquery results 4-3 Mutability property 2-527 setting for all variables 2-527 MUTABLE keyword in SET ALL_MUTABLES statement 2-527 in SET DEFAULT TABLE_SPACE statement 2-549 in SET DEFAULT TABLE_TYPE statement 2-550 in SET ENVIRONMENT statement 2-563 in SET PDQPRIORITY statement 2-587
N
NAME field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 NAME keyword See also NAME field. External Routine Reference segment 5-20 in ALTER FUNCTION statement 2-31 in ALTER PROCEDURE statement 2-35 in ALTER ROUTINE statement 2-37 Named row type assigning with ALTER TABLE 2-64 associating with a column 4-29 creating with CREATE ROW TYPE 2-158 dropping with DROP ROW TYPE 2-310 inheritance 2-159 privileges on 2-379 Under privilege 2-469 unloading 2-631, 2-633 updating fields 2-647 Naming convention database 5-15 database objects 5-17 Natural logarithms 4-88 Index
X-23
NCHAR data type syntax 4-19 negate() operator function 4-41 Negator functions 2-381, 2-464, 5-57 NEGATOR keyword Routine Modifier segment 5-55, 5-57 Nested loop join 2-567, 2-571, 5-39 NESTED optimizer directive 5-42 Nested ordering in SELECT statement 2-517 NET API 4-143 New features xi, xiii NEW keyword, in CREATE TRIGGER statement 2-229, 2-230 Newline characters in quoted strings 4-143 NEXT keyword in ALTER TABLE statement 2-61 in CREATE TABLE statement 2-201 in FETCH statement 2-344 NEXT SIZE keywords in ALTER TABLE statement 2-61 in CREATE TABLE statement 2-201 NEXTVAL operator 2-166, 4-61 NO KEEP ACCESS TIME keywords in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199 NO keyword in SET COLLATION statement 2-531 NO LOG keywords in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199 in SELECT statement 2-520 NOCACHE keyword in ALTER SEQUENCE statement 2-42 in CREATE SEQUENCE statement 2-167 NOCTCLE keyword in CREATE SEQUENCE statement 2-167 NOCYCLE keyword in ALTER SEQUENCE statement 2-42 NODEFDAC environment variable 2-109, 2-147, 2-378, 2-464 effects on new routine 2-109, 2-147 effects on new table 2-206 GRANT statement with 2-381 NOMAXVALUE keyword in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-167 NOMINVALUE keyword in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-167 NON_RESIDENT keyword in SET INDEX statement 2-573 in SET Residency statement 2-574, 2-590 in SET TABLE statement 2-601 Noncursor function 5-53 Nondefault code sets 4-148 NONE keyword, in SET ROLE statement 2-592 NONE role 2-155, 2-307 Nonlogging temporary tables creating 2-210 duration 2-215 Nonvariant functions 5-21 NOORDER keyword in ALTER SEQUENCE statement 2-42 in CREATE SEQUENCE statement 2-167 NORMAL keyword in ALTER INDEX statement 2-33, 2-34 in CREATE INDEX statement 2-132 NOT FOUND keywords, in WHENEVER statement 2-659
NOT keyword in ALTER INDEX statement 2-34 in Condition segment 4-5, 4-6, 4-9, 4-11, 4-14 in MERGE statement 2-416 in SELECT statement 2-508, 2-509 in SET LOCK MODE statement 2-580 Routine Modifier segment 5-55 with BETWEEN keyword 2-508 with IN keyword 2-510 NOT NULL keywords in ALTER TABLE statement 2-47 in collection data type declarations 4-31 in CREATE EXTERNAL TABLE statement 2-101 in CREATE ROW TYPE statement 2-160 in CREATE TABLE statement 2-176 in CREATE Temporary TABLE statement 2-211 in DEFINE statement 3-12 in SELECT statement 2-508 NOT VARIANT keywords, in External Routine Reference segment 5-20 NOT WAIT keywords in SET LOCK MODE 2-580 notequal() operator function 4-147 NULL keyword ambiguous as a routine variable 5-30 Argument segment 5-2 in ALTER TABLE statement 2-46 in Condition segment 4-8 in CREATE EXTERNAL TABLE statement 2-99 in CREATE ROW TYPE statement 2-160 in CREATE TABLE statement 2-174 in CREATE Temporary TABLE statement 2-211 in DEFINE statement 3-7 in Expression segment 4-50, 4-51, 4-52 in INSERT statement 2-398 in SELECT statement 2-508 in SET ROLE statement 2-592 in UPDATE statement 2-639, 2-640 Null values checking for in SELECT statement 2-323, 2-326 in IF statement 3-26 inserting with the VALUES clause 2-403 invalid for collection types 4-31 loading 2-408 returned implicitly by SPL function 3-37 updating a column 2-640 used in Condition with NOT operator 4-16 used in the ORDER BY clause 2-517 WHILE statement 3-43 with AND and OR keywords 4-16 with NVL function 4-52 NULLABLE field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 NULLABLE keyword See NULLABLE field. NUMBER keyword, in GET DIAGNOSTICS statement 2-365 Numeric data types 4-21 NVARCHAR data type 4-20 syntax 4-19 NVL function 4-52
O
Object mode. See Database object mode.
X-24
Object-List format, in SET Database Object Mode statement 2-540 Octal numbers 2-103, 2-524 OCTET_LENGTH function 4-89, 4-90 ODBC API 4-143 OF keyword in CREATE DUPLICATE statement 2-96 in CREATE TRIGGER statement 2-216, 2-223, 2-224, 2-244 in CREATE VIEW statement 2-247 in DECLARE statement 2-260 in DELETE statement 2-275 in DROP DUPLICATE statement 2-299 in SELECT statement 2-519 in UPDATE statement 2-636 OF TYPE keywords in CREATE TABLE statement 2-204 in CREATE VIEW statement 2-247 OFF keyword in SET DATASKIP statement 2-545 in SET ENVIRONMENT statement 2-563 in SET EXPLAIN statement 2-568 in SET PDQPRIORITY statement 2-586 in SET STATEMENT CACHE statement 2-597 in TRACE statement 3-41 OLD keyword, in CREATE TRIGGER statement 2-229, 2-230, 2-231 OLEDB API 4-143 OLTP See Online transaction processing. OLTP (on-line transaction processing) 2-172 ON DELETE CASCADE keywords in ALTER TABLE statement 2-49 in CREATE TABLE statement 2-179 restrictions with triggers 2-221 ON EXCEPTION statement 3-31 ON EXECPTION keyword Statement Block segment 5-69 ON keyword in ALTER FRAGMENT statement 2-11 in CREATE INDEX statement 2-116 in CREATE TABLE statement 2-180 in CREATE TRIGGER statement 2-218, 2-222, 2-223, 2-224, 2-244 in GRANT FRAGMENT statement 2-388 in GRANT statement 2-378, 2-381, 2-382 in MERGE statement 2-416 in REVOKE FRAGMENT statement 2-471 in REVOKE statement 2-459, 2-462, 2-463, 2-464, 2-465 in SET DATASKIP statement 2-545 in SET ENVIRONMENT statement 2-563 in SET EXPLAIN statement 2-568 in SET STATEMENT CACHE statement 2-597 in TRACE statement 3-41 oncheck utility 2-139, 4-89 ONCONFIG paramaters DIRECTIVES 5-34 ONCONFIG parameter SYSSBSPACENAME 2-652 ONCONFIG parameters DATASKIP 2-545 DBSERVERALIAS 2-280, 2-402, 2-487, 2-645 DBSERVERNAME 2-280, 2-402, 2-487, 2-645 DEADLOCK_TIMEOUT 2-581 DEF_TABLE_LOCKMODE 2-62, 2-204 DS_ADM_POLICY 2-594 DS_NONPDQ_QUERY_MEM 2-516 DS_TOTAL_TMPSPACE 2-566
ONCONFIG parameters (continued) EXT_DIRECTIVES 2-476 FILLFACTOR configuration parameter 2-125 IFX_EXTEND_ROLE 2-386, 2-468 LOCKS 2-297 MAX_PDQPRIORITY 2-586 OPTCOMPIND 2-598 STACKSIZE 5-59 STMT_CACHE 2-597 STMT_CACHE_HITS 2-599 STMT_CACHE_NOLIMIT 2-599 STMT_CACHE_NUMPOOL 2-599 STMT_CACHE_SIZE 2-599 USEOSTIME 4-58 ondblog utility 2-582 oninit utility 4-76 Online help xxii ONLINE keyword in CREATE INDEX statement 2-134 in DROP INDEX statement 2-303 Online manuals xxi Online notes xix, xx Online transaction processing 2-302 ONLY keyword in DECLARE statement 2-260 in DELETE statement 2-275, 2-276 in SAVE EXTERNAL DIRECTIVES statement 2-476 in SELECT statement 2-492, 2-497 in SET TRANSACTION statement 2-602 in TRUNCATE (XPS) statement 2-628 in UPDATE statement 2-636, 2-637 in UPDATE STATISTICS statement 2-649, 2-654 onmode utility 2-597 onspaces utility 2-12, 2-126, 2-390, 2-472 onstat utility 2-545, 2-565 onutil check utility 4-89 onutil utility 2-102, 2-106 Opaque data types alignment of 2-137 as argument 2-137 associating with a column 4-28 creating 2-136 DESCRIBE with 2-360 dropping 2-317 extended identifier 2-360, 2-558 GET DESCRIPTOR with 2-360 in DELETE 2-279 in DROP TABLE 2-315 in dynamic SQL 2-558 in INSERT 2-400 in LOAD 2-411 in UPDATE 2-644 loading 2-409, 2-411 modifiers 2-137 name of 2-360, 2-558 naming 2-136 owner name 2-360, 2-558 support functions 2-138 unloading 2-631 with SET DESCRIPTOR 2-558 OPEN statement 2-424 Open-Fetch-Close Optimization 2-553 OPERATIONAL keyword in ALTER TABLE statement 2-63 in CREATE TABLE 2-173 in CREATE TABLE statement 2-171 in SELECT statement 2-521 Index
X-25
OPERATIONAL keyword (continued) in SET Default Table Type statement 2-550 Operator class btree_ops 2-144 creating 2-141 default 2-144, 5-47 default for B-Tree 2-144 defined 2-123, 2-141 dropping with DROP OPCLASS 2-304 rtree_ops 2-144 specifying with CREATE INDEX 2-118, 2-123 Operator function divide() 4-40 equal() 4-147 greaterthan() 4-147 greaterthanorequal() 4-147 lessthan() 4-147 lessthanorequal() 4-147 like() 4-12 matches() 4-12 minus() 4-40 negate() 4-41 notequal() 4-147 plus() 4-40 positive() 4-41 times() 4-40 OPT_GOAL configuration parameter 5-40 OPT_GOAL environment variable 5-40 OPTCOMPIND configuration parameter 5-39 OPTCOMPIND environment variable 2-567, 2-598 OPTCOMPIND keyword, in SET ENVIRONMENT statement 2-567 Optical Subsystem list of statements 1-8 Optimization specifying a high or low level 2-584 OPTIMIZATION keyword in SET OPTIMIZATION statement 2-584 Optimizer and SAVE EXTERNAL DIRECTIVES statement 2-476 and SET OPTIMIZATION statement 2-584 Optimizer Directives segment 5-34 strategy functions 2-142 with UPDATE STATISTICS 2-656 Optimizer directives AVOID_FULL 5-36 AVOID_INDEX 5-36 comment symbols 5-34 external 2-476 FULL 5-36 INDEX 5-36 INDEX_ALL 5-36 inline 2-476 join-order 5-37 NESTED 5-42 not followed 2-504 ORDERED 5-37 restrictions 5-35, 5-36 segment 5-34 Optimizing a database server 2-584 a query 2-476, 2-568 across a network 2-585 OPTION keyword in CREATE TRIGGER statement 2-247 in CREATE VIEW statement 2-250 in GRANT FRAGMENT statement 2-388
OPTION keyword (continued) in GRANT statement 2-371 OR keyword defined 4-17 in Condition segment 4-5 ORDER BY clause no table expressions 2-494 ORDER BY keywords in SELECT statement 2-515 restricted in INSERT 2-404 ORDER keyword in ALTER SEQUENCE statement 2-42 in CREATE SEQUENCE statement 2-167 ORDERED keyword, in Optimizer Directives segment 5-37 OUT keyword Routine Parameter List segment 5-61 OUT parameter 5-20, 5-63 user-defined function 5-63 with a statement-local variable 4-112, 4-115 Outer join. See Join, outer. OUTPUT statement 2-431 Output support function 2-138 Overflow bin 2-655 Overloaded routine 5-3, 5-19 Owner ANSI-compliant database 5-44 case-sensitivity 2-391, 2-457, 2-467, 2-472, 5-43 Database Object Name segment 5-17, 5-43 in ANSI-compliant database 2-391, 2-457, 2-467, 2-472 in CREATE SYNONYM 2-169 in DROP SEQUENCE 2-312 in RENAME TABLE statement 2-454 in system catalog table 2-61 Owner Name segment 5-43 Owner Name segment 5-43 Owner-privileged UDR 2-147
P
Packed decimal 2-99, 2-100 PACKED keyword in CREATE EXTERNAL TABLE statement 2-99 PAGE keyword in ALTER TABLE statement 2-62 in CREATE TABLE statement 2-203 in CREATE Temporary TABLE statement 2-164 Page number 4-48 Page-level locking, in CREATE TABLE 2-203 Parallel distributed queries SET PDQPRIORITY statement 2-586 Parallelizable data query 5-57 PARALLELIZABLE keyword, in Routine Modifier segment 5-55, 5-56 Parameter BYTE or TEXT in SPL 3-15 dynamic 2-287 in CALL statement 3-2 Java method 5-67 UDRs 5-2 PARAMETER keyword External Routine Reference segment 5-20 Parameterizing prepared statements 2-326 Parameterizing a statement, with SQL identifiers 2-437 Parent table 2-421 Parent-child relationship 2-179
X-26
PARTITION keyword in ALTER FRAGMENT statement 2-13, 2-19, 2-24, 2-25, 2-26, 2-29 Partitions 2-390, 2-472 PASSEDBYVALUE keyword, in CREATE OPAQUE TYPE statement 2-137 passwd file 2-80 PASSWORD keyword in SET ENCRYPTION PASSWORD statement 2-560 PDQ SET ENVIRONMENT statement 2-563 SET PDQPRIORITY statement 2-586 PDQ thread safe functions 5-58 PDQPRIORITY environment variable 2-564, 2-586, 2-587, 2-598 PDQPRIORITY keyword in SET PDQPRIORITY statement 2-586 PERCALL_COST keyword, Routine Modifier segment 5-54, 5-55 Percent (%) sign as wildcard 4-11 Period (.) dot notation 4-45 Period symbol (.) DATETIME separator 4-133 DECIMAL values 4-137 INTERVAL separator 4-135 MONEY values 4-137 Permission. See Privilege. Phantom row 2-576, 2-604 Pipe character ( | ) 2-103, 2-407, 2-523, 2-633 PIPE keyword in CREATE EXTERNAL TABLE statement 2-102 in OUTPUT statement 2-431 PLOAD keyword in SET PLOAD FILE statement 2-589 Plus (+) sign arithmetic operator 4-34 Plus operator (+) unary 4-136, 4-137 Plus sign (+) optimizer directives 5-34 unary operator 4-136, 4-137 plus() operator function 4-40 POINTER data type 4-19 Polar coordinates 4-102 positive() operator function 4-41 POW function 4-69, 4-70 Precedence, dot notation 4-46 PRECISION field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 PRECISION keyword. See PRECISION field. PREPARE statement deferring 2-552 for collection variables 2-435 increasing efficiency 2-441 multistatement text 2-335, 2-439 parameterizing a statement 2-437 parameterizing for SQL identifiers 2-437 question (?) mark as placeholder 2-433 releasing resources with FREE 2-355 restrictions with SELECT 2-435
PREPARE statement (continued) statement identifier 2-271 statement identifier use 2-434 syntax 2-433 valid statement text 2-434 with external routines 2-435 with SPL routines 2-435 Prepared statement comment symbols in 2-434 DESCRIBE statement with 2-281 executing 2-321 parameterizing 2-326 prepared object limit 2-261, 2-433 setting PDQ priority 2-586 valid statement text 2-434 with DESCRIBE INPUT statement 2-286 Preserving newline characters in quoted strings 4-143 PREVIOUS keyword, in FETCH statement 2-344 Primary key column, no NULL default 2-175 Primary key constraint moving 2-421 PRIMARY KEY keywords in ALTER TABLE statement 2-47, 2-59, 2-60 in CREATE TABLE statement 2-176, 2-184, 2-204 in CREATE Temporary TABLE statement 2-211 PRIMARY keyword in CREATE ACCESS_METHOD statement 2-82 Primary-key constraint data type conversion 2-56 defining column as 2-178 dropping 2-61 requirements for 2-48, 2-178 rules of use 2-179 using 2-178 Printed manuals xxii PRIOR keyword, in FETCH statement 2-344 PRIVATE keyword in CREATE SYNONYM statement 2-168 Privilege Alter 2-376 chaining grantors 2-468 column-specific 2-461 Connect 2-373 database-level 2-373, 2-457 DBA 2-374, 2-459 effect of NODEFDAC 2-378 Execute 2-380, 2-463 for triggered action 2-239 fragment-level 2-388 revoking 2-471 granting 2-371 in system calls 3-39 needed, to create a cast 2-87 on a synonym 2-168 on a view 2-248 on languages 2-381, 2-464 on named row type 2-379 on remote objects 2-592 on sequences 2-382, 2-465 on table fragments 2-388 Resource 2-374 table-level 2-460 ANSI-compliant 2-378 column-specific 2-375 effect on view 2-378 Usage 2-463
Index
X-27
PRIVILEGES keyword in GRANT statement 2-376 in INFO statement 2-393 in MOVE TABLE statement 2-419 in REVOKE statement 2-459 Procedural language 4-58 Procedure altering with ALTER PROCEDURE 2-35 creating from file 2-153 dropping with DROP PROCEDURE 2-305 dropping with DROP ROUTINE 2-308 modifying path to executable file 2-36 modifying routine modifiers 2-35 privileges 2-147 specific name 5-68 stored. See SPL Routine. system catalog tables for 2-149 user-defined, definition 2-146 Procedure cursor opening 2-425 PROCEDURE keyword DEFINE statement 3-7 in ALTER PROCEDURE statement 2-35 in CREATE PROCEDURE statement 2-145 in DROP PROCEDURE statement 2-305 in EXECUTE PROCEDURE statement 2-336 in GRANT statement 2-380 in REVOKE statement 2-463 in TRACE statement 3-41 in UPDATE STATISTICS statement 2-655 Procedure Name. See Database Object Name. Projection column with dot notation 4-45 field projection 4-45 Projection clause 2-481 Projection list 2-481 See Select list. Promotable lock 2-265 Pseudo-table 2-417 Pseudo-users 2-469 PUBLIC keyword in CREATE SYNONYM statement 2-168 in GRANT FRAGMENT statement 2-390 in GRANT statement 2-383 in REVOKE FRAGMENT statement 2-472 in REVOKE statement 2-466 Purpose defined 5-46 Purpose flags adding and deleting 2-9 list 5-47 Purpose functions adding, changing, and dropping 2-9 for access methods 2-626, 5-48 for XA data source types 5-49 parallel-execution indicator 5-48 Purpose options, valid settings 5-47 Purpose values adding, changing, and dropping 2-9 PUT keyword in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199 PUT statement FLUSH with 2-442 source of row values 2-443
Q
Qualifier field for DATETIME 4-32 Qualifier, field for DATETIME 4-132 for INTERVAL 4-127, 4-135 Qualifying rows 2-479 Query distributed 2-486, 2-560, 4-19, 4-28, 5-18 external databases 5-18 external directives 2-477 operator overflow 2-566 optimizing Optimizer Directives 5-34 optimizing prepared statements 2-599 optimizing with SAVE EXTERNAL DIRECTIVES 2-476 optimizing with SET OPTIMIZATION 2-584 piping results to another program 2-432 priority level 2-586 qualifying rows 2-479 remote databases 5-18 result set 2-479 scheduling level for 2-594 sending results to an operating-system file 2-431 sending results to another program 2-432 Query optimizer recalculating distributions 2-649 Question mark (?) as placeholder in PREPARE 2-433 as wildcard 4-12 dynamic parameters 2-287 generating unique large-object filename 4-94 naming variables in PUT 2-444 Question Mark (?) placeholder in PREPARE 2-427 Quotation marks delimited identifier 5-24 double 5-25 effects of DELIMIDENT environment variable 5-25 literal in a quoted string 4-144 literal nested collection 4-130 owner name 5-43 quoted string delimiter 4-142, 4-144 single 5-25 Quoted Pathname segment 5-65 Quoted string as constant expression 4-55 DATETIME values as strings 4-144 effects of DELIMIDENT environment variable 5-25 in INSERT 2-398, 4-145 INTERVAL values as strings 4-144 maximum length 4-145 newline characters 4-111 newline characters in 4-143 segment 4-142 wildcards 4-145 with LIKE keywords 2-509
X-28
R
R-tree index creating 2-124, 2-135 default operator class 2-144 detached 2-127 dropping 2-303 rtree_ops operator class 2-144 uses 2-124 R-tree secondary-access method 2-124, 2-141 Radicand 4-70 Radix-64 encryption format 2-560 RAISE EXCEPTION statement 3-35 Range fragmentation 2-43 RANGE function 4-116, 4-122 RANGE keyword, in CREATE TABLE statement 2-195 RAW keyword in ALTER TABLE statement 2-63 in CREATE TABLE 2-173 in CREATE TABLE statement 2-171 in SELECT statement 2-520 in SET Default Table Type statement 2-550 READ COMMITTED keywords in SET TRANSACTION statement 2-602 READ keyword, in SET ISOLATION statement 2-576 READ ONLY keywords in DECLARE statement 2-260 in SELECT statement 2-519 in SET TRANSACTION statement 2-602 READ UNCOMMITTED keywords in SET TRANSACTION statement 2-602 READ WRITE keywords in SET TRANSACTION statement 2-602 REAL data type 4-24 Real numbers 4-25 Receive support function 2-138 RECORDEND environment variable 2-103, 2-524 RECORDEND keyword in CREATE EXTERNAL TABLE statement 2-103 in SELECT statement 2-524 REFERENCES keyword in ALTER TABLE statement 2-49 in CREATE TABLE statement 2-179 in DEFINE statement 3-7 in GRANT statement 2-376 in INFO statement 2-393 in Return Clause segment 5-51 in REVOKE statement 2-459 References privilege defined 2-376 displaying 2-394 revoking 2-460 REFERENCING keyword in CREATE TRIGGER statement Delete triggers 2-229 Insert triggers 2-229 INSTEAD OF triggers 2-244 SELECT triggers 2-231 Update triggers 2-230 view column values 2-243 Referential constraint 2-97 B-tree index 2-118 Dataskip feature 2-545 defining 2-179 delete triggers 2-221 dropping 2-61 locking 2-180 Referential integrity 2-277
REJECTFILE keyword in CREATE EXTERNAL TABLE statement 2-103, 2-104 Relational operators IN 4-9 segment 4-146 with WHERE keyword in SELECT 2-508 RELATIVE keyword, in FETCH statement 2-344 Release Notes xx REMAINDER IN keywords in ALTER FRAGMENT statement 2-22, 2-24, 2-26, 2-28 in CREATE INDEX statement 2-127 in CREATE TABLE statement 2-190, 2-195, 2-197 Remote query 2-486 RENAME COLUMN statement 2-449 RENAME DATABASE statement 2-451 RENAME INDEX statement 2-452 RENAME keyword in MOVE TABLE statement 2-419 RENAME SEQUENCE statement 2-453 RENAME TABLE statement 2-454 REOPTIMIZATION keyword in OPEN statement 2-424 Reoptimizing query plans 2-650 Repeatable Read isolation level 2-91, 2-567, 2-577, 2-604 Repeatable Read isolation level, emulating during update 2-350 REPEATABLE READ keywords in SET ISOLATION statement 2-575 in SET TRANSACTION statement 2-602 REPLACE function 4-107 REPLICATION keyword in BEGIN WORK statement 2-66 Reserved words delimited identifiers 5-24 identifiers 5-23 SQL A-1, B-1 using in triggered action 2-233 RESOLUTION keyword, in UPDATE STATISTICS statement 2-654 Resource Grant Manager 2-588, 2-594 RESOURCE keyword in GRANT statement 2-373 in REVOKE statement 2-457 Resource privilege 2-374, 2-457 with CREATE ACCESS_METHOD statement 2-82 Resource usage policy 2-527 RESTART keyword, in ALTER SEQUENCE statement 2-41 RESTRICT keyword in DROP ACCESS_METHOD statement 2-294 in DROP OPCLASS statement 2-304 in DROP ROW TYPE statement 2-310 in DROP TABLE statement 2-314 in DROP TYPE statement 2-317 in DROP VIEW statement 2-318 in DROP XADATASOURCE statement 2-319 in DROP XADATASOURCE TYPE statement 2-320 in MOVE TABLE statement 2-419 in REVOKE statement 2-456 RESTRICTED mode of UDRs 2-595 Result sets 2-479, 2-499 RESUME keyword in ON EXCEPTION statement 3-31 in RETURN statement 3-37 RETAIN UPDATE LOCKS keywords in SET ISOLATION statement 2-575 RETURN statement 3-37
Index
X-29
Return value declaring in CREATE FUNCTION 5-51 RETURNED_SQLSTATE field 2-280, 2-351 RETURNED_SQLSTATE keyword, in GET DIAGNOSTICS statement 2-366 RETURNING keyword example 2-110, 2-112 in CALL statement 3-2 Return Clause Segment 5-51 RETURNS keyword Return Clause segment 5-51 Shared-Object-Filename segment 5-66 REUSE keyword in TRUNCATE statement 2-624 REVOKE FRAGMENT statement 2-471 REVOKE statement 2-456 RGM 2-594 See Resource Grant Manager RIGHT keyword in ANSI Joined Tables segment 2-502 in SELECT statement 2-504 Right outer joins 2-500 Role activating with SET ROLE 2-592 built-in 2-155, 2-307 case-sensitivity 2-372 creating with CREATE ROLE 2-155 currently enabled 4-53 default 2-385, 2-467, 4-53 default roles 2-593 definition 2-155 dropping with DROP ROLE statement 2-307 enabling with SET ROLE 2-592 establishing with CREATE, GRANT, SET 2-383 EXTEND 2-386, 2-468 granting privileges with GRANT 2-384 granting role with GRANT 2-383 revoking privileges 2-466 scope of 2-592 ROLE keyword in CREATE ROLE statement 2-155 in DROP ROLE statement 2-307 in SET ROLE statement 2-592 REVOKE statement 2-467 ROLES keyword in MOVE TABLE statement 2-419 ROLLBACK WORK statement 2-66, 2-474 with WHENEVER 2-71 ROOT function 4-69, 4-70 ROUND function 4-69, 4-71 ROUND ROBIN keywords in ALTER FRAGMENT statement 2-22 in CREATE TABLE statement 2-190 Rounding error 4-148 ROUTINE keyword in ALTER ROUTINE statement 2-37 in DROP ROUTINE statement 2-308 in GRANT statement 2-380 in REVOKE statement 2-463 in UPDATE STATISTICS statement 2-655 Routine manager 2-341 Routine modifier CLASS 5-54 COSTFUNC 5-56 HANDLESNULLS 5-56 INTERNAL 5-57 ITERATOR 5-57
Routine modifier (continued) NEGATOR 5-57 NOT VARIANT 5-60 PARALLELIZABLE 5-57 PERCALL_COST 5-58 SELCONST 5-59 SELFUNC 5-59 STACK 5-59 VARIANT 5-60 Routine signature 5-63 Routines altering with ALTER ROUTINE 2-37 checking references 2-239 creating with CREATE ROUTINE FROM 2-157 dropping with DROP ROUTINE 2-308 modifying path to executable file 2-36, 2-38 routine modifiers 2-37 privileges 2-147 restrictions in triggered action 2-238 specific name 5-68 ROW constructor, in Expression segment 4-63 ROW data types collection-derived tables 5-7 constructor syntax 4-63 dot notation with 4-45 loading field values 2-408, 2-411 nested 4-141 privileges 2-379 selecting fields 2-488, 2-498 selecting from 2-498 unloading 2-631, 2-633 updating 2-643, 2-647 ROW keyword in ALLOCATE ROW statement 2-8 in ALTER TABLE statement 2-62 in CREATE ROW TYPE statement 2-158 in CREATE TABLE statement 2-203 in CREATE Temporary TABLE statement 2-164 in CREATE TRIGGER statement 2-226, 2-231 in DROP ROW TYPE statement 2-310 in Expression segment 4-63 in Literal Row segment 4-139 Row variable accessing 5-14 allocating memory 2-8 deallocating memory for 2-259 inserting 2-401 inserting into 2-405 selecting from 2-498 updating 2-647 ROW_COUNT keyword, in GET DIAGNOSTICS statement 2-365 Row-column level encryption 4-80 Row-level locking, in CREATE TABLE 2-203 Row-type columns, generating values for 4-64 ROWID adding column with INIT clause 2-21 adding with ALTER TABLE 2-45 dropping from fragmented tables 2-45 specifying support 5-48 use in a column expression 4-48 use in fragmented tables 2-21 used as column name 5-27, 5-28 rowid column 2-22, 2-191, 2-518 ROWID keyword, in Expression segment 4-44
X-30
ROWIDS keyword in ALTER FRAGMENT statement 2-22 ROWIDS keyword, in CREATE TABLE statement Rows deleting 2-275 finding location 4-48 inserting through a view 2-397 with a cursor 2-397 order of qualifying rows 2-483 phantom row 2-576 retrieving with FETCH 2-346 rowid defined 2-346 uncommitted row 2-576 updating through a view 2-638 waiting for a locked row 2-580 writing buffered rows with FLUSH 2-353 ROWS keyword, in START VIOLATIONS TABLE statement 2-609 RPAD function 4-108 RSAM access method 2-522 RTNPARAMTYPES data type 4-19
2-190
S
sales_demo database xi SAMEAS keyword in CREATE EXTERNAL TABLE statement 2-99 Sample-code conventions xviii Sampled queries 2-496 SAMPLES OF keywords, in SELECT statement 2-496 Sampling data 2-496 SAVE EXTERNAL DIRECTIVES statement 2-476 SBSPACENAME parameter 2-200 sbspaces specifying in ALTER TABLE 2-58 specifying in CREATE TABLE 2-199 SCALE field, with DESCRIBE INPUT statement 2-288 SCALE field, with DESCRIBE statement 2-283 SCALE keyword See also SCALE field. in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 Scan cost 2-9 Scan threads 2-565 SCHEDULE keyword in SET SCHEDULE LEVEL statement 2-594 Scheduling level 2-594 Schema name 5-43 Scope of reference global 2-322, 3-9 in subqueries with UNION 2-526 local 3-11 module 2-322 static 2-234 SCRATCH keyword in CREATE Temporary TABLE statement 2-209 in SELECT statement 2-520 in SET Default Table Type statement 2-550 Scratch table See also Temporary table. creating 2-209, 2-524 duration 2-215 Screen reader reading syntax diagrams C-1 Scroll cursors defined 2-267
Scroll cursors (continued) with FETCH 2-345 WITH HOLD 2-578 SCROLL keyword, in DECLARE statement 2-260 SECOND keyword in DATETIME Field Qualifier segment 4-32 in INTERVAL Field Qualifier 4-127 SECONDARY keyword in CREATE ACCESS_METHOD statement 2-82 Secondary-access methods B-tree 2-124, 2-142 default operator class 2-144 defined 2-117, 2-141 R-Tree 2-141 Rtree 2-124 registering 2-82 USING clause 2-124 Secure auditing 4-80 Segment defined 4-1, 5-1 SELCONST keyword routine modifier 5-54, 5-55 Select cursor declaring 2-262 opening 2-425, 2-426 reopening 2-425 SELECT ITEM keywords, in Collection-Subquery segment 4-3 SELECT keyword ambiguous use as routine variable 5-30 in Collection Subquery segment 4-3 in Condition segment 4-14, 4-15 in CREATE INDEX statement 2-132 in CREATE TRIGGER statement 2-223 in CREATE VIEW statement 2-249 in DECLARE statement 2-260 in GRANT statement 2-382 in INSERT statement 2-395 in LET statement 3-28 in OUTPUT statement 2-431 in REVOKE statement 2-459, 2-465 in UNLOAD statement 2-630 Select list 2-481 Select privilege 2-376, 2-460, 2-465 SELECT statements aggregate functions in 4-116 BETWEEN condition 2-508 collection with 2-497 column numbers 2-517 cursor for 2-518, 2-519 FIRST clause 2-484, 2-486 FOR READ ONLY clause 2-519 FOR UPDATE clause 2-518 FROM clause 2-492 GROUP BY clause 2-513 HAVING clause 2-514 IN condition 2-508 in FOR EACH ROW trigger 2-228 in INSERT 2-403 indicator variables with 2-331 INTO clause with ESQL 2-489 INTO EXTERNAL clause 2-523 INTO SCRATCH clause 2-524 INTO TEMP clause 2-522 IS NULL condition 2-509 joining tables in WHERE clause 2-511 LIKE or MATCHES condition 2-510 null values in the ORDER BY clause 2-517 Index
X-31
SELECT statements (continued) ORDER BY clause 2-515 outer join 2-507 Projection clause 2-481 relational-operator condition 2-508 restrictions in routine 5-70 restrictions with INTO clause 2-435 row type 2-488, 2-498 ROWID keyword 4-48 select numbers 2-517 singleton 2-490 SKIP option 2-483 smart large objects with 4-48 SPL routine in 2-488 subquery with WHERE keyword 2-508 syntax 2-479 UNION operator 2-524 use of expressions 2-487 user-defined routine in 2-488 with DECLARE 2-260 with FOREACH 3-20 with LET 3-29 WITH NO LOG keywords 2-523 writing rows retrieved to an ASCII file 2-630 SELECT triggers 2-224 Selecting from a specific table in a table hierarchy 2-497 Selectivity 5-59 argument information 5-59 defined 5-59 Selectivity functions 5-59 Self-join defined 2-512 with aliases 2-494 SELFUNC keyword routine modifier 5-54, 5-55 SELFUNC_ARG data type 5-59 SELFUNCARGS data type 4-19 Semantic integrity 2-219, 2-403 Semicolon ( ; ) statement terminator 2-163 Send support function 2-138 SENDRECV data type 4-19 SEQ_CACHE_SIZE configuration parameter 2-42, 2-167 Sequence cache 2-167 creating a synonym for 2-168 generator 2-165 privileges on 2-382, 2-465 SEQUENCE keyword in ALTER SEQUENCE statement 2-41 in CREATE SEQUENCE statement 2-165 in DROP SEQUENCE statement 2-312 in RENAME SEQUENCE statement 2-453 Sequential cursor with DECLARE 2-267 with FETCH 2-345 SERIAL columns nonsequential numbers in 2-194 resetting counter 2-55 use with hash fragmentation 2-194 SERIAL data type inserting values 2-400 invalid default 2-174 length 4-23 resetting counter 2-55, 2-400 value range 4-23 Serial key 2-546
SERIAL8 data type inserting values 2-400 invalid default 2-174 value range 4-23 SERIALIZABLE keyword in SET TRANSACTION statement 2-602 SERVER_NAME keyword, in GET DIAGNOSTICS statement 2-366 Session set initial environment for 2-150 Session control block 4-75 Session ID 4-75 SESSION keyword, in SET SESSION AUTHORIZATION statement 2-595 Session password 2-560 SET ALL_MUTABLES statement 2-527 SET AUTOFREE statement 2-529 SET COLLATION statement 2-531 SET columns, generating values for 4-65 SET CONNECTION statement 2-534 SET CONSTRAINTS See SET Database Object Mode statement and SET Transaction Mode statement. SET CONSTRAINTS statement 2-538 SET data type See also Collections. defined 4-65 deleting elements from 2-279 unloading 2-631 updating elements 2-647 SET Database Object Mode statement syntax 2-539 with CREATE TRIGGER statement 2-241 SET DATASKIP statement syntax 2-545 SET DEBUG FILE statement syntax 2-547 SET DEBUG FILE TO statement with TRACE statement 3-41 SET Default Table Space statement syntax 2-549 SET Default Table Type statement syntax 2-550 SET DEFERRED_PREPARE statement syntax 2-552 SET DESCRIPTOR statement syntax 2-554 SET ENCRYPTION PASSWORD statement audit-event mnemonic 4-80 syntax 2-560 SET ENVIRONMENT statement 2-563 SET EXPLAIN statement 2-570 SET INDEX See SET Residency statement. SET INDEX statement 2-573 SET INDEXES See SET Database Object Mode statement. SET INDEXES statement 2-574 SET ISOLATION statement isolation levels defined 2-603 similarities to SET TRANSACTION statement 2-602 SET keyword in DEFINE statement 3-12 in Expression segment 4-65 in Literal Collection 4-129 in MERGE statement 2-416
X-32
SET keyword (continued) in ON EXCEPTION statement 3-31 in UPDATE statement 2-639 SET LOCK MODE statement 2-580 SET LOG statement 2-582 SET OPTIMIZATION statement ALL_ROWS option 2-584 FIRST_ROWS option 2-584 HIGH option 2-584 LOW option 2-584 syntax 2-584 SET PDQPRIORITY statement 2-586 SET PLOAD FILE statement 2-589 SET Residency statement 2-590 SET ROLE statement 2-592 SET SCHEDULE LEVEL statement 2-594 SET SESSION AUTHORIZATION statement 2-595 SET STATEMENT CACHE statement 2-597 SET TABL See SET Residency statement. SET TABLE statement 2-601 SET Transaction Mode statement 2-606 SET TRANSACTION statement 2-602 default database levels 2-604 effects of isolation 2-605 similarities to SET ISOLATION statement 2-603 SET TRIGGERS See SET Database Object Mode statement. SET TRIGGERS statement 2-608 Set-column- level encryption 4-80 setenv utility 2-80 setnet32 utility 2-79 setUDTExtName() procedure 2-111, 2-149 Shadow columns 2-45 SHARE keyword, in LOCK TABLE statement 2-413 Shared library functions 5-20 Shared libraries 2-386 Shared library functions 4-90 Shared lock mode 2-413 Shared memory index fragments 2-573 table fragments 2-601 Shared-object files 2-111, 5-20 Shell script 3-40 Side-effect index 2-143 Signatures 5-19 Simple assignment 3-28 Simple join 2-500 Simple large objects declaration syntax 4-25 declaring 4-25 loading 2-408, 2-410 unloading 2-631, 2-632 Simple table expression 2-494 Simple view 2-245 SIN function 4-100, 4-101 Single quotes literal in a quoted string 4-144 quoted string delimiter 4-142 Single-byte characters 4-20 Single-threaded application 2-535 Singleton SELECT statement 2-485, 2-490, 2-641 SITENAME function See also DBSERVERNAME function. in ALTER TABLE statement 2-46 in Condition segment 4-9
SITENAME function (continued) in CREATE TABLE statement 2-174 in DEFINE statement 3-9 SIZE keyword in ALTER TABLE statement 2-61 in CREATE EXTERNAL TABLE statement 2-103 in CREATE TABLE statement 2-199, 2-201 in SELECT statement 2-520 SKIP keyword in SELECT statement 2-483 Slash and asterisk (/* */) comment indicator 1-4, 2-434, 5-35 Slot number 4-48 SLV. See Statement-local variable. SMALLFLOAT data type 4-24 literal values 4-137 systems not supporting 2-256 SMALLINT data type, literal values 4-137 Smart large objects 4-26 accessing column data 4-48 copying to a file 4-93 copying to a smart large object 4-95 creating from a file 4-90, 4-91 expressions with 4-48 extent size 2-200 functions for copying 4-91 generating filename for 4-94 handle values 4-48 loading values 2-408, 2-410 logging 2-200 storing 2-58, 2-199 unloading 2-631, 2-632 SMI. See System-Monitoring Interface. SOME keyword beginning a subquery 2-510 in Condition segment 4-15 Sort-merge join 2-567 Sorting in a combined query 2-525 in SELECT 2-515 SOURCEID field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 SOURCETYPE field in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 Spatial data 2-315, 2-644 SPECIFIC FUNCTION keywords in ALTER FUNCTION statement 2-31 in GRANT statement 2-380 in REVOKE statement 2-463 in UPDATE STATISTICS statement 2-655 SPECIFIC keyword in ALTER FUNCTION statement 2-31 in ALTER PROCEDURE statement 2-35 in ALTER ROUTINE statement 2-37 in CREATE FUNCTION statement 2-107 in CREATE PROCEDURE statement 2-145 in DROP FUNCTION statement 2-300 in DROP PROCEDURE statement 2-305 in DROP ROUTINE statement 2-308 in GRANT statement 2-380 in REVOKE statement 2-463 in UPDATE STATISTICS statement 2-655 Specific Name segment 5-68 SPECIFIC PROCEDURE keywords in ALTER PROCEDURE statement 2-35 Index
X-33
SPECIFIC PROCEDURE keywords (continued) in GRANT statement 2-380 in REVOKE statement 2-463 in UPDATE STATISTICS statement 2-655 SPECIFIC ROUTINE keywords in ALTER ROUTINE statement 2-37 in GRANT statement 2-380 in REVOKE statement 2-463 in UPDATE STATISTICS statement 2-655 SPL function CREATE FUNCTION 2-110 cursors 3-20 dropping 2-300 dynamic routine-name specification 2-332 executing 2-329, 2-435 optimization 2-110 registering 2-110 registering from inside an external routine 2-114 SPL keyword in GRANT statement 2-381 in REVOKE statement 2-464 SPL procedure creating with CREATE PROCEDURE 2-148 dynamic routine-name specification 2-337 executing 2-435 optimization 2-148, 2-650 registering with CREATE PROCEDURE 2-148 sysdbclose() 2-150 sysdbopen() 2-150 SPL routines as triggered action 2-233 BYTE and TEXT data types 3-15 comment indicators 1-5 debugging 3-41 defined 3-1 definition 2-146 dropping with DROP PROCEDURE 2-306 executing operating-system commands 3-39 handling multiple rows 3-37 header 3-7 in SELECT statement 2-488 limits on parameters 5-62 output file for TRACE statement 2-547 ownership of created objects 2-150 preparing 2-435 receiving data from SELECT 2-489 reoptimizing 2-650 restrictions when used with DML statements 5-71 sending mail 3-39 setting environment variables 3-40 simulating errors 3-35 SQL-statement restrictions 5-70 SPL statements, defined 3-1 sqexplain.out file 2-570, 5-40 SQL comments 1-3 compliance of statements with ANSI standard 1-8 reserved words A-1, B-1 statement types 1-6 SQL code xviii SQL Communications Area 2-69 result after CLOSE 2-68 result after DATABASE 2-255 result after DATASKIP event 2-545 result after DELETE 2-280, 4-75 result after DESCRIBE 2-282 result after DESCRIBE INPUT 2-287
SQL Communications Area (continued) result after EXECUTE 2-325, 2-440 result after FETCH 2-351 result after FLUSH 2-353 result after INSERT 4-74 result after OPEN 2-426 result after PUT 2-447 result after SELECT 2-492, 4-75 result after UPDATE 4-75 sqlca.sqlerrd1 4-74 sqlca.sqlerrd2 4-75 SQL DESCRIPTOR keywords in DESCRIBE INPUT statement 2-286 in DESCRIBE statement 2-281 in EXECUTE statement 2-322, 2-326 in FETCH statement 2-344 in OPEN statement 2-424 in PUT statement 2-442 SQL expression. See Expression. SQL Function. See Function, SQL. SQL statement cache disabling 2-598 prepared statements 2-599 qualifying criteria 2-598 SQL statements See also Statements, SQL. restrictions within SPL routines 5-70 SQL-99 standard 1-4 SQLCA. See SQL Communications Area. SQLCODE variable 2-69, 2-280, 2-351, 2-353, 2-426, 2-661 sqld value 2-349 sqlda structure in DESCRIBE 2-281, 2-282 in DESCRIBE INPUT 2-286 in EXECUTE 2-324 in EXECUTE ... INTO 2-324 in FETCH 2-349 in OPEN 2-323, 2-326, 2-424, 2-442, 2-443 in OPEN...USING DESCRIPTOR 2-428 SQLDA. See sqlda structure. SQLERRD array number of inserted rows 2-353 value of inserted SERIAL8 value 4-77, 4-80 SQLERROR keyword, in WHENEVER statement 2-659 sqlexplain.out file 2-477, 2-504 sqlhosts file 2-79 SQLJ built-in procedures 2-338 sqlj schema 2-339 SQLJ.ALTER_JAVA_PATH procedure 2-341 sqlj.install_jar procedure 5-67 SQLJ.INSTALL_JAR procedure 2-339, 5-20 SQLJ.REMOVE_JAR procedure 2-340 SQLJ.REPLACE_JAR procedure 2-340 SQLJ.SETUDTEXTNAME procedure 2-342 SQLJ.UNSETUDTEXTNAME procedure 2-342 SQLNOTFOUND error conditions with EXECUTE statement 2-325 with INSERT statement 2-404 SQLNOTFOUND value 2-521 SQLSTATE after REVOKE 2-462 list of codes 2-363 not found condition 2-280, 2-351, 2-661
X-34
SQLSTATE (continued) runtime errors 2-661 warnings 2-661 sqlstypes.h header file 2-282, 2-287 sqltypes.h file 2-361 sqltypes.h header file 2-557 SQLUNKNOWN data type 2-288 sqlvar structures 2-349 SQLWARNING keyword, in WHENEVER statement 2-659 sqlxtype.h header file 2-557 SQRT function 4-69, 4-71 srvsendrecv data type 2-138 STABILITY keyword, in SET ISOLATION statement 2-577 STACK keyword Routine Modifier segment 5-54, 5-55 STACKSIZE configuration parameter 5-59 STANDARD keyword in ALTER TABLE statement 2-63 in CREATE TABLE statement 2-171, 2-172 in SELECT statement 2-521 in SET Default Table Type statement 2-550 START keyword in CREATE SEQUENCE statement 2-166 START VIOLATIONS TABLE statement 2-609 STAT data type 4-19 Statement block segment 5-69 Statement identifier cursor for 2-271 defined 2-434 in DECLARE 2-260 in FREE 2-355 in PREPARE 2-434 releasing 2-434 STATEMENT keyword, in SET STATEMENT CACHE statement 2-597 Statement Local Variables data type of 4-114 declaration 4-113 defined 4-112 expression 4-114 name space of 4-114 OUT parameter 5-63 precedence of 4-114 scope of 4-115 using 4-114 Statements SQL ANSI-compliant 1-8 entering 1-1 extensions to ANSI standard 1-9 Statements, SQL valid only in ESQL/C 4-42 STATIC keyword in ALTER TABLE statement 2-63 in CREATE TABLE statement 2-171, 2-173 in SELECT statement 2-521 in SET Default Table Type statement 2-550 STATISTICS keyword UPDATE STATISTICS statement 2-649 STATUS keyword, in INFO statement 2-393 Status, displaying with INFO statement 2-394 STDEV function 4-116, 4-123 STEP keyword audit-event mnemonic 4-80 STEP keyword in FOR statement 3-17 STMT_CACHE configuration parameter 2-597 STMT_CACHE environment variable 2-597
STMT_CACHE_HITS configuration parameter 2-599 STMT_CACHE_NOLIMIT configuration parameter 2-599 STMT_CACHE_SIZE configuration parameter 2-599 STOP keyword, in WHENEVER statement 2-659 STOP VIOLATIONS TABLE statement 2-622 STORAGE keyword in TRUNCATE statement 2-624 Storage options, CREATE Temporary TABLE 2-213 Stored Procedure Language. See SPL Routine. Stored Procedure Languages 3-1 stores_demo database x stores_demo database. See Demonstration databases. Storing smart large objects 2-199 STRATEGIES keyword, in CREATE OPCLASS statement 2-142 Strategy functions 2-142 String-manipulation functions 4-102 Structured Query Language See SQL. STYLE keyword External Routine Reference segment 5-20 SUBCLASS_ORIGIN keyword, in GET DIAGNOSTICS statement 2-366 Subdiagram reference 4-1 Subordinate table 2-500 Subquery beginning with ALL, ANY, SOME keywords 2-510 beginning with EXISTS keyword 2-510, 4-15 beginning with IN keyword 2-510, 4-14 correlated 4-14 defined 2-508 in a table hierarchy 2-642 in Collection Subquery segment 4-3 in Condition segment 4-13 no FIRST keyword 2-484, 2-485 with DISTINCT keyword 2-486 with UNION or UNION ALL 2-525 Subscripting on character columns 2-516 Subscripting character columns 4-47 Subsecond precision 4-58 SUBSTR function 4-105 Substring in column expression 4-47 in ORDER BY clause of SELECT 2-516 SUBSTRING function 4-107, 4-108 Subtable inherited properties 2-205 restrictions 2-161 Subtype creating 2-159 dropping 2-310 SUM function 4-116, 4-122 superstores_demo database xi Supertable updating 2-642 Supertype creating 2-159 dropping 2-310 Support functions assigning 2-139, 2-401, 2-411, 2-645 comparing 2-139 defined 2-143 defining 2-138 destroy 2-139, 2-279, 2-315 Index
X-35
Support functions (continued) export 2-139 exportbinary 2-139 import 2-139 importbinary 2-139 input 2-138 lohandles 2-139 output 2-138 receive 2-138 send 2-138 specifying in CREATE OPCLASS 2-143 SUPPORT keyword, in CREATE OPCLASS statement 2-141 Synonym chaining 2-170 creating 2-168 difference from alias 2-168 dropping 2-313 external 2-314 SYNONYM keyword in DROP SYNONYM statement 2-313 Syntax diagrams conventions for xv keywords in xvii reading in a screen reader C-1 variables in xviii Syntax segment xvii sysaggregates system catalog table 2-84, 2-295 sysams system catalog table 2-9, 2-82, 2-124, 2-294 columns 5-47 sysblobs system catalog table 2-207 syscasts system catalog table 2-87, 2-296 syschecks system catalog table 2-449 syscolattribs system catalog table 2-200 syscolauth system catalog table 2-158 syscolumns system catalog table 2-207, 2-310, 2-396, 2-556, 2-650 sysconstraints system catalog table 2-59, 2-118, 2-302, 2-452 sysdbclose() procedure 2-151, 2-567 sysdbopen() procedure 2-150, 2-151, 2-527, 2-549, 2-550, 2-567, 2-587 sysdirectives system catalog table 2-477 sysdistrib system catalog table 2-421, 2-650, 2-652 sysfragauth system catalog table 2-155, 2-389, 2-392 sysfragments system catalog table 2-13, 2-18, 2-207, 2-452, 4-74 sysindexes system catalog table 2-182, 2-302, 2-452, 2-650, 2-652 sysinherits system catalog table 2-207, 2-310 sysmaster database 2-207, 2-420, 4-75, 4-119 sysobjstate system catalog table 2-452, 2-539 sysprocauth system catalog table 2-110, 2-149, 2-152, 2-155, 2-207 sysprocbody system catalog table 2-110, 2-148, 4-81 sysprocedures system catalog table 2-110, 2-149, 2-152, 2-339, 2-595 sysprocplan system catalog table 2-110, 2-149, 2-152, 2-656 sysroleauth system catalog table 2-155 SYSSBSPACENAME onconfig parameter 2-652 syssequences system catalog table 2-41, 2-165, 2-312 syssynonyms system catalog table 2-313 syssyntable system catalog table 2-313 systabauth system catalog table 2-155, 2-159, 2-389, 2-421 systables system catalog table 2-207, 2-302, 2-470, 2-650, 2-652, 4-74 System catalog tables owner informix 5-17 sysaggregates 2-84
System catalog tables (continued) sysams 2-9, 2-82 syscasts 2-87, 2-296 syschecks 2-449 syscolauth 2-155 syscolumns 2-310, 2-650 sysconstraints 2-118, 2-182, 2-302, 2-452 sysdepend 2-318 sysdirectives 2-476 sysdistrib 2-650 sysfragauth 2-155, 2-389, 2-392 sysfragments 2-13, 2-207, 2-452 sysindexes 2-182, 2-452, 2-650 sysinherits 2-310 sysobjstate 2-452, 2-539 sysprocauth 2-110, 2-149, 2-152, 2-155 sysprocbody 2-149, 2-152, 4-81 sysprocedures 2-149, 2-152, 2-595 sysprocplan 2-110, 2-149, 2-152, 2-656 sysroleauth 2-155 syssequences 2-41, 2-42, 2-165, 2-312 syssynonyms 2-313 syssyntable 2-313 systabauth 2-155, 2-159, 2-387, 2-389 systables 2-302, 2-310, 2-470, 2-650 systriggers 2-219, 2-316 sysusers 2-155, 2-160 sysviews 2-248, 2-449, 2-455 sysviolations 2-609 sysxadatasources 2-252, 2-319 sysxasourcetypes 2-253, 2-320 sysxtdtypeauth 2-136, 2-155, 2-159, 2-207, 2-379 sysxtdtypes 2-136, 2-159, 2-310, 2-361, 2-379 System catalogs creating 2-90 dropping tables 2-315 System clock 4-53, 4-60 System constants 4-53 System index 2-129, 5-35 System name database qualifier 5-15 SYSTEM statement 3-39 System-descriptor area assigning values 2-554 creating 2-6 deallocating 2-258 item descriptors 2-6 OPEN using 2-326, 2-428, 2-445 resizing 2-554 use with EXECUTE statement 2-327 with ALLOCATE DESCRIPTOR 2-6 with DESCRIBE 2-283 with DESCRIBE INPUT 2-288 with EXECUTE ... INTO 2-324 System-diagnostics area 2-351 System-monitoring interface tables 4-75 System-Monitoring Interface tables 2-627, 4-119 systriggers system catalog table 2-219, 2-316 sysucolauth system catalog table 2-421 sysusers system catalog table 2-155, 2-160, 2-207, 2-394 sysutils database 2-420 sysviews system catalog table 2-248, 2-449, 2-455 sysviolations system catalog table 2-609 sysxadatasources system catalog table 2-252, 2-319 sysxasourcetypes system catalog table 2-253, 2-320 sysxtdtypeauth system catalog table 2-94, 2-136, 2-155, 2-207, 2-379
X-36
sysxtdtypes system catalog table 2-94, 2-136, 2-160, 2-207, 2-310, 2-317, 2-361, 2-379, 2-558, 2-559 DESCRIBE and GET DESCRIPTOR with 2-360
T
tabid column 2-21 Table adding a constraint 2-59 adding a constraint to a column with data 2-56 alias in SELECT 2-492 child 2-179 creating 2-171 creating a synonym for 2-168 default privileges 2-389 defining fragmentation strategy 2-190 deleting all rows 2-624, 2-628 diagnostics 2-219, 2-617 diagnostics table 2-622 dominant 2-500 dropping 2-299, 2-314 dropping a synonym 2-313 dropping a system table 2-315 duplicate 2-96, 2-299 external 2-98, 2-219, 2-523, 5-35 fragmented 2-12 inheritance 2-220, 2-225 inheritance hierarchy 2-204, 2-637 isolating 2-189 joins in Condition segment 2-511 loading data with the LOAD statement 2-407 locking with ALTER INDEX 2-33 with LOCK TABLE 2-413 logging 2-210 memory-resident 2-590, 2-601 moving 2-419 nonfragmented 2-22 operational 2-219 parent 2-179 permanent 2-214 privilege 2-12 privileges granting 2-374 moving 2-422 privileges on 2-206, 2-460 qualifiers 5-18 raw 2-219 read-only 2-96 renaming 2-420, 2-421, 2-454 residency status 2-590, 2-601 scratch 2-209, 2-219, 2-524 static 2-219, 2-603 subordinate 2-500 system catalog 2-219 target 2-609, 2-613, 2-622 temporary 2-210, 2-219, 2-522, 2-650 typed 2-158, 2-205 unlocking 2-635 untyped 2-205 updating statistics 2-650 violations 2-219, 2-612 violations table 2-622 virtual 5-6 waiting for a locked table 2-580 Table expression 2-494 Table format, in SET Database Object Mode statement
TABLE keyword Collection-Derived-Table segment 5-5 in ALTER FRAGMENT statement 2-11 in ALTER TABLE statement 2-62 in CREATE DUPLICATE statement 2-96 in CREATE EXTERNAL TABLE statement 2-98 in CREATE TABLE statement 2-203 in CREATE Temporary TABLE statement 2-164 in Data Type segment 4-25 in DROP DUPLICATE statement 2-299 in DROP TABLE statement 2-314 in LOCK TABLE statement 2-413 in MOVE TABLE statement 2-419 in RENAME TABLE statement 2-454 in SELECT statement 2-499 in SET Residency statement 2-590 in SET TABLE statement 2-601 in START VIOLATIONS TABLE statement 2-609 in STOP VIOLATIONS TABLE statement 2-622 in TRUNCATE (XPS) statement 2-628 in TRUNCATE statement 2-624 in UNLOCK TABLE statement 2-635 in UPDATE STATISTICS statement 2-649 Table name INFO statement 2-393 TABLE_SPACE keyword in SET Default Table Space statement 2-549 TABLE_TYPE keyword in SET Default Table Type statement 2-550 Table-level locking, in CREATE TABLE 2-203 Table-level privileges 2-386 TABLES keyword, in INFO statement 2-393 TAN function 4-100, 4-101 Target table relationship to diagnostics table 2-613, 2-622 relationship to violations table 2-613, 2-622 TCP/IP connections 2-280, 2-402, 2-487, 2-645 TEMP keyword in CREATE Temporary TABLE statement 2-209 in SELECT statement 2-520 in SET Default Table Space statement 2-549 in SET Default Table Type statement 2-550 Temp table. See Temporary table. TEMP_TAB_EXT_SIZE keyword, in SET ENVIRONMENT statement 2-566 TEMP_TAB_NEXT_SIZE keyword, in SET ENVIRONMENT statement 2-566 Temporary dbspace 2-210, 2-214 Temporary tables and fragmentation 2-213 constraints allowed 2-211 creating 2-209 creating constraints for 2-211 default location 2-549 default type 2-550 defining columns 2-210 differences from permanent tables 2-214 duration 2-215 extent size 2-566 implicit creation 2-550 INFO statement restrictions 2-215 system-generated 2-566 updating statistics 2-650 when deleted 2-215 where stored 2-214
2-541 Index
X-37
TEST keyword, in SAVE EXTERNAL DIRECTIVES statement 2-476 TEXT column, modifying 2-55 TEXT data type declaration syntax 4-25 default value 2-175 loading 2-408 SPL routines 3-8, 3-15 storage location 4-25 unloading 2-631, 2-632 with SET DESCRIPTOR 2-559 TEXT keyword in CREATE EXTERNAL TABLE statement 2-99 in Data Type segment 4-25 in DEFINE statement 3-7 Return Clause segment 5-51 THEN keyword in CASE statement 3-4 in Expression segment 4-50, 4-51 in IF statement 3-25 in MERGE statement 2-416 Thread-safe application defined 2-292, 2-535, 2-537 Threads defined 2-535 in multithreaded application 2-535 THREADS keyword, in EXECUTE FUNCTION statement 2-333 Time and date, getting current 4-58 Time data types 4-27 Time function restrictions with GROUP BY 2-513 use in Function Expressions 4-96 use in SELECT 2-487 TIME keyword in ALTER TABLE statement 2-58 in CREATE TABLE statement 2-199 Time unit DATETIME data types 4-132 INTERVAL data types 4-127 Time zone 2-565 of CURRENT operator 4-59 of TODAY operator 4-58 times() operator function 4-40 TMPSPACE_LIMIT keyword in SET ENVIRONMENT statement 2-566 TO CLUSTER keywords, in ALTER INDEX statement TO keyword in ALTER FRAGMENT statement 2-28 in ALTER INDEX statement 2-33 in CONNECT statement 2-75 in DATETIME field qualifier 4-32 in Expression segment 4-96 in FOR statement 3-17 in GRANT FRAGMENT statement 2-388 in INTERVAL Field Qualifier segment 4-127 in MOVE TABLE statement 2-419 in OUTPUT statement 2-431 in RENAME COLUMN statement 2-449 in RENAME DATABASE statement 2-451 in RENAME INDEX statement 2-452 in RENAME SEQUENCE statement 2-453 in RENAME TABLE statement 2-454 in SET ALL_MUTABLES statement 2-527 in SET DEBUG FILE statement 2-547 in SET Default Table Space statement 2-549 in SET Default Table Type statement 2-550
2-33
TO keyword (continued) in SET EXPLAIN statement 2-568 in SET ISOLATION statement 2-575 in SET LOCK MODE statement 2-580 in SET PLOAD FILE statement 2-589 in SET SESSION AUTHORIZATION statement 2-595 in UNLOAD statement 2-630 in WHENEVER statement 2-662 TO_CHAR function 4-96, 4-99 TO_DATE function 4-96 function, SQL TO_DATE 4-100 TOC Notes xx TODAY function as expression 4-53 in ALTER TABLE statement 2-46 in Condition segment 4-9 in CREATE TABLE statement 2-174 in DEFINE statement 3-9 in INSERT 2-398, 2-403 TRACE statement 3-41 output file 2-547 TRAILING keyword, in TRIM expressions 4-102 TRANSACTION keyword in CONNECT statement 2-75 in SET TRANSACTION statement 2-602 Transaction mode constraints 2-606 Transactions access mode 2-604 defined 2-67 example 2-67, 2-268 read-only 2-604 statements that initiate 2-596 using cursors in 2-274 Trigger inherited 2-220, 2-225 overriding 2-225 Trigger action 2-216 Trigger event 2-216 DELETE 2-222, 2-243 INSERT 2-229, 2-246 privileges on 2-221 SELECT 2-223, 2-245 UPDATE 2-222, 2-243 TRIGGER keyword in DROP TRIGGER statement 2-316 Triggered action action statements 2-233 cascading 2-228 correlation names 2-239 effect of cursors 2-221 for multiple triggers 2-228, 2-245 list of actions 2-232 on triggering view 2-244 syntax for views 2-244 WHEN condition 2-232 Triggering statement consistent results 2-233 performance 2-222 UPDATE 2-223 Triggering view 2-244 Triggers affected by dropping a column from table 2-53 affected by modifying a column 2-57 enabling or disabling 2-541 overriding 2-241
X-38
TRIGGERS keyword, in SET Database Object Mode statement 2-540, 2-541, 2-608 Trigonometric function ACOS function 4-101 ASIN function 4-101 ATAN function 4-102 ATAN2 function 4-102 COS function 4-101 SIN function 4-101 TAN function 4-101 TRIM function 4-102 TRUNC function 4-69, 4-71 TRUNCATE statement 2-624, 2-628 Trusted Facility feature 4-80 Two-phase commit operations 2-252 TYPE field changing from BYTE or TEXT 2-559 in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-555 setting in X/Open programs 2-557 with DESCRIBE INPUT statement 2-288 with DESCRIBE statement 2-283 with X/Open programs 2-359 Type hierarchy 2-159 TYPE keyword See also TYPE field. in ALTER TABLE statement 2-63, 2-64 in CREATE DISTINCT TYPE statement 2-93 in CREATE ROW TYPE statement 2-158 in CREATE TABLE statement 2-204 in CREATE VIEW statement 2-247 in CREATE XADATASOURCE TYPE statement 2-253 in DROP ROW TYPE statement 2-310 in DROP TYPE statement 2-317 in DROP XADATASOURCE TYPE statement 2-320 in GRANT statement 2-378 in REVOKE statement 2-462 Typed collection variable 2-4, 3-13 Typed table ADD TYPE clause 2-64 altering 2-65 altering serial columns 2-55, 2-161 inheritance 2-205 NOT NULL constraint 2-160 Typed view 2-248 Typographical conventions xiv
U
UDR. See User-Defined Routine. Unary minus operator (-) 4-136, 4-137 Unary plus operator (+) 4-136, 4-137 Unbuffered logging 2-582 UNCOMMITTED keyword in SET TRANSACTION statement 2-603 Uncommitted row 2-576 UNDEFINED parameter value 5-3 UNDER keyword in CREATE ROW TYPE statement 2-158 in CREATE TABLE statement 2-204 in GRANT statement 2-378 in REVOKE statement 2-459, 2-462 UNDER ON TYPE keywords in GRANT statement 2-378 in REVOKE statement 2-462 Under privilege 2-376, 2-379, 2-460, 2-462
Underscore (_) as wildcard 4-11 Unicode 2-176, 2-532, 4-20 UNION operator in collection subquery 4-4 in SELECT statement 2-479, 2-524 OUT parameter and 4-115 restrictions on use 2-404, 2-525 Union view 2-249 Unique constraint dropping 2-61 moving 2-421 rules of use 2-177 UNIQUE keyword in ALTER TABLE statement 2-47, 2-59 in CREATE INDEX statement 2-117, 2-132 in CREATE TABLE statement 2-176, 2-184 in CREATE Temporary TABLE statement 2-211 in Expression segment 4-116, 4-125 in SELECT 2-486 in subquery 4-14 Units of time, INTERVAL values 4-136 UNITS operator 4-53 UNIX operating system default locale x home directory 2-110 shell script 3-40 UNKNOWN truth values 4-16 UNLOAD statement 2-630 UNLOAD TO file 2-630 UNLOCK TABLE statement 2-635 Unnamed row data types field definition 4-30 unloading 2-631, 2-633 updating fields 2-647 Untyped collection variable 2-4, 2-257, 2-497, 4-30, 5-7, 5-11 Untyped row variable 2-8 Untyped view 2-248 Updatable view 2-251, 2-278 Update cursor locking considerations 2-265 opening 2-425 restricted statements 2-269 use in DELETE 2-278 UPDATE keyword in CREATE TRIGGER statement 2-222 in DECLARE statement 2-260 in GRANT FRAGMENT statement 2-389 in GRANT statement 2-376 in MERGE statement 2-416 in REVOKE FRAGMENT statement 2-472 in REVOKE statement 2-459 in SELECT statement 2-479, 2-519 in SET ISOLATION statement 2-577 in UPDATE STATISTICS statement 2-649 Update locks 2-578 Update privilege 2-376, 2-460 Update privilege, with a view 2-638 UPDATE statements 2-636 and triggers 2-233 collection variables 5-10 cursor with 2-264 distributed 2-645 OUT parameter and 4-115 SET clause 2-639 single-column SET clause 2-639 smart large objects with 4-48 Index
X-39
UPDATE statements (continued) specifying support 5-48 update triggers 2-220 updating through a view 2-638 with SELECT...FOR UDATE 2-519 with FETCH 2-349 UPDATE STATISTICS statement 2-649 distribution bins 2-655 dropping data distributions 2-652 examining index pages 2-651 only one table in hierarchy 2-650 specifying distributions only 2-655 using when upgrading database server 2-657 Update trigger 2-223, 2-244 Updating a specific table in a table hierarchy 2-637 Upgrading the database server 2-650, 2-657 UPPER function 4-109 Uppercase characters, converting to 4-109 USAGE keyword in GRANT statement 2-378, 2-381 in REVOKE statement 2-462, 2-464 USAGE ON LANGUAGE keywords in GRANT statement 2-381 in REVOKE statement 2-464 USAGE ON TYPE keywords in GRANT statement 2-378 in REVOKE statement 2-462 USE_HASH keyword, in Optimizer Directives Segment 5-38 USE_NL keyword, in Optimizer Directives Segment 5-38 USEOSTIME configuration parameter 4-58 USER function as constant expression 4-53 defined 4-56 in ALTER TABLE statement 2-46 in Condition segment 4-9 in CREATE TABLE statement 2-174 in DEFINE statement 3-9 in INSERT statement 2-398, 2-403 in Literal Row segment 4-139 User informix 2-93, 2-296, 2-386, 2-468, 2-590, 5-44 privileges associated with 2-374 User name case-sensitivity 2-372, 2-596 using another name 2-595 User-defined access method creating 2-82 modifying 2-9 User-defined aggregates creating 2-84 defined 4-117 dropping 2-295 invoking 4-125 User-defined data types 4-27 maximum in one row 2-174, 4-28 privileges 2-378, 2-379, 2-463 User-defined function 4-111 arguments 5-2 cursor 2-331 inserting data with 2-404 iterator 5-57 negator 5-57 noncursor 2-331 OUT parameter 4-115 selectivity 5-59 variant 5-20, 5-60
User-defined routines arguments 2-137, 5-2 defined 2-146 dropping with DROP ROUTINE 2-308 EXTEND role 2-386, 2-468 ill-behaved 5-56 in SELECT statements 2-488 inserting data with 2-404 ownership of created objects 2-112, 5-19 privileges 2-380, 2-463 REFERENCES keyword with BYTE or TEXT data type 3-15 RESTRICTED mode 2-595 return values 5-51 User-defined VP class 5-56 USETABLENAME environment variable 2-43, 2-314, 2-625 USING BITMAP keywords, in CREATE INDEX statement 2-116 USING DESCRIPTOR keywords in EXECUTE 2-326 in FETCH 2-349 in OPEN 2-424 in PUT 2-327 USING keyword in CREATE EXTERNAL TABLE statement 2-98 in CREATE INDEX statement 2-116, 2-123 in CREATE TABLE statement 2-202 in CREATE XADATASOURCE statement 2-252 in DELETE statement 2-275 in EXECUTE statement 2-322, 2-326 in FETCH statement 2-344 in INTO EXTERNAL clause 2-520 in MERGE statement 2-416 in OPEN statement 2-424, 2-427 in PUT statement 2-442 in START VIOLATIONS TABLE statement 2-609 USING SQL DESCRIPTOR keywords in DESCRIBE INPUT statement 2-286, 2-288 in DESCRIBE statement 2-281, 2-283 in EXECUTE statement 2-327 UTF-8 locale 2-176, 2-532, 4-20 Utilities dbaccess x dbschema 2-41 oncheck 2-139, 4-89 ondblog 2-582 oninit 4-76 onmode 2-528, 2-599 onspaces 2-12, 2-126, 2-390, 2-472 onstat 2-527, 2-545, 2-565, 2-600 onutil 2-102, 2-106, 4-89 setenv 2-80 setnet32 2-79
V
V option of oninit 4-76 VALUE clause after null value is fetched 2-360 relation to FETCH 2-359 use in GET DESCRIPTOR 2-359 use in SET DESCRIPTOR 2-555 VALUE keyword in GET DESCRIPTOR statement 2-357 in SET DESCRIPTOR statement 2-554 VALUES clause effect with PUT 2-443
X-40
VALUES clause (continued) in INSERT statement 2-398 in MERGE statement 2-416 VALUES keyword in MERGE statement 2-416 VALUES keyword, in INSERT statement 2-398 VARCHAR data type 4-19, 4-20 in LOAD statement 2-410 in UNLOAD statement 2-631 VARIABLE keyword, in CREATE OPAQUE TYPE statement 2-136 Variable-length UDT 2-174, 4-28 Variables declaring in SPL 3-7 default values in SPL 3-9, 3-12 global 3-9 local 3-7, 3-11 PROCEDURE type 3-14 uninitialized 3-43, 4-8 unknown values in IF 3-26 Variables, in syntax diagrams xviii VARIANCE function 4-116, 4-123 Variant function 2-51, 5-20, 5-60 VARIANT keyword External Routine Reference segment 5-20 Routine Modifier segment 5-55 Varying-length opaque data type 2-137 version option of DBINFO 4-76 View affected by dropping a column 2-53 affected by modifying a column 2-57 creating a synonym for 2-168 creating a view 2-247 dependent 2-318 dropped by ALTER FRAGMENT statement 2-17 dropping 2-318 materialized 2-248 privileges 2-378 typed 2-158, 2-248 union 2-249 untyped 2-248 updatable 2-251 updating 2-638 VIEW keyword in CREATE VIEW statement 2-247 in DROP VIEW statement 2-318 VIOLATIONS keyword in START VIOLATIONS TABLE statement 2-609 in STOP VIOLATIONS TABLE statement 2-622 Violations table creating 2-609 effect on transactions 2-610 examples 2-615, 2-622 filtering mode 2-542 how to stop 2-622 privileges 2-614 relationship to diagnostics table 2-184, 2-613 relationship to target table 2-613 restriction on dropping 2-315 restriction on moving 2-420, 2-422 schema 2-612 Virtual column 2-249 Virtual index 2-82 Virtual processors 2-565 Virtual table 5-6 Virtual-processor class 5-56
C-1
W
WAIT keyword, in SET LOCK MODE statement WARNING keyword in SET ISOLATION statement 2-576 WEEKDAY function 4-96 Well-behaved C UDRs 5-56 WHEN keyword in CASE statement 3-4 in CREATE TRIGGER statement 2-232 in Expression segment 4-50, 4-51 in MERGE statement 2-416 WHENEVER statement syntax and use 2-659 WHERE clause in SELECT 2-479 in system descriptor area 2-6 joining tables 2-511 with a subquery 2-508 with ALL keyword 2-510 with ANY keyword 2-510 with BETWEEN keyword 2-508 with IN keyword 2-508 with IS keyword 2-509 with relational operator 2-508 with SOME keyword 2-510 WHERE CURRENT OF keywords in DELETE statement 2-275 in UPDATE statement 2-636 optimizer directives 5-35 WHERE keyword in CREATE INDEX statement 2-134 in DELETE statement 2-275 in SELECT statement 2-507 in UPDATE statement 2-636 WHILE keyword in CONTINUE statement 3-6 in EXIT statement 3-16 WHILE statement 3-43 Whitespace characters delimited dentifiers 5-24 SQL statements 1-1 Wildcard character asterisk (*) 4-12 backslash (\) 4-11, 4-12 brackets ( [ ] ) 4-12 caret (^) 4-12 percent sign (%) 4-11 question mark (?) 4-12 underscore (_) 4-11 with LIKE 2-509, 4-11 with LIKE or MATCHES 4-145 with MATCHES 2-509, 4-12 Windows batch file 3-40 default locale x sqlhosts subkey 2-79 WITH APPEND keywords in SET DEBUG FILE statement 2-547 in SET EXPLAIN statement 2-568 in SET PLOAD FILE statement 2-589 2-580
Index
X-41
WITH BUFFERED LOG keyword in CREATE DATABASE 2-90 WITH CHECK OPTION keywords, in CREATE VIEW statement 2-247, 2-250 WITH CONCURRENT TRANSACTION keywords in CONNECT statement 2-77 WITH CRCOLS keywords, in CREATE Temporary TABLE statement 2-212 WITH ERROR keywords in ALTER TABLE statement 2-49 in CREATE INDEX statement 2-130 in CREATE TABLE statement 2-182, 2-184 in SET Database Object Mode statement 2-541 WITH GRANT OPTION keywords in GRANT FRAGMENT statement 2-388 in GRANT statement 2-371 WITH HOLD keywords in DECLARE statement 2-260 in FOREACH statement 3-20 WITH keyword in ALLOCATE DESCRIPTOR statement 2-6 in ALTER FRAGMENT statement 2-22 in ALTER FUNCTION statement 2-31 in ALTER PROCEDURE statement 2-35 in ALTER ROUTINE statement 2-37 in ALTER SEQUENCE statement 2-41 in CONNECT statement 2-75 in CREATE AGGREGATE statement 2-84 in CREATE CAST statement 2-87 in CREATE DATABASE statement 2-90 in CREATE FUNCTION statement 2-107 in CREATE INDEX statement 2-130 in CREATE PROCEDURE statement 2-145 in CREATE SEQUENCE statement 2-166 in CREATE VIEW statement 2-247, 2-250 in DECLARE statement 2-260 in GRANT FRAGMENT statement 2-388 in GRANT statement 2-371 in MOVE TABLE statement 2-419 in OPEN statement 2-424 in SET CONSTRAINTS statement 2-538 in SET Database Object Mode statement 2-541 in SET DEBUG FILE statement 2-547 in SET ENCRYPTION PASSWORD statement 2-560 in SET EXPLAIN statement 2-568 in SET INDEXES statement 2-574 in SET ISOLATION statement 2-576 in SET PLOAD FILE statement 2-589 WITH LISTING IN keyword in CREATE FUNCTION statement 2-107 WITH LISTING IN keywords in CREATE PROCEDURE statement 2-145 WITH LOG keyword in CREATE DATABASE statement 2-90 WITH LOG MODE ANSI keyword in CREATE DATABASE 2-90 WITH MAX keywords in ALLOCATE DESCRIPTOR statement 2-6 WITH NO LOG keywords in CREATE Temporary TABLE statement 2-209 in SELECT statement 2-520, 2-523 WITH REOPTIMIZATION keywords in OPEN statement 2-424 WITH RESUME keywords in ON EXCEPTION statement 3-31 in RETURN statement 3-37
WITH ROWIDS keywords in ALTER FRAGMENT statement 2-21 in CREATE TABLE statement 2-190 WITH WARNING keywords in SET ISOLATION statement 2-576 WITHOUT ERROR keywords in ALTER TABLE statement 2-49 in CREATE INDEX statement 2-130 in CREATE TABLE statement 2-182, 2-184 in SET Database Object Mode statement 2-541 WITHOUT HEADINGS keywords in OUTPUT statement 2-431 WITHOUT keyword in BEGIN WORK statement 2-66 in CREATE INDEX statement 2-130 in OUTPUT statement 2-431 in SET CONSTRAINTS statement 2-538 in SET Database Object Mode statement 2-541 in SET INDEXES statement 2-574 Word length (32-bit or 64-bit) 4-77 WORK keyword in BEGIN WORK statement 2-66 in COMMIT WORK statement 2-73 in ROLLBACK WORK statement 2-474 WORK WITHOUT REPLICATION keywords, in BEGIN WORK statement 2-66 WRITE keyword in SET TRANSACTION statement 2-602 Write lock 2-265 Writing direction 4-105
X
X for storage in an extent space 2-83, 5-47 X/Open DTP XA standard 2-252, 2-253 X/Open mode CONNECT statement 2-81 FETCH statement 2-345 GET DESCRIPTOR statement 2-359 OPEN statement 2-428 SET DESCRIPTOR statement 2-557 XA data source privileges to create 2-252 privileges to drop 2-319 XA data source type creating 2-253 dropping 2-320 registering 2-253 XA Switch Structure 5-50 xa.h file 5-50 XADATASOURCE keyword in CREATE XADATASOURCE statement 2-252 in CREATE XADATASOURCE TYPE statement 2-253 in DROP XADATASOURCE statement 2-319 in DROP XADATASOURCE TYPE statement 2-320 XID data type 4-19 xopen compiler option 2-557
Y
Y value of MORE field YEAR function defined 4-96 SQL 4-98 2-365
X-42
YEAR keyword in DATETIME data type 4-32 in DATETIME Field Qualifier 4-132 in INTERVAL data type 4-135 in INTERVAL Field Qualifier 4-127 YES NODEFDAC setting 2-378
Z
Zero 4-98 CLIENT_TZ setting 2-565 EXT_DIRECTIVES setting 2-476 IFX_EXTDIRECTIVES setting 2-477 in UNLOAD file 2-631 invalid divisor 2-364 longitude 2-565 OPTCOMPIND setting 2-567 prohibited MOD divisor 4-70 returned value 4-90 scale and ROUND function 4-71 scale and TRUNC function 4-71 setting of STMT_CACHE_NOLIMIT 2-599 sqlca value after INSERT 2-404 subseconds and CURRENT function 4-58 Sunday and WEEKDAY function 4-98 sysdirectives.active value 2-477 to specify next serial value 2-400 variance and STDEV function 4-123 variance and VARIANCE function 4-123 Zoned decimal 2-100 ZONED keyword in CREATE EXTERNAL TABLE statement 2-99
Index
X-43
X-44
Printed in USA
G251-2284-02
Spine information:
IBM Informix
Version 10.0/8.5