Teradata RDBMS for UNIX SQL Reference

The Teradata RDBBS for UNIX SQL Reference describes the Teradata Structured Query Language (SQL) and how to use SQL to d

387 7 2MB

English Pages 913 Year 1997

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Teradata RDBMS for UNIX SQL Reference

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Teradata RDBMS for UNIX SQL Reference BD10-4957-C

Teradata® RDBMS for UNIX® SQL Reference Version 2 Release 2.0.2

BD10-4957-C 01.00.00 June, 1997

The program product(s) described in this book is a licensed product of NCR Corporation. BYNET is a registered trademark of Teradata Corporation CICS, CICS/ESA, CICS/VS, DATABASE2, DB2, IBM, MVS/ESA, MVS/XA, QMS, RACF, SQL/DS, VM/XA, and VTAM are trademarks or registered trademarks of International Business Machines Corporation in the U. S. and other countries. DBC/1012 is a registered trademark of Teradata Corporation. DEC, VAX, MicroVax, and VMS are registered trademarks of Digital Equipment Corporation. EXCELAN is a trademark of Excelan, Incorporated. HEWLETT-PACKARD is a registered trademark of Hewlett-Packard Company, INTELLECT and KBMS are trademarks of Trinzic Corporation. INTERTEST is a registered trademark of Computer Associates International, Inc. ISO is a trademark of International Standards Organization. MICROSOFT, MS-DOS, DOS/V, and WINDOWS are registered trademarks of Microsoft Corporation. SABRE is a trademark of Seagate Technology, Inc. SAS and SAS/C are registered trademarks of SAS Institute Inc. SUN and SUN OS are trademarks of Sun Microsystems, Incorporated. TCP/IP protocol is a United States Department of Defense Standard ARPANET protocol. TERADATA is a registered trademark of Teradata Corporation. UNIX is a registered trademark of UNIX System Laboratories. YNET is a registered trademark of Teradata Corporation. X/Open and the X device are trademarks of X/Open Company Limited. XNS is a trademark of Xerox Corporation. The information in this document is subject to change without notice. NCR Corporation (NCR) assumes no responsibility for any errors that may appear in this document. While every effort has been made to ensure the accuracy of all information in this document, NCR assumes no liability to any party for any loss or damage caused by errors or omissions or by statements of any kind in this document, its updates, supplements, or special editions, whether such errors are omissions or statements resulting from negligence, accident, or any other cause. NCR further assumes no liability arising out of the application or use of any product or system described herein; nor any liability for incidental or consequential damages arising from the use of this document. NCR disclaims all warranties regarding the information contained herein, whether expressed, implied or statutory, including implied warranties of merchantability or fitness for a particular purpose. NCR makes no representation that the interconnection of products in the manner described herein will not infringe on existing or future patent rights, nor do the descriptions contain herein imply the granting or license to make, use, or sell equipment constructed in accordance with this description. All features, functions, and operations described herein may not be marketed by NCR in all parts of the world. Therefore, before using this information product, consult your NCR representative or NCR office for information that is applicable and current. To maintain the quality of our information products, we need your comments on the accuracy, clarity, organization, and value of this book. Please complete the User Feedback Form and mail or e-mail the form to: [email protected] Information Engineering NCR Corporation 100 North Sepulveda Boulevard El Segundo, CA 90245-4361 U.S.A. Copyright © 1997 By NCR Corporation Dayton, Ohio U.S.A. All Rights Reserved Printed in U.S.A.

About This Book

About This Book

0

The Teradata RDBBS for UNIX SQL Reference describes the Teradata Structured Query Language (SQL) and how to use SQL to do any of the following things:

Purpose

● ● ●

Manipulate data Define or restructure the database Assign or revoke access to the data.

This preface describes the organization of the Teradata RDBMS for UNIX SQL Reference and identifies information you should know before using it. This manual should be used in conjunction with the Teradata RDBMS for UNIX Database Design and Administration manual. System administrators, database administrators, security administrators, NCR field engineers, end users, and other technical personnel responsible for designing, maintaining, and using the Teradata RDBMS (database system), will find this manual useful.

Audience

How This Book Is Organized

This manual has nine chapters, ten appendixes, and an index. ● 0













Chapter 1, “Notation,”describes the notation and conventions used in this manual. Chapter 2, “Data Handling Fundamentals,”gives an overview of data manipulation, SQL requests, and basic information about using the database. Chapter 3, “Joins: Working with Multiple Tables,”describes Inner, Outer, and Cross Joins, and Self-Joins. Chapter 4, “Teradata SQL Lexicon,”discusses SQL words used, delimiters and separators, constants and operators. Chapter 5, “Data Definition,”describes data types and their attributes and format phrases, and how to convert from one data type to another. Chapter 6, “SQL Expressions,”describes the SQL arithmetic, character string, logical and conditional expressions, as well as built-in values such as DATE and TIME. Chapter 7, “Queries: The SELECT Statement,”describes the SELECT statement and associated clauses (GROUP BY, FROM, WITH, WHERE, ORDER BY, etc.) as well as the query expressions (set operators) UNION, INTERSECT, and EXCEPT (MINUS).

Teradata RDBMS for UNIX SQL Reference

i

About This Book ●





















ii

Chapter 8, “Teradata SQL Syntax Guide,”provides, in alphabetical order, details of the main SQL statements. Each description contains a brief explanation of the command function, syntax, usage notes, and one or more examples of how the command is used. Chapter 9, “Advanced SQL,”describes a few more advanced Teradata SQL features such as transaction semantics, the SQL flagger, correlated subqueries, referential integrity, EXPLAIN examples, and updatable cursors. Appendix A, “Teradata SQL Keywords,”lists the Teradata SQL keywords reserved for use in SQL statements. Appendix B, “Teradata SQL Versus ANSI SQL,”lists the Teradata RDBMS SQL syntax alphabetically and notes which are Entry Level, Intermediate Level, or Full ANSI, and which are Teradata extensions to ANSI. Also listed is a comparative chart of keywords and future keywords. Appendix C, “Comparing Teradata SQL and DB2,”describes similarities and differences between Teradata SQL and DB2. It includes a description of new features in DB2 Version 2 Release 2.0 and 3.0, Version 3, and Version 4. DB2 extensions not supported by Teradata SQL, and Teradata SQL extensions not supported by DB2, are outlined, as well as a comparison of reserved words. Appendix D, “Sample Personnel Database,”includes the SQL syntax used to create the sample Personnel database tables, along with some sample data listings in each table. Appendix E, “ASCII Hexadecimal Translation Tables,”lists hexadecimal code used to translate standard English language characters to and from EBCDIC to ASCII Appendix F, “Data Conversion,”lists the data types and formats of the Teradata RDBMS and various client systems including Bull HN Worldwide Information Systems, IBM, DEC VAX/VMS, and PC. Appendix G, “International and Japanese Character Support,”covers determining site attributes, user control of character sets and collation, Kanji feature sites and compatibility, European feature sites, and user-defined character sets. Appendix H, “Japanese Character Sets,”describes the internal and external Kanji feature character ranges supported by the Teradata RDBMS for Japan with any combination of IBM, UNIX, and DOS/V client systems. Appendix I, “Non-valid Japanese Character Code,”lists nonvalid ranges for object names for KanjiEBCDIC, KanjiEUC, and KanjiShift-JIS.

Teradata RDBMS for UNIX SQL Reference

About This Book ●

Prerequisites

0

Appendix J, “VARGRAPHIC Conversion,”lists the conversion of a single byte character to its double byte equivalent during the VARGRAPHIC function usage.

If you are not familiar with the Teradata RDBMS, you will find it useful to read the Introduction to the Teradata RDBMS for UNIX, before reading this document. The Teradata RDBMS for UNIX Database Design and Administration is a useful parallel text to this one.

Teradata RDBMS for UNIX SQL Reference

iii

Changes to This Book

Changes to This Book Introduction

Changes made to the SQL Reference include: ● ●



iv

DR Maintenance. Revision to some syntax diagrams to reflect a new presentation style. Large scale reformatting of the book.

Teradata RDBMS for UNIX SQL Reference

List of Acronyms

List of Acronyms The following acronyms, listed in alphabetical order, are applicable to this release: Acronym

Explanation

AP

Application Processor

API

Application Programming Interface

AMP

Access Module Processor Access Module Process in V2R2.0 and future releases.

APPC

Advanced Program to Program Communication

ASF

Archive Storage Facility

AWS

Administration WorkStation

BTEQ

Basic TEradata Query facility

BYNET

Banyan Network - High speed interconnect

CLI

Call Level Interface

CNS

Console Subsystem

cs0, cs1, cs2, cs3

Four code sets (codeset 0, 1, 2, and 3) used in EUC encoding for Japanese character support.

DBA

Database Administrator

DBC

Database Computer (Teradata)

DBMS

Database Management System

DBW

Database Window

DSU

Disk Storage Unit

FK

Foreign Key

LAN

Local Area Network

LT/ST

Large Table/Small Table (join)

LUN

Logical Unit (disk array logical unit)

MBC

Multibyte Character

MBCS

Multibyte Character Set

MPP

Massively Parallel Processing

NIST

National Institute of Standards and Technology

NN

No Nulls

NUPI

Non-Unique Primary Index

Teradata RDBMS for UNIX SQL Reference

v

List of Acronyms

vi

Acronym

Explanation

NUSI

Non-Unique Secondary Index

OLAP

On-Line Analytical Processing

OLTP

On-Line Transaction Processing

OSF

Open Software Foundation

OSI

Open Systems Interconnect

PDE

Parallel Database Extensions

PE

Parsing Engine

PK

Primary Key

POSIX

Portable Operating System Interface for Computing Environments

POST

Power On System Test

QIC

Quarter Inch Cartridge

RAID

Redundant Array of Inexpensive Disks

RDBMS

Relational Database Management System

RISC

Reduced Instruction Set Computing

SBC

Single Byte Character

SBCS

Single Byte Character Set

SCSI

Small Computer System Interface

SMP

Symmetrical Multi-Processor

SQL

Structured Query Language

TCP/IP

Transmission Control Protocol/Internet Protocol

TOS

Teradata Operating System

TPA

Trusted Parallel Application

vproc

Virtual Process

Teradata RDBMS for UNIX SQL Reference

Teradata RDBMS for UNIX Library

Teradata RDBMS for UNIX Library Titles of publications in the Teradata RDBMS for UNIX library begin with Teradata RDBMS for UNIX. The following publications, listed in alphabetical order, apply to Teradata RDBMS for UNIX, Version 2 Release 2.0.2:

Product ID

Publication Title

BD10-5059-A

Data Dictionary Reference

BD10-4953-B

Database Design and Administration

BD10-5060-A

Database Window Reference

BD10-5061-C

Field Support Guide

BD10-4955-A

Introduction to the Teradata DBS for UNIX

BD10-4956-A

Master Index, Bibliography, and Glossary

BD10-5062-C

Messages Reference

BD10-5013-A

Performance Monitor Reference

BD10-5063-A

Publications Roadmap for Kanji Functions and Features

BD10-5064-B

Resource Usage Macros and Tables

BD10-5052-B

Security Administration Guide

BD10-4957-C

SQL Reference

BST0-2140-81

SQL Quick Reference

BD10-5065-C

Support Utilities Reference

B035-1503-077B

Support Utilities Reference Card

BD10-5066-B

SystemFE Macros

BD10-5067-C

Utilities Reference

B035-9002-057B

Utilities Reference Card

B035-1901-087D

Teradata RDBMS for UNIX V2R2.0.1 and Client 9701 User Documentation CD-ROM

Teradata RDBMS for UNIX SQL Reference

vii

Client Reference Library

Client Reference Library The following publications, listed in alphabetical order, apply to Client 9701 products: Product ID

viii

Publication Title

BD10-5071-A

CICS Interface to the Teradata DBS

BD10-5072-A

IMS Interface to the Teradata DBS

BD10-4952-A

Teradata Application Programming With Embedded SQL for C, COBOL, and PL/I

BD10-5069-A

Teradata Archive/Recovery Reference for Channel-Attached Systems

BST0-2140-88

Teradata Archive/Recovery Reference Card

BD10-5087-A

Teradata Archive Storage Facility 2 (ASF2) Administration and Operations

BD10-5086-A

Teradata Archive Storage Facility 2 (ASF2) Command Language Reference

BD10-5091-A

Teradata BTEQ Reference

BST0-2140-85

Teradata BTEQ Reference Card

BD10-5068-A

Teradata Call-Level Interface Version1 for Channel-Attached Systems

BD10-5070-A

Teradata Call-Level Interface Version2 for Channel-Attached Systems

BD10-5078-B

Teradata Call-Level Interface Version2 for Network-Attached Systems

BD10-5084-A

Teradata Client for MVS Installation Guide

BD10-5095-B

Teradata Client for NCR UNIX MP-RAS Installation Guide

BD10-5085-A

Teradata Client for VM Installation Guide

BD10-5024-A

Teradata Data Definition Language Processor Reference

BD10-5089-B

Teradata Database Query Manager Installation Guide

BD10-5088-B

Teradata Database Query Manager User’s Guide

BST0-2140-86

Teradata Embedded SQL Reference Card

BD10-5094-B

Teradata Enhanced Call-Level Interface Reference

BD10-5079-A

Teradata FastExport Reference

Teradata RDBMS for UNIX SQL Reference

Client Reference Library Product ID

Publication Title

BD10-4954-B

Teradata FastLoad Reference

BD10-5074-A

Teradata ITEQ Reference Manual for Channel-Attached Systems

BD10-5075-A

Teradata ITEQ User’s Guide for Channel-Attached Systems

BST0-2122-30

Teradata ITEQ Keypad Template

BST0-2122-34

Teradata ITEQ Keypad Template (3270 PC)

BST0-2126-20

Teradata ITEQ Reference Card

BST0-2140-84

Teradata Load Utilities Reference Card

BD10-5092-A

Teradata Manager for the Teradata DBS for UNIX User’s Guide

BCD0-0136-A

Teradata Manager for the Teradata DBS for UNIX User’s Guide - CD-ROM

BD10-5076-A

Teradata MultiLoad Reference

BST0-2141-00

Teradata ODBC Driver for Windows Installation and User’s Guide

BD10-5062-B

Teradata RDBMS for UNIX Messages Reference

BD10-4966-A

Teradata TDP Reference

BST0-2140-87

Teradata TDP Reference Card

BD10-5080-A

Teradata TS/API Concepts and Facilities

BD10-5083-A

Teradata TS/API Installation Guide

BD10-5082-A

Teradata TS/API System & Database Administration Guide

BD10-5081-A

Teradata TS/API User’s Guide

BD10-5090-A

Teradata WinCLI Application Developer’s Guide

BD10-5093-A

Teradata WinCLI Installation Guide

BCD0-0143-A

Teradata RDBMS for UNIX V2R2.0.1 and Client 9701 User Documentation CD-ROM

Teradata RDBMS for UNIX SQL Reference

ix

How to Order Teradata Publications

How to Order Teradata Publications You may always order Teradata publications through your NCR Sales Representative, or you may use one of the methods listed below.

Order Form Ordering Address

To order Teradata publications, use the Information Products Order Form (form number IPP-WD02001). Send orders to the following address:

U.S. Orders

NCR IPP-DAYTON 1529 Brown Street Dayton, OH 45479 USA

FAX: 937-445-6245 PHONE: 1-800-543-2010 or VP 622-3727 E-MAIL: [email protected] MS-MAIL: Publishing, Information+Products

Non- U.S. Orders

NCR IPP-BRUSSELS-OTC Rue de la Fusee 50 B-1130 Brussels Belgium

FAX: 32-2-727-95-50 PHONE: 32-2-727-95-49 or 32-2-727-95-71 E-MAIL: [email protected]

x

Teradata RDBMS for UNIX SQL Reference

Table of Contents

Contents

About This Book ........................................................................................i Changes to This Book..............................................................................iv List of Acronyms ......................................................................................v Teradata RDBMS for UNIX Library.................................................... vii Client Reference Library...................................................................... viii How to Order Teradata Publications.....................................................x

Chapter 1 Notation About This Chapter...............................................................................1-1 Notation ..................................................................................................1-2 How to Read the Syntax Diagrams.....................................................1-3 Japanese Character Code Notation Used in this Manual ................1-7

Chapter 2 Data Handling Fundamentals About This Chapter...............................................................................2-1 Standard Form for Data in the Teradata RDBMS .............................2-2 Using Partial Names .............................................................................2-3 Basic Data Manipulation ......................................................................2-4 Data Definition Statements ..................................................................2-6 SQL Requests........................................................................................2-10 Structure of a SQL Statement.............................................................2-12 Data Manipulation Language (DML)...............................................2-13 Default Database..................................................................................2-16 Macros ...................................................................................................2-17 Subqueries ............................................................................................2-19 Supported Characters .........................................................................2-20 Operations on Null..............................................................................2-21 Statement Response.............................................................................2-24

Teradata RDBMS for UNIX SQL Reference

xi

Table of Contents

Success Response .................................................................................2-25 Warning Response...............................................................................2-26 Failure Response..................................................................................2-27 Error Response (ANSI Mode Only) ..................................................2-28 Session Parameters ..............................................................................2-29

Chapter 3 Joins: Working with Multiple Tables About This Chapter ...............................................................................3-1 Inner Join.................................................................................................3-2 Outer Join................................................................................................3-3 Cross Join ..............................................................................................3-15 Self-Join .................................................................................................3-16

Chapter 4 Teradata SQL Lexicon About This Chapter ...............................................................................4-1 Words and Names (Non-Japanese Character) ..................................4-2 Japanese Character Names...................................................................4-5 Japanese Character Object Names ......................................................4-7 Rules for Non-ASCII Character Object Names ...............................4-11 Object Name Translation and Storage..............................................4-14 Object Name Comparisons ................................................................4-15 Finding the Internal Hexadecimal Representation of a Name .....4-18 Specifying Names in a Logon String ................................................4-20 Keywords..............................................................................................4-21 Delimiters..............................................................................................4-22 Constants ..............................................................................................4-24 Numeric Constants..............................................................................4-25 Character Constants ............................................................................4-27 Hexadecimal Constants ......................................................................4-28 Graphic Constants ...............................................................................4-32 Operators ..............................................................................................4-33 Lexical Separators................................................................................4-35 Statement Separator ............................................................................4-36 Request Terminator .............................................................................4-37

xii

Teradata RDBMS for UNIX SQL Reference

Table of Contents

Chapter 5 Data Definition About This Chapter...............................................................................5-1 What Is Data Definition? ......................................................................5-2 Data Type Attributes: ANSI Mode .....................................................5-4 Data Types ..............................................................................................5-6 GRAPHIC Conversion..........................................................................5-9 Numeric Data Types ...........................................................................5-11 DATE Numeric Data Type.................................................................5-13 Arithmetic on Date Values .................................................................5-17 DECIMAL and NUMERIC Numeric Data Types...........................5-18 BYTEINT Numeric Data Type...........................................................5-20 SMALLINT Numeric Data Type .......................................................5-21 INTEGER Numeric Data Type ..........................................................5-22 FLOAT, REAL, and DOUBLE PRECISION Numeric Data Types .........................................................................................5-23 Character Data Types..........................................................................5-25 CHAR, VARCHAR and LONGVARCHAR Character Data Types ........................................................................................5-27 Specifying Case for Character Data ..................................................5-29 Defining Case Sensitivity for Table Columns .................................5-32 Case Sensitivity of Character String Literals ...................................5-33 Character String Definitions in the USING Phrase ........................5-34 Case Sensitivity in Comparisons.......................................................5-35 Character Data for a Japanese Character Supported Site ..............5-38 BYTE and VARBYTE Data Types .....................................................5-41 Japanese Character Graphic Data Types..........................................5-43 Column and Table Constraints..........................................................5-47 Constraints............................................................................................5-48 FORMAT Phrase..................................................................................5-49 Character Formats ...............................................................................5-52 Graphic Formats ..................................................................................5-53 Numeric Formats.................................................................................5-55 DATE Formats .....................................................................................5-61 Data Default Formats ..........................................................................5-64 Naming Columns and Expressions ..................................................5-66 TITLE Phrase ........................................................................................5-69 Default Control Phrase .......................................................................5-73 COMPRESS Phrase..............................................................................5-77 Data Type Conversions ......................................................................5-80

Teradata RDBMS for UNIX SQL Reference

xiii

Table of Contents

Implicit Type Conversion Rules ........................................................5-82 CAST in Data Type Conversion ........................................................5-84 Explicit Data Type Conversion with a Data Definition Phrase ....5-86 Numeric Conversion ...........................................................................5-87 Numeric to Numeric ...........................................................................5-88 Numeric to Character..........................................................................5-89 Character Conversion .........................................................................5-91 Character to Numeric..........................................................................5-92 Character To Character.......................................................................5-94 Graphic Conversion ............................................................................5-95 Byte Conversion...................................................................................5-96 DATE Conversion................................................................................5-97 Signed Zone Decimal Conversion.....................................................5-99 Using CAST in Data Type Conversions .........................................5-102 Attribute Functions ...........................................................................5-104 Hash-Related Expressions................................................................5-111 HASHROW Function........................................................................5-112 HASHBUCKET Function .................................................................5-113 HASHAMP Function ........................................................................5-114 HASHBAKAMP Function................................................................5-115

Chapter 6 SQL Expressions About This Chapter ...............................................................................6-1 Arithmetic Operators ............................................................................6-3 Arithmetic Functions.............................................................................6-7 Date and Time Expressions................................................................6-11 The EXTRACT Function .....................................................................6-13 ADD_MONTHS Function ..................................................................6-16 Aggregate Operators...........................................................................6-17 Character String Expressions .............................................................6-23 Character Strings: Concatenation Operator.....................................6-24 Character String Expressions: String Functions..............................6-30 String Functions: SUBSTRING and SUBSTR...................................6-31 Japanese Characters and Graphic Data ............................................6-34 String Function: MSUBSTR ................................................................6-36 String Function: INDEX......................................................................6-37 String Function: MINDEX ..................................................................6-42 String Function: TRIM ........................................................................6-43 String Function: VARGRAPHIC .......................................................6-48 String Function: CHAR2HEXINT .....................................................6-50

xiv

Teradata RDBMS for UNIX SQL Reference

Table of Contents

String Function: UPPER......................................................................6-51 Logical Expressions .............................................................................6-52 Comparison Operators .......................................................................6-53 CASE Blind Comparisons ..................................................................6-56 Japanese Character Site Comparison Operators .............................6-62 Logical Expressions: IN and NOT IN Operator..............................6-65 Logical Expressions: IS NULL and IS NOT NULL Operator........6-68 Logical Expressions: EXISTS..............................................................6-70 Logical Expressions: LIKE Partial-String Operator ........................6-74 Logical Expressions: Japanese Character Site, LIKE Operator .....6-81 Conditional Expressions.....................................................................6-85 CASE Expression .................................................................................6-88 Valued CASE Expression ...................................................................6-89 Searched CASE Expression ................................................................6-91 CASE Abbreviation Expressions .......................................................6-94 NULLIF .................................................................................................6-95 COALESCE...........................................................................................6-96 Built-In Values .....................................................................................6-97 DATE .....................................................................................................6-98 TIME ......................................................................................................6-99 USER....................................................................................................6-100 NULL...................................................................................................6-101 Indicator Variables ............................................................................6-102

Chapter 7 Queries: The SELECT Statement About This Chapter...............................................................................7-1 SELECT ...................................................................................................7-2 Simple SELECT Statement .................................................................7-10 WHERE Clause ....................................................................................7-11 Subqueries in Search Conditions.......................................................7-14 FROM Clause .......................................................................................7-16 Derived Tables .....................................................................................7-20 WITH Clause........................................................................................7-24 GROUP BY Clause...............................................................................7-30 HAVING Clause ..................................................................................7-32 ORDER BY Clause...............................................................................7-34 DISTINCT and ALL Options .............................................................7-41 Query Expressions (Set Operators)...................................................7-43 UNION Operator.................................................................................7-50 INTERSECT Operator.........................................................................7-54

Teradata RDBMS for UNIX SQL Reference

xv

Table of Contents

EXCEPT (MINUS) Operator ..............................................................7-55

Chapter 8 Teradata SQL Syntax Guide About This Chapter ...............................................................................8-1 ABORT ....................................................................................................8-4 ALTER TABLE .......................................................................................8-7 BEGIN/END LOGGING....................................................................8-29 BEGIN/ END TRANSACTION ........................................................8-38 CHECKPOINT .....................................................................................8-42 COLLECT STATISTICS ......................................................................8-44 COMMENT ..........................................................................................8-48 COMMIT...............................................................................................8-51 CREATE DATABASE .........................................................................8-53 CREATE INDEX ..................................................................................8-59 CREATE MACRO................................................................................8-62 CREATE TABLE ..................................................................................8-69 CREATE USER.....................................................................................8-95 CREATE VIEW ..................................................................................8-104 DATABASE ........................................................................................8-116 DELETE...............................................................................................8-117 DELETE DATABASE, DELETE USER ...........................................8-125 DROP DATABASE, DROP USER ...................................................8-127 DROP INDEX .....................................................................................8-129 DROP MACRO, TABLE, or VIEW ..................................................8-131 DROP STATISTICS............................................................................8-133 ECHO ..................................................................................................8-136 EXECUTE............................................................................................8-137 EXPLAIN Modifier............................................................................8-140 GIVE ....................................................................................................8-148 GRANT................................................................................................8-151 GRANT Statement (SQL) .................................................................8-152 GRANT Statement (MONITOR) .....................................................8-163 GRANT LOGON ...............................................................................8-166 GROUP BY Clause.............................................................................8-169 HAVING Clause ................................................................................8-170 HELP....................................................................................................8-171 HELP COLUMN................................................................................8-175 HELP CONSTRAINT........................................................................8-179 HELP DATABASE HELP USER .....................................................................................8-182

xvi

Teradata RDBMS for UNIX SQL Reference

Table of Contents

HELP INDEX......................................................................................8-183 HELP SESSION..................................................................................8-184 HELP STATISTICS ............................................................................8-185 HELP TABLE HELP VIEW HELP MACRO ................................................................................8-186 HELP (OnLine) ..................................................................................8-189 INSERT................................................................................................8-192 LOCKING Modifier ..........................................................................8-199 MODIFY DATABASE .......................................................................8-207 MODIFY USER ..................................................................................8-213 ORDER BY Clause.............................................................................8-221 RENAME ............................................................................................8-222 REPLACE MACRO ...........................................................................8-223 REPLACE VIEW ................................................................................8-225 REVOKE..............................................................................................8-232 REVOKE Statement (SQL) ...............................................................8-233 REVOKE Statement (MONITOR) ...................................................8-236 REVOKE LOGON .............................................................................8-240 ROLLBACK ........................................................................................8-243 SELECT ...............................................................................................8-245 SET SESSION COLLATION ............................................................8-246 SHOW MACRO SHOW TABLE SHOW VIEW...................................................................................8-249 UPDATE .............................................................................................8-254 USING Modifier.................................................................................8-260 WHERE Clause ..................................................................................8-263 WITH Clause......................................................................................8-264

Chapter 9 Advanced SQL About This Chapter...............................................................................9-1 Transaction Semantics: Operating in ANSI or Teradata Mode ......9-2 ANSI Mode.............................................................................................9-5 Teradata Mode .......................................................................................9-6 SQL Flagger ............................................................................................9-7 Correlated Subqueries ..........................................................................9-9 Correlated Subqueries and SELECT .................................................9-12 Correlated Subqueries and UPDATE ...............................................9-13 Correlated Subqueries and DELETE ................................................9-14

Teradata RDBMS for UNIX SQL Reference

xvii

Table of Contents

Correlated Subqueries and ABORT/ROLLBACK..........................9-16 SELECT COUNT(*) .............................................................................9-17 Referential Integrity ............................................................................9-18 Updatable Cursors...............................................................................9-25 EXPLAIN Modifier: Examples of Complex Queries ......................9-26

Appendix A Teradata SQL Keywords Teradata SQL Keywords .................................................................... A-1

Appendix B Teradata SQL Versus ANSI SQL Teradata SQL Versus ANSI SQL: Introduction................................ B-1 Teradata SQL Versus ANSI SQL: Summarized ............................... B-2

Appendix C Comparing Teradata SQL and DB2 Teradata SQL and DB2 Compatibilities ............................................C-1 DB2 and Teradata SQL Differences ...................................................C-3 DB2 Version 2 Release 3.0 Changes ...................................................C-8 DB2 Version 2 Release 2.0 Changes .................................................C-11 DB2 Version 3.0 Changes...................................................................C-12 DB2 Version 4.0 Changes...................................................................C-13 DB2 Extensions Not Supported in Teradata SQL ..........................C-16 Teradata SQL Extensions Beyond DB2............................................C-20 Reserved Words Comparison ...........................................................C-23 Limitations: DB2 Vs. Teradata SQL .................................................C-27

Appendix D Sample Personnel Database Personnel Database ............................................................................. D-1

Appendix E ASCII Hexadecimal Translation Tables About This Appendix .......................................................................... E-1

xviii

Teradata RDBMS for UNIX SQL Reference

Table of Contents

Appendix F Data Conversion Data Conversion ................................................................................... F-1 Teradata RDBMS Internal Format ..................................................... F-2 AT&T 3B-Series Internal Format ........................................................ F-3 Bull HN Internal Format ..................................................................... F-5 IBM Mainframe Internal Format ........................................................ F-6 UTS Internal Format............................................................................. F-8 VAX or VMS Internal Format ............................................................. F-9 PC Internal Format ............................................................................. F-10

Appendix G International and Japanese Character Support About This Appendix ..........................................................................G-1 Overview ...............................................................................................G-3 Defining Character Sets .......................................................................G-6 User-Defined Character Set Restrictions...........................................G-9 User-Defined Character Sets: Alternative Currency Symbols .....G-11 Naming User-Defined Kanji Character Sets ...................................G-13 Columns in DBC. CharTranslations View ......................................G-15 Example of Inserting the Swedish Character Set into DBC.CharTranslations ....................................................................G-18 Installing Character Sets ....................................................................G-22 Teradata Internal Form for International Character Support ......G-23 Using Collation Sequences................................................................G-27 Changing the Standard Multinational Default Collation .............G-31 Defining Your Own Collation Sequence .........................................G-34 Japanese Character Support..............................................................G-40 IBM-Compatible Character Sets .......................................................G-42 KANJIEBCDIC5026_0I.......................................................................G-43 KANJIEBCDIC5035_0I.......................................................................G-44 KATAKANAEBCDIC ........................................................................G-45 KANJIEUC_0U....................................................................................G-46 KANJISJIS_0S ......................................................................................G-47 UNIX-Compatible Character Set (KanjiEUC_0U) .........................G-49 DOS/V-Compatible Character Set (KanjiShift-JIS_0S) .................G-51 Japanese Character Translation and Storage..................................G-52 Accessing and Sharing Japanese Character Objects and Data.....G-54 Japanese Character Sites ....................................................................G-56

Teradata RDBMS for UNIX SQL Reference

xix

Table of Contents

Setting Up Japanese Character Support ......................................... G-58

Appendix H Japanese Character Sets About This Appendix ......................................................................... H-1 JIS-x0201 ................................................................................................ H-2 JIS-x0208 ................................................................................................ H-3 IBM Mainframe Character Sets for Japan ........................................ H-5 IBM EBCDIC Character Set for Japan............................................... H-6 Non-Kanji EBCDIC Character Set for Japanese Character Site..... H-7 EBCDIC KATAKANA Character Set ............................................... H-8 EBCDIC Japanese Extended Katakana Character Set .................... H-9 EBCDIC Japanese Extended English Character Set...................... H-10 EBCDIC Kanji Character Set ............................................................ H-11 Extended UNIX Code (EUC) ........................................................... H-13 Shift-JIS (DOS Kanji) Encoding........................................................ H-16

Appendix I Non-valid Japanese Character Code Non-Valid Characters in Object Names: KanjiEBCDIC................. I-1 Non-Valid Characters in Object Names: KanjiEUC ....................... I-4 Non-Valid Characters in Object Names: KanjiShift-JIS ................... I-5

Appendix J VARGRAPHIC Conversion VARGRAPHIC Function Conversion Tables .................................... J-1

xx

Teradata RDBMS for UNIX SQL Reference

List of Figures

List of Figures

Appendix G

International and Japanese Character Support Figure G-1 Figure G-2

Required EBCDIC Characters ...................................G-10 Example Views of Populated Translation and Host Tables ...................................................................G-22

Appendix H

Japanese Character Sets Figure H-1

The JIS-x0201 Standard................................................ H-2 ......................................................................................... H-2 Figure H-2 The JIS-x0208 Standard................................................ H-4 ......................................................................................... H-4 Figure H-3 IBM EBCDIC Character Set for Japan ....................... H-6 Non-Kanji EBCDIC Character Set for Japanese Character Site ................................................................ H-7 Figure H-4 EBCDIC KATAKANA Character Set......................... H-8 Figure H-5 EBCDIC Japanese Extended Katakana Character Set ................................................................................... H-9 Figure H-6 EBCDIC Japanese Extended English Character Set ................................................................................. H-10 Figure H-7 EBCDIC Kanji Character Distribution......................H-11 Figure H-8 KanjiEUC Encoding for Kanji................................... H-15 Figure H-9 Shift-JIS Encoding for Kanji ...................................... H-17 Figure H-10 Shift-JIS Encoding--Detailed View........................... H-18 ....................................................................................... H-18

Teradata RDBMS for UNIX SQL Reference

xxi

List of Figures

xxii

Teradata RDBMS for UNIX SQL Reference

List of Tables

List of Tables

Chapter 1

Notation Table 1-1

Kanji Code Shorthand Notation...................................1-7

Chapter 2

Data Handling Fundamentals Table 2-1

Returned Nulls .............................................................2-23

Chapter 4

Teradata SQL Lexicon Table 4-1 Table 4-2 Table 4-3 Table 4-4 Table 4-5 Table 4-6 Table 4-7

KanjiEBCDIC Object Name Examples ......................4-12 KanjiEUC Object Name Examples.............................4-13 KanjiShift-JIS Object Name Examples.......................4-13 Delimiters in Teradata SQL.........................................4-23 Hexadecimal Values Internal ASCII ..........................4-29 Forms for Hexadecimal Graphic Constants .............4-31 Operator Precedence....................................................4-34

Chapter 5

Data Definition Table 5-1 Table 5-2 Table 5-3 Table 5-4 Table 5-5 Table 5-6

Data Type Declaration ...................................................5-6 Data Type Attributes......................................................5-7 Data Conversion Results ...............................................5-9 Effects of Rounding......................................................5-12 Effect of Converting to Uppercase on Japanese Character Data ..............................................................5-26 Numeric Format Results .............................................5-58

Teradata RDBMS for UNIX SQL Reference

xxiii

List of Tables

Table 5-7 Table 5-8 Table 5-9 Table 5-10 Table 5-11 Table 5-12 Table 5-13 Table 5-14 Table 5-15 Table 5-16 Table 5-17 Table 5-18 Table 5-19 Table 5-20

European Alternative Currency Symbols .................5-59 Japanese Alternative Currency Symbols...................5-59 DATE Format Characters ............................................5-61 DATE Format Results ..................................................5-62 Data Type Format Defaults .........................................5-64 FORMAT Displays .......................................................5-64 Values for DEFAULT Phrase Keywords....................5-74 System Values for WITH DEFAULT Phrase .............5-75 Signed Zoned Decimal Character Set........................5-99 KanjiEBCDIC - CHARACTERS(string_expr).........5-108 KanjiEUC - CHARACTERS(string_expr) ...............5-109 KanjiShift-JIS - CHARACTERS(string_expr) .........5-109 KanjiEBCDIC - MCHARACTERS(string_expr) .....5-109 Results from OCTET_LENGTH (string-expr)........5-110

Chapter 6

SQL Expressions Table 6-1 Table 6-2 Table 6-3 Table 6-4 Table 6-5 Table 6-6 Table 6-7 Table 6-8 Table 6-9 Table 6-10 Table 6-11 Table 6-12 Table 6-13 Table 6-14 Table 6-15 Table 6-16 Table 6-17 Table 6-18

xxiv

Binary Arithmetic Result Data Types ..........................6-4 Aggregate Operators....................................................6-17 Default Attributes of Aggregate Results...................6-19 KanjiEBCDIC - SUBSTR(string_expr, n1 [, n2]) .........................................................................6-35 KanjiEUC - SUBSTR(string_expr, start [, len]) .............................................................................6-35 KanjiShift-JIS - SUBSTR(string_expr, start [, len]) ...6-35 KanjiEBCDIC - MSUBSTR(string_expr, n1 [, n2])....6-36 KanjiEBCDIC - INDEX(string_expr1, string_expr2) .................................................................6-41 KanjiEUC - INDEX(string_expr1, string_expr2) .................................................................6-41 KanjiShift-JIS - INDEX(string_expr1, string_expr2) .................................................................6-41 KanjiEBCDIC - MINDEX(string_expr1, string_expr2) .................................................................6-42 KanjiEBCDIC - VARGRAPHIC(string_expr) ...........6-49 Comparison Operators ................................................6-53 Implicit Conversions for Comparisons .....................6-55 KanjiEBCDIC - (expr LIKE pattern-string) ...............6-83 KanjiEUC - (expr LIKE pattern-string)......................6-83 KanjiShift-JIS - (expr LIKE pattern-string)................6-83 AND Truth Table (x AND y).......................................6-86

Teradata RDBMS for UNIX SQL Reference

List of Tables

Table 6-19

OR Truth Table (x OR y) ..............................................6-86

Chapter 7

Queries: The SELECT Statement Table 7-1

European Sort Order....................................................7-37

Chapter 8

Teradata SQL Syntax Guide Table 8-3 Table 8-4 Table 8-5

Restrictions on Granted Privileges ..........................8-159 HELP COLUMN Information and Attributes .......8-177 Table, View, or Macro Attributes..............................8-187

Appendix B

Teradata SQL Versus ANSI SQL Table B-1

Teradata SQL Vs. ANSI SQL, Summarized ............... B-2

Appendix C

Comparing Teradata SQL and DB2 Table C-1 Table C-2

Table C-3 Table C-4 Table C-5 Table C-6

GRANT Database Privileges: Differences in DB2 and Teradata SQL ...............................................C-18 Teradata SQL Reserved Words (not in DB2) (1 of 2)............................................................................C-25 Teradata SQL Reserved Words (not in DB2) (2 of 2)............................................................................C-26 Limitations, Teradata SQL and DB2 .........................C-27 Numeric Limits: Teradata SQL Vs. DB2...................C-28 String Length Limits: Teradata SQL Vs. DB2 ..........C-28 Datetime Limits: Teradata SQL Vs. DB2 ..................C-29

Teradata RDBMS for UNIX SQL Reference

xxv

List of Tables

Appendix E

ASCII Hexadecimal Translation Tables Table E-1 Table E-2

EBCDIC to ASCII Translation...................................... E-2 ASCII to EBCDIC Translation...................................... E-4

Appendix G

International and Japanese Character Support Table G-1 Table G- 1 Table G-2 Table G-3 Table G-4

Teradata Internal Form for International Character Support ...................................................... G-24 Equivalence Classes and Ordering Within Class (Teradata Standard Multinational Collation) ......... G-29 Teradata RDBMS Kanji Character Sets.................... G-41 KanjiEBCDIC Character Data Validity Criteria ..... G-47 KanjiEBCDIC Graphic Data Validity Criteria ........ G-47

Appendix H

Japanese Character Sets Table H-1 Table H-2 Table H-3 Table H-4 Table H-5 Table H-6

xxvi

IBM Kanji Encoding ................................................... H-12 Selected Characters for EBCDIC Kanji.................... H-12 EUC Code Set Localization ....................................... H-14 Selected Characters for EUC Kanji .......................... H-14 Shift-JIS Encoding....................................................... H-16 Selected Characters for Shift-JIS Kanji..................... H-16

Teradata RDBMS for UNIX SQL Reference

Revision Record

Revision Record

Date

Description

September 1996

Initial printing. Supports Teradata DBS for UNIX V2R2.0.

April 1997

Supports Teradata RDBMS for UNIX V2R2.0.1

June 1997

Supports Teradata RDBMS for UNIX V2R2.0.2

Teradata RDBMS for UNIX SQL Reference

xxvii

xxviii

Teradata RDBMS for UNIX SQL Reference

Notation

Chapter 1

Notation

Teradata RDBMS for UNIX SQL Reference

Notation

Teradata RDBMS for UNIX SQL Reference

Notation About This Chapter 1

About This Chapter

1

This chapter describes the following: ● ● ●

Notation How to read syntax diagrams Japanese character (Kanji) conventions

Teradata RDBMS for UNIX SQL Reference

1-1

Notation Notation

Notation

1

Throughout this manual, three conventions are used to describe the SQL syntax and code: ●



Syntax diagrams - used to describe SQL syntax form, including options. See “How to Read Syntax diagrams” in the next section. Square braces in the text, used to represent options. For example: ● DECIMAL [(n[,m])] means the decimal data type can be defined optionally: ● without specifying the precision value n or scale value m ● specifying precision value (n) only ● specifying scale value (m) only ● specifying both values (n,m) The parentheses are required when specifying n and/or m. GRAPHIC [(n)] means that use of (n) double-byte zeros is optional. The parentheses are required when using n. Japanese character code shorthand notation, used to represent unprintable Japanese characters. See later in this chapter.





1-2

Teradata RDBMS for UNIX SQL Reference

Notation How to Read the Syntax Diagrams

How to Read the Syntax Diagrams

1

This section describes the rules that apply to the syntax diagrams used in this book. The following table defines the notation used in this section:

Notation Conventions

Item

Definition / Comments

Letter

An uppercase or lowercase alphabetic character ranging from A through Z.

Number

A digit ranging from 0 through 9. Do not use commas when entering a number with more than three digits.

Word

Variables and reserved words. IF a word is shown in . . .

THEN it represents . . .

UPPERCASE LETTERS

a keyword. Syntax diagrams show all keywords in uppercase, unless operating system restrictions require them to be in lowercase. If a keyword is shown in uppercase, you may enter it in uppercase or mixed case.

lowercase letters

a keyword that you must enter in lowercase, such as a UNIX command.

lowercase italic letters

a variable such as a column or table name. You must substitute a proper value.

lowercase bold letters

a variable that is defined immediately following the diagram that contains it.

UNDERLINED LETTERS

the default value. This applies both to uppercase and to lowercase words.

Spaces

Use one space between items, such as keywords or variables.

Punctuation

Enter all punctuation exactly as it appears in the diagram.

Teradata RDBMS for UNIX SQL Reference

1-3

Notation How to Read the Syntax Diagrams

Paths

The main path along the syntax diagram begins at the left, and proceeds, left to right, to the vertical bar, which marks the end of the diagram. Paths that do not have an arrow or a vertical bar only show portions of the syntax. The only part of a path that reads from right to left is a loop. Paths that are too long for one line use continuation links. Continuation links are small circles with letters indicating the beginning and end of a link: A

A

FE0CA002

When you see a circled letter in a syntax diagram, go to the corresponding circled letter and continue.

Required Items

Required items appear on the main path: SHOW

FE0CA003

If you can choose from more than one item, the choices appear vertically, in a stack. The first item appears on the main path: SHOW

CONTROLS VERSIONS FE0CA005

Optional Items

Optional items appear below the main path: SHOW CONTROLS

FE0CA004

If choosing one of the items is optional, all the choices appear below the main path: SHOW CONTROLS VERSIONS FE0CA006

You can choose one of the options, or you can disregard all of the options.

1-4

Teradata RDBMS for UNIX SQL Reference

Notation How to Read the Syntax Diagrams

Abbreviations

If a keyword or a reserved word has a valid abbreviation, the unabbreviated form always appears on the main path. The shortest valid abbreviation appears beneath. SHOW

CONTROLS CONTROL FE0CA042

In the above syntax, the following formats are valid: ● ●

Loops

SHOW CONTROLS SHOW CONTROL

A loop is an entry or a group of entries that you can repeat one or more times. Syntax diagrams show loops as a return path above the main path, over the item or items that you can repeat. , (

4

cname

) FE0CA015

The following rules apply to loops: IF...

THEN...

there is a maximum number of entries allowed

the number appears in a circle on the return path. In the example, you may enter cname a maximum of 4 times.

a separator character is required between entries

the character appears on the return path. If the diagram does not show a separator character, use one blank space. In the example, the separator character is a comma.

a delimiter character is required around entries

the beginning and end characters appear outside the return path. Generally, a space is not needed between delimiter characters and entries. In the example, the delimiter characters are the left and right parentheses.

Teradata RDBMS for UNIX SQL Reference

1-5

Notation How to Read the Syntax Diagrams

Excerpts

1

Sometimes a piece of a syntax phrase is too large to fit into the diagram. Such a phrase is indicated by a break in the path, marked by | terminators on either side of the break. A name for the excerpted piece appears between the break marks in boldface type. The named phrase appears immediately after the complete diagram, as illustrated by the following example.

LOCKING

excerpt

A

A HAVING

con excerpt

where_cond ,

cname , col_pos JC01A014

1-6

Teradata RDBMS for UNIX SQL Reference

Notation Japanese Character Code Notation Used in this Manual

Japanese Character Code Notation Used in this Manual

1

Japanese character code provides support for mixed multibyte/single byte characters in the following character sets: ● ● ●

KanjiEBCDIC KanjiEUC KanjiShift-JIS.

Normally multibyte characters and some of the single byte characters (e.g., Katakana) are not printable. To represent the unprintable characters in this manual, a notation shorthand has been adopted, as shown in Table 1-1. Table 1-1 Kanji Code Shorthand Notation

Code Set

Symbol

Meaning

KanjiEBCDIC




Shift In [SI] (0x0F) - used to indicate transition from multibyte to single byte KanjiEBCDIC.

Any

T

Any multibyte character or graphic. Its encoding depends on the current character set.May be used in conjunction with KanjiEUC code set 1 data.

Any

I

Single byte Hankaku Katakana character - especially as stored in the canonical form of JIS-x0201.

Any

a..z A..Z 0..9

Any single byte Latin letter or digit. Internal storage requires one byte.

KanjiEUC

ss2B

Multibyte character representing code set 2 data (Hankaku Katakana) in KanjiEUC. In this manual, “ss2” represents the code set 2 identifier (0x8E); letter “B” represents a single byte of storage. Internal storage requires two bytes.

KanjiEUC

ss3E

Multibyte character representing code set 3 data in KanjiEUC. In this manual, “ss3” represents the code set 3 identifier (0x8F); E represents a multibyte character. Internal storage requires three bytes.

Any



Represents either a single or multibyte blank, depending on context.

Teradata RDBMS for UNIX SQL Reference

1-7

Notation Japanese Character Code Notation Used in this Manual Code Set

Symbol

Meaning

KanjiEUC

ss2

Represents the EUC code set 2 introducer (0x8E).

KanjiEUC

ss3

Represents the EUC code set 3 introducer (0x8F).

For example, string ‘TEST’ where each letter is represented as double byte letter will be represented as TEST. Occasionally, when encoding is important, we will indicate hex representation. For example, the following mixed single byte/multibyte character data in KanjiEBCDIC character set LMNQRS

is represented as: D3 D4 D5 0E 42E3 42C5 42E2 42E3 0F D8 D9 E2

Note: KatakanaEBCDIC uppercases single byte data. Refer also to the Glossary and Acronyms sections in the Preface of this manual for further explanation of terms used.

Note Throughout this manual and other Teradata manuals, the term “Japanese character site”, or “Japanese character supported site”, implies and is the same as a “Kanji feature site”.

1-8

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals

Chapter 2

Data Handling Fundamentals

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals About This Chapter 2

About This Chapter

2

This chapter covers the following topics: ● ● ● ● ● ● ● ● ● ● ● ● ●

Standard form for data in the Teradata RDBMS Using partial names Basic data manipulation Data definition statements SQL requests Data Manipulation Language (DML) Default database Macros Subqueries Supported characters Operations on null Statement response Session parameters

Teradata RDBMS for UNIX SQL Reference

2-1

Data Handling Fundamentals Standard Form for Data in the Teradata RDBMS

Standard Form for Data in the Teradata RDBMS Data is stored in the Teradata RDBMS according to the relational model, which is based on tables with rows and columns. Each row of a table is composed of a number of fields identified by column name. Each field contains a data item with a single data type. A database consists of a collection of related tables. The rows of a table can be considered an unordered set. The form for referencing a column is: columnname tablename. databasename.

HH01A101

A fully qualified name consists of a database name, a table name, and a column name. For example, a fully qualified reference for the Name column in the Employee table is: Personnel.Employee.Name

An asterisk references all columns in a row simultaneously, for example, SELECT * FROM Employee;

references the following columns: Personnel.Employee.EmpNo Personnel.Employee.Name Personnel.Employee.DeptNo Personnel.Employee.JobTitle Personnel.Employee.Salary Personnel.Employee.YrsExp Personnel.Employee.DOB Personnel.Employee.Sex Personnel.Employee.Race Personnel.Employee.MStat Personnel.Employee.EdLev Personnel.Employee.HCap

2-2

Teradata RDBMS for UNIX SQL Reference

2

Data Handling Fundamentals Using Partial Names

Using Partial Names

2

A partial name is a table or column reference that is not fully qualified. For example, “DeptNo” is a partial name. Teradata SQL allows you to omit database names and table names in referencing columns as long as the reference is not ambiguous. For example, the WHERE clause in the following statement: SELECT Name, DeptNo, JobTitle FROM Personnel.Employee WHERE Personnel.Employee.DeptNo=100 ;

can be written as: WHERE DeptNo=100 ;

because the database name and table name can be derived from the “Personnel.Employee” reference in the FROM clause. Also, “Personnel” may be omitted if you have established Personnel as the default database. A default database is established via the DATABASE option in a CREATE USER or MODIFY USER statement, and can be changed during a session by executing a DATABASE statement. Note: An ambiguous partial name results in an error.

Name Resolution

The following principles govern name resolution: 2

● ●

● ●



Name resolution is performed statement by statement. When an INSERT statement contains a subquery, names are resolved in the subquery first. Names in a view are resolved when the view is created. Names in a data manipulation statement (SELECT, INSERT, UPDATE) contained in a macro, are resolved when the macro is created. Names in a data definition statement (CREATE or REPLACE) contained in a macro, are not resolved until the macro is executed, and resolution uses the default database of the user submitting the EXECUTE statement. It is therefore recommended that all names in a data definition statement contained in a macro, be fully qualified.

Teradata RDBMS for UNIX SQL Reference

2-3

Data Handling Fundamentals Basic Data Manipulation

Basic Data Manipulation Teradata SQL Statement Syntax

2

A typical SQL statement consists of a statement keyword, one or more column names, a database name, a table name, and one or more optional clauses introduced by keywords. As an example, in the following single-statement request: SELECT deptno, name, salary FROM personnel.employee WHERE deptno IN(100, 500) ORDER BY deptno, name ;

the statement keyword is “SELECT”. The select list for this statement is made up of the names: ● ● ●

Deptno, name, and salary (the column names) Personnel (the database name) Employee (the table name)

The optional clause WHERE deptno IN(100, 500)

is introduced by the keyword WHERE. The optional clause ORDER BY deptno, name

is introduced by the keyword ORDER BY.

Statement Punctuation

SQL statements are punctuated using: 2











● ●

Periods to separate database names from table names, and table names from a particular column name (for example, personnel.employee.deptno). Commas to separate and distinguish column names in the select list, or column names or parameters in an optional clause. Apostrophes to delimit the boundaries of character string constants. Parentheses to group expressions or define the limits of a phrase. Semicolon to separate statements in multi-statement requests, and terminates requests submitted via certain utilities such as BTEQ. Quote marks to define the extent of nonstandard names. Colons to prefix reference parameters or client system variables.

To include an apostrophe or show possession in a title, double the apostrophes.

2-4

Teradata RDBMS for UNIX SQL Reference

2

Data Handling Fundamentals Basic Data Manipulation

Case-Sensitivity

2

Refer to Chapter 5, “Character Data Types,”and Chapter 6, “SQL Expressions.”

For Further Information

2

The section on “Defining the Database” in the Teradata RDBMS for UNIX Database Design & Administration, describes how to create, prototype, and restructure tables. Indexing, normalizing data, and designing the database for optimum performance, are also discussed there.

Teradata RDBMS for UNIX SQL Reference

2-5

Data Handling Fundamentals Data Definition Statements

Data Definition Statements Data definition statements enable you to: ● ● ● ● ● ● ● ● ● ●

Create, drop, rename, and alter tables Create, drop, rename, and replace views and macros Create and drop indexes Create, drop, and modify users and databases Collect statistics on a column or index Grant and revoke privileges Give ownership of a database to another user Establish a default database Comment on database objects Set a different collation sequence for the session

A data definition statement may not be entered as part of a multi-statement request. A data definition statement may be entered as: ● ●



A single-statement request The solitary statement, or as the last statement, in an “explicit transaction” (one or more requests enclosed by user-supplied BEGIN TRANSACTION and END TRANSACTION statement in Teradata mode, or in ANSI mode, one or more requests ending with the COMMIT keyword). The solitary statement in a macro

Successful execution of a data definition statement automatically creates and updates entries in the Data Dictionary.

2-6

Teradata RDBMS for UNIX SQL Reference

2

Data Handling Fundamentals Data Definition Statements

Data definition statements include: ALTER TABLE COMMENT COLLECT STATISTICS CREATE DATABASE INDEX MACRO TABLE USER VIEW DATABASE DELETE DATABASE USER DROP DATABASE INDEX MACRO STATISTICS

Creating Tables

2

GIVE GRANT GRANT LOGON MODIFY DATABASE USER RENAME MACRO TABLE VIEW REPLACE MACRO VIEW REVOKE REVOKE LOGON SET SESSION COLLATION TABLE USER VIEW

Tables are defined using the CREATE TABLE statement. The CREATE TABLE statement specifies a table name, one or more column names, and the attributes of each column. The CREATE TABLE statement can also be used to define a primary index and one or more secondary indexes. You can define the primary index, and any secondary index as unique, depending on whether duplicate values are to be allowed in the indexed field or fields. This statement can be used to specify the datablock size, percent freespace, and other attributes. In either Teradata or ANSI mode, if a table is created ●



not to allow duplicate rows, duplicate rows are not allowed. Such rows are silently ignored; no duplicate row error message is returned when they are encountered. to allow duplicate rows and has no UNIQUE indexes, duplicate rows are accepted.

For more information on duplicate row rules, refer to Chapter 8, “Teradata SQL Syntax Guide,”, “CREATE TABLE” and “INSERT” (INSERT...SELECT).

Teradata RDBMS for UNIX SQL Reference

2-7

Data Handling Fundamentals Data Definition Statements

Creating Indexes

2

A primary index is required. If not defined, it is assigned automatically. Secondary indexes can improve system performance; note, however, that creating a secondary index causes the Teradata RDBMS to build a separate internal subtable to contain the index data rows. After the table is created and usage patterns have developed, additional secondary indexes can be defined with the CREATE INDEX statement. See Chapter 8, “Teradata SQL Syntax Guide,” under the topics “CREATE TABLE” and “CREATE INDEX” for more information.

Views

2

A view can be compared to a window through which you can see selected portions of a database. A view is used to retrieve portions of one or more tables and/or other views. Views look like tables; they display data in columns and rows and, in general, may be used as if they were tables. However, only their column definitions are stored. A view does not contain data. It is not assembled until it is referenced by a statement. Some operations used on tables are not valid on views, and other operations are restricted, depending on how the view is defined. The CREATE VIEW statement defines a view. The statement names the view and its columns, defines a SELECT of one or more columns from one or more underlying tables and/or views, and can include conditional expressions and aggregate operators to limit the row retrieval.

Altering Table Structure and Definition

The ALTER TABLE statement changes the structure of an existing table by performing any or all of the following functions: 2

● ● ● ● ● ● ●

Adding and dropping columns. Adding column default control, FORMAT, and TITLE attributes. Changing column attributes (that do not affect stored data). Adding or removing journaling options. Adding or removing the FALLBACK option. Changing the DATABLOCKSIZE or percent FREESPACE. Adding and dropping column and table level constraints.

The table name can be changed via the RENAME statement. To redefine the primary index for a table, the table must be recreated. A table also must be recreated to change a data type attribute that affects existing data, to define or delete the

2-8

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals Data Definition Statements

COMPRESS attribute for an existing column, or to add a column that would exceed the maximum column count. Refer to the “ALTER TABLE” in Chapter 8, for rules on changing the data type attribute. Interactively, the SHOW TABLE statement can call up the current table definition, which can then be modified and resubmitted to create a new table. If the stored data is not affected by incompatible data type changes, an INSERT... SELECT statement can be used to transfer data from the existing table to the new table.

Dropping Tables, Views, and Indexes

2

The DROP TABLE and DROP VIEW statements allow you to remove a table or view from a database. The DROP INDEX statement allows you to remove a secondary index.

Teradata RDBMS for UNIX SQL Reference

2-9

Data Handling Fundamentals SQL Requests

SQL Requests Introduction

2

A request to the Teradata RDBMS consists of one or more SQL statements, and can span any number of input lines. The Teradata RDBMS can receive and execute statements that are: ●

● ● ●

Single-Statement Requests

2

2

Embedded in an application program that is written in a procedural language. Entered interactively through the BTEQ interface. Submitted in a BTEQ script as a batch job. Submitted through other supported methods (such as ODBC)

A single-statement request consists of a statement keyword followed by one or more expressions, other keywords, clauses, and phrases. A single-statement request is treated as a solitary unit of work. The form of a single statement request is: statement ; HH01A003

Multi-Statement Requests

2

Refer to Chapter 9, “Advanced SQL,” “Transaction Semantics: Operating in ANSI or Teradata Mode” for more detail. A multi-statement request consists of two or more statements separated by semicolons. The form of a multi-statement request is: ; statement ; HH01A004

Note: Multiple statement requests are flagged as non-ANSI when the SQL flagger is on.

Teradata Mode

2-10

2

A multi-statement request is treated as an implicit transaction. That is, if an error is found in any statement in the request, then the entire transaction is aborted. Abort processing backs out any changes made to the database as a result of any preceding statements, deletes any associated spooled output, and releases any associated locks. Any remaining statements are bypassed.

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals SQL Requests

ANSI Mode Complete Requests

2

2

If any error is found in a request, then just the request is aborted and not the entire transaction. A request is considered complete when either an End of Text character or the request terminator is encountered. A request terminator is a semicolon. It is the last nonblank character on an input line. A request terminator is optional except when the request appears in a SQL macro, or when it is entered through BTEQ.

Null Statements or Comments

A null statement is a statement that is empty except for spaces or SQL comments. The form of a null statement or comment is: 2

- - comment_text

;

or /* comment_text

*/

; HH01A100

Teradata RDBMS for UNIX SQL Reference

2-11

Data Handling Fundamentals Structure of a SQL Statement

Structure of a SQL Statement Introduction

2

The general structure of an SQL statement is:. 2

,

StatementKeyword(s)

expressions keywords clauses phrases HH01A082

where

Requests in Macros

2-12

2

This syntax element . . .

Specifies . . .

StatementKeyword

the function of the statement and introduces the primary clause.

expressions

constants, name references, or operations using names and constants.

keywords

functions or special values; introduce clauses or phrases. Most keywords are reserved words and may not be used in names.

clauses

subordinate statement qualifiers.

phrases

data attribute phrases.

A request defined in the body of a macro must be terminated. Refer to “Macros”, for a discussion of macros and their use.

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals Data Manipulation Language (DML)

Data Manipulation Language (DML) Selecting Columns

2

2

Use the SELECT statement to obtain information from the tables in a database. The SELECT statement specifies the table columns from which to obtain the data, the corresponding database (if you have not already established a default database), and the table (or tables) that you need within that database. For example, to request the data from the name, salary, and jobtitle columns of the employee table, enter: SELECT name, salary, jobtitle FROM employee ;

and receive: Name -----------Newman P Chin M Aguilar J Russell S Clements D

Salary ---------28,600.00 38,000.00 45,000.00 65,000.00 38,000.00

JobTitle ---------Test Tech Controller Manager President Salesperson

Note: The left-to-right order of the columns in a result is determined by the order in which the column names are entered in the statement. As long as a statement is otherwise constructed properly, the spacing between statement components may vary. For example, the statement above could be: SELECT

name ,

salary,jobtitle

FROM

employee;

To select all the data in the employee table, you could enter: SEL * FROM employee ;

The asterisk specifies that the data in all columns of the table be returned.

Selecting Rows

2

The SELECT statement retrieves stored data from a table. All rows, specified rows, or specific fields of all or specified rows can be retrieved. The FROM, WHERE, ORDER BY, DISTINCT, WITH, GROUP BY, and HAVING clauses provide for a fine detail of selection criteria. To obtain data from specific rows of a table, use the WHERE clause of the SELECT statement. That portion of the clause following the keyword WHERE causes a search for rows that satisfy the condition specified. For example, to get the name, salary, and title of each employee in Department 100, use the WHERE clause:

Teradata RDBMS for UNIX SQL Reference

2-13

Data Handling Fundamentals Data Manipulation Language (DML) SELECT name, salary, jobtitle FROM employee WHERE deptno = 100 ;

The response is: Name ----------Chin M Greene W Moffit H Peterson J

Adding Rows

Salary ---------38,000.00 32,500.00 35,000.00 25,000.00

JobTitle ---------Controller Payroll Ck Recruiter Payroll Ck

The INSERT statement is used to add rows to a table. 2

One statement is required for each new row, except in the case of an INSERT...SELECT statement. For more detail on this, refer to Chapter 8, “Teradata SQL Syntax Guide,” (INSERT-SELECT). Values for all the fields (columns) or one or more selected fields of the row may be specified in any order. A NULL, or the column’s default value, is stored in any field for which a value is not specified. One statement is required for each new row except in the case of INSERT ... SELECT. The CREATE TABLE statement affects an insert operation in the following ways:

Modifying Rows

2

WHEN an INSERT statement . . .

THEN . . .

attempts to add a duplicate row or rows for at least one unique index, either primary or secondary, or the table is defined not to allow duplicate rows

an error returns.

omits a value for a field for which a default value is defined

the default value is supplied for that field.

omits a value for a field for which NOT NULL is specified, and no default is specified

the operation is rejected and an error message is returned.

supplies a value that does not satisfy the constraints specified for a field, or violates some defined constraint on a field or fields

the operation is rejected and an error message is returned.

The UPDATE statement allows you to modify data in one or more rows of a table. In the UPDATE statement, the column name of the data to be modified is specified along with the new value. A WHERE clause is used to qualify the row(s) whose data is changed. Attributes specified in the CREATE TABLE statement affect an update operation in the following ways:

2-14

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals Data Manipulation Language (DML) ●





Removing Rows

2

When an update supplies a value that does not satisfy the constraints specified for a field, or violates some defined constraint on a field or fields, the update operation is rejected and an error message is returned. When an update supplies the value NULL and a NULL is allowed, any existing data is removed from the field. If the result of an UPDATE will violate uniqueness constraints or create a duplicate row in a table which does not allow duplicate rows, an error message is returned.

The DELETE statement allows you to remove an entire row or rows from a table. A WHERE clause qualifies the rows that are deleted.

Teradata RDBMS for UNIX SQL Reference

2-15

Data Handling Fundamentals Default Database

Default Database While in a Teradata session, you may repeatedly query, define, and manipulate data from a single database. To avoid keying the name of the database each time you enter a statement, establish a default database by specifying the name in a DATABASE statement. Once defined, the default database remains in effect until the end of a session or until it is replaced by a subsequent DATABASE statement. Once you establish a default database, SQL automatically supplies the database name every time you enter a statement. For example, after entering: DATABASE personnel ;

you can enter a SELECT statement as follows: SELECT deptno (TITLE ’Org’), name FROM employee ;

which would have the same results as: SELECT deptno (TITLE ‘Org’), name FROM personnel.employee;

To establish a default database, you must have some privilege on a database, macro, table, user, or view in that database. You can define a default database for subsequent sessions. Enter a MODIFY USER statement with a DEFAULT database clause that specifies the name of a database that will be invoked each time you log on. For example, the following statement automatically establishes Personnel as Marks’ default database at the next logon: MODIFY USER marks AS DEFAULT DATABASE = personnel ;

The statement examples in this document assume that you have established Personnel as your default database. To obtain information from a table in another database, specify that database name, a period, and the table name. For further information about using a default database refer to Teradata RDBMS for UNIX Database Design & Administration, Chapter 8, under “Object Ownership.”

2-16

Teradata RDBMS for UNIX SQL Reference

2

Data Handling Fundamentals Macros

Macros Introduction

2

2

A frequently used SQL statement or series of statements, can be set up in a macro and defined using CREATE MACRO. See “CREATE MACRO” in Chapter 8, “Teradata SQL Syntax Guide.”. The statements in the macro are executed using the EXECUTE statement. See “EXECUTE” in Chapter 8, “Teradata SQL Syntax Guide.” A macro can include an EXECUTE statement that executes another macro. You can create a macro for your own use, or grant the privilege of executing it to others. For example, your macro may enable a user in another department to perform operations on the data in the Teradata RDBMS. In executing the macro, the user need not be aware of the database being accessed, the tables affected, or even the results. Note: Although a multi-statement operation may be entered interactively, using an explicit transaction (either BT/ET or COMMIT) it is preferable to define such an operation as a macro, because an explicit transaction will cause locks placed on objects by statements in the transaction to be held until the statement sequence is completed with an END TRANSACTION or COMMIT. If you were to enter such a sequence interactively from BTEQ, items in the database could remain locked to others while you keyed and entered each statement. A macro is deleted by using the DROP MACRO statement.

Contents of a Macro

2

A macro can contain a data definition statement (for example, CREATE, ALTER, MODIFY) if it is the only SQL statement in that macro. Such a data definition statement is not resolved until the macro is executed, at which time unqualified database object references are fully resolved using the default database of the user submitting the EXECUTE statement. It is, therefore, recommended that object references in a macro data definition statement be fully qualified (e.g., databasename.tablename) in the macro body. A macro may contain parameters that are filled each time the macro is executed. It also may include a USING modifier, which allows the parameters to be filled with data from an external source such as a disk file. The colon character is used to prefix references to a parameter name in the macro.

Teradata RDBMS for UNIX SQL Reference

2-17

Data Handling Fundamentals Macros

In general, the statements that make up a macro are written into a file on a text processing system, and then are transferred via a utility such as BTEQ. Changes and corrections are made via the text processing system and the REPLACE MACRO statement. A macro also may be deleted and a replacement created. If you are unfamiliar with any part of this process, be sure to coordinate with your installation’s system or database administrator.

Executing a Macro

2

Regardless of the number of statements in a macro, the system treats it as a single request. When you execute a macro, either all of its statements are processed successfully, or none are processed. If a macro fails, it is aborted; any changes made to the data are backed out, and the database is returned to its original state. As a macro is executed, each of the statements acquires the appropriate read or write lock. A read lock enables users to query the same data at the same time, while disallowing any changes in the data; a write lock prevents queries against data that is being changed.

2-18

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals Subqueries

Subqueries Introduction

2

2

Subqueries are nested SELECT statements. They can be used to ask a series of questions in order to arrive at a single answer. Subqueries may be nested to any number of levels. Correlated subqueries are described in Chapter 9, “Advanced SQL.”

Three Level Subqueries: Example

The following subqueries, nested to three levels, are used to answer the question “Who is Marston’s manager?”: 2

SELECT Name FROM Employee WHERE EmpNo IN (SELECT MgrNo FROM Department WHERE DeptNo IN (SELECT DeptNo FROM Employee WHERE Name = ’Marston A’) ) ;

The subqueries that pose the questions that lead to the final answer are inverted: ●





The third subquery asks the Employee table for the number of Marston’s department. The second subquery asks the Department table for the employee number (MgrNo) of the manager associated with this department number. The first subquery asks the Employee table for the name of the employee associated with this employee number (MgrNo).

The result: Name Watson L

can be obtained using only two levels of subquery, as follows: SELECT Name FROM Employee WHERE EmpNo IN (SELECT MgrNo FROM Department, Employee WHERE Employee.Name = ’Marston A’ AND Department.DeptNo = Employee.DeptNo) ;

In this example, the second subquery defines a join of Employee and Department tables.

Teradata RDBMS for UNIX SQL Reference

2-19

Data Handling Fundamentals Supported Characters

Supported Characters

2

The Teradata RDBMS can support standard (English language) characters, and characters having diacritical marks (such as ä, ê, and à). Multinational character data, including diacritical characters, is provided for each RDBMS system configuration. When the Kanji hash feature is installed, the RDBMS does not support diacritical characters. See Appendix E through Appendix J for more information on character support.

Multinational Character Handling

2

The Teradata RDBMS supports row data containing diacritical characters (diacritical characters cannot be used in statement syntax or to define objects). This feature allows you to insert and retrieve data in a language other than English. It also allows the current character set and collation sequence to be controlled at the session level. The Teradata RDBMS supports the following multinational characters: ●

● ● ●

lowercase and uppercase letters and diacritical characters (a-z and A-Z (including diacriticals a, c, e, i, n, o, u, y and A, C, E, I, N, O, U, Y)); Arabic numbers (0-9); punctuation, graphic, and control characters; several unassigned characters.

Change the HashFuncDBC option to International, via the RDBMS Control utility. To save HashFuncDBC change, run the System Initializer (SysInit) utility. Note: See Teradata RDBMS for UNIX Support Utilities Reference, for more information on the RDBMS Control and System Initializer utilities. To manipulate data using a multinational character set, the database administrator must populate system tables with the applicable translation codes and a default character set for each client connection. The Teradata RDBMS then must be reset to establish the character sets as currently available for use in a session. The DBC.CharTranslations, DBC.CharSets, and DBC.HostsInfo views can be queried to review the contents of the system tables associated with multinational character support. If the views are empty or do not exist, multinational character sets are not available.

2-20

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals Operations on Null

Operations on Null Introduction Arithmetic Operator or Function

Comparison Operator

2

2

2

2

A null indicates an empty field, an unknown value, or that the field has no data. If an operand of any arithmetic operator or function other than ZEROIFNULL, NULLIF, COALESCE, or CASE, is null, the result of the operation or function is null. WHEN the expression is . . .

THEN the result is . . .

5 + NULL

Null

LOG(NULL)

Null

NULLIFZERO(NULL)

Null

ZEROIFNULL(NULL))

0

COALESCE(NULL, 6)

6

If either operand of a comparison operator is null, the result is UNKNOWN. WHEN the expression is . . .

THEN the result is . . .

5 = NULL

Unknown

5 NULL

Unknown

NULL = NULL

Unknown

NULL NULL

Unknown

Note that if the argument of the NOT operator is unknown, the result is unknown. This translates to false as a final boolean result.

NULLIF

COALESCE

Null is the Lowest Value

2

Considered a CASE abbreviation expression. Similar in function to NULLIFZERO but more versatile. See Chapter 6, “SQL Expressions” under the topic “CASE Expression”.

2

COALESE is also considered a CASE abbreviation expression. It returns the first NON NULL in an expression list. See Chapter 6, “SQL Expressions” under the topic “CASE Expression”.

2

When records are sorted using an ORDER BY clause, null is sorted as the lowest value.

Teradata RDBMS for UNIX SQL Reference

2-21

Data Handling Fundamentals Operations on Null

If any row has a null in the column being grouped, then all rows with a null are placed into one group.

Searching for Nulls

2

The IS NULL operator tests row data for the presence of nulls. For example, to search for the names of all employees who have a null in the deptno column, you could enter the statement: SELECT name FROM employee WHERE deptno IS NULL ;

This query produces the names of all employees with a null in the deptno field. Because all employees contained in the employee table have been assigned to a department, no rows are returned.

Searching for Non Nulls

2

To exclude nulls from the results of a query, use the IS NOT NULL operator. For example, to search for the names of all employees with a value other than null in the jobtitle column, enter the statement: SELECT name FROM employee WHERE jobtitle IS NOT NULL ;

The result of this query is the names of all employees with a value other than null in the jobtitle column. When you enter this statement, the names of all employees are returned because every employee has been given a job title.

Searching for Nulls and Non Nulls Together

2

To search for nulls and non-nulls in the same statement, the search condition for nulls must be included separately from any other search conditions. For example, to select the names of all employees with the job title of vice pres, manager, or null, enter either of the following statements: SELECT name, jobtitle FROM employee WHERE jobtitle IN (’Manager’ or ’Vice Pres’) OR (jobtitle IS NULL) ;

or SELECT name, jobtitle FROM employee WHERE jobtitle IS NULL OR jobtitle IN (’Manager’ or ’Vice Pres’) ;

Teradata RDBMS Fills in Values in NULL columns

2-22

2

When the Teradata RDBMS returns information to the client system in record mode, it is necessary to fill null items with some value for the underlying column. This is required because client system languages do not recognize null as a value. Table 2-1 shows the values returned for declared column types.

Teradata RDBMS for UNIX SQL Reference

Data Handling Fundamentals Operations on Null Table 2-1 Returned Nulls Column Type

Value Returned

INTEGER

0

FLOAT

0

DECIMAL

0

REAL

0

DOUBLE PRECISION

0

NUMERIC

0

CHAR[(n)]

Blank if n omitted, or n blanks

BYTE[(n)]

0 byte if n omitted, or n binary zero bytes

VARBYTE(n)

Zero-length byte string

VARCHAR(n)

Zero-length character string

DATE

0

GRAPHIC[(n)]

If n is omitted, one double-byte zero; otherwise, n double-byte zeros

VARGRAPHIC(n)

Zero-length graphic string

Note: The values returned for NULLS are not distinguishable from legal NON NULLs. It is normal to access the data in IndicData mode, in which additional information is returned to the Host, flagging NULLs. BTEQ, for example, displays NULLs in reports as ‘?’, based on knowing whether values are NULL.

Teradata RDBMS for UNIX SQL Reference

2-23

Data Handling Fundamentals Statement Response

Statement Response Introduction

2

The Teradata RDBMS responds to an SQL statement with one of the following responses: ● ● ●

Success response, with optional Warning response Failure response Error response (ANSI mode only)

Depending on the type of statement, the RDBMS also responds with one or more rows of data. A response to a request that contains more than one statement, such as a macro, is not returned to the client until all statements in the request are successfully executed. The manner in which the response is returned depends on the interface that is being used. For example, if an application is using a language preprocessor, the activity count, warning code, error code, and fields from a selected row are returned directly to the program’s variables. If a user is working through BTEQ, a success, error, or failure response is displayed automatically.

2-24

Teradata RDBMS for UNIX SQL Reference

2

Data Handling Fundamentals Success Response

Success Response Introduction

2

2

A success response contains an activity count that indicates the total number of rows involved in the result. For example, the activity count for a SELECT statement is the total number of rows selected for the response. For a SELECT, COMMENT, or ECHO statement, the activity count is followed by the data that completes the response. An activity count is meaningful for a SELECT, INSERT, UPDATE, or DELETE statement. For other SQL statements, activity count is meaningless.

Example

The following SELECT statement 2

sel avg(f1) from Inventory;

in BTEQ, returns the successful response message: *** Query completed. One row found. One column returned. *** Total elapsed time was 1 second. Average(f1) ----------14

Teradata RDBMS for UNIX SQL Reference

2-25

Data Handling Fundamentals Warning Response

Warning Response Introduction

Example

2

An SQL warning response typically occurs with a successful request response. It indicates that either an anomaly has occurred or informs the user about the anomaly, as it may be important to the interpretation of the results returned. If nulls are included in column f1’s data, then

2

sel avg(f1) from Inventory;

in BTEQ, returns the successful response message with a warning about the nulls: *** Query completed. One row found. One column returned. *** Warning: 2892 Null value eliminated in set function *** Total elapsed time was 1 second. Average(f1) ----------14

This warning response is not generated if the session is running in Teradata mode.

2-26

Teradata RDBMS for UNIX SQL Reference

2

Data Handling Fundamentals Failure Response

Failure Response Introduction

Example

2

2

A failure response includes a statement number, an error code, and an associated text string describing the cause of the failure. If one statement in a macro fails, a single failure response is returned to the client, and the results of any previous statements in the transaction are backed out. The following SELECT statement

2

sel * from Inventory:;

in BTEQ, returns the failure response message: *** Failure 3709 Syntax error, replace the ':' that follows the name with a ';'. Statement# 1, Info =20 *** Total elapsed time was 1 second.

Teradata RDBMS for UNIX SQL Reference

2-27

Data Handling Fundamentals Error Response (ANSI Mode Only)

Error Response (ANSI Mode Only) Introduction

2

An error response occurs when the anomaly was bad enough to prevent the correct processing of the request. In ANSI mode, an error for a request causes the request to rollback, and not the transaction.

Example 1

The following command 2

.set session trans ANSI;

returns the error message: *** Error: You must not be logged on .logoff to change the SQLFLAG or TRANSACTION settings.

Example 2

Assume that the user has logged on in ANSI mode. 2

show table inv;

----------------------CREATE MULTISET TABLE MAIN.inv, FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL ( item INTEGER CHECK ((item >=10) AND (item 5;

A.a A.b

B.a

3

1

3

6

6

6

When the WHERE restriction is applied, the results are: A.a A.b 6

B.a 6

6

But if we include the WHERE restriction as part of the join condition: SELECT * FROM A LEFT OUTER JOIN B ON (A.b >5) and (A.a = B.a);

Teradata RDBMS for UNIX SQL Reference

3-13

Joins: Working with Multiple Tables Outer Join

we get some confusing results: A.a A.b

B.a

6

6

6

3

1

Null

Notice that the join condition, which specified the inner join (of the left outer join), restricts the results to 6, 6, 6. But then the rows that were not returned as a result of the inner join are returned and extended with nulls. Thus, for our second row, we get 3, 1, null.

Using Search Conditions in ON Clauses

3

In some cases, a search condition is needed in the ON clause. For example, to list all courses offered as well as the course requested by employee 236, the following query can be used: SELECT Offerings.CourseNo, Enrollment.EmpNo FROM Offerings LEFT JOIN Enrollment ON Offerings.CourseNo = Enrollment.CourseNo AND Enrollment.EMpNo=236;

Where to Put Search Conditions?

It is recommended that the search condition on an: ● 3



outer table be put in the WHERE clause, inner table be put in the ON clause.

Note: In a left outer join, the outer table is the left table, and the inner table is the right table.

Joins on Views Joins on Views Containing an Aggregate

3

There can be joins using the outer join syntax between two tables, a table and a view, and two views. Join operations can be performed on views containing an aggregate.

3

In the following example, table CustFile, is joined with view CustProdSales, containing a SUM operation, in order to determine which companies purchased more than $10,000 of item #123: CREATE VIEW CustProdSales (custno, pcode, sales) AS SELECT custno, pcode, SUM(sales) FROM SalesHist GROUP BY custno, pcode; SELECT company_name, sales FROM CustProdSales a, CustFile b WHERE a.custno = b.custno AND a.pcode = 123 AND a.sales > 10000;

3-14

Teradata RDBMS for UNIX SQL Reference

Joins: Working with Multiple Tables Cross Join

Cross Join

3

SQL allows unconstrained joins; that is, joins in which a WHERE relationship between the tables that are joined is not specified. The result of an unconstrained join is a cross join, also called a Cartesian product because the join is the product of the number of rows in each table that is joined. If you do not wish to specify a join condition, and thus wish to return a Cartesian product, which is not an outer join of two or more tables, you would write the following SELECT statement: SELECT . . . FROM A CROSS B;

Since the reserved words CROSS and JOIN are optional, you could simply write: SELECT . . . FROM A,B;

For example, if Table A contained five rows, and table B contained three rows, the Cartesian product is 3 x 5, or 15. An unconstrained join on these tables would result in 15 rows being returned. Before performing a cross join, you should consider how expensive, with respect to resource consumption, a cross join may be. For example, a cross join of two tables, A and B, each with 1,000 rows would return a joined table of 1 million rows.

Teradata RDBMS for UNIX SQL Reference

3-15

Joins: Working with Multiple Tables Self-Join

Self-Join

3

A self-join combines the information from two or more rows of the same table into a single result row, effectively joining the table with itself. For example, the following query asks the names of employees who have more years of experience than their managers: SELECT Workers.Name, Workers.YrsExp, Workers.DeptNo, Managers.Name, Managers.YrsExp FROM Employee Workers, Employee Managers WHERE Managers.DeptNo = Workers.DeptNo AND Managers.JobTitle IN (’Manager’, ’Vice Pres’) AND Workers.YrsExp > Managers.YrsExp ;

The operation treats the Employee table as if it were two tables; one named Workers, the other named Managers. This is accomplished using the FROM clause. Because each of these fictitious tables have the same columns (Name, YrsExp, and DeptNo) each column name must be qualified with its table’s alias name, as defined in the FROM clause (for example, “Workers.DeptNo”). The WHERE clause establishes the following things: ● ● ●

A key to both tables (DeptNo) Which employees belong in the Managers table (first AND) Which workers and managers should be listed in the tables (second AND)

The result of the self-join is as follows: Name Greene W Carter J Smith T Aguilar J Leidner P Russell S

3-16

YrsExp

DeptNo

15 20 10 11 13 25

100 500 700 600 300 300

Teradata RDBMS for UNIX SQL Reference

Name Jones M Watson L Watson L Regan R Phan A Phan A

YrsExp 13 8 8 10 12 12

Teradata SQL Lexical Conventions

Chapter 4

Teradata SQL Lexical Conventions

TeradataR DBMS for UNIX SQL Reference

Teradata SQL Lexical Conventions

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon About This Chapter 4

About This Chapter

4

This chapter explains how to use the Teradata SQL (Structured Query Language) language, a single, unified, non-procedural language that provides capabilities for query, data definition, data modification, and data control on the Teradata RDBMS. The SQL lexicon consists of: ● ● ● ● ● ● ● ●

Words Names Delimiters Constants Operators Lexical separators Statement separator Request terminator

This chapter includes detail on lexicon for Japanese characters. The characters that comprise this lexicon may be represented on the client system in ASCII, EBCDIC, or other defined character sets. If the client system character data is not ASCII, it is converted by the Teradata RDBMS to an internal form that depends on the character support chosen (European or Japanese), for processing and storage; data returned to the client system is converted to the client’s character set. The internal forms used for European and Japanese character support are described in Appendix E through Appendix J. Japanese character (Kanji) code notation used in this manual is described in Chapter 1, “Notation.” Note: The ASCII code mentioned in this manual is based on Standard ASCII, but includes some Teradata extensions. See Appendix G, “International and Japanese Character Support,” Table G-1, “Teradata Internal Form for International Character Support”, and Appendix E, “ASCII Hexadecimal Translation Tables”.

Teradata RDBMS for UNIX SQL Reference

4-1

Teradata SQL Lexicon Words and Names (Non-Japanese Character)

Words and Names (Non-Japanese Character) Introduction

Words

4

4

The definitions of words and names in this section pertain to the ASCII character set (non Japanese character set). The definitions and rules for using Japanese character names are described in the following sections. Words consist of strings of 1 to 30 characters, chosen from the following set: ● ● ●



Uppercase or lowercase letters (A to Z and a to z). Digits (0 through 9). The special characters dollar sign ($), pound sign (#), and underscore ( _ ). A word must begin with a letter.

A word may be either a name or a keyword. A word is a name that is not in double quotes. Note: Words with lower case letters, ($), (#), beginning with an underscore, or more than 18 characters in length, are flagged as not entry level ANSI when the SQL flagger is enabled.

Names

4

A name is a word that is not a keyword or a set of characters in double quotes. Passwords and account identifiers are also considered to be names. Names must meet the requirements defined above for words. Names that define databases and objects must follow these rules: ● ●

● ● ● ● ●

Databases cannot have identical names. Tables, views, or macros in a database cannot have identical names. Columns of a table or view cannot have identical names. Parameters defined for a macro cannot have identical names. Two indexes on a table cannot have the same name. Two constraints on a table cannot have the same name. Indexes and constraints can have the same name as the table they are in.

You must define and reference each object (user, database, table, column, view, macro, and checkpoint) by a symbolic name.

4-2

Teradata RDBMS for UNIX SQL Reference

4

Teradata SQL Lexicon Words and Names (Non-Japanese Character)

CHECK constraints, REFERENCE constraints, and INDEX objects, may also have assigned names. For these objects, names are optional. A name is stored as a string of hexadecimal bytes. The maximum length of a name is 30 bytes.

Use of Quotation Marks

4

Although unquoted multiword names with blanks are not supported, an existing database or object that was created with such a name may still be referenced by enclosing its name in quotation marks. For example: “Current Salary”

Note: Multi-word names and blanks are supported if they are quoted. Quotation marks may also be used to support any nonstandard name, such as one that contains special characters. For example: “D’Agusta”

The quotation marks are not part of the name; they are required, however, if the name is not a valid word. Examples of valid names are: Employee job_title CURRENT_SALARY DeptNo Population_of_Los_Angeles Totaldollars “Table A” “Today’s Date”

Caution:

If you use quoted names, the double quote characters that delineate the names are not counted in the length of the name and are not stored in Dictionary tables used to track name usage. If a Dictionary view is used to display such names, they are displayed without the double quote characters, and if the resulting names are used, without adding double quotes, the likely outcome is an error report. In the example above, “D’Augusta” may be the name of a column in the Dictionary view DBC.Columns and the HELP statements that return column names return the name as D’Augusta (without being enclosed in double quotes).

Teradata RDBMS for UNIX SQL Reference

4-3

Teradata SQL Lexicon Words and Names (Non-Japanese Character)

Case-Dependence

4

Names are not case-dependent; a name may not be used twice simply by changing its case. Any mix of upper- and lower-case may be used when referencing symbolic names in a request. For example, the following statements are considered to be identical: SELECT Salary FROM Employee WHERE EmpNo = 10005; SELECT SALARY FROM EMPLOYEE WHERE EMPNO = 10005; SELECT salary FROM employee WHERE eMpNo = 10005;

The case in which column names are defined can be important. The column name is the default title of an output column, and symbolic names are returned in the same case in which they were defined. For example, assume that the columns in the SalesReps table are defined as follows: CREATE TABLE SalesReps ( last_name VARCHAR(20) NOT NULL, first_name VARCHAR(12) NOT NULL, ...

Use of TITLE Phrase

In response to a query that does not define a TITLE phrase, such as: 4

SELECT Last_Name, First_Name FROM SalesReps ORDER BY Last_Name ;

the column names are returned exactly as defined; for example, Last_ Name, then First_Name. The TITLE phrase can be used either in the column definition, or in a statement to specify the case, wording, and placement of an output column heading.

4-4

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Japanese Character Names

Japanese Character Names Introduction

4

For a Japanese character site, a name is a string that conforms to the following rules: ● ●

Cross-Platform Integrity

4

4

The length may be from 1 to 30 bytes. The contents also may include the following: ● Uppercase and lowercase simple Latin letters (A...Z, a...z) in single byte and multibyte forms. ● Digits (0-9) in single byte and multibyte forms. These cannot appear as the first character in the string. ● The special characters $ (dollar sign), # (number sign), and _ (underscore) in single byte and multibyte forms. ● Katakana characters and sound marks in single byte and multibyte forms. ● Hiragana characters. ● Kanji ideographs from JIS-x0208.

Note that the Teradata RDBMS uses canonical representations for simple Latin letters, digits, the symbols $, #, and _. Katakana characters can be shared between Kanji EBCDIC and KanjiShift-JIS clients. JIS-x0208 characters can be shared between KanjiEUC and KanjiShift-JIS clients. It is the responsibility of the user to ensure cross-platform integrity where desired. Note: Comparison between multibyte characters is always CASESPECIFIC. For future compatibility, use the CASESPECIFIC option to define all columns with a data type of CHAR, VARCHAR, or LONG VARCHAR.

Calculating the Length of a Name

4

The length of a name is measured by the physical bytes of its internal representation, not by the number of visual characters. (Note that under the KanjiEBCDIC character sets, the Shift-Out and Shift-In characters that delimit an multibyte character string are included in the byte count.)

Teradata RDBMS for UNIX SQL Reference

4-5

Teradata SQL Lexicon Japanese Character Names

For example, the following table name contains six logical characters of mixed single byte characters/multibyte characters, defined during a KanjiEBCDIC session: QR

All single byte characters, including the Shift-Out/Shift-In characters, are translated into the Teradata RDBMS internal encoding, based on JIS-x0201 (Figure H-1 defines the single byte portion for internal storage of KanjiEBCDIC). Under the KanjiEBCDIC character sets, all multibyte characters remain in the client encoding. Thus, the processed name is stored as a string of twelve bytes, padded on the right with the single byte space character to a total of 30 bytes. The internal representation is as follows: 0E 42E3 42C1 42C2 42F1 0F 51 52 20 20 20 20 20 20 20 20 20 20 20 20 . . . < T A B 1 > Q R

To ensure upgrade compatibility, an object name created under one character set should not exceed 30 bytes after conversion to the encoding of another character set. When you create objects from one Kanji character set, ensure that the object name can be converted to any other Kanji character set without exceeding 30 bytes in the internal encoding for that character set. For example, a single Katakana character occupies 1 byte in KanjiShift-JIS. However, when KanjiShift-JIS is converted to KanjiEUC, each Katakana character occupies two bytes. Thus, a 30byte Katakana name in KanjiShift-JIS would expand in KanjiEUC to 60 bytes, which is illegal. The formula for calculating the correct length of an object name is as follows: Len = ASCII + (2*KANJI) + MAX(2*KATAKANA, (KATAKANA + 2*S2M + 2*M2S))

where:

4-6

This variable . . .

Represents the number of . . .

ASCII

single byte ASCII characters in the name.

KATAKANA

single byte Hankaku Katakana characters in the name.

Kanji

double byte characters in the name from the JIS-x0208 standard.

S2M

transitions from ASCII, or KATAKANA, to JIS-x0208.

M2S

transitions from JIS-x0208 to ASCII or KATAKANA.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Japanese Character Object Names

Japanese Character Object Names Introduction

4

An object name cannot contain any of the following: 4

● ●



● ●

Multibyte spaces. Symbols other than $ (dollar sign), # (number sign), or _ (underscore) in single byte or multibyte forms. Digits (0-9) in single byte or multibyte forms when they are the first character in the name. Greek and Cyrillic characters. User-defined characters.

When creating object names, additional restrictions apply under each type of character set, as discussed in the following passages. Charts of the supported Kanji character sets, the Teradata RDBMS internal JIS encodings, the valid character ranges for Kanji object names and data, and the invalid character ranges for Kanji data are given in Appendix H, “Japanese Character Sets.” The non-valid character ranges for Kanji object names are given in Chapter I, “Non-valid Japanese Character Code.”

Teradata RDBMS for UNIX SQL Reference

4-7

Teradata SQL Lexicon Japanese Character Object Names

Character Ranges for KanjiEBCDIC Object Names

The following passages discuss the JIS characters that are valid and invalid for KanjiEBCDIC object names. JIS Character Set

Relevant passages in the book

JIS-x0201

Figure H-5 in Appendix H, “Japanese Character Sets”, documents the valid ranges for the KATAKANAEBCDIC character set for JIS-x0201.

4

Figure H-6 in Appendix H documents the valid ranges for the KANJIEBCDIC5026_0I character set for JISx0201. Neither set permits Katakana symbols 0x41—0x45, nor Latin symbols other than $, #, and _. Appendix Figure H-7 in Appendix H documents the valid ranges for the KANJIEBCDIC5035_0I character set for JIS-x0201. This set does not permit Katakana symbols 0x42—0x46 nor Latin symbols other than $, #, and _.

JIS-x0208

Appendix Figure H-8 in Appendix H documents the valid ranges for JIS-x0208. Note that characters in the User and Marker areas in table are not allowed. Also, many KanjiEBCDIC characters cannot be mapped to JIS-x0208. A list of these characters is given in Table H-1 and Table H-2 in Appendix H.

4-8

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Japanese Character Object Names

Character Ranges for KanjiEUC Object Names

4

The following passages discuss the JIS characters that are valid and invalid when creating object names under the KanjiEUC character set. JIS Character Set

Relevant passages in the book

JIS-x0201

The valid ranges for JIS-x0201 characters in KanjiEUC object names can be found in rows cs0 and cs2 in Table H-3 in Appendix H. The set does not permit Katakana symbols 0x8EA1—0x8EA5 nor Latin symbols other than $, #, and _.

JIS-x0208

The valid ranges for JIS-x0208 are given in row cs1 of the “EUC Code Set Localization” table. However, note that within the JIS-x0208 ranges, there are unassigned characters that are not allowed in object names; for example, the unassigned character 0xA9A1 is illegal. In addition, certain characters that are valid in JISx0208 do not map to the KanjiEBCDIC encoding and are not allowed at all under KanjiEUC. These are shown in Appendix I.

JIS-x0212

The JIS-x0212 characters found in row cs3 of the table Table H-3 are not allowed in KanjiEUC object names.

Teradata RDBMS for UNIX SQL Reference

4-9

Teradata SQL Lexicon Japanese Character Object Names

Character Ranges for KanjiShift-JIS Object Names

The following passages discuss the JIS characters that are valid and invalid when creating object names under the KanjiShift-JIS character set. 4

JIS Character Set

Relevant passages in the book

JIS-x0201

The valid ranges for JIS-x0201 characters in KanjiShift-JIS object names are listed in table Table H-5 in Appendix H. The set does not permit Katakana symbols 0x82A1—0x82A5 nor Latin symbols other than $, #, and _.

JIS-x0208

The valid ranges for JIS-x0208 are detailed in Table H-5 in Appendix H. Note that: Gaiji, with first byte values 0xF0-0xF9, and the IBM defined characters, with first byte values 0xFA-0xFC, as listed in Table H-5 are not allowed in object names. Within the JIS-x0208 ranges, there are unassigned characters that are not allowed in object names; for example, the unassigned character 0x8540 is illegal.

Certain characters that are valid in JIS-x0208 do not map to the KanjiEBCDIC encoding and are not allowed under KanjiShift-JIS. These are given in Appendix I.

4-10

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Rules for Non-ASCII Character Object Names

Rules for Non-ASCII Character Object Names

4

Note: This section only applies to the use of extended character sets. Rules for ASCII (EBCDIC) are comparably simpler.

Object Name Validation Rules

When constructing an object name from a session with character set X, you must make sure that the following criteria are met: 4









How Validation Occurs

4

The single byte character Katakana characters are those defined in JIS-x0201, with the exceptions noted for each character set in the preceding passages. The multibyte character Kanji characters are those defined in JIS-x0208, with the exceptions noted for each character set in the preceding passages. The object name can be converted to any other character set Y, and that it will not exceed 30 bytes in Y’s encoding. That is, LEN < 31 bytes. Adjacent Shift-Out and Shift-In () characters and adjacent Shift-In and Shift-Out characters (> 30 bytes. b. LEN is > 30 bytes c. Consecutive SO and SI characters are not allowed. d. Consecutive SI and SO characters are not allowed. e. The double byte blank is not allowed.

4-12

S2M

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Rules for Non-ASCII Character Object Names Table 4-2 KanjiEUC Object Name Examples string_expr

ASCII

ABCDEFGHIJKLM ABCDEFGHIJKLM ss2ABCDEFGHIJKL Ass2BCDEFGHIJKL ss3C

Katakana

Kanji

S2M

M2S

LEN

Result

6

0

7

3

3

32

Not Valida

6

0

7

2

2

28

Valid

0

1

11

1

1

27

Valid

0

1

11

2

2

31

Not Validb

0

0

0

1

1

4

Not Validc

Katakana

Kanji

S2M

M2S

a. LEN is > 30 bytes. b. LEN is > 30 bytes c. Characters from code set 3 are not allowed

Table 4-3 KanjiShift-JIS Object Name Examples string_expr

ASCII

ABCDEFGHIJKLMNOPQR ABCDEFGHIJKLMNOPQR

LEN

Result

6

7

5

1

1

30

Valid

6

7

5

2

2

31

Not Valida

a. LEN is > 30 bytes.

Teradata RDBMS for UNIX SQL Reference

4-13

Teradata SQL Lexicon Object Name Translation and Storage

Object Name Translation and Storage Object names are stored in the dictionary tables using the following translation conventions: Character Type

Description

Single byte

All single byte characters in a name, including the KanjiEBCDIC Shift-Out/Shift-In characters, are translated into the Teradata internal representation (based on JIS-x0201 encoding).

Multibyte

Multibyte characters in object names are handled according to the character set in effect for the current session, as follows: Multibyte Character Set

Description

KanjiEBCDIC

Each multibyte character within the Shift-Out/Shift-In delimiters is stored without translation; that is, it remains in the client encoding. The name string must have matched (but not consecutive) Shift-Out and Shift-In delimiters.

KanjiEUC

Under code set 1, each multibyte character is translated from KanjiEUC to KanjiShift-JIS. Under code set 2, byte ss2 (0x8E) is translated to 0x80; the second byte is left unmodified. This translation preserves the relative ordering of code set 0, code set 1, and code set 2.

KanjiShift-JIS

Each multibyte character is stored without translation; it remains in the client encoding.

Note: The regular or standard ASCII character set is stored as is. EBCDIC is stored as ASCII.

4-14

Teradata RDBMS for UNIX SQL Reference

4

Teradata SQL Lexicon Object Name Comparisons

Object Name Comparisons

4

In comparing two names, the following rules apply:

Comparison Rules

4







A simple Latin lowercase letter is equivalent to its corresponding simple Latin uppercase letter. For example, ‘a’ is equivalent to ‘A’. Multibyte characters that have the same logical presentation but have different physical encodings under different character sets do not compare as equivalent. Two names compare as identical when their internal hexadecimal representations are the same, even if their logical meanings are different under the originating character sets.

Note that identical characters on keyboards connected to different clients are not necessarily identical in their internal encoding on the Teradata RDBMS. The Teradata RDBMS could interpret two logically identical names as different names if the character sets under which they were created are not the same. For example, the following strings illustrate the internal representation of two names, both of which were defined with the same logical multibyte characters. However, the first name was created under KanjiEBCDIC, and the second name was created under KanjiShift-JIS. KanjiEBCDIC: KanjiShift-JIS:

Caution:

0E 42E3 42C1 42C2 42F1 0F 51 52 8273 8260 8261 8250 D8 D9

It is the user’s responsibility to avoid semantically duplicate object names in situations where duplicate object names would not normally be allowed. Also, two different character sets might have the same internal encoding for two logically different multibyte characters. Thus, two names might compare as identical even if their logical meanings are different.

Using the Internal Hexadecimal Representation of a Name

4

The Teradata RDBMS knows an object name by its internal hexadecimal representation, and this is how a name is stored in the various system tables of the Data Dictionary. As discussed in the previous passages, the encoding of a name’s internal representation depends on the components of the name string (single byte characters and/or multibyte characters, with or without Shift Out/Shift In (SO/SI) characters) and the character set in effect when the name was created.

Teradata RDBMS for UNIX SQL Reference

4-15

Teradata SQL Lexicon Object Name Comparisons

Assume that a user under one character set needs to reference a name created by a user under a different character set. If the current user attempts to reference the name with the actual characters (that is, by typing the ideographs or by selecting non-specific entries from a dictionary table), the access could fail or the returned name could be meaningless. For example, assume that User1 invokes a session under KanjiEBCDIC and creates Tname with multibyte characters. If User2 invokes a session under KanjiEUC and issues the statement SELECT Tname FROM DBC.Tables, User2 will receive the KanjiEBCDIC characters in KanjiEUC presentation, which probably would not make sense. You can avoid this condition in the following ways. ●

You can create names using only simple single byte Latin letters (A...Z, a...z) digits, and the dollar sign, pound sign, and underscore symbols. Because these characters always translate to the same internal representation, they display exactly the same presentation to any session, regardless of the client or the character set.



You can always reference a name by its internal representation, using the following form: 'hexadecimal_digit(s)'

XN HH01A099

where: ’hexadecimal_digits’ is the quoted hexadecimal string of the Teradata RDBMS internal encoding. The key letters XN specify that the string is a hexadecimal name.

Example

4

The following table name, which contains mixed single byte characters and multibyte characters, was created under a KanjiEBCDIC character set: KAN

The client encoding in which this name was received is as follows: 0E 42E3 42C1 42C2 42F1 0F D2 C1 D5 < T

A

B

1 > K A N

The single byte characters (the letters K, A, and N, and the SO/SI characters) were translated into internal JIS-x0201 encoding. The multibyte characters were not translated and remained in the host encoding.

4-16

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Object Name Comparisons

The resulting internal string by which the name was stored is as follows: 0E 42E3 42C1 42C2 42F1 0F 4B 41 4E < T A B 1 > K A N

To access this table from a KanjiShift-JIS or KanjiEUC character set, you could use the following Teradata SQL statement: SELECT * FROM ‘0E42E342C142C242F10F4B414E’XN;

The response would be all rows from table KAN.

Teradata RDBMS for UNIX SQL Reference

4-17

Teradata SQL Lexicon Finding the Internal Hexadecimal Representation of a Name

Finding the Internal Hexadecimal Representation of a Name Introduction

Example 1

4

4

The CHAR2HEXINT function converts a character string to its internal hexadecimal representation. You can use this function to find the internal representation of any Teradata RDBMS name. For example, to find the internal representation of all Teradata RDBMS table names, issue the following Teradata SQL statement: SELECT CHAR2HEXINT(T.TableName) (TITLE ‘Internal Hex Representation of TableName’) , T.TableName (TITLE ‘TableName’) FROM DBC.Tables T WHERE T.TableKind = ‘T’ ORDER BY T.TableName;

This statement selects from the DBC.Tables view all rows where the value of the TableKind field is T. For each row selected, it returns both the internal hexadecimal representation and the character format of the value in the TableName field. The rows are sorted alphabetically. An example of a portion of the output from this statement is shown below. In this example, the first name (double byte-A) was created using the KanjiEBCDIC character set. Internal Hex Representation of TableName TableName -----------------------------------------------------------------------0E42C10F2020202020202020202020202020202020202020202020202020 416363657373526967687473202020202020202020202020202020202020 AccessRights 4163634C6F6752756C6554626C2020202020202020202020202020202020 AccLogRuleTb 4163634C6F6754626C202020202020202020202020202020202020202020 AccLogTbl 4163636F756E747320202020202020202020202020202020202020202020 Accounts 416363746720202020202020202020202020202020202020202020202020 Acctg 416C6C202020202020202020202020202020202020202020202020202020 All 4368616E676564526F774A6F75726E616C20202020202020202020202020 ChangedRowJo 636865636B5F7065726D2020202020202020202020202020202020202020 check_perm 436F70496E666F54626C2020202020202020202020202020202020202020 CopInfoTbl

(Note that the first name, , cannot be interpreted. To obtain a printable version of a name, log onto a session under the same character set under which the name was created.)

Example 2

4

You can use the same syntax to obtain the internal hexadecimal representations of all views or all macros. To do this, modify the WHERE condition to TableKind=’V’ for views and to TableKind=’M’ for macros. To obtain the internal hexadecimal representation of all database names, you can issue the following statement: SELECT

4-18

Teradata RDBMS for UNIX SQL Reference

4

Teradata SQL Lexicon Finding the Internal Hexadecimal Representation of a Name CHAR2HEXINT(D.DatabaseName) (TITLE ‘Internal Hex Representation of DatabaseName’) , D.DatabaseName (TITLE ‘DatabaseName’) FROM DBC.Databases D ORDER BY D.DatabaseName;

This statement selects every DatabaseName from DBC.Databases. For each DatabaseName, it returns the internal hexadecimal representation and the name in character format, sorted by DatabaseName. An example of the output from this statement is as follows: Internal Hex Representation of DatabaseName DatabaseName -------------------------------------------------------------------416C6C202020202020202020202020202020202020202020202020202020 All 434F4E534F4C452020202020202020202020202020202020202020202020 CONSOLE 437261736864756D70732020202020202020202020202020202020202020 Crashdumps 444243202020202020202020202020202020202020202020202020202020 DBC 44656661756C742020202020202020202020202020202020202020202020 Default 5055424C4943202020202020202020202020202020202020202020202020 PUBLIC 53797341646D696E20202020202020202020202020202020202020202020 SysAdmin 53797374656D466520202020202020202020202020202020202020202020 SystemFe

Example 3

4

Note that these statements return the padded hexadecimal name; the value 0x20 represents a space character in the internal representation. You can use the TRIM function to obtain the hexadecimal values without the trailing spaces, as follows: SELECT CHAR2HEXINT(TRIM(T.TableName)) (TITLE ‘Internal Hex Representation of TableName’) , T.TableName (TITLE ‘TableName’) FROM DBC.Tables T WHERE T.TableKind = ‘T’ ORDER BY T.TableName;

Teradata RDBMS for UNIX SQL Reference

4-19

Teradata SQL Lexicon Specifying Names in a Logon String

Specifying Names in a Logon String The Teradata RDBMS logon string consists of the following: tdpid/username ,password

,accountname

HH01A079

The username, password, and accountname parameters may contain mixed single byte characters/multibyte characters, as determined by the current character set. Note: The Teradata RDBMS does not support the hexadecimal representation of a username, a password, or an accountid in a logon string. For example, if you attempt to log on as user DBC by entering ‘444243’XN, the logon will be unsuccessful and an error message will be generated. Note: The password format options allows the site administrator to change the minimum and maximum number of characters allowed in the password string, and control the use of digits and special characters. For allowed password strings refer to the previous section “Object Names ( (Japanese Character) Rules. The password string rules are identical to those for naming objects. When creating passwords, additional restrictions apply under each type of character set. The password formatting feature does not apply to multibyte character sets. Charts of supported Kanji character sets, the Teradata RDBMS internal JIS encodings, the valid characters ranges for Kanji object names and data, and the invalid character ranges for Kanji data are given in Appendix H, “Japanese Character Sets” and Appendix I, “Non-valid Japanese Character Code.”.

4-20

Teradata RDBMS for UNIX SQL Reference

4

Teradata SQL Lexicon Keywords

Keywords Introduction

4

4

Keywords are words that are reserved for use in SQL statements; they cannot be used as object names. The first keyword in a SQL statement is usually a verb; for example, in the INSERT statement, the first keyword is INSERT. Other keywords appear throughout a statement as modifiers (for example, DISTINCT, PERMANENT), or as words that introduce clauses (for example, IN, AS, AND, TO, WHERE). In this manual, keywords are shown entirely in uppercase letters. Although SQL does not discriminate between uppercase and lowercase letters in a keyword. For example, the following SELECT statements are considered to be identical: Select Salary from Employee where EmpNo = 10005; SELECT Salary FROM Employee WHERE EmpNo = 10005; select Salary FRom Employee WherE EmpNo = 10005;

Refer to Appendix A for a list of Teradata SQL keywords. Note: Double byte Roman characters may not be used in keywords. Note: Entry-level ANSI requires that all keywords be expressed as uppercase. If the SQL flagger is turned on, then lowercase keywords are flagged.

Keywords Are Reserved Words

4

Note that you cannot use keywords to name database objects. Because new keywords are frequently added to new releases of the Teradata RDBMS, you may experience a problem with database object names that were valid in prior releases but which become nonvalid in a new release. The workaround for this is to do one of the following things: ● ●

Put the newly nonvalid name in double quotes. Rename the object

In either case you must change your applications.

Teradata RDBMS for UNIX SQL Reference

4-21

Teradata SQL Lexicon Delimiters

Delimiters Introduction

4

4

Delimiters are special characters whose meaning depends on context. Teradata SQL delimiters are: ()

,

:

.

;





The function of each delimiter is described in Table 4-4.

Example

4

In the following statement, the period separates the database name (Examp and Personnel) from the table name (Profile and Employee), and, where reference is qualified to avoid ambiguity, it separates the table name (Profile, Employee) from the column name (DeptNo). UPDATE Examp.Profile SET FinGrad = ’A’ WHERE Name = ’Phan A’ ; SELECT EdLev, FinGrad,JobTitle, YrsExp FROM Examp.Profile, Personnel.Employee WHERE Profile.DeptNo = Employee.DeptNo ;

The first semicolon separates the UPDATE statement from the SELECT statement. The second semicolon terminates the entire multistatement request. Note: The semicolon is not a delimiter in ANSI SQL. It is used in Teradata SQL only to separate multiple statements, either in a request or macro definition, or to terminate a request for requests submitted through BTEQ.

4-22

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Delimiters Table 4-4 Delimiters in Teradata SQL

Delimiter

Function

() (parentheses)

Group expressions and define the limits of various phrases.

, (comma )

Separates items in a list, or as a date separator.

: (colon)

Prefixes a referenced parameter or client system variable; also used as a date separator.

. (period)

Separates a database name from a table name, or a table name from a column name. In numeric constants, the period is the decimal point. The period is also used as a date separator.

; (semicolon)

Separates statements in a request. In BTEQ, a semicolon at the end of an input line, terminates a request.

’ (apostrophe)

Defines the boundaries of character string constants; also used as a date separator.

“ (quote mark)

Defines the boundaries of nonstandard names.

@ (at sign)

Obsolete; replaced by : (colon)

/

Slash; used as a date separator.

B

Blank; used as a date separator.

-

Dash; used as a date separator.

Teradata RDBMS for UNIX SQL Reference

4-23

Teradata SQL Lexicon Constants

Constants Introduction

4

A constant is a literal string or value embedded in a Teradata SQL statement. A constant may be of the type date, character, numeric, or string. On a Japanese character supported site, sessions using the KanjiEBCDIC character set also can express a graphic string constant. Other character sets can express a graphic literal in hexadecimal.

4-24

Teradata RDBMS for UNIX SQL Reference

4

Teradata SQL Lexicon Numeric Constants

Numeric Constants Introduction

4

Numeric constants may have the following forms: 4

● ● ●

Integer, Smallint, and Byteint Decimal (Numeric) Floating point (Real, Double Precision)

Note: The Numeric constant has been implemented to function like the Decimal constant. Real and Double Precision have been implemented to function like the Floating point constant. The following are examples of valid numeric constants: Byteint

Smallint

Integer

Decimal

Floating Point

127

32767

32768

0.0

1E1

-36

-12000

-60400

-235.

1.4E6

-128

-32768

2147483647

2147483650

18E-3

The difference between Byteint, Smallint, and Integer is the range of values allowed and the internal storage size. A Byteint occupies 1 byte, Smallint 2 bytes, and Integer 4 bytes. Valid ranges for these numeric constants are as follows: Numeric Constant

Low

High

BYTEINT

-128

127

SMALLINT

-32768

32767

INTEGER

-2147483648

2147483647

Depending on the kind of operation performed or the type of column in which a value is to be stored, SQL may convert literal constants from one numeric data type to another, or convert character strings that represent numeric values to a numeric data type.

Converting Numeric Constants

4

If a constant is outside the range for a required type, an error is reported during conversion. An error also is reported if a character string that is assigned to a numeric field does not represent a valid numeric value.

Teradata RDBMS for UNIX SQL Reference

4-25

Teradata SQL Lexicon Numeric Constants

Converting Character Strings to Numeric Constants

4

A character string may contain a value that is intended to be a numeric value (for example, ‘15’). In such cases, the Teradata RDBMS attempts to convert the string to a numeric value whenever a numeric value is required in the context. Note that this may result in repeated conversions during data access. Thus, when a character field is compared with a numeric field, the character field is converted to numeric. If a numeric field is defined and stored as a character field and a select operation requires a full table scan in comparing the character field with a numeric constant, the character field is converted to numeric in every row of the table.

Non-Valid Numeric Constants: Examples

The following are examples of invalid numeric constants: 4

1234567890123456789012 $20,000.00 -38.7E2945

The first constant is invalid because it contains more than 18 digits. The second is invalid because it contains a dollar sign and a comma. The third is invalid because it contains 4 digits following the “E”.

4-26

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Character Constants

Character Constants

4

Character constants consist of 0 to 255 characters, delimited by a matching pair of apostrophes. A zero-length character constant is represented by two consecutive apostrophes (’’). Note: Character constants are treated as VARCHAR(n) fields, where n is the length of the constant. As a VARCHAR field, trailing blanks can be trimmed. The following are examples of valid character constants: ’Los Angeles’ ’’

To enter an apostrophe in a character constant, double the embedded apostrophe. For example: ’He said ’’yes’’ to her question’

In a Japanese character supported site, all character data is assumed to be mixed single byte characters/multibyte characters. The maximum length for an single byte character/multibyte character string constant remains 255 bytes. Note: A literal character string is defined as NOT CASESPECIFIC or CASESPECIFIC, depending on the mode (Teradata or ANSI) of the session parsing the string for execution. In ANSI mode, strings are CASESPECIFIC. In Teradata mode, strings are NOT CASESPECIFIC. The CASESPECIFIC attribute of character strings, determines the rules for string comparisons.

Teradata RDBMS for UNIX SQL Reference

4-27

Teradata SQL Lexicon Hexadecimal Constants

Hexadecimal Constants

4

Hexadecimal string constants can consist of 0 to 255 “characters” in hexadecimal form. Hexadecimal string constants are not ANSI. Character and byte strings are represented by an even number of hexadecimal digits. An integer is represented by an odd or even number of hexadecimal digits. A hexadecimal digit consists of a “character” in the following set: 0 1 2 3 4 5 6 7 8 9 A B C D E F

The digits of a hexadecimal string must be enclosed in single quotes, and may have one of the following forms: ●

Teradata internal form:

For characters, bytes, and integers, use: "hexadecimal digits(s)'

X C V F

Character Options

B V F

Byte Options

I 1 2 4

Integer Options HH01A089

Note: Hexadecimal character constants inserted, are not verified for validity. Note: An integer can be submitted only in this form, and may be represented by an odd or even number of hexadecimal digits. ●

External client form:

: X

'hexadecimal digits' HH01A081

This form is taken as a client system-dependent input character string, and is mapped from the hexadecimal representation of the client system character set to the internal ASCII hexadecimal representation.

4-28

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Hexadecimal Constants

Note that the interpretation of this form is affected by the client system-dependency feature. That is, a string in this form is assumed to be EBCDIC when submitted from a mainframe, but is assumed to be ASCII when submitted from another type of client system, or from the Teradata RDBMS console. This is true even when the session character set normally allows multibyte characters.

Internal ASCII Forms

4

Table 4-5 Hexadecimal Values Internal ASCII

Character or Byte Constants

Valid ways to enter hexadecimal values in the internal ASCII form are listed in Table 4-5. Data Type

4

Format

Example

VARCHAR

’hexadecimal digits’XC or XCV

’41’XC ’41’XCV

CHAR[(n)] VARBYTE

’hexadecimal digits’XCF ’hexadecimal digits’XB or XBV

’41’XCF ’55’XB ’55’XBV

BYTE[(n)]

’hexadecimal digits’XB or XBF

’55’XBF

INTEGER

’hexadecimal digits’X or XI or XI4

’5’X ’5’XI ’5’XI4

SMALLINT

’hexadecimal digits’XI2

’5’XI2

BYTEINT

’hexadecimal digits’XI1

’5’XI1

The ASCII hexadecimal string is packed into bytes starting on the left. If the number of digits is odd, an implied 0 digit is added to the right. For example: ’C1C’XC = ’C1C0’XC

INTEGER, BYTEINT, and SMALLINT Constants

The ASCII hexadecimal string is right-justified in the constant. For example, the value 1000 may be expressed as: 4

’3e8’X

or

’0003e8’X

or

’000003e8’X

Teradata RDBMS for UNIX SQL Reference

4-29

Teradata SQL Lexicon Hexadecimal Constants

Constant Created Using XC

4

A constant created using the XC form should be composed of values that are correct for the ASCII character set. The XC form is useful for inserting character strings such as TAB or BACKSPACE, which cannot generally be entered directly on the terminal keyboard. For example, if data is represented on the client system in EBCDIC and a user’s terminal keyboard does not include the brace characters {}, the data “{12}” could be represented in ASCII hexadecimal as: ’7B31327D’XC

where 7B and 7D represent { and }, respectively.

Constants Using XB

4

XB is the only form for entering a byte string in internal ASCII from the keyboard. For example, suppose the column CodeVal has been defined as BYTE(4). To insert the characters “xy” as an ASCII hexadecimal string, use the form: ... (CodeVal) VALUES (’7879’XB) ;

To select those rows from CodeVal, specify the conditional as: ... WHERE CodeVal = ’78790000’XB ;

Byte String in EBCDIC Hexadecimal

To enter a byte string in EBCDIC hexadecimal from a mainframe, use the X’hexadecimal digits’ form. 4

For example: ... (CodeVal) VALUES (X’A7A8’) ; ... WHERE CodeVal = X’A7A80000’ ;

Note: Interpretation of the external client form is dependent on the client system. If this form is submitted from an ASCII client system or the Teradata RDBMS console, the string is taken as internal ASCII hexadecimal. For example, an input of X’60’ from an ASCII system dumped to an EBCDIC client system will not be 60H. On a Japanese character supported site, the hexadecimal representation can be used to define a graphic string constant under any character set. (Note that if the character set is KanjiEBCDIC, the Shift-Out and Shift-In characters must be omitted.) A graphic hexadecimal constant can be only to a column defined as a GRAPHIC or VARGRAPHIC data type.

4-30

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Hexadecimal Constants

Under all character sets, use the following form to define a graphic hexadecimal constant: 'hexadecimal_digits' XG V F

HH01A078

where: Syntax Element ...

Description

’hexadecimal_digits’

The quoted hexadecimal representation (in the client encoding) of the multibyte character string. Note that the string must equate to an even multiple of bytes Each quote mark ( ’ ) is a single byte character.

XG

The letters XG specify a hexadecimal graphic constant.

FV

The letters XG can be appended by the letter F (for a fixed-length string) or V (for a variable-length string).

Note: Teradata RDBMS does not check that a hexadecimal graphic string represents valid graphic data. The possible forms for each graphic data type are illustrated in Table 4-6. Table 4-6 Forms for Hexadecimal Graphic Constants

Data Type

Form

Example

GRAPHIC

’hexadecimal_digits’XG

’82728273’XG

GRAPHIC

’hexadecimal_digits’XGF

’82728273’XGF

VARGRAPHIC

’hexadecimal_digits’XGV

’82728273’XGV

Teradata RDBMS for UNIX SQL Reference

4-31

Teradata SQL Lexicon Graphic Constants

Graphic Constants

4

On a Japanese character site, graphic string constants can be used if a KanjiEBCDIC character set is in effect for the session. (For other character sets, you can use the hexadecimal representation of the graphic string.) Note: Graphic constants are flagged as non ANSI, when the SQL flagger is enabled. A graphic string constant has the following form: G

'< ABC >' HH01A090

where: Syntax Element...

Description

G

The letter G specifies that the string is graphic data.

‘‘

Each ’ (quote mark) is a single byte character.

ABC is the quoted string of valid KanjiEBCDIC multibyte characters, surrounded by Shift-Out and Shift-In.

A graphic constant has the following conventions: ●





The target column must be a GRAPHIC or VARGRAPHIC data type. The constant must be enclosed within Shift-Out/Shift-In characters (as normal KanjiEBCDIC encoding requires). The maximum length is 127 logical (double byte) characters. Note that the Shift-Out/Shift-In characters are not included in the byte count and are not stored as part of the graphic constant.





4-32

The contents must be valid graphic data (multibyte characters that are printable under the KanjiEBCDIC character set in effect for the current session). The internal representation must be an even number of bytes.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Lexicon Operators

Operators

4

Operators are used to express logical and arithmetic operations. Operators of the same precedence are evaluated from left to right. Parentheses can be used to control the order of precedence. When parentheses are present, operations are performed from the innermost set of parentheses outward. SQL operations, and the order in which they are performed when no parentheses are present, are listed in Table 4-7. The following definitions apply to Table 4-3:

Term

Definition

numeric

Any constant, data reference, or expression having a numeric value.

string

Any character string or string expression.

logical

A Boolean expression (resolves to true or false).

value

Any numeric, character, or byte data item.

set

A collection of values returned by a subquery, or a list of values separated by commas and enclosed by parentheses.

charvalue

A character data item.

Teradata RDBMS for UNIX SQL Reference

4-33

Teradata SQL Lexicon Operators Table 4-7 Operator Precedence

Precedence

Operation

highest

numeric

+ numeric (unary plus) - numeric (unary minus)

intermediate

numeric

numeric ** numeric

numeric

numeric * numeric (multiplication) numeric / numeric (division) numeric MOD numeric (modulo operator)

numeric

numeric + numeric (addition) numeric - numeric (subtraction)

string

concatenation operator

logical

value EQ value value NE value value GT value value LE value value LT value value GE value value IN set value NOT IN set value BETWEEN value AND value charvalue LIKE charvalue

logical

NOT logical

logical

logical AND logical

logical

logical OR logical

lowest

4-34

Result Type

Teradata RDBMS for UNIX SQL Reference

(exponentiation)

Teradata SQL Lexicon Lexical Separators

Lexical Separators Introduction

4

A lexical separator is a character string that can be placed between words, constants, and delimiters without changing the meaning of a statement. Valid lexical separators are: ● ●



Comments Example

4

4

Comments Blanks (several blanks are treated as a single blank, except in a string constant) RETURN characters (hexadecimal 0D)

The ANSI form of comment is delimited by the characters -- at the beginning of the comment and the end-of-line.

4

SELECT EmpNo, Name FROM Payroll_Test ORDER BY Name -- Alphabetic order ;

Alternatively, a comment is a text string of unrestricted length that is delimited by the characters /* and */. It may begin anywhere on an input line, and may span one or more lines. Note: This form of comment will be flagged as non-ANSI. A comment has no effect on the processing of the statement. The following CREATE TABLE statement illustrates the use of a comment: CREATE TABLE Payroll_Test /* This is a test table set up to process actual payroll data on a test basis. The data generated from this table will be compared with the existing payroll system data for 2 months as a parallel test. */ (EmpNo INTEGER NOT NULL FORMAT ’ZZZZ9’, Name VARCHAR(12) NOT NULL, DeptNo INTEGER FORMAT ’ZZZZ9’, . .

Blanks Return Character

4

One or more blanks are treated as a single blank, unless the blanks are contained in a character string. The value of the return character is hexadecimal 0D.

4

Teradata RDBMS for UNIX SQL Reference

4-35

Teradata SQL Lexicon Statement Separator

Statement Separator Introduction

4

The semicolon is a statement separator. Each statement of a multistatement request must be separated from the subsequent statement with a semicolon. The following multistatement request illustrates the use of the semicolon as a statement separator. SHOW TABLE Payroll_Test ; INSERT INTO Payroll_Test (EmpNo, Name, DeptNo) VALUES (’10044’, ’Jones M’, ’300’) ; INSERT INTO ...

When statements are entered through BTEQ a semicolon at the end of an input line terminates the request, unless that line is a comment line, beginning with two dashes. Everything to the right of the - - is a comment. In this case, the semicolon should be on the following line. Note: The semicolon as a statement separator is non-ANSI.

Null Statement

4

A semicolon that precedes the first (or only) statement of a request is taken as a null statement. A null statement may or may not be preceded by a comment. Here are two examples. The semicolon in the following request is a null statement: /* This example shows a comment followed by a semicolon used as a null statement */ ; UPDATE Pay_Test SET ...

The first semicolon in the following request is a null statement. The second semicolon is taken as statement separator: /*

This example shows a semicolon used as a null statement and as a statement separator */ ; UPDATE Payroll_Test SET Name = ’Wedgewood A’ WHERE Name = ’Wedgewood A’ ; SELECT ... -- This example shows the use of an ANSI component -- used as a null statement and statement separator ;

4-36

Teradata RDBMS for UNIX SQL Reference

4

Teradata SQL Lexicon Request Terminator

Request Terminator

4

A request is considered complete when either the “End of Text” character or the request terminator character is detected. A semicolon is a request terminator when it is the last non-blank character on an input line in BTEQ, unless that line is a comment line beginning with two dashes. In this case, the semi-colon request terminator should be on the following line, after the comment line. For example, on the following input line: SELECT * FROM Employee ;

the semicolon terminates the single-statement request “SELECT * FROM Employee”. In BTEQ, semicolons are used for terminating multistatement requests. A request terminator is mandatory only for requests that appear in the body of a macro, or that are entered via the BTEQ interface, or other interfaces that require it. Note: The semicolon as a request terminator is non-ANSI. The following statement illustrates the use of a request terminator in the body of a macro: CREATE MACRO Test_Pay (number (INTEGER), name (VARCHAR(12)), dept (INTEGER) AS ( INSERT INTO Payroll_Test (EmpNo, Name, DeptNo) VALUES (:number, :name, :dept) ; UPDATE DeptCount SET EmpCount = EmpCount + 1 ; SELECT * FROM DeptCount ; )

When entered through BTEQ, the entire CREATE MACRO statement must be terminated. For example: CREATE MACRO Test_Pay (number (INTEGER), name (VARCHAR(12)), dept (INTEGER) AS (INSERT INTO Payroll_Test (EmpNo, Name, DeptNo) VALUES (:number, :name, :dept) ; UPDATE DeptCount SET EmpCount = EmpCount + 1 ; SELECT * FROM DeptCount ; ) ;

Teradata RDBMS for UNIX SQL Reference

4-37

Teradata SQL Lexicon Request Terminator

4-38

Teradata RDBMS for UNIX SQL Reference

Data Definition

Chapter 5

Data Definition

Teradata RDBMS for UNIX SQL Reference

Data Description Phrases

TeradataR DBMS for UNIX SQL Reference

Data Definition About This Chapter 5

About This Chapter

5

This chapter describes the following topics:: ●





● ●

● ● ● ● ● ● ● ● ● ● ●

Data definition? ● Data definition form ● Using data definitions ● ANSI and Teradata mode Numeric data types ● Date ● Decimal and NUMERIC ● Byteint ● Integer ● Float, Double Precision, Real Character data types ● Char, Varchar, and Long Varchar ● Specifying case ● Character data on a Japanese character site Byte/Varbyte data types: Kanji graphic data types ● Graphic ● Vargraphic CHECK constraint phrase FORMAT phrase NAMED phrase TITLE phrase Default control phrase COMPRESS phrase Data type conversion Implicit type conversion rules Using CAST in data type conversions Attribute functions Hash-related expressions

Teradata RDBMS for UNIX SQL Reference

5-1

Data Definition What Is Data Definition?

What Is Data Definition? Data Definition Form

5

Data definition has the following form: 5

data type declaration data type attribute column storage attribute column constraint attribute HH01A014

Note: Data definition must be included when defining a column. Data definition can be omitted when modifying data. Each data definition has the following characteristics: Characteristic

Description

Data type declaration

See Table 5-1 for a summary.

Data type attributes (optional)

See Table 5-2, Table 5-3, and Table 5-4 for a summary of these, and to later in this chapter, “Attribute Functions”.

Column storage attributes (optional)

See the ALTER TABLE and CREATE TABLE statements.

Column constraint attributes (optional)

See the ALTER TABLE and CREATE TABLE statements. “Column and Table Constraints”, are also described later in this chapter.

Data type attributes control the internal representation of stored data and determine how data is presented for a column or an expression result.

5-2

Teradata RDBMS for UNIX SQL Reference

Data Definition What Is Data Definition?

Using Data Definition

5

Data definition can occur in any order In a statement. Data definitions are also referred to as data description phrases. When used to define the attributes of a data value, also referred to as data type attributes, the data definition immediately follows a column name. In the following statement, the following columns are assigned attributes using data definitions: ● ● ● ● ● ● ● ● ● ● ● ●

EmpNo Name DeptNo JobTitle Salary YrsExp DOB Sex Race MStat EdLev HCap

CREATE TABLE Employee (EmpNo PRIMARY KEY SMALLINT FORMAT ’9(5)’ CHECK(EmpNo BETWEEN 1000 AND 32001), Name VARCHAR(12)NOT CASESPECIFIC NOT NULL, DeptNo SMALLINT FORMAT ’999’ CHECK (DeptNo BETWEEN 100 AND 900), JobTitle VARCHAR(12)NOT CASESPECIFIC, Salary DECIMAL(8,2) FORMAT ’ZZZ,ZZ9.99’ CHECK (Salary BETWEEN 1.00 AND 999000.00), YrsExp BYTEINT FORMAT ’Z9’ CHECK (YrsExp BETWEEN -99 AND 99), DOB DATE FORMAT ’MMMbDDbYYYY’ NOT NULL, Sex CHAR UPPERCASE NOT NULL, Race CHAR UPPERCASE, MStat CHAR UPPERCASE, EdLev BYTEINT FORMAT ’Z9’ CHECK (EdLev BETWEEN 0 AND 2) NOT NULL, HCap BYTEINT FORMAT ’Z9’ CHECK (HCap BETWEEN -99 AND 99) INDEX (Name) ;

Teradata RDBMS for UNIX SQL Reference

5-3

Data Definition Data Type Attributes: ANSI Mode

Data Type Attributes: ANSI Mode Introduction

5

5

When used to modify the attributes of returned value, the data definition immediately follows the column being modified. This usage is non-ANSI. For example, in the following SELECT statement, a TITLE phrase overrides the default heading (the column name) for EmpNo, and a FORMAT phrase modifies the display format defined for Salary data in the CREATE TABLE statement. SELECT EmpNo (TITLE ’Emp#’), Salary (FORMAT ’$$$,$$9.99’) FROM Employee;

The expression “data type attributes” is non-ANSI. Also attributes such as TITLE and FORMAT, are non-ANSI. For applications that need to conform to ANSI and to modify attributes of a value, the CAST function should be used. In this case, the modification is restricted to converting to an ANSI defined type. CAST is described later in detail.

Example

ANSI Versus Teradata Mode

To select Name as a fixed length field and Salary as an integer value: 5

SELECT CAST (Name AS CHAR(12)), CAST (Salary AS INTEGER) FROM Employee ;

5

A move to be fully ANSI compliant with regards to Case Sensitivity, would have caused large regression problems. To facilitate the transition to ANSI SQL compatibility, two modes of operation, concerning character comparison rules, Teradata mode and ANSI mode, are provided. Installations may choose Teradata or ANSI mode as a system default, and sessions can individually be set to override the system default. Refer also to Chapter 9, “Advanced SQL.”, “Transaction Semantics: Operating in ANSI or Teradata Mode”.

5-4

Teradata RDBMS for UNIX SQL Reference

Data Definition Data Type Attributes: ANSI Mode

Caution:

Because some instances of comparison attributes occur with data definitions, and others with text in queries (or text from dictionary tables scanned as queries are executed), use of the two modes can be rather involved and you should exercise caution when doing so. You should use current Teradata RDBMS SQL syntax which is the most ANSI compliant for all newly written applications. The current release of Teradata SQL still supports old applications, written in prior versions of Teradata SQL.

Teradata RDBMS for UNIX SQL Reference

5-5

Data Definition Data Types

Data Types

5

The data type phrase does the following: ● ●

Specifies how data is presented to the user. Determines how data is stored on the Teradata RDBMS.

A data type phrase must be specified for each column when a table is created. No default data type is provided. A data type phrase can also be used to specify data conversions in expressions. The data type is defined as being either numeric, character, or byte, by using the following data type phrases: Table 5-1 Data Type Declaration

NAb

NEc

BYTEINT

X

X

DATEd

X

X

Data Type

Aa

Numeric INTEGER

X

SMALLINT

X

REAL

X

DOUBLE PRECISION

X

FLOAT [n]

X

DECIMAL [(n[,m])]

X

NUMERIC [(n[,m])]

X

Character CHAR[(n)]

X

VARCHAR(n)

X

X

CHAR VARYING(n)

X

X

LONG VARCHAR

X

X

BYTE[(n)]

X

X

VARBYTE(n)

X

X

GRAPHIC[(n)]

X

X

VARGRAPHIC(n)

X

X

LONG VARGRAPHIC

X

X

Bytee

Graphic

a. A = Supported by ANSI SQL

5-6

Teradata RDBMS for UNIX SQL Reference

Data Definition Data Types b. NA=Not Supported by ANSI SQL; Teradata SQL extension to ANSI SQL c. NE= Not Entry Level ANSI SQL d. DATE is (1) not Entry Level ANSI, and (2) not compatible with Intermediate or Full ANSI e. All byte and graphic data types are flagged as non-ANSI SQL when the SQL flagger is enabled.

For further information on the different level of ANSI compliance in this release, refer to Appendix B, “Teradata SQL Versus ANSI SQL.”.

Client System Data Types

Data Type Attributes Table 5-2 Data Type Attributes

5

In addition to SQL data types, the Teradata RDBMS supports numeric, character, and byte data types as used on various supported client systems. Refer to the list of client related manuals in the Preface of this manual. Table 5-2 lists the main Teradata data type attributes.

5

Data Type Attribute

NOT NULL

ANSI

Teradata Extension to ANSI

X

UPPERCASE

X

[NOT] CASESPECIFIC

X

FORMAT quotestring

X

TITLE quotestring

X

NAMED name

X

DEFAULT number

X

DEFAULT USER

X

DEFAULT DATE

X

DEFAULT TIME

X

DEFAULT NULL WITH DEFAULT

Teradata RDBMS for UNIX SQL Reference

X X

5-7

Data Definition Data Types

Column Storage Attributes

5

Column storage attributes are a Teradata RDBMS extension to ANSI SQL. Column storage attributes for the Teradata RDBMS are: ● ● ● ●

Column Constraint Attributes

Constraint names are intermediate ANSI SQL, and will be flagged as non entry level ANSI SQL when the SQL flagger is enabled. 5

Column constraint attributes for the Teradata RDBMS are: ● ● ● ● ●

5-8

COMPRESS COMPRESS NUL COMPRESS quotestring COMPRESS constant

CONSTRAINT CONSTRAINT UNIQUE CONSTRAINT PRIMARY KEY CONSTRAINT CHECK (boolean condition) CONSTRAINT REFERENCES tname col_name

Teradata RDBMS for UNIX SQL Reference

Data Definition GRAPHIC Conversion

GRAPHIC Conversion Introduction Character to Graphic Conversion

5

5

5

The GRAPHIC data type is not implicitly converted to any other data type for the purposes of string or logical expression operations. You cannot explicitly convert character data to graphic data. You can do this conversion implicitly by using the VARGRAPHIC function. Under a KanjiEBCDIC character set, users can request the graphic equivalent of a character string. This type of conversion is done using the VARGRAPHIC string function, as shown in the following example: SELECT VARGRAPHIC (’string_expr’);

Other Conversions

5

You can convert only graphic data explicitly, as shown in the following example: SELECT ‘42434244’xg (GRAPHIC);

Numeric fields are converted to the same representation prior to the conversion. Table 5-3 shows the data types resulting from the conversions. Table 5-3 Data Conversion Results

FROM \ TO

Numeric

Date

Character

Byte

Graphic

Numeric

numeric

date/ errora

character

error

error

Date

numeric

date

character

error

error

Character

numeric/ errorc

date/ errorb

character

error

error

Byte

error

error

error

byte

error

Graphic

error

error

error

error

graphic

a. If a numeric or character value is converted to date and the value does not represent a valid date, an error is reported. b. If a numeric or character value is converted to date and the value does not represent a valid date, an error is reported. c. Valid result only if the character string represents a numeric value.

Teradata RDBMS for UNIX SQL Reference

5-9

Data Definition GRAPHIC Conversion

You should store numbers as numeric data, not as character data. For example, a table is created with the following code: CREATE TABLE job AS (job_code CHAR(6)PRIMARY KEY ,description CHAR(70) ; )

Subsequently, the following query is made: SELECT job_code, description FROM job WHERE job_code = 1234;

The problem here is that ‘1234’, ‘ 1234’, ‘01234’, ‘001234’, ‘+1234’, and so on, are all valid character representations of the numeric literal value, and the system cannot tell which value to use for hashing. Therefore, the system must do a full table scan to convert all job_code values to their numeric equivalents so that it can do the comparisons. Refer to Appendix F, “Data Conversion,” for further details on data conversion.

5-10

Teradata RDBMS for UNIX SQL Reference

Data Definition Numeric Data Types

Numeric Data Types Introduction

5

The following date types represent numeric data: 5

● ● ● ● ● ● ● ● ●

DATE DECIMAL NUMERIC BYTEINT SMALLINT INTEGER REAL DOUBLE PRECISION FLOAT

Each of these types can be combined with a range constraint when the data type is defined or modified. The following table lists Teradata-ANSI synonyms for numeric data types. ANSI Name

Teradata Equivalent

NUMERIC

DECIMAL

REAL

FLOAT

DOUBLE PRECISION

Rounding DECIMAL or NUMERIC Data Types

5

If a value being inserted does not fit into a field defined as a DECIMAL or NUMERIC data type, the value is rounded. The Teradata RDBMS rounds using the first non-stored value. IF the value is . . .

THEN it is. . .

5

rounded up

Teradata RDBMS for UNIX SQL Reference

5-11

Data Definition Numeric Data Types

Additional considerations come into play when the value is exactly 5. IF the first non-stored value is exactly 5 and . . .

there are no trailing non-zero digits

there are trailing non-zero digits

Rounding FLOAT, REAL, DOUBLE PRECISION Data Types

5

THEN . . .

the last stored value is checked IF that value is . . .

THEN the non-stored value is rounded . . .

odd

up

even

down

rounding behaves as if the first non-stored value is greater than 5.

If a value being inserted does not fit into a field defined as a FLOAT, REAL, or DOUBLE PRECISION data type, the same rounding rule applies, but it is based on the value of the actual number that is stored internally. For example, if the number 0.995 is stored as 0.9949999, it is rounded down to 0.99; if it is stored as 0.9950001, it is rounded up to 1.0. Table 5-4 shows the results of inserting values into column NUMBER, which is defined as DECIMAL(3,2).

Table 5-4 Effects of Rounding

5-12

WHEN the value of INSERT is . . .

AND the data value in NUMBER is . . .

THEN the value is rounded . . .

.014

.01

Down

5

.024

.02

Down

5

Teradata RDBMS for UNIX SQL Reference

Because the non-stored value is . . .

Data Definition DATE Numeric Data Type

DATE Numeric Data Type Introduction

5

5

The DATE data type identifies a field as a date and simplifies handling and formatting of date variables. Sign

DATE

FF19A001

The DATE data type handles the following conversions: ● ● ●

month-to-month year-to-year leap year

Note: DATE is flagged as not ANSI when the SQL flagger is enabled. Teradata RDBMS DATE is not compatible with the DATE in Intermediate Level ANSI SQL.

DATE is Stored as an Integer

The Teradata RDBMS stores each DATE value as an integer, using the following formula: 5

(year - 1900) * 10000

+

(month * 100)

+ day

Allowable date values range from AD January 1, 1 to AD December 31, 3500. This integer value represents the date as YYMMDD (Y=year, M=month, D=day) for dates in the years 1900 to 1999. For example, December 31, 1985 would be stored as the integer 851231; July 4, 1776 is stored as -1239296, and March 30, 2041 is stored as 1410330. Whenever a date is specified as an integer, it is specified in this form.

Default DATE Display

The default display format for DATE is YY/MM/DD. 5

You can override he default display with a FORMAT phrase. Note that if you use a FORMAT phrase, the DATE values must be defined in the specified format for both insertion and retrieval. For example, the Employee table defines the DOB column as follows: DOB DATE FORMAT ’MMMbDDbYYYY’ NOT NULL,

Note: All newly written applications should use a date format such as ‘YYYY-MM-DD’ to avoid the so-called Year 2000 problem.

Teradata RDBMS for UNIX SQL Reference

5-13

Data Definition DATE Numeric Data Type

Inserting DATE

5

To insert a birthdate of October 17, 1948, the value should be defined as: INSERT INTO employee (name, empno, deptno, dob, sex, edlev) VALUES (’Orebo B’, 10005, 300, ’Nov 17 1957’, ’M’, 18) ;

Note: The form of the character string constant used in the INSERT is correct for the format defined for DOB, ‘MMM DD YYYY’.

Retrieving DATE

5

To retrieve the same date, define the WHERE condition should be defined as: SELECT Name FROM Employee WHERE DOB = ’Oct 17 1948’;

Arithmetic Operations on DATE

5

A field that is declared as DATE can be operated on using addition and subtraction. The MIN, MAX, AVERAGE, and COUNT aggregate operators can also be used with DATE values. You can perform arithmetic operations on DATE data. For example, a number of days can be added to or subtracted from a DATE field, or from the DATE built-in value. The result of such operations is a value that is also a DATE data type. The number of days between two dates can be calculated by subtracting one DATE value from another DATE value.The result is an integer. The Teradata RDBMS handles month-to-month, year-to-year, and leap year arithmetic automatically; it does not store invalid dates, such as 84/02/30. In the following column definition, DOB is assigned the DATE data type (a 4-digit year form is recommended): DOB DATE FORMAT ’YYYY/MM/DD’ NOT NULL,

The following operations can be performed on data defined as DATE: ● ● ● ● ●

5-14

Arithmetic (addition and subtraction) ADD_MONTHS function Comparison (=, , >, >=, is Shift-In, bold = double byte b. cs2 c. cs3 d. / bold-italic = single byte katakana

5-26

Teradata RDBMS for UNIX SQL Reference

Conversion Result

MNB MNa ss2B ss3c MNIabc

Data Definition CHAR, VARCHAR and LONGVARCHAR Character Data Types

CHAR, VARCHAR and LONGVARCHAR Character Data Types CHAR

5

5

The CHAR or CHARACTER data type represents a fixed-length character string. The specification of a CHAR data type is:

CHAR (n)

n = 1 to 32000 (number of bytes)

FF19A007

CHARACTER data is stored in extended ASCII for European character support, and extended JIS-x201 for Japanese character support. Character data is allocated in terms of bytes. Except for characters available with Japanese character support (e.g., Kanji), each character requires one byte of storage. Any conversion to or from the client system’s data type is done by the Teradata RDBMS. This data type supports national character sets. If a value for n is not specified, the default is CHAR(1). In the following column definitions, the column named Sex is assigned the CHAR data type with a length of one, and the column named Frgn_Lang is assigned the CHAR data type with a length of seven. Sex CHAR NOT NULL UPPERCASE, Frgn_Lang CHAR(7) NULL UPPERCASE,

The default display format of CHAR(n) is X(n). For example, X(5), where data ’HELLO’ displays as ’HELLO’.

VARCHAR(n)

5

The VARCHAR(n) data type represents a variable-length character string of length n. The maximum value for n is 32,000 characters. Synonyms for VARCHAR(n) are CHAR VARYING [(n)], and CHARACTER VARYING [(n)].

VARCHAR (n)

Two byte length

n = 1 to 32000 (maximum number of bytes) FF19A008

Teradata RDBMS for UNIX SQL Reference

5-27

Data Definition CHAR, VARCHAR and LONGVARCHAR Character Data Types

VARCHAR data is stored in extended ASCII for European character support, and extended JIS-x0201 for Japanese character support. Character data is allocated in terms of bytes. Except for characters available with Japanese character support (e.g., Kanji), each character requires one byte of storage. Note: VARCHAR(n) is flagged as not Entry Level ANSI, when the SQL flagger is enabled. The default display format for VARCHAR(n) is X(n). For example, X(6) displays data ‘GOOD’ as ‘GOOD ’ with two trailing blanks.

LONG VARCHAR

5

LONG VARCHAR is equivalent to VARCHAR(32000). LONG VARCHAR specifies the longest permissible variable length character string.

LONG VARCHAR Two byte length

Equivalent to VARCHAR(32000)

FF19A009

LONG VARCHAR data is stored in extended ASCII for European character support, and extended JIS-x0201 for Japanese character support. Character data is allocated in terms of bytes. Except for characters available. with Japanese character support (e.g., Kanji), each character requires one byte of storage. Note: LONG VARCHAR is flagged as non-ANSI when the SQL flagger is enabled. The column offset points to the actual location of the data. The default display format for LONG VARCHAR(n) is X(32000).

5-28

Teradata RDBMS for UNIX SQL Reference

Data Definition Specifying Case for Character Data

Specifying Case for Character Data Introduction

5

5

Users can operate in either ANSI mode or in Teradata mode. The current release’s changes concern the definition of data in columns of tables and the interpretation of character string literals parsed as queries are executed. If CASESPECIFIC or NOT CASESPECIFIC is omitted from the column definition, the option may be included in an SQL request. In character string comparisons, if either of the strings being compared is CASESPECIFIC, the comparison is CASESPECIFIC. Execution of Views, Macros, and CHECK constraints in tables may involve parsing character string literals at execution time, as well as character string literals in queries. Refer also to Chapter 7, “Queries: The SELECT Statement”, “ORDER BY Clause”, ‘CASESPECIFC Option’.

Teradata Mode

ANSI Mode

5

5

In Teradata mode, all character data defaults to NOT CASESPECIFIC. In either mode, this default can be overridden (although this is a Teradata extension). In ANSI mode, all character data defaults to CASESPECIFIC. Case blind comparisons may be done via the UPPER function, applied to any character string value that is not assured to contain no lower case latin letters. See also Chapter 6, “Logical Expressions - expr: Caseblind Comparisons”. NOT CASESPECIFIC is another way of doing case blind comparisons. UPPER is the preferred ANSI method. Note: The UPPER function is an ANSI defined function, and is not the same as declaring a value UPPERCASE, which is not ANSI. The syntax of the UPPER function is: UPPER

( character_string_expr) HH01A076

Note: Explicit declarations of CASESPECIFIC and NOT CASESPECIFIC are flagged as non-ANSI when the SQL flagger is enabled.

Teradata RDBMS for UNIX SQL Reference

5-29

Data Definition Specifying Case for Character Data

The following table summarizes case criteria and issues: Case Function Specified or Not

Usage Rules

CASESPECIFIC (CS)

Character data is stored in the case in which it was entered unless UPPERCASE is also specified. In sorts or compares, lowercase characters are not converted to uppercase, and are not equal to uppercase characters. ● If a column is defined as CASESPECIFIC, characters entered as ‘aaa’ are not equivalent to ‘AAA’ when used in a unique index. ● Applications which most fully conform to ANSI should define columns and run in ANSI mode and use the UPPER function described later, to force case blind comparisons. An example is shown below.

NOT CASESPECIFIC

In sorts or compares, lowercase characters are converted to uppercase; therefore, ‘aaa’ is equivalent to ‘AAA’. Note: Because the stored characters were not converted from lowercase to uppercase, data entered as ‘aaa’ is not equivalent to ‘AAA’ when used in a unique index.

Neither CS nor NOT CASESPECIFIC

The session mode (ANSI or Teradata) determines the attribute assigned. ● In ANSI mode, CASESPECIFIC is set. ● In Teradata mode, NOT CASESPECIFIC is set.

UPPERCASE not specified, NOT CASESPECIFIC set

AAA, aaa, AaA, and aAA are all equivalent as unique indexes.

UPPERCASE (UC)

Character data is stored in uppercase, regardless of the case in which it was entered (any lowercase characters are converted to uppercase). Because of the conversion, if a column is defined as UPPERCASE, characters entered as ‘aaa’ are equivalent to ‘AAA’ when used in a unique index.

Examples Example 1

5

The following examples illustrate the options for specifying case for character data: The Sex column is created using the UPPERCASE option:

5

Sex CHAR UPPERCASE

If Sex data is entered in lowercase thus: INSERT INTO Employee (Name, EmpNo, Sex . . . VALUES (’Smith’, 10021, ’f’, . . . SELECT Sex FROM Employee

it will be stored and returned in uppercase.

5-30

Teradata RDBMS for UNIX SQL Reference

Data Definition Specifying Case for Character Data

Example 2

5

The following query returns a result only if a case specific comparison of the literal ‘Leidner P’ finds a match. SELECT Name FROM Employee WHERE Name(CS) = ’Leidner P’ ;

The literal ‘Leidner P’ may default to CS or NOT CS, depending on the current mode, but since the type modifier of the comparison is CS, the comparison will be CS. To ensure a case blind comparison, whether the current session is in Teradata or ANSI mode, the query would be: SELECT Name FROM Employee WHERE Name (NOT CS) = ‘Leidner P’ (NOT CS) ;

or alternatively, using ANSI compatible query: SELECT Name FROM Employee WHERE UPPER (Name) = ‘LEIDNER P‘ ;

Example 3

5

If used with the concatenation (||) operator, UC must be enclosed in parentheses, and be placed immediately after the column name: SELECT (City_name (UC)) || ’, ’ || State, Population FROM World WHERE Country = ’USA’ ORDER BY Population ;

Example 4

5

CASESPECIFIC comparison and collation on mixed case data may produce unintended results. SELECT Last_Name FROM SalesReps ORDER BY Last_Name(CS) ;

may return one of the following, depending on the collation in effect for the session: EBCDIC bart fernandez hill Albert ACME ALBERT FARRAH Kimble

ASCII ACME ALBERT Albert FARRAH Kimble bart fernandez hill

MULTINATIONAL ACME Albert ALBERT bart FARRAH fernandez hill Kimble

Collation can be defined as an attribute of the user via the CREATE USER or MODIFY USER statement, and can be set at the session level via the SET SESSION COLLATION statement. Note: MULTINATIONAL collation is user defined, and can produce different results depending on how your system administrators have set up your system.

Teradata RDBMS for UNIX SQL Reference

5-31

Data Definition Defining Case Sensitivity for Table Columns

Defining Case Sensitivity for Table Columns Introduction

ANSI Mode

Teradata Mode

5

5

5

The default for character columns defined under previous releases has been NOT CASESPECIFIC, and columns were CASESPECIFIC only by explicit definition. All character data columns defined in ANSI mode will, by default, be defined as case specific. This means that the columnar values 'aa' and 'AA' are not treated as equals. An option of NOT CASESPECIFIC is implemented to allow columns to be case insensitive. If the SQL flagger option is on and the NOT CASESPECIFIC option is specified, it is flagged as an extension. All character data columns defined in Teradata mode are, by default, defined as NOT case specific. Character data columns for tables created under prior releases continue to have the data type attribute set when columns were defined. The UPPERCASE attribute is still supported, but flagged as nonANSI. Observe that since simple Latin letters have canonical representation in all Teradata RDBMS character sets, including Kanji, the effect of the uppercase function is the same for each character set.

5-32

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Case Sensitivity of Character String Literals

Case Sensitivity of Character String Literals

5

Character string literals may occur in the text of SQL queries, in the text defining views and macros, and the text of CHECK constraints. In all of these instances, execution of queries using these literals involves parsing the literals and assigning case sensitivity attributes at that time. IF the session is in this mode . . .

THEN all character string literals are . . .

ANSI

CASESPECIFIC

Teradata

NOT CASESPECIFIC

This means that for views with WHERE constraints and for CHECK, if character string literals are included, they compare differently when executed by users with sessions in ANSI and Teradata mode. You should modify existing views with WHERE constraints to explicitly control case sensitivity in comparisons. Refer to Chapter 8, “Teradata SQL Syntax Guide,” “ALTER TABLE” and “CREATE TABLE” for explanations on the use of CHECK. Also refer to Chapter 6, under “Comparison Operators” You should explicitly control case sensitivity in character string comparisons.

Teradata RDBMS for UNIX SQL Reference

5-33

Data Definition Character String Definitions in the USING Phrase

Character String Definitions in the USING Phrase In prior releases, a designation of CHAR or VARCHAR with no explicit CASESPECIFIC declaration, meant that for comparisons, the values processed were to be designated NOT CASESPECIFIC. IF the session is in this mode . . .

THEN the values default to this declaration . . .

ANSI

CASESPECIFIC

Teradata

NOT CASESPECIFIC

The explicit designation NOT CASESPECIFIC can be added to the definition of a CHAR or VARCHAR field in the USING phrase to override the default. Refer also to “USING Modifier” in Chapter 8, “Teradata SQL Syntax Guide.” NOT CASESPECIFIC has been added to ease transition to ANSI mode of operation, but instead of using this transitional feature, you should instead use the ANSI UPPER function to perform case blind comparison.

5-34

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Case Sensitivity in Comparisons

Case Sensitivity in Comparisons Introduction

5

5

All character data accessed in the execution of a Teradata SQL statement has an attribute of CASESPECIFIC or NOT CASESPECIFIC, either as a default or by explicit designation. This is not an ANSI compatible attribute, as ANSI does ALL character comparisons as the equivalent of CASESPECIFIC. Columns of tables carry the attribute assigned, by default or explicitly, at the time the column was defined unless a non-ANSI type modification phrase is used in their access. Provision is made when the user logs on and uses the .SET SESSION command in BTEQ, or uses the equivalent session control command in other applications, to declare a session as operating in ANSI or Teradata mode. IF a session is in this mode . . .

THEN the default for character data in a CREATE or ALTER statement is . . .

ANSI

CASESPECIFIC

Teradata

NOT CASESPECIFIC This attribute persists unless the column definition is modified by a later ALTER statement.

Defaults

Domain Checking

5

5

Character Literal strings in queries, view and macro definitions, CHECK constraints and data accessed via the USING phrase have defaults determined at session logon unless explicitly modified. The default attribute exists only for the life of the session. Expression operands must be of the same data type before a comparison can occur. If the operands data types are different, one of them must be converted. If you find that your applications are comparing data from different domains, you need to review your database design to ensure that column data is properly typed.

Defaults for Non-English Collation

5

On a European or a Japanese character site, which support diacritical or Japanese character sets, respectively, the default collation can be set to a sequence that is compatible with your session’s character set. Use the HELP SESSION statement to find the settings of your current session.

Teradata RDBMS for UNIX SQL Reference

5-35

Data Definition Case Sensitivity in Comparisons

The attribute of your site, the availability of diacritical or Japanese character sets, and your default collation sequence are under the control of your database administrator. For collation details, see the “SET SESSION COLLATION” in Chapter 8, “Teradata SQL Syntax Guide,” the “ORDER BY Clause” of the SELECT statement in Chapter 7, and also Appendix G.

Comparing Character and Graphic Strings

5

Comparison of Japanese character graphic strings is based on logical characters. If the strings are of different lengths, the shorter string is padded with one or more graphic pad characters (double byte zeros). The prepared strings are compared byte-by-byte; trailing pad characters are included in the comparison. If character strings of unequal length are compared on a European Teradata RDBMS, the shorter field is padded with blanks on the right prior to the comparison. If Japanese character strings of unequal length are compared, the shorter string is padded with one or more characters. For non CASESPECIFIC comparisons, any lowercase single byte Latin letters are converted to uppercase before comparison begins. The prepared strings are compared byte-to-byte and any trailing spaces are ignored.

Comparison Rule for Two Character Arguments, CHARA and CHARB

5-36

5

Because of the long persistence of the CASESPECIFIC attribute for columns, and the shorter persistence for attributes carried in the source text of queries, the same statement can have different results for a user logged on in ANSI versus Teradata mode. IF . . .

THEN the comparison is . . .

either argument is CASESPECIFIC

case specific.

both arguments are NOT CASESPECIFIC

case blind for .

Teradata RDBMS for UNIX SQL Reference

Data Definition Case Sensitivity in Comparisons

Example

Consider the following query: 5

SELECT * FROM STUDENTS WHERE FIRSTNAME = ‘George’ ;

IF column FIRSTNAME is . . .

AND the session is in this mode. . .

THEN the match succeeds for rows with FIRSTNAME containing . . .

CASESPECIFIC

either

‘George’

NOT CASESPECIFIC

ANSI

‘George’

NOT CASESPECIFIC

Teradata

‘george’ ‘GEORGE’ ‘George’ and any other combination of cases spelling out the name George.

If you wans to assure case blind comparisons and have an ANSI compliant application, the recommended approach is to define FIRSTNAME with ANSI default (CASESPECIFIC). Use the statement: SELECT * FROM STUDENTS WHERE UPPER(FIRSTNAME) = ‘GEORGE’;

Teradata RDBMS for UNIX SQL Reference

5-37

Data Definition Character Data for a Japanese Character Supported Site

Character Data for a Japanese Character Supported Site Introduction

5

On a Japanese character site, the Teradata RDBMS assumes that all character data is mixed single and multibyte characters. Mixed single and multibyte character data is associated with any column defined as CHAR, VARCHAR, or LONG VARCHAR. Encoding of character data will be either KanjiEBCDIC, KanjiEUC, or KanjiShift-JIS, depending on the current character set for the session. Depending on the character set of the session, single byte and multibyte characters are distinguished as described by the following table. Character Set

KanjiEBCDIC

Definition

Characters are assumed to be single byte until a shift-out character is encountered. Subsequent characters are assumed to be multibyte characters until a Shift-In character is encountered. If the end of the string is reached without finding Shift-In, an error condition occurs.

KanjiEUC KanjiShift-JIS

The first byte of a multibyte character always has the most significant bit on. Multibyte characters are two bytes except KanjiEUCcs3 characters, which require three bytes. The size of a single byte/multibyte character column must be expressed in bytes required to store the internal representation of the data.

Fixed Length KanjiEBCDIC: Example

Assume that a fixed-length column is to contain the following KanjiEBCDIC data: 5

< S T R I N G > 12

The column definition must be at least 16 bytes (CHAR(16)) to accommodate the internal representation of the data plus the ShiftOut and Shift-In characters, which is as follows: 0E 42E2 42E3 42D9 42C9 42D5 42D7 0F 31 32

Note that each of the three client representations of multibyte character data could require a different length for the same sequence of symbols.

5-38

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Character Data for a Japanese Character Supported Site

Fixed Length KanjiShiftJIS: Example

The same string in KanjiShift-JIS, is as follows: S T R I N G 12 5

and requires a length of only 14 bytes (CHAR(14). Again, here bold indicates a double byte character. The internal equivalent for the KanjiShift-JIS string is as follows: 8272 8273 8271 8268 826D 8266 31 32

Character Expressions Assigned to Shorter or Longer CHAR Columns

If a character expression of some length is assigned to a CHAR column of a longer length, the field is padded with the character. 5

In Teradata mode, if a character expression is assigned to a CHAR column of a shorter length, the extra bytes are truncated. This may result in an improper string. You are not informed that this truncation has occurred. In ANSI mode, an error occurs if a nonblank character is truncated. Shorter strings are padded with single byte spaces, regardless of whether the mode is Teradata or ANSI. Only truncation differs between the two modes.

SQL Terminal Symbols

5

Each of the valid SQL terminal symbols is translated into the same internal code for KanjiEBCDIC, KanjiEUC, and KanjiShift-JIS. Thus, a string containing only SQL terminal symbols is retrievable from any of the three character sets. Refer to the Glossary for a definition of SQL terminal symbols.

Character Data Validation and Storage

5

On a Japanese character supported site, the Teradata RDBMS validates all character data against the range of hexadecimal values considered valid for the character set of the current session. All validated single byte character data is translated into the Teradata RDBMS internal representation, which is based on JIS0x0201. See Appendix H, “Japanese Character Sets” and Appendix I, “Non-valid Japanese Character Code”, for the tables of character set encodings, translation mapping, and validation ranges.

Teradata RDBMS for UNIX SQL Reference

5-39

Data Definition Character Data for a Japanese Character Supported Site

Translation and storage of validated multibyte character data depends on the character set of the current session, as explained in the following table. Character Set

How Multibyte Characters are Translated and Stored

KanjiEBCDIC

All multibyte characters are stored as received; that is, they are not translated and remain in the client encoding. The Shift-Out and Shift-In characters are stored as part of the string after being translated to the same value (0E and 0F, respectively).

KanjiShift-JIS

All multibyte characters are stored as received; that is, they remain in the client encoding.

KanjiEUC

Multibyte character data is handled according to the client encoding of the current session, as follows For each character of this code set . . .

The first byte is translated as to. . .

cs1

KanjiShift-JIS (based on JISx0208), as illustrated in the translation map in Appendix H and I. Subsequent characters are also translated to KanjiShift-JIS.

5-40

cs2

0x80 and the second byte is left unmodified.

cs3

0xFF and the remaining two bytes are left unmodified.

Teradata RDBMS for UNIX SQL Reference

Data Definition BYTE and VARBYTE Data Types

BYTE and VARBYTE Data Types Introduction

5

5

BYTE and VARBYTE are flagged as non-ANSI when the SQL flagger is enabled. The BYTE data type represents a fixed-length binary string. The specification is: BYTE VARBYTE

n HH01A097

If no value is specified for n, the default is BYTE(1).

Fixed Length Binary String

5

BYTE (n)

n = 1 to 32000 (number of bytes)

FF19A010

The BYTE data type is stored in the client system’s format; it is never translated by the Teradata RDBMS. BYTE data is handled as n-byte unsigned binary integers. This data type is suitable for digitized images. The VARBYTE(n) data type represents a variable length binary string. The maximum length for n (BYTE and VARBYTE) is 32,000.

Variable Length Binary String

VARBYTE (n) 5

Two byte length

n = 1 to 32000 (maximum number of bytes)

FF19A011

The VARBYTE data type is stored in the client system’s format; it is not translated. VARBYTE data is handled as n-byte unsigned binary integers. This data type is suitable for digitized images. The BYTE and VARBYTE(n) data types store raw data as logical bit streams. For any machine, BYTE data is transmitted directly from

Teradata RDBMS for UNIX SQL Reference

5-41

Data Definition BYTE and VARBYTE Data Types

the client system’s memory. The sort order is logical, and values are compared as if they were n-byte, unsigned binary integers. Logical bit streams are not translated. However, the interpretation of a hexadecimal string in EBCDIC form depends on the client system from which that string is submitted. In the following example, BinaryData is assigned the VARBYTE(n) data type: BinaryData VARBYTE(12),

5-42

Teradata RDBMS for UNIX SQL Reference

Data Definition Japanese Character Graphic Data Types

Japanese Character Graphic Data Types Introduction

5

5

Japanese character graphic data types are flagged as non-ANSI, when the SQL flagger is enabled. On a Japanese character supported site, the following data types can be used to represent multibyte character data. ● ● ●

GRAPHIC VARGRAPHIC LONG VARGRAPHIC

Each multibyte character in a graphic string is stored assuming two bytes per logical character. Therefore, a graphic data string must always represent an even multiple of bytes. Graphic data types accommodate the following character sets: ● ● ●

KanjiEBCDIC double byte graphic data KanjiShift-JIS for double byte Shift-JIS codes KanjiEUC for fixed-length, double byte EUC characters

Graphic columns also accept EUC codeset cs3 characters (three bytes per character) as long as the string contains an even multiple of cs3 characters (representing six bytes, twelve bytes, and so forth). However, this is not recommended because the correct manipulation of cs3 characters cannot be guaranteed. Graphic columns also can be defined and populated under the standard EBCDIC and ASCII character sets, as long as it is Japanese character site. Because graphic data types are available to all users regardless of their character set, special care must be taken when graphic data that was inserted under one character set is retrieved under a different character set. The data returned to the client might not be valid double byte characters for the character set of the current session. A logical graphic string constant is allowed only under the KanjiEBCDIC character sets (others may use the hexadecimal constant equivalent). Under KanjiEBCDIC, use the following form: G

'< graphic_characters >' HH01A098

Teradata RDBMS for UNIX SQL Reference

5-43

Data Definition Japanese Character Graphic Data Types

Under a KanjiEBCDIC character set, the multibyte characters comprising the graphic string constant must be delimited with the Shift-Out/Shift-In characters; for example: INSERT INTO TableEBCDIC (ColGRAPH) VALUES (G’’);

where AB is a valid string of KanjiEBCDIC multibyte characters.1 The size of a graphic data string is expressed in logical characters. Each logical character represents two bytes; thus, the maximum length for a GRAPHIC data type is half of the maximum length for a CHAR data type (16,000 or 32,000/2).

Japanese Character Graphic Literals GRAPHIC[(n)] Data Type

The graphic literal G’’ is flagged as non-ANSI when the SQL flagger is enabled. 5

5

The GRAPHIC data type represents a fixed-length multibyte character string of length n, where n is the length in logical characters. The maximum value of n is 16,000. The GRAPHIC data type is valid only for Japanese character supported sites. If any other site attempts to use this data type, the system generates an error message that states: The GRAPHIC data type is not allowed for non Japanese sites.

VARGRAPHIC(n) Data Type

5

THEN . . .

the length is not specified

the default is GRAPHIC(1); that is, one logical character (two bytes).

a graphic string is shorter than the specified length of the column

the remaining space is filled with the graphic pad character (double byte zero).

a graphic string is longer than the specified length of the column

the extra bytes are truncated.

The VARGRAPHIC data type represents a variable-length string of length n, where n is the length in logical characters. The maximum value of n is 16,000. There is no default; omitting the length specification results in an error.

1

5-44

WHEN . . .

bold = double byte character, G specifies the string is graphic data, and each ‘(quote)’ is a single byte character.

Teradata RDBMS for UNIX SQL Reference

Data Definition Japanese Character Graphic Data Types

During data transmission between the Teradata RDBMS and the client system, a VARGRAPHIC string is prefixed with two physical bytes that indicate the length of the string. If a graphic string is longer than the maximum specified length of the column, the extra bytes are truncated.

LONG VARGRAPHIC Data Type Graphic Data Validation and Storage

5

5

The LONG VARGRAPHIC data type specifies the longest permissible variable-length graphic string. It is equivalent to specifying a data type of VARGRAPHIC(16000). Graphic data must always contain an even number of bytes. Any attempt to insert data that results in an odd number of bytes generates an error. Graphic data is stored without translation; that is, the multibyte characters remain in the encoding of the session character set. The Teradata RDBMS validates graphic data against the range of hexadecimal values considered valid for the character set of the current session. Hexadecimal constants cannot be validated. Note: Because padding is provided for fixed-length graphic columns, a graphic pad character is considered a valid character.

Retrieving Graphic Data

5

Under all character sets, graphic data is returned to the client untranslated. The following special considerations are important in KanjiEBCDIC. In this mode . . .

Graphic data is . . .

Record

assumed to be multibyte characters and no surrounding Shift-Out/Shift-In characters are expected.

Indicator Field

treated as a character string and the data is expected to be surrounded by Shift-Out/ Shift-In characters. The Teradata RDBMS provides these characters to achieve a properly formatted result.

Teradata RDBMS for UNIX SQL Reference

5-45

Data Definition Japanese Character Graphic Data Types

Example

5

Assume that Col1 is defined as GRAPHIC(3) and that it contains the following data: 457F4577456D

Under a KanjiEBCDIC session in record or indicator mode, the contents of Col1 are returned to the user as follows: 457F4577456D

Under a KanjiEBCDIC session in field mode, the contents of Col1 are returned to the user in proper format, as follows: 0E457F4577456D0F

5-46

Teradata RDBMS for UNIX SQL Reference

Data Definition Column and Table Constraints

Column and Table Constraints Introduction

5

5

You can specify constraints on column values when the columns are defined during table creation and modification. These constraints can be defined on single columns as a part of the column definition, or on multiple columns. The full syntax for such constraints is shown in the CREATE TABLE and ALTER TABLE sections in Chapter 8.

Column Constraints

Column constraints include: 5

● ● ● ●

Table Level Constraints

PRIMARY KEY UNIQUE CHECK (boolean expression) REFERENCES tablename [(column name list)]

Table level constraints include: 5

● ● ● ● ●

FOREIGN KEY (column name list) PRIMARY KEY UNIQUE CHECK (boolean expression) REFERENCES tablename [(column name list)]

Teradata RDBMS for UNIX SQL Reference

5-47

Data Definition Constraints

Constraints Introduction

Constraints may be categorized as any of the following 5

● ● ●

Uniqueness CHECK Referential Integrity

UNIQUE means the table can not include two or more rows where the values for the column or set of columns are identical.

PRIMARY KEY

UNIQUE

CHECK CONSTRAINTS

5

5

5

A column or set of columns defined as PRIMARY KEY, must also be NOT NULL. The Teradata RDBMS instantiates a PRIMARY KEY as a UNIQUE [PRIMARY] INDEX. Only one PRIMARY KEY can be defined for a table. Refer to “CREATE TABLE” on page 8-69 for more details. A column or set of columns defined as UNIQUE and which cannot be NULL. Queries are flagged as not Entry Level ANSI when the SQL flagger is enabled if any listed column is not also defined as NOT NULL. The Teradata RDBMS instantiates UNIQUE as a UNIQUE [PRIMARY or SECONDARY] INDEX. CHECK CONSTRAINTS compare values of a field or fields in the same row with constants or other fields. CHECK CONSTRAINTS are applied to rows generated as candidates for INSERT and UPDATE operations. If the condition is met or the proposed INSERT or UPDATE contains NULLs, the operation is permitted. If the condition is not met, an error is reported as ‘constraint violation’. Refer to “CREATE TABLE” on page 8-69 for details on the syntax. Examples: CHECK (Sex = ‘F’ OR Sex = ‘M’) CHECK (EdLev BETWEEN 0 AND 22 AND YrsExp BETWEEN 0 and 70)

REFERENTIAL CONSTRAINTS

5-48

5

REFERENTIAL CONSTRAINTS are used to indicate relationships between columns of different tables. They are used to enforce referential integrity on related tables. Refer to “CREATE TABLE” on page 8-69 and “ALTER TABLE” on page 8-7 and also Chapter 9, “Advanced SQL” in the section “Referential Integrity”

Teradata RDBMS for UNIX SQL Reference

5

Data Definition FORMAT Phrase

FORMAT Phrase Introduction

5

5

The FORMAT phrase is flagged as non-ANSI when the SQL flagger is enabled. The FORMAT phrase controls the display of expressions and column data. Formats can be specified for columns that have character, graphic, numeric, byte, or date data types. The FORMAT phrase is also used in controlling conversions between data types (character to numeric, numeric to character, e.t.c.) Note: FORMAT pertains to data exported in report form, as is the case in BTEQ. FORMAT does not control internal storage representation of data, or data returned in record or indicator variable mode.

Truncation of Japanese Character Fields

5

It is possible to truncate the output of character fields within multibyte characters when you use any of the following character sets: ● ● ●

KanjiEBCDIC KanjiEUC KanjiShift-JIS

In Teradata mode, the system does not tell you this has happened. returns the number of bytes specified. The table below shows the hexadecimal returns. The actual return display depends on which system is used.

This character . . .

With this format . . .

Returns this values in hexadecimal . . .

With this character set . . .

B

‘xx’

C20E

KanjiEBCDIC

BAA

‘xx’

42A3

KanjiEUC

In ANSI mode, an error occurs if a non-blank character is truncated. Graphic formats are discussed in the following section. A FORMAT specification cannot contain more than 30 characters; the specification must be enclosed by apostrophes. A FORMAT phrase can describe up to 18 digit positions (17 if the FORMAT contains an E). IEEE 64-bit floating numbers are accurate to about 15 digits.

Teradata RDBMS for UNIX SQL Reference

5-49

Data Definition FORMAT Phrase

The FORMAT phrase can be used in a CREATE TABLE statement or ALTER TABLE statement to define the display format for a column. It can also be used in a retrieval statement to override the default format of a column or to define the display format of an expression. See also the following sections “Graphic Formats”, “Numeric Formats”, “DATE Formats”, and “Data Default Formats”, as well as the earlier descriptions under each data type, for default format examples.

Example 1 Example 2 Without FORMAT Clause

The following are simple examples of the FORMAT phrase: 5

FORMAT ’9(5)’, FORMAT ’Z999’, FORMAT ’XX’

If the CREATE TABLE statement defined the format for the Salary column as ’ZZ9999.99’, the result of the statement: 5

SELECT SUM(Salary) FROM Employee;

is: SUM(Salary) 851100.00

Example 3: With FORMAT Clause

The result of the following statement: SELECT SUM(Salary) (FORMAT ’$$99,999.99’) FROM Employee;

5

is: SUM(Salary) $851,100.00

FORMAT phrases, by themselves, cannot cause conversion of character to numeric data, or numeric to character data. An error message is returned if a FORMAT phrase implies data conversion.

Example 4: Using FORMAT to Change the Format of Returned Data

Reconfigure the format of data returned: SELECT empno(FORMAT ’99-999’), name, deptno FROM employee; 5

The system returns: EmpNo 10-021 10-007 10-018 10-011

5-50

Name Smith T Aguilar J Russell S Chin M

DeptNo 700 600 300 100

Teradata RDBMS for UNIX SQL Reference

Data Definition FORMAT Phrase

Example 5: Using FORMAT to Override Default Format

Another example of overriding the column default and formatting the output: 5

SELECT salary(FORMAT ’$$$,$$9.99’) FROM employee WHERE empno = 10019 ;

The system returns: Salary $28,600.00

Example 6: Using FORMAT as Part of Derived Expression

You can use FORMAT as part of a derived expression. To determine the percent increase if employee 10019 is given a $1000 raise: 5

SELECT (1000/salary)*100 (FORMAT ’zz9%’) (TITLE ’Percent Incr’) FROM employee WHERE empno = 10019 ; Percent Incr 3%

Teradata RDBMS for UNIX SQL Reference

5-51

Data Definition Character Formats

Character Formats Introduction

5

The letter X formats the display of character data. The data is returned in left-to-right order. For example, if a column is defined as FORMAT ’X’, only the first (leftmost) character of each field value is displayed. Multiple character positions can be defined either by following the first X with a parenthetical number defining the length of the string, or by entering the equivalent number of X characters.

Example 1

5

Both of the following FORMAT phrases specify a five-position character string: FORMAT ’X(5)’ FORMAT ’XXXXX’

Character strings are padded or truncated on the right when they are shorter or longer than the positions defined in the FORMAT phrase.

Example 2

If your data is the character string ’HELLO’, then: 5

FORMAT ’X(6)’

displays HELLO with trailing blank, and: FORMAT ’X’

displays H.

5-52

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Graphic Formats

Graphic Formats Introduction

5

5

The letter G, which formats the display of graphic data, corresponds to one logical (double byte) character of the graphic string, and the data is returned in left-to-right order. For example, if a column is defined as FORMAT ’G’, only the first (leftmost) logical character of each field value is returned. To define that multiple graphic positions are to be returned, either follow the first G with a parenthetical number defining the length of the string, or enter the equivalent number of G characters. To specify a six-position graphic string, use either of the following: FORMAT ’G(6)’ FORMAT ’GGGGGG’

Graphic strings are padded or truncated on the right when they are shorter or longer than the positions defined in the FORMAT phrase. The padding character is regardless of the current character set. If the FORMAT phrase is not included in a CREATE or ALTER TABLE statement, the default is to return the full length specified for a fixed-length GRAPHIC column (that is, any padding characters are returned unless TRIM is included in the SELECT statement), and to return the actual graphic string for a VARGRAPHIC or LONG VARGRAPHIC column. Under a KanjiEBCDIC session in field mode, an expression referencing a GRAPHIC data type returns a properly formatted string; that is, the Shift-Out/Shift-In characters are included in the output. This increases the length of the result expression by two bytes. The increase is automatic; it need not be accounted for in a FORMAT phrase, but it should be considered when designing a BTEQ report format.

Teradata RDBMS for UNIX SQL Reference

5-53

Data Definition Graphic Formats

Example

5

Assume that a table named Test_Graphic defines Col1 and Col2 as GRAPHIC(3). If the following insert is performed from a KanjiEBCDIC machine, as follows: INSERT INTO Test_Graphic (Col1,Col2 ) VALUES (’42C142C242C3’XG,G’’);

then the following query, in field mode: SELECT Col1 (FORMAT ’G(3)’), Co2L2 (FORMAT ’G(3)’ FROM Test_Graphic;

returns the following string2: Col1 -----

2

5-54

Col2 ------

< is Shift-out, > is Shift-In

Teradata RDBMS for UNIX SQL Reference

Data Definition Numeric Formats

Numeric Formats Introduction

5

The result of a formatted numeric string is right-justified. 5

A FORMAT phrase that defines fewer positions than are required by numeric values will cause the data to be returned as follows: Asterisks are displayed when the integer portion cannot be accommodated. When only the integer portion can be accommodated, any digits to the right of the least significant digit are either truncated (for an integer value) or rounded (for a floating or decimal value).





Rounding is based on “Round to the Nearest” mode, as explained by the following procedure. Step

Action

1

Let B represent the actual result.

2

Let A and C represent the nearest bracketing values that can be represented, such that A < B < C.

3

The determination as to whether A or C is the represented result is made as follows Step

Example

Action

1

When possible, the result is the value nearest to B.

2

If A and C are equidistant (for example, the fractional part is exactly .5), the result is the even number.

The following SELECT statement returns 48 and 48. 5

SELECT 47.5(FORMAT ‘zzzz’), 48.5(FORMAT(‘zzzz’)) ;

returns 48 and 48. A FORMAT clause can cause the number to be rounded; for example, consider the following: SELECT 1.3451 (FORMAT ’zz.z’);

The FORMAT clause operates on 1.3451 and returns 1.3. SELECT 13451 / 10000.000

1.345 is returned as expected. SELECT 13451 / 10000.000 (FORMAT ’zz.z’);

Teradata RDBMS for UNIX SQL Reference

5-55

Data Definition Numeric Formats

The arithmetic expression is evaluated first (yielding 1.345), then the FORMAT clause is applied, returning 1.3. SELECT 13451 / 10000.00 (FORMAT ’zz.z’);

The arithmetic expression is evaluated first (yielding 1.35), then the FORMAT clause is applied. Note that two ‘roundings’ occur; first to 1.35 when evaluating the expression, then a second rounding to 1.4 as a result of the FORMAT statement. To avoid double ‘roundings’, extend the precision of the arithmetic expression two decimal places beyond that of the FORMAT clause. Character

Meaning

/ : %

Insertion characters.

,

Insertion character.

Copied to output string where they appear in the FORMAT phrase

The comma is inserted only if a digit has already appeared. .

A special insertion character in that it represents a decimal point position.

B

Insertion character. A blank is copied to the output string wherever a B appears in the FORMAT phrase.

+ -

Sign characters. May be placed at the beginning or end of a format string. One sign character places the edit character in a fixed position for the output string. If two or more of these characters are present on the left, the sign floats (moves to the position just to the left of the number as determined by the stated structure). The + translates to + or - as appropriate; the - translates to - or blank.

5-56

Teradata RDBMS for UNIX SQL Reference

Data Definition Numeric Formats Character

$

Meaning

Dollar sign. One $ places the edit character in a fixed position for the output string. If a result is formatted using a single $ with Z’s for zero-suppressed decimal digits (for example, $ZZ9.99), blanks may occur between the $ and the leftmost nonzero digit of the number. If two or more $’s are present, the $ floats to the right, leaving no blanks between it and the leftmost digit. If + or - is present, the dollar sign cannot precede it.

V

Implied decimal point position. Internally, the V is recognized as a decimal point to align the numeric value properly for calculation. Because the decimal point is implied, it does not occupy any space in storage and is not included in the output.

Z

Zero-suppressed decimal digit. Translates to blank if the digit is zero and preceding digits are also zero. If only Z’s, commas, and dots are used, the FORMAT phrase means “blank when zero.” For example, ZZZZZ, ZZ.Z, and Z,ZZZ.ZZ print only blanks if the number is zero. A Z is illegal if it is specified following a 9.

9

Decimal digit (no zero suppress).

E

For exponential notation. Defines the end of the mantissa and the start of the exponent.

char(n)

For more than one occurrence of the following characters: -, +, $, Z, or 9. The (n) notation means that the character is to be repeated n number of times.

Teradata RDBMS for UNIX SQL Reference

5-57

Data Definition Numeric Formats Character

-

Meaning

Dash character. Used when storing numbers such as telephone numbers, social security numbers, and account numbers. If a dash appears immediately after the first digit or before the last digit, it is taken as an embedded dash rather than a sign character. A dash is illegal if it follows a period, comma, plus sign, dollar sign, or V.

S

Signed Zoned Decimal character. Defines signed zoned decimal input as a numeric data type, and displays numeric output as signed zone decimal character strings. The S must follow the last decimal digit in the FORMAT phrase. It cannot be used in the same phrase with the characters: %, +, $, Z, or E.

The display results of various FORMAT phrases for numeric data are shown in Table 5-6. Table 5-6 Numeric Format Results

FORMAT Phrase

Data

Result

(FORMAT ’$$9.99’)

.069

$0.07

(FORMAT ’$$9.99’)

1095

******

(FORMAT ’ZZ,ZZ9.99’)

1095

1,095.00

(FORMAT ’9.99E99’)

1095

1.10E03

(FORMAT ’999V99’)

128.457

128.46

(FORMAT ’$(5).9(2)’)

1

$1.00

(FORMAT ’999-9999’)

8278777

827-8777

(FORMAT ’ZZ,ZZ9.99-’)

1095

1,095.00

(FORMAT ’ZZ,ZZ9.99-’)

-1095

1,095.00-

If the format phrase does not include a sign character, a negative value will have its sign discarded, and the data will appear as positive. Asterisks are displayed when the defined format is too small to accommodate the integer portion of a numeric value. When digits to the right of the least significant digit cannot be accommodated by the defined format, INTEGER values are truncated, and FLOAT or DECIMAL values are rounded. The rounding mode is rounding to nearest.

5-58

Teradata RDBMS for UNIX SQL Reference

Data Definition Numeric Formats

Japanese Character Site

European Character Site

5

5

On a Japanese character site, a set of alternate currency symbols is available when the AltCurrency flag in the RDBMS ControlRecord is set ON. On a European character site, alternative currency symbols also are available with the AltCurrency flag is ON. However, your userdefined character sets must map each external currency symbol to the appropriate Teradata RDBMS internal representation before the symbols can be used. The advantage is that any keyboard character can be created as a currency symbol by mapping it to one of the internal currency codes. (Contact your database administrator to determine if European alternative currency symbols are available.) The symbols in each set are as follows:

Table 5-7 European Alternative Currency Symbols

Table 5-8 Japanese Alternative Currency Symbols

External Symbol

Description

$

Dollar sign

¢

Cent sign

£

Pound sign

¥

Yen sign

general currency

General currency symbol

External Symbol (Any Kanji Character Set)

Description

$

Dollar sign

¥

Yen sign

Teradata RDBMS for UNIX SQL Reference

5-59

Data Definition Numeric Formats

An alternate currency symbol has the same formatting rules and properties as the standard dollar sign ($). That is, one symbol places the edit character in a fixed position of the output string; two or more symbols defined with Z’s (for zero-suppressed decimal digits) causes the symbol to float to the right, leaving no blanks in the output between the sign and the leftmost digit. Activating alternate currency symbols does not affect the normal properties of the FORMAT phrase. For example, if alternate symbols are in effect, then the following statement: SELECT 1 (FORMAT ’$$Z,ZZ9.99’), 2 (FORMAT ’YYZ,ZZ9.99’);

returns the following: $1.00 Y2.00

Note that two currency symbols (for example, $ and ¥) cannot be used together in the same FORMAT phrase.

5-60

Teradata RDBMS for UNIX SQL Reference

Data Definition DATE Formats

DATE Formats Introduction

DATE formats consist of the characters shown in Table 5-9. 5

Table 5-9 DATE Format Characters

Characters

Meaning

DMY

Specifies both the input and output formats for dates. DMY indicates the day, month, and year, respectively.

DDD

Specifies that date is represented in the format of sequential day in the year. For example, January 12 is represented as ’012’. DDD may be used with any combination of year and separator characters or blanks.

MMM

Specifies that the month is to be abbreviated to 3 characters (OCT).

MM

Format Consistency Is Enforced

5

5

Specifies that the month is to be indicated as a 2-digit number.

/

Slash; used as a separator.

B

Blank; used as a separator.

,

Comma; used as a separator.



Apostrophe; used as a separator.

:

Colon; used as a separator.

.

Period; used as a separator.

-

Dash; used as a separator.

If a field is declared as type DATE and a FORMAT phrase is specified, the data must be entered and retrieved in the specified format, and it must be enclosed in apostrophes. For example, data supplied to, or used in, a conditional WHERE or HAVING clause for the following column must be defined as ’OCT 24, 1985’. DOB DATE FORMAT ’MMMbDD,bYYYY’

DATE Comparisons

5

If a field is to be compared with a DATE value, or inserted into a column in DATE format, you must be sure that the contents of the field match the default DATE format, or you must specify a date in the conversion that matches the expected content.

Teradata RDBMS for UNIX SQL Reference

5-61

Data Definition DATE Formats

For example, if the data is CHAR(6) in the form yymmdd, and a column date format is YYMMDD, the conversion works. If, however, the default DATE format (YY/MM/DD) is used, the conversion fails. In this case, you can force the conversion from character to integer by including the INTEGER phrase. Numeric values can be converted into dates in the default format (YY/MM/DD), or you can force conversion to DATE with the format YYMMDD. IF this field . . .

Is in this form . . .

THEN the conversion is . . .

CHAR(8)

yy/mm/dd

Direct.

yyyymmdd

to DATE (FORMAT ’YYYYMMDD’)

mmmdd,yyyy

to DATE (FORMAT ’MMMDD,YYYY’).

CHAR(11)

Note: The recommended format for DATE, as the year 2000 approaches, is ‘YYYY-MM-DD’, which is also the only ANSI SQL format for a date. Formats using only the last two digits of the year will be a problem as the year 2000 is approached. For Example: ‘2000-01-01’. Formatted as ‘YY/MM/DD’ this is 00/01/01 and so is ‘1900-01-01’. Table 5-10 lists examples of formats that can be used to present a date. Table 5-10 DATE Format Results

5-62

FORMAT Phrase

Data

Result

FORMAT ’YY/MM/DD’

85/09/12

85/09/12

FORMAT ’DD-MM-YY’

85/09/12

12-09-85

FORMAT ’YYYY/MM/DD’

85/09/12

1985/09/12

FORMAT ’YYYY-MM-DD’

85/09/12

1985-09-12

FORMAT ’YYYY.DDD’

85/09/12

1985.225

FORMAT ’YYBDDD’

85/09/12

85 225

FORMAT ’DDBMMMBYYYY’

85/09/12

12 Sep 1985

FORMAT ’MMMBDD,BYYYY’

85/09/12

Sep 12, 1985

FORMAT ’YYYYBMMMBDD’

85/09/12

1985 Sep 12

FORMAT ’MMM’

85/09/12

Sep

Teradata RDBMS for UNIX SQL Reference

Data Definition DATE Formats

How To Query When The Response Must Be Uppercase

If all uppercase is required, the following statement can be used: SELECT DATE (FORMAT ’MMMbdd,bYYYY’) (CHAR(12), UC) ;

5

Using 85/09/12 for data, this statement returns: SEP 12, 1985

The following query shows how the date is specified if purchase_date had been specified as ’DDBMMMBYYYY’: SELECT * FROM sales WHERE purchase_date = ’30 Mar 1994’...

If purchase_date had been specified as ’YYYY-MM-DD’, the query would be as follows: SELECT * FROM sales WHERE purchase_date = ’1994-03-30’...

Record Mode Import Anomaly

5

When data is imported in Record Mode (for example, using bulkload) into a NULLABLE DATE field, and the source data is a binary integer of value zero, then the field is set to NULL (not 0).

Teradata RDBMS for UNIX SQL Reference

5-63

Data Definition Data Default Formats

Data Default Formats

5

Table 5-11 lists the default display format that Teradata SQL provides for each data type. Table 5-11 Data Type Format Defaults

Data Type

Default Format

BYTEINT

-(3)9

SMALLINT

-(5)9

INTEGER

-(10)9

DECIMAL[(n[,m])] NUMERIC[(n[,m])]

The total length is n+2; the +2 represents one additional position for the sign, and one additional position for the decimal point. n = total number of digits. m = decimal portion of the number n-m = integer portion of the number.

FLOAT REAL DOUBLE PRECISION

-9.99999999999999E-999

CHAR[(n)]

X(n)

VARCHAR(n)

X(n)

LONG VARCHAR

X(32000)

BYTE[(n)]

X(2n)

VARBYTE(n)

X(2n)

DATE

a

YY/MM/DD

GRAPHIC[(n)]

G(n), where n represents the number of two-byte logical characters

VARGRAPHIC(n)

G(n) where n represents the number of two-byte logical characters

a. This default may be changed in future releases to ‘yyyy-mm-dd’ (the ANSI date format) to avoid the crossover problem from 1999 to 2000.

Examples of character string and numeric formats are shown in Table 5-12. Table 5-12 FORMAT Displays

5-64

FORMAT

Data

Display Form

X(6)

’HELLO’

’HELLO ’

XXXXXX

’HELLO’

’HELLO ’

Teradata RDBMS for UNIX SQL Reference

Data Definition Data Default Formats FORMAT

Data

Display Form

X

’HELLO’

’H’

X(5)

’HELLO’

’HELLO’

$$9.99

.079

’ $0.08’

$$9.99

1095

******

ZZ,ZZ9.99

1095

’ 1,095.00’

9.99E99

1095

’1.09E03’

999V99

123.456

’12346’

$(5).9(2)

1

’$1.00’

YY.DDD

85.224

’85.224’

MMMBDD,BYY

Sep 12, 85

’Sep 12, 85’

YYYY-MM-DD

1996-02-14

‘1996-02-14’

YYYYBMMMBDD

1985 Sep 12

’1985 Sep 12’

KatakanaEBCDICa

mNb

MNB

with default format

a. < is Shift-Out, > is Shift-In, bold means double byte. Note that KatakanaEBCDIC uppercases single byte data.

Teradata RDBMS for UNIX SQL Reference

5-65

Data Definition Naming Columns and Expressions

Naming Columns and Expressions Introduction

5

Each column in a SELECT result has a name that is derived from the list of specified column names (expressions) that generated the data. The name is typically the column name from which the data came. For example, the columns in the result SELECT EmpNo,Name FROM Employee;

are labeled EmpNo and Name by default. In some cases, it is necessary to associate a column name that is different from the default column name with a result. The Teradata RDBMS provides two ways to name a column.

Naming Columns

5

The Teradata RDBMS allows for naming derived columns and renaming existing columns. The syntax allowed in a query expression is of the form: value_expression

name AS or

(expression)

(NAMED

name) HH01A015

Note: Keyword AS is optional, in the first form above. For every expression which is given a name, that name is entered in a NAMED list which associates the expression with the name. This is true regardless of which of the above two syntax forms is used.

Example 1

5

This syntax allows the ORDER BY clause to include column names rather than positionally defining the sort columns. SEL COLUMN1 +100 AS FIRST_VALUE, COLUMN2 SECOND_VALUE FROM TABLE1 WHERE SECOND_VALUE >100 ORDER BY FIRST_VALUE

The AS keyword is not included when COLUMN2 is renamed to SECOND_VALUE.

5-66

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Naming Columns and Expressions

Example 2

5

This syntax creates a temporary named column and uses it in a WHERE clause. SELECT Name,((Salary + (YrsExp * 200))/12) AS Projection FROM Employee WHERE DeptNo = 600 AND Projection < 2500;

Alternatively, a NAMED clause can be used to name columns.

Example 3

5

In the following SELECT statement, a NAMED phrase associates an arithmetic expression with the name “Projection” so that the calculated column may be more easily referenced in the WHERE clause. The second form, using the NAMED clause, is flagged as non-ANSI when the SQL flagger is enabled. SELECT Name, ((Salary + (YrsExp * 200))/12) (NAMED Projection) FROM Employee WHERE DeptNo = 600 AND Projection < 2500;

The ANSI syntax for Example 3 is shown in Example 2.

Rules

5

If a column is associated with an explicit tablename, the table is searched for the columnname. Based on the result of the search, one of the following occurs: IF this thing . . .

IS . . .

THEN . . .

matching column

found in the table

the requested information is returned.

not found in the table

the NAMED list is searched for the column name.

not found in the explicit table name or in the NAMED list

a nonexistent column error is reported.

column name

Teradata RDBMS for UNIX SQL Reference

5-67

Data Definition Naming Columns and Expressions

If a column is not associated with an explicit tablename, tables named in the SQL statement are searched and one of the following occurs. IF this thing . . .

IS . . .

THEN . . .

matching column

found in only one table

the requested information is returned.

found in two or more tables

an ambiguous column name error is reported.

not found

the NAMED list is searched for the column name.

not found inn the SQL statement or in the NAMED list

a nonexistent column error is reported.

column name

Naming Differences: AS Versus NAMED

ANSI syntax is similar to the Teradata NAMED clause. The primary difference is that in the NAMED keyword in the Teradata syntax. 5

NAMED is required in Teradata syntax, while AS is not. The NAMED clause is enclosed in parentheses immediately following the renamed column. The NAMED clause is supported as an extension; do not use it for newly developed applications.

5-68

Teradata RDBMS for UNIX SQL Reference

Data Definition TITLE Phrase

TITLE Phrase Introduction

5

5

Title Phrase is flagged as non-ANSI when the SQL flagger is enabled. The TITLE phrase defines a heading for displayed or printed results that is different from the column name, which is used by default. A TITLE specification string is limited to 60 characters, and must be enclosed in single quotes. In statements submitted via BTEQ, a TITLE specification may be up to three lines. A double slash character (//) defines a line break. You can insert blanks to center lines. You can use the TITLE phrase in a CREATE TABLE or ALTER TABLE statement to specify a standard heading.

Example 1

Example 2

The following example shows a simple use of the TITLE phrase. 5

CREATE TABLE Charges, FALLBACK (EmpNo SMALLINT FORMAT ’9(5)’ TITLE ’Employee//Id’ BETWEEN 10001 AND 32001 NOT NULL, Proj_Id CHAR(8) TITLE ’Project// Id’ NOT NULL, WkEnd DATE FORMAT ’YY/MM/DD’ TITLE ’Week//Ending’, Hours DECIMAL(4,1) FORMAT ’ZZ9.9’ BETWEEN 0.5 AND 999.5) PRIMARY INDEX (EmpNo, Proj_Id) INDEX (Proj_Id);

5

When a title is specified at column creation time, note that the defined column name, not the title name, must be specified in statements that access the column. For example, to retrieve the total hours worked on each project by user Peterson, the project column must be referenced: SELECT Proj_Id,SUM(Hours) FROM CHARGES WHERE EmpNo = 10001 GROUP BY Proj_Id ORDER BY Proj_Id ;

The return, however, uses the following title headings: Project Id PAY-0001 PAY-0002

Sum(Hours) 9.5 34.5

A TITLE phrase also can be given in the retrieval statement to override any defined standard.

Teradata RDBMS for UNIX SQL Reference

5-69

Data Definition TITLE Phrase

For example, in the following SELECT statement, a TITLE phrase provides a more descriptive heading for the DOB column. SELECT Name, DOB (TITLE ’Birthdate’) FROM Employee;

This statement returns: Name Smith T Newman P Omura H . .

Birthdate 51/10/31 56/08/29 54/04/24 . .

The multi-line TITLE definition in this statement: SELECT Name, DOB (TITLE ’Date//Of//Birth’) FROM Employee;

returns:

Name Smith T Newman P Omura H . .

5-70

Date Of Birth 51/10/31 56/08/29 54/04/24 . .

Teradata RDBMS for UNIX SQL Reference

Data Definition TITLE Phrase

Title Phrase Rules

5

Teradata SQL uses the following rules to generate a title for expression x:

IF x is . . .

THEN . . .

Example

a column reference with an explicit TITLE phrase

that title value is returned.

The explicit title for Project.ProjId, as defined in the CREATE TABLE statement for the Project table, is Project//Id. The statement: SELECT Proj_Id FROM Project WHERE Description = ’O/E Batch System’;

returns the following result: Project Id OE2-0003

a column reference with no explicit TITLE

the column name is returned.

The Salary column has no explicit TITLE. The statement: SELECT Salary FROM Employee WHERE EmpNo = 10002;

returns the following result: Salary 35,000.00

a constant

of the form “op y”, where op is a unary (+ or -) or aggregate operator

TITLE(x) is a character representation of that constant. The character representation of the constant could be the identical characters or a normalized form of the constant.

The following statement:

TITLE(x) is ’op’ followed by TITLE(y)

The statement:

SELECT Name, 12 FROM Employee WHERE EmpNo = 10003;

returns 12 as the column heading for the value 12: Name12 Leidner P12

SELECT SUM(Salary) FROM Employee WHERE DeptNo = 700;

returns the name of the aggregate operator (SUM) as part of the column title: Sum(Salary) 113,000.00

Teradata RDBMS for UNIX SQL Reference

5-71

Data Definition TITLE Phrase IF x is . . .

THEN . . .

Example

of the form “y op z”

TITLE(x) is TITLE(y), followed by “op”, followed by TITLE(z).

The statement: SELECT Salary_Loan FROM Employee_Loan WHERE EmpNo = 10004;

returns the headings of the column data for which the computation is performed and the operator: Salary_Loan 41000.00

an attribute function of the form “fcn (y)”

TITLE(x) is fcn(’, followed by TITLE(y), followed by ’).

The statement: SELECT FORMAT (Employee.EmpNo);

returns the following result: FORMAT(EmpNo) ZZZZ9

of the form “y (datadescription)” , where datadescription does not contain a TITLE phrase

TITLE(x) is TITLE(y).

The statement: SELECT YrsExp (BYTEINT) FROM Employee WHERE EmpNo = 10016;

returns the following result: YrsExp 20

of the form “y (datadescription)” , where datadescription contains a TITLE phrase

TITLE(x) is the title that is specified by the clause.

The statement: SELECT Salary (INTEGER, TITLE ’Pay’) FROM Employee WHERE EmpNo = 10018;

returns the following result: Pay 65000

The number of dashes used to define a column’s width is an attribute of BTEQ. You can modify this display by including a FORMAT phrase in the SELECT statement.

5-72

Teradata RDBMS for UNIX SQL Reference

Data Definition Default Control Phrase

Default Control Phrase Introduction

5

5

A default control phrase determines the action to be taken when you do not supply a value for a field. Default controls apply only to the following: ●



Fields defined in CREATE TABLE and ALTER TABLE statements Parameters defined in CREATE MACRO and REPLACE MACRO statements

Default controls are not effective for views and expressions. The following phrases provide default controls: ● ● ●

NOT NULL DEFAULT WITH DEFAULT

The default value for a field is null, unless NOT NULL is specified. Allowing nulls can affect query performance; therefore, consider using NOT NULL unless you intend to have null values in the column. Default values are not effective for views and expressions. The WITH DEFAULT phrase is mutually exclusive with the NULL and the DEFAULT phrase.

NOT NULL Phrase

5

The NOT NULL phrase specifies that the fields of a column must contain a value; they cannot be empty. For example, in the following column definition, the Name column is defined as follows: Name VARCHAR(12) NOT NULL,

The NOT NULL specification ensures that every row in the Employee table contains a value in the Name field. An error is returned if a user attempts to insert row data without a name, or to specify NULL in place of a name. If a NOT NULL phrase is specified for a column in an ALTER TABLE statement, a DEFAULT or a WITH DEFAULT phrase must also be specified unless the table is empty. This ensures that any empty field in that column will be supplied with default values in compliance with the NOT NULL specification.

Teradata RDBMS for UNIX SQL Reference

5-73

Data Definition Default Control Phrase

DEFAULT Phrase

5

The DEFAULT phrase defines the value that is to be inserted in the field when a value is not supplied in an inserted row. The syntax of the DEFAULT phrase is: DEFAULT

constantvalue HH01A091

where constant_value is either a keyword or the value of the default. A default value must be compatible with the data type specified for the column. For example, the phrase INTEGER DEFAULT 3.5 is illegal. A default value must not violate any CHECK constraints specified for the column. If a default value would violate a CHECK constraint, the conflict is not recognized at the time the table is defined or altered. The conflict is recognized the first time that an INSERT or UPDATE attempts to enter a NULL, which would be replaced by the default. A keyword used as a constant_value inserts a string that is already known to the system, such as the date, the time, or the name of the user defining the column. In an ALTER TABLE statement, the DEFAULT phrase can be used with a keyword to override a previously-defined value. In such cases, the data type of the column must agree with the string inserted by that keyword. This form of the DEFAULT phrase, the function performed by each keyword, and the corresponding data types are summarized in Table 5-13. Table 5-13 Values for DEFAULT Phrase Keywords

This form . . .

Inserts the current . . .

Using this data type . . .

DEFAULT DATEa

date

DATE, INTEGER

DEFAULT TIMEb

time

FLOAT

DEFAULT USER

user name

CHAR(n), VARCHAR(n) where n can be length of longest username

a. Flagged as non-ANSI when the SQL flagger is enabled. b. Flagged as non-ANSI when the SQL flagger is enabled.

5-74

Teradata RDBMS for UNIX SQL Reference

Data Definition Default Control Phrase

For example, if the DeptNo column is defined as: DeptNo SMALLINT DEFAULT 100 FORMAT’999’ BETWEEN 100 AND 900,

and the value for the DeptNo field is not provided in an INSERT statement, then the value 100 is inserted automatically. If a column does not have a default value specified by the DEFAULT or WITH DEFAULT phrase, the default for the column is null. No error or warning messages are given if DEFAULT NULL is specified with the NOT NULL phrase in CREATE TABLE or ALTER TABLE ADD statements. You can assign a value, such as a blank, with the DEFAULT phrase as follows: CHAR(1) DEFAULT ’ ’ ...

WITH DEFAULT Phrase

5

The WITH DEFAULT phrase specifies that a system default value is to be inserted in the field when a value is not specified in an INSERT statement. This phrase is flagged as non-ANSI when the SQL flagger is enabled. The value of a system default is determined by the data type defined for the column. The data types and associated system default values are listed in Table 5-14.

Table 5-14 System Values for WITH DEFAULT Phrase

Data Type

System Default

BYTE[(n)]

0 if n omitted, or n binary zeros

BYTEINT

0

CHAR[(n)]

blank if n omitted, or n blanks

DATE

current date

DECIMAL and NUMERIC

0

FLOAT

0

REAL

0

DOUBLE PRECISION

0

INTEGER

0

LONG VARCHAR

’ ’ (null string)

SMALLINT

0

VARBYTE (n)

’ ’ (null string)

VARCHAR(n)

’ ’ (null string)

Teradata RDBMS for UNIX SQL Reference

5-75

Data Definition Default Control Phrase Data Type

System Default

GRAPHIC[(n)]

graphic pad character if n is omitted; otherwise, n graphic pad characters.

VARGRAPHIC(n)

’ ’ (null string)

LONG VARGRAPHIC

’ ’ (null string)

When a CREATE TABLE statement is processed, any WITH DEFAULT phrase is converted to a DEFAULT phrase in which the appropriate system value becomes the default constantvalue.

Example 1

If the EdLev column is defined as: 5

EdLev BYTEINT FORMAT ’Z9’ NOT NULL WITH DEFAULT,

then the column definition for EdLev is stored as: EdLev BYTEINT NOT NULL DEFAULT 0 FORMAT ’Z9’,

Example 2

5

The following INSERT statement adds a row containing a 0 in the EdLev field: INSERT INTO Employee (Name, EmpNo, DeptNo, DOB, Sex, EdLev) VALUES (’Newhire A’, 10025, ’49/10/17’, ’M’,) ;

As is the case for explicit default values, WITH DEFAULT may generate a value that violates some CHECK constraint on the table. The conflict will not be recognized until an INSERT or UPDATE provides a NULL value for the column. Caution:

5-76

Do not use a default value that violates defined constraints.

Teradata RDBMS for UNIX SQL Reference

Data Definition COMPRESS Phrase

COMPRESS Phrase Introduction

5

5

The COMPRESS phrase is flagged as non-ANSI when the SQL flagger is enabled. The COMPRESS phrase allows values in one or more columns of a table to be compressed to zero space. The syntax for the COMPRESS phrase is: COMPRESS NULL

const FF07A066

where const is the value to be compressed. WHEN . . .

THEN . . .

the NULL phrase is used

nulls are compressed. Data for that value would not take much space hence using COMPRESS would not necessarily save much space.

a const is specified

the value are compressed.

COMPRESS is used alone

nulls are compressed to zero space.

If const is specified for a CHAR data type without the UPPERCASE option, a data value must match the case of ’constantvalue’ in order to be compressed.

Example: const CHAR Data Type

For example, if a column named “Doc_Type” is defined as follows: Doc_Type CHAR(6) COMPRESS ’Manual’ 5

then each value must be entered as ’Manual’ in order to be compressed. A value in a different case, such as ’MANUAL’, ’manual’, or ’mANUAL’, is not compressed. COMPRESS can be used with any fixed-length character, byte, or numeric data type. Fixed-length character or byte columns cannot be compressed if they are longer than 255 bytes. As many columns as is practical can be compressed for any table.

Teradata RDBMS for UNIX SQL Reference

5-77

Data Definition COMPRESS Phrase

Only one COMPRESS phrase can be defined per column. WHEN the percentage of compressible values is . . .

AND the column is . . .

THEN . . .

high

large

COMPRESS can be valuable, especially if the table will occupy fewer data blocks due to the shortened rows. This can realize a significant savings in resources as well as space, because accessing a data block consumes a fixed amount of CPU and I/O resources.

low

● ● ●

Achieving Maximum Benefit With COMPRESS

5





5

the increase in access time may offset the savings in space

has a higher column id than other compressed columns

Typically, five columns or less can be compressed without impacting access time. However, maximum benefit is achieved when the COMPRESS phrase is applied to a column under the following conditions: ●

COMPRESS with GRAPHIC Columns

small frequently accessed

The majority of rows contain a compressible value (nulls, zeros, blanks, or constantvalue) in the compressed field. The column with the highest rate of access and the highest percentage of non-compressible values has the lowest column id (relative to other compressed columns). The shortened row length results in the elimination of one or more data blocks.

COMPRESS can be applied to a fixed-length GRAPHIC column that does not exceed 127 logical characters (254 bytes). The value to be compressed is saved internally as character data. To review the compressed value of a GRAPHIC column, apply the CHAR2HEXINT function to the CompressValue column of the DBC.Columns view. For example, assume that the following table was defined during a KanjiEBCDIC session: CREATE TABLE Test_Gcomp (Col1 INTEGER, Col2 GRAPHIC(2) COMPRESS G’’);

5-78

Teradata RDBMS for UNIX SQL Reference

Data Definition COMPRESS Phrase

If the following statement is submitted: SELECT ColumnName, CHAR2HEXINT(CompressValue) FROM DBC.Columns WHERE TableName = ’Test_Gcomp’ and ColumnName = ‘Col2’;

the return displays the hexadecimal representation of the constant to be compressed, as follows: ColumnName -----------------------------Col2

Char2HexInt(CompressValue) -------------------------42C1426C2

If the CHAR2HEXINT function is not applied to the CompressValue column, the graphic compress value is treated as character data and the result is misleading. CHAR2HEXINT is useful for cases when shift-out and shift-in characters are not stored in the character string for KanjiEBCDIC data. For KanjiSHift-JIS or KanjiEUC, this procedure is not required.

Teradata RDBMS for UNIX SQL Reference

5-79

Data Definition Data Type Conversions

Data Type Conversions Introduction

5

A data type conversion modifies the data type of an expression and can be either implicit or explicit. Explicit conversions can be made with the CAST function or with a data definition phrase.

Field Mode: User Response Data

5

In Field Mode, a report format used in BTEQ, all data is returned in character form. The alignment and spacing of columns is controlled by data formats and title information. Each row returned is essentially a character string ready for display. In Field Mode it is unnecessary to convert numeric data to character format. If, in Field Mode, the query includes a character value that has been explicitly converted to numeric, the following rules apply:

Example

5

IF the character value . . .

THEN . . .

does not represent a number

an error is reported.

represents a number but that number is not in the range of declared numeric type

no error is reported, but a set of asterisks is placed in the response row at the point that the numeric value would be displayed

The following example demonstrates how data type conversions are handled for varying values of Field1. T.Field1 is CHAR(6) SELECT Field1(SMALLINT) FROM T1

The range of SMALLINT is -32768 to 32768 ● ● ●

The value ‘31000’ displays normally The value ‘100000’ displays as ****** The value ‘xyz’ causes an error return with no values.

The same query executed in Record or Indicator Variable Mode returns an error for the latter two cases.

5-80

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Data Type Conversions

Forms of Data Type Conversions

The Teradata RDBMS supports three forms of data conversion: ● 5



Implicit Using the CAST function: CAST

( expression

AS

data_definition ) HH01A018



Using a data definition phrase: value

( data definition ) HH01A075

Converting Graphics and Character to Graphics Implicit Conversion

This section discusses the role of implicit and explicit conversion of character to graphic and of graphic data: 5

5

The RDBMS never performs implicit conversions between graphic and other data types. Furthermore, the standard explicit conversion syntax generates an error message. However, from a KanjiEBCDIC session, conversion from character data to graphic data is possible via the VARGRAPHIC function. For example, from a KanjiEBCDIC session, the following syntax is allowed: SELECT VARGRAPHIC (‘string_expr’);

Explicit Conversion

5

Explicit conversion syntax can be used to change the size or nature of graphic data, for example: SELECT CAST (‘42434244’XG AS GRAPHIC(3));

See also “CAST in Data Type Conversion”.

Teradata RDBMS for UNIX SQL Reference

5-81

Data Definition Implicit Type Conversion Rules

Implicit Type Conversion Rules Introduction

Example

5

5

This form of conversion is non-ANSI and is flagged as such if the SQL flagger is enabled. The Teradata RDBMS allows the assignment and comparison of values that cannot be assigned or compared without converting values. The following statement is not valid in ANSI SQL since T1.Yrs is a CHAR(2) value, added to a numeric value, to be assigned to a numeric value, therefore it is flagged if the SQL flagger is enabled. Field Age of T2 is SMALLINT Field Yrs of T1 is CHAR(2)

with character representations of numeric values. UPDATE T2 SET Age = T1.Yrs + 5;

This is not evident in the syntax of the source statement, but becomes evident when the dictionary information for tables T1 and T2 is accessed.

Implicit Conversion Rules

5

SQL performs implicit type conversions on expressions before any operation is performed. The rules for implicit conversion are as follows: ●

Numeric items are converted to the same numeric type before any arithmetic or comparison operation is performed. The result returned is of this same underlying type. Refer to Table 5-3 and Table 5-5 for information about defaults of arithmetic and aggregate results. Conversions performed for numeric expressions are as follows: ●

BYTEINT + DECIMAL(2,1) = DECIMAL(18,1) For example, 1 +1.5 = 2.5



SMALLINT + DECIMAL(3,2) = DECIMAL(18,2) For example, 1000 + 7.08 = 1007.08



INTEGER * DECIMAL(2,1) = DECIMAL(18,1) For example, 60000 * 1.1 = 66000.0



FLOAT * BYTEINT = FLOAT For example, 1.0E6 * 2 = 2.0E6

5-82

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Implicit Type Conversion Rules ●

Comparisons of strings (fixed-length or variable-length) require operands of equal length. The following table shows that the shorter string is converted by being filled in on the right. Expression Converted To Result Is ’x’=’x ’

’xb’=’x ’

True

’x’=’xx’

’xb’=’xx’

False

where b is a blank. ●

If a character string is present in an expression that requires a numeric operand, it is read as a formatted numeric and is converted to a floating-point (FLOAT) value, with an implied FORMAT of: ’-$$$,$$$,$$$.999999E-999’ An explicit FORMAT phrase or TYPE phrase can override this implicit format. A blank or empty character string (zero length) is interpreted as having a numeric value of zero. Expression Converted To 1.1*’$20.00’ 1.1E0*2.0E1 ’2’+’2’ 2.0E0+2.0E0 ’A’ + 2 ----------



Result Is 2.2E1 4.0E0 error

If a column or parameter of numeric data type is specified with a string value, the string is again assumed to be a formatted numeric. For example, the following INSERT statement specifies the Salary as a numeric string: INSERT INTO Employee (EmpNo, Name, Salary) VALUES (10022, ’Clements D’, ’$38,000.00’);

The conversion to numeric type removes editing symbols. When selected, the salary data contains only the special characters allowed by the FORMAT phrase for Salary in the CREATE TABLE statement: Salary 38,000.00

Teradata RDBMS for UNIX SQL Reference

5-83

Data Definition CAST in Data Type Conversion

CAST in Data Type Conversion Introduction

The ANSI SQL syntax for CAST is: 5

CAST

( expression

ansi_sql_data_type )

AS

HH01A020

This can be used to convert data types in ANSI SQL compliant statements, and Teradata SQL statements. The Teradata RDBMS version of this syntax is more general in that it allows both a type declaration and data attribute. CAST

( expression

AS

data_definition ) HH01A018

The function is ANSI SQL compliant only if the data definition consists of an ANSI SQL data type declaration. Refer to the Data Type Declaration options table, Table 5-1, defined under “Data Types”, at the beginning of this chapter.

Using CAST with Character and Graphic Types

5-84

CAST can be used for character and graphic strings.

5

IF the string . . .

THEN . . .

expands

pad it with the pad character.

contracts

Japanese character truncation rules apply.

Teradata RDBMS for UNIX SQL Reference

5

Data Definition CAST in Data Type Conversion

Character and Graphic Truncation Rules

If conversion requires truncation of trailing non-blank characters (or non-zero bytes), an error is reported. 5

The following rules apply to character and graphic strings (including Japanese character and graphic strings): ANSI Mode This type of string . . .

Is truncated of . . .

Character

trailing single byte character spaces to achieve the desired length. Truncation of other characters, or part of a multibyte character, returns an error

Graphic

trailing double-byte zeroes to achieve the desired length

All others

Nothing. Returns an error. If non-pad characters are removed from a string, an error is returned Teradata Mode

Character

trailing single byte character spaces to achieve the desired length. Truncation of other characters, or part of a multibyte character, returns an error.

Graphic

trailing double-byte zeroes to achieve the desired length.

All others

Nothing. Returns an error. If non-pad characters are removed from a string, an error is returned. When truncation occurs but no error is returned, it is because the rules used prior to this release are being supported.

Numeric Overflow, FIELD MODE, and CAST

5

Numeric overflows are handled differently depending on whether you are running ANSI or Teradata mode and whether you are running in FIELD MODE or not. FIELD MODE is not ANSI-compatible. When results for a field value that is a result of a numeric overflow of a numeric or decimal data type are returned in FIELD MODE, what is returned is asterisks (‘***’) rather than an error message. RECORD and INDICATOR modes do not behave in this manner and return an error message.

Teradata RDBMS for UNIX SQL Reference

5-85

Data Definition Explicit Data Type Conversion with a Data Definition Phrase

Explicit Data Type Conversion with a Data Definition Phrase Introduction

The data definition phrase is: 5

value

( data definition ) HH01A075

where data definition includes a data type declaration. and serves to redefine the data. This syntax is flagged as non-ANSI when the SQL flagger is enabled. If data definition includes a data type declaration, then the data is converted at runtime. At that time, a data conversion or range check error may occur. For any kind of data type conversion of the form: col_name

(data_definition) HH01A013

Where the item that includes a data type declaration is an operand of a complex expression, you must enclose the appropriate entities in parentheses, or use the CAST syntax. New applications should use the CAST function to perform conversions to ensure ANSI compatibility.

Example

To evaluate correctly an expression of the form: 5

columnname (integer) + variable

You could enter the expression as follows: (columnname (integer)) + variable

or, preferably as: CAST (columnname AS INTEGER) + variable

See the section “CAST in Data Type Conversion”.

5-86

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Numeric Conversion

Numeric Conversion Introduction

5

5

Numeric conversion is performed on numeric data, producing either a numeric or a character data type result.

Teradata RDBMS for UNIX SQL Reference

5-87

Data Definition Numeric to Numeric

Numeric to Numeric Introduction

5

5

The form for converting numeric data to data of a different numeric type is: CAST

(numeric_expression

or

AS

numeric_data_definition)

numeric_expression (numeric_data_definition) HH01A085

Example 1

5

CAST (IntegerField AS DECIMAL (7,2))

Conversion of numeric to INTEGER, BYTEINT, or SMALLINT truncates any decimal portion. Conversion to DECIMAL produces a rounded result. If a range violation occurs, the operation may fail. Some examples of numeric conversions are: Value 20000.99 20000.99 20000.99 200000

Converted To INTEGER DECIMAL(6,1) DECIMAL(4,1) SMALLINT

Result 20000 20001.0 error error

Although the FORMAT phrase cannot be used to change the underlying data type defined for a column, the phrase may be used to change the display for a numeric value.

Example 2

5

For example, if the field values for columns Wholesale and Retail, both defined as DECIMAL(7,2), are 12467.75 and 21500.50, respectively, the result of the expression: CAST (Wholesale - Retails AS FORMAT ‘-99999’)

is: -09033.

A FORMAT phrase does not affect data that is returned to the client system in Record Mode (client system internal format). In the previous example, the value returned to the client system is still in packed decimal format (for example, -9032.75). Note: The use of FORMAT in CAST is a non-ANSI extension.

5-88

Teradata RDBMS for UNIX SQL Reference

Data Definition Numeric to Character

Numeric to Character Introduction

5

5

The form for converting numeric type data to a character data type is: CAST

(numeric_expression

numeric_expression

or

character_data_definition)

AS

(character_data_definition) HH01A086

The process followed for the CAST function is described in the following table. Stage

Process

1

Convert the numeric value to a character string using the default or specified format for the numeric value.

2

Trim leading and trailing blanks.

3

Extend to the right as required.

4

If truncation of non-blank characters is required, report truncation error.

The CAST operation, differs from the Teradata SQL conversion as follows: Results are left justified. Column display will not be aligned. Truncation of significant data gives an error.

● ●

Use of Teradata syntax i.e., not using CAST) for explicit conversion of numeric to character data, requires caution. The process followed is described in the following table. Stage

Process

1

Convert the numeric value to a character string using the default or specified FORMAT for the numeric value.

2

Leading and trailing blanks are not truncated.

3

Extend to the right with blanks if required, or truncate from the right if required, to conform to the target length specification.

4

If non-blank characters are truncated, no error is reported.

Teradata RDBMS for UNIX SQL Reference

5-89

Data Definition Numeric to Character

Example 1

5

T1.Field1 is INTEGER with the default format ‘10(9)’. The user has values such as 123456, with no values of over 999999. SELECT Field1(CHAR(8)) FROM T1;

returns ‘ 123’ for the value 123456, where the result includes 5 leading blanks and truncates significant digits.

Example 2

Based on the following description of Salary data: 5

Salary (DECIMAL(8,2), FORMAT ’$$$,$$9.99’)

data is converted as illustrated in the following table (b = blank space): Data 20000.00 9000.00 20000.00 9000.00

Conversion Salary(CHAR(10)) Salary(CHAR(10)) Salary(FORMAT’9(5)’) (CHAR (5)) CAST(Salary AS CHAR(10))

Result ’$20,000.00’ ‘b$9,000.00’ ’20000’ ‘$9,000.00b‘

The resultant character string is either extended with blanks or truncated to conform to the given character description. In order for a numeric type value to be converted to a character string, the character description must contain a data type declaration. A FORMAT phrase, by itself, cannot be used to convert a numeric type value to a character type value. The phrase only controls how the resultant value is displayed. If a FORMAT phrase is not included in the character description, then the FORMAT associated with the original numeric value determines how data is displayed. The data definition phrases form of numeric to character conversion uses explicit or default FORMATs to convert to a character representation. It then truncates or pads with blanks, depending what length the character string dictates. This can lead to a loss of significance.

Example 3

5

Suppose EmpNo was defined as SMALLINT with the default format of ‘9(6)’. Suppose a value in EmpNo is 12501. The statement: SELECT EmpNo(CHAR(5)) FROM Employee

returns the ‘1250’, with a leading blank and the low order digit missing. The CAST function used for the same conversion, converts to the character representation of the numeric value, trims leading blanks, and finally truncates or pads on the right, i.e., SELECT CAST (EmpNo AS CHAR(5) ) FROM Employee

returns ‘12501’.

5-90

Teradata RDBMS for UNIX SQL Reference

Data Definition Character Conversion

Character Conversion Introduction

5

5

Character expression conversion is performed on character type data producing either a numeric type data result or a character type data result.

Teradata RDBMS for UNIX SQL Reference

5-91

Data Definition Character to Numeric

Character to Numeric Introduction

5

5

The form for converting character type data to a numeric data type is: CAST

(character_expression

character_expression

AS

numeric_data_definition)

(numeric_data_definition) HH01A087

or character_expression

(numeric_data_definition) HH01A088

Before any processing begins, the system scans the numeric description for a FORMAT phrase with an implied decimal point (V). If a V is found, the number of digits is counted after the V and the number of implied decimal positions is saved. Conversion is performed positionally, character by character, from left to right, until the end of the number. The following list shows the steps for converting character type data to numeric. Conversion is performed step by step, without returning to a previous step; however, steps may be skipped. Step

Action

1

Leading blanks are ignored except when conversion involves signed zoned decimal input. In this case, an error would result (embedded blanks result in an error in all cases)

2

The sign (+ or -) is saved as part of the number.

3

The currency sign is ignored. Only one currency sign is allowed.

4

Digits are saved as the whole part of the number. Commas, slashes, colons, and percent signs are ignored. The allowance of dollar signs, commas, colons, slashes, percent signs and embedded dashes is a non-ANSI extension of character to numeric conversion.

5-92

5

Embedded dashes are allowed, unless the number is signed.

6

The decimal point is saved as the beginning of the fractional part of the number.

7

Digits are saved as the fractional part of the number.

8

The letter E is saved as the beginning of the exponent part of the number. One space is allowed following an E.

Teradata RDBMS for UNIX SQL Reference

Data Definition Character to Numeric Step

Numeric Overflow

5

Action

9

The exponent sign (+ or -) is saved

10

The exponent digits are saved. A sign character cannot appear after any exponent digit.

As noted earlier, in Field Mode, numeric over flow is not treated as an error, but asterisks are displayed in lieu of an unrepresentable number. In Record and Indicator Variable Modes, numeric overflow is reported as an error. This behavior applies to both the CAST and data definition phrase syntax.

FORMAT Phrase Only Controls Resultant Display

5

A FORMAT phrase, by itself, cannot be used to convert a character type value to a numeric type value. The phrase only controls how the resultant value is displayed. Some examples of character to numeric conversion are: Character String

Converted To

Result

’$20,000.00’ ’$$$.50’ ’$.50’ ’.345’ ’-1.234E-02’ ’-1E.-2’ ’- 55’ ’E67’ ’9876’ ’-123’ ’9876’ ’1-2-3’ ’123-’ ’-1.234E 02’

DECIMAL DECIMAL DECIMAL DECIMAL(8,3) FLOAT FLOAT INTEGER FLOAT DECIMAL,FORMAT ’99V99’ INTEGER DECIMAL,FORMAT ’9(2)V9(2)’ INTEGER INTEGER FLOAT

20000.00 error .50 .345 -.01234 error error error 98.76 -123 98.76 -123 -123 -123.4

Teradata RDBMS for UNIX SQL Reference

5-93

Data Definition Character To Character

Character To Character Introduction Form

5

5

The data type of a source character string can be converted by specifying the data type for the target field. The form for such character data type conversions is:

5

CAST

(character_expression

character_expression

AS

character_data_definition)

(character_data_definition) HH01A083

Usage Notes

5

If the source string (either CHAR or VARCHAR) is longer than the target data type (CHAR or VARCHAR), excess characters are truncated. If the session doing an INSERT or UPDATE is in ANSI mode, and non-blank characters would be truncated to store character values in a table, an error is reported. In Teradata mode, no error is reported. IF the source string data type is . . .

Examples

AND the target data type is . . .

AND it is . . .

THEN . . .

CHAR

longer than the target

VARCHAR

any trailing blanks are trimmed.

CHAR or VARCHAR

shorter than the target

CHAR

any trailing blanks are appended to the target.

CHAR

all blanks

VARCHAR

the field is truncated to zero length.

Following are examples of character to character conversions: 5

Character String ’HELLO’ ’HELLO’ ’HELLO’ ’HELLO ’ ’HELLO ’

String Length 5 5 5 7 7

Character Description CHAR(3) CHAR(7) VARCHAR(7) VARCHAR(6) VARCHAR(3)

Conversion Result ’HEL’ ’HELLO ’ ’HELLO’ ’HELLO ’ ’HEL’

Converted Length 3 7 5 6 3

If CAST is used to convert data to a character string and non-blank characters would be truncated, an error is reported.

5-94

Teradata RDBMS for UNIX SQL Reference

Data Definition Graphic Conversion

Graphic Conversion

5

The form for graphic conversion is: CAST

(graphic_expression

graphic_expression

AS

graphic_data_definition)

(graphic_data_definition) HH01A095

If the length specified by graphic_data_definition is less than the length of graphic_expression or graphic_string, graphic characters beyond the specified length are truncated. If graphic_data_definition is fixed-length and the length is greater than that of graphic_string, bytes of value double byte zero are appended as required. For graphic data, the Teradata RDBMS allows conversions only to an identical type (graphic to graphic). Graphic data cannot be converted to any of the following data types: ● numeric ● byte ● character If CAST is used and significant characters are truncated (or for bytes, if non-zero bytes are truncated), an error is reported.

Teradata RDBMS for UNIX SQL Reference

5-95

Data Definition Byte Conversion

Byte Conversion

5

The form for byte conversion is: CAST

(byte_expression

byte_expression

AS

byte_data_definition)

(byte_data_definition) HH01A096

If the length specified by byte_data_definition is less than the length of byte_expression or byte_string, bytes beyond the specified length are truncated. If byte_data_definition is fixed-length and the length is greater than that of byte_expression, bytes of value binary zero are appended as required. For byte data, the Teradata RDBMS allows conversions only to an identical type (byte to byte). Byte data cannot be converted to numeric, graphic, or character data. If CAST is used and non-zero bytes are truncated, an error is reported.

5-96

Teradata RDBMS for UNIX SQL Reference

Data Definition DATE Conversion

DATE Conversion Introduction

5

5

DATE data may be converted to INTEGER, DECIMAL(n,m), or FLOAT. However, when date data is converted to a numeric, the value returned is YYMMDD (or YYYYMMDD in a numeric format. Note: No error is generated on conversion of DATE to DECIMAL or FLOAT. Date data may also be converted to and from character data. A format phrase may be used when converting character data to date data if the specified format matches the input data. When converting date data to a character string, data is returned as specified in the format phrase. If CHARACTER is specified, and the number of digits (n) is too small, characters are truncated.

Example 1

5

To list employees who were born between March 7, 1938, and August 25, 1942, you could specify the date information as follows: SELECT name, dob FROM employee WHERE dob BETWEEN 380307 AND ’082542’(DATE, FORMAT ’mmddyy’) ORDER BY dob;

In this example, the first date string is integer in the form yymmdd and requires no qualification. The second string is in the form mmddyy and must be identified to the system. The result returns the date of birth information as specified for the Employee table: Name Inglis C Peterson J

Example 2

5

DOB Mar 07 1938 Mar 27 1942

To change the date format to an alternate form, change the SELECT to: SELECT name, dob (FORMAT ’yy-mm-dd’) FROM employee WHERE dob BETWEEN 380307 AND ’082542’(DATE, FORMAT ’mmddyy’) ORDER BY dob ;

The format specification changes the display to the following: Name Inglis C Peterson J

DOB 38-03-07 42-03-27

Teradata RDBMS for UNIX SQL Reference

5-97

Data Definition DATE Conversion

Example 3

5

To change the display from date format to integer format, change the statement to: SELECT name, dob(INTEGER) FROM employee WHERE dob BETWEEN 380307 AND 420825 ORDER BY dob ;

or SELECT name, CAST (dob AS INTEGER) FROM employee WHERE dob BETWEEN 380307 AND 420825 ORDER BY dob ;

and the display becomes: Name Inglis C Peterson J

DOB 380307 420327

Notice how the change in format in the WHERE statement simplifies the request.

EXTRACT

5

It is perhaps more useful to get the value of the year, month, or dayof-month as integer values than the encoded date. The function: EXTRACT

(

YEAR

FROM

value )

MONTH DAY

HH01A069

provides the indicated result as an integer. Date, for EXTRACT, must be a value of DATE data type or a literal in the format ‘YYYYMM-DD’. If a literal is used, the validity of the date is checked as part of the function.

5-98

Teradata RDBMS for UNIX SQL Reference

Data Definition Signed Zone Decimal Conversion

Signed Zone Decimal Conversion Introduction

5

5

Teradata SQL can convert input data that is in signed zone (external) decimal format to a numeric data type, thus allowing numeric operations to be performed on row values. The column in which the signed zone decimal data is to be stored may be any numeric data type. A FORMAT phrase incorporating the S sign character filters the data as it passes in and out of the Teradata RDBMS. The rightmost character of the input data string is assumed to contain the zone (overpunch) bit. The characters representing the various zone-numeric combinations are listed in Table 5-15.

Table 5-15 Signed Zoned Decimal Character Set

Last Character (Input String)

Numeric Conversion

Last Character (Input String)

Numeric Conversion

{ A B C D E F G H I

n...0 n...1 n...2 n...3 n...4 n...5 n...6 n...7 n...8 n...9

} J K L M N O P Q R

-n . . . 0 -n . . . 1 -n . . . 2 -n . . . 3 -n . . . 4 -n . . . 5 -n . . . 6 -n . . . 7 -n . . . 8 -n . . . 9

The sign FORMAT phrase can be included in a CREATE TABLE or ALTER TABLE statement when the column is defined, or in the INSERT statement when the data is loaded. The chosen method depends on how the stored value is to be used. When a sign FORMAT phrase is specified at column creation time, it is considered attached to the column because it translates data at the column level; that is, both when the data is loaded and when it is retrieved. If field values are normally retrieved for manipulation as numeric data, the sign FORMAT phrase should not be used at column creation time.

Using FORMAT in CREATE TABLE

When the FORMAT phrase is used in the CREATE TABLE statement, as follows: 5

CREATE TABLE Test1 (Col1 DECIMAL(4) FORMAT ’9999S’);

Teradata RDBMS for UNIX SQL Reference

5-99

Data Definition Signed Zone Decimal Conversion

then zoned input character strings can be loaded with standard INSERT statements, whether the data is defined: INSERT INTO Test1 (Col1) VALUES (’123J’);

or read from an client system data record via the USING modifier: USING Ext1 (CHAR(4)) INSERT INTO Test1 (Col1) VALUES (:Ext1);

The data record contains the string ’123J’. Subsequently, a simple select, such as: SELECT Col1 FROM Test1;

returns: Col1 123J

Using Another FORMAT in the SELECT Statement

5

To override an attached format, another FORMAT phrase is needed in the retrieval statement. Using the preceding table, one of the two following statements must be used to retrieve the numeric value: SELECT Col1 (FORMAT ’+9999’) FROM Test1;

or SELECT CAST (Col1 AS INTEGER) FROM Test1;

The result is as follows. Col1 -1231

If FORMAT is Not Attached to the Column

5

If the format is not attached to the column, the sign FORMAT phrase must be used each time signed zoned decimal data is loaded and each time the row value is to be retrieved in signed zoned decimal format. If a table is defined as: CREATE TABLE Test2 (Col2 DECIMAL(5));

then the sign FORMAT phrase must be included whenever signed zoned decimal strings are inserted, whether defined: INSERT INTO Test2 (Col2) VALUES (’5678B’ (DECIMAL(5), FORMAT ’99999S’)); INSERT INTO Test2 (Col2) VALUES (’9012L’ (DECIMAL(5), FORMAT ’99999S’));

or read from a client system data record: USING Ext2 (CHAR(5)) INSERT INTO Test2 (Col2)

5-100

Teradata RDBMS for UNIX SQL Reference

Data Definition Signed Zone Decimal Conversion VALUES (:Ext2 (DECIMAL(5), FORMAT ’99999S’)); USING Ext2 (CHAR(5)) INSERT INTO Test2 (Col2) VALUES (:Ext2 (DECIMAL(5), FORMAT ’99999S’));

where Ext2 contains the strings ’5678B’ and ’9012L’. Because Col2 does not have an attached FORMAT phrase, a simple select, such as: SELECT Col2 FROM Test2;

returns: Col2 56782. -90123.

A sign FORMAT phrase must be included in the SELECT statement in order To retrieve the values ’5678B’ and ’9012L’. It is important to remember this rule when manipulating signed zoned decimal values, especially when using such facilities as a subquery.

Example

If a column is created as a CHARACTER data type: 5

CREATE TABLE Test3 (Col3 CHAR(5));

and is loaded by selecting, without a sign FORMAT phrase, values from an “unattached” column: INSERT INTO Test3 (Col3) SELECT Col2 FROM Test2 ;

the values are actually inserted as: Col3 5678 -9012

The sign FORMAT phrase must be included in the query specification in order to insert the values ’5678B’ and ’9012L’.

Teradata RDBMS for UNIX SQL Reference

5-101

Data Definition Using CAST in Data Type Conversions

Using CAST in Data Type Conversions Introduction

5

5

Data type conversions can be done using the following non-ANSI syntax: expression

( data_type_list ) HH01A019

The ANSI syntax for data type conversion is: CAST

( expression

AS

data_type ) HH01A021

Teradata RDBMS supports an extension of this ANSI syntax: CAST

( expression

AS

ansi_sql_data_type ) HH01A020

where in the fully compliant form, the SQL-type-list includes only a single data type from the ANSI data types. CAST is flagged as not Entry Level ANSI in the current release, when the SQL flagger is enabled. New SQL scripts should avoid using the type list form in data type conversions. The extended form of CAST should be avoided for any application intended to be ANSI compliant and portable.

Examples Example 1

5

The following examples illustrate the use of CAST in data type conversions: Type List Syntax:

5

WHERE INTEGER_FIELD = SUBSTR(CHAR_FIELD, 3,3)(INTEGER);

CAST Syntax: WHERE INTEGER_FIELD = CAST(SUBSTRING(CHAR_FIELD FROM 3 FOR 3) AS INTEGER);

5-102

Teradata RDBMS for UNIX SQL Reference

Data Definition Using CAST in Data Type Conversions

Example 2

Type List Syntax: 5

SELECT SUBSTR(CHAR_FIELD, 1,2) (INTEGER), SUBSTR(CHAR_FIELD, 3,3)(INTEGER) FROM T1;

CAST Syntax : SELECT CAST(SUBSTRING(CHAR_FIELD FROM 1 FOR 2) AS INTEGER),CAST(SUBSTRING (CHAR_FIELD FROM 3 FOR 3) AS INTEGER) FROM T1;

Teradata RDBMS for UNIX SQL Reference

5-103

Data Definition Attribute Functions

Attribute Functions Introduction

5

Attribute functions return descriptive information about the operand. The operand need not be a column reference; it can be a general expression that is not evaluated mathematically. When an attribute function is used in a request, the response returns one row for every data row that meets the search condition. Note These functions are extensions to ANSI SQL, and will be flagged if the SQL flagger is enabled. Each function is described in the following sections. Attribute functions include: ● ● ● ● ● ● ● ● ●

TYPE TITLE FORMAT NAMED CHARACTERS BYTES MCHARACTERS OCTET_LENGTH CHAR2HEXINT

The CHAR2HEXINT function is described in Chapter 6, “SQL Expressions”, under “String Function: CHAR2HEXINT”. See also Table 5-2 for a list of data type attributes. Each function is described in the following passages.

TYPE Function

5

The TYPE(x) function displays the data type defined for the specified operand (x). Information on range constraints is not returned. The data type is returned as a character string, which is always one of the following: ● ● ● ● ● ● ●

5-104

BYTEINT SMALLINT INTEGER DECIMAL(n,m) FLOAT CHAR(n) BYTE(n)

Teradata RDBMS for UNIX SQL Reference

5

Data Definition Attribute Functions ● ● ● ● ●

GRAPHIC(n) VARCHAR(n) VARBYTE(n) VARGRAPHIC(n) DATE

If, for example, the following TYPE function is used to request the data type defined for the Name and Salary columns of the Employee table: SELECT TYPE(Name), TYPE(Salary) FROM Employee;

the data returned is: TYPE(Name) VARCHAR(12)

TYPE(Salary) DECIMAL(8,2) . . .

If the TYPE function is used to request the data type of three columns, defined as graphic, vargraphic, and long vargraphic, respectively, the result is as follows: TYPE(GColName) TYPE(VGColName) GRAPHIC(n) VARGRAPHIC(n)

TYPE(LVGColName) VARGRAPHIC(n)

In the case of a long vargraphic column, the length returned is the current maximum length of VARGRAPHIC (16,000).

TITLE Function

5

The TITLE(x) function returns a character string for the corresponding attribute of the specified operand (x). The data type is returned as a variable character string of up to 60 characters. In the following example, the TITLE function returns the title defined for the Name column in the Employee table: SELECT TITLE(Name) FROM Employee;

The returned data is: TITLE(Name) Name . . .

FORMAT Function

5

The FORMAT(x) function returns a character string for the corresponding attribute of the specified operand (x). The data type returned by the FORMAT(x) phrases is a variable character string of up to 30 characters.

Teradata RDBMS for UNIX SQL Reference

5-105

Data Definition Attribute Functions

Example

5

This selects the FORMAT for the Salary column in the Employee table: SELECT FORMAT(Salary) FROM Employee;

The result is: FORMAT(Salary) ZZZ,ZZ9.99 . . .

In the following example, the NAMED clause specifies a column name as part of the row information to accompany data which is returned: SELECT empno, salary, NAMED(Salary) FROM employee WHERE empno = 10019;

The result is: EmpNo 10019

NAMED Function

5

Salary 28,600.00

Named(Salary) Salary

In the following example, the NAMED clause specifies a column name as part of the row information to accompany data which is returned: SELECT empno, salary, NAMED(salary) FROM employee WHERE empno = 10019 ; EmpNo 10019

CHARACTERS and BYTES Functions

CHARACTERS Function

5

5

Salary 28,600.00

Named(Salary) Salary

The CHARACTERS and BYTES attribute functions operate on an individual data value in the form of a character string or a byte string. Each function returns an integer value equal to the number of characters or bytes contained in the (edit) operand string. The following statement applies the CHARACTERS function to the Name column, which is type VARCHAR, to obtain the number of characters in each employee name: SELECT Name, CHARACTERS(Name) FROM Employee;

The result is as follows (note that separator blanks are considered characters): Name Smith T Newman P Omura H .

Characters(Name) 7 8 7 .

Because trailing blanks are considered characters, the length of the value in a fixed-length column is always equal to the length defined for the column.

5-106

Teradata RDBMS for UNIX SQL Reference

Data Definition Attribute Functions

The length of the value in a variable-length column is always equal to the number of characters, including any trailing blanks, contained in that value. If you do not want trailing blanks included in the character count for a data value, use the TRIM function to suppress the trailing blanks in a value. The form for using TRIM with the CHARACTERS function is: SELECT

CHARACTERS

(TRIM (TRAILING FROM expression)) ; HH01A073

CHARACTERS can be abbreviated as CHARACTER, CHARS, or CHAR.

BYTES Function

5

To use the TRIM function with a byte data value, substitute BYTES for CHARACTERS in the preceding example. BYTES can be abbreviated as BYTE.

CHARACTERS and BYTES Functions for Japanese Character Sites

5

The data value for the CHARACTERS function can be a character or graphic string on a Japanese character supported site. The operation of the CHARACTERS function depends on the character set of the current session, as follows:

FOR this character set . . .

AND this data type . . .

AND this mode . . .

CHARACTERS returns . . .

KanjiEBCDIC

Graphic

All

the length of string_expr as the number of logical characters. Any trailing graphic pad characters are included in the result count.

Character

Teradata

returns the length of string_expr as the number of bytes. A mix of single and multibyte characters is expected. If any Shift-Out/Shift-In characters are present, they are included in the result count.

ANSI

logical characters.

Teradata RDBMS for UNIX SQL Reference

5-107

Data Definition Attribute Functions FOR this character set . . .

AND this data type . . .

AND this mode . . .

CHARACTERS returns . . .

KanjiEUC KanjiShift-JIS

Graphic

All

the length of string_expr as the number of logical characters. Any trailing graphic pad characters are included in the result count. Note the unusual result given in the first example in Table 5-17

Character

the length of string_expr as the number of logical characters, based on the character set of the current session. A mix of single and multibyte characters is expected.

ASCII EBCDIC

Graphic

All

the length of string_expr as the number of logical characters. Any trailing graphic pad characters are included in the result count.

Character

the length of string_expr as the number of bytes.

The following tables show examples of the count returned by the CHARACTERS function under each type of Kanji character set3. Table 5-16 KanjiEBCDIC CHARACTERS(string_expr)

string_expr

3

5-108

Data Type

Result

ABC

Graphic

3

De

Character

10

Character

4

< is Shift-Out, > is Shift-In, bold = double byte

Teradata RDBMS for UNIX SQL Reference

Data Definition Attribute Functions Table 5-17 KanjiEUC - CHARACTERS(string_expr)

string_expr

Table 5-18 KanjiShift-JIS CHARACTERS(string_expr)

MCHARACTERS Function

Data Type

Result

ss3C ss3D

Graphic

3 (improper result, not reported as an error)

ss3C ss3D

Character

2

dA ss2B ss3E

Character

4

string_expr

Data Type

Result

ABC

Graphic

3

DeF

Character

3

The MCHARACTERS function has the following for 5

MCHARACTERS

(string_expr ) HH01A092

The MCHARACTERS function operates exactly like the CHARACTERS function except for the KanjiEBCDIC character set. Under KanjiEBCDIC, this function performs as follows: ●





The length of string_expr is returned as the number of logical characters. Any Shift-Out/Shift-In characters are not included in the result count. Any trailing graphic pad characters are included in the result count.

The following table shows example results of the MCHARACTERS function under a KanjiEBCDIC character set. Table 5-19 KanjiEBCDIC MCHARACTERS(string_expr)

string_expr

Data Type

Result

ABC

Graphic

3

De

Character

5

Character

0

Teradata RDBMS for UNIX SQL Reference

5-109

Data Definition Attribute Functions

OCTET_LENGTH Function

The OCTET_LENGTH function has the following form: OCTET_LENGTH

(string_expr )

5

HH01A093

This function accepts either character or graphic data and returns the corresponding number of bytes. The length of the string is in bytes for both mixed SBC/MBC and graphic strings. Any Shift-Out/Shift-In and trailing graphic pad characters are included in the result count. This function is supported in the same manner in both Teradata and ANSI modes. Examples of output from the OCTET_LENGTH function are given in the following table4. Table 5-20 Results from OCTET_LENGTH (stringexpr)

Character Set

4

5-110

Data Type

string_expr

Result

EBCDIC

Character

abcdefgh

8

ASCII

Character

abcdefgh

8

KanjiEBCDIC

Character

ABP

11

KanjiEBCDIC

Graphic

MNOP

8

KanjiEUC

Character

dA ss2B ss3E

8

KanjiShift-JIS

Character

DeF

5

KanjiShift-JIS

Graphic

ABC

6

< is Shift-Out, > is Shift-In, bold = double byte

Teradata RDBMS for UNIX SQL Reference

Data Definition Hash-Related Expressions

Hash-Related Expressions

5

Hash-related expressions are expressions invoking hash functions as defined below. Hash functions allow you to extract some statistical properties of the current primary index, or to evaluate these properties for other columns to determine their suitability as a future primary index. These statistics also help minimize hash synonyms and enhance the uniformity of data distribution. Hash functions are valid within a Teradata SQL statement where other functions, like SUBSTR or INDEX, can occur. The four hash functions listed below are described in the following passages: ● ● ● ●

HASHROW HASHBUCKET HASHAMP HASHBAKAMP

Several error messages are related to hash functions. See the Teradata RDBMS for UNIX Messages Reference, for these messages.

Teradata RDBMS for UNIX SQL Reference

5-111

Data Definition HASHROW Function

HASHROW Function Introduction Form

5

5

5

The HASHROW function returns the rowhash value of a given sequence of expressions in BYTE(4) data type. The HASHROW function has the following form, where expr is any expression that can appear in the expression list of the select clause of the SELECT statement. Typically, expr list is a comma-separated list of column names that make up the index under investigation. , HASHROW

(

expression

) HH01A025

Usage Notes

5

If you use the HASHROW function without any arguments, HASHROW(), it will return WITH FFFFFFFF. If the arguments of HASHROW result in a NULL value, then HASHROW will return with 00000000. There are a maximum of 4,294,967,295 hash codes available in the system, and they are in the range of 0 to FFFFFFFF. SELECT HASHROW ( ), as in the following example, returns the last hash code in the system, which is “FFFFFFFF”: SELECT HASHROW ( ) ;

Example 1

5

The following example returns the average number of rows per row hash where columns C1 and C2 constitute the primary index of the table T: SELECT COUNT (*) (FLOAT) / COUNT (DISTINCT HASHROW (C1,C2), FROM T:

If columns C1 and C2 qualify for a unique index, this example returns the average number of rows with the same hash synonym.

Example 2

5

The next example evaluates the efficiency of changing the decimal format of a numeric field to eliminate synonyms. Assume that columns C1 and C2 are declared as DECIMAL(2,2). You can determine the effect of reformatting the columns to DECIMAL(8,6) and DECIMAL(8,4) on hash collisions by submitting these two queries: SELECT COUNT (DISTINCT C1(DECIMAL(8,6)) || C2 (DECIMAL(8,4)) FROM T; SELECT COUNT (DISTINCT HASHROW (C1(DECIMAL(8,6)), C2 (DECIMAL(8,4))) FROM T;

5-112

Teradata RDBMS for UNIX SQL Reference

Data Definition HASHBUCKET Function

HASHBUCKET Function Introduction

Form

5

5

The HASHBUCKET function returns the bucket number that corresponds to a hashrow. The bucket number is an integer data type. The HASHBUCKET function has the following form:

5

HASHBUCKET

( expression ) HH01A026

where expr, is a byte (4) data type expression.

Usage Notes

5

You can call the HASHBUCKET function without any argument, for instance, HASHBUCKET( ). In this case it will return with the maximum hash bucket, which is 65,536. Hash buckets range from 0 to 65,536. If you call a HASBUCKET function with a NULL argument, for example, HASHBUCKET (NULL), the function returns the NULL value. HASHBUCKET (HASHROW (NULL)) returns the 0 hash bucket.

Example 1

5

The following example returns the number of rows in each hash bucket where C1 and C2 are to be the primary index of T: SELECT HASHBUCKET (HASHROW (C1,C2)), COUNT (*) FROM T GROUP BY 1 ORDER BY 1;

Example 2

5

The results of the following example can be treated as a histogram of the table distribution among the AMPs: SELECT HASHAMP (HASHBUCKET (HASHROW (C1, C2))), HASHBUCKET (HASHROW (C1,C2)) FROM T GROUP BY 1,2 ORDER BY 1,2 ;

Teradata RDBMS for UNIX SQL Reference

5-113

Data Definition HASHAMP Function

HASHAMP Function Introduction Form

5

5

The HASHAMP function returns the identification number of the primary AMP corresponding to a hash bucket. The HASHAMP function has the following form, where expr is any expression that can appear in the expression list of the select clause of the SELECT statement, and is an integer: HASHAMP

( expression ) HH01A027

Usage Notes

5

If you specify a type other than INTEGER for an argument of the HASHAMP function, you get the following run-time error: Invalid type of argument for HASHAMP function.

If the data does not evaluate to an integer value between zero and 65535, you get the following error: Invalid value used as an argument for HASHAMP function.

In case of error, check the data against the function being used. You can call the HASHAMP function without any argument, for instance, HASHAMP( ). Without an argument, the function returns one less than the maximum number of AMPs on the system. If you call a HASHAMP function with a NULL argument, for example, HASHAMP(NULL), the function returns NULL.

Example 1

5

The following example assumes table T with one integer column B populated with integer numbers from zero to the maximum number of hash buckets on the system: SELECT B, HASHAMP (B) FROM T ORDER BY 1;

This query returns the distribution of the primary buckets among the AMPs.

Example 2

5

Another example returns the number of rows on each AMP where C1 and C2 are to be the primary index of T: SELECT HASHAMP (HASHBUCKET (HASHROW (C1,C2))), COUNT (*) FROM T GROUP BY 1 ORDER BY 1;

5-114

Teradata RDBMS for UNIX SQL Reference

5

Data Definition HASHBAKAMP Function

HASHBAKAMP Function Introduction Form

5

5

5

The HASHBAKAMP function returns the identification number of the fallback AMP corresponding to a hash bucket. The HASHBAKAMP function has the following form, where expression is any expression that can appear in the expression list of the select clause of the SELECT statement: HASHBAKAMP

( expression ) HH01A028

Usage Notes

5

If you specify a type other than INTEGER for an argument of the HASHBAKAMP function, you get the following run-time error: Invalid type of argument for HASHBUCKET function.

You can call the HASHBAKAMP function without any argument, for instance, HASHBAKAMP( ). Without an argument, the function returns one less than the maximum number of fallback AMPs on the system. If you call a HASHBAKAMP function with a NULL argument, for example, HASHBAKAMP(NULL), the function returns NULL.

Example

5

The following example assumes table T with one integer column B populated with integer numbers from zero to the maximum number of hash buckets on the system: SELECT B, HASHBAKAMP (B) FROM T ORDER BY 1;

This query returns the distribution of the fallback buckets among the AMPs.

Teradata RDBMS for UNIX SQL Reference

5-115

Data Definition HASHBAKAMP Function

5-116

Teradata RDBMS for UNIX SQL Reference

SQL Expressions

Chapter 6

SQL Expressions

Teradata RDBMS for UNIX SQL Reference

SQL Expressions

Teradata RDBMS for UNIX SQL Reference

SQL Expressions About This Chapter 6

About This Chapter

6

This chapter describes the following types of expressions: ● ● ●

● ●



● ● ● ●

Arithmetic operators Arithmetic functions Date and time expressions (EXTRACT function, ADD_MONTHS function) Aggregate operators Character string expressions ● Concatenation operator ● String functions ● SUBSTRING and SUBSTR ● SUBSTR with KanjiEUC and KanjiEBCDIC ● MSUBSTR ● INDEX ● MINDEX ● TRIM ● VARGRAPHIC ● CHAR2HEXINT ● UPPER Logical Expressions ● Comparison operators ● IN and NOT IN ● IS NULL IS NOT NULL ● LIKE operator ● Japanese character site LIKE operator ● EXISTS Conditional CASE Built-in values Indicator values in Arithmetic Expressions

Teradata RDBMS for UNIX SQL Reference

6-1

SQL Expressions About This Chapter

Expressions allow you to perform arithmetic and logical operations, and to generate new values or Boolean results from constants and stored values. An expression can consist of a: ● ● ●

Column name Constant Combination of column names and constants connected by operators.

The following are examples of expressions: ’Test Tech’ Employee.Name Salary * 12 + 100 Salary / 52 > 500 Salary > 50000 OR Age > 60

6-2

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Arithmetic Operators

Arithmetic Operators Introduction

6

The Teradata RDBMS uses the following arithmetic operators: 6

Operator

Function

**

exponentiate

*

multiply

/

divide

MOD

modulo (remainder). The MOD operator calculates the remainder in a division operation. For example, 60 MOD 7 = 4: 60 divided by 7 equals 8, with a remainder of 4. The result takes the sign of the dividend, thus: -17 MOD 4 = -1 -17 MOD -4 = -1 17 MOD -4 = 1 17 MOD 4 = 1

+

add

-

subtract

+

positive value

-

negative value

Note: ** and MOD are Teradata RDBMS extensions to ANSI SQL. They will be flagged when the SQL flagger is enabled The data type of the result of an arithmetic expression is a function of the data types of the two operands. Operands are converted to the result type before the operation is performed. For example, before an INTEGER value is added to a DECIMAL(5,2) value, the INTEGER value is converted to DECIMAL(18,2), the data type of the result.

Binary Arithmetic Operators

6

Table 6-1 shows the result data type for the binary arithmetic operators (*, /, +, and -). Note that the result data type for the MOD operator is the same as that for the divide (/) operator; that for VARCHAR(n) the same as that for CHAR. The result data type for the exponentiation operator (**) is always FLOAT.

Teradata RDBMS for UNIX SQL Reference

6-3

SQL Expressions Arithmetic Operators Table 6-1 Binary Arithmetic Result Data Types

Int (1) Int

Dec(m,n)

Flt Datea

CHAR(n)

Dec(k,j)

Flt (1)

Date*

CHAR(n)

INTEGER

Dec(18,j)

Flt

+ Date (2) - Date (5) * or / Int (6)

Flt (4)(7)

± Dec(18,n)

± Dec (min (18, (1+max (n,j)

Flt

+ Date (3) - Date (5)

Flt (4)(7)

* Dec(m,n)

* Dec (min (18,m+k), (n+j))

/ Dec(m,n)

/ Dec (18,max (n,j))

Flt

Flt

Flt

Flt (6)

Flt (4)(7)

± Date (2)

± Date (3) (6)

Flt

- Int

Flt (6)(7)

* or / Int (6)

* or / Dec(18,j) (6)

Flt (3)(7)

Flt (3)(7)

+max (m-n,k-j))), max (n,j))

* or / Dec(18,n) (6)

+ * / (6) Flt (3)(7)

Flt (6)(7)

Flt (4)(7)

a. The DATE is not the ANSI SQL DATE.

Notes

6-4

1

Int refers to BYTEINT, SMALLINT, or INTEGER. Flt refers to FLOAT.

2

If the value of a date result is not in the range of values allowed for the DATE type, an error is reported. The range is any date on the Gregorian calendar from year 1 to year 3500.

3

Fractions of decimal values are truncated when added to or subtracted from date values (note 2 also applies).

4

The character string is converted to a floating value. If the string does not represent a single numeric value, an error is reported.

5

Error

6

These operations on DATE will not report an error, but results are in general, not meaningful.

7

If an argument of an arithmetic operator is a character string, the first action is to attempt a conversion of the characters to a numeric value. If this conversion fails, an error is reported.

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions Arithmetic Operators

An error is reported if: ● ● ●

Division by zero is attempted The numeric range is exceeded The exponentiation operator is used with a negative left argument and a right argument that is not a whole number

Arithmetic operations cannot be applied to GRAPHIC operands. When computing an expression, decimal results that are not exact are rounded, not truncated. Integer division yields whole results, truncated toward zero.

Arithmetic Expression: Format

Example

The format of an arithmetic expression is the same as the default format of the result data type. 6

Note: FORMAT is relevant only in FIELD Mode BTEQ applications.

6

Assume that you are to raise the salary for each employee in department 600 by $200 for each year spent with the company (up to a maximum of $2500 per month). To determine who is eligible, and the new salary, enter the following statement: SELECT name, (salary+(yrsexp*200))/12 AS projection FROM employee WHERE deptno = 600 AND projection < 2500 ;

This statement returns the following response: Name -------Newman P

Projection ---------2483.33

In the statement, parentheses are used to cause the operation YrsExp * 200 to be performed first. Its result is then added to salary and the total is divided by 12. In all cases, SQL performs ● ●

Arithmetic expressions from left to right Exponentiation, multiplication and division before addition and subtraction in any expression

Therefore, the parentheses enclosing the dividend are not strictly necessary, but if no parentheses were used in this expression, the operation yrsexp * 200 would be divided by 12 and the result added to Salary, producing an erroneous value.

Teradata RDBMS for UNIX SQL Reference

6-5

SQL Expressions Arithmetic Operators

Note the use of AS projection

This is the ANSI compatible alternative to (NAMED projection)

phrase in this statement. It associates the arithmetic expression (Salary + (YrsExp * 200)/12) with Projection. This enables you to refer to the expression by this name in the WHERE clause, rather than type the entire expression again. The result is formatted without a comma separating thousands from hundreds. To specify a comma (or other formatting characters) in such a result, include a FORMAT phrase in the SELECT statement.

6-6

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Arithmetic Functions

Arithmetic Functions Introduction

6

6

The Teradata RDBMS provides the following arithmetic functions that require numeric arguments: Function

Result

ABS (arg)

Absolute value

EXP (arg)

Raises e to the power of arg (e**arg)

LOG (arg)

Base 10 logarithm

LN (arg)

Natural logarithm; (e = 2.71828182845905)

SQRT (arg)

Square root

NULLIFZERO (arg)

Returns NULL if arg is zero; else arg

ZEROIFNULL (arg)

Returns zero is arg is NULL; else arg

If the argument is not numeric, it is converted to a numeric value. If it cannot be converted, an error is reported. Arithmetic functions cannot be applied to GRAPHIC operands. The above arithmetic functions will be flagged as not ANSI SQL, when the SQL flagger is enabled.

Teradata RDBMS for UNIX SQL Reference

6-7

SQL Expressions Arithmetic Functions

Examples of Arithmetic Functions and Their Results

Result Data Types

Representative arithmetic function expressions and their results are:

Expression

6

Results

ABS(-12)

12

ABS(’23’)

2.30000000000000E+001

EXP(1)

2.71828182845905E+000

EXP(0)

1.00000000000000E+000

LOG(50)

1.69897000433602E+000

LOG(100)

2.00000000000000E+000

LN(2.71828182845905)

1.00000000000000E+000

LN(0)

Error

SQRT(2)

1.41421356237309E+000

SQRT(-2)

Error

NULLIFZERO(6)

6

NULLIFZERO(0)

Null

ZEROIFNULL(NULL)

0

ZEROIFNULL(6)

6

Arithmetic functions have the following result data types: 6

Function

6-8

Result Data Type

ABS

Same as arg

EXP

FLOAT

LOG

FLOAT

LN

FLOAT

SQRT

FLOAT

NULLIFZERO

Same as arg

ZEROIFNULL

Same as arg

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Arithmetic Functions

Some Typical Error Messages

Executing arithmetic functions may result in the following errors: 6

Function

Error

EXP (arg)

Numeric overflow

LOG (arg)

For zero or negative arg

LN (arg)

For zero or negative arg

SQRT (arg)

For negative arg

If arg is a character string that represents a single numeric value, it is converted to a numeric value of the FLOAT data type before the function is executed. If a character string arg does not represent a single number, an error is reported. The default title for the arithmetic functions, for example, ABS, EXP, LOG, LN, or SQRT is: functionname(arg)

NULLIFZERO

6

The primary use of the NULLIFZERO function is to avoid problems in division by zero. For example, the expression 6/x or 6/exp produces an error if the value of x or exp is zero. The expression 6/NULLIFZERO(x) or 6/NULLIFZERO(exp) produces a null result (no error), which is not a division by zero and hence not considered an error. NULLIFZERO returns the actual value of a numeric column if the value is nonzero, or NULL if the value is NULL or zero. The following request: SELECT empno, NULLIFZERO(hcap) FROM employee WHERE empno = 10019 ;

produces a null in the second column because the HCap field value for Newman is zero. In BTEQ this displays as a ‘?’.

Teradata RDBMS for UNIX SQL Reference

6-9

SQL Expressions Arithmetic Functions

ZEROIFNULL

6

ZEROIFNULL provides a way of avoiding a null result in a case where it would create an error. ZEROIFNULL returns the value of the data in a field if the data is NON-NULL, or a zero if the value is NULL or zero. In the following example, you can test the Salary column for null: SELECT empno, ZEROIFNULL(salary) FROM employee ;

A non-zero value is returned for each employee number, indicating that no nulls exist in the Salary column. See also CASE, NULLIF, and COALESCE for additional expressions involving checks for NULL values.

6-10

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Date and Time Expressions

Date and Time Expressions Introduction

6

6

Teradata SQL provides a data type for date values and provides for storage of time values as encoded numbers, stored in REAL data type. Since both Date and Time are encoded values, not simple integers or real numbers, arithmetic operations on these values are restricted.

Date and Integer Arithmetic

The following arithmetic functions can be performed with date and an integer: 6

Date + Integer (integer is interpreted as a number of days) ● Integer + Date ● Date - Integer These expressions are not processed as simple addition or subtraction. The process is as follows: ●

Stage

Date and Date Arithmetic

Process

1

The encoded date value is converted to an intermediate value which is the number of days since some fixed date

2

The integer value is then added or subtracted, forming another value as number of days, since the fixed base date

3

The result is converted back to a date, valid in the Gregorian calendar.

The Date - Date expression is not processed as a simple subtraction. 6

The process is as follows the encoded date values are converted to intermediate values which are each the number of days since a fixed date. The second of these values is then subtracted from the first, giving the number of days between the two dates. Other arithmetic operations on date values may provide results, but those results are not meaningful.

Teradata RDBMS for UNIX SQL Reference

6-11

SQL Expressions Date and Time Expressions

Example

Date/2 provides an integer result, but the value has no meaning. 6

There are no simple arithmetic operations that have meaning for time values. The reason is that a time value is simply a real number with time encoded as: (HOUR*10000 + MINUTE*100 + SECOND)

where SECOND may include a fractional value.

6-12

Teradata RDBMS for UNIX SQL Reference

SQL Expressions The EXTRACT Function

The EXTRACT Function Introduction

Syntax

6

6

Teradata SQL supports a subset of the ANSI SQL EXTRACT function. Full ANSI SQL EXTRACT allows the extraction of any field contained in any DateTime or INTERVAL value. We support EXTRACT of YEAR, MONTH, or DAY from Teradata date value, or HOUR, MINUTE, or SECOND from a Teradata REAL value, which contains a time as Teradata currently encodes time. The syntax for extracting the year, month, or day is:

6

EXTRACT

(

YEAR

FROM

date_value )

MONTH DAY

Example1

6

HH01A072

IF the date_value is . . .

THEN . . .

given as a character string literal

the literal must be of the form ‘YYYY-MM-DD’.

not a character string literal

it must be the value of an expression that results in a Teradata DATE data type.

NULL

the result is NULL.

not NULL

the result is an integer.

The following example returns the year, as an integer, from the current date. EXTRACT (YEAR FROM DATE)

Example 2

6

Assuming PurchaseDate is a DATE field, this example returns the month of the date value formed by adding 90 days to PurchaseDate as an integer. EXTRACT (MONTH FROM PurchaseDate+90)

Example 3 Example 4

The following returns 12 as an integer. 6

EXTRACT (DAY FROM ‘1996-12-12’)

6

This example returns an error, found by trying to evaluate the character string literal as a date. EXTRACT (DAY FROM ‘1996-02-30’)

Teradata RDBMS for UNIX SQL Reference

6-13

SQL Expressions The EXTRACT Function

Example 5

6

The following returns an error, found by trying to evaluate the character string literal as a date. In this case, the reason is that EXTRACT uses the ANSI format for recognizing a date as a character string literal. EXTRACT (DAY FROM ‘96-02-15’)

Except for character string literals, if the argument for this form of EXTRACT is a value of the Teradata RDBMS type DATE, the value contained is warranted to be a valid date, for which EXTRACT cannot return an error.

EXTRACT HOUR, MINUTE, SECOND From a REAL Value

The syntax for extracting the hour, minute, or second is: EXTRACT

(

HOUR

FROM

real_value )

MINUTE 6

SECOND

HH01A070

The current release of Teradata RDBMS does not support TIME as a separate data type, but does allow the storage of time values encoded in REAL values. The encoding is (HOUR*10000 + MINUTE*100 + SECOND)

where SECOND is carried to two decimal places. Current time is retrieved as the system value TIME, to the indicated precision. Externally created values of time can be appropriately encoded and stored in a REAL column, to any desired precision as long as the encoding created a value representable by REAL without precision loss. Since TIME is not a true data type in the current Teradata RDBMS, there is no warranty that a value passed to this form of EXTRACT contains a valid time. EXTRACT of HOUR, MINUTE, or SECOND, may thus return an error detectable only at execution time. If the real_value is NULL, the result is NULL.

Example 1 Example 2

6-14

This example returns the hour, as an integer, from the current time. 6

EXTRACT (HOUR FROM TIME)

6

The following is not reliable because TIME + 50 means add 50 seconds to the current time, using real arithmetic. If the seconds field has a value greater than 09, say 37, the result will be 87 in that field, which is invalid. In this case, the value of (TIME + 50) is recognizably invalid and an error will be returned. If the seconds field has a value greater than 49, say 53, the result is 03 in that field, with the minute field incremented by one. In this case, the value of

Teradata RDBMS for UNIX SQL Reference

SQL Expressions The EXTRACT Function

(TIME + 50) looks like a valid time and no error will be detected, but the result is incorrect. The error here is not in EXTRACT, but in assuming that arithmetic operations on Teradata RDBMS time values are supported. They are not. EXTRACT (SECOND FROM TIME + 50)

Example 3

6

If the argument is a character literal formatted as a time value, it is converted to REAL and processed. In this example, 59 is returned. EXTRACT (MINUTE FROM ‘23:59:17.3’)

Example 4

6

The following example returns the seconds as DECIMAL(8,2). This choice of precision is based on the fact that system time on the Teradata RDBMS is measured in centiseconds. EXTRACT (SECOND FROM TIME)

Teradata RDBMS for UNIX SQL Reference

6-15

SQL Expressions ADD_MONTHS Function

ADD_MONTHS Function Introduction

Syntax

6

As noted previously, days can be added or subtracted from a Date value and the operation carries out processes to provide a correct result. A common need, however is to add or subtract a number of months or years to a date, and the peculiarity of the Gregorian Calendar is such that adding 90 days for 3 months, or 730 days for 2 years is not quite right. The addition of a function to add or subtract a number of months resolves this, directly, for months, and indirectly, for years, since a year is unambiguously 12 months. The syntax for the ADD_MONTHS function is:

6

ADD_MONTHS

( date_expr, integer_expr ) HH01A071

One issue on month intervals is that of handling a target month with fewer days than the month in the start date.

Example 1

6

The result of the following is a date in February, 1996, and if 31 were a valid day in February, there would be no problem. The result, when the day value in the source date is higher than the last day value for the target month, is the last day value for the target month. ADD_MONTHS (‘1995-12-31’, 2)

In the example above, the result is ‘1996-02-29’. Note the leap year, if the interval was 14, the result would be ‘1997-02-28’. ●



Example 2 Example 3

6-16

If either argument of ADD_MONTHS is NULL, the result is NULL. If the result would not be in the range ‘0000-01-01’ to ‘3500-1231’, an error is reported.

To get the current date plus 13 years: 6

ADD_MONTHS (DATE, 12*13)

To get the date 6 months ago: 6

ADD_MONTHS (DATE, -6)

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions Aggregate Operators

Aggregate Operators Introduction

6

6

The aggregate operators listed in Table 6-2 can be used in arithmetic expressions. An aggregate operator is used to perform a computation for the values in a group. The group used is the entire table unless a GROUP BY clause is specified.

Table 6-2 Aggregate Operators

Aggregate Operator Rules

Operator

Syntax

Abbreviation

SUM

SUM([DISTINCT] numeric)

AVERAGE

AVG ([DISTINCT] numeric)

COUNT

COUNT([DISTINCT] value) COUNT(*)

MINIMUM

MINIMUM(expr)

MIN

MAXIMUM

MAXIMUM(expr)

MAX

AVE or AVG

The aggregate operator rules are listed below: 6





It is legal to apply AVERAGE, MINIMUM, MAXIMUM, or COUNT to Date, but it is not legal to do SUM(Date). All aggregate operators can be applied to numeric data. For example, the following SELECT statement can be used to calculate the average salary, and minimum and maximum number of years of experience for employees in each department. SELECT DeptNo, AVG(Salary), MIN(YrsExp), MAX(YrsExp) FROM Employee GROUP BY DeptNo ORDER BY DeptNo;

The result returned is: DeptNo

Average (Salary)

Minimum (YrsExp)

Maximum (YrsExp)

100

36,100.00

3

15

300

47,666.67

12

25

500

38,285.71

4

20

600

36,650.00

6

11

700

37,666.67

5

10

Teradata RDBMS for UNIX SQL Reference

6-17

SQL Expressions Aggregate Operators ●

MIN, MAX, and COUNT operate on character as well as numeric data; MIN may be used to return the lowest sort order of a character expression, MAX the highest. For example: SELECT MIN(Name), MAX(Name) FROM Employee;

returns the following result: Minimum(Name) Aguilar J ●



Maximum(Name) Watson L

Aggregate operators are normally used in the expression list of a SELECT statement and in the summarylist of a WITH clause. If an aggregate operation is used on an expression that represents zero elements, the following results are returned: Aggregate Type COUNT(expr) WHERE... All except COUNT(expr) WHERE... All(expr)...GROUP BY,HAVING....



Returned 0 Null No Record Found

Aggregate operations can not be nested, for example: AVG(MAXIMUM (Salary))

Aggregates Used in WHERE and HAVING Clauses

Aggregates can be used in the following types of clauses: ●

6



Graphics

DISTINCT Option

6

6

The WHERE clause of an ABORT statement, to specify an abort condition. An aggregate operator can not be used in the WHERE clause of a SELECT statement. A HAVING clause, to specify a group condition.

The MINIMUM (MIN), MAXIMUM (MAX), and COUNT operators can be applied to graphic columns. MIN and MAX return the minimum or maximum value of a character or graphic expression, based on the rules of comparison discussed under Logical Expressions. The DISTINCT option specifies that duplicate values not be used when an expression is processed. SELECT COUNT(DISTINCT JobTitle) FROM...

returns the number of unique job titles. In a single query, only one expression may be used as the argument for an aggregate operator that uses the DISTINCT option. However, more than one aggregate operator may use DISTINCT with other occurrences of the expression in the same query. For example: SELECT SUM(DISTINCT x), AVG(DISTINCT x) FROM...

DISTINCT aggregate operations may be done only at the first level of aggregation.

6-18

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Aggregate Operators

Table 6-3 lists the attributes for the result of an aggregate operation.

Result Attributes

6

The SUM operator may result in a numeric overflow or the loss of data because of the default output format. If this occurs, a data type declaration may be used to override the default. For example, if x comprises many rows of INTEGER values, it may be necessary to specify: SUM(x(FLOAT))

If x is an integer, AVERAGE does not display a fractional value. A fractional value may be obtained by casting the value as decimal. For example: CAST(AVG(value) AS DECIMAL(9,2))

Table 6-3 Default Attributes of Aggregate Results

Operation

Data Type

Format

Result Heading

SUM(x)

DECIMAL(15,m) if x is DECIMAL(n,m);

Same format as x, if x is DECIMAL(n,m);

Sum(x)

SUM(x)

INTEGER, if x is BYTEINT or SMALLINT; otherwise, same data type as x.

’-(10)9’ if x is BYTEINT or SMALLINT; otherwise, same format as x

Sum(x)

AVG(x)

FLOAT

Same format as x

Average(x)

MIN/MAX(x)

Same data type as x

Same format as x

Minimum(x) or Maximum(x)

COUNT(x)

INTEGER/DECIMAL(15,0)

’-(10)9’/’Z(14)9’

Count(x)

COUNT(*)

INTEGER/DECIMAL(15,0)

’-(10)9’/’Z(14)9’

Count(*)

COUNT Operator

6

The COUNT operator has two forms, COUNT(value) and COUNT(*). Both forms of COUNT operate on any data type. The COUNT(value) form counts the total number of non-null occurrences of (value). The COUNT(*) form counts the total number of rows in each group of a GROUP BY clause, including groups with Null values for (value).

Teradata RDBMS for UNIX SQL Reference

6-19

SQL Expressions Aggregate Operators

Internally, COUNT uses decimal (18,0). In this mode . . .

The number is cast to . . .

With a default display format of . . .

ANSI

DECIMAL (15,0)

‘Z(14)9’

or NUMERIC (15,0) Teradata

INTEGER

-(10)9

The decimal result has been chosen for ANSI mode because the Teradata RDBMS uses COUNT on tables that have a row count beyond the range of INTEGER. The INTEGER result is retained for BTET (Teradata) mode to avoid regression problems. When in Teradata mode, if the result of COUNT overflows and an error is reported, the script can be changed to use CAST, for example SELECT CAST(COUNT(*) AS DECIMAL(15,0) FORMAT ‘Z(14)9’) FROM BIGTABLE;

Example 1

COUNT(*) selects the number of employees in each department: 6

SELECT DeptNo, COUNT(*) FROM Employee GROUP BY DeptNo ORDER BY DeptNo;

If any employees have been inserted but not yet assigned to a department, the return includes a count of the Null values in the DeptNo column. Assuming that two new employees are unassigned, the return is: DeptNo ? 100 300 500 600 700

Count(*) 2 4 3 7 4 3

However, the form of the COUNT operator shown in this statement only registers non-null occurrences of DeptNo. Therefore, the return does not include unassigned employees. SELECT COUNT(DeptNo) FROM Employee;

The output is as follows. Count(DeptNo) 21

6-20

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Aggregate Operators

Example 2

6

Here, COUNT provides the number of male employees in the Employee table of the database: SELECT COUNT(sex) FROM Employee WHERE sex = ’M’ ;

The result is as follows. Count(Sex) 12

Example 3

6

In this examples, assume that in addition to the 21 employees in the Employee table, there are two new employees who have not yet been assigned to a department (that is, the row for each new employee has a null department number). As a prefix operator in the following statement: SELECT COUNT(deptno) FROM employee ;

COUNT returns a total of the non-null occurrences of department number. Since aggregate operations ignore Nulls, the two new employees are not reflected in the figure: Count(DeptNo) 21

Example 4

As a prefix operator in the statement: 6

SELECT deptno, COUNT(deptno) FROM employee GROUP BY deptno ORDER BY deptno ;

COUNT provides for each department a total of the rows that have non-null department numbers. Again, the two new employees are not included in the count. DeptNo 100 300 500 600 700

Count(DeptNo) 4 3 7 4 3

To get the number of employees by department, use COUNT(*): SELECT deptno, COUNT(*) FROM employee GROUP BY deptno ORDER BY deptno ;

In this case, the nulls are included, indicated by >?: DeptNo ? 100 300 500 600 700

Count 2 4 3 7 4 3

Teradata RDBMS for UNIX SQL Reference

6-21

SQL Expressions Aggregate Operators

Example 5

6

To determine the number of departments in the Employee table, use COUNT (DISTINCT) as a prefix operator, as follows: SELECT COUNT (DISTINCT DeptNo) FROM Employee ;

The system responds with: Count(Distinct(DeptNo)) 5

6-22

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Character String Expressions

Character String Expressions

6

Teradata SQL provides the following to format and operate on character string arguments: ● ●

Concatenation operator String functions

Also, comparison operators and the LIKE partial-string operator can be used with CHAR, VARCHAR, GRAPHIC, and VARGRAPHIC string arguments. Character string expressions are flagged as non Entry Level ANSI SQL, when the SQL flagger is enabled.

Teradata RDBMS for UNIX SQL Reference

6-23

SQL Expressions Character Strings: Concatenation Operator

Character Strings: Concatenation Operator Introduction

The concatenation operator has the form: 6

string_expr

string_expr string_expr FF07A076

where string_expr is a string or string expression. The result of a concatenation operation is a string formed by concatenating the arguments in a left-to-right direction. Note: Exclamation marks (!!), previously acceptable in Teradata RDBMS as concatenation operators, are non-ANSI. Solid (||) and broken vertical bars are acceptable by Teradata RDBMS and ANSI as concatenation operators. Note that on some keyboards, the vertical bar is shown as a broken vertical bar but prints as a solid vertical bar.

Argument String Data Types

The rules of data type for the argument strings are as follows: ● 6

If any argument is a numeric data type, the value is first converted to a character string using the format for the numeric value. See Chapter 5, “Data Definition,” in the section “Numeric to Numeric” for details about the caution when implicitly converting numeric to character types.



6-24

If any argument is BYTE or VARBYTE, all other arguments must also be of a byte data type.

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions Character Strings: Concatenation Operator

Data Types of Concatenated Results

Data types of the concatenated results are shown following, where (n) is the sum of the lengths of all arguments: 6

BYTE, VARBYTE, GRAPHIC, and VARGRAPHIC are not ANSI compliant. If you are in ANSI mode and the flagger is enabled you are flagged when you use these data types. IF this argument . . .

IS this data type . . .

THEN the result is . . .

any

null

null.

VARBYTE

VARBYTE(n).

VARCHAR

VARCHAR(n)

VARGRAPHIC

VARGRAPHIC(n)

BYTE

BYTE(n)

CHAR

CHAR(n)

GRAPHIC

GRAPHIC(n)

CASESPECIFIC

CASESPECIFIC

all

either argument of concatenation

The default heading of the concatenation result is the name or title of the left operand, then || followed by the name or title of the right operand.

Concatenation Operator: Examples Example 1

6

6

TstTblA defines Col1 as BYTE(2) and Col2 as VARBYTE(10). Also, hexadecimal constants ’4142’XB and ’7A7B7C’XB are inserted, into Col1 and Col2. Constants, spaces, and the TITLE phrase can be included in the operation definition to format the result heading and improve readability. For example, the following definition: SELECT (’Sex ’ || Sex || ’, Marital Status ’ || MStat)(TITLE ’ ’) FROM Employee ;

returns side titles, evenly spaced result strings, and a blank heading: Sex Sex Sex Sex Sex Sex Sex

M, F, M, F, F, M, F,

Marital Marital Marital Marital Marital Marital Marital

Status Status Status Status Status Status Status

S M M M M M W

. .

Teradata RDBMS for UNIX SQL Reference

6-25

SQL Expressions Character Strings: Concatenation Operator

A Names table contains last and first names columns, defined as VARCHAR, thus: Lname Ryan Villegas Kanieski Brown

Example 2

6

Fname Loretta Arnando Carol Alan

Use string concatenation and a space separator to combine first and last names: SELECT fname ||’ ’|| lname FROM names ORDER BY lname ;

The result is: ((Fname||’ ’)||Lname) Alan Brown Carol Kanieski Loretta Ryan Arnando Villegas

Example 3

Change the SELECT and the separator to obtain Lname, Fname: 6

SELECT lname!!’,’!!fname FROM names ORDER BY lname;

The result is: ((Lname||,)||Fname) Brown, Alan Kanieski, Carol Ryan, Loretta Villegas, Arnando

Example 4 (Concatenating Byte Strings)

The following example concatenates byte strings. SELECT Col2

||

Col1 FROM TstTblA ;

6

returns: (Col2||Col1) 7A7B7C4142

Japanese Character Site Features

6

With the exception of KanjiEBCDIC, concatenation on a Japanese character site acts as described above. Under KanjiEBCDIC, any adjacent shift-out and shift-in characters (> is Shift-In, bold = double byte, G=graphic data string

Teradata RDBMS for UNIX SQL Reference

6-27

SQL Expressions Character Strings: Concatenation Operator

KanjiShift-JIS string_expr1 || string_expr2

Concatenation With TRIM

string_expr1 6

6

string_expr2

Data Type

Result

ABCD

B

Graphic

ABCDB

mnABCI

B

Character

mnABCIB

mnABCI

g

Character

mnABCI g

If the TRIM function is specified on CHAR or VARCHAR(n) data types, trailing blanks (leading and/or trailing blanks, when in ANSI mode) are suppressed in the concatenated string, according to which syntax is used. For example, when the following information is used: First_Name (CHAR(12)) has a value of ’Mary Last_Name (CHAR(12)) has a value of ’Jones

’ ’

the statement: SELECT TRIM (BOTH FROM Last_Name) || ’, ’ || TRIM (BOTH FROM First_Name) FROM...

results in the string: ’Jones, Mary’

If the TRIM function is not specified, trailing blanks are returned in the result string. SELECT (Last_Name || ’, ’ || First_Name) FROM ...

results in the string: ’Jones

6-28

, Mary



Teradata RDBMS for UNIX SQL Reference

SQL Expressions Character Strings: Concatenation Operator

The data types and results of concatenated strings with TRIM are listed below, where (n) is the sum of the lengths of all arguments: ● ●

If any argument is null, the result is null. If the concatenation arguments are ● CHAR ● VARCHAR ● both then the result of an argument on which TRIM is specified is VARCHAR(n), blank trimmed, with a possible zero length.



If the concatenation arguments are ● BYTE ● VARBYTE ● both then the result of an argument on which TRIM is specified is VARBYTE(n), binary 00 trimmed, with a possible zero length.



If the concatenation arguments are ● GRAPHIC ● VARGRAPHIC ● both then the result of an argument on which TRIM is specified is VARGRAPHIC(n), the graphic pad characters (binary 0000) trimmed, with a possible zero length.





In ANSI mode, if LEADING or TRAILING or BOTH is not specified, then TRIM removes both leading and trailing pad characters (blank for CHAR, zero byte for BYTE, zero byte-pair for GRAPHIC). In Teradata mode, TRIM removes only trailing pad characters if LEADING or TRAILING or BOTH is not specified.

Teradata RDBMS for UNIX SQL Reference

6-29

SQL Expressions Character String Expressions: String Functions

Character String Expressions: String Functions The string functions include the following: ●

● ● ● ● ● ● ●

SUBSTRING ● SUBSTR ● SUBSTR with KanjiEUC and KanjiShift-JIS MSUBSTR INDEX MINDEX TRIM VARGRAPHIC CHAR2HEXINT UPPER

Each function is described in the following passages. The string functions are flagged as non-ANSI when the SQL flagger is enabled, with the following exceptions: ● ●

TRIM (a subset of ANSI TRIM) SUBSTRING

SUBSTR is the Teradata RDBMS syntax for SUBSTRING. New applications should use SUBSTRING.

6-30

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions String Functions: SUBSTRING and SUBSTR

String Functions: SUBSTRING and SUBSTR Form

6

The SUBSTRING and SUBSTR functions have the form: 6

SUBSTRING

(string_expr

n1

FROM

) FOR n2

SUBSTR

( string_expr,n1

)

,n2

HH01A066

where:

Usage Notes

6

Syntax Element . . .

Is . . .

string_exp

a string or string expression

n1

the starting position of the substring, string_expr.

n2

the string_expr length.

SUBSTRING is the ANSI SQL syntax. Teradata syntax using SUBSTR is still supported for backward compatibility If a numeric expression is used as the first argument, it is converted to a CHAR(n) string, where n and the format are determined by the default format for the numeric type. Note: SUBSTR, SUBSTRING, and MSUBSTR perform in the same manner except for the one case when SUBSTR is used for KanjiEBCDIC in Teradata mode. Refer to the following section, “Japanese Characters and Graphic Data”.

Teradata RDBMS for UNIX SQL Reference

6-31

SQL Expressions String Functions: SUBSTRING and SUBSTR

SUBSTRING and SUBSTR Behavior

The behavior of SUBSTRING and SUBSTR is explained by the following rules: 6

IF n2 is . . .

AND IF . . .

THEN . . .

ELSE . . .

ELSE IF. . .

specified

n2 < 0

an error returns.

set n2=n2+n1 - 1

IF n1 > string_length

WHEN n2 string_length

This is the only case for which an error returns. n1 < 1

WHEN n2 string_length

a string with zero length returns

THEN set n2 = string_length - n1 +1

a string with zero length returns a substring starting at n1 and ending with the last character of string_expr returns.

The possibly adjusted n1 and n2 now identify the substring to be returned. The substring returned starts at position n1 in the string_expr and extends for n2 characters or to the last character in the source string if there are not n2 characters available. Note: In prior releases using SUBSTR, an error was reported if n1 was zero, negative, or more than one position beyond the length of the string_expr.

Example1

6

Suppose SN is a CHAR(15) field of Serial IDs for Automobiles and positions 3 to 5 represent the country of origin as three letters. For example: 12JAP3764-35421 37USA9873-26189 11KOR1221-13145

6-32

Teradata RDBMS for UNIX SQL Reference

SQL Expressions String Functions: SUBSTRING and SUBSTR

To search for SNs of cars made in the USA: SELECT MAKE, SN FROM Autos WHERE SUBSTRING (SN FROM 3 FOR 3) = ‘USA’

Example 2

6

If instead of SN, we wanted the last five characters, which represent manufacturing sequence number, another substring can be accessed. SELECT MAKE, SUBSTRING (SN FROM 11) AS SEQUENCE FROM Autos WHERE SUBSTRING (SN FROM 3 FOR 3) = ‘USA’

Example 3

6

Suppose NAMEADDRESS is a VARCHAR(120) field, and the application used positions 1 to 30 for name, starting address at 31. To return address only, but limit the number of characters returned to 50 use: SUBSTRING (NAMEADDRESS FROM 31 FOR 50)

This returns an address of up to 50 characters.

Example 4

6

The following example shows various returns from SUBSTR—A SELECT expression requesting substrings from a character field returns the characters in positions 1 through 4 for every row: SELECT SUBSTRING (Jobtitle FROM 1 FOR 4) FROM EMPLOYEE ;

The result is as follows. SUBSTRING(JobTitle FROM 1 FOR 4) Tech Cont Sale Secr Test .

Teradata RDBMS for UNIX SQL Reference

6-33

SQL Expressions Japanese Characters and Graphic Data

Japanese Characters and Graphic Data Introduction

6

6

The start position, n1, and length, n2, are parameters that pertain to logical characters. For character sets with a fixed number of bytes per character, the operations are straightforward in terms of the arithmetic expressions shown. this applies to ASCII and GRAPHIC data types. For some Japanese character data types, the physical operation of finding and adjusting the start_position, n1 and length, n2, is complicated by the fact that the intent is to count logical characters, requiring a scan of the characters in the source string to relate logical to physical.

SUBSTR in Teradata Mode

6

If the session is in Teradata Mode, the SUBSTR function operates without change. Certain multibyte character sets are handled as though they were single byte characters. This is described as physical mode. The multibyte character sets are: ● ● ●

SUBSTR in ANSI Mode

6

If the session is in ANSI Mode, it is handled like MSUBSTR. All MBC character sets are handled in terms of logical characters. ●



Returning Part of a Character or Graphic Expression

6-34

KatakanaEBCDIC KanjiEBCDIC5035_0I KanjiEBCDIC5026_0I

For the syntax: MSUBSTR ( string, start [,length]) there is no change. For the syntax: SUBSTRING ( string FROM start [FOR length]), the behavior is the same as for MSUBSTR, that is, all multibyte character sets are handled in terms of logical characters.

On a Japanese character site, the SUBSTR function can return part of a character or graphic expression: IF string_expr is this data type . . .

THEN the result is this type of substring . . .

character

character

graphic

graphic

6

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Japanese Characters and Graphic Data Table 6-4 KanjiEBCDIC - SUBSTR(string_expr, n1 [, n2])

SUBSTRING with KanjiEUC and KanjiShiftJIS

string_expr

6

n1

ABCD

2

MNP

2

MNP

3

MNP

4

n2

2 8

Data Type

Result

Graphic

BC

Character

NP

Character

Character (improperly formatted)

ABC>P

Under KanjiEUC and KanjiShift-JIS, if string_expr is character data, the SUBSTRING (SUBSTR and MSUBSTR) function expects mixed single byte character/multibyte character strings and operates on logical characters that are valid for the character set of the session. In this case, n1 is a positive integer pointing to the first character of the result and n2 is in terms of logical characters. If string_expr is graphic data, a character is a logical character and n1 and n2 are in terms of logical characters. The following tables detail the results of the SUBSTRING (SUBSTR and MSUBSTR) function for the KanjiEUC and KanjiShift-JIS character sets.

Table 6-5 KanjiEUC - SUBSTR(string_expr, start [, len])

Table 6-6 KanjiShift-JIS - SUBSTR(string_expr, start [, len])

string_expr

n1

ABCD

2

A ss2B CD

2

ss3A ss2B ss3C ss2D

2

string_expr

n1

ABCD

2

mnABCI

6

mnABCI

4

n2

Data Type

Result

Graphic

BCD

2

Character

ss2B

2

Character

ss2B ss3C

n2

1

Teradata RDBMS for UNIX SQL Reference

Data Type

C

Result

Graphic

BCD

Character

I

Character

BCI

6-35

SQL Expressions String Function: MSUBSTR

String Function: MSUBSTR Introduction

Form

6

6

This function operates only on logical characters. It performs in the same manner as the SUBSTR function, except that n1 and n2 must be specified in terms of logical characters, and the Shift-Out/ShiftIn characters are added as necessary to achieve a properly formatted substring. MSUBSTR has the following form:

6

MSUBSTR

( string_expr,

n1

)

, n2 HH01A030

MSUBSTR behaves the same as SUBSTRING. The following table details the results of the MSUBSTR function for the KanjiEBCDIC character set. Table 6-7 KanjiEBCDIC - MSUBSTR(string_expr, n1 [, n2])

6-36

string_expr

start

ABCD

2

MNP

2

MNP

4

MNP

4

len

2

2

Teradata RDBMS for UNIX SQL Reference

Data Type

Result

Graphic

BC

Character

NP

Character

Character

P

SQL Expressions String Function: INDEX

String Function: INDEX Introduction

6

6

INDEX returns the position in the string where the substring starts. If the substring is not found in the string, the result is zero. If an argument is a numeric value, the value is converted to a character string using the default format of the numeric type.

Form

The INDEX function has the form: 6

INDEX

( string_expr

, string_expr ) HH01A031

where the first argument represents a string to be searched and the second represents a possible substring. The result of INDEX is always INTEGER.

Example 1

Example 2

Examples of INDEX expressions and their results follow: 6

Expression INDEX(’catalog’,’dog’ INDEX(’41424344’XB,’43’XB)

6

Result INDEX(’catalog’,’log’ 0 3

5

In this example, INDEX is applied to the substring ’ ’ (space) in the value strings in the Name column of the Employee table: SELECT name FROM employee WHERE INDEX(name, ’ ’) > 6 ;

The system examines the Name field and returns all values where the defined space appears in a character position beyond the sixth (i.e., character position seven or further). The result is a list in which each last name is six characters or longer.

Example 3

6

In the following example, display a list of projects in which the word Batch appears in the project description, and list the starting position of the word: SELECT proj_id, INDEX(description, ’Batch’) FROM project WHERE INDEX(description, ’Batch’) > 0 ;

Teradata RDBMS for UNIX SQL Reference

6-37

SQL Expressions String Function: INDEX

The system returns: Project Id OE2-0003 AP2-0003 OE1-0003 AP1-0003 AR1-0003 AR2-0003

Example 4

6

Index (

Project Description, ’Batch’) 5 13 5 13 10 10

A somewhat more complex construction employing SUBSTRING, INDEX, and concatenation may be more useful. Transpose the form of the names selected from the Employee table and change the punctuation with: SELECT empno, SUBSTRING(name FROM INDEX(name,’ ’)+1 FOR 1)||’. ’|| SUBSTRING(name FROM 1 FOR INDEX(name, ’ ’)-1) (TITLE ’Emp Name’) FROM employee ;

The system returns: EmpNo 10021 10007 10018 10011 10019 . . .

6-38

Emp Name T. Smith J. Aguilar S. Russell M. Chin P. Newman

Teradata RDBMS for UNIX SQL Reference

SQL Expressions String Function: INDEX

Japanese Character Features IF both string_expr are this data type . . .

On a Japanese character site, the INDEX function can be applied to graphic and character data as follows: 6

AND . . .

THEN . . .

graphic

matching is in terms of logical characters; that is, multibyte characters are matched against multibyte characters

the position of the result, if not zero, is reported as the logical character offset within string_expr.

character

if CASESPECIFIC is specified,

simple Latin letters are considered matched only if they are the same letters and the same case.

if CASESPECIFIC is not specified for either expression

any lowercase simple Latin letters are converted to uppercase before the operation begins. Any nonLatin single byte character, any multibyte character, and any byte indicating a transition between single byte character and multibyte character data are excluded from the conversion to uppercase function.

matching is in terms of logical characters; that is, single byte characters are matched against single byte characters, and multibyte characters are matched against multibyte characters. Therefore, representation of the logical character must be identical in both expressions in order for a match to occur.

the position of the result, if not zero, is reported as follows:

(Note that under a KanjiEBCDIC character set, although the ShiftOut/Shift-In characters are included in the offset count, they are not matched; they are treated only as an indication of a transition between a single byte character and an multibyte character.)

Teradata RDBMS for UNIX SQL Reference

6-39

SQL Expressions String Function: INDEX IF both string_expr are this data type . . .

6-40

AND . . .

THEN . . .

Under this character set . . .

The result is . . .

KanjiEBCDIC

the byte corresponding to the first byte of the logical character offset (including ShiftOut/Shift-In in the offset count) within string_expr

all other Kanji

the logical character offset within string_expr

Teradata RDBMS for UNIX SQL Reference

SQL Expressions String Function: INDEX

The following tables detail the output from the INDEX function for each type of Kanji character set. Table 6-8 KanjiEBCDIC INDEX(string_expr1,string_expr2)

Table 6-9 KanjiEUC - INDEX(string_expr1, string_expr2)

Table 6-10 KanjiShift-JIS - INDEX(string_expr1, string_expr2)

string_expr1

string_expr2

Data Type

Result

ABC

C

Graphic

3

ABC

C∆

Graphic

0

MN

Character

6

MN

Character

4

MNP

P

Character

9

MINP

Character

7

string_expr1

string_expr2

Data Type

Result

CS1_DATA

A

Graphic

6

a b ss3A

ss3A

Character

3

a b ss2B

ss2B

Character

3

CS1_DATA

A

Character

6

a b ss2D ss3E ss2F

ss2F

Character

5

a b C ss2D ss3E ss2F

ss2F

Character

6

CS1_DmATA

A

Character

7

string_expr1

string_expr2

Data Type

Result

ABCD

B

Graphic

2

mnABCI

B

Character

4

mnABCI

I

Character

6

Teradata RDBMS for UNIX SQL Reference

6-41

SQL Expressions String Function: MINDEX

String Function: MINDEX Introduction

Form

6

6

Under the KanjiEBCDIC character set, MINDEX reports the logical character offset of the position where string_expr2 was found within string_expr1. The MINDEX function is expressed in the following form:

6

MINDEX

( string_expr1, string_expr2 ) HH01A032

The following table details the output from the MINDEX function. Table 6-11 KanjiEBCDIC - MINDEX(string_expr1, string_expr2)

6-42

string_expr1

string_expr2

Data Type

Result

ABC

C

Graphic

3

ABC

C∆

Graphic

0

MN

Character

4

MN

Character

3

MNP

P

Character

5

MINP

Character

5

Teradata RDBMS for UNIX SQL Reference

SQL Expressions String Function: TRIM

String Function: TRIM Introduction

Form

6

6

The TRIM function takes a single argument, of character or byte data, and trims blank characters or bytes with value zero only in the argument result. The syntax is:

6

TRIM

string_expr )

( BOTH FROM LEADING FROM TRAILING FROM

HH01A034

where: Syntax Element . . .

Trims . . .

BOTH FROM

both trailing and leading blanks.

TRAILING FROM

only trailing blanks.

LEADING FROM

only leading blanks.

string_expr

the following data types for string_expr: ●





BYTE and VARBYTE data types, in which case null bytes are trimmed. CHAR and VARCHAR, in which case blanks and SBC/MBC spaces are trimmed. GRAPHIC and VARGRAPHIC, in which case double byte nulls or zeroes are trimmed.

The form TRIM string_expr is dependent on whether the session is in Teradata or ANSI mode:

Returns From a TRIM Function

In this mode . . .

TRIM is equivalent to . . .

ANSI

TRIM (BOTH FROM string_expr)

Teradata

TRIM (TRAILING FROM string_expr)

Possible returns from TRIM are: 6

Teradata RDBMS for UNIX SQL Reference

6-43

SQL Expressions String Function: TRIM

Using TRIM With Concatenation Operator Example 1

6

6

WHEN TRIM operates on . . .

THEN the result is . . .

a null argument

null.

a numeric argument

first converted to a character string using the default format of the numeric value.

the return from a CHAR or VARCHAR argument

VARCHAR, blank trimmed, with a possible zero length.

the return from a BYTE or VARBYTE arguments

VARBYTE, binary 00 trimmed, with a possible zero length.

the return from a GRAPHIC argument

VARGRAPHIC, binary 0000 trimmed with a possible zero length.

The TRIM function is typically used with the concatenation operator to remove trailing blanks or trailing bytes containing binary 00 from the concatenated string. If the Names table includes the columns First_Name and Last_Name, which contain the following information: First_Name (CHAR(12)) has a value of ’Mary ’ Last_Name (CHAR(12)) has a value of ’Jones ’ then this statement: SELECT TRIM (BOTH FROM Last_Name) || ’, ’ || TRIM(BOTH FROM First_Name) FROM Names ;

returns the following string (note that the seven trailing blanks at the end of string Jones, and the eight trailing blanks at the end of string Mary are not included in the result): ’Jones, Mary’

Example 2

If the TRIM function is removed, the statement: 6

SELECT Last_Name || ’, ’ || First_Name FROM Names;

returns trailing blanks in the string: ’Jones

6-44

, Mary



Teradata RDBMS for UNIX SQL Reference

SQL Expressions String Function: TRIM

Example 3

6

As another example, assume column A is BYTE(4) and column B is VARBYTE(10). If these columns contained the following values: A 78790000 68690000 12550000

B 43440000 3200 332200

then this expression: SELECT TRIM (TRAILING FROM A) ||TRIM (TRAILING FROM B) FROM ...

returns: 78794344 686932 12553322

On a Japanese character site, TRIM operates as follows: IF string_expr is this data type . . .

THEN . . .

AND the result is . . .

GRAPHIC

any trailing graphic pad characters are removed

VARGRAPHIC

CHAR

only leading and/or trailing single byte space characters are removed

VARCHAR TRIM takes into consideration multibyte character and ShiftOut/Shift-In characters when determining whether single byte character spaces can be removed.

The following tables detail the results of the TRIM function under each of the supported Kanji character sets.

Teradata RDBMS for UNIX SQL Reference

6-45

SQL Expressions String Function: TRIM

KanjiEBCDIC - TRIM (BOTH FROM string_expr)

Data Type

string_expr

Result

6

ABC∆∆

Graphic

ABC

(∆ is a represented by 0x0000) MNDD

MN

Character (D is a ,

MN

∆AB∆>D

D
:c)

The following statement uses a comparison operator with the ANY quantifier to select the employee number, name, and department number of anyone in departments 100, 300, and 500: This Expression . . .

Is Equivalent to . . .

SELECT EmpNo, Name, DeptNo FROM Employee WHERE DeptNo = ANY (100,300,500) ;

SELECT EmpNo, Name, DeptNo FROM Employee WHERE (DeptNo = 100) OR (DeptNo = 300) OR (DeptNo = 500) ;

The preceding statements are also equivalent to: SELECT EmpNo, Name, DeptNo FROM Employee WHERE DeptNo IN (100,300,500) ;

quantifier

6

Teradata SQL recognizes the quantifiers ANY (or SOME) and ALL. A quantifier allows one or more expressions to be compared with one or more constants (see above) or with one or more values returned by a subquery. If a quantifier is used with a subquery, the subquery may return none, one, or several values. The results are determined as follows: OR takes this value when subquery returns no values . . .

The following expression is TRUE . . .

WHEN expression is greater than . . .

expression > ALL subquery

every value in the set of values returned by subquery. It is also TRUE if subquery returns no values

TRUE

expression > ANY subquery

at least one value of the set of values returned by subquery

FALSE

Teradata RDBMS for UNIX SQL Reference

6-59

SQL Expressions CASE Blind Comparisons

When the following form is used: (

, expr

)

comparison_operator

quantifier

( subquery )

HH01A041

the subquery must select the same number of expressions as are specified in the expression list. For example: SELECT EmpNo,Name,JobTitle,Salary,YrsExp FROM Employee WHERE (Salary,YrsExp) >= ALL (SELECT Salary,YrsExp FROM Employee) ;

subquery

6

A subquery is a SELECT statement that returns values used to satisfy the comparison operation. The subquery must be enclosed in parentheses; it does not end with a semicolon. The subquery must refer to at least one table. A table that is in the WHERE clause, but that is not referred to in any other parts of the subquery, is not applicable. A comparison operation may be used with a subquery whether or not a quantifier is used. If a quantifier is not used, however, then an error condition results if the subquery returns more than one value. If a subquery returns no values, and if a quantifier is not used, then the result of the comparison is false. Therefore, if the following form is used, the subquery must return either no values (in which case the comparison evaluates to false), or it returns one value. expression > (subquery)

With the following form, subquery must select the same number of expressions as are specified in the expression list. (

, expr

)

comparison_operator

quantifier

( subquery )

HH01A041

The two expression lists are equal if each of the respective expressions are equal. If the respective expressions are not equal, then the result of the comparison is determined by comparing the first pair of expressions (from the left) for which the comparison is not true.

6-60

Teradata RDBMS for UNIX SQL Reference

SQL Expressions CASE Blind Comparisons

Example

6

The following statement uses the ALL quantifier to compare two expressions with the values returned from a subquery to find the employee(s) with the most years of experience in the group of employees having the highest salary: SELECT EmpNo, Name, DeptNo, JobTitle, Salary, YrsExp FROM Employee WHERE (Salary,YrsExp) >= ALL (SELECT Salary,YrsExp FROM Employee) ;

Teradata RDBMS for UNIX SQL Reference

6-61

SQL Expressions Japanese Character Site Comparison Operators

Japanese Character Site Comparison Operators Introduction

Equality Comparison

6

6

On a Japanese character site, the Teradata RDBMS can perform comparison operations on graphic data. Comparison of Japanese character or graphic strings is described in the following passages. The handling of an equality comparison depends on whether the expressions are character or graphic strings, as explained below. ●



6-62

Comparison of character strings, which can contain mixed single byte character/multibyte character data, is handled as follows: ● If expr1 and expr2 are of different lengths, the shorter string is padded with one or more characters. (The single byte space is always used, regardless of the character set.) ● Unless the CASESPECIFIC qualifier is applied to one or both of the expressions, any simple Latin letters in both expr1 and expr2 are converted to uppercase before comparison begins. ● The prepared strings are compared byte-by-byte; any trailing spaces are ignored. Comparison of graphic strings is based on logical characters. The operation is handled as follows: ● A graphic expression cannot be converted; it can be compared only to another graphic expression. ● If expr1 and expr2 are of different lengths, the shorter string is padded with one or more characters. (Note that a double byte zero is always used, regardless of the current character set.) ● The prepared strings are compared byte-by-byte; any trailing zeros are included in the comparison. For example, AB is not equal to AB. ● Two graphic strings are considered equal if their byte representation is the same. (This is true on any site and under any character set.)

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions Japanese Character Site Comparison Operators

Non-Equality Comparison: Collation Sequences

6

Non-equality comparisons involve the collation in effect for the session. Three collations are available: EBCDIC, ASCII, and MULTINATIONAL. Collation can be set at the user level with the COLLATION option of the CREATE USER or MODIFY USER statements, and at the session level with the [[.]SET] SESSION COLLATION statement or the CLIv2 CHARSET call.) If the MULTINATIONAL collation sequence is in effect, the collation sequence of a Japanese character site is determined by the collation setting installed during start-up. For further information on collation sequences, please refer to Appendix G, “International and Japanese Character Support”.

Collation Settings

6

To make sure that sorting and comparison of character data will be identical with the same operations performed by the client, users on a Japanese character site should set collation as follows: IF you are using this character set . . .

THEN you shoud use this collation sequence . . .

KanjiEUC KanjiShift-JIS

ASCII

KATAKANAEBCDIC KANJIEBCDIC5026_0I KANJIEBCDIC5035_0I

MULTINATIONAL

Teradata RDBMS for UNIX SQL Reference

6-63

SQL Expressions Japanese Character Site Comparison Operators

Comparing Non-Equality Character Strings

A non-equality comparison operation is handled as explained below. 6

Stage

Comparing Graphic Strings Based on Logical Characters.

1

If expr1 and expr2 are of different lengths, the shorter string is padded with the character.

2

Unless the CASESPECIFIC qualifier is applied to one or both of the expressions, any simple Latin letters in both expr1 and expr2 are converted to uppercase before comparison begins.

3

Characters identified as single byte characters under the current character set are converted according to the collation sequence in effect for the session.

4

Note that under the KanjiEUC character set, the ss3 0x8F character is converted to 0xFF. This means that a user-defined KanjiEUC codeset 3 will not be properly ordered with respect to other KanjiEUC code sets. The ordering of other KanjiEUC codesets is proper; that is, ordering is the same as the binary ordering on the client system.

5

Characters identified as multibyte characters remain in the client encoding and are collated based on their binary values.

6

The prepared strings are compared byte-by-byte; any trailing spaces are ignored.

The following rules apply to graphic string comparisons based on logical characters: ● 6



6-64

Process

If expr1 and expr2 are of different lengths, the shorter string is padded with characters. The prepared strings are compared byte-by-byte; any trailing zeros are included in the comparison

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: IN and NOT IN Operator

Logical Expressions: IN and NOT IN Operator Introduction

6

The IN and NOT IN operators have two forms, which allow you to: 6





Compare the value of an expression with values in an explicit list of constants. Compare values in a list of expressions with values and in a set of corresponding expressions in a subquery.

Note: The IN operator can be substituted by IN ANY or = ANY, and the NOT IN operator can be substituted by ^= ALL or ALL.

Form 1: expr IN and NOT IN an expr or constants

The first form of IN and NOT IN is: 6

expr

expr ,

IN NOT (

const

) HH01A001

where const may be a constant, a macro parameter, or a built-in value such as TIME or DATE. ●

If IN is used with a single-term operator, that operator may be a constant or an expression. If a multiple-term operator is used, that operator must consist of constants; expressions are not allowed.







The expr data types and the const values must be compatible. Implicit conversion rules are the same as for the comparison operators. If the value of expr is null, then the result is considered to be unknown. If the set of constants consists of 70 or fewer members, the system uses hashing to retrieve the rows. If the set consists of more than 70 members, the system does a full-table scan. Note that full-table scans are much more time consuming than row hashed accesses.

Using this form, the IN search condition is satisfied if the expression is equal to any of the values in the list of constants; the NOT IN

Teradata RDBMS for UNIX SQL Reference

6-65

SQL Expressions Logical Expressions: IN and NOT IN Operator

condition is satisfied if the expression is not equal to any values in the list of constants.

Example (Form 1 of IN and NOT IN)

Form 2: expr IN and NOT IN Subquery

6

THIS form . . .

Is valid in this mode . . .

AND means . . .

expr1 IN expr2

Teradata

expr1 = expr2

Expr1 NOT IN expr2

both

expr1 expr2

This Expression . . .

Is Equivalent To . . .

SELECT DeptNo FROM Department WHERE DeptNo IN (500, 600) ;

SELECT DeptNo FROM Department WHERE (DeptNo = 500) OR (DeptNo = 600) ;

UPDATE Employee SET Salary=Salary + 200 WHERE DeptNo NOT IN (100, 700) ;

UPDATE Employee SET Salary=Salary + 200 WHERE (DeptNo ^= 100) AND (DeptNo ^= 700) ;

The second form of IN and NOT IN is either of the following two forms: 6

expr

IN

(

subquery

)

NOT , (

expr

IN

) NOT

(

subquery

) HH01A002

where subquery is a SELECT statement that returns values that satisfy the search condition. The subquery must meet the following criteria: ● ● ●

be enclosed in parentheses, not end with a semicolon, and select the same number of expressions as are defined in the expr-list.

A statement result does not include null column values when IN is used with a subquery. ●

For a query of the form: SELECT ... FROM T1 WHERE x NOT IN (SELECT y FROM T2);

If one of the y values is null, then no T1 rows are returned for the entire query.

6-66

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: IN and NOT IN Operator ●



If some rows are returned by the subquery, and if x contains some null values, then those T1 rows that contain a null in x are not returned. For a query in the form: SELECT ... FROM T1 WHERE expression_list1 NOT IN (SELECT expression_list2 FROM T2);

Examples (Form 2 of IN and NOT IN)

IF a null is . . .

THEN . . .

the first field in expressionlist2

no rows from T1 are returned

in a field other than the first field of expressionlist2

some rows may be returned

in the first field in expressionlist1 of only some of the returned rows

the T1 rows containing a null in the first field of expressionlist1 are not returned.

The following statement searches for the names of all employees who work in Atlanta. 6

SELECT Name FROM Employee WHERE DeptNo IN (SELECT DeptNo FROM Department WHERE Loc = ’ATL’) ;

Using a similar example but assuming that the DeptNo is divided into two columns, the following statement could be used: SELECT Name FROM Employee WHERE (DeptNoA, DeptNoB) IN (SELECT DeptNoA, DeptNoB FROM Department WHERE Loc = ’LAX’) ;

Teradata RDBMS for UNIX SQL Reference

6-67

SQL Expressions Logical Expressions: IS NULL and IS NOT NULL Operator

Logical Expressions: IS NULL and IS NOT NULL Operator Introduction Form

6

You may search for or exclude nulls in an expression by using the IS NULL and IS NOT NULL operators. The format is:

6

expression

IS

NULL NOT HH01A042

Examples

6

For example, to search for the names of all employees who have not been assigned to a department, enter the following statement: SELECT Name FROM Employee WHERE DeptNo IS NULL;

The result of this query is the names of all employees with a null in the DeptNo field. Conversely, to search for the names of all employees who have been assigned to a department, you could enter the following statement: SELECT Name FROM Employee WHERE DeptNo IS NOT NULL;

This query returns the names of all employees with a non-null value in the DeptNo field.

Searching for NULL and NOT-NULL in the Same Statement

If you are searching for nulls and non-null values in the same statement, the search condition for null values must be listed separately. 6

For example, to select the names of all employees without the job title of “Manager” or “Vice Pres”, plus the names of all employees with a null in the JobTitle column, you must enter the statement as follows: SELECT Name, JobTitle FROM Employee WHERE (JobTitle NOT IN (’Manager’ OR ’Vice Pres’)) OR (JobTitle IS NULL) ;

6-68

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions Logical Expressions: IS NULL and IS NOT NULL Operator

Searching a Table That May Contain NULLs

6

You must be careful when searching a table that may contain nulls. For example, if the EdLev column contains nulls and you enter the following: SELECT Name, EdLev FROM Employee WHERE (EdLev < 16) ;

the result contains only the names of employees with an education level of less than 16 years. To make sure that the result of a statement will contain nulls, the statement must be entered as: SELECT Name, EdLev FROM Employee WHERE (EdLev < 16) or (EdLev IS NULL) ;

Teradata RDBMS for UNIX SQL Reference

6-69

SQL Expressions Logical Expressions: EXISTS

Logical Expressions: EXISTS Introduction

6

6

Exists conditions are used to test a specified table, normally a derived table, for the existence of at least one row (i.e. to test whether the table in question is non-empty). The EXISTS predicate is supported as the predicate of the search condition in a WHERE clause.

Form

The general form of its usage is: 6

EXISTS

subquery

NOT HH01A047

The meaning of the EXISTS predicate is to test the result of the subquery.

Usage Notes

6

If execution of the subquery would return response rows then the where condition is considered satisfied. Use of the NOT qualifier for the EXISTS predicate reverses the sense of the test. Execution of the subquery does not, in fact, return any response rows. It simply returns a boolean to indicate whether responses would or would not be returned. EXISTS predicate is used to test the existence of specified rows of a subquery. In general, EXISTS can be used to replace comparisons with IN and NOT EXISTS can be used to replace comparisons with NOT IN. However, the reverse is not true. There are problems which can be solved only by using EXISTS and/or NOT EXISTS predicate (see the example in the section ‘For All’).

Example

6

To select rows of t1 whose values in column x1 are equal to the value in column x2 of t2, one of the following queries can be used: SELECT * FROM t1 WHERE x1 IN (SELECT x2 FROM t2); SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t1.x1=t2.x2);

To select rows of t1 whose values in column x1 are not equal to any value in column x2 of t2, one of the following queries can be used: SELECT * FROM SELECT * FROM t1.x1=t2.x2); SELECT ‘T1 is SELECT ‘T1 is

6-70

t1 WHERE x1 NOT IN (SELECT x2 FROM t2); t1 WHERE NOT EXISTS (SELECT * FROM t2 WHERE not empty’ WHERE EXISTS (SEL * FROM t1); empty’ WHERE NOT EXISTS (SEL * FROM t1);

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: EXISTS

EXISTS Predicate Versus NOT IN and Null Values

The NOT EXISTS predicate should be used instead of NOT IN if the following conditions are true: 6

● ●



Some column of the NOT IN condition is defined as nullable. Any rows from the main query with a Null value in any column of the NOT IN condition should always be returned. Any NULL values returned in the select list of the subquery should not prevent any rows from the main query from being returned.

For example, the following query with NOT IN: SELECT dept, DeptName FROM Department WHERE Dept NOT IN (SELECT Dept FROM Course);

should be replaced by a query with NOT EXISTS as follows: SELECT dept, DeptName FROM Department WHERE NOT EXISTS (SELECT Dept FROM Course WHERE Course.Dept=Department.Dept);

if all of the above conditions are true. That is, either Course.Dept or Department.Dept is nullable and a row from Department with a null value for Dept should be returned and a Null value in Course.Dept should not prevent rows from Department from being returned.

For ALL

6

Two nested NOT EXISTS can be used to express a SELECT statement which embodies the notion of “for all (logical ∀) the values in a column, there exists (logical ∃) ...” For example the query to select a ‘true’ value if the library has at least one book for all the publishers can be expressed as follows: SELECT ‘TRUE’ WHERE NOT EXISTS (SELECT * FROM publisher pb WHERE NOT EXISTS (SELECT * FROM book bk WHERE pb.PubNum=bk.PubNum);

Select all student names who have registered in at least one class offered by some department.

Example: EXISTS with Correlated Subqueries

6

SELECT SName, SNo FROM student s WHERE EXISTS (SELECT * FROM department d WHERE EXISTS (SELECT * FROM course c, registration r, class cl WHERE c.Dept=d.Dept AND c.CNo=r.CNo AND s.SNo=r.SNo AND r.CNo=cl.CNo AND r.Sec=cl.Sec));

The content of the student table is as follows: Sname

SNo

Helen Chu

1

Alice Clark

2

Kathy Kim

3

Tom Brown

4

Teradata RDBMS for UNIX SQL Reference

6-71

SQL Expressions Logical Expressions: EXISTS

The content of the department table is as follows: Dept

DeptName

100

Computer Science

200

Physic

300

Math

400

Science

The content of course table is as follows: CNo

Dept

10

100

11

100

12

200

13

200

14

300

The content of the class table is as follows: CNo

Sec

10

1

11

1

12

1

13

1

14

1

The content of the registration table is as follows:

6-72

CNo

SNo

Sec

10

1

1

10

2

1

11

3

1

12

1

1

13

2

1

14

1

1

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: EXISTS

The following rows are returned: SName Helen Chu1 Alice Clark Kathy Kim

SNo * 2 3

See Chapter 9, “Advanced SQL,” for a full explanation of correlated subqueries.

Example: NOT EXISTS with Correlated Subqueries

Select all student names who have registered in at least one class offered by each department which offers a course. 6

SELECT SName, SNo FROM student s WHERE NOT EXISTS SELECT * FROM department d WHERE d.Dept IN SELECT Dept FROM course) AND NOT EXISTS SELECT * FROM course c, registration r, class cl WHERE c.Dept=d.Dept AND c.CNo=r.CNo AND s.SNo=r.SNo AND r.CNo=cl.CNo AND r.Sec=cl.Sec));

With the contents of the tables as in example 1, the following rows are returned: SName Helen Chu

SNo 1

Teradata RDBMS for UNIX SQL Reference

6-73

SQL Expressions Logical Expressions: LIKE Partial-String Operator

Logical Expressions: LIKE Partial-String Operator Introduction

Form

6

The partial-string operator is LIKE. 6

LIKE is used to search for character string data that partially matches a given string. The LIKE partial-string operator has the forms: 6

expr

LIKE

pattern_expr

NOT

expr

ESCAPE ( subquery )

LIKE

quantifier

NOT

escape_character

ESCAPE

escape_character

,

expr

(

( subquery )

LIKE

) NOT

quantifier

, (

expr

ESCAPE

escape_character

, )

LIKE

quantifier

(

pattern_string

NOT

) ESCAPE

escape_character HH01A043

where: Syntax Element . . .

Is . . .

expr

A character string or character string expression argument.

pattern_string

A character string literal argument.

pattern_expr

A character string or a character expression argument.

quantifier

The quantifier argument is one of the keywords ANY (or SOME) or ALL. SOME is a synonym for ANY.

expr

6-74

6

subquery

A SELECT statement argument.

ESCAPE escape_character

Keyword specifying a single escape character (single or multibyte). It can also be in ‘xxxx’xg format.

If any expression in a comparison is null, the result of the comparison is unknown. For a LIKE operation to provide a true result when searching fields that may contain null values, the statement must include the IS [NOT] NULL operator.

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: LIKE Partial-String Operator

If neither the string being matched nor pattern-string has been designated CASESPECIFIC, any lowercase letters are converted to uppercase before the comparison operation occurs. If either the string being matched or the pattern-string has been designated CASESPECIFIC, letters are considered matched only if they are the same letters and the same case.

pattern-string

6

The pattern_string must always be placed to the right of the LIKE operator. The pattern_string may be any character string. The % and _ characters may be used in any combination in pattern-string. The % character represents any string of zero or more arbitrary characters. The _ (underscore) represents exactly one arbitrary character. Any single character is acceptable in the position in which the underscore character appears, and any string of characters is acceptable as a replacement for the percent. However, the underscore and percent characters cannot be used in a pattern. To get around this, specify a single escape character in addition to the pattern_string. See the previous page, for the LIKE predicate syntax.

ESCAPE Feature of LIKE

6

When the defined escape character is used in the pattern string it must be immediately followed by either an underscore, percent sign, or another escape character. In a left-to-right scan of the pattern string the following rules apply when ESCAPE is specified: ●







Until an instance of the escape character occurs, characters in the pattern are interpreted at face value. When an escape character occurs immediately followed by another instance of the escape character, the two character sequence is treated as though it were a single instance of the escape character, considered as a normal character. When an escape character occurs immediately followed by an underscore, the sequence is treated as a single underscore character (not a wildcard character). When an escape character occurs immediately followed by a percent character, the sequence is treated as a single percent character (not a wildcard character).

Teradata RDBMS for UNIX SQL Reference

6-75

SQL Expressions Logical Expressions: LIKE Partial-String Operator ●

Example

When an escape character is not immediately followed by an underscore, a percent, or another instance of itself, the scan stops and an error is reported.

The following example illustrates the use of ESCAPE: 6

To look for the pattern ‘95%’ in a string such as ‘Result is 95% effective’, if Result is the field to be checked, use: WHERE Result LIKE ‘%95Z%%’ ESCAPE ‘Z’

This clause finds the value ‘95%’.

Blanks

6

The following notes apply to blank characters and how they are treated in strings: ●



Blanks are significant in both the character expression, and in the pattern string. When using pattern matching, be aware that both leading and trailing blanks in the field or expression must match exactly with the pattern. For example, ‘A%BC’ matches ‘AxxBC’, but not ‘AxxBCb’, and ‘A%BCb’ matches ‘AxxBCb’, but not ‘AxxBC’ or ‘AxxBCbb’ (b indicates a blank).



To retrieve the row in all cases, consider using the TRIM function, which removes both leading and trailing blanks from the source string before doing the pattern match. For example, to remove trailing blanks: TRIM (TRAILING FROM expr) LIKE pattern-string

To remove leading and trailing blanks: TRIM (BOTH FROM expr) LIKE pattern-string ●

If the pattern_string is forced to a fixed length (perhaps by a macro parameter or a USING clause), trailing blanks may be appended. In such cases, the field must contain the same number of trailing blanks in order to match. For example: CREATE MACRO (pattern (CHAR(5)) AS field LIKE :pattern...

appends trailing blanks to pattern strings shorter than 5 characters long.

6-76

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: LIKE Partial-String Operator ●

To retrieve the row in all cases, apply the TRIM function to the pattern string (TRIM (TRAILING FROM :pattern) ), or the macro parameter can be defined as VARCHAR. These two methods do not always return the same results.TRIM removes blanks, while the VARCHAR method maintains the data pattern exactly as entered.

Example 1

6

The following example uses a LIKE partial-string operator to select a list of employees whose job title contains the string “Pres”: SELECT Name, DeptNo, JobTitle FROM Employee WHERE JobTitle LIKE ’%Pres%’ ;

The form %string% requires the Teradata RDBMS to examine much of each string x. If x is long and there are many rows in the table, the search for qualifying rows may take a long time. The result returned is: Name Watson L Phan A Russell S

Example 2

6

DeptNo 500 300 300

JobTitle Vice President Vice President President

The next example selects a list of all employees whose last name begins with the letter P. SELECT Name FROM Employee WHERE Name LIKE ’P%’;

The result returned is: Name Phan A Peterson J

Example 3

6

The % and _ characters are used to select a list of employees with the letter A as the second letter in the last name. The length of the string returned may be two or more characters. SELECT Name FROM Employee WHERE Name LIKE ’_a%’;

returns the result: Name Marston A Watson L Carter J

If the preceding example used _a_, the search would be for a three-character string with the letter a as the second character.

Teradata RDBMS for UNIX SQL Reference

6-77

SQL Expressions Logical Expressions: LIKE Partial-String Operator

Because none of the names in the Employee table fit this description, no rows would be returned. Both leading and trailing blanks in a pattern are significant to the matching rules.

Example 4: quantifier

6

6

LIKE ’bbZ%’ (where b = blank) locates only those fields that start with two blanks followed by Z. Teradata SQL recognizes the quantifiers ANY (or SOME) and ALL. A quantifier allows one or more expressions to be compared with one or more values. For example, using the following form: ,

expr

LIKE

quantifier

(

pattern_string

) HH01A044

IF you specify this quantifier . . .

THEN the search condition is satisfied if expr LIKE pattern-string, ... is true for . . .

ALL

every string in the list.

ANY

any string in the list.

The ALL quantifier is the logical statement FOR ∀. The ANY quantifier is the logical statement FOR ∃. The following table restates this. This Expression . . .

Is Equivalent to . . .

x LIKE ALL (’A%’,’%B’,’%C%’)

x LIKE ’A%’ AND x LIKE ’%B’ AND x LIKE ’%C%’

x LIKE ANY (’A%’,’%B’,’%C%’)

x LIKE ’A%’ OR x LIKE ’%B’ OR x LIKE ’%C%’

The following statement selects from the employee table the row of any employee whose job title includes the characters “Pres” or begins with the characters “Man”: SELECT * FROM Employee WHERE JobTitle LIKE ANY (’%Pres%’, ’Man%’);

6-78

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: LIKE Partial-String Operator

The result of this statement is: EmpNo 10021 10008 10007 10018 10012

Name Smith T Phan A Aguilar J Russell S Watson L

DeptNo 700 300 600 300 500

JobTitle Manager Vice Pres Manager President Vice Pres

Salary 45,000.00 55,000.00 45,000.00 65,000.00 56,000.00

... ... ... ... ... ...

For the following forms: expr

( subquery )

LIKE

quantifier

NOT , (

expr

( subquery )

LIKE

) NOT

quantifier

HH01A059

If the ALL or ANY/SOME quantifier is specified, the subquery may return none, one, or several rows. If, however, a quantifier is not used, then the subquery must return either no value, or a single value. That is:

Example

6

This Expression . . .

Is True WHEN expre matches. . .

expr LIKE (subquery)

the single value returned by subquery.

expr LIKE ANY (subquery)

at least one value of the set of values returned by subquery; is false if subquery returns no values.

expr LIKE ALL (subquery)

each individual value in the set of values returned by subquery, and is true if subquery returns no values.

The following statement uses the ANY quantifier to retrieve every row from the Project table, which contains either the Accounts Payable or the Accounts Receivable project code: SELECT * FROM Project WHERE Proj_Id LIKE ANY SELECT Proj_Id FROM Charges WHERE Proj_Id LIKE ANY (’A%’)) ;

Teradata RDBMS for UNIX SQL Reference

6-79

SQL Expressions Logical Expressions: LIKE Partial-String Operator

subquery

6

If the following form is used, the subquery may return none, one, or several values. expr

LIKE

quantifier

( subquery )

NOT HH01A045

For example, the following statement: SELECT * FROM Project WHERE Proj_Id LIKE ANY (SELECT Proj_Id FROM Charges WHERE Proj_Id LIKE ’%AP1%’) ;

returns: Project Id AP1-0003 AP1-0001 AP1-0002

Project Description A/P Payable Batch System A/P Payable DB Design A/P Payable Online System

Received Date 82/08/09 82/08/09 82/08/09

When this form is used, the subquery must select the same number of expressions as are in the expression list: , (

expr

LIKE

)

quantifier

( subquery )

NOT HH01A046

For example: (x,y) LIKE ALL (SELECT a,b FROM c)

is equivalent to: (x LIKE c.a) AND (y LIKE c.b)

6-80

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: Japanese Character Site, LIKE Operator

Logical Expressions: Japanese Character Site, LIKE Operator Introduction

6

6

On a Japanese character site, the LIKE operator can also operate on graphic data. The string being matched and the pattern string must be of the same data type. Refer to Appendix G, “International and Japanese Character Support,” for detail about Japanese character supported sites.

Graphic Data

If the expressions reference graphic data, the following apply: 6

● ● ●



Character Data

If the expressions reference character data, the following apply: 6









Teradata Mode

All data comparisons are performed on logical characters. The represents one logical character. The represents a string of zero or more logical characters. Any other character represents a single occurrence of itself.

6

Unless the CASESPECIFIC qualifier is applied to one of the expressions, simple Latin letters in both the string being matched and the pattern string are converted to uppercase before comparison begins. If the and symbols appear in the pattern string data, handling is as follows: ● An refers to one single byte character; an refers to one logical character. ● A , either single byte character or multibyte character, refers to any number of characters of any type, either single byte character or multibyte character. Under the KanjiEBCDIC character set, the Shift-Out/Shift-In characters are treated only as an indication of a transition between an single byte character and an multibyte character. Any other character represents a single occurrence of itself, and can be converted to uppercase.

For mixed single byte character/multibyte character strings, 4 metacharacters are allowed: ● ●

a single byte underscore matches a single byte character. a double byte underscore matches a single byte character or multibyte character.

Teradata RDBMS for UNIX SQL Reference

6-81

SQL Expressions Logical Expressions: Japanese Character Site, LIKE Operator

a single byte percent matches any sequence of single byte characters or multibyte characters. ● a double byte percent matches any sequence of single byte characters or multibyte characters. The operation of the LIKE pattern-string is the same as described above. ●

ANSI Mode

6

When using ANSI mode at Japanese character sites, note the following usage rules: For mixed single byte character/multibyte character strings, 4 metacharacters are allowed: This metacharacter . . .

Matches this sequence of characters . . .

single byte underscore

a single single- or multibyte character.

double byte underscore

a single single byte or multibyte character.

single byte percent

any (single or multibyte).

double byte percent

any (single or multibyte).

Note: The above allows 4 meta-characters. This differs semantically from ANSI SQL which specifies 2 meta-characters.

Graphic Strings

6

The following are rules for using graphic strings in LIKE operators at Japanese character sites: For graphic strings, 2 meta-characters are allowed: This metacharacter . . .

Matches this sequence of characters . . .

double byte underscore

a single

double byte percent

any (multibyte characters).

Note: Single-byte underscore and percent do not exist in the graphic repertoire. Any multibyte character can be the escape-character. The following tables detail the results of applying the LIKE operator under the various Japanese character sets. The results in the following examples do not consider the effects of a column defined as uppercase or case specific.

6-82

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Logical Expressions: Japanese Character Site, LIKE Operator Table 6-15 KanjiEBCDIC - (expr LIKE pattern-string) expr1

pattern-string

Data Type

Return Value, ANSI mode

Return Value, Teradata mode

ABC

%C

Graphic

True

True

ABC

__C

Graphic; __ represents an

False

False

MN

%

Character

True

True

MNP

%

Character

True

True

xyz

%B%

Character data and graphic data - improper operation

Error

Error

MNP

%P

Character

True

True

MNP

%%

Character; __ represents an

False

False

Table 6-16 KanjiEUC - (expr LIKE pattern-string) exp

pattern-string

Data Type

Return Value, ANSI mode

Return Value, Teradata mode

ss3A ss2B ss3C ss2D

% ss2B%

Character

True

True

M ss2B N ss2D

M __%

Graphic; __ represents an

True

True

ss3A ss2B ss3C ss2D

__%

Character; __ represents an

True

True

ss3A ss2B ss3C ss2D

_%

Character; _ represents an

Truea

False

a. The underscore in the pattern-string matches a SBC in Teradata mode, but a SBC or MBC in ANSI mode.

Table 6-17 KanjiShift-JIS - (expr LIKE pattern-string) expr

pattern-string

Data Type

Return Value, ANSI mode

Return Value, Teradata mode

ABCD

__B%

Graphic; __ represents an

True

True

mnABCI

%B%

Character

True

True

mnABCI

%I

Character

True

True

Teradata RDBMS for UNIX SQL Reference

6-83

SQL Expressions Logical Expressions: Japanese Character Site, LIKE Operator

expr

pattern-string

Return Value, ANSI mode

Data Type

Return Value, Teradata mode

mnABCI

mn_%I

Character; _ represents an

Truea

False

mnABCI

mn__%I

Character; __ represents an

True

True

a. The underscore in the pattern-string matches a single byte character in Teradata mode, but a single byte character or multibyte character in ANSI mode.

Japanese Character, LIKE.. ESCAPE

6

LIKE ... ESCAPE is an extended form of LIKE, and follows the same rules as discussed above for ANSI and Teradata modes, with the exception that the escape_character is also allowed. The ESCAPE feature functions as follows:

6-84

FOR this type of character string . . .

This character can function as the escape_character . . .

mixed single and double byte

any

graphic

any multibyte

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Conditional Expressions

Conditional Expressions Introduction

6

6

A conditional expression specifies the criteria for selecting a row or summary information. A conditional expression may be used as the operand in a WHERE or HAVING clause. A conditional expression consists of one or more logical expressions connected by the logical operators AND and OR. For example: expr1

OR

expr2

OR

expr3 HH01A048

The NOT logical operator can be used to negate an expression, as follows: expr1

AND NOT

expr2 HH01A049

When used in a HAVING clause, a logical expression may be used with an aggregate operator (SUM, AVERAGE, COUNT, MINIMUM, or MAXIMUM). For example: WHERE a=c1 GROUP BY a HAVING AVG(b) BETWEEN c2 AND c3 ;

Order of Evaluation

6

The following rules apply to evaluation order for conditional expressions: ●







If an expression contains more than one of the same operator, the evaluation precedence is left to right. If an expression contains a combination of operators, NOT is evaluated before AND, and AND is evaluated before OR. Parentheses may be used to establish the desired evaluation precedence. The logical expressions in a conditional expression are not always evaluated left to right. A conditional expression should be avoided if its accuracy depends on the order in which its logical expressions are evaluated. For example, compare the following two expressions: F2/(NULLIF(F1,0))>500 F1 0 AND F2/F1 > 500

Teradata RDBMS for UNIX SQL Reference

6-85

SQL Expressions Conditional Expressions

The first expression guarantees exclusion of division by zero. The second allows the possibility of error, because the order of its evaluation determines the exclusion of zeros.

Evaluation Results

6

Each logical expression in a conditional expression is evaluated as to whether it is TRUE, FALSE, or UNKNOWN. As a final Boolean result, UNKNOWN and NOT UNKNOWN both translate to FALSE. Table 6-18 and Table 6-19 illustrate the OR and AND logic used in evaluating conditional expressions.

Table 6-18 AND Truth Table (x AND y)

x False

x Unknown

x True

y False

False

False

False

y Unknown

False

Unknown

Unknown

y True

False

Unknown

True

x False

x Unknown

x True

y False

False

Unknown

True

y Unknown

Unknown

Unknown

True

y True

True

True

True

Table 6-19 OR Truth Table (x OR y)

Examples of Conditional Expressions Example 1

The following examples illustrate the use of conditional expressions: 6

6

The following example uses a conditional expression to select from a user table named Profile the names of applicants who have either more than two years of experience or at least twelve years of schooling with a high school diploma: SELECT Name FROM Profile WHERE YrsExp > 2 OR (EdLev >= 12 AND Grad = ’Y’) ;

Example 2

6

The following statement requests a list of all the employees who report to manager number 10007 or manager number 10012. The manager information is contained in the Department table, while the employee information is contained in the Employee table. The request is processed by joining the tables on DeptNo, their common column. DeptNo must be fully qualified in every reference to avoid ambiguity and an extra set of parentheses is needed to group the

6-86

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Conditional Expressions

ORed IN conditions. Without them, the result is a Cartesian product. SELECT EmpNo,Name,JobTitle,Employee.DeptNo,Loc FROM Employee,Department WHERE (Employee.DeptNo=Department.DeptNo) AND ((Employee.DeptNo IN (SELECT Department.DeptNo FROM Department WHERE MgrNo=10007)) OR (Employee.DeptNo IN (SELECT Department.DeptNo FROM Department WHERE MgrNo=10012))) ;

Assuming that the Department table contains the following rows: DeptNo 100 600 500 300 700

Department Administration Manufacturing Engineering Exec Office Marketing

Loc NYC CHI ATL NYC NYC

MgrNo 10005 10007 10012 10018 10021

the join statement returns: EmpNo 10012 10004 10014 10009 10006 10015 10007 10010 10013 10016 10019

Example 3

6

Name Watson L Smith T Inglis C Marston A Kemper R Omura H Aguilar J Reed C Regan R Carter J Newman P

JobTitle Vice Pres Engineer Tech Writer Secretary Assembler Programmer Manager Technician Purchaser Engineer Test Tech

DeptNo 500 500 500 500 600 500 600 500 600 500 600

Loc ATL ATL ATL ATL CHI ATL CHI ATL CHI ATL CHI

The following example uses a conditional expression in a HAVING clause to select from the Employee table those departments with the number 100, 300, 500, or 600, and with a salary average of at least $35,000 but not more than $55,000: SELECT AVG(Salary) FROM Employee WHERE DeptNo IN (100,300,500,600) GROUP BY DeptNo HAVING AVG(Salary) BETWEEN 35000 AND 55000 ;

Teradata RDBMS for UNIX SQL Reference

6-87

SQL Expressions CASE Expression

CASE Expression CASE is used to specify alternate values based on conditions. CASE provides an efficient and powerful method for application developers to change the representation of data. It allows data conversion without requiring host program intervention. For example, you could code employee status as 1 or 2, meaning full-time or part-time, respectively. For efficiency, the system will store the numeric code but print or display the corresponding word in reports for readers. This storage and conversion is managed by the Teradata RDBMS. In addition, CASE allows applications to generate null values based on information derived from the database, again without host program intervention. Conversely, CASE can be used to convert a null into a more concrete value, such as a zero (0). CASE expressions are specified in two different forms: Valued and Searched.

6-88

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions Valued CASE Expression

Valued CASE Expression Introduction Syntax

6

6

This section discusses the Valued form of CASE. CASE returns one of a specific set of values, depending on a condition.

6

value_expression

CASE

value_expression

WHEN

A

A

value_expression

THEN

B

END

B ELSE

value_expression HH01A050

The WHEN clauses are processed sequentially. When a WHEN clause’s condition is true, the value of the associated scalar expression is used as the overall result. The process ends. If no condition expressions are true, the ELSE scalar expression is used as the overall result. Another way of explaining the Valued CASE syntax is: CASE

expr1

WHEN

expr2

THEN

result1

A END

A ELSE

result_n HH01A051

If ELSE is omitted the clause is evaluated as ELSE NULL.

Usage Notes

6

WHEN expr1 equals this value . . .

The result of the CASE expression is . . .

expr2

result1

result2

result2

none of the indicated expressions

result_n

The ELSE clause is optional. If it is left out and none of the WHEN conditions is TRUE, the default value of NULL is returned. The data type of the CASE operand must be comparable with the data types of all of the WHEN operands. Otherwise, an error is returned to the user.

Teradata RDBMS for UNIX SQL Reference

6-89

SQL Expressions Valued CASE Expression

Example

6

The following example uses a Valued CASE expression to calculate the fraction of cost in the total cost of inventory represented by parts of type 1: SELECT SUM( CASE part WHEN ‘1‘ THEN cost ELSE 0 END)/SUM(cost) FROM t;

6-90

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Searched CASE Expression

Searched CASE Expression Introduction Syntax

6

6

This section discusses the second form of CASE expression, Searched.

6

CASE

WHEN

search_condition

THEN

value_expression

A

END

A ELSE

value_expression HH01A052

One way of explaining the ‘Searched’ CASE syntax is: CASE A

WHEN

search_condition1

search_condition2

THEN

THEN

result2

ELSE

result1 result_n

WHEN

A

END HH01A053

The result of the expression is result1 if search_condition1 is met, result2 if search_condition2 is met, ... and result_n if none of the conditions are met.

Example 1

6

The following statement is equivalent to the Valued form given in the example above. SELECT SUM( CASE WHEN part=‘1‘ THEN cost ELSE 0 END)/SUM(cost) FROM t;

Example 2

CASE expressions may be used in place of any value-expressions. 6

SELECT * FROM t WHERE x = CASE WHEN y=2 THEN 1 WHEN (z=3 AND y=5) THEN 2 END;

Teradata RDBMS for UNIX SQL Reference

6-91

SQL Expressions Searched CASE Expression

Example 3

The following example uses an ELSE clause. 6

The system does not support embedded SELECTs in a WHEN condition. Again, the ELSE clause is optional. If it is left out and none of the WHEN conditions are TRUE, the default value of NULL is returned. SELECT * FROM t WHERE x = CASE WHEN y=2 THEN 1 ELSE 2 END;

WHEN Search Conditions

6

WHEN search conditions have the properties listed in the following list ●





Can take the form of any of the comparison operators, such as LIKE, =, , and so forth. May be a quantified predicate, such as ALL, IN ANY, and so forth. May also contain joins of 2 tables; for example: SELECT CASE WHEN t1.x=t2.x THEN t1.y ELSE t2.y END FROM t1,t2;



Data Type of CASE Expression

6-92

6

May not contain any SELECT statements.

Since the values in the THEN/ELSE clauses may belong to different data types, the following rules apply to the data type of the result of the CASE expression. IF . . .

THEN . . .

any of the THEN/ELSE expressions is of character type

the CASE expression is character type, with the length equal to the maximum of all of the formats of the different data types of the THEN/ELSE expressions.

the byte, graphic, date, or time data type is found in a THEN/ELSE expression

all other expressions can only have the same data type.

THEN/ELSE dataset is composed of decimals and integers

all are converted to real.

no character string is found and if any of the THEN/ELSE expressions is of approximate numeric (real)

the CASE expression returns a result that is of approximate numeric data type.

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Searched CASE Expression

Error Conditions

The WHEN clause of a CASE expression is considered illegal when: 6



An invalid WHEN expression is supplied in a simple CASE expression. For example: SELECT CASE a=1 WHEN 1 THEN 1 ELSE 0 END FROM t;



An invalid WHEN condition is supplied in a searched CASE expression. For example: SELECT CASE WHEN a THEN 1 ELSE 0 END FROM t;



A SELECT statement (subquery) is detected in a WHEN condition of a searched CASE expression. For example: SELECT CASE WHEN t.a IN (SEL u.a FROM u) THEN 1 ELSE 0 END FROM t;

Teradata RDBMS for UNIX SQL Reference

6-93

SQL Expressions CASE Abbreviation Expressions

CASE Abbreviation Expressions Introduction

There are two CASE abbreviation expressions: 6

● ●

6-94

NULLIF COALESCE

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions NULLIF

NULLIF Introduction

6

NULLIF is a CASE abbreviation expression. The expression 6

NULLIF

(expression1, expression2 ) HH01A094

is the equivalent of: CASE WHEN expression1 = expression2 THEN NULL ELSE expression1 END

Example 1: NULLIF

Field Age IS SMALLINT. 6

NULLIF (Age,0)

This is the ANSI compliant form of the Teradata function NULLIFZERO(Age), and is more versatile.

Example 2: NULLIF

Field Code IS CHAR(4) and blanks indicate no value. 6

Use NULLIF (Code, ‘

’)

Teradata RDBMS for UNIX SQL Reference

6-95

SQL Expressions COALESCE

COALESCE Introduction Form

6

COALESCE is a CASE abbreviation expression 6

6

The general form of COALESCE is the following, where m>=2. COALESCE, then, returns the first NON NULL value in the expression list.COALESCE, then, returns the first NON NULL value in the expression list. COALESCE

(

m-1 , expression_m

) HH01A056

For a simple application of COALESCE, using only two expressions, the form is the following. COALESCE

( expression1, expression2 ) HH01A054

Example

6

The following example returns the named individual’s home phone number, if present, or office phone if HomePhone is null, or MessageService if present and both home and office phone values are null. Returns null if all three values are null. SELECT Name, COALESCE (HomePhone, OfficePhone, MessageService) FROM PhoneDir;

6-96

Teradata RDBMS for UNIX SQL Reference

SQL Expressions Built-In Values

Built-In Values Introduction

6

6

Built-in values are predefined values. The built-in values include the following: ● ● ● ●

DATE TIME USER NULL

The built-in values, except for NULL, can be used anywhere that a constant can appear. If a SELECT statement that contains a built-in value references a table name, then the result of the query contains one row for every row of the table that satisfies the search condition.

Teradata RDBMS for UNIX SQL Reference

6-97

SQL Expressions DATE

DATE Introduction

6

6

The DATE built-in value supplies the current date. It is of data type DATE, length 4, and is nullable. This is not the ANSI form of DATE, and when used it will be flagged if the SQL flagger is enabled.

Caution:

Form Example

The value of DATE as stored internally allows for dates from 000101-01 to 3500-12-31. To avoid problems concerning years from 200001-01 on, use the format ‘YYYY-MM-DD’. The default format of the DATE built-in value is YY/MM/DD.

6

The following example selects today’s date: 6

SELECT DATE;

The system returns: Date 96/03/30

Use the FORMAT phrase to change the presentation: SELECT date (FORMAT ’mm-dd-yy’) ; Date 03-30-96

Another form gives: SELECT date (FORMAT ’mmmbdd,byyyy’) ; Date Mar 30, 1996

Examples of date formats are listed as follows: Format ‘yyyy-mm-dd’ ’yy/mm/dd’ ’mm/dd/yy’ ’mmbddbyy’ ’mmmbdd,byyyy’ ’mmm.dd.yy’ ’yy--mm--dd’ ’dddyy’ ’dd.mm.yyyy’

6-98

Result 1996-11-30 96/03/30 03/30/96 03 30 96 Mar 30, 1996 Mar.30.96 96--03--30 08996 30.03.1996

Teradata RDBMS for UNIX SQL Reference

This is the ANSI format

SQL Expressions TIME

TIME Introduction

Form Example

6

The TIME built-in value provides the current time. 6

Time is based on a 24-hour day, and is stored as HHMMSS.CC (hours, minutes, seconds, and hundredths of a second).

6

The format of the TIME built-in value is defined as 99:99:99; the type is FLOAT, and the length is 8. For example:

6

SELECT TIME (FORMAT ’99:99:99.99’);

The hundredths of a second are not displayed by the default format; they can be displayed by overriding the format. This is not the ANSI form of time. It will be flagged if the SQL flagger is enabled. The following example inserts a row in a hypothetical table (in which the column ’InsertTime’ is data type FLOAT) and records the time that the row was inserted: INSERT INTO HypoTable (ColumnA, ColumnB, InsertTime) VALUES (’Abcde’, 12345, TIME);

Teradata RDBMS for UNIX SQL Reference

6-99

SQL Expressions USER

USER Introduction

The USER built-in value provides the user name of the current user. 6

Internal representation of the format of the USER value is as follows: VARCHAR(30) NOT NULL,

Example

You can identify the current user with the following statement: 6

SELECT user ;

The system responds with something like: User SQL01

The following example selects the job title for the current user. SELECT JobTitle FROM Employee WHERE Name = USER;

6-100

Teradata RDBMS for UNIX SQL Reference

6

SQL Expressions NULL

NULL

6

The NULL built-in value has no data type. NULL can be assigned to a field to indicate that the value is not known or not applicable to that field. If a row is inserted into a table without specifying values for some of the columns, the columns will be set to NULL, assuming there is no default value or a column has not been defined as NOT NULL.

Teradata RDBMS for UNIX SQL Reference

6-101

SQL Expressions Indicator Variables

Indicator Variables In indicator mode, the Teradata RDBMS returns data in client system internal format. Immediately preceding the first response row is a DataInfo parcel containing information on the total number of columns returned, the data type, and length of each column. Each response row begins with indicator variables corresponding to each data item in that row. The indicator variables are: 0

Indicates that the value in the corresponding field is not NULL or is non-nullable.

1

Indicates that the value in the corresponding field is NULL.

Indicator variables are processed by the Teradata RDBMS as follows: ●

● ●

One indicator variable corresponds to each data item (field) of a response row. Each indicator variable occupies one bit of space. If there are n data fields, the first (n + 7)/8 bytes of a response row contain the indicator variables for the data in that row. For example, if a response row contains 19 data items, then (19 + 7)/8 = 3 bytes contain the indicator variables for that row.)



6-102

Indicator variables are held in the minimum number of eight-bit bytes required to store them; unused bits are zeroed.

Teradata RDBMS for UNIX SQL Reference

6

Queries: The SELECT Statement

Chapter 7

Queries: The SELECT Statement

TeradataR DBMS for UNIX SQL Reference

Queries: The SELECT Statement

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement About This Chapter 7

About This Chapter

7

SQL queries are performed using the SELECT statement. The form, usage, and examples of the SELECT statement are described in this chapter, along with the FROM list, and the various SELECT statement clauses. This chapter describes the following: ● ● ● ● ● ● ● ● ●

The SELECT statement Simple SELECT statement WHERE clause, including subqueries using logical expressions FROM list, including derived tables WITH clause GROUP BY clause HAVING clause ORDER BY clause DISTINCT

This chapter also describes the following query expressions (also referred to as set operator expressions): ● ● ●

UNION INTERSECT EXCEPT (MINUS)

Teradata RDBMS for UNIX SQL Reference

7-1

Queries: The SELECT Statement SELECT

SELECT The SELECT statement returns row data in the form of a result table.

Function Syntax

7

SELECT SEL

*

DISTINCT ALL

A

,

expr aname AS tname.* ,

A

FROM

tname

B

aname

Single Tables

AS

joined_table

JOIN joined_table ON search_cond INNER LEFT RIGHT OUTER FULL CROSS JOIN single_table

( subquery )

derived_tname

Derived Tables

,

AS

Joined Tables

( col_name ) B WHERE search_cond GROUP BY

C

, col_name col_pos expr

C

HAVING cond

D

, ORDER BY

expr col_name col_pos

ASC DESC

D WITH

;

, expr 1

, BY

expr 2 ASC DESC

7-2

Teradata RDBMS for UNIX SQL Reference

FF06R023

Queries: The SELECT Statement SELECT

where: Syntax Element . . .

Specifies . . .

DISTINCT

that only one row is to be returned from any set of duplicates that might result from a given expr-list. Two rows are considered duplicates only if each value in one is equal to the corresponding value in the other.

ALL

that all rows, including duplicates, are to be returned in the results of the expr-list. This is the default value.

*

that all columns of all tables referenced in the FROM clause be returned. When qualified by tname, specifies that all columns of only tname are to be returned. View columns are explicitly enumerated when views are defined; therefore, if a table is changed after a view is defined, those changes will not appear if the SELECT * construct is used.

expr

an expression. Aggregate and arithmetic operators may be used with expressions.

AS

an introduction to the alias name, aname. Optional.

aname

a different, temporary name (alias) for the table that is referenced by tname. aname must be used during a self-join operation on the table. aname is also used to name expressions.

tname

the name of a table or view. The tname.* in the SELECT list can define the table from which rows are to be returned when two or more tables are referenced in the FROM clause.

FROM

an introduction to the names of one or more tables, views, or derived tables from which expr is to be derived. See the section FROM list for detail. Single Table

This option allows the FROM list to specify single tables. A FROM list may include a sequence of “single table” references. In this case, an implicit join is created. This is distinguished from explicit joins where the keyword JOIN is part of the syntax. tname

the name of a single table or view referred to in the FROM list.

AS

an introduction to the alias name, aname.

aname

a temporary name (alias) for the table that is referred to in the FROM list, tname. Joined Tables

Joined tables options allows the FROM list to specify multiple tables, joined in explicit ways, as described below. joined_table

the name of a joined table.

Teradata RDBMS for UNIX SQL Reference

7-3

Queries: The SELECT Statement SELECT Syntax Element . . .

Specifies . . .

INNER

a join in which qualifying rows from one table are combined with qualifying rows from another table according to some join condition. Types of inner joins include exclusion, merge, nested, product, and row ID. This is the default join type.

OUTER

a join in which qualifying rows from one table that do not have matches in the other table, are included in the join result. The rows from the first table are extended with null values.

LEFT OUTER

the table that was listed first in the FROM clause. In a LEFT OUTER JOIN, the rows from the left table that are not returned in the result of the inner join of the two tables, are returned in the outer join result, and extended with null values.

RIGHT OUTER

the table that was listed second in the FROM clause. In a RIGHT OUTER JOIN, the rows from the right table that are not returned in the result of the inner join of the two tables, are returned in the outer join result, and extended with null values.

FULL OUTER

that rows be returned from both tables. In a FULL OUTER JOIN, rows from both tables that have not been returned in the result of the inner join, will be returned in the outer join result, and extended with null values.

JOIN

an introduction to the name of the second table to participate in the join.

joined_table

the name of the joined table.

ON search_condition

one or more conditional expressions that must be satisfied by the result rows. An ON condition clause is required if the FROM clause specifies an outer join.

CROSS JOIN

an unconstrained, or Cartesian join; it returns all rows from all tables specified in the FROM clause.

single_table

the name of table participating in join. Derived Tables

The derived table option allows the FROM list to specify a spool file, comprised of selected data from an underlying table(s). The derived table acts like a viewed table. This is an extension of a current functionality. It is a full ANSI SQL feature.

7-4

(subquery)

nested SELECT statements.

AS

an introductory clause to derived table name. Optional.

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement SELECT Syntax Element . . .

Specifies . . .

derived_tname

the name of derived table.

col_name

the column name. The col_name field is for the columnname only; forms such as Databasename.Tablename.Columnname, or Tablename.Columnname should not be used.

WHERE

an introduction to the search condition in the SELECT statement.

search_cond

a conditional search expression that must be satisfied by the row(s) returned by the statement.

GROUP BY

an introduction to a reference to one or more expressions in the select expression list.

col_name

a group for which an aggregate operation is specified in the expr-list. Col_name specifies the name(s) of columns used in the GROUP BY clause in the SELECT statement.

col_pos

the numeric position of the column(s) used in the GROUP BY clause.

expr

the expression(s) used in the GROUP BY clause.

HAVING

an introduction to the conditional clause in the SELECT statement.

cond

one or more conditional expressions that must be satisfied by the result rows. Aggregate operators may be used with HAVING. HAVING cond selects rows from a single group defined in the select expr-list that has only aggregate results, or it selects rows from the group or groups defined in a GROUP BY clause.

ORDER BY

the order in which result rows are to be sorted.

col_name

specifies the name(s) of columns used in the ORDER BY clause in the SELECT statement. These can be ascending or descending.

col_pos

specifies the numeric position of the column(s) used in the ORDER BY clause. This can be ascending or descending.

expr

refers to an expression in the SELECT expr-list, either by name, or by means of a constant that specifies the numeric position of the expression in the expression list. If the sort field is a character string, the expr used in the ORDER BY phrase can include a type modifier to force the sort to be either CASESPECIFIC or NOT CASESPECIFIC. See “Specifying Case for Character Data” of Chapter 5, “Data Definition.”

Teradata RDBMS for UNIX SQL Reference

7-5

Queries: The SELECT Statement SELECT Syntax Element . . .

Specifies . . .

ASC DESC

either an ascending or a descending sort order. The default order is ASC. ASC specifies ascending order; DESC specifies descending order. If a sort option is not given, result values are sorted in ascending order according to the client system’s collating sequence. If ORDER BY is not specified, rows are returned unsorted.

WITH expr1

WITH is a keyword introducing the condition to be fulfilled by the SELECT statement. Specifies a summary line (such as a total) for the values in a column (expr) of the select result. Expr1 may contain one or more aggregate expressions that are applied to column values.

BY expr2

specifies one or more result columns (expressions) for which expr1 is provided. BY is valid only when used with WITH. * Expr2 may refer to an expression in the select expression list either by name or by means of a constant that specifies the numeric position of the expression in the expr-list.

ASC DESC

either an ascending or a descending sort order. The default order is ASC. ASC specifies ascending order; DESC specifies descending order. If a sort option is not given, result values are sorted in ascending order according to the client system’s collating sequence. If the BY clause is not specified, at that level, there is no ordering of the result.

Note: WITH is a Teradata RDBMS extension to ANSI SQL. Note: The syntax shown specifies the ANSI SQL ordering of options FROM, WHERE, GROUP BY, HAVING, and ORDER BY. Teradata RDBMS does not enforce this ordering, but it is recommended for ANSI compliance.

Privilege Restrictions

READ LOCK on Tables, Rows Referenced

7-6

7

7

To select data from a table, the user must have SELECT privilege on that table. To select data through a view, the user must have the SELECT privilege on that view. Also, the immediate owner of the view (that is, the database in which the view resides) must have SELECT WITH GRANT OPTION privileges on all tables or views referenced in the view. A select operation sets a Read lock on the tables or rows referenced in the SELECT statement. If the SELECT statement references a view, then a Read lock is placed on the underlying tables.

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement SELECT

Only SELECT Uses Set Operators (Query Expressions)

Joins

Success Response Indicates Returned Rows

Uses of the SELECT Statement

7

7

7

The SELECT statement is the only SQL statement that can use the set operators (query expressions) UNION, INTERSECT, and MINUS. The set operators allow the user to manipulate the answers to two or more queries by combining the results of each query into a single result set. A select operation can reference data in one or more tables and/or views. In this case, the statement defines a join of the tables or views. When the result of a SELECT statement is returned to a user, the activity count that is part of the success response indicates the total number of returned rows. If no rows are returned, the activity count equals zero. The SELECT statement is the most frequently used SQL statement. The SELECT statement may be used to specify:

7

● ● ●

The set of result data that is returned The order in which result data is returned The format in which result data is returned

The abbreviation for the SELECT statement is SEL, but the full spelling is recommended for ANSI compliance. The following sections describe how to use the SELECT statement to request data from the Teradata RDBMS. Topics include: ● ● ● ● ● ● ●



The FROM List (Clause)

7

Simple SELECT statement WHERE clause, including subqueries FROM list WITH clause GROUP BY clause HAVING clause ORDER BY clause ● CASESPECIFIC option ● International sort orders DISTINCT option

A SELECT can be used in an outer query, main query or subquery. The SELECT syntax has changed in this release, to require that tables referenced in the SELECT must be specified in a FROM clause if there are any subqueries in the request. Requests with no subqueries are still allowed to avoid regression problems. Such requests are not ANSI-compliant and NCR strongly

Teradata RDBMS for UNIX SQL Reference

7-7

Queries: The SELECT Statement SELECT

suggests that new scripts avoid using this non-ANSI syntax.and that existing scripts be revised as is practicable. If a SELECT is specified in a subquery, any table referenced in the subquery must be specified in the FROM clause of either the subquery or some outer query. The following examples illustrate the use of SELECT:

Examples Example 1: Valid SELECT in All Releases

Example 2: Non Valid SELECT in Current Release, Valid Previously

The following queries are syntactically correct in all releases: 7

SELECT date, time;1 SELECT x1,x2 FROM t1,t2 WHERE t1.x1=t2.x2; SELECT x1 FROM t1 WHERE x1 IN (SELECT x2 FROM t2);

The following queries are syntactically correct in all previous releases but now invalid: 7

SELECT * FROM t1 WHERE t1.x1 IN (SELECT t2.x2); SELECT * FROM t1 WHERE t1.x1 IN (SELECT x2 FROM t2 WHERE t2.n=t3.n);

These statements are not valid because table t2 is referenced in the subquery and not in the main query. Also, subqueries always require a FROM clause.

Example 3: Valid SELECT, Previously Invalid

7

The following query is illegal in previous releases (as long as no table named p with a column named department exists in the default database) but legal in the current release: SELECT name FROM personnel p WHERE salary = (SELECT MAX(salary) FROM personnel sp WHERE p.department=sp.department);

The above query returns the name of employees who have the highest salary in each department.

1This query references no tables, hence no FROM clause is needed. This form is flagged as non-ANSI SQL when the SQL flagger is enabled.

7-8

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement SELECT

Example 4: SELECT Executed Differently in Previous Releases

The following query is syntactically correct in all releases but has a different meaning in the current release than in previous releases: 7

SEL PubName FROM Publisher WHERE 0 = (SELECT COUNT(*) FROM book WHERE book.PubNum=Publisher.pubnum);

In previous releases, the above query will not retrieve any publisher name if any publisher has some books in the library, which is not a very useful result in this case. In V2R2.0 and future releases, even if some publishers might have some books in the library, the above query still retrieves the name of a publisher if this publisher has no books in the library. The difference occurs because of the implementation of correlated subqueries. See Chapter 9, “Advanced SQL,” Correlated Subqueries, for a detailed explanation.

Teradata RDBMS for UNIX SQL Reference

7-9

Queries: The SELECT Statement Simple SELECT Statement

Simple SELECT Statement A simple form of the SELECT statement returns a result that is determined by a list of expressions.

Function Syntax

, SELECT SEL

expr

FROM

tname

tname *

; FF06A024

The following four examples demonstrate the use of the simple SELECT statement.

Examples Example 1

7

7

In the following example, Name is the only expression in the SELECT expr-list. The statement returns all of the employee names in the Employee table: SELECT Name FROM Employee;

Example 2

7

The following statement selects all of the data (all columns and rows) in the Employee table: SELECT * FROM Employee;

Example 3

7

The following statement selects the name, job title, and department number for each employee in the Employee table (in this example, Name, JobTitle, and DeptNo are expressions in the select expression list): SELECT Name, JobTitle, DeptNo FROM Employee;

Example 4

7

The following statement selects the name of, and calculates the monthly salary for, each employee: SELECT Name, Salary/12 AS MSalary FROM Employee;

The expression list of the preceding SELECT statement contains two expressions: ● ●

7-10

Name Salary/12 AS MSalary

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement WHERE Clause

WHERE Clause

7

The WHERE clause of the SELECT statement selects rows that satisfy a conditional expression. The WHERE clause can also be used in DELETE, UPDATE, INSERT-SELECT, and ROLLBACK/ABORT statements.

Function

Syntax

WHERE search_cond FF06A011

where: Syntax Element . . .

Is . . .

search_cond

a search condition is a conditional expression as described in Chapter 6. The arguments can be individual values and or include subqueries, but the overall expression must be of a form that returns a single boolean (TRUE/FALSE) result. Note: Logical expressions include comparisons of numeric values, character strings, and partial string comparisons.

Except when used in the ROLLBACK statement, the WHERE conditional may not specify an aggregate operation. However, the conditional may be applied to an aggregate result returned by a subquery.

Usage Notes

Use WHERE with AND and OR

7

The WHERE clause can be used with the AND and OR operators. For example, the following statement selects employees who are in department 100 and who have either a college degree or at least 5 years of experience: SELECT * FROM Employee WHERE Dept = 100 AND (EdLev >= 16 OR YrsExp >= 5);

Use WHERE to Search for a Character String

7

The WHERE clause may be used to select data by searching for a character string. For example, the following statement is processed by searching the Employee table for every row that satisfies the condition: the JobTitle field contains the character string “analyst”. The Name and DeptNo fields for those rows are then listed. SELECT Name, DeptNo FROM Employee

Teradata RDBMS for UNIX SQL Reference

7-11

Queries: The SELECT Statement WHERE Clause WHERE UPPER (JobTitle) LIKE ’%ANALYST%’ ;

WHERE in JOINS

Example

7

7

The WHERE clause can define a condition for joining table rows; namely, that the values in a common column must match in both tables. The following statement, which requests the name of each employee from the Employee table and the location of each employee’s department from the Department table, is processed by joining the Employee and Department tables: SELECT Name, Loc FROM Employee, Department WHERE Employee.DeptNo = Department.DeptNo ;

JOIN Efficiency and Indexes Example

7

7

The efficiency of a join operation depends on whether the WHERE condition uses values for columns on which primary or secondary indexes are defined. If indexes are defined on the DeptNo columns in both the Employee and Department tables, specifying an equality condition between the values in each indexed column, as in the preceding example, allows the rows in the two tables to be matched using the values in both indexed columns. Efficiency is increased if a primary index is defined on one or both of these columns. For example, define DeptNo as the unique primary index for the Department table.

EXISTS

7

The EXISTS (logical ∃) predicate is supported as the predicate in a search condition. The meaning of the exists predicate is to test the result of the subquery. If execution of the subquery would return response rows then the where condition is considered satisfied. Use of the NOT qualifier for the EXISTS predicate reverses the sense of the test. Execution of the subquery does not, in fact, return any response rows. It simply returns a boolean to indicate whether responses would or would not be returned. The subquery may be correlated with an outer query. Refer also to Chapter 6, “SQL Expressions,” “Logical Expressions: EXISTS” for more detail on using EXISTS.

7-12

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement WHERE Clause

Using Unconstrained Joins (No WHERE clause)

SQL allows unconstrained joins; that is, joins in which a WHERE is not specified for the tables that are joined. 7

Caution:

The result of an unconstrained join is a Cartesian product, which is the product of the number of rows in each table that is joined. An unconstrained join might produce a great many rows, returning a result that the user may not have intended. Also, it might place a heavy load on system resources. Consider carefully before specifying an unconstrained join. If a SELECT uses both alias and real names (for example, alias names in a WHERE clause and real names in the SELECT), the Teradata RDBMS does an unconstrained join. The following examples illustrate the use of the WHERE clause:

Examples Example 1

7

The following statement may be used to select the name and job title of every employee in Department 100. In this statement, “DeptNo = 100" is the conditional expression. SELECT Name, JobTitle FROM Employee WHERE DeptNo = 100;

Example 2

7

he following statement returns only those rows from the EMployee table whose values in the EmpNo column match the values in the MgrNo column of the Department table. Note the use of tname.* form of the SELECT list. SELECT Employee.* FROM Employee, Department WHERE Employee.EmpNo=Departmentt.MgrNo;

Example 3

7

If EmpNo is defined as CHAR(5), then EmpNo values should be compared with character values in the WHERE condition, as: WHERE EmpNo = ‘12345’

Teradata RDBMS for UNIX SQL Reference

7-13

Queries: The SELECT Statement Subqueries in Search Conditions

Subqueries in Search Conditions Introduction

Subqueries can also be used in search conditions. 7

Syntax

expr , (

expr

comparison_operator )

(query )

IN

ANY

NOT IN

SOME ALL HH01A064

The following is the syntax for a special case of using subqueries in logical expressions: EXISTS

(query ) HH01A065

The following examples illustrate the use of subqueries in search conditions:

Examples Example 1

7

The following request can be used to select the names and department locations of those employees who report to manager Aguilar (whose EmpNo is 10007): SELECT Name, Loc FROM Employee, Department WHERE Employee.DeptNo = Department.DeptNo AND Employee.DeptNo IN ( SELECT DeptNo FROM Department WHERE MgrNo = 10007 ) ;

Example 2

7

The following statement finds every employee in the Employee table with a salary that is greater than the average salary of all employees in the table: SELECT Name, DeptNo, JobTitle, Salary FROM Employee WHERE Salary > ( SELECT AVG(Salary) FROM Employee) ORDER BY Name ;

The statement returns: Name -----------Aguilar J Carter J Omura H Phan A Regan R Russell S Smith T Smith T Watson L

7-14

DeptNo -----600 500 500 300 600 300 500 700 500

JobTitle -----------Manager Engineer Programmer Vice Pres Purchaser President Engineer Manager Vice Pres

Teradata RDBMS for UNIX SQL Reference

Salary ---------45,000.00 44,000.00 40,000.00 55,000.00 44,000.00 65,000.00 42,000.00 45,000.00 56,000.00

7

Queries: The SELECT Statement Subqueries in Search Conditions

Example 3

7

The following statement retrieves the employee who has the highest salary and the employee who has the most years of experience: SELECT EmpNo,Name,JobTitle,Salary,YrsExp FROM Employee WHERE (Salary,YrsExp) >= ALL (SELECT Salary,YrsExp FROM Employee) ;

The result shows that a single employee has both the highest salary and the most years of experience: EmpNo Name JobTitle Salary YrsExp ----- ------------ ------------ ---------- -----10018 Russell S President 65,000.00 25

Teradata RDBMS for UNIX SQL Reference

7-15

Queries: The SELECT Statement FROM Clause

FROM Clause

7

The FROM clause of the SELECT statement lists the tables or views that are referenced by the select operation, as illustrated by the preceding SELECT examples

Function

The FROM clause also may be used to define a temporary alias name for a table or view. An alias name must be used when a table is to be joined with itself for a self-referencing operation, or to simplify the references to columns.

Usage Notes

7

If the FROM clause refers to a table name or view name that is used in more than one database, you must use the fully-qualified form of the name, thus: databasename.tablename

If a SELECT statement is a subquery, it must contain a FROM list and every field referenced in the SELECT statement must be in a table referenced either in the FROM list for the subquery, or some FROM list in an outer query containing the subquery. ANSI SQL also requires that all fields referenced in an outer query be in tables listed in the FROM list. Teradata RDBMS does not require this, but it is recommended.

Syntax

, FROM

tname Single Tables

aname AS

joined_table

JOIN joined_table ON search_cond INNER LEFT RIGHT FULL

CROSS JOIN ( subquery )

Joined Tables

OUTER

single_table

derived_tname AS

, ( col_name )

Derived Tables FF06A013

7-16

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement FROM Clause

where: Syntax Element . . .

Is . . . Single Tables

Note that a FROM list may include a sequence of “single table” references. In this case, an implicit join is created. This is distinguished from explicit joins where the keyword JOIN is part of the syntax. tname

the name of a table, view or macro. If database is omitted, it is inferred from context.

AS aname

a different, temporary name (alias) for the table that is referenced by tname. aname must be used during a self-join operation on the table. The use of AS introducing aname is optional. Joined Tables

Joined tables options allows the FROM list to specify multiple tables, joined in explicit ways, as described below. joined_table

either (and usually is) a single table name with optional alias name, or a joined table, indicating nested joins.

INNER

a join in which qualifying rows from one table are combined with qualifying rows from another table according to some join condition. Types of inner joins include exclusion, merge, nested, product, and row ID. This is the default join type.

OUTER

a join in which qualifying rows from one table that do not have matches in the other table, are included in the join result. The rows from the first table are extended with null values.

LEFT [OUTER]

a signifier for the join type. LEFT indicates the table that was listed first in the FROM clause. In a LEFT OUTER JOIN, the rows from the left table that are not returned in the result of the inner join of the two tables, are returned in the outer join result, and extended with null values.

RIGHT [OUTER]

a signifier for the join type. RIGHT indicates the table that was listed second in the FROM clause. In a RIGHT OUTER JOIN, the rows from the right table that are not returned in the result of the inner join of the two tables, are returned in the outer join result, and extended with null values.

Teradata RDBMS for UNIX SQL Reference

7-17

Queries: The SELECT Statement FROM Clause Syntax Element . . .

Is . . .

FULL [OUTER]

a signifier for the join type. FULL OUTER JOIN returns rows from both tables. In a FULL OUTER JOIN, rows from both tables that have not been returned in the result of the inner join, will be returned in the outer join result, and extended with null values.

CROSS JOIN

a signifier for the join type. A CROSS JOIN is an unconstrained, or Cartesian join; it returns all rows from all tables specified in the FROM clause. Two joined tables can be CROSS joined.

ON search_cond

one or more conditional expressions that must be satisfied by the result rows. An ON condition clause is required if the FROM clause specifies an outer join. Derived Tables

The derived table allows the FROM list to specify a spool file, comprised of selected data from underlying table(s). The derived table acts like a viewed table.

subquery

the subquery that defines the derived table contents.

AS derived_tname

an assigned name for the temporary derived table. AS in an introductory optional keyword for derived table. a list of column names or expressions listed in the subquery. Allows referencing subquery columns by name.

col_name

The following provides rules for using the FROM list in queries:

Usage Notes Self-Joins

FROM Clause Using Both a Permanent Table and an Alias Name

7-18

7

During a self-join operation, related data selected from different rows of the same table is combined and returned as one row. The temporary table names that are defined using the FROM clause qualify different references to the same table columns. When a FROM clause references both a permanent table and an alias name, whether a self-join is performed depends on how the column references are qualified.

7

For a successful self-join operation, column references must be fully qualified (that is, specified in the form tablename.columname) and the qualifiers should involve both the permanent and the alias table names, as shown in the following example.

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement FROM Clause

Once an alias is declared, any subsequent reference to the base table name causes a new instance of the table to be used; the result of the select may be a Cartesian self-join. In the following example, table TAB1 is given the alias T; the subsequent use of TAB1 causes the Cartesian product. SELECT * FROM TAB1 T WHERE TAB1.COL1 = 2;

How Column References Affect Self-Joins

7

The effect of column references on self-join processing is as follows (these rules apply only to cases where a single aliasing FROM clause is supplied). See also Table 7-1:

IF this column reference . . .

Is . . .

THEN a self-join is . . .

all

unqualified (the preceding

not performed.

qualified (but the qualifiers reference only the alias name)

not performed.

qualified and the qualifiers reference only the alias name

not performed.

qualified and the qualifiers reference only the permanent table name

moot because no assumptions can be made about the owner of the unqualified columns.

some, but not all

tablename or aliasname is omitted)

This condition producers the following error message, where n is a number indicating the position of the statement in the request. *** Failure 3809 Column ’columnname’ is ambiguous. Statement# n, Info = 0

Teradata RDBMS for UNIX SQL Reference

7-19

Queries: The SELECT Statement Derived Tables

Derived Tables Introduction

A table can be a base table, a derived table, or a view. 7

A derived table is obtained from one or more other tables through the results of a query. Derived tables can do all of the following things: ●

Enhance performance. How derived tables are implemented determines how or if performance is enhanced or not. For example, one way of optimizing a query is for the user to control how data from different tables is accessed in joins, by using derived tables. The use of a derived table in a SELECT forces the subquery to create a spool file, which then becomes the derived table. Derived tables can then be treated in the same way as base tables.





Avoid CREATE and DROP TABLE statements for storing retrieved information. Assist in optimizing joins (see Chapter 3).

Note: The scope of derived tables is only visible to the level of the SELECT statement calling the subquery. Derived tables are one of the options in the FROM list, and can also be used in the ABORT and DELETE statements. The use of derived tables in the FROM clause is fully ANSI compliant.

Rules for Using Derived Tables

Adhere to the following rules when using derived tables: ● 7



A unique table name is required for the derived table. A column name list is optional if the fields in the SELECT list of the derived table are unique. For example: SELECT * FROM (SELECT a FROM t1) t5; SELECT * FROM (SELECT a FROM t1) t5(x);



A column name list is required if there is an expression in the SELECT list of the derived table. For example: SELECT * FROM (SELECT MAX(a) FROM t1) t5(x); SELECT * FROM (SELECT MAX(a), a+b FROM t1) t5 (x,y);

7-20

Teradata RDBMS for UNIX SQL Reference

7

Queries: The SELECT Statement Derived Tables ●

A column name list is required if the fields in the SELECT list of the derived table are not unique. For example: SELECT * FROM (SELECT t1.a, t2.a FROM t1, t2) t5 (x,y);



Derived Tables and Multilevel Aggregates

7

The number of fields in the column name list for the derived table must be the same as the number of fields in the SELECT list of the derived table.

Aggregates are not allowed in WHERE phrases. Because of this, it has been difficult to obtain values such as ‘above-average’ salary in a single query. The derived table feature makes such calculations easy to perform. The following example shows how derived tables facilitate this: SELECT Name, Salary, Average_Salary FROM (SELECT AVG(Salary) FROM Employee) Workers (Average_Salary), Employee WHERE Salary > Average_Salary ORDER BY Salary DESC;

returns: Name Russel S Watson L Phan A Aguilar J Carter J

Salary 55,000.00 56,000.00 55,000.00 45,000.00 44,000.00

Average_Salary 38,147.62 38,147.62 38,147.62 38,147.62 38,147.62

In this example, the WHERE condition compares values from rows in the base table Employee with the (in this case single) values of the field Average_Salary in the derived table, Workers. A more elaborate example is to group the same information by DeptNo: SELECT Name, Salary, DeptNo, Average_Salary FROM (SELECT AVG(Salary), DeptNo FROM Employee GROUP BY DeptNo) Workers (Average_Salary, DeptNum), Employee WHERE Salary > Average_Salary AND DeptNum = DeptNo ORDER BY DeptNo,Salary DESC;

Here the request is to return salaries above the department averages. This returns: Name Chin M Moffit H Russel S Phan A Watson L Carter J Smith T Aguilar J

Salary 38,000.00 35,000.00 55,000.00 55,000.00 56,000.00 44,000.00 42,000.00 45,000.00

DeptNo 100 100 300 300 500 500 500 600

Teradata RDBMS for UNIX SQL Reference

Average_Salary 32,625.00 32,625.00 47,666.67 47,666.67 38,285.71 38,285.71 38,285.71 36,650.00

7-21

Queries: The SELECT Statement Derived Tables

Here the derived table is a grouped table, providing a set of rows. The outer WHERE clause needs an equality join between the Department Number of the base table, Employee, and the derived table. The following illustrate the use of the FROM list in queries:

Examples Example 1: FROM List

7

The following statement may be used to list employees who have more years of work experience than their department managers: SELECT Workers.Name, Workers.YrsExp, Workers.DeptNo, Managers.Name, Managers.YrsExp FROM Employee Workers, Employee Managers WHERE Managers.DeptNo = Workers.DeptNo AND UPPER (Managers.JobTitle) IN (’MANAGER’ OR ’VICE PRES’) AND Workers.YrsExp > Managers.YrsExp ;

The FROM clause in the preceding statement allows the Employee table to be treated as though it were two identical tables; one named “Workers,” the other named “Managers.” As in a normal join operation, the WHERE clause defines the conditions of the join, establishing DeptNo as the column whose values are common to both tables. The request is processed by first selecting “Managers” rows that contain a value of either ‘MANAGER’ or ‘VICE PRES’ in the JobTitle field. These rows are then joined to the “Workers” rows using a merge-join operation with the following join condition: ●



A “Workers” row must contain a DeptNo value that matches the DeptNo value in a “Managers” row. The matching “Workers” row must also contain a YrsExp value that is greater than the YrsExp value in the “Managers” row.

The following result is returned: Name YrsExp DeptNo -------- ------ -----Greene W 15 100 Carter J 20 500 Aguilar J 11 600 Leidner P 13 300 Russell S 25 300

Example 2: FROM List OUTER Join

7-22

7

Name -------Jones M Watson L Regan R Phan A Phan A

YrsExp -----13 8 10 12 12

The following example illustrates an outer join. In the example, the Skills table lists various skills and the associated skill number, and the Employee table lists employee numbers along with the employee‘s skill.

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement Derived Tables

Skills SkillNo

SkillName

1

baker

2

doctor

3

farmer

4

lawyer

5

mason

6

tailor

EmpNo

SkillNo

6123

1

6234

1

6392

3

7281

5

7362

4

6169

1

Employee

You could use the following query to determine which skill areas do not have assigned employees: SELECT Skills.SkillName, Employee.EmpNo FROM Skill LEFT OUTER JOIN Employee ON Skills.SkillNo = Employee.SkillNo;

The following result is returned SkillName

EmpNo

baker

6123

baker

6234

baker

6169

doctor

null

farmer

6392

lawyer

7362

mason

7281

tailor

null

In order to include all skills in the result, an OUTER JOIN must be used. An implicit join, using just FROM Employee, Skills does not return rows for nulls and would not list doctor or tailor in the above result.

Teradata RDBMS for UNIX SQL Reference

7-23

Queries: The SELECT Statement WITH Clause

WITH Clause

7

The WITH clause of the SELECT statement specifies summary lines and breaks (grouping conditions) that determine how selected results are returned (typically used for subtotals).

Function

Syntax

, WITH

expr 1 , BY

expr 2 ASC DESC

FF06A014

where: Syntax Element . . .

Specifies . . .

WITH

the keyword introducing the condition to be fulfilled by the SELECT statement.

expr1

the use of expr following the WITH keyword. It implies a summary line (such as a total) for the values in a column (expr) of the select result. Expr1 can contain one or more aggregate and arithmetic operators that are applied to column values.

BY expr2

one or more result columns (expressions) for which expr1 is provided. BY is valid only when used with WITH. expr2 can be considered as a group condition. Expr2 may refer to an expression in the select expression list either by name or by means of a constant that specifies the numeric position of the expression in the expr-list.

ASC DESC

the sort order. The default order is ASC. ASC specifies ascending order. DESC specifies descending order. If a sort option is not given, result values are sorted in ascending order according to the client system’s collating sequence. If ORDER BY is not specified, rows are returned unsorted.

7-24

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement WITH Clause

The following expressions can be used:

Expressions



Expressions operated on by aggregate operators (SUM, AVERAGE, COUNT, MIN, or MAX). An aggregate operator must be specified directly before each column to which the operator applies, for example, WITH SUM(Salary) or MAX(YrsExp).



Expressions associated with the field values of an expression contained in the BY phrase, for example, WITH DeptNo, SUM(Salary) BY DeptNo.

The WITH clause is a Teradata RDBMS extension to ANSI. If the SQL flagger is enabled, it will be flagged.

TITLE Phrases

7

A TITLE phrase can be used to specify a title for any expression contained in expr1 and the SELECT expression list. The TITLE phrase must be enclosed by parentheses and follow the entire expression to which it applies. Title is relevant only for FieldMode output for report generation and normally done only via BTEQ. In the following statement, the title “Subtotal” is listed at each summary row: WITH SUM(Salary)(Title ’Subtotal’)

In the following statement, a blank title is specified: WITH SUM(Salary)(Title ’ ’)

Expr2 is an expression that determines where summary lines are generated. For example, “BY DeptNo” specifies a summary for each value in the DeptNo column; a summary line is generated following a listing of the values for each DeptNo. If the BY phrase is not used, the summary line applies to the entire result, as specified by the SELECT expression list. As in the ORDER BY clause, the values of any expression specified by expr2 may be sorted in ascending (ASC) or descending (DESC) order. For example: WITH SUM(Salary) BY DivNo ASC, DeptNo DESC

Likewise, expr2 may specify a constant that references an expression by its position in the SELECT expression list. For example: WITH SUM(Salary) BY 2 ASC, 3 DESC

However, an expression that is specified in expr1 or expr2 need not appear in the SELECT expression list.

Teradata RDBMS for UNIX SQL Reference

7-25

Queries: The SELECT Statement WITH Clause

The following statement can be used to generate a departmental salary report that contains a detail line for each employee and a subtotal line for each department:

Example

SELECT Name, DeptNo, Salary FROM Employee WITH SUM(Salary) BY DeptNo;

The result returned is: Name ---Peterson J Moffit H Jones M Chin M Greene W

Leidner P Phan A Russell S

Multiple WITH Clauses

7





7

Salary ----25,000.00 35,000.00 50,000.00 38,000.00 32,500.00 ---------Sum(Salary) 180,500.00 300 300 300

34,000.00 55,000.00 65,000.00 ---------Sum(Salary)154,000.00

More than one WITH clause can be used in a SELECT statement to specify different kinds of summaries. Each succeeding WITH clause refers to an ever-broader grouping of rows. ●

Example

DeptNo -----100 100 100 100 100

The BY phrase in the first-specified WITH clause defines the least important sort key. The BY phrase in the next-specified WITH clause defines the next-to-least important sort key. The final WITH clause defines the major sort key.

The following statement generates a report of employee salaries ordered by department, with a summary line of total salaries for each department and a final summary line of total salaries for the entire organization: SELECT Name, DeptNo, Salary FROM Employee WITH SUM(Salary) BY DeptNo WITH SUM(Salary);

7-26

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement WITH Clause

The result returned is: Name ---------Peterson J Moffit H Jones M Chin M Greene W

Leidner P Phan A Russell S

Smith T Clements D

Combined WITH and ORDER BY Clauses

Example

DeptNo ------100 100 100 100 100

Salary ----------25,000.00 35,000.00 50,000.00 38,000.00 32,000.00 ---------Sum(Salary) 180,000.00

300 300 300

34,000.00 55,000.00 65,000.00 ---------Sum(Salary) 154,000.00 500 700

42,000.00 38,000.00 ---------Sum(Salary) 113,000.00 ---------Sum(Salary) 851,000.00

An ORDER BY clause can be specified before or after any WITH clause. See also the “ORDER BY Clause” following. 7

7

When WITH and ORDER BY are used together, the WITH clause defines the major sort key and the ORDER BY clause defines the minor sort key. This is true regardless of the structure of the query or the number of WITH clauses. Both of the following statements use an ORDER BY clause to sort employee names in ascending order in each department group: SELECT Name, DeptNo, Salary FROM Employee ORDER BY Name WITH SUM(Salary) BY DeptNo WITH SUM(Salary); SELECT Name, DeptNo, Salary FROM Employee WITH SUM(Salary) BY DeptNo WITH SUM(Salary) ORDER BY Name;

The result returned is: Name ---------Chin M Greene W Jones M Moffit H Peterson J

DeptNo ------100 100 100 100 100

Salary ----------38,000.00 32,500.00 50,000.00 35,000.00 25,000.00 ---------Sum(Salary) 180,500.00

Teradata RDBMS for UNIX SQL Reference

7-27

Queries: The SELECT Statement WITH Clause . . . Brangle B Clements D Smith T

. . . 700 700 700

. . . 30,000.00 38,000.00 45,000.00 ---------Sum(Salary) 113,000.00 ---------Sum(Salary) 851,100.00

If any sort key column contains character data that was entered in mixed case, the results produced from WITH...BY and/or ORDER BY may be unexpected, depending on whether the CASESPECIFIC option was defined on the column and the collation in effect for the session (see the “ORDER BY Clause” earlier in this section, and the SET SESSION COLLATION statement).

Combined WITH and GROUP BY Clauses Example

7

7

Using WITH and GROUP BY clauses in the same SELECT statement may produce unintended results. See also the “GROUP BY Clause” in a following section. The following statement is intended to generate a report of salary totals by department with a grand total of employee salaries: SELECT DeptNo, SUM(Salary) FROM Employee GROUP BY DeptNo WITH SUM(Salary);

The result returned is: DeptNo -----100 300 500 600 700 Sum(Sa

Sum(Salary) ----------180,500.00 143,000.00 268,000.00 146,600.00 113,000.00 ----------851,100.00

As would be expected, the WITH clause produces a summary line of total salaries for all departments. However, the summary title is truncated because of the narrow width of the DeptNo column.

7-28

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement WITH Clause

If the WITH clause contains an unnecessary BY phrase: SELECT DeptNo, SUM(Salary) FROM Employee GROUP BY DeptNo WITH SUM(Salary) BY DeptNo;

a useless summary line is generated following each department salary total: DeptNo -----100 Sum(Sa

700 Sum(Sa

Sum(Salary) ---------180,500.00 ---------180,500.00 . . . 113,000.00 ----------113,000.00

Teradata RDBMS for UNIX SQL Reference

7-29

Queries: The SELECT Statement GROUP BY Clause

GROUP BY Clause Function

The GROUP BY clause of the SELECT statement groups result rows by the values in one or more columns.

Syntax

, GROUP BY

col_name col_pos expr FF06A015

where: Syntax Element . . .

Specifies . . .

GROUP BY

a reference to one or more expressions in the select expression list.

col_name

the name(s) of columns used in the GROUP BY clause in the SELECT statement.

col_pos

the numeric position of the column(s) used in the GROUP BY clause.

expr

the expression(s) used in the GROUP BY clause. Teradata SQL expressions are described in Chapter 6. col_name, col_pos, and expr can comprise single entries or a list.

Usage Notes

The GROUP BY clause creates summary information for a table. Each col_name in a GROUP BY clause is the name of a column referenced in the SELECT expression list. A col_pos is a positive integer that represents the numeric position of a column referenced in the SELECT expression list. When an aggregate operation (SUM, AVERAGE, MAX, MIN, or COUNT) is specified, GROUP BY can be used to return a summary row for each group. Aggregate operators can be used only in the SELECT expression list or in the optional HAVING clause. All non-aggregate groups in a SELECT expression list must be included in the GROUP BY clause. If an ORDER BY clause is used,

7-30

Teradata RDBMS for UNIX SQL Reference

7

Queries: The SELECT Statement GROUP BY Clause

any group contained in the ORDER BY clause must also be included in the GROUP BY clause. The use of col_pos or expr in GROUP BY is non-ANSI, and will be flagged when the SQL flagger is enabled. The following examples illustrate the use of the GROUP BY clause.

Examples Example 1

7

If the following statement was used to generate a report of salary totals by department: SELECT DeptNo, SUM(Salary) FROM Employee GROUP BY DeptNo;

the result returned would be: DeptNo -----100 300 500 600 700

Example 2

Sum(Salary) ----------180,500.00 143,000.00 268,000.00 146,600.00 113,000.00

The following statement: 7

SELECT Employee.DeptNo, Department.DeptName, AVG(Salary) FROM Employee, Department WHERE Employee.DeptNo = Department.DeptNo GROUP BY Employee.DeptNo;

returns an error message because a non-aggregate group listed in the SELECT expr-list (Department.DeptName) is not listed in the GROUP BY clause.

Example 3

The following statement: 7

SELECT Employee.DeptNo, AVG(Salary) FROM Employee, Department WHERE Employee.DeptNo = Department.DeptNo ORDER BY Department.DeptName GROUP BY Employee.DeptNo;

returns an error message because the non-aggregate group listed in the ORDER BY clause is not listed in the GROUP BY clause.

Teradata RDBMS for UNIX SQL Reference

7-31

Queries: The SELECT Statement HAVING Clause

HAVING Clause Function

The HAVING clause of the SELECT statement specifies a conditional expression that must be satisfied for a group of rows to be included in the result data. The condition may define one or more aggregates (MAX, MIN, AVG, SUM, COUNT), and may be applied to the rows of: ●



A single group defined in the SELECT expression list, which has only aggregate results One or more groups defined in a GROUP BY clause

Syntax

HAVING cond FF06A016

where: Syntax Element . . .

Specifies . . .

HAVING

the conditional clause in the SELECT statement.

cond

one or more conditional expressions that must be satisfied by the result rows. Aggregate operators may be used with HAVING. HAVING cond selects rows from a single group defined in the SELECT expression list that has only aggregate results, or it selects rows from the group or groups defined in a GROUP BY clause.

Usage Notes

When the WHERE, GROUP BY, and HAVING clauses are used together in a SELECT statement, the order of evaluation is: 1

WHERE

2

GROUP BY

3

HAVING

HAVING results in an error message when the tables referenced in the HAVING clause meet any of the following criteria: ● ● ●

7-32

Not declared in the FROM clause Do not appear in the SELECT expression list Do not appear in a non-aggregate condition.

Teradata RDBMS for UNIX SQL Reference

7

Queries: The SELECT Statement HAVING Clause

The following examples illustrate the use of the HAVING clause.

Examples Example 1: HAVING

7

The following statement can be used to display salary ranges for specified departments whose salaries average more than $37,000: SELECT DeptNo, MIN(Salary), MAX(Salary), AVG(Salary) FROM Employee WHERE DeptNo IN (100,300,500,600) GROUP BY DeptNo HAVING AVERAGE(Salary) > 37000;

The result is: DeptNo -----300 500

Example 2: HAVING

7

Minimum(Salary) Maximum(Salary) --------------- --------------23,000.00 65,000.00 22,000.00 56,000.00

Average(Salary) --------------47,666.67 38,285.71

The following example demonstrates the use of HAVING as applied to the aggregate results of a single group defined in the SELECT expression list. This application is particularly useful in a SELECT subquery. SELECT COUNT(Employee) FROM Department WHERE DeptNo = 100 HAVING COUNT(Employee) > 10 ;

Other examples of legitimate use of the HAVING clause: SELECT SUM(t.a) from t,u HAVING SUM(t.a)=SUM(u.a); SELECT SUM(t.a), SUM(u.a) from t,u HAVING SUM(t.b)=SUM(u.b); SELECT SUM(t.a) from t,u HAVING SUM(t.b)=SUM(u.b) and u.b = 1 GROUP BY u.b;

Aggregating a Join in a Having Clause Example:

Teradata SQL supports using the HAVING clause when referencing columns from two or more tables. 7

7

The columns named price and sales_qty are from two different tables t1, UnitPriceCost, and t2, SalesHist. To find which category of items is sold for a profit margin greater than $1000: SELECT t1.category, (t2.price - t2.cost) * SUM (t1.sales_qty)(NAMED margin) FROM SalesHist t1, UnitPriceCost t2 WHERE t1.prodno=t2.prodno GROUP BY t1.category,t2.price, t2.cost HAVING margin > 1000;

A subquery can have a join on a view with an aggregate operation and a HAVING clause which references more than one table.

Teradata RDBMS for UNIX SQL Reference

7-33

Queries: The SELECT Statement ORDER BY Clause

ORDER BY Clause Function

7

The ORDER BY clause of the SELECT statement specifies how result data is to be sorted.

Syntax

, ORDER BY

expr col_name col_pos

ASC DESC

FF06A017

where: Syntax Element . . .

Specifies . . .

ORDER BY

the order in which result rows are to be sorted.

col_name

the name(s) of columns used in the ORDER BY clause in the SELECT statement. These can be ascending or descending.

col_pos

the numeric position of the column(s) used in the ORDER BY clause. This can be ascending or descending.

expr

an expression on which to sort. If the sort field is a character string, the expr used in the ORDER BY phrase can include a type modifier to force the sort to be either CASESPECIFIC or NOT CASESPECIFIC. See “Specifying Case for Character Data” in Chapter 5, “Data Definition.”

ASC

ascending sort order. The default order is ASC. If a sort option is not given, result values are sorted in ascending order according to the client system’s collating sequence. If ORDER BY is not specified, rows are returned unsorted.

DESC

descending sort order.

Use of expr in ORDER BY is a Teradata RDBMS extension to ANSI SQL.

7-34

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement ORDER BY Clause

Column Name

Column Position

Use Parentheses for Required Sort Order

7

7

Each col_name in an ORDER BY clause is the name of a column in a table or view referenced in the SELECT expression list. The columns named do not have to match the columns in the SELECT expression list. The column position (col_pos) is a positive integer that refers to the numeric position of a column in the SELECT expression list. The column position cannot be passed as a parameter; attempts to do so cause it to be interpreted as an expression, and the data to be sorted by a constant. If a SELECT statement includes multiple parameters such as: ●

7



FORMAT or NAMED, or if it involves type conversion,

and if an ORDER BY clause is part of the query, use parentheses to group the parameters so that they are processed in the proper sequence. Without parentheses, the parameters will be processed in left to right order.

Sorting and Default Sort Order

7

The sort key created by the ORDER BY clause is constructed by concatenating the expr values. The maximum size of the sort key is limited to 4096 bytes. If the sort key is greater than 4096 bytes, the key is truncated and the data may or may not come back in the desired order. The Teradata RDBMS sorts nulls to follow all other data in a column. IBM’s DB2 and SQL/DS sort nulls to appear before other data in a column. This difference may cause an error in an application that expects nulls to be handled identically to DB2 or SQL/DS. By default, the result values of a character expr are sorted in ascending order, using the collating sequence in effect for the session.

Specifying Collation

7

Collation can be specified as an attribute of the user via the CREATE USER or MODIFY USER statement, and can be requested at the session level via the SET SESSION COLLATION statement. Otherwise, collation for a session is determined by the logon client system. The direction of the sort can be controlled by including the DESC (descending) or ASC (ascending) sort option in the SQL request. The Teradata RDBMS supports ASCII, EBCDIC, and MULTINATIONAL collating sequences. If MULTINATIONAL is in effect, your collation will be one of the European (diacritical) or Kanji sort sequences described later in this section under the passage titled “International Sort Orders”.

Teradata RDBMS for UNIX SQL Reference

7-35

Queries: The SELECT Statement ORDER BY Clause

The following subsections explain the results of ORDER BY as affected by whether character string expressions have the CASESPECIFIC or NOTCASESPECIFIC attribute.

Case Sensitivity NOT CASESPECIFIC Sorts With Japanese Character Data

7

7

For details on Case Sensitivity, see Chapter 5, “Data Definition” in the section “Specifying Case for Character Data”. On a Japanese character site, if the character strings to be sorted have the NOT CASESPECIFIC attribute, only lowercase simple Latin letters (a...z) are converted to uppercase before a comparison or sorting operation is performed. Any non-Latin single byte character, any multibyte character, and any byte indicating a transition between single byte characters and multibyte characters is excluded from this function. If the character strings to be sorted have the CASESPECIFIC attribute, conversion to uppercase is not performed. (CASESPECIFIC is the default in ANSI mode). In this condition, simple Latin letters are considered matched only if they are the same letters and the same case.

International Sort Orders

7

MULTINATIONAL collation is enabled at the user level via the COLLATION option of the CREATE USER or MODIFY USER statement. If a collation is not specified, the default is HOST; that is, the standard sort ordering of the client system (EBCDIC for IBM clients, ASCII for all others). You can override the default at the session level by issuing the SET SESSION COLLATION statement. When MULTINATIONAL collation is in effect, the default collation sequence is determined by the collation setting installed at system start-up. See also Appendix G, “International and Japanese Character Support”. Each international sort sequence is defined by program constants and no check is made to ensure that collation is compatible with the character set of the current session. The choice of sequence is controlled by your database administrator. The programmed character sequences cannot be changed.

7-36

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement ORDER BY Clause

European Sort Order

7

The sort order uses a two-level comparison that involves the following rules: For Teradata mode, all lowercase letters are first mapped to their uppercase equivalents unless CASESPECIFIC is specified in the query or was defined for the column being accessed.



In ANSI mode, the default is casespecific and the user must use NOT CASESPECIFIC to change this. All diacritical forms of the same letter are given the value of the base letter; that is, Ä is given the value of A, Ö is given the value of O, and so forth. If two strings produce the same value, the characters are further ordered according to their sort position in their diacritical equivalence class (defined in Table 7-1). Unless the query specifies the DESC sort option, collation is in ascending order.







When these rules are applied, the words “abbey,” “Active,” and “adage” are returned in this order, abbey

Active

adage

and the names Muller, Handl, Böckh, Mueller, Händl, Bohr, Bock, and Müller are ordered as: Bock Händl

Böckh Mueller

Bohr Muller

Handl Müller

Equivalence classes and the ordering of diacritical characters in each class are shown in Table 7-1. The classes listed here are for characters that have diacritical forms. Table 7-1 European Sort Order

A

a à á â ã ä

A À Á Â Ã Ä S

s S {{ss}}

C

c ç

C Ç

E

e è é ê ë

U

u U ù Ù ú Ú û Û (U tilde) ü Ü

E È É Ê Ë

Y

y ÿ

Y

Teradata RDBMS for UNIX SQL Reference

I

i ì í î ï

N

I Ì Í Î Ï

n ñ

AE

æ Æ

N Ñ

O

o ò ó ô õ ö o

O slash

ø

Ø

O Ò Ó Ô Õ Ö O A ring

å

Å

7-37

Queries: The SELECT Statement ORDER BY Clause

Japanese Character Sort Order Considerations

7

To ensure that sorting of character data is identical with that of the client, users at a Japanese character site should set their collation as follows: ●



KanjiEUC or KanjiShift-JIS character set users should use the ASCII collation. Users under the KATAKANAEBCDIC, KANJIEBCDIC5026_0I, or KANJIEBCDIC5035_0I character set, should install either KATAKANAEBCDIC, KANJIEBCDIC5026_0I, or KANJIEBCDIC5035_0I, respectively, at start-up time, and use MULTINATIONAL collation.. These sort orders use a one-level binary ordering.

Character data collation is handled as follows: ●



Unless the CASESPECIFIC qualifier is specified in the query or was defined for the column being accessed, any simple Latin Letters are converted to their uppercase equivalents. Characters identified as single byte characters under the current character set are converted according to the collation sequence in effect for the session. Note that under the KanjiEUC character set, the ss3 0x8F is converted to 0xFF. This means that a user-defined KanjiEUC codeset 3 are not ordered properly with respect to other KanjiEUC code sets. The order of other KanjiEUC codesets is proper (that is, ordering is the same as the binary ordering on the client system).





The following examples illustrate the use of the ORDER BY clause:

Examples Example 1

Characters identified as multibyte characters remain in the client encoding and are collated based on their binary values. Graphic data string collation is based on logical characters in the binary value on the client system.

7

The following example produces a list of employees, sorted by years of work experience. SELECT Name, JobTitle, YrsExp FROM Employee ORDER BY YrsExp;

7-38

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement ORDER BY Clause

Example 2

7

The following example substitutes a positive integer (3) for YrsExp in the ORDER BY clause. The 3 refers to the numeric position of YrsExp in the select expr-list. SELECT Name, JobTitle, YrsExp FROM Employee ORDER BY 3;

Example 3

7

The following example produces a list of employees, sorted by DeptNo and Salary (note that Salary is not contained in the select expr-list). SELECT Name, DeptNo FROM Employee ORDER BY DeptNo, Salary;

Example 4

7

The following example returns a list of each department number and its total population, in the order of lowest-population first: SELECT COUNT(Name), DeptNo FROM Employee GROUP BY DeptNo, ORDER BY COUNT(Name) ;

Example 5

7

Each of the following statements may be used to list employees by department number, with the highest-paid employee listed first and the lowest-paid last: SELECT Name, DeptNo, Salary FROM Employee ORDER BY 2 ASC, 3 DESC; SELECT Name, DeptNo, Salary FROM Employee ORDER BY DeptNo, Salary DESC;

Example 6: Case Sensitivity

Consider the following statements that create and populate table T: 7

CREATE TABLE T (A CHAR(4) NOT CASESPECIFIC, B BYTEINT) PRIMARY INDEX (A,B); INSERT INTO T VALUES (’AAAA’, 1); INSERT INTO T VALUES (’aaaa’, 2); INSERT INTO T VALUES (’BBBB’, 3); INSERT INTO T VALUES (’bbbb’, 4);

If the default handling of case is allowed, the following request, SELECT * FROM T ORDER BY A ;

produces: A ---AAAA aaaa BBBB bbbb

B ---1 2 3 4

Teradata RDBMS for UNIX SQL Reference

7-39

Queries: The SELECT Statement ORDER BY Clause

On the other hand, when CASESPECIFIC (CS) is used: SELECT * FROM T ORDER BY CAST(A AS CASESPECIFIC);

or SELECT CAST(A AS CASESPECIFIC), B FROM T ORDER BY 1;

the results will be one of the following, depending on the collation in effect for the session: EBCDIC A B ---- ---aaaa 2 bbbb 4 AAAA 1 BBBB 3

7-40

ASCII A B ---- ---AAAA 1 BBBB 3 aaaa 2 bbbb 4

Teradata RDBMS for UNIX SQL Reference

MULTINATIONAL A B ---- ---aaaa 2 AAAA 1 bbbb 4 BBBB 3

Queries: The SELECT Statement DISTINCT and ALL Options

DISTINCT and ALL Options

7

The DISTINCT option specifies that duplicate values not be returned when an expression is processed.The DISTINCT option of the SELECT statement eliminates duplicate rows from the result

Function

The ALL option is the default situation, and indicates that all rows are to be returned.

Syntax DISTINCT ALL FF06A018

where: Syntax Element . . .

Specifies that . . .

DISTINCT

only one row is to be returned from any set of duplicates that might result from a given expression list. Two rows are considered duplicates only if each value in one is equal to the corresponding value in the other.

ALL

all rows, including duplicates, are to be returned in the results of the expression list. This is the default value. It is optional in the query (Set Operator) expressions. Refer to the following section.

Usage Notes

When the DISTINCT option is used, the SELECT statement cannot contain a WITH clause.

Examples

The following examples illustrate the use of the DISTINCT and ALL options.

Example 1

The following example returns the number of unique job titles. 7

SELECT COUNT(DISTINCT JobTitle) FROM...

Teradata RDBMS for UNIX SQL Reference

7-41

Queries: The SELECT Statement DISTINCT and ALL Options

Example 2: DISTINCT With More Than One Aggregate Operator

7

In a single query, only one expression can be used as the argument for an aggregate operator that uses the DISTINCT option. However, more than one aggregate operator can use DISTINCT with other occurrences of the expression in the same query. SELECT SUM(DISTINCT x), AVG(DISTINCT x) FROM...

DISTINCT aggregate operations can be done only at the first level of aggregation.

Example 3

7

The following statement can be used to list unique department numbers in the Employee table: SELECT DISTINCT DeptNo FROM Employee ;

The result returned is: DeptNo -----100 300 500 600 700

Example 4: Using ALL

7

In contrast, the following statement returns a department number for every employee. SELECT ALL Deptno FROM Employee ;

ALL is the default option, except in query (Set Operator) expressions, where ALL is an option, but not the default.

7-42

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement Query Expressions (Set Operators)

Query Expressions (Set Operators) Introduction

7

7

A query expression is a set of queries combined by the set operators UNION, INTERSECT, and EXCEPT (also called MINUS). The query expressions allow you to manipulate the answers to two or more queries by combining the results of each query into a single result set. When query expressions are used, the data type, title, and format clauses contained in the first SELECT statement determine the data type, title, and format information which appear in the final result. This information is ignored in all other SELECT statements in the query. The query expressions (set operators) are: Query Expression (Set Operator)

Function

UNION

Combines the results of two or more SELECT statements.

INTERSECT

Returns result rows that appear in all answer sets generated by the individual SELECT statements. INTERSECT is flagged as not entry level ANSI, when the SQL flagger is enabled.

EXCEPT (MINUS)

Result is those rows returned by the first SELECT except for those also selected by the second SELECT. MINUS is flagged as non-ANSI when the SQL flagger is enabled. MINUS is the same as EXCEPT, and is an extension to ANSI.

Syntax

Query term: SELECT

statement

(query_expr ) HH01A060

Query factor: query_term query_factor

query_term

INTERSECT ALL

HH01A061

Teradata RDBMS for UNIX SQL Reference

7-43

Queries: The SELECT Statement Query Expressions (Set Operators)

Query expr: query_factor query_expr

query_factor

UNION MINUS

,

ALL (query_expr )

EXCEPT

ORDER BY

expr ASC DESC HH01A062

where: Syntax Element ...

Is . . . Query term

SELECT statement

Refer to “SELECT” in this chapter.

query_expr

an optional expression which may or may not include set operators, other expressions, and an ORDER BY clause. Query factor

INTERSECT

a set operator returning the result row appearing in all answer sets.

ALL

an optional keyword, allowing duplicate rows to be returned. Query expr

7-44

UNION MINUS EXCEPT

an optional set operators specifying how the two or more queries are to interfunction and determined by what result rows are required to be returned.

ALL

an optional keyword, allowing duplicate rows to be returned.

ORDER BY

the ORDER BY clause to order the result rows returned. Refer to “ORDER BY Clause” in this chapter.

expr

an expression used in the ORDER BY clause to determine the sort order of returned rows in the result.

ASC DESC

The sort order for the returned result rows. ASC is the default.

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement Query Expressions (Set Operators)

By default, duplicate rows are not returned. Use the ALL option to allow duplicate rows to be returned.

Usage Notes

The query expressions (set operators) are only used with the SELECT statement. The SELECT statements connected by set operators can include the normal options for SELECT, except the WITH clause. A further restriction, is that a query expression can include only one ORDER BY specification, at the end. A query expression including the UNION operator, for example, may contain as the left operand of the UNION, another query expression. Examples of query expressions involving several Set Operators are shown below.

Precedence

7

The precedence for processing set operators is INTERSECT first, then UNION, then MINUS from left to right For example, when a query of the following form is executed: SELECT stmnt_1 UNION SELECT stmnt_2 MINUS SELECT stmnt_3 INTERSECT SELECT stmnt_4

The Teradata RDBMS first performs the INTERSECT of SELECT stmnt_3 and SELECT stmnt_4, then the UNION of SELECT stmnt_1 and SELECT stmnt_2. From the UNION result, the INTERSECT result is deleted.

Using Parentheses

7

Precedence can be overridden by using parentheses. The operations in the parentheses are performed first. For example, when the following form is used: ((SELECT stmnt_1 UNION SELECT stmnt_2) MINUS (SELECT stmnt_3 UNION SELECT stmnt_4)) MINUS SELECT stmnt_5 INTERSECT SELECT stmnt_6

Teradata RDBMS for UNIX SQL Reference

7-45

Queries: The SELECT Statement Query Expressions (Set Operators)

Stage

Retaining Duplicate Rows Using ALL

7

Process

1

UNION of SELECT stmnt_1 and SELECT stmnt_2

2

UNION of SELECT stmnt_3 and SELECT stmnt-4

3

result of the second UNION is subtracted from the result of the first UNION

4

INTERSECT of SELECT stmnt_5 and SELECT stmnt_6 in performed

5

INTERSECT result is subtracted from the remainder of the UNION operations.

Unless the ALL option is used, duplicate rows are eliminated from the final result. The ALL option retains duplicate rows for the result set to which it is applied. the ALL option is flagged as non-ANSI when the SQL flagger is enabled.

Example 1

7

The following query returns duplicate rows for each result set, including the final ELECT stmnt_1: UNION ALL SELECT stmnt_2 MINUS ALL SELECT stmnt_3 INTERSECT ALL SELECT stmnt_4 SELECT LEVEL, PARAM, ’GMKSA’ (TITLE ’OWNER’) FROM GMKSA WHERE CYCLE = ’03’ UNION SELECT LEVEL, PARAM, ’GMKSA CONTROL’ FROM GMKSA_CONTROL WHERE CYCLE = ’03’ ORDER BY 1, 2 ;

The query returns the following response: ***QUERY COMPLETED. 5 ROWS FOUND. 3 COLUMNS RETURNED. LEVEL PARAM OWNER ----- ----- ----00 A GMKSA 00 T GMKSA 85 X GMKSA SF A GMKSA SF T GMKSA

7-46

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement Query Expressions (Set Operators)

This query does not use the ALL option; therefore, duplicate rows are dropped. Furthermore, the first SELECT specifies GMKSA, which is CHAR(5); that data type is then forced on the second SELECT. As a result, GMKSA_CONTROL entries are dropped because the first five characters are the same.

Example 2

In the next query, the SELECT order is reversed: 7

SELECT LEVEL, PARAM ’GMKSA CONTROL’ (TITLE ’OWNER’) FROM GMKSA_CONTROL WHERE CYCLE = ’03’ UNION SELECT LEVEL, PARAM, ’GMKSA’ FROM GMKSA WHERE CYCLE = ’03’ ORDER BY 1, 2 ;

This query returned the following answer set: ***QUERY COMPLETED.10 ROWS FOUND. 3 COLUMNS RETURNED. LEVEL PARAM OWNER --------------------00 A GMKSA 00 A GMKSA CONTROL 00 T GMKSA 00 T GMKSA CONTROL 85 X GMKSA 85 X GMKSA CONTROL SF A GMKSA SF A GMKSA CONTROL SF T GMKSA SF T GMKSA CONTROL

In this case, because the first SELECT specified ‘GMKSA CONTROL’, the rows were not duplicate, and were, therefore, included in the answer set.

Example 3

7

This example demonstrates how a poorly formed query can result in truncation of the results. SELECT LEVEL, PARAM, ’GMKSA FROM GMKSA WHERE CYCLE = ’03’

’ (TITLE ’OWNER’)

UNION SELECT LEVEL, PARAM,’GMKSA CONTROL’ FROM GMKSA_CONTROL WHERE CYCLE = ’03’ ORDER BY 1, 2 ;

Teradata RDBMS for UNIX SQL Reference

7-47

Queries: The SELECT Statement Query Expressions (Set Operators)

This query returns the following answer set: ***QUERY COMPLETED.10 ROWS FOUND. 3 COLUMNS RETURNED. LEVEL PARAM OWNER -------------------00 A GMKSA 00 A GMKSA CONTRO 00 T GMKSA 00 T GMKSA CONTRO 85 X GMKSA 85 X GMKSA CONTRO SF A GMKSA SF A GMKSA CONTRO SF T GMKSA SF T GMKSA CONTRO

This query returned the expected rows; note, however, that because of the way the name was specified in the first SELECT, there was some truncation.

Rules for Connecting Queries by Set Operators

7

Certain restrictions apply to SELECT statements connected by set operators that may not apply elsewhere. Therefore, in order for a group of queries to be connected by set operators, the queries must satisfy the following rules: ●

All SELECT statements in a multiple query must have the same number of expressions. If the first SELECT statement contains 3 expressions, all succeeding SELECT statements must contain 3 expressions. A null expression may be used in a SELECT statement as a place marker for an expression. In the following example: SELECT EmpNo, NULL (CHAR(5)) FROM Employee

the second expression is null. ● ●



WITH clauses cannot be used in the SELECT statements. GROUP BY clauses are allowed in individual SELECT statements and apply only to that SELECT statement and not to the result set. ORDER BY clauses are allowed only in the last SELECT statement and specify the order of the result set. ORDER BY clauses can contain only numeric constants. For example, to order by the first column in your result set, specify ORDER BY 1.



7-48

Each SELECT statement must identify the table that the data is to come from even if all SELECT statements reference the same table.

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement Query Expressions (Set Operators) ●

The data types of corresponding items in each SELECT statement must be compatible. For example, if the first field in the first SELECT statement is character data type, then the first field in each succeeding SELECT statement must be character data type. If a field in the first SELECT statement is numeric, then the corresponding field in succeeding SELECT statements must also be numeric. It is not necessary that the numeric data types be the same; that is, one field may be defined as INTEGER and the corresponding field may be defined as SMALLINT. All succeeding corresponding numeric fields will be converted to the numeric data type of the first SELECT statement.



A set operator cannot be used in subqueries. A set operator cannot be used in the definition of a view.

Teradata RDBMS for UNIX SQL Reference

7-49

Queries: The SELECT Statement UNION Operator

UNION Operator Introduction

7

7

The UNION operator combines two or more SELECT statements into a single query. Each query connected by UNION is executed to produce a result consisting of a set of rows. The union should include the same number of columns from each table in each SELECT statement, and the data types of these columns should be the same. All the result sets are then combined into a single result set.

Form

The form of the UNION operator is: 7

query expr

query factor

UNION ALL

FF06A020

Unless the ALL option is used, duplicate rows are eliminated from each result set and from the final result. If the ALL option is used, duplicate rows are retained for the applicable result set. The ALL option may be specified for each UNION operator in the query to retain in the final result every occurrence of duplicate rows.

Example 1: UNION

7

To select the name, project, and the number of hours spent by employees assigned to project OE1-0001, plus the names of employees not assigned to a project, the following query could be used: SELECT Name, Proj_Id, Hours FROM Employee,Charges WHERE Employee.Empno = Charges.Empno AND Proj_Id IN (’OE1-0001’) UNION SELECT Name, NULL (CHAR (8)), NULL (DECIMAL (4,2)) FROM Employee WHERE Empno NOT IN (SELECT Empno FROM Charges)

7-50

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement UNION Operator

This query returns the following rows: Name Aguilar J Brangle B Chin M Clements D Kemper R Marston A Phan A Regan R Russell S Smith T Watson L Inglis C Inglis C Leidner P Leidner P Moffit H Moffit H

Project Id Hours ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? OE1-0001 30.0 OE1-0001 30.5 OE1-0001 10.5 OE1-0001 23.0 OE1-0001 12.0 OE1-0001 33.5

In this example, null expressions are used in columns 2 and 3 of the second SELECT statement. The null expressions are used as place markers so that both SELECT statements in the query contain the same number of expressions.

Example 2: UNION

7

To determine the department number and names of all employees in departments 500 and 600, the UNION operator could be used as follows: SELECT DeptNo, Name FROM Employee WHERE DeptNo = 500 UNION SELECT DeptNo, Name FROM Employee WHERE DeptNo = 600 ;

This query returns the following rows: DeptNo

Name

500 500 500 500 500 500 500 600 600 600 600

Carter J Inglis C Marston A Omura H Reed C Smith T Watson L Aguilar J Kemper R Newman P Regan R

Although these results could have been achieved with a simpler query, such as the following: SELECT Name, DeptNo FROM Employee WHERE (DeptNo = 500) OR (DeptNo = 600);

there is an advantage to using the UNION operator instead. The advantage is that if the Deptno column is the primary index for the Employee table, using the query with the UNION operator

Teradata RDBMS for UNIX SQL Reference

7-51

Queries: The SELECT Statement UNION Operator

guarantees that the basic selects are prime key operations. There is no guarantee that a query using the OR operation will make use of the primary index.

Example 3: UNION

7

In addition, the UNION operator is useful if you must merge lists of values taken from two or more tables. For example, if departments 500 and 600 had their own Employee tables, the following query could be used to select data from two different tables and merge that data into a single list: SELECT Name, DeptNo FROM Employee_dept_500 UNION SELECT Name, DeptNo FROM Employee_dept_600 ;

Example 4: UNION

7

Suppose you want to know the number of man-hours charged by each employee who is working on a project. In addition, suppose you also wanted the result to include the names of employees who are not working on a project. To do this, you would have to perform a union operation as illustrated in the following example. SELECT Name, Proj_Id, Hours FROM Employee, Charges WHERE Employee.EmpNo = Charges.EmpNo UNION SELECT Name, Null (CHAR(8)), Null (DECIMAL(4,2)), FROM Employee WHERE EmpNo NOT IN SELECT EmpNo FROM Charges) UNION SELECT Null (VARCHAR(12)), Proj_Id, Hours FROM Charges WHERE EmpNo NOT IN SELECT EmpNo FROM Employee);

The first portion of the statement joins the Employee table with the Charges table on the EmpNo column. The second portion accounts for the employees who may be listed in the Employee table, but not the Charges table. The third portion of the statement accounts for the employees who may be listed in the Charges table and not in the Employee table. This ensures that all the information asked for is included in the response.

UNION Operator and the Outer Join

7-52

7

Example 4 above does not illustrate an outer join. That operation returns all rows in the joined tables for which there is a match on the join condition and rows from the “left” join table, or the “right” join

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement UNION Operator

table, or both tables for which there is no match. Moreover, nonmatching rows are extended with null values. It is possible, however, to achieve an outer join using inner joins and the UNION operator, though the union of any two inner joins is not the equivalent of an outer join. The following example shows how to achieve an outer join using two inner joins and the UNION operator. Notice how the second inner join uses null values. SELECT Offering.CourseNo, Offerings.Location, Enrollment.EmpNo FROM Offerings, Enrollment WHERE Offerings.CourseNo = Enrollment.CourseNo UNION SELECT Offerings.CourseNo, Offerings.Location, NULL FROM Offerings, Enrollment WHERE Offerings.CourseNo Enrollment.CourseNo;

The above UNION operation yields results equivalent to the results that the left outer join example, shown above, yields:

O.CourseNo

O.Location

E.EmpNo

C100

El Segundo

236

C100

El Segundo

668

C200

Dayton

Null

C400

El Segundo

Null

Teradata RDBMS for UNIX SQL Reference

7-53

Queries: The SELECT Statement INTERSECT Operator

INTERSECT Operator Introduction

7

7

The INTERSECT operator returns only the result rows that exist in the result of both queries. Unless the ALL option is used, duplicate rows are eliminated from the final result If the ALL option is specified, duplicate rows are retained. The ALL option may be specified for as many INTERSECT operators as are used in a multi-statement query. When the SQL flagger is enabled, the INTERSECT operator is flagged as not entry level ANSI and the ALL option is flagged as non-ANSI.

Form

The form of the INTERSECT operator is: 7

query factor

query term

INTERSECT ALL

FF06A021

Example: INTERSECT

Assume that two tables contain the following rows: 7

SPart table SuppNo

SLocation table PartNo

SuppNo

SuppLoc

100

P2

100

London

101

P1

101

London

102

P1

102

Toronto

103

P2

103

Tokyo

To then select supplier number (SuppNo) for suppliers located in London (SuppLoc) who supply part number P1 (PartNo), the following request could be used: SELECT SuppNo FROM SLocation WHERE SuppLoc = ’London’ INTERSECT SELECT SuppNo FROM SPart WHERE PartNo = ’P1’;

The result of this request is: SuppNo 101

7-54

Teradata RDBMS for UNIX SQL Reference

Queries: The SELECT Statement EXCEPT (MINUS) Operator

EXCEPT (MINUS) Operator Introduction

Form

7

7

The EXCEPT (MINUS) operator determines which result rows returned by the first SELECT query are not contained in the second SELECT query’s result rows. That is, the result rows returned by the second SELECT query are subtracted from the result rows returned by the first SELECT query. The form of the EXCEPT (MINUS) operator is:

7

query expr

EXCEPT MINUS

query factor ALL FF06A022

The ALL option causes duplicate rows to be retained. If ALL is not specified, duplicate rows are eliminated in the final result. The ALL option may be specified for more than one EXCEPT (MINUS) operator in a multi-statement query Note: When the SQL flagger is enabled, the EXCEPT(MINUS) operator and the ALL option are flagged as non-ANSI.

Example

7

Using the SLocation and SPart tables defined earlier, the following query could be used to determine the suppliers in London who do not stock part P1: SELECT SuppNo FROM SLocation WHERE UPPER (SuppLoc) = ’LONDON’ EXCEPT SELECT SuppNo FROM SPart WHERE UPPER (PartNo) = ’P1’ ;

The result of this query is: SuppNo 100

Teradata RDBMS for UNIX SQL Reference

7-55

Queries: The SELECT Statement EXCEPT (MINUS) Operator

7-56

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide

Chapter 8

Teradata SQL Syntax Guide

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide About This Chapter 8

About This Chapter Introduction

8

8

This chapter documents SQL statements valid for the Teradata RDBMS with the exception of those statements that are specific to embedded SQL. Those statements are documented in the manual Teradata Application Programming With Embedded SQL for C, COBOL, and PL/I.

Teradata SQL Syntax Abbreviations

With the exception of the abbreviations marked with (*), the following syntax abbreviations are Teradata extensions to ANSI. They are flagged as non-ANSI when the SQL flagger is enabled.

Abbreviation

Syntax

BT

BEGIN TRANSACTION

CD

CREATE DATABASE

CM

CREATE MACRO

CT

CREATE TABLE

CV

CREATE VIEW

DEL

DELETE

ET

END TRANSACTION

EXEC

EXECUTE

INS

INSERT

SEL

SELECT

SS

SET SESSION

UPD

UPDATE

Teradata RDBMS for UNIX SQL Reference

8-1

Teradata SQL Syntax Guide About This Chapter

Data Type Abbreviations

The following data type abbreviations appear in views and tables of the Data Dictionary.

Abbreviations Listed in Data Dictionary Tables and Views

Data Type

BF

BYTE

BV

VARBYTE

CF

CHARACTER

CV

VARCHAR

D

DECIMAL NUMERIC

DA

DATE

F

FLOAT REAL DOUBLE PRECISION

GF

GRAPHIC

GV

VARGRAPHIC LONG VARGRAPHIC

I

INTEGER

I1

BYTEINT

I2

SMALLINT

Note: The GRAPHIC, VARGRAPHIC, and LONG VARGRAPHIC abbreviations will change in a future release.

Access Rights Abbreviations

The following lists the abbreviations used for access rights in the Data Dictionary:

Abbreviations Listed in Data Dictionary Tables and Views

8-2

Access Right

CM

CREATE MACRO

CT

CREATE TABLE

CU

CREATE USER

CV

CREATE VIEW

D

DELETE

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide About This Chapter Abbreviations Listed in Data Dictionary Tables and Views

Access Right

DM

DROP MACRO

DU

DROP USER

DV

DROP VIEW

E

EXECUTE

I

INSERT

RF

REFERENCE

IX

CREATE/DROP INDEX

Teradata RDBMS for UNIX SQL Reference

8-3

Teradata SQL Syntax Guide ABORT

ABORT

8

ABORT is a synonym for ROLLBACK. Refer also to the “ROLLBACK” statement.

Function

ABORT is flagged non-ANSI when the SQL flagger is enabled.

Syntax

ABORT

quotestring

FROM option

WHERE clause FF07A068

where: Syntax Element ...

Is . . .

quotestring

the text of the message to be returned when the transaction is terminated. If quotestring is omitted, the message defaults to: user-generated transaction ABORT

FROM option

an optional clause that may be required if the WHERE clause includes subqueries. The contents of the FROM option are described in Chapter 7, “Queries: The SELECT Statement,” under the “FROM List”. ABORT is non-ANSI.

WHERE clause

an optional clause that introduces a conditional expression. The expression may specify an aggregate operation (see Usage Notes). If the WHERE clause is omitted, termination is unconditional. See also Chapter 7, “Queries: The SELECT Statement,” the “WHERE Clause”. The WHERE condition specifies an expression whose result must be true if termination is to occur. If the result is false, transaction processing continues.

ABORT With a WHERE Clause

8-4

8

ABORT tests each value separately; therefore, the WHERE clause should not introduce both an aggregate and a non-aggregate value. The aggregate value becomes, in effect, a GROUP BY value, and the mathematical computation is performed on the group aggregate results.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ABORT

For example, assuming that ● ● ●

The table Test contains several rows, The sum of Test.colA is 188, and Only one row contains the value 125 in Test.colB

then, the following statement incorrectly terminates the transaction: ABORT WHERE (SUM(Test.colA) 188) AND (Test.ColB = 125);

The preceding statement is processed first by performing an all-rows scan with the condition (ColB = 125), which selects a single row and then computes intermediate aggregate results with the condition (SUM(ColA) 188). The condition tests true because the value of ColA in the selected row is less than 188. If ABORT ... WHERE is used and it requires READ access to an object for execution, the user executing this DML statement must have SELECT right to the data being accessed. The WHERE condition of an ABORT may include subqueries. If so, then the subqueries require FROM clauses, and the ABORT should have a FROM if it is desired that the scope of a reference in a subquery is the ABORT condition. See also “Correlated Subqueries” in Chapter 9. Refer also to the “ROLLBACK” statement.

Multiple ABORT Statements Two Types of ABORTS

8

8

If a macro or multistatement request contains multiple ABORT statements, those ABORT statements are processed in order, even if the expressions could be evaluated immediately. There are two categories of ABORT statements, those that: can be evaluated by the parser and do not require access to a table and those that require table access. If ABORT expressions are used that do not reference tables, and if their order of execution is not important relative to the other statements, then they should be placed ahead of any statements that reference tables so that the abort can be done at minimum cost. In the following example, the first two ABORT statements can be evaluated by the parser, and do not require access to tables. The third ABORT requires access to a table. REP MACRO macroname (P1 INTEGER, P2 INTEGER) AS. . . ABORT ’error’ WHERE :P1 < 0; ABORT ’error’ WHERE :P2 < 0; SELECT. . . ABORT ’error’ WHERE tab.C1 = :P1;

Teradata RDBMS for UNIX SQL Reference

8-5

Teradata SQL Syntax Guide ABORT

The following examples illustrate the use of ABORT:

Examples Example 1

8

In the following example, the ABORT statement terminates macro execution if the row for the employee being deleted is not present in the Employee table. Statements in the body of the macro are entered as one multistatement request. Therefore, if the WHERE condition of the ABORT statement is met, the entire request is aborted and the accuracy of the count in the Department table is preserved. CM DelEmp (num SMALLINT FORMAT ’9(5)’, dname VARCHAR(12), dept SMALLINT FORMAT ’999’) AS ( ABORT ’Name does not exist’ WHERE :num NOT IN (SELECT EmpNo FROM Employee WHEREUPPER (Name) = UPPER (:dname)) ; DELETE FROM Employee WHERE UPPER (Name) = UPPER (:dname) ; UPDATE Department SET EmpCount = EmpCount - 1 WHERE DeptNo = :dept; ) ;

Example 2: Valid ABORT in All Releases Example 3: Valid in V2R2.0 and Above But Not in Prior Releases

Example 4

The following queries are syntactically correct in previous releases and the current release: 8

ABORT WHERE user=’DBC’;

The following queries are illegal in all previous releases but legal in the current and future releases: 8

8

ABORT from t1 WHERE t1.x1=1; ABORT FROM t1 WHERE t1.x1 > 1; ABORT FROM t1,t2 WHERE t1.x1=t2.x2;

The following example uses the ABORT statement in macro NewEmp. NewEmp adds a row to the Employee table for each new employee and then executes the SELECT statement to verify that the new information was entered correctly. The ABORT statement ensures that no new employee is inadvertently added to the executive office department (300). CREATE MACRO NewEmp ( number INTEGER, name VARCHAR(12), dept INTEGER 100 TO 900, position VARCHAR(12), sex CHAR, edlev BYTEINT ) AS ( ABORT ’Department number 300 not valid’ WHERE :dept = 300 ; INSERT INTO Employee (EmpNo, Name, DeptNo, JobTitle, Sex, EdLev) VALUES (:number, :name, :dept, :position, :sex, :edlev) ; SELECT * FROM Employee WHERE EmpNo = :number ; ) ;

8-6

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE

ALTER TABLE Function

8

The ALTER TABLE statement can be used to perform all the following functions: ● ● ● ● ● ● ● ●

Add new attributes for one or more columns of a table Add one or more new columns to an existing table Drop one or more columns from a table Modify column-level and table-level constraints Modify referential constraints Request or remove the FALLBACK option for a table Request or modify the JOURNAL option for a table Change the DATABLOCKSIZE or percent FREESPACE

The FALLBACK and JOURNAL defaults for a data table are originally determined by the statement that created or modified the object in which the table resides (see “CREATE TABLE”, “CREATE USER”, “CREATE DATABASE”, and “MODIFY USER” statements). Either or both of these defaults can be overridden by the statement that created the data table (see “CREATE TABLE” statement) or by the ALTER TABLE statement. ALTER TABLE statement is flagged as non entry level ANSI when the SQL flagger is enabled.

Teradata RDBMS for UNIX SQL Reference

8-7

Teradata SQL Syntax Guide ALTER TABLE

Syntax ,

tname

ALTER TABLE

A

,

dbname.

FALLBACK PROTECTION NO WITH JOURNAL TABLE = tname

dbname. JOURNAL NO

BEFORE

DUAL AFTER JOURNAL NO DUAL

Alter Table Options

LOCAL NOT LOCAL DEFAULT FREESPACE FREESPACE = integer PERCENT DATABLOCKSIZE = integer

IMMEDIATE BYTES KBYTES KILOBYTES

MINIMUM

DATABLOCKSIZE

MAXIMUM DEFAULT , A

col_name

ADD

data type declaration data type attributes data type attributes COMPRESS

Column Storage Attributes

NULL Table Column Definition

constant UNIQUE

Column Changes

CONSTRAINT name

PRIMARY KEY

Column Constraint Attributes

CHECK REFERENCE

boolean_condition

(

tname

, (

ADD

col_name

)

col_name

)

NULL

col_name

DROP

, ADD

FOREIGN KEY

col_name

(

)

REFERENCES tname

CONSTRAINT name

DROP

Reference Definition

, ( col_name

)

DROP INCONSISTENT REFERENCES ADD

CHECK

boolean_condition

(

)

col_name Constraint Changes

CONSTRAINT name DROP

CHECK

col_name

MODIFY

CONSTRAINT name col_name

CHECK

(

boolean_condition

)

CONSTRAINT name DROP

CONSTRAINT name

Unique Definition

, ADD

UNIQUE CONSTRAINT name

8-8

(

col_name

)

PRIMARY KEY

Teradata RDBMS for UNIX SQL Reference

FF07A043

Teradata SQL Syntax Guide ALTER TABLE

where: Syntax Element...

Description

dbname.tname

Name of the table to be altered. If dbname is not specified, the default database for the current session is assumed. Create Table Options

The Create Table options are a Teradata RDBMS extension to ANSI SQL The options can be listed in any order

NO FALLBACK PROTECTION

Specification to add or remove duplicate copy protection for the table.

WITH JOURNAL TABLE =

Specification to define or redefine the journal table for the data table being altered. tname need not reside in the same database as the data table.

dbname.tname

If fallback is being added, a duplicate copy of the table is created and stored. If fallback is being removed, the existing duplicate copy is deleted. The FALLBACK keyword used alone implies PROTECTION.

If a database is not specified, the default database for the current session is assumed. If a database is specified, it must exist and tname must have been defined as its default journal table.

NO BEFORE JOURNAL DUAL BEFORE JOURNAL

Alters the number of before-change images to be maintained for the table. If the JOURNAL keyword is specified without NO or DUAL, then a single copy of the image is maintained unless FALLBACK is in effect or is also specified. If journaling is requested for a table that uses fallback protection, DUAL images are maintained automatically.

NO AFTER JOURNAL

Alters the type of image to be maintained for the table; any existing images are not affected until the table is updated.

DUAL AFTER JOURNAL

NO, DUAL, LOCAL, or NOT LOCAL: the NO and DUAL options specify the number of after-change images to be maintained for the table.

LOCAL AFTER JOURNAL

NOT LOCAL and LOCAL specify whether single after-image journal rows for non-fallback data tables are written on the same virtual AMP (LOCAL) as the changed data rows, or on another virtual AMP in the cluster (NOT LOCAL).

NOT LOCAL AFTER JOURNAL

Teradata RDBMS for UNIX SQL Reference

8-9

Teradata SQL Syntax Guide ALTER TABLE Syntax Element...

Description

BEFORE JOURNAL

Specifies the type of image to be maintained for the table. The default for this option is established by a CREATE DATABASE, CREATE USER, MODIFY USER, or MODIFY USER statement for the database in which the table is to be created.

AFTER JOURNAL

Refer to the “CREATE TABLE” statement description for a listing of the journaling that results from the available journal and image type specifications. FREESPACE

Used to set the percent of free space that will remain on a cylinder during loading operations.

DEFAULT FREESPACE

Specifies the change of value of the percent freespace attribute to the specified value integer or to the unspecified value, as in the case of the CREATE TABLE statement. The old attribute value is discarded.

integer PERCENT

This does not have any immediate effect on existing cylinders, but the new attribute value will control the subsequent allocation of blocks on cylinders. If the specified value does not fall within the allowable range (0 to 75 percent), an error message is generated, as with the CREATE TABLE statement. See the “CREATE TABLE” statement for more details. Data Block Size Option

integer

Numeric constant specifying the data block size.

DATABLOCKSIZE = integer BYTES

Changes the value of the data block size attribute to the value integer. (The old attribute value is discarded.) The calculation is made in the same manner as in the case of the CREATE TABLE statement.

DATABLOCKSIZE = integer KBYTES DATABLOCKSIZE = integer KILOBYTES IMMEDIATE

If the IMMEDIATE option is included, the attribute value will still be changed, but, in addition, the rows in all existing data blocks are repacked into data blocks of the specified size. If the IMMEDIATE option is not included, existing data blocks are left unmodified. In both cases, the new attribute value will determine the maximum size of multiple-row data blocks subsequently modified or allocated. If the computed attribute value does not fall within the allowable range, an error message is generated, as with the CREATE TABLE statement. See the “CREATE TABLE” statement for more details.

8-10

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE Syntax Element...

Description

DEFAULT DATABLOCKSIZE

Specifying the DEFAULT DATABLOCKSIZE option when the data block size has been previously specified to be a size other than the default size resets the size to the default size. This is the same as the maximum size, i.e. 63 sectors (each sector is 512 bytes).

MINIMUM DATABLOCKSIZE

The minimum DATABLOCKSIZE is 12 sectors, where each sector is 512 bytes i.e. the minimum is 6,144 bytes. See the discussion of MINUMUM or MAXIMUM DATABLOCKSIZE for the “CREATE TABLE” statement.

MAXIMUM DATABLOCKSIZE

The maximum DATABLOCKSIZE is 63 sectors, where each sector is 512 bytes, i.e. the maximum is 32,256 bytes. This is the same as the default DATABLOCKSIZE. See the discussion of MINUMUM or MAXIMUM DATABLOCKSIZE for the “CREATE TABLE” statement. Column Change Options

ADD col_name type definition, type attributes, column storage attribute, or column constraint attribute

ADD . . . or DROP . . . cannot both be specified on the same column with the same “ALTER TABLE” statement. The ADD keyword either changes the definition of an existing column or adds a new column to the table. If the named column already exists, ADD indicates that its attributes are to be changed. See Chapter 5, Table 5-2 and Table 5-1 for listings of allowed type attributes and definitions. Column storage and constraints attributes are listed in Chapter 5. If the named column does not exist, ADD indicates that it is to be added to the table. New columns that are to be compressed should be defined in the order that achieves maximum accessibility (see Usage Notes).

col_name specifies the name of a column whose attributes are to be changed or that is to be added or dropped. ADD col_name NULL

Not supported by ANSI SQL. This is used to change an existing column col_name from being NOT NULL to being nullable.

DROP col_name

Indication that the named column is to be removed from the table.

Teradata RDBMS for UNIX SQL Reference

8-11

Teradata SQL Syntax Guide ALTER TABLE Syntax Element...

Description Constraint Change Options

ADD/DROP CONSTRAINT name FOREIGN KEY (col_name list) REFERENCES

Constraint names are not supported by Entry Level ANSI SQL and are flagged when the SQL flagger is enabled. ANSI SQL allows the adding and dropping of constraints only.

tname col_name DROP INCONSISTENT REFERENCES

ANSI SQL does not allow DROP INCONSISTENT REFERENCES, when used it is flagged if the SQL flagger is enabled This statement is commonly used after a restore, when the AFTER TABLE DROP FOREIGN KEY might not work. This statement will delete all inconsistent references defined on the table.

ADD CHECK (boolean condition)

Constraint names are not supported by Entry Level ANSI SQL. They are only allowed if col_name does not already possess a constraint.

ADD col_name CHECK (boolean condition)

When a constraint is added or modified, all existing rows of the table are accessed to validate that the current values conform to the specified search condition. If not, an error is returned and no change is made to the table definition.

ADD CONSTRAINT name CHECK (boolean condition) DROP CHECK

Drops all unnamed column-level CHECK constraints on col_name.

DROP col_name CHECK

DROP CHECK will drop the unnamed table-level constraints on the table.

DROP CONSTRAINT name CHECK MODIFY col_name CHECK (boolean condition), or MODIFY CONSTRAINT name CHECK (boolean condition)

8-12

DROP CONSTRAINT name CHECK or DROP CONSTRAINT name, will drop a named table-level CHECK constraint on the table.

MODIFY CONSTRAINT name CHECK (boolean search condition) is allowed only if a constraint with name already exists in the table. This also includes the named constraints defined as part of the column definition since those constraints are handled as named table-level constraints i.e. MODIFY col_name is allowed only if col_name already possesses a constraint. Existing rows of the table are accessed to validate that the current values conform to the specified search condition. If not, an error is returned and no change is made to the table definition.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE Syntax Element...

Description

DROP CONSTRAINT name

Drops table-level named constraint on the stated table.

ADD unique definition

Adds a uniqueness constraint UNIQUE or PRIMARY KEY on one or more columns of the table. The internal implementation is to add a unique secondary index. Existing rows of the table are accessed to validate that the proposed uniqueness constraint is not violated. If the uniqueness is violated, an error is reported and no change is made. The columns listed for uniqueness must all be NOT NULL.

For an existing column, the data type and attribute in general may: ●

● ●

Examples:

8

specify FORMAT, TITLE, and/or DEFAULT or WITH DEFAULT phrases. not specify the COMPRESS phrase. specify changes in data type attributes or in data types in cases where the change will not require rewriting existing rows.

Changing CASESPECIFIC or NOT CASESPECIFIC is valid, but changing to UPPERCASE is not. Changing from VARCHAR(6) to VARCHAR(12) is valid, but changing from VARCHAR(12) to VARCHAR(6) is not

Privileges Locks

8

8

To alter a table, the user must have DROP TABLE privilege on the table. When an ALTER TABLE statement is executed, an exclusive lock is placed on the table being altered. Because of the overhead required to alter a table for dropped or added columns, you should establish a final form for a table before it contains many rows. When a very large table is altered, it may be unavailable to other users for a long time.

ALTER TABLE and FALLBACK Dropping Columns

8

8

When an ALTER TABLE is executed to add FALLBACK, a full file scan is generated. To add the FALLBACK option to a table, all AMPs must be on-line and operational. When a column is dropped, the Teradata RDBMS must delete the field corresponding to the dropped column in every row in the table.

Teradata RDBMS for UNIX SQL Reference

8-13

Teradata SQL Syntax Guide ALTER TABLE

Indexed columns cannot be dropped. If an attempt is made to drop a column on which a primary or secondary index is defined, an error message is returned. To drop a column on which this type of index is defined . . .

Primary

Follow this procedure . . .

Step

Action

1

Create a new table with the correct primary index.

2

Copy the data into the new table. A simple way to do this is to use an INSERT statement with a SELECT subquery.

Secondary

Adding Columns

8

3

Drop the original table.

4

Rename the new table.

Step

Action

1

Drop the index.

2

Drop the index columns

3

Define a new index on the correct columns.

When a column is added to a table that contains rows, the Teradata RDBMS generally needs to access each row of the table to add a field for the new column. The following principles apply when adding a column to a table that contains rows:

IF this phrase . . .

IS . . .

THEN all rows are initially . . .

DEFAULT

specified for the new column

contain the specified constant values in the field.

WITH DEFAULT

specified for the new column

contain the system default in the field.

DEFAULT WITH DEFAULT

not specified for the new column

are null for the field, and the NOT NULL phrase cannot be specified for the column.

A column that is specified for NOT NULL but has no DEFAULT or WITH DEFAULT phrase can only be added to an empty table.

8-14

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE

Adding the UNIQUE or PRIMARY KEY Definition

8

A PRIMARY KEY can be added via the ALTER TABLE statement if and only if there is no PRIMARY KEY specification currently in the table, regardless of the type of index (Unique Primary Index/Unique Secondary Index) this PRIMARY KEY is currently mapped to. Also, the PRIMARY KEY being added will always be mapped to a Unique Secondary Index. The PRIMARY KEY constraint can be dropped if and only if it is mapped to an Unique Secondary Index. Unnamed unique constraints must be dropped via the DROP INDEX statement. ALTER TABLE...ADD/DROP UNIQUE/PRIMARY KEY will require INDEX rights on the table, or, for compatibility with the past, DROP TABLE rights.

Adding or Dropping REFERENCES or FOREIGN KEY

DEFAULT FREESPACE

An ALTER TABLE can only add or drop one foreign key reference and it cannot contain any options other than to add or drop the foreign key. 8

8

The user issuing the ALTER TABLE statement containing a REFERENCES option must have the REFERENCE right on the referenced table or on all specified columns of the referenced table. At any give time, the FREESPACE value returned in response to a SHOW TABLE command is the value specified in the most recently entered ALTER TABLE (or CREATE TABLE) command. Refer to the descriptions of the CREATE TABLE and SHOW TABLE statements. ●



DATABLOCKSIZE

8

Specifying the DEFAULT FREESPACE option when the percent freespace has been previously set to a value other than the default value, resets the size to the system default value. If the FREESPACE option is not included in the ALTER TABLE statement, then the percent freespace attribute value is not changed.

Repacking the data blocks of large tables is a time-consuming process, so specifying the IMMEDIATE option for large tables will substantially increase the amount of time required for the ALTER TABLE statement to complete. Also, in the event that an ALTER TABLE statement with the IMMEDIATE option aborts or is aborted by the user, the repacking may be incomplete—that is, some data blocks are of their original size, while others are of the new, specified size.

Teradata RDBMS for UNIX SQL Reference

8-15

Teradata SQL Syntax Guide ALTER TABLE ●



Journaling: NO or DUAL

Journaling: BEFORE or AFTER

At any given time, the DATABLOCKSIZE value returned in response to a SHOW TABLE command is the value specified in the most recently entered ALTER TABLE (or CREATE TABLE) command. See the descriptions of the “CREATE TABLE” and “SHOW MACRO SHOW TABLE SHOW VIEW” statements.) If the DATABLOCKSIZE specification option is not included in the ALTER TABLE statement, then the data block size attribute value is not changed and no data blocks are repacked.

8

8

IF . . .

THEN . . .

the JOURNAL keyword is specified without NO or DUAL

a single copy of the image is maintained unless FALLBACK is in effect or is also specified.

journaling is requested for a database that uses fallback protection

DUAL images are maintained automatically.

IF . . .

THEN . . .

the JOURNAL keyword is specified without AFTER or BEFORE

both types of images are maintained.

one type is specified

only the default for that type is overridden.

This option may appear twice in the same statement: once to specify a BEFORE or AFTER image, and again to specify the alternate type.

For example, if AFTER is specified, before-image journaling remains at the default setting. both types are specified

Journaling: LOCAL

8-16

8

the two specifications must not conflict.

Refer to “CREATE DATABASE” under “Local Journaling” on page 8-56 and “CREATE TABLE”. For further detail refer to the Teradata RDBMS for UNIX Database Design and Administration, Chapter 12, “Permanent Journaling”.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE

Maximum Number of Columns

8

A table may contain a maximum of 256 columns, and a total of 512 columns can be defined for a table over its lifetime. Dropping columns does not affect this rule. Therefore, if a table has 256 columns, the ALTER TABLE statement may be used to drop and replace up to 256 columns. If new columns need to be added that would exceed this number, a new table must be created. The SHOW TABLE statement and the INSERT statement with a SELECT subquery can be used to create and load the spinoff table.

Column IDs and COMPRESS

Changing Column Attributes: NOT NULL/NULL

8

When new columns are being added, the first column defined is assigned the lowest column id. Column ids may be important if you specify any column for COMPRESS. As a general rule, the compressed column that will have the highest rate of access and the lowest rate of compressible values should be defined before other compressed columns. Changing column attributes in ANSI SQL is restricted to setting or dropping a default clause.

8

To change the attributes of an existing column, the ALTER TABLE statement must include the ADD keyword. The following table summarizes the allowable cases for converting or adding NULL to NOT NULL, or NOT NULL to NULL columns. WHEN you add this qualifier to a column . . .

Composite changes are . . .

NULL

never permitted.

NOT NULL

permitted.

For columns not contained in indexes: Column type . . .

When defined as . . .

Can be altered to this column type . . .

Indexed

NULL

NULL only

NOT NULL

NOT NULL only

NULL

NOT NULL

Nonindexed

If and only if the column does not contain nulls. NOT NULL

Teradata RDBMS for UNIX SQL Reference

NULL

8-17

Teradata SQL Syntax Guide ALTER TABLE

Adding and Dropping CHECK Constraint

The following section describes the rules for adding and dropping CHECK constraints: 8

The following list names the rules for using constraints with the ALTER TABLE statement. ●









The following table explains rules for ADD and MODIFY. col_name . . .

THIS form . . .

IS allowed only if

ADD col_name CHECK (search condition)

does not already have a constraint.

MODIFY

does have a constraint.

The following form drops all unnamed column level CHECK constraints on col_name. ALTER TABLE tname DROP col_name CHECK The following form drops all unnamed table level CHECK constraints on the table name. ALTER TABLE tname DROP CHECK Either of the following is legitimate syntax for dropping a named CHECK constraint: DROP CONSTRAINT name CHECK DROP CONSTRAINT name The following form is valid only if a constraint with name already exists in the table. MODIFY CONSTRAINT name CHECK (search condition) This also applies to the named constraints defined as part of the column definition because those constraints are handled as named table level constraints.



To ensure maximum system performance, there is a limit of 100 table-level constraints that can be defined for any table. A combination of table-level, column-level, and WITH CHECK on view constraints may create a constraint expression that is too large to be parsed for INSERT and UPDATE statements.



You can add a primary key (using ALTER TABLE) if and only if no primary key is already defined for the table. There are no exceptions to this rule. The added primary key always maps to a unique secondary index (USI).



8-18

You can drop a PRIMARY KEY constraint if and only if it is mapped to a unique secondary index (USI).

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE ●



Use the DROP INDEX statement to drop unnamed unique constraints. Details of adding and dropping constraints with ALTER TABLE are explained in the following table. IF you perform this statement . . .

THEN you add/drop the following number of unnamed table level CHECK constraints . . .

ALTER TABLE ... DROP CHECK

all

anything else

one per ALTER TABLE statement, regardless of constraint type.

ALTER TABLE ... ADD UNIQUE/PRIMARY KEY requires the following rights: ● ●

Adding or Dropping Referential Integrity Constraints

DROP TABLE on the table or its database INDEX on the table

Referential integrity provides for specification of columns within a referencing table that are foreign keys for columns in some other referenced table. 8

The columns in the referenced table must be defined either as the primary key columns for the table or have the unique attribute defined for them. The foreign key columns in the referencing table must be identical in definition with the keys in the referenced table. See also “Referential Integrity” in Chapter 9, “Advanced SQL.” The COMPRESS option is not allowed on either column. Column level constraints are not compared for the columns.

Maintaining Foreign Key Integrity

8

The Teradata RDBMS verifies that the structure of columns defined as foreign keys or referenced by foreign keys, is not changed so as to violate the rules for definition of a foreign key constraint. An ALTER TABLE (or DROP INDEX) statement attempting to change the structure of such a column causes an error to be returned by the Teradata RDBMS. The Teradata RDBMS verifies that a table referenced by another is not dropped. Before dropping the table, an ALTER TABLE statement must be executed to drop the foreign key reference.

ADDING a Foreign Key

8

If an ALTER TABLE statement is executed to ADD a foreign key reference, then the RDBMS validates all the values in the foreign key columns against columns in the referenced table.

Teradata RDBMS for UNIX SQL Reference

8-19

Teradata SQL Syntax Guide ALTER TABLE

When the ALTER TABLE statement is executed, an error table with the same columns and PRIMARY KEY as the ALTER TABLE target table is created. The name of the error table is the target table name suffixed with the reference index number. A reference index number is assigned to each foreign key constraint for a table. This number can be determined by using a new system view. The error table is created under the same user/databases the target table. If a table already exists with the same name as that generated for the error table, then an error is returned to the ALTER TABLE statement. The RDBMS does not return an error if rows in the referencing table contain values in the foreign key columns which cannot be found in any row of the referenced table. Instead, a row is inserted into the error table for each such row found in the target table. Correction of data values in the referenced or referencing tables so that full referential integrity exists between the two tables is the responsibility of the user. The rows in the error table can be used to define which corrections must be made. Maintenance of the error table is also the responsibility of the user.

Referential Integrity When a Table is Inconsistent

8

While a table is marked as inconsistent, no updates inserts, or deletes are permitted. The table is fully usable only when the inconsistencies are resolved. It is possible that the user either intends to or must revert to a definition of a table which results in an inconsistent reference on that table. The Archive and Restore operations most commonly cause the inconsistencies. While the Child table is archived, the Parent table can be dropped. When the Child table is restored, the Parent table no longer exists. The normal ALTER TABLE DROP FOREIGN KEY statement does not work, because the Parent table references cannot be resolved. A special option of the ALTER TABLE statement which can be used to remove these inconsistent references from a table. The form is: ALTER TABLE

dbname.tname

DROP INCONSISTENT REFERENCES

The user executing the statement must have DROP rights on the target table of the statement. This statement removes all inconsistent internal indexes used to establish references. The base data of the table is not modified.

8-20

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE

Compress

8

The COMPRESS attribute cannot be changed, and the Data Type cannot be changed if that would require a change to existing data (see ). In both cases, the user must first create a new table defining the proper attributes, then insert the values from the existing table into the new table. If the data type of any column in the new table is not compatible with the value of the corresponding field in the existing table, individual INSERT statements must be used to load each row. If all the new data types are compatible with all the existing values (for example, only the COMPRESS attribute is being changed), the INSERT statement may be used with a SELECT subquery to insert all the rows in a single statement (see “INSERT” statement).

Data Type

8

Changing A Data Type Attribute

The Data Type defined for a column may be changed only if the new type does not require a change to the existing data. The rules for changing the Data Type of a column are listed in . See also the following section, “Changing A Data Type Attribute”. The display attributes FORMAT and TITLE may be altered at any time and a new DEFAULT declared.

8

COMPRESS may not be declared. CASESPECIFIC or NOT CASESPECIFIC can be declared for character columns.

Rules for Changing a Data Type

The following table illustrates the rules for changing a data type from the existing type to a new type. 8

Old Data Type

New Data Type

Restrictions on Changes

All

All

Cannot change for a column used in a primary or secondary index.

CHAR(n),UC or CHAR(n)

CHAR(n) or CHAR(n),CS

Lengths must remain identical, can change only CS (case specific) setting.

CHAR

CHAR(n)

Not allowed if n > 1.

VARCHAR(m),UC

VARCHAR(n)

Cannot decrease maximum length; cannot change from no UC to UC. *

GRAPHIC

GRAPHIC(n)

Not allowed if n > 1.

VARGRAPHIC(m)

VARGRAPHIC(n)

Cannot decrease maxlength. *

BYTE

BYTE(n)

Not allowed if n > 1.

VARBYTE(m)

VARBYTE(n)

Cannot decrease maximum length. *

Teradata RDBMS for UNIX SQL Reference

8-21

Teradata SQL Syntax Guide ALTER TABLE

Procedure to Change Column Data Types

Old Data Type

New Data Type

Restrictions on Changes

DATE

INTEGER

No restrictions.

INTEGER

DATE

Cannot change (integer column could include invalid date values).

DECIMAL(n,0)

INTEGER

Can change only if n = 5 to 9.

DECIMAL(n,0)

SMALLINT

Can change only if n = 3 or 4.

DECIMAL(n,0)

BYTEINT

Can change only if n = 1 or 2.

DECIMAL(n,f)

DECIMAL(m,f)

Can change only as follows: m >= n, no change in f; n = 1 or 2, m < 3; n = 3 or 4, m < 5; n = 5 to 9, m < 10; n = 10 to 15, m field_2) ;

Constraint name unique_1 must not be an existing name in table_1. 8

ALTER TABLE table_1 ADD CONSTRAINT unique_1 UNIQUE (field_1, field_2) ;

Constraint name reference_1 must not be an existing name in table_1. 8

ALTER TABLE table_1 ADD CONSTRAINT reference_1 FOREIGN KEY (field_1) REFERENCES table_2 ;

Constraint name check_1 must exist in table_1. 8

ALTER TABLE table_1 DROP CONSTRAINT check_1 ;

8

Constraint name check_1 must exist in table_1 as a CHECK constraint. ALTER TABLE table_1 DROP CONSTRAINT check_1 CHECK ;

Example 17

Example 18

8-26

Constraint name unique_1 must exist in table_1. 8

ALTER TABLE table_1 DROP CONSTRAINT unique_1 ;

Constraint name reference_1 must exist in table_1. 8

ALTER TABLE table_1 DROP CONSTRAINT reference_1 ;

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ALTER TABLE

Example 19

8

Replace the current definition for CHECK constraint check_1 with the new definition. Constraint name check_1 must be an existing CHECK constraint. ALTER TABLE table_1 MODIFY CONSTRAINT check_1 CHECK (field_2 > 0)

Adding/Dropping Unnamed Constraints Example 20

The following set of examples deals with adding or dropping unnamed constraints. 8

8

The following statement adds the unnamed check constraint to ensure that column field2 is always greater than 100. ALTER TABLE table_1 ADD CHECK (field2 > 100) ;

Example 21

8

This example is valid only if there is not an existing unnamed column level CHECK for field_1 in table_1. ALTER TABLE table_1 ADD field_1 CHECK (field_1 > 0) ;

Example 22

8

The following statement adds an unnamed uniqueness constraint to columns field_3 and field_4. ALTER TABLE table_1 ADD UNIQUE (field_3, field_4) ;

Example 23

8

The following statement a foreign key designation for column field_2 in table_1. ALTER TABLE table_1 ADD FOREIGN KEY (field_2) REFERENCES table_3 ;

Example 24

Example 25

This example drops all unnamed table level CHECK constraints. 8

ALTER TABLE table_1 DROP CHECK ;

8

This example drops the unnamed column level CHECK constraint from column field_1 in table_1. ALTER TABLE table_1 DROP field_1 CHECK ;

Example 26

8

This example drops the foreign key designation for column field_2 in table table_3. ALTER TABLE table_1 DROP FOREIGN KEY (field_2) REFERENCES table_3 ;

Teradata RDBMS for UNIX SQL Reference

8-27

Teradata SQL Syntax Guide ALTER TABLE

Example 27

Example 28: Non-Valid Use of ALTER TABLE: Constraints

Field field_1 is an existing, already constrained field in table_1. 8

ALTER TABLE table_1 MODIFY field_1 CHECK (field_1 IS NOT NULL) ;

The following statement is not valid because you can add or drop only one constraint per ALTER TABLE statement. 8

ALTER TABLE table_1 DROP CONSTRAINT check_1, ADD CONSTRAINT check_2 CHECK (field_2 > 0) ;

The system returns the message: Only a check specification is allowed for the modification.

Example 29: Non-Valid Use of ALTER TABLE: Named Constraints

By definition, the constraint named dup_constr_name already exists in table_1. For this reason, you cannot add it to the table. 8

ALTER TABLE table_1 ADD CONSTRAINT dup_constr_name FOREIGN KEY (field_3) REFERENCES table_2 ;

The system returns the message: Constraint with the same name ‘dup_constru_name’ already exists in table.

By definition, the constraint named no_such_constr does not exist in table_1. For this reason, you cannot modify or drop it.

Example 30: Non-Valid Use of ALTER TABLE: Named Constraints Example 31: Non-Valid Use of ALTER TABLE: Named Constraints

The following statement is not valid.

8

ALTER TABLE table_1 MODIFY CONSTRAINT no_such_constr CHECK (field_1 > 0) ;

The following statement is not valid. ALTER TABLE table_1 DROP CONSTRAINT no_such_constr ; 8

The system returns the message: The specified constraint name does not exist in table.

8-28

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide BEGIN/END LOGGING

BEGIN/END LOGGING Function

8

The BEGIN LOGGING and END LOGGING statements start and stop the auditing of SQL requests that attempt to access data. Each time a user named in a BEGIN LOGGING statement attempts to execute a specified action against a specified object, an entry is logged in the system table DBC.AccLogTbl. Possible entry data includes the type of access, the text of the request, the frequency of access, the action requested, the name of the requesting user, and the referenced objects. Logging can be ended on any action, user, or object for which logging is currently active. A logging entry does not indicate that a statement was executed; rather, it indicates that the system checked the privileges necessary to execute the statement. These statements may be executed only if the DIPACC DBCSQL script, which is provided on the software release tape, has been run to create the special security macro DBC.AccLogRule, and the system has been reset to initialize the logging software. Otherwise, access logging will not be allowed on the Teradata RDBMS.







BEGIN/END LOGGING is non-ANSI. If the SQL flagger is enabled, it is flagged.

Syntax

BEGIN

ON

LOGGING DENIALS

END

A

WITH TEXT

FIRST LAST FIRST AND LAST EACH

A

ALL ,

operation

, BY

dbname

GRANT

ON

DATABASE dbname USER dbname

name

TABLE VIEW MACRO

Teradata RDBMS for UNIX SQL Reference

dbname. FF07A044

8-29

Teradata SQL Syntax Guide BEGIN/END LOGGING

where Syntax Element ...

Description

DENIALS

Specifies that entries should be made only when statement execution fails because the user did not have the privilege(s) necessary to execute the statement. DENIALS is applied to only those actions listed in the BEGIN LOGGING statement that contains it. For example, two BEGIN LOGGING statements may specify the same object, user, and action, but different frequency and DENIALS options. This allows the user to log all denials, but only the first successful use of a privilege. If this option is not specified, a log entry is potentially made if the privilege check either fails or succeeds.

WITH TEXT

Specifies that the text of the statement which caused the log entry is to be saved in the log. If two BEGIN LOGGING statements specify the same user, object, and action, and one of the statements specifies the DENIALS option, then WITH TEXT may be specified on either one of the statements, on both statements, or on neither statement.

8-30

IF WITH TEXT is specified . . .

THEN the text is . . .

only on the statement that also specifies DENIALS

saved only when the entry is created as a result of a denial.

either without DENIALS or on both statements

always saved.

on END LOGGING

flag is turned off for the specified actions and for the DENIALS specification, but the logging frequency is not affected.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide BEGIN/END LOGGING Syntax Element ...

Description

FIRST LAST FIRST AND LAST EACH

Default for a BEGIN LOGGING statement is FIRST. Allowed combinations are FIRST and LAST. An entry will potentially be made for either the first, the last, the first and last, or each time during any session that the specified action is attempted against the specified object. If logging is already begun for an action on an object, a subsequent statement to begin logging for the same object, action, user, and DENIALS specification causes only the current frequency to be changed according to the new statement.

ALL Operation (or Privilege Definitions) GRANT

Specifies the frequency with which log entries should be made. ALL specifies that a log entry will potentially be made when any of the actions listed in the table following this one is attempted against the specified object. Note that the column name and id are displayed for each logged column when you log rights to UPDATE and REFERENCES. If logging has already begun for ALL actions on an object, a subsequent statement to begin or end logging for an action will change logging activity only for the specified action. One or more action-names from the list in , can be used to specify the type of access to be logged (note that these are the same as the functions for which an access right can be granted.)

BY dbname

Specifies the community for which log entries should be made. If username is specified, it must be a user currently defined in the Teradata RDBMS; that is, a name for which space has been created and under which a Teradata RDBMS session may be established. Absence of the “BY username” option implies all users (those already defined on the Teradata RDBMS as well as any defined in the future while this logging directive is in effect). If both “BY username” and “ON keyword object-name” are absent, then potentially the specified action is logged for all users and objects throughout the Teradata RDBMS system.

Teradata RDBMS for UNIX SQL Reference

8-31

Teradata SQL Syntax Guide BEGIN/END LOGGING Syntax Element ...

Description

ON

Specifies one or more entities for which log entries should be made. Absence of the “ON keyword object-name” option implies all entities the user attempts to access. If both “ON keyword object-name” and “BY username” are absent, then potentially the specified action is logged for all users and objects throughout the Teradata RDBMS. Note: The MONITOR privilege does not require the ON keyword, and is illegal if ON is specified in relation to MONITOR access rights. Therefore, to check who has MONITOR privileges, leave the ON clause out of the BEGIN/END LOGGING statement.

DATABASE

dbname USER dbname TABLE

dbname.name VIEW

The keyword DATABASE, USER, TABLE, VIEW, or MACRO must precede each object-name. An object-name is the name of a database, user, table, view, or macro. The name of a table, view, or macro must be qualified by the name of the database or user under which that object is defined.

dbname.name MACRO

dbname.name

CHECKPOINT

DROP MACRO

INSERT

CREATE DATABASE

DROP TABLE

MONITOR

CREATE MACRO

DROP USER

REFERENCES

CREATE TABLE

DROP VIEW

RESTORE

CREATE USER

DUMP

ROLLBACK DATABASE

CREATE VIEW

EXECUTE

ROLLFORWARD DATABASE

DELETE

GRANT

SELECT

DROP DATABASE

INDEX

UPDATE

Object Limits

Up to 20 objects can be in a BEGIN/END LOGGING statement. If more than 20 objects are required, use two or more BEGIN/END LOGGING statements.

8-32

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide BEGIN/END LOGGING

If more or less logging is desired, the full function of the BEGIN LOGGING statement may be used. Also, logging may be ended on any action, user, or object for which logging is currently active. Only those sites that require access logging should create the DBC.AccLogRule macro. The feature extracts a performance penalty even if little or no logging is performed. This feature must be installed by any site that used to use the SecurityLog and wishes to continue to do so. Once the DBC.AccLogRule macro is created and logging is initialized, the following command will cause the same statements to be logged as were previously logged in the SecurityLog. The user executing a BEGIN LOGGING or END LOGGING statement must have the EXECUTE privilege on the DBC.AccLogRule macro.

Changing Options: No Access Rights if MODIFY USER is used

You can enter a self-referent MODIFY USER statement. In such a case, no access rights are required in order to change the following options: 8

● ● ● ● ● ● ●

BEFORE JOURNAL AFTER JOURNAL DEFAULT JOURNAL TABLE PASSWORD STARTUP COLLATION DEFAULT DATABASE.

Logging is triggered by the use of access rights. Therefore, no logging occurs if you enter a self-referent MODIFY USER statement.

BEGIN LOGGING Generates Entries in DBC.AccLogTbl

8

The rules resulting from the execution of BEGIN/END LOGGING statements are entered in the system table DBC.AccLogRuleTbl. When logging is begun, the specified privilege checks performed by the Teradata RDBMS generates entries in system table DBC.AccLogTbl. The contents of these tables can be monitored via the system views DBC.AccLogRules and DBC.AccessLog. Aged log entries can be purged automatically. The viewname to be used in the DELETE statement is DBC.DeleteAccessLog. Privilege checks generate log entries. MODIFY is not a privilege that can be granted, so MODIFY is not a privilege that is checked. MODIFY operations are not logged.

Teradata RDBMS for UNIX SQL Reference

8-33

Teradata SQL Syntax Guide BEGIN/END LOGGING

To log MODIFY operations, select logging for the type of access that the MODIFY will or might require. Specifying such actions as DROP, INSERT, DELETE, and UPDATE can cause logging on actions used by a MODIFY. For example, to log MODIFY DATABASE operations, specify logging on the DROP DATABASE privilege. DROP DATABASE is the documented access right requirement for MODIFY DATABASE.

ALL, Operation, or GRANT Actions with Access Rights

Rules related to access rights are the following: ●

8



CREATE DATABASE/MACRO/TABLE/VIEW/USER and DROP DATABASE/USER are allowed only on databases or users. DROP TABLE includes ALTER TABLE. DROP MACRO or VIEW includes REPLACE MACRO or VIEW.

● ●



END LOGGING

8

Only DROP, EXECUTE, and GRANT are allowed on macros. DUMP, RESTORE, and CHECKPOINT are not allowed on views. DATABASE, TABLE, VIEW, MACRO, and USER confer both CREATE and DROP privileges.

The END LOGGING statement erases only the frequency and/or text flags for the specified actions and user or object. However, if erasing a frequency leaves all logging blank for a particular user, database, and table, the row is deleted from the AccLogRuleTbl table. Use of the END LOGGING statement results in an error if BEGIN LOGGING is not currently in effect for the community for which logging is to be ended.

Checked Access Rights Are Not Always for the Logon Username

8

The access rights checked at execution time are not always those for the logon username. In the execution of a macro, for example, the access right checked for the EXECUTE statement is for the logon username. However, the access rights checked for the individual statements within the macro are for the owner of the macro. The logon name is always present. The second name is also present, as the owner name, if the access right being checked is for other than the logon name.

Log Entry Generated If Logging Rule Present

8-34

8

A log entry is generated only if a logging rule is present for the object or for the user whose privilege is being checked. Log entries may contain two names.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide BEGIN/END LOGGING

Logging begun for specific usernames cannot be ended by omitting the “BY username” option.

Object Level Used in the Logging Statement

Scenario A

8

8

When a logging statement specifies logging at the database level, actions against all the tables in that database are candidates for log entries. Also, the object level used in granting an access right should be considered when specifying the object level of a logging statement. Assume that DatabaseA contains several tables, and that INSERT privilege has been granted to PUBLIC on Table1 only. Assume also that a current statement specifies “BEGIN LOGGING ON FIRST INSERT ON DatabaseA”. Subsequently, if users other than the owner submit insert statements against tables in DatabaseA, the log entries are: ● ●

A granted entry for the first insert into Table1 A denied entry for the first attempt at an insert into any other table in the database

When logging is specified at the database level, the same type of action against separate tables in that database might not be logged as separate actions.

Scenario B

8

Assume that DatabaseA contains Table1 and Table2, and that a current BEGIN LOGGING statement specifies logging on FIRST INSERT for DatabaseA. Next, assume that rows were inserted into both Table1 and Table2 in the same session. The logging result would be a single entry identifying the table that was the object of the first insert. If the access right is at the database level and the logging specification is at the table level, only actions against the table are considered for logging entries (the absence of an access right at the table level does not necessarily result in a log entry). A single log entry is generated for the table according to the results of privilege checks at both the table level and the database level

Teradata RDBMS for UNIX SQL Reference

8-35

Teradata SQL Syntax Guide BEGIN/END LOGGING

.

Scenario C

8

IF . . .

THEN . . .

either check succeeds

a granted entry is generated.

neither check succeeds

a denied entry is generated.

access logging is specified for a data object (for example, a table)

log entries are generated whether that object is accessed by name or through a view or macro.

A logging statement specifying FIRST SELECT ON DatabaseA.Table1 causes a log entry to be generated if an access statement is any of the following: ● ● ●

SELECT . . . FROM Table1 SELECT . . . FROM View1_of_Table1 EXECUTE MACRO1

(where Macro1 contains the statement SELECT . . . FROM Table1) A user can enter a MODIFY USER statement that references himself or herself as the user. In such a case, no access rights are required to change the following options: BEFORE JOURNAL, AFTER JOURNAL, DEFAULT JOURNAL TABLE, PASSWORD, STARTUP, COLLATION AND DEFAULT DATABASE. Logging is triggered by the use of access rights. Therefore, no logging will take place if the user enters a MODIFY USER command that only changes one or more of these options for the user himself or herself.

Rules for Using DDL Statements

8

The BEGIN LOGGING and END LOGGING statements are DDL statements because they affect Data Dictionary entries, which are used by the Teradata RDBMS when executing subsequent statements. As such, they must be submitted following the rules for DDL statement usage.: ●



A DDL statement cannot be combined with other statements as part of a multistatement request. A DDL statement can only be included in an explicit transaction (one or more requests bracketed by BEGIN/END TRANSACTION statements) if it is one of the following: ● the only statement in the transaction, or ● structured as a single-statement request that appears as the last request in the transaction.

When a BEGIN LOGGING or END LOGGING statement is submitted, the system checks that the executing user has EXECUTE privilege on the special system macro associated with that

8-36

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide BEGIN/END LOGGING

statement. No checks are made for privileges on the user or objects identified in the LOGGING statement. If the statement cannot be executed, an error message is returned to the user. Possible errors could be an invalid action-name, username, or object-name. Logging entries are stored in the system table DBC.AccLogTbl. The view DBC.AccessLog offers access to the contents of this table. If BEGIN LOGGING statements are not submitted, the system default is to not generate any entries on any user action. Refer to the Teradata RDBMS for UNIX Security Administration Guide, for more detail on using BEGIN and END LOGGING.

Examples

The following example illustrates the use of BEGIN/END LOGGING: BEGIN LOGGING WITH TEXT ON EACH USER, DATABASE, GRANT ;

Teradata RDBMS for UNIX SQL Reference

8-37

Teradata SQL Syntax Guide BEGIN/ END TRANSACTION

BEGIN/ END TRANSACTION

8

The BEGIN TRANSACTION and END TRANSACTION statements define a single logical transaction, and are valid in Teradata mode only.

Function

For ANSI mode transaction control statements, refer to “COMMIT” on page 8-51 and “ROLLBACK” on page 8-243.

Syntax

8

BEGIN TRANSACTION BT

; ; statement

END TRANSACTION

;

ET FF07A045

where:

Explicit Transactions

8

Syntax Element...

Description

BEGIN TRANSACTION

Initializes a transaction.

statement

Specifies a SQL statement that is part of the transaction. Statements can be included in the request or subrequest.

END TRANSACTION

Closes a transaction and may be at the end of the query or in a later query.

An explicit, or user-generated, transaction is a single set of BEGIN TRANSACTION/END TRANSACTION statements surrounding one or more requests. A LOGOFF command following a BEGIN TRANSACTION statement and one or more requests also forms an explicit transaction; however, this construct aborts the transaction, and the processing results of all the requests between BEGIN TRANSACTION and LOGOFF are rolled back. All other transactions are implicit.

Implicit Transactions

8-38

8

An implicit, or system-generated, transaction, is typically a macro, a data manipulation statement that affects one or more table rows, or a multistatement request that is not part of an explicit transaction and for which the system automatically supplies “BEGIN/END TRANSACTION” statements.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide BEGIN/ END TRANSACTION

Rules for Transactions Containing DDL Statements

A transaction may be any solitary SQL statement, including a DDL statement. However, when a request contains multiple statements, a DDL statement is allowed only if the following are true: 8





Teradata RDBMS Transaction Handling Protocol

8

The transaction is explicit (bracketed by BEGIN TRANSACTION and END TRANSACTION statements). The DDL statement is the last statement in the transaction (immediately followed by the END TRANSACTION statement).

The Teradata RDBMS handles transactions to maintain valid, consistent, and available data for all users. When a transaction is received, various locks are placed on the data according to the types of requests contained in the transaction. Then, either all the requests in the transaction are processed, or none of them are processed. If for some reason, such as a statement error, deadlock, access rights violation, table constraint violation, or premature logoff, a request does not complete successfully or causes processing to time out, the following occurs: Stage

1

Process

The entire transaction is aborted. Abort processing performs the following actions. Stage

2

Process

1

Performs an implicit END TRANSACTION.

2

Backs out any changes made to the database by the transaction.

3

Releases any usage locks associated with the transaction.

4

Discards any partially accumulated results (spool files) generated by the transaction.

A failure or time-out response is returned to the user.

When the Teradata RDBMS receives a BEGIN TRANSACTION statement, it looks for a statement keyword. The user should be aware of this when determining the organization of statement modifiers. For example, if the first statement in an explicit transaction is associated with a USING clause, the USING clause must precede the BEGIN TRANSACTION statement. The abbreviation for BEGIN TRANSACTION statement is BT. The abbreviation for END TRANSACTION statement is ET.

Teradata RDBMS for UNIX SQL Reference

8-39

Teradata SQL Syntax Guide BEGIN/ END TRANSACTION

Nested BT/ETs

8

BT/ET pairs can be nested and the Teradata RDBMS checks to ensure that every BT has a matching ET. The outermost BT/ET pair defines the explicit transaction; the inner BT/ET pairs, other than to validate that they match, have no effect on the transaction. Also, any embedded multi-statement requests and macro executions are considered part of the outermost BT/ET explicit transaction, and are not considered as implicit transactions in this context. For example, the following is considered one explicit transaction: BT; SELECT ...; UPDATE .... EXEC A(3,4); BT; UPDATE ...; INSERT ... ;INSERT ...; ET; INSERT ...; ET;

If an error occurs in the middle of a nested BT/ET, everything rolls back to the initial BT.

Scenarios Scenario 1

8

8

The following scenarios illustrate the use of BEGIN/END TRANSACTION. Assuming that the Department table contains an EmpCount column, the following explicit transaction could be used to remove a row from the Employee table and then decrement a departmental head count in the Department table: BEGIN TRANSACTION; DELETE FROM Employee WHERE Name = ’Reed C’; UPDATE Department SET EmpCount = EmpCount -1 WHERE DeptNo = 500; END TRANSACTION;

To prevent the UPDATE statement from being executed in the event that the DELETE statement fails, or completes without removing a row, the user must check the DELETE return code. If a “No rows removed” condition is indicated, the user must take action to bypass the update. Otherwise, the count result for department 500 is incorrect. Alternatively, the statements could be defined in a macro containing a conditional ABORT to protect the count.

8-40

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide BEGIN/ END TRANSACTION

Scenario 2

8

The following example illustrates an explicit transaction in which each statement, including the first, is associated with a USING clause: USING ssnumfile (INTEGER) BEGIN TRANSACTION ; INSERT INTO Employee (SocSecNo) VALUES (:ssnumfile) ; USING ssnumfile (INTEGER) INSERT INTO Employee (SocSecNo) VALUES (:ssnumfile) ; USING ssnumfile (INTEGER) INSERT INTO Employee (SocSecNo) VALUES (:ssnumfile) ; END TRANSACTION ;

Scenario 3

8

The following examples illustrate the use a DDL statement in an explicit transaction. These transactions create a temporary table, perform an aggregate operation on the result of another aggregate operation, and then drop the temporary table. Two transactions are used because a DDL statement must be either the only statement in a transaction, or the last statement in a transaction BT; CREATE TABLE DeptSumSal (DeptNo SMALLINT FORMAT ’999’ BETWEEN 100 AND 900 NOT NULL, SumSal DECIMAL(8,2) FORMAT ’ZZZ,ZZ9.99’) PRIMARY INDEX(DeptNo); ET; BT; INSERT INTO DeptSumSal SELECT DeptNo, SUM(Salary) FROM Employee GROUP BY DeptNo; SELECT AVG(SumSal) FROM DeptSumSal; DROP TABLE DeptSumSal; ET;

Scenario 4: Implicit Transaction (BTEQ)

The following example is structured as a BTEQ multistatement request, and therefore, is processed as a single implicit transaction. 8

Note the placement of the USING modifier and the semicolons. With this construct, the failure of any WHERE conditions causes the transaction to abort and all completed inserts and updates to be rolled back. USING var1(CHAR), var2(CHAR), var3(CHAR) INSERT INTO TestTabU (C1) VALUES (:var1) ; INSERT INTO TestTabU (C1) VALUES (:var2) ; INSERT INTO TestTabU (C1) VALUES (:var3) ; UPDATE TestTabU SET C2 = C1 + 1 WHERE C1 = :var1 ; UPDATE TestTabU SET C2 = C1 + 1 WHERE C1 = :var2

Teradata RDBMS for UNIX SQL Reference

8-41

Teradata SQL Syntax Guide CHECKPOINT

CHECKPOINT

8

The CHECKPOINT statement places a mark in a journal table which may be used to coordinate future recovery activities.

Function

CHECKPOINT is flagged as non-ANSI.when the SQL Flagger is turned on.

Syntax

CHECKPOINT

8

tname , NAMED chkpt_name

; FF07A007

where:

Usage Notes

8

Syntax Element...

Description

tname

Specifies the journal table that is to be marked with the checkpoint entry.

chkpt_name

Specifies a label that may be used to reference the checkpoint entry in database recovery activities. ● chkpt_name should be unique. However, if the chkpt_name duplicates an existing entry in the journal, the entry may be qualified by the system-assigned event number. ● If a chkpt_name is not specified, the checkpoint entry must be referenced in recovery activities by its event number (see Usage Notes).

If an explicit transaction or a multistatement request contains a CHECKPOINT statement, that CHECKPOINT statement must precede any INSERT, UPDATE, or DELETE statements in the transaction or request. To checkpoint a journal table, the user must meet one of the following criteria: ● ●

● ●

have CHECKPOINT privilege on the journal table have CHECKPOINT privilege on the database containing the journal table be an owner of the database containing the journal table be an immediate or indirect owner of the journal table.

The CHECKPOINT statement causes the Teradata RDBMS to place a read lock on all data tables that write journal images to the table

8-42

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CHECKPOINT

named in the “CHECKPOINT” statement. This lock causes any new transactions to wait until the checkpoint operation is complete. It also causes the checkpoint operation to await the completion of outstanding update transactions. This action guarantees that the checkpoint saved in the journal represents a clean point in the transaction environment. When the checkpoint operation completes, the locks are released. Each checkpoint entry is assigned an event number by the Teradata RDBMS. This number may be returned as a result of CHECKPOINT processing; it is also stored, along with other information about statement execution, in a Data Dictionary table. You can review the table data through the DBC.Events system view.

Example

8

The following statement can be used to place a checkpoint entry into a journal table: CHECKPOINT AccountDB.JnlTable, NAMED DailyPayMaint;

When this statement is executed, all update activity is ceased on tables that write journal images to the journal table, (JnlTable). A record containing the DailyPayMaint label is placed into the journal table. The label may then be used in rollforward or rollbackward operations.

Teradata RDBMS for UNIX SQL Reference

8-43

Teradata SQL Syntax Guide COLLECT STATISTICS

COLLECT STATISTICS

8

The COLLECT STATISTICS statement collects statistical data for one or more columns of a table that may be used by the Teradata Optimizer in optimizing data access.

Function

The purpose of COLLECT STATISTICS is to provide data that is used internally by the Teradata RDBMS to optimize data access. The statistical data that is collected is retained in an internal format for use during the optimizing phase, and is not available as user-readable data. COLLECT STATISTICS is an extension to ANSI. If used, it is flagged when the SQL flagger is enabled.

Syntax

8

tname

COLLECT STATISTICS ON

COLUMN

col_name1

;

, INDEX ( col_name2 )

INDEX

name FF07A008

where:

Why Collect Statistics?

8-44

8

Syntax Element...

Description

tname

Specifies the name of the table for which statistics are to be collected.

col_name1

Indicates that the collection is for a single column. Specifies the name of a column for which statistics are to be collected.

col_name2

Indicates that the collection is for one or more columns that comprise an index on the table. Specifies the names of columns for which statistics are to be collected.

name

Means collect statistics on a named index. In this case, no column names are needed.

COLLECT STATISTICS can be useful in situations where data is to be accessed using a column or index that may have an uneven distribution of values.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide COLLECT STATISTICS

Also, current statistics can enable the SQL optimizer to better choose the fastest method of data access and selection, particularly in cases of complex queries or queries that involve joins. NCR strongly recommends that statistics be collected on the primary index of small tables involved in joins.

Privileges Rules for Using COLLECT STATISTICS

8

You must have the DROP privilege on a table in order to collect statistics for the table. The following lists rules for using COLLECT STATISTICS: ●

8







COLLECT STATISTICS Can Take a Long Time

Collecting Statistics When No COLUMN or INDEX Is Specified

8

8

COLLECT STATISTICS is a DDL statement; as such, it must be entered as a single-statement request, as the only statement in a macro, or as the only or last statement in a transaction that is enclosed by BEGIN and END TRANSACTION statements. When a COLLECT STATISTICS statement is executed, an ACCESS lock is placed on the table. COLLECT STATISTICS can be run on an empty table. This allows defining the columns and/or indexes for subsequent collection. In previous Teradata SQL releases, COLLECT STATISTICS could only be run on a table with at least one row. When HELP STATISTICS is run, the ‘Unique Value’ column returns the value of 1, the default value. If you attempt a COLLECT STATISTICS on a table that has had a DROP STATISTICS run, and if it has not had a COLLECT STATISTICS COLUMN or COLLECT STATISTICS INDEX run on it, an error is reported.

Because COLLECT STATISTICS must do a full file scan, it can take a long time to complete; the duration is dependent on the size of the table, and the load on the system. Columns with no indexes or unique primary indexes take more time; columns with non-unique secondary indexes take less time. If COLUMN or INDEX is not specified, COLLECT STATISTICS updates statistics for columns or indexes that had been previously collected (provided that a DROP STATISTICS statement has not been entered for one or more of these columns or indexes in the meantime). If COLUMN or INDEX is not specified, the number of columns and indexes included in tname is restricted to 40 or less.

Single Column INDEXES

8

If an index has a single column col_name, the collection of statistics for COLUMN col_name is the same as that for INDEX(col_name).

Teradata RDBMS for UNIX SQL Reference

8-45

Teradata SQL Syntax Guide COLLECT STATISTICS

Keep Statistics Current

8

If a table has been extensively modified (more than 10% of the rows added or deleted) since statistics were last collected, the outdated statistics may cause poor access optimization and bad join plans. Therefore, statistics should be periodically refreshed or dropped. Keep statistics current! The efficacy of collected statistics vary with the types of access used on a table. If performance does not seem to be improved by the statistics collected, the DROP STATISTICS statement may be used to remove the statistics. Do a HELP STATISTICS to see which columns and indexes currently have statistics, and to see a portion of the collected information.

Using Named Indexes

8

You can specify either an index name or an index definition in a COLLECT (or DROP STATISTICS) statement, but not both. To collect (or drop statistics) from an unnamed index, you must specify the complete index definition.

Examples Example 1

The following examples illustrate the use of COLLECT STATISTICS: 8

8

Assuming that an index is defined on the EmpNo and Name columns of the Employee table, the following statement: COLLECT STATISTICS Employee INDEX(EmpNo,Name);

collects statistics for that index.

Example 2

Subsequent entry of the following statement: 8

COLLECT STATISTICS Employee;

refreshes the statistics for the EmpNo/Name index, along with any other statistics previously collected for the Employee table.

Example 3: Named Indexes

Example 4

8

8

COLLECT STATISTICS ON table_1 INDEX unique_1 ;

The following example illustrates collecting statistics on an unnamed index that includes the index definition. COLLECT STATISTICS ON table_1 INDEX (field_1, field_2) ;

8-46

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide COLLECT STATISTICS

Example 5: Non-Valid Use of Named Index

This statement is not valid because the index named does not exist for the table. 8

COLLECT STATISTICS ON table_1 INDEX no_such_index ;

The system returns an error message: The specified constraint name does not exist in table.

Example 6: Non-Valid Use of Named Index

8

This statement is not valid because you cannot have both an index name and an index definition in the same COLLECT STATISTICS statement. COLLECT STATISTICS ON table_1 INDEX unique_1 (field_1, field_2) ;

The system returns an error message: Only index name or index column(s) allowed in COLLECT/DROP STATISTICS.

Teradata RDBMS for UNIX SQL Reference

8-47

Teradata SQL Syntax Guide COMMENT

COMMENT

8

The COMMENT statement stores or selects a description or commentary on a user-defined object (view, table, column).

Function

COMMENT is an extension to ANSI. It is flagged when the SQL flagger is enabled.

Syntax

8

objname

COMMENT ON

'string'

DATABASE USER

AS

TABLE

IS

;

VIEW MACRO COLUMN

FF07A009

where: Syntax Element...

Description

DATABASE USER TABLE VIEW MACRO COLUMN

An optional, introductory keyword that describes the type of object for which the comment is stored.

objname

Specifies the name of the object. A column may be a table column, a view column, or a macro parameter. If an optional keyword does not precede the object name, the object is deduced from the level of qualification in the name. That is, the unqualified name x implies COMMENT ON DATABASE x; the name x.y implies COMMENT ON TABLE, VIEW, or MACRO y; and the name x.y.z implies COMMENT ON COLUMN (or macro parameter) z. The object type is not checked; use the fully-qualified name if there is any ambiguity.

8-48

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide COMMENT Syntax Element...

Description

AS IS

Optional keyword that introduce a clause defining a comment string.

’string’

Specifies the commentary on or description of the object name. Maximum string length is 255 characters. An existing string may be changed by specifying a new string. If no string is specified, any string previously stored is returned.

Privileges

Rules for Using COMMENT

8

To store a comment, the user must have the DROP privilege on the object. If the object is a column, the DROP privilege must be on the column’s table. No access privilege is needed to return a comment. COMMENT is treated as a DDL statement, and as such, it cannot be used in 2PC mode.

8

The COMMENT statement is typically used in conjunction with a CREATE statement (except for CREATE INDEX statement), to document a newly created object. Depending on whether a string is defined (with or without an AS or IS keyword), entering a COMMENT statement produces the following results: ●



Japanese Characters

8

If a string is specified, it replaces a previous string in the Data Dictionary (provided the user who specifies the string has the proper DROP privilege). If a string is not specified, the previous string (or, if no previous string is stored, a NULL value) is returned.

On Japanese character sites, comments can contain single byte characters and/or multibyte characters from KanjiEBCDIC, KanjiEUC, or KanjiShift-JIS character sets. For example, the following comment, created under the KanjiEBCDIC character set, contains Latin, Katakana (0x81, 0x83 and 0x91, 0x92), and multibyte character (0x’0E454E45440F’) characters. Note: In this example, the Katakana characters and multibyte characters are shown in hexadecimal in place of the actual characters. THIS COMMENT IS USED FOR TABLE TAB81830E454E45440F9192

Teradata RDBMS for UNIX SQL Reference

8-49

Teradata SQL Syntax Guide COMMENT

Examples Example 1

The following examples illustrate the use of COMMENT: 8

8

The following statement may be used to specify a description of the Name column in the Employee table: COMMENT ON COLUMN Employee.Name IS ’Employee name, last name followed by first initial’;

Example 2

Given the following statement: 8

COMMENT ON COLUMN Employee.Name;

the Teradata RDBMS returns: Employee name, last name followed by first initial

8-50

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide COMMIT

COMMIT

8

In ANSI mode, the first SQL statement in a session initiates a transaction. The transaction is terminated by sending either a COMMIT, ROLLBACK, or ABORT statement, or failure causes a rollback of the transaction.

Function

To terminate the current SQL-transaction successfully the COMMIT statement must be used. COMMIT is valid in ANSI mode only

Syntax

8

COMMIT ;

WORK

FF07A046

COMMIT Is Explicit

COMMIT Is Not Valid in Teradata Mode COMMIT and BTEQ

8

Syntax Element...

Description

WORK

If the SQL flagger is on, the absence of WORK causes the statement to be flagged.

There are no implicit transactions in ANSI mode. The COMMIT must always be explicitly stated, and be the last statement of a transaction in order for the transaction to terminate successfully. In Teradata mode, the COMMIT statement is not allowed. If used, it returns a failure and aborts the transaction.

8

8

If you use COMMIT in a BTEQ script with either the .SESSION or the .REPEAT command, you must send the COMMIT statement along with the repeated SQL statement as one request. If you send the repeated request without the COMMIT, one of the requests is eventually blocked by other sessions and the job hangs because of a deadlock.

Teradata RDBMS for UNIX SQL Reference

8-51

Teradata SQL Syntax Guide COMMIT

BTEQ Example

The following dummy example illustrates how this should be done: 8

.session trans ansi .sessions 10 .logon TDPID/USER,PASSWD .import data file = .repeat I using i(integer), j(integer) insert into

(col1, col2) values (:1, :j); COMMIT WORK; .quit

Examples Example 1

The following examples illustrate the use of COMMIT: 8

8

The INSERT statement in the following example opens the transaction. COMMIT closes the transaction. INSERT INTO employee (name, empno) VALUES (‘Sinclair P’, 101) WHERE dept = ‘400’; COMMIT;

Example 2

8

The following UPDATE initiates the transaction and COMMIT WORK terminates it: UPDATE parts SET part_num = 20555 WHERE location = ‘seoul’; COMMIT WORK;

8-52

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE DATABASE

CREATE DATABASE

8

The CREATE DATABASE statement creates a database in which other objects may be created.

Function

CD is the abbreviation for CREATE DATABASE. CREATE DATABASE is flagged as non-ANSI when the SQL flagger is enabled.

Syntax

8

name

CREATE DATABASE

A

AS FROM dbname ,

A

PERMANENT

= n ;

BYTES

PERM SPOOL = n BYTES ACCOUNT =

acctid

FALLBACK NO

PROTECTION JOURNAL NO

BEFORE

DUAL AFTER JOURNAL NO DUAL LOCAL NOT LOCAL

tname

DEFAULT JOURNAL TABLE =

dbname.

FF07A047

where: Syntax Element. . . .

Specifies . . .

name

the name of the new database.

dbname

the name of the immediate owning user/ database. The default is the username associated with the current session.

Teradata RDBMS for UNIX SQL Reference

8-53

Teradata SQL Syntax Guide CREATE DATABASE Syntax Element. . . .

Specifies . . .

PERMANENT = n BYTES

the total number of bytes (n) to be reserved for permanent storage of the new database. The space is taken from unallocated space in the database of the immediate owner. n may be entered as an integer, a decimal value, or as a floating point value. This option must be specified. There is no default.

SPOOL = n BYTES

the number of bytes (n) to be allocated for spool files. The default is the largest value that is not greater then the owner’s spool space and that is a multiple of the number of AMPs on the system. n must not exceed the owner’s spool space. n may be entered as an integer, a decimal value, or as a floating point value.

ACCOUNT = acctid

the account to be charged for the space used by this database. If not specified, it defaults to the account identifier of the immediate owner database. acctid can be up to 30 characters and must be enclosed by apostrophes.

NO FALLBACK PROTECTION

whether to create and store a duplicate copy of each table created in the new database. The default value is NO FALLBACK. This setting may be overridden for a particular data table when the table is created (refer to “CREATE TABLE” statement). The FALLBACK keyword used alone implies PROTECTION.

NO DUAL BEFORE JOURNAL

the number of before change images to be maintained by default for each data table created in the new database. The JOURNAL keyword without NO or DUAL implies single-copy journaling. If journaling is specified, a DUAL journal is maintained for data tables with FALLBACK protection. The JOURNAL keyword without BEFORE implies both types (BEFORE and AFTER) of images.

8-54

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE DATABASE Syntax Element. . . .

Specifies . . .

NO DUAL LOCAL NOT LOCAL AFTER JOURNAL

the type of image to be maintained by default for data tables created in the new database. The JOURNAL keyword without NO or DUAL implies single-copy journaling. If journaling is specified, a DUAL journal is maintained for data tables with FALLBACK protection. NOT LOCAL and LOCAL specify whether single after-image journal rows for non-fallback data tables are written on the same virtual AMP (LOCAL) as the changed data rows, or on another virtual AMP in the cluster (NOT LOCAL). See also Usage Notes below, “Local Journaling”. The JOURNAL keyword without AFTER implies both types (BEFORE and AFTER) of images. The default is no journaling If only AFTER JOURNAL is specified, then a before-change image is not maintained. If both types are specified, the two specifications must not conflict. This setting may be overridden for a particular data table when the table is created (refer to “CREATE TABLE” statement).

DEFAULT JOURNAL TABLE = dbname.tname

Identifies the default table that is to receive the journal images of data tables created in the new database. tname must be defined if journaling is requested, but it need not reside in the new database. tname is automatically created in the new database if dbname is not specified, or the new database is specified. If a different database is specified, then that database must exist and tname must have been defined as its default journal table.

An option can only be defined once. The PERMANENT or PERM option must be specified.

Privileges

8

Privileges given to the creator and owner are WITH GRANT OPTION. To create a database, a user must have the CREATE DATABASE privilege on the immediate owner database. The creator and owner receive all privileges on the newly created database. The newly created database also receives all relevant privileges on itself.

Teradata RDBMS for UNIX SQL Reference

8-55

Teradata SQL Syntax Guide CREATE DATABASE

Privileges given to the database are not WITH GRANT OPTION. No privilege is needed to specify a DEFAULT JOURNAL TABLE in a database other than the database being created. However, when a table is created that specifies a journal, then the user creating the table must have INSERT privilege on the journal table.

Rules for Using CREATE DATABASE

The following rules apply to the CREATE DATABASE statement. ● 8









Local Journaling

8

The combined total number of databases and users for any one system is limited to 32,767. A database may contain only one journal table. However, any table in a particular database may use a journal table in a different database. When a database contains a journal table, the journal table shares the database’s PERMANENT storage with any data tables created in that database. If necessary, the defined PERM or SPOOL space is changed to the next higher value that is a multiple of the number of AMPs on the system. When a CREATE DATABASE statement is executed, an EXCLUSIVE lock is placed on the database being created.

The LOCAL single AFTER image journal is supported analogously to single BEFORE image journal: The Access Rights required to create or drop LOCAL single AFTER image journal are the same as for the analogous operation on single BEFORE image journal. LOCAL single AFTER image journaling is restricted to non-fallback data tables. Archive/Recovery rules for LOCAL single AFTER image journal and single BEFORE image journal are the same, except that LOCAL single AFTER image journal is used with ROLLFORWARD only, while single BEFORE image journal is used with ROLLBACK only. MultiLoad, FastLoad and Archive/Recovery are impacted by the use of LOCAL journaling. See also the Teradata RDBMS for UNIX Database Design and Administration, Chapter 12, “Permanent Journaling”.

8-56

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE DATABASE

The following examples illustrate the use of CREATE DATABASE:

Examples Example 1

8

The following statement creates a database named Personnel from database Administration: CREATE DATABASE Personnel FROM Administration AS PERMANENT = 5000000 BYTES, FALLBACK, BEFORE JOURNAL, DUAL AFTER JOURNAL, DEFAULT JOURNAL TABLE = Personnel.FinCopy;

The FALLBACK keyword specifies that for each table created in the Personnel database, the default is to store a secondary (duplicate) copy in addition to the primary copy. The JOURNAL option specifies that the default journaling for each data table is to maintain a single copy of the before-change image and dual copies of the after-change image. A duplicate before-change image is maintained automatically for any table in this database that uses both the fallback and the journal defaults. The DEFAULT JOURNAL TABLE clause is required because journaling is requested. This clause specifies that a new journal table named “FinCopy” is to be created in the new database.

Example 2

8

To create a new database for the Finance department, use the “CREATE DATABASE” statement: CREATE DATABASE finance FROM sysadmin AS PERMANENT = 60000000 ,SPOOL = 120000000 ,FALLBACK PROTECTION ,AFTER JOURNAL ,BEFORE JOURNAL ,DEFAULT JOURNAL TABLE = finance.journals ,ACCOUNT = ’ACCTG’ ;

The Finance database is created from the space available in Sysadmin. The 60,000,000 value represents bytes of storage. To create a database, the initiator must have CREATE DATABASE privileges on the FROM entry. The new database receives all privileges that have been granted to the initiator. In the example, the FROM clause allocates space for finance from the Sysadmin space rather than from user space; therefore, Sysadmin is Finance’s immediate owner, and Marks is not in the hierarchy of ownership. However, Marks is granted automatic creator privileges on Finance, which include the right to create other objects in Finance’s space.

Teradata RDBMS for UNIX SQL Reference

8-57

Teradata SQL Syntax Guide CREATE DATABASE

The CREATE DATABASE statement does not include the PASSWORD and STARTUP clauses or the DEFAULT DATABASE and COLLATION options. Because these clauses and options affect the session environment (and only a user can establish a session), they do not apply to a database.

8-58

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE INDEX

CREATE INDEX

8

The CREATE INDEX statement creates a new secondary index on an existing table. The index can be optionally be given a name.

Function

CREATE INDEX is an extension to ANSI. It is flagged if the SQL flagger is enabled.

Syntax

,

8

, ( col_name )

INDEX

CREATE UNIQUE

name

ON

tname ; FF07A011

where:

Privileges

Locks

Maximum Number of Indexes

Syntax Element...

Description

UNIQUE

Specifies that no two rows in the table can have the same value or combination of values in the indexed fields.

name

name is optional, and specifies the name you assign to the UNIQUE or non-UNIQUE secondary INDEX (30 characters maximum).

col_name

Names one or more columns whose field values are to be indexed. If more than one column is named, the new index is based on the combined values of each column. A maximum of 16 columns can be defined for one index.

tname

Identifies the table for which an index is to be created.

8

To create new secondary indexes via CREATE INDEX, the user must have the INDEX or DROP TABLE privilege on the named table.

8

When CREATE INDEX is executed, an EXCLUSIVE lock is placed on the table that contains the index column(s). The time required depends on the number of rows in the table.

8

A maximum of 32 secondary indexes can be defined for one table and in one CREATE INDEX statement. This permits all the index operations for one table to be treated as a single transaction; that is,

Teradata RDBMS for UNIX SQL Reference

8-59

Teradata SQL Syntax Guide CREATE INDEX

all the indexes are created, or none of them are created. Up to 16 columns can be defined for each index. Alternatively, secondary indexes can be created as part of the CREATE TABLE statement.

Secondary Indexes Require Subtable Creation, Which Takes Space

Secondary Indexes Can Shorten Search Time

UNIQUE Versus NonUNIQUE

8

8

8

Secondary indexes require the creation of subtables in which to store index values and associated row IDs. Each secondary index subtable requires additional disk space, and insert, delete, and update operations require more processing time when secondary index subtables also need to be changed. Secondary index values can shorten table search time. Each row in a secondary index subtable is made up of the index value and one or more row IDs that point to the data row or rows containing that value. Therefore, when secondary index values are used as conditional expressions in SQL statements, only the row or rows that contain the specified value are accessed. The UNIQUE option prevents the occurrence of duplicate index values. Only one row can have a value that corresponds to an entry in the subtable of a unique secondary index. The subtable of a unique secondary index is a hashed table, which is very efficient for retrieving a single row. When the values of non-unique secondary indexes are used to apply complex conditional expressions to very large tables, efficient retrieval is achieved by bit mapping the subtable row IDs.

EXPLAIN Helps To Analyze Each Secondary Index

8

Before a request is processed, the Teradata RDBMS estimates comparative costs to determine whether the use of a secondary index offers savings or is plausible. If not, a full-table search is used. It is advisable, therefore, to test the performance of a secondary index before implementing it. The EXPLAIN modifier can be used to obtain a description of the processing sequence and thus determine whether a secondary index should be retained.

Named Indexes

8

The index name must conform to the usual rules for a Teradata SQL identifier and be unique in the table. See also “DROP INDEX”. Refer also to the examples below.

8-60

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE INDEX

The following examples illustrate the use of CREATE INDEX.

Examples Example 1

8

The following statement can be used to create a secondary index on the DeptNo column of the Employee table: CREATE INDEX (DeptNo) ON Employee;

The UNIQUE option is not used in this statement because multiple rows will contain the same department number. The index should be useful, however, because department numbers are often used for conditional retrievals and for ordering employee listings.

Example 2: Named Indexes

This example demonstrates a named index. CREATE UNIQUE INDEX Ident (Name, SocSecNo) ON Employee ; 8

This, in turn, allows the named index to be dropped using: DROP INDEX Ident ON Employee;

Example 3: Creating a Named Unique Secondary Index Example 4: Creating a Named Non-Unique Secondary Index Example 5: Creating an Unnamed Unique Secondary Index Example 6: Creating an Unnamed Non-Unique Secondary index Example 7: Non-Valid Use of CREATE INDEX

This example creates a named unique secondary index index_1. Index index_1 must not be an existing name in table table_1. 8

CREATE UNIQUE INDEX index_1 (field_1, field_2 ON table_1;

This example creates a named non-unique secondary index index_2. Index index_2 must not be an existing name in table table_2. 8

CREATE INDEX index_2 (field_1) ON table_2;

This example creates an unnamed unique secondary index. CREATE UNIQUE INDEX (field_1, field_2) ON table_3; 8

This example creates an unnamed non-unique secondary index. CREATE INDEX (field_1) ON table_4; 8

This example demonstrates how not to use CREATE INDEX. 8

CREATE UNIQUE INDEX ThisIsAnExtremelyLongNamedIndex (field_1, field_2) ON table_7;

The name for the index exceeds the maximum of 30 characters. The system returns the message: Name is longer than 30 characters.

Teradata RDBMS for UNIX SQL Reference

8-61

Teradata SQL Syntax Guide CREATE MACRO

CREATE MACRO

8

The CREATE MACRO statement defines a set of statements that are frequently used, or that perform a complex operation. The statements in the macro body are submitted when the macro is invoked by a subsequent EXECUTE statement.

Function

The user who executes a macro need not be aware of the tables affected by macro execution CREATE MACRO is flagged as non ANSI when the SQL flagger is enabled.

Syntax

8

CREATE MACRO

A

macroname ,

CM ( param_name

type declaration

)

type attribute A

AS (

statement ; USING modifier

LOCKING modifier

) ; FF07A051

where:

8-62

Syntax Element . . .

Specifies . . .

macroname

the name of the new macro. If a fully qualified name is not specified, the default database is used.

param_name

the name of a parameter that is replaced with a value during macro execution. When referenced in the macro body, a parameter name is prefixed by the : (colon) character. A parameter may be defined as a constant or a constant expression.

type declaration

a data definition or default definition for a parameter. If a default value is not assigned, the user must specify a value for the parameter at EXECUTE time. For a list of data types see Table 5-1.

type attribute

the data type attributes, such as NOT NULL, UPPERCASE, TITLE (quotestring), etc. For a list of data type attributes, see Table 5-2.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE MACRO Syntax Element . . .

Specifies . . .

USING modifier

one or more variable parameter names. A value is substituted for each parameter name when the request is processed. See also “USING Modifier”. This is an optional phrase. USING in a macro does not work when accessed via BTEQ. When running BTEQ, put USING in the EXECUTE statement. To use USING in a macro may require special programming. Refer to your NCR Field support engineer or the NCR Support Center for help.

LOCKING modifier

a lock on a database, table, view or row. It overrides the default usage lock placed in response to a request. See also “LOCKING Modifier”. This is an optional phrase. Specifies an SQL statement. Every statement in the macro body must be terminated by a semicolon.

statement

Parameter names referenced in the macro body are prefaced by the : (colon) character. The macro body may include EXECUTE statements to invoke other macros.

Why Use Macros?

Limit On Request Size

Employ USING Phrase to Retrieve Data Use Colon to Specify Value Insertion Variables

8

8

Executing a macro frees a user from having to enter the entire set of statements in the macro body each time the operation defined by these statements is needed. Macro execution simplifies an operation that is complex or must be performed frequently. Because expansions of source text in macro definitions are made to fully qualify object names and to normalize expressions, it is possible for a macro to be defined but to be unusable because of stack overflows in the Syntaxer at execution. In this case, the system returns a 3741 error. In designing a macro, employ the USING modifier to retrieve data from an external source such as a data file.

8

8

Use the : (colon) character to identify the fields whose values are to be inserted at execution time. For example, if the CREATE MACRO statement contained the following VALUES clause: VALUES (:name, :city, :empno)

then the EXECUTE statement might contain a clause such as: (name=’Kimble G’, city=’Altadena’, empno=8273)

Teradata RDBMS for UNIX SQL Reference

8-63

Teradata SQL Syntax Guide CREATE MACRO

Privileges

8

To create a macro, a user must have the CREATE MACRO privilege. The creator of a macro is automatically granted the DROP MACRO and EXECUTE privileges WITH GRANT OPTION on the macro. Access to data via a macro is controlled by the access rights of the macro owner. A user creating the macro must have the rights of all statements needed to be executed by the macro. To allow other users to execute the macro, the macro creator must have the privileges necessary to execute each statement, and all privileges must have the WITH GRANT OPTION. Once a macro has been created, its owner is the database in which it exists, not the user who created it. The owning database must have all the appropriate rights for executing the macro, including WITH GRANT OPTION.

Not All Data Attributes Supported in Macro Parameters Macro Execution

8

8

Not all of the data attributes that may be specified for table columns are relevant to, or operate fully (as a type declaration, type attribute) for, macro parameters. For example, CHECK constraints and the COMPRESS phrase are not supported. Executing a macro constitutes an implicit transaction. Therefore, in BTET transaction mode (Teradata RDBMS mode) a macro need not be enclosed by a BEGIN TRANSACTION statement and an END TRANSACTION statement. If the session executing a macro is in ANSI mode, then unless the body of the macro ends with a COMMIT, the actions of the macro are uncommitted until a commit or rollback occurs in subsequent statements. If a user defines a macro with a COMMIT, it may be executed only by sessions running in ANSI mode. Because macro execution implies a transaction, the user receives no response to individual statements contained in the macro body until all statements have been completed successfully. Any object that is referenced by a statement is locked until the macro transaction is completed, or until the macro is terminated because of a statement error. If a macro contains a data definition statement (CREATE, DROP, MODIFY, etc.), it may contain no other statement. Note that a data definition statement in a macro is not fully resolved until the macro is executed. At that time, unqualified references to database objects are resolved using the default database for the user submitting the EXECUTE statement. It is therefore recommended that object references in a macro data definition statement be fully qualified (e.g., databasename.tablename) in the macro body.

8-64

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE MACRO

Note: If the macro is used only in ANSI mode, add a COMMIT to the end of the macro. If the macro is used in both ANSI and Teradata mode, the user should provide the COMMIT, when in ANSI mode.

Resolution of Unqualified Names

8

When the CREATE MACRO statement is executed, the RDBMS parses the text of statements in the macro, and resolves names that are not fully qualified in those statements. Note, however, that unqualified names in DDL statements in macros are left unresolved. Consider the following example, in which the default database for user XYZ is database ABC, and the database for user LMN is database is DEF. User XYZ defines Macro1 thus: CREATE MACRO Macro1 AS (CREATE TABLE mambo AS (c1 INTEGER, c2 INTEGER););

The fully-qualified (databasename.tablename) name was not supplied in the CREATE TABLE DDL statement. Because CREATE MACRO is a DDL statement, the database name is not resolved when the macro is created, but instead, when the macro is executed. The result is that if user XYZ executes Macro1, table ABC.mambo is created, and if user LMN executes Macro1, table DEF.mambo is created. Now, if user XYZ creates another macro (Macro2) thus: CREATE MACRO Macro2 AS (UPDATE tango SET counter = counter + 1;);

The fully-qualified (databasename.tablename) table name was not supplied in the UPDATE statement. Because UPDATE is a DML statement, it is fully resolved when the macro is created. Subsequently, executing Macro2 results in ABC.tango being updated, whether executed by user ABC, LMN, or any other user. To summarize, the following are true for unqualified names in macros: ●



Using the * Symbol in Macros

They are not resolved in DDL statements when the macro is created—they are resolved when the macro is executed. They are fully resolved in DML statements when the macro is created.

A macro defined using the * symbol is forever bound by the definition of the table as of the time the macro was created. 8

For example: CREATE MACRO GetEmp AS (SELECT * FROM Employee;);

Teradata RDBMS for UNIX SQL Reference

8-65

Teradata SQL Syntax Guide CREATE MACRO

If a column is later added to or removed from the table, the following statement still returns the number of columns that existed in Employee when GetEmp was defined. EXECUTE GetEmp;

If columns have been dropped or data types have been changed, executing the Macro may result in an error message or unexpected behavior.

Naming Macros

Creating Macros

8

In creating a macro, the name must be unique in the database, because with one exception, duplicate-named objects are not permitted. A macro, table, or view may take the same name as the database in which it is created, but a macro named TEST and a table named TEST cannot co-exist in the same database. Use the following procedure to create a macro.

8

Step

Action

1

Identify the parameters.

2

Following the AS keyword, specify the SQL statements to perform the task, each terminated by a semicolon. The entire set of statements is enclosed in parentheses.

3

Rules for Using Parameters in Macros

8

Terminate the CREATE MACRO statement with a final semicolon.

Parameters are values that are entered by the user into the EXECUTE statement for use by the macro during execution. Use of parameters is optional in a macro and, when required, are specified as part of the CREATE MACRO statement. In defining parameters for a macro, follow the macro name in your CREATE MACRO statement with the names and attributes of the appropriate parameters. Note that data type definitions are required for each parameter. Other attributes may include format specifications or default values. Define new formats via a format phrase and defaults by a default control phrase, as necessary. In executing the macro, if you supply a parameter value in the EXECUTE statement that does not conform to the specified format, data type, or default value, execution halts and an error message is displayed.

8-66

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE MACRO

Examples Example 1

The following examples illustrate the use of CREATE MACRO: 8

8

The following statement creates a macro that first inserts a row for a new employee in the Employee table, then executes a SELECT statement to verify that the information was entered correctly: CREATE MACRO NewEmp1 (number INTEGER, name VARCHAR(12), dept INTEGER DEFAULT 900, position VARCHAR(12) sex CHAR, dob DATE FORMAT ’MMMbDDbYYYY’, edlev BYTEINT ) AS ( INSERT INTO Employee (Empno, Name, DeptNo, JobTitle, Sex, DOB, EdLev) VALUES (:number, :name, :dept, :position, :sex, :dob, :edlev) ; -The following select verifies the insert SELECT * FROM Employee WHERE EmpNo = :number ; ) ;

Use the -- construct to include comments in the macro. Text appearing after -- and up to the end of the line, is not executed. If this macro is executed in ANSI mode, the INSERT has not been committed. This is also true in Teradata mode, where the macro is executed as part of an explicit transaction, which is not common.

Example 2

8

This example creates a macro that also inserts a row for a new employee in the Employee table, but then executes an UPDATE statement rather than a SELECT statement. The UPDATE statement changes the Department table by incrementing the employee count in the row containing a department number that matches the value of the :dept parameter. CREATE MACRO NewEmp2 ( number INTEGER, name VARCHAR(12), dept INTEGER DEFAULT 900, position VARCHAR(12), sex CHAR, dob DATE FORMAT ’MMMbDDbYYYY’, edlev BYTEINT ) AS ( INSERT INTO Employee (Empno, Name, DeptNo, JobTitle, Sex, DOB, EdLev) VALUES (:number, :name, :dept, :position, :sex, :dob, :edlev) ; UPDATE Department SET EmpCount=EmpCount+1 WHERE DeptNo = :dept ; ) ;

If this macro is executed in ANSI mode, the INSERT and UPDATE have not been committed.

Teradata RDBMS for UNIX SQL Reference

8-67

Teradata SQL Syntax Guide CREATE MACRO

Example 3

8

Here, the INSERT statement is replaced by the DELETE statement, and the departmental count of employees in the Department table is decremented. The ABORT statement terminates macro execution if the row for the employee being deleted is not present in the Employee table. This example shows a macro designed for use in ANSI mode, and for which the user wants to commit if the delete and update are successful. Note that the statements in the body of the macro are entered as one multi-statement request. Therefore, if the WHERE condition of the ROLLBACK statement is met, the entire request is aborted and the value in EmpCount is protected. CM DelEmp (num SMALLINT FORMAT ’9(5)’, dname VARCHAR(12), dept SMALLINT FORMAT ’999’) AS ( ABORT’Name does not exist’ WHERE :num NOT IN (SELECT EmpNo FROM Employee WHERE Name = :dname) ; DELETE FROM Employee WHERE Name = :dname ; UPDATE Department SET EmpCount = EmpCount - 1 WHERE DeptNo = :dept; COMMIT; ) ;

Example 4

8

You can include a condition for halting execution of a macro by incorporating an ABORT (ROLLBACK) statement into the macro. If the specified condition is encountered during execution, the macro is aborted (the transaction in process is concluded, locks on the tables are released, changes made to data are backed out, and any spooled output is deleted). As an example, to restrict the NewEmp macro from being used to add employees to the Executive Office, department 300, incorporate an ABORT statement into the macro specification, as follows: CREATE MACRO Personnel.NewEmp ( Number (SMALLINT FORMAT ’9(5)’), Name (VARCHAR(12)), Dept (SMALLINT FORMAT ’999’), Position (VARCHAR(12)), Birthdate (DATE FORMAT ’MMMbDDbYYYY’), Sex (CHAR(1)) Education (BYTEINT) ) AS (ABORT ’Department 300 not valid’ WHERE :dept = 300 ; INSERT INTO employee (empno, name, deptno, jobtitle, dob, sex, edlev) VALUES (:number, :name, :dept, :position, :birthdate, :sex, :education) ; );

Following the ABORT keyword, specify the text of an optional error message, enclosed in apostrophes. This message is displayed on the terminal screen if the macro is aborted for the specified condition. The abort condition (:dept = 300) is identified via the WHERE clause in the ABORT statement.

8-68

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE

CREATE TABLE Function

8

The CREATE TABLE statement defines the columns, indexing, and other qualities of a new table. It can also define the data block size and percent freespace attributes. The table structure definition is stored in the Data Dictionary.

Teradata RDBMS for UNIX SQL Reference

8-69

Teradata SQL Syntax Guide CREATE TABLE

Syntax

8

, CREATE

TABLE

tname

A ,

SET

FALLBACK

MULTISET

NO WITH JOURNAL TABLE =

CT

PROTECTION tname dbname. JOURNAL

NO

BEFORE

DUAL AFTER JOURNAL Create Table Options

NO DUAL LOCAL NOT LOCAL FREESPACE = integer PERCENT DATABLOCKSIZE integer BYTES KBYTES KILOBYTES MINIMUM

DATABLOCKSIZE

MAXIMUM , A

(

col_name

data type declaration

)

B

data type attributes COMPRESS NULL

Column Storage Attributes

constant Column Definition

UNIQUE CONSTRAINT name

PRIMARY KEY CHECK

(

boolean condition

Column Constraint Attributes

)

REFERENCES tname ( col_name

, Table Level Definition

UNIQUE Unique Definition

CONSTRAINT name

Check Definition

col_name

)

PRIMARY KEY ,

FOREIGN KEY References Definition

(

)

(

col_name

)

REFERENCES tname

CONSTRAINT name

,

col_name

(

)

CHECK (boolean condition ) CONSTRAINT name

B Index Definition

,

, (

INDEX UNIQUE

PRIMARY

col_name

)

name FF07B048

8-70

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE

Data Type Declarations INTEGER SMALLINT BYTEINT DATE REAL DOUBLE PRECISION FLOAT ( integer ) DECIMAL ( integer

NUMERIC

)

, integer CHAR BYTES

( integer )

GRAPHIC VARCHAR

( integer )

CHAR VARYING VARBYTE VARGRAPHIC LONG VARCHAR LONG VARGRAPHIC FF07B050

Data Type Attributes NOT NULL UPPERCASE CASESPECIFIC NOT FORMAT

quotestring

TITLE NAMED DEFAULT

name number USER DATE TIME NULL WITH DEFAULT FF07B049

Teradata RDBMS for UNIX SQL Reference

8-71

Teradata SQL Syntax Guide CREATE TABLE

where Syntax Element...

Description

CREATE MULTISET TABLE

SET and MULTISET are used for duplicate row control.

CREATE SET TABLE

If there are any uniqueness constraints on any column or set of columns in the table definition, then the table cannot have duplicate rows even if declared MULTISET. IF this keyword is specified . . .

THEN duplicate rows are . . .

SET

not allowed. This is compatible with Teradata RDBMS tables from prior releases.

MULTISET

allowed, in compliance with the ANSI standard.

neither

a check is run on the mode the session is running in. When in Teradata mode, duplicate rows default to SET. When in ANSI mode, duplicate rows default to MULTISET.

tname

Specifies the name of the new table. If the name is not fully qualified, the name of the default database for the current session is assumed. Create Table Options

Create Table options are an extension to ANSI SQL, and flagged when the SQL flagger is enabled.

[NO] FALLBACK

8-72

Specification to choose or not choose duplicate copy protection for the table.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description

When FALLBACK is chosen, then duplicate copies of rows in the table are created and stored. The default for this option is established by a CREATE DATABASE, CREATE USER, LOCKING Modifier, or MODIFY USER statement for the database in which the table is to be created.

PROTECTION

This keyword can be optionally specified after the FALLBACK keyword. It has no affect whether specified or not.

WITH JOURNAL TABLE = dbname.

Specification to define or redefine the journal table for the data table being created. tname need not reside in the same database as the data table.

tname

IF a database is . . .

THEN . . .

specified

it must exist and tname must have been defined as its default journal name.

not specified

the default database for the current session is assumed and tname must have been defined as the default journal table.

tname can reside in the same database as the

table being created or in a different database. This clause is required if the CREATE TABLE statement requests some level of journaling, but a default journal table was not defined for the database in which the new table is being created. If a default journal table was defined for the database, then this clause may be used to override the default

Teradata RDBMS for UNIX SQL Reference

8-73

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description

NO BEFORE JOURNAL

Alters the number of before-change images to be maintained for the table.

DUAL BEFORE JOURNAL

If the JOURNAL keyword is specified without NO or DUAL, then a single copy of the image is maintained unless FALLBACK is in effect or is also specified. If journaling is requested for a table that uses fallback protection, DUAL images are maintained automatically.

NO AFTER JOURNAL DUAL AFTER JOURNAL LOCAL AFTER JOURNAL NOT LOCAL AFTER JOURNAL

Alters the type of image to be maintained for the table; any existing images are not affected until the table is updated. NO, DUAL, LOCAL, or NOT LOCAL: the NO and DUAL options specify the number of afterchange images to be maintained for the table. NOT LOCAL and LOCAL specify whether single after-image journal rows for non-fallback data tables are written on the same virtual AMP (LOCAL) as the changed data rows, or on another virtual AMP in the cluster (NOT LOCAL). See also “CREATE DATABASE”, “Local Journaling”, and “ALTER TABLE” “Journaling: Local”.

8-74

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description

BEFORE JOURNAL

Specifies the type of image to be maintained for the table.

AFTER JOURNAL

The default for this option is established by a CREATE DATABASE, CREATE USER, or MODIFY USER statement for the database in which the table is to be created. IF this keyword is specified . . .

THEN this type of journal image is maintained . . .

none

both. This option may appear twice in the same statement: once to specify a BEFORE or AFTER image, and again to specify the alternate type.

BEFORE or AFTER

only the default for that type is overridden. For example, if AFTER is specified, before-image journaling remains at the default setting.

BEFORE and AFTER

FREESPACE integer

both, but the two specifications must not conflict with one another.

Used to set the percent of free space that will remain on a cylinder during loading operations. Specifies the value of the percent freespace attribute to the specified value integer. If the specified value does not fall within the allowable range (0 to 75 percent), an error message is generated.

PERCENT

This keyword can be optionally specified. It has no effect whether it is specified or not.

Teradata RDBMS for UNIX SQL Reference

8-75

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description Data Block Size Option

DATABLOCKSIZE = integer BYTES

Sets the value of the data block size attribute to the value integer.

DATABLOCKSIZE = integer KBYTES

(See also the discussion of DEFAULT, MINIMUM or MAXIMUM DATABLOCKSIZE.)

DATABLOCKSIZE = integer KILOBYTES MINIMUMDATAB LOCKSIZE MAXIMUM DATABLOCKSIZE

The minimum DATABLOCKSIZE is 12 sectors, where each sector is 512 bytes i.e. the minimum is 6,144 bytes. The maximum DATABLOCKSIZE is 63 sectors, where each sector is 512 bytes, i.e. the maximum is 32,256 bytes. Column Definition

col_name

col_name specifies the name of one or more columns, in the order in which they and their attributes are to be defined for the table. Up to 256 columns can be defined for a table.

data type definition

Data type definition and data type attribute designates one or more data definition phrases that define data for the column.

data type attribute

A single Data Type must be specified for each col_name. The use of other phrases is optional. Data Types and attributes used to define table columns are described in the CREATE TABLE syntax diagrams and in Chapter 5. A column definition must include exactly one data type definition with optional data attributes. It is recommended that the type definition be listed before any attributes.

8-76

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description Column Storage Attributes

Column storage attributes are a Teradata RDBMS extension to ANSI SQL. COMPESS

COMPRESS allows specified values in one or more columns to be compressed to zero space. If any column is to be compressed, the order should be chosen to achieve maximum accessibility.

NULL

If NULL is specified, nulls are compressed. Null data for that value would not take much space, hence COMPRESSing nulls does not save as much space as COMPRESSing other data types.

constant

If constant is specified, NULLS and the value are compressed. Constants are typically character type quotestrings. The most space a numeric value can take is 8 bytes. Using COMPRESS on a numeric is not recommended. Using COMPRESS on fixed length character data saves space depending on the percentage of rows for which the compressed value is assigned. Using variable length character data is preferable to COMPRESS for most applications. Column Constraint Attributes

The column listed as UNIQUE or PRIMARY KEY must be NOT NULL. CONSTRAINT

name

Constraint names are an extension to ANSI SQL. See the Note below this table and the section “Constraint Names”.

UNIQUE

specifies that no two rows in the table can have the same value in the indexed field. Used on a single column.

PRIMARY KEY

designates that the values of the column are to be used as the basis of a unique primary or secondary index for the table.

Teradata RDBMS for UNIX SQL Reference

8-77

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description

CHECK (boolean conditio)

boolean condition is a conditional expression,

including scalar comparison predicates using, for example, any of the following: ● ● ● ●

=

> >=

See also Chapter 6, “SQL Expressions.” When CHECK is part of the column definition, then a search condition cannot reference any columns other than the one being defined, nor are aggregate functions allowed. See also “Check Constraints”, below. REERENCES tnme

col_name list

Each reference column in the REFERENCES clause identifies a col_name of the referenced table tname. tname must refer to a base table. It cannot refer to that of a view. See also REFERENCES, below. UNIQUE Definition

Table level definition. CNSTRAINT name

Constraint names are a Teradata RDBMS extension to ANSI SQL and are flagged as nonentry level ANSI SQL if the SQL flagger is enabled.

UNIQUE

specifies that no two rows in the table can have the same value in the field(s).

PRIMARY KEY

designates that the values of one or more columns are to be used as the basis of a unique primary key.

col_name

If more than one col_name is specified, the unique columns or primary key is based on the combined values of each column named. A maximum of 16 column names may be specified. Columns listed in a UNIQUE or PRIMARY KEY table constraint must all be NOT NULL.

8-78

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description REFERENCES Definition

Table level definition CONSTRAINT

name FOREIGN KEY

Constraint names are flagged as non entry level ANSI SQL, when the SQL flagger is enabled. The FOREIGN KEY and its use is described in Usage Notes. It is a table-level constraint. A maximum of 64 foreign keys can be defined for a single table.

col_name

col_name refers to column(s) defined in the CREATE TABLE statement.

REFERENCES

REFERENCES is described in the notes section.

name

the name of the referenced Parent table used in the referential constraint definition. If not in the same database as the Child table, it must be qualified.

col_nam

Designates one or more columns which comprise the Parent table’s PRIMARY KEY or UNIQUE columns. Optional column name in tname, used in the referential constraint definition. CHECK Definition

Table level definition. CONSTRAINT

name

Constraint names are flagged as non entry level ANSI SQL when the SQL flagger is enabled

Teradata RDBMS for UNIX SQL Reference

8-79

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description

CHECK (boolean condition)

boolean condition is a conditional expression,

including scalar comparison predicates using, for example, any of the following: ● ● ● ●

=

> >=

See also Chapter 6, “SQL Expressions.” When CHECK is part of the column definition, then a search condition cannot reference any columns other than the one being defined, nor are set specifications allowed. See also “Check Constraints” below. A table level CHECK can compare any columns defined for the table, against each other and against constants. INDEX Definition

INDEX

Keyword used to define the table’s indexes. An INDEX list is an extension to ANSI SQL. Unlike the indexes created by the UNIQUE and PRIMARY KEY constraint definitions, indexes defined by the index list may include nullable fields.

UNIQUE

UNIQUE may be defined for any or all secondary indexes, and the PRIMARY index.

PRIMARY

Each table must have a PRIMARY index. The primary index uses a hashing algorithm to determine where rows are stored on the AMPs of the Teradata RDBMS. If not specified, a default PRIMARY index is chosen

8-80

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE Syntax Element...

Description

name

Designates an optional name for the index.

col_name list

Designates one or more columns whose values are to be used as the basis for a primary or secondary index. If more than one name is specified, the index is based on the combined values of each column named. A maximum of 16 columns may be named for one index, and a maximum of 32 secondary indexes may be created for one table.

Column storage attributes are a Teradata RDBMS extension to ANSI SQL. Constraint names are intermediate ANSI SQL and are flagged by the SQL flagger as non Entry Level ANSI SQL.

Locks Privileges Required and Acquired

8

When a CREATE TABLE statement is entered, an EXCLUSIVE lock is placed on the table being created. To create a table, the user must have the CREATE TABLE privilege on the database in which the table is created.

8

The creator receives DROP TABLE, INDEX, INSERT, UPDATE, DELETE, REFERENCE, SELECT, and DUMP and RESTORE privileges on the newly created table, WITH GRANT OPTION. If the JOURNAL option is specified, the user also must have INSERT privilege on the journal table.

Table Size and Maximum Columns Per Table Compressing Columns

If a CREATE TABLE statement defines more than 256 columns, the Teradata RDBMS returns an error message. 8

8

The first column defined in the CREATE TABLE statement is assigned the lowest column ID. Column IDs may be important if you specify any column for COMPRESS). As a general rule, the compressed column that will have the highest rate of access and the lowest rate of compressible values should be defined before other compressed columns.

Assigning a Journal to the Table

8

To assign a journal to a new data table, the named journal table must exist in the database being used. If it does not, a journal table can be added or created by using MODIFY DATABASE or MODIFY USER. Then CREATE TABLE can be resubmitted, or ALTER TABLE can be used to assign the new journal table to the existing data table.

Teradata RDBMS for UNIX SQL Reference

8-81

Teradata SQL Syntax Guide CREATE TABLE

SET and MULTISET

Constraint Names

8

8

If neither SET or MULTISET is specified explicitly, then assignment is made according to the rules in the following table. IF the session creating the table is in this mode . . .

THEN this keyword is implied . . .

ANSI

MULTISET

Teradata

SET

Constraint name must conform to the rules for a Teradata name (30 characters maximum) and be unique among all other constraint names specified in the table. The characters used can be: ● ● ●

uppercase and lowercase alphabetic characters integer characters - , #, or $

Unnamed constraints will not be assigned system-generated names.

CHECK Constraints

8

The Teradata RDBMS supports the CHECK constraint both at the column and at the table level. It allows constraints that reference one or more fields in a data row. The following rules apply to CHECK constraints. ●



If a CHECK constraint is added as part of the column specification then the “boolean” (search) condition cannot reference any columns other than the one being defined. If more than one unnamed CHECK constraint is defined for a column in the column specification, they are combined into a single column constraint. However, each named column-level CHECK constraint is handled as though it is a table-level named CHECK constraint. The only difference between defining a named CHECK constraint in the column definition and table definition is that when a named CHECK constraint is defined in the column definition, it must satisfy the rule for column constraints described under the first bullet in this list.



To ensure maximum system performance, there is a limit of 100 table-level constraints that can be defined for any table. A combination of table-level, column-level, and WITH CHECK on view constraints may create a constraint expression that is too large to be parsed for INSERT and UPDATE statements.

8-82

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE ●

Unnamed CHECK constraints with identical wording and case are considered to be duplicates, and the system returns an error stating ‘Check already exists’ in those cases. For example, the CREATE TABLE statement CREATE TABLE t1 (f1 int, check (f1 > 0, check (F1 >0));

is valid because the case of f1 and F1 is different, but the CREATE TABLE statement CREATE TABLE t1 (f1 int, check (f1 > 0), check (f1

>

0));

is not valid and returns the error message. ●

BETWEEN ... AND ...

8

The Boolean (search) condition for a table level CHECK constraint can not reference columns from other tables. Also, aggregate functions are not allowed.

The Teradata RDBMS construct of: BETWEEN number AND number is still supported, and treated as equivalent to: CHECK (field-name BETWEEN

number

AND

number)

New applications should use the (ANSI compliant) CHECK syntax.

FOREIGN KEYS

8

If a FOREIGN KEY is specified in a table-constraint, the FOREIGN KEY column-name-list must refer to columns defined within the CREATE TABLE statement for the target table, and the same column name should not be included in the FOREIGN KEY definition more than once. The column-name-list should be identical to a list of columns in the referenced table which are defined either as the primary key columns or as a set of columns with the unique attribute. Table-name refers to the referenced table which must be a base table, and not a view. The following rules apply to foreign keys. ●



REFERENCES

8

A maximum of 64 FOREIGN KEYS can be defined on a single table. This includes all references to and from the table. If 10 tables (t1, t2, ... t10) reference table tx, only 64 - 10 = 54 additional FOREIGN KEYS can be defined on table tx. Each FOREIGN KEY can consist of up to 16 columns.

Each column in the REFERENCES clause should identify a column of the referenced table, and the same column name should not be identified more than once.

Teradata RDBMS for UNIX SQL Reference

8-83

Teradata SQL Syntax Guide CREATE TABLE

The following rules apply to column- and table-level rules for REFERENCES. ●





The referencing column list should contain the same number of column names as the referenced column list. The i-th column of the referencing list corresponds to the i-th column identified in the referenced list. The data type of each referencing column should be the same as the data type of the corresponding referenced column. The user issuing the CREATE TABLE statement containing a REFERENCES option must have the REFERENCE right on the referenced table or on all specified columns of the referenced table. If REFERENCES is specified in a column-constraint, table-name defines the referenced table. It must refer to a base table, and not a view.

The following rules apply to column-level rules for REFERENCES. ●



UNIQUE PRIMARY KEY Versus INDEX

8

If the col_name is omitted then the referenced table must be defined with a single column, primary key and the foreign key column references the primary key column of the referenced table. If the col_name is present then it must be the referenced table single column PRIMARY KEY, or a referenced table single column defined with the UNIQUE attribute.

As noted in the CREATE TABLE syntax, UNIQUE and PRIMARY KEY may be defined as constraints on a table. The internal Teradata implementation of these constraints is as indexes, with the restrictions that these indexes are always UNIQUE, and cannot have nullable columns. The Teradata RDBMS also allows the syntax [ UNIQUE [ PRIMARY] INDEX. Non-unique primary and secondary indices (NUPIs and NUSIs) are still supported. Non-unique indexes are flagged as non-ANSI when the SQL flagger is enabled.

Index Types Primary Indexes

8-84

Selection of indexes is an important aspect of database design. 8

8

The primary index uses a hashing algorithm to determine where rows are stored on the AMPs of the Teradata RDBMS. Selection or update operations that use the primary index in a WHERE clause may use this hashing algorithm to determine the location of any row with maximum efficiency.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE

You cannot compress columns that are components of a primary index.

Secondary Indexes

8

Up to 32 secondary indexes can be defined for one table, and any secondary index can be unique or non-unique. Secondary indexes also may be added to an existing table with the CREATE INDEX statement. Each secondary index requires the creation of a subtable; insert, delete, and update operations require more processing time when secondary index subtables also need to be changed. Secondary indexes can greatly reduce table search time. Each row in a secondary index subtable contains the index value and one or more row IDs that point to the data row(s) containing that value. The subtable of a unique secondary index is a hashed table and is efficient for selecting a single row or a small number of rows. Bit mapping of the row IDs of non-unique secondary indexes provides efficient retrieval when complex conditional expressions are applied to very large tables. It is advisable to test the performance of a secondary index before implementing it.

PRIMARY KEY and UNIQUE Constraints Vs. PRIMARY INDEXES

The user is allowed to define the primary indexes using PRIMARY KEY and UNIQUE constraints syntax in a CREATE TABLE statement. 8

The rules are: ●











A table can have at most only one PRIMARY KEY and must have one PRIMARY INDEX. If both a PRIMARY INDEX and PRIMARY KEY are specified in a CREATE TABLE statement, then the PRIMARY INDEX is the hashing index and the PRIMARY KEY is mapped to a unique secondary index (USI). If a PRIMARY KEY is specified in a CREATE TABLE statement and the PRIMARY INDEX is not, then the PRIMARY KEY is mapped to unique primary index (UPI). If neither PRIMARY INDEX nor PRIMARY KEY is specified in a CREATE TABLE statement then the first UNIQUE constraint is considered as the UNIQUE PRIMARY INDEX (UPI). If there is no PRIMARY INDEX, PRIMARY KEY, or UNIQUE constraint specified in a CREATE TABLE statement, then the first column of the table is used as the primary index. This index is non-unique except for the case of a single column table with the SET option. Columns defined as PRIMARY KEY or UNIQUE, must not be nullable. However, columns defined as [UNIQUE] PRIMARY INDEX or [UNIQUE] INDEX can be nullable.

Teradata RDBMS for UNIX SQL Reference

8-85

Teradata SQL Syntax Guide CREATE TABLE

It is not permitted to define a PRIMARY KEY or UNIQUE constraint with the same column or set of columns, as a defined INDEX. A UNIQUE constraint cannot be defined on the same columns as a PRIMARY KEY.

Nullable Columns are Accepted in UNIQUE [PRIMARY] INDEXes

DATABLOCKSIZE

Columns defined as primary keys or unique must not be nullable. However, nullable columns are accepted in UNIQUE [PRIMARY] INDEXes. 8

8

If an index declared as a UNIQUE INDEX or UNIQUE PRIMARY INDEX consists of a single column, only one row can exist in the table with a null value for that column. If an index declared as a UNIQUE index or UNIQUE PRIMARY index consists of several columns the table cannot have more than one row in which all of the non-null values are identical. DATABLOCKSIZE controls the value of the data block size attribute, as follows: ●

If BYTES is specified, the value of the data block size attribute is the result of rounding n to the nearest multiple of the sector size (in bytes), where the rounding is upward in the case that n is exactly halfway between two consecutive multiples. A sector is 512 bytes. If the computed value for the data block size attribute does not fall within the allowable range (6144 to 32256 bytes), an error message is returned.



If KILOBYTES is specified, the data block size attribute is the result of rounding 1024n (instead of n) in the same manner. In either case, the attribute value will determine the maximum size of multiple-row data blocks in the table.



If neither BYTES nor KILOBYTES is specified, BYTES is assumed as the default.

As is the case elsewhere in Teradata SQL, BYTES can be abbreviated to BYTE. The word KILOBYTES can be abbreviated to KBYTE. Neither KILOBYTES nor KBYTES is a reserved word in Teradata SQL. At any give time, the DATABLOCKSIZE value returned in response to a SHOW TABLE command is the value specified in the CREATE TABLE (or most recently entered ALTER TABLE) command. See also the descriptions of “ALTER TABLE” and “SHOW MACRO SHOW TABLE SHOW VIEW”.

8-86

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE

MINIMUM or MAXIMUM DATABLOCKSIZE

Since the RDBMS allows data block sizes in the range 6144 bytes (12 sectors) to 32256 bytes (63 sectors): 8





MINIMUM DATABLOCKSIZE option is identical to specifying DATABLOCKSIZE = 6144 BYTES MAXIMUM DATABLOCKSIZE is identical to specifying DATABLOCKSIZE = 32256 BYTES.

As is the case elsewhere in Teradata SQL, MINIMUM and MAXIMUM can be abbreviated as MIN and MAX, respectively.

FREESPACE

8

FREESPACE = n specifies the value for the percent freespace attribute, assuming that n is an integer constant. Consequently, n percent free space will remain on each cylinder during bulk load operations on this table. The PERCENT clause does not affect the semantics. ●





Specifying Data Protection

8

The percent freespace attribute must be in the range 0 to 75 percent. If n is decimal or floating point constant, an integer value is obtained in the same manner as in other similar cases existing in Teradata SQL, namely, by taking the result of truncating n. At any give time, the FREESPACE value returned in response to a SHOW TABLE command is the value specified in the CREATE TABLE (or most recently entered ALTER TABLE) command. See the descriptions of the “ALTER TABLE” and “SHOW MACRO SHOW TABLE SHOW VIEW” statements.

Refer to Teradata RDBMS for UNIX Database Design and Administration for further information on optimizing data protection. Specific protection for the data in a table is provided by the FALLBACK and the JOURNAL options. The FALLBACK option causes a duplicate copy (the fallback copy) to be maintained by the Teradata RDBMS. The fallback copy is used if the primary copy becomes unavailable. The JOURNAL option provides data protection through system-generated before- and after-journals that contain the data that changed. These journals are used to restore a table or to reverse the changes that were made to a table.

Providing for Fallback Data

8

If you have specified FALLBACK (either explicitly or by default) in creating the table, the Teradata RDBMS automatically maintains a duplicate (fallback) copy of the data in the table. This fallback copy is used if the primary copy becomes unavailable. To create a fallback copy of a new table, include the FALLBACK option in your CREATE TABLE statement. If there is to be no fallback copy of a new table in a database or user space for which

Teradata RDBMS for UNIX SQL Reference

8-87

Teradata SQL Syntax Guide CREATE TABLE

FALLBACK is in effect, specify the NO FALLBACK option as part of the CREATE TABLE statement defining the new table. Alternatively, if the default is desired, do not specify the option for the table. Unless specified as part of the CREATE TABLE statement, protection for the data in a new table defaults to that specified for the database in which the table is created.

Providing for a Permanent Journal

8

A permanent journal table is created when the default JOURNAL table option and a journal table name are specified as part of the CREATE/MODIFY DATABASE/USER statement. The named table resides in the database or user space that defined it. In addition, the specified JOURNAL parameters serve as the defaults for all the data tables created in that database or user space. In defining a new table, you may override the established journaling defaults by specifying the JOURNAL options as part of the CREATE TABLE statement. Thus you may specify the journal table, the type of image (before-change or after-change, or both) and whether dual images are to be maintained for the created table, no matter the option established when the containing database or user was created.

8-88

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE

The following examples illustrate the use of CREATE TABLE:

Examples Example 1

8

The following CREATE TABLE statement defines the Employee table. CREATE TABLE Employee (EmpNo SMALLINT FORMAT ’9(5)’ CHECK (EmpNo >= 10001 AND EmpNo = 100 AND DeptNo = 1.00 AND Salary = -99 AND YrsExp =0 AND EdLev = -99 AND HCap = 10001 AND EmpNo = 100 AND DeptNo = 1.00 AND Salary = -99 AND YrsExp =0 AND EdLev = -99 AND HCap 0), field_4 INTEGER CONSTRAINT reference_1 REFERENCES parent_1 ) ;

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE

Example 5: Named Constraints - Table Level

Example 6: Named and Unnamed Constraints Table and Column Levels

The statement in this example names constraints at the table level. 8

CREATE TABLE good_2 ( field_1 INTEGER NOT NULL field_2 INTEGER NOT NULL, field_3 INTEGER NOT NULL, field_4 INTEGER NOT NULL, field_5 INTEGER, field_6 INTEGER, CONSTRAINT primary_1 PRIMARY KEY (field_1, field_2), CONSTRAINT unique_1 UNIQUE (field_3, field_4), CONSTRAINT check_1 CHECK (field_3 > 0 OR field_4 IS NOT NULL), CONSTRAINT reference_1 FOREIGN KEY (field_5, field_6) REFERENCES parent_1 (field_2, field_3) ) ;

The statement in this example defines named and unnamed constraints at both the column and table levels. 8

CREATE TABLE good_3 ( field_1 INTEGER NOT NULL CONSTRAINT primary_1 PRIMARY KEY, field_2 INTEGER NOT NULL CONSTRAINT unique_1 UNIQUE CONSTRAINT check_1 CHECK (field_2 3) CONSTRAINT reference_1 REFERENCES parent_1 CHECK (field_2 > 0) REFERENCES parent_1 (field_4), field_3 INTEGER NOT NULL field_4 INTEGER NOT NULL field_5 INTEGER, CONSTRAINT unique_2 UNIQUE (field_3), CONSTRAINT check_2 CHECK (field_3 > 0 AND field_3 < 100), CONSTRAINT reference_2 FOREIGN KEY (field_3) REFERENCES parent_1 (field_5), UNIQUE (field_4), CHECK (field_4 > field_5), FOREIGN KEY (field_4, field_5) REFERENCES parent_1 (field_6, fielt_7) ) ;

Teradata RDBMS for UNIX SQL Reference

8-91

Teradata SQL Syntax Guide CREATE TABLE

Example 7: Named and Unnamed CHECK Constraints at Both Levels

Example 8: Primary Index and Primary Key Specification

The statement in this example combines the three unnamed CHECKs for field_1. Constraint check_0 and each of the named CHECKs for field_2 are treated as Table Constraints. 8

CREATE TABLE good_4 ( field_1 INTEGER CHECK (field_1 > 0) CHECK (field_1 < 999) CHECK (field_1 NOT IN (100,200,300) CONSTRAINT check_0 CHECK (field_1 IS NOT NULL), field_2 INTEGER CONSTRAINT check_1 CHECK (field_2 > 0) CHECK (field_2 < 999) ) ;

The statement in this example specifies both a primary index and primary key. 8

The Primary Key (field_1) is mapped to a unique secondary index (USI). The UNIQUE constraint on field_3 also is mapped to a unique secondary index. These two secondary indexes cannot be null. The Unique Primary Index (field_2) is the hashing index and can be null. CREATE TABLE good_5 ( field_1 INTEGER NOT NULL PRIMARY KEY, field_2 INTEGER, field_3 INTEGER NOT NULL UNIQUE, field_4 INTEGER ) UNIQUE PRIMARY INDEX (field_2 ) ;

Example 9: Primary Key Specification

8

The statement in this example specifies a primary key only, so field_2 is mapped to a unique primary index (UPI). In this case the PRIMARY index cannot be null. CREATE TABLE good_6 ( field_1 INTEGER, field_2 INTEGER NOT NULL PRIMARY KEY ) ;

Example 10: Neither Primary Index Nor Primary Key Are Specified

8-92

The statement in this example specifies neither a primary index nor a primary key. The RDBMS maps the first unique field, field_2, to a unique primary index (UPI), which cannot be null. 8

CREATE TABLE good_7 ( field_1 INTEGER, field_2 INTEGER NOT NULL UNIQUE, field_3 INTEGER NOT NULL UNIQUE ) ;

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE TABLE

Example 11: Non-Valid Use of CHECK Constraint

The statement in this example is not valid because it contains duplicate CONSTRAINT names (dup_name). 8

CREATE TABLE bad_1 ( field_1 INTEGER CONSTRAINT dup_name CHECK (field_1 > 0), field_2 INTEGER NOT NULL CONSTRAINT dup_name UNIQUE ) ;

The system returns the error message: Constraint with the same name “dup_name” already exists in table.

Example 12: Non-Valid Use of Primary Key ‘Constraint’

The statement in this example is not valid because it defines more than one PRIMARY KEY constraint for the table. 8

CREATE TABLE bad_2 ( field_1 INTEGER NOT NULL CONSTRAINT primary_1 PRIMARY KEY, field_2 INTEGER NOT NULL, PRIMARY KEY (field_2) ) ;

The system returns the error message: More than one primary index or primary key specified.

Example 13: Non-Valid Use of Constraint Name

The statement in this example is not valid because the CONSTRAINT name exceeds the limit of 30 characters. 8

CREATE TABLE bad_3 ( field_1 INTEGER NOT NULL CONSTRAINT ThisIsAVeryLongNamedUniqueConstraint UNIQUE ) ;

The system returns the error message: Name is longer than 30 characters.

Teradata RDBMS for UNIX SQL Reference

8-93

Teradata SQL Syntax Guide CREATE TABLE

Example 14: Local Journaling

As an example, given the following CREATE TABLE statements: 8

CREATE TABLE cyk.test_1, NO FALLBACK, AFTER JOURNAL (x INTEGER, Y INTEGER) PRIMARY INDEX(X); CREATE TABLE cyk.test_2, NO FALLBACK,NOT LOCAL AFTER JOURNAL (x INTEGER, Y INTEGER) PRIMARY INDEX(X); CREATE TABLE cyk.test_3, NO FALLBACK, LOCAL AFTER JOURNAL (x INTEGER, Y INTEGER) PRIMARY INDEX(X);

SHOW TABLE produces the following output: CREATE TABLE cyk.test_1, NO FALLBACK, NO BEFORE JOURNAL, NOT LOCAL AFTER JOURNAL (x INTEGER, Y INTEGER) PRIMARY INDEX(X); CREATE TABLE cyk.test_2, NO FALLBACK, NO BEFORE JOURNAL, NOT LOCAL AFTER JOURNAL (x INTEGER, Y INTEGER) PRIMARY INDEX(X); CREATE TABLE cyk.test_3, NO FALLBACK, NO BEFORE JOURNAL, LOCAL AFTER JOURNAL (x INTEGER, Y INTEGER) PRIMARY INDEX(X);

8-94

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE USER

CREATE USER

8

The CREATE USER statement defines a new user of the Teradata RDBMS.

Function

CREATE USER is flagged as non ANSI by the SQL flagger.

Syntax CREATE USER

name

=n

PERMANENT

AS

A

PERM

FROM dbname

,

BYTES

, A

PASSWORD =

pwd ;

NULL

STARTUP = 'string;'

,

SPOOL = n BYTES DEFAULT DATABASE = dbname COLLATION =

ASCII EBCDIC MULTINATIONAL HOST

ACCOUNT =

'acctid' ( 'acctid'

,

,'acctid'

)

FALLBACK NO

PROTECTION JOURNAL BEFORE

NO DUAL

AFTER JOURNAL NO DUAL LOCAL NOT LOCAL

tname

DEFAULT JOURNAL TABLE =

dbname.

Teradata RDBMS for UNIX SQL Reference

FF07R052

8-95

Teradata SQL Syntax Guide CREATE USER

where: Syntax Element...

Description

name

Specifies the name of the new user.

FROM

Introduces an optional clause that specifies the owner dbname.

dbname

Specifies the name of the immediate owner user/ database. The default is the username associated with the current session.

AS

Introduces the first user options. Subsequent options may be entered in any order.

PERMANENT = n BYTES

Specifies the total number of bytes (n) to be reserved for permanent storage of the new user database. The space is taken from unallocated space in the database of the immediate owner. n may be entered as an integer, a decimal value, or as a floating point value. This option must be specified. There is no default.

PASSWORD

Keyword introducing the clause that specifies pwd or NULL. This option must be specified. There is no default.

pwd

Specifies the password associated with the new user. A password is considered to be a Teradata SQL word and may contain up to 30 characters. Refer to Chapter 4, “Teradata SQL Lexicon”, for a complete description of Teradata SQL ‘words’.

NULL

NULL allows the user to logon without a password, as long as the user is authorized by the TDP or CLI.

STARTUP

Keyword introducing the clause declaring the startup string.

’string;’

Specifies one or more SQL statements separated by semicolons, that are executed to establish the session environment when the user logs on. Strings may be up to 255 characters, must be terminated by a semicolon, and must be enclosed by apostrophes. The default is no startup string. A startup string may execute a macro, however, the USING modifier is not supported in a startup string (see Usage Notes). If a string includes a DDL statement, no other statement is allowed in the string. Strings are only executed when you logon through BTEQ or ITEQ.

SPOOL

8-96

Keyword introducing the bytes allocated to the spool file.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE USER Syntax Element...

Description

n

Specifies the number of bytes (n) to be allocated for spool files. The default is the largest value that is not greater then the owner’s spool space, and that is a multiple of the number of AMPs on the system. n must not exceed the owner’s spool space. n may be entered as an integer, a decimal value, or as a floating point value.

BYTES

Optional unit assigned to n bytes allocated to the spool file. n refers to bytes, whether or not BYTES is appended.

DEFAULT DATABASE

Keyword introducing the database name, dbname.

dbname

Specifies the name of the default database established each time a user logs on the Teradata RDBMS. The username associated with the CREATE USER statement is the default if the DEFAULT DATABASE clause is not specified.

COLLATION

Keyword introducing the clause that sets a collation default for the user.

ASCII

The default HOST collation is ASCII for configurations other than an IBM channel-attached host. ASCII here refers to Teradata’s extension to the ASCII standard.

EBCDIC

The default HOST collation is EBCDIC for an IBM channel-attached host.

MULTINATIONAL

MULTINATIONAL is always available in this release. It specifies that the default collation for the user is one of the European (diacritical) or Japanese character (Kanji) sort sequences (see Usage Notes).

HOST

HOST specifies that the default collation for the user is determined by the collation of the logon client system. This is the default.

ACCOUNT

Keyword introducing the account id identifiers.

’acctid’

Specifies an identifier or a list of identifiers of the account(s) that are to be charged for the space used by this user. The first (or only) identifier becomes the default account for the user. If acctid is specified in the logon string, it must have been specified in the ACCOUNT option of the last CREATE USER or MODIFY USER statement for the user. If acctid is not specified, it defaults to the account identifier. Each acctid can be up to 30 characters and must be enclosed in apostrophes. A list of acctids must be separated by commas and enclosed in parentheses.

Teradata RDBMS for UNIX SQL Reference

8-97

Teradata SQL Syntax Guide CREATE USER Syntax Element...

Description

NO FALLBACK PROTECTION

Specifies whether to create and store a duplicate copy of each table created in the new database. The default value is NO FALLBACK. This setting may be overridden for a particular data table when the table is created (refer to “CREATE TABLE” statement). The FALLBACK keyword used alone implies PROTECTION.

NO JOURNAL

Specifies the number of before change images to be maintained by default for each data table created in the new database.

DUAL JOURNAL BEFORE JOURNAL

The JOURNAL keyword without NO or DUAL implies single-copy journaling. If journaling is specified, a DUAL journal is maintained for data tables with FALLBACK protection. The JOURNAL keyword without BEFORE implies both types (BEFORE and AFTER) of images.

8-98

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE USER Syntax Element...

Description

NO AFTER JOURNAL

Specifies the type of image to be maintained by default for data tables created in the new database.

DUAL AFTER JOURNAL

The JOURNAL keyword without NO or DUAL implies single-copy journaling.

LOCAL AFTER JOURNAL

If journaling is specified, a DUAL journal is maintained for data tables with FALLBACK protection.

NOT LOCAL AFTER JOURNAL

NOT LOCAL and LOCAL specify whether single after-image journal rows for non-fallback data tables are written on the same virtual AMP (LOCAL) as the changed data rows, or on another virtual AMP in the cluster (NOT LOCAL). The JOURNAL keyword without AFTER implies both types (BEFORE and AFTER) of images. The default is no journaling If only AFTER JOURNAL is specified, then a before-change image is not maintained. If both types are specified, the two specifications must not conflict. This setting may be overridden for a particular data table when the table is created (refer to “CREATE TABLE” statement).

DEFAULT JOURNAL TABLE = dbname.tname

Identifies the default table that is to receive the journal images of data tables created in the new database. tname must be defined if journaling is requested, but it need not reside in the new database. tname is automatically created in the new database if dbname is not specified, or the new database is specified. If a different database is specified, then that database must exist and tname must have been defined as its default journal table.

An option may only be specified once the PERMANENT or PERM option and the PASSWORD options are specified.

Maxima

See also “CREATE DATABASE”. The combined total number of databases and users for any one system is limited to 32,767.

Teradata RDBMS for UNIX SQL Reference

8-99

Teradata SQL Syntax Guide CREATE USER

Required Privileges

8

To create a user, the creator must have CREATE USER privilege on the immediate owner database. The creator receives all privileges on the newly created user: ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●

Create Database Create Macro Checkpoint Create Table Create User Create View Delete Drop Database Drop Macro Drop Table Drop User Drop View Dump Execute Insert Revoke Restore Update.

Privileges given to the creator and owner are WITH GRANT OPTION. The newly created user also receives all relevant privileges on itself, namely: ● ● ● ● ● ● ● ● ● ● ● ● ● ●

8-100

Create Macro Checkpoint Create Table Create View Delete Drop Macro Drop Table Drop View Dump Execute Insert Revoke Restore Update.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE USER

Privileges given to the new user are not WITH GRANT OPTION. The newly created user is automatically granted the privileges needed to create and maintain objects in his or her space, but must be explicitly granted the privileges needed to CREATE USER/MODIFY USER or CREATE DATABASE (see also the “GRANT” statement). The privileges automatically granted are: The acctid may include an optional priority-level prefix. User priorities are used to maintain efficient operation of the Teradata RDBMS while providing equitable service to multiple client systems and users. Any user who has the CREATE/MODIFY USER privilege may assign a priority; therefore, the Teradata RDBMS administrator should monitor priority assignments closely.

Collation

8

The COLLATION option determines the ordering of data characters during comparison operations and when sorting data in response to a SELECT query that includes an ORDER BY or WITH...BY clause. If the COLLATION option is not given the default is HOST. During any session, the user can override the COLLATION attribute by executing a SET SESSION COLLATION statement.

Collation: Multinational

8

The MULTINATIONAL parameter defines the default collation sequence for the user to be one of the International sort orders (see the ORDER BY clause of the “SELECT” statement). The MULTINATIONAL parameter determines that the International sort order is to be used (see the ORDER BY clause of the SELECT statement in Chapter 7). This parameter returns an error unless the hashing algorithm of the Teradata RDBMS is set to recognize diacritical characters. On International/European sites MULTINATIONAL collation is two level. On Japanese sites, it is one level. It is always available.

Acctids Can Be Specified During Logon

8

The user may specify an acctid at logon time by including an acctid in the logon string. The acctid specified at logon must be one of the acctid’s defined for the user. If not specified in the logon string, the default is the first (or only) acctid defined in the most recent CREATE/MODIFY USER statement for the user. If not specified in a CREATE/MODIFY USER statement, the default is the acctid of the user’s immediate owner database. CPU and I/O resources expended by a session are charged to the account specified (or implied) at logon time.

Teradata RDBMS for UNIX SQL Reference

8-101

Teradata SQL Syntax Guide CREATE USER

Reporting Space Used by User PERM/SPOOL Space

USING Modifier in STARTUP String

8

8

8

All space (permanent and spool) used by the user is reported in the Teradata views under the acctid specified at logon (or the default acctid, if not specified at logon). If necessary, the defined PERM or SPOOL space is changed to the next higher value that is a multiple of the number of AMPs on the system. If no SPOOL space is defined, the space assigned is the largest value that is not greater then the owner’s spool space and that is a multiple of the number of AMPs on the system. The USING modifier is not supported in a STARTUP string. If it is specified, no error is indicated when the CREATE USER statement is processed, and the USING clause is stored. However, when the string is invoked, a message is returned that a “Data or IndicData parcel” is not found or is not allowed. A user/database can contain only one journal table.

LOCAL JOURNALING

8

The LOCAL single AFTER image journal is supported analogously to single BEFORE image journal: ●





Example

8-102

The Access Rights required to create or drop LOCAL single AFTER image journal are the same as for the analogous operation on single BEFORE image journal. LOCAL single AFTER image journaling is restricted to nonfallback data tables. Archive/Recovery rules for LOCAL single AFTER image journal and single BEFORE image journal are the same, except that LOCAL single AFTER image journal is used with ROLLFORWARD only, while single BEFORE image journal is used with ROLLBACK only.

Assume that a Teradata RDBMS has just been installed. The system administrator has logged on as user DBC, and has submitted a CREATE USER statement to create the administrative user called Sysadmin. As user Sysadmin, the administrator submits CREATE DATABASE statements to create such required databases as Finance, Executive, Engineering, Manufacturing, and Marketing.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE USER

The administrator adds a new user to the finance database via the CREATE USER statement, as follows: CREATE USER marks FROM finance AS PERMANENT = 1000000 ,PASSWORD = finan1 ,SPOOL = 1200000 ,FALLBACK PROTECTION ,DUAL AFTER JOURNAL ,DEFAULT JOURNAL TABLE = finance.journals ,DEFAULT DATABASE = finance ,STARTUP = ’EXEC setpf;’ ,ACCOUNT = ’$mfinance’,’$hfinance’ ;

Once user Marks is created, Sysadmin submits the following statement to grant Finance privileges with the WITH GRANT OPTION to Marks: GRANT ALL ON finance TO marks WITH GRANT OPTION ;

Teradata RDBMS for UNIX SQL Reference

8-103

Teradata SQL Syntax Guide CREATE VIEW

CREATE VIEW Function

The CREATE VIEW statement defines a relational view of data in the database.

8-104

Teradata RDBMS for UNIX SQL Reference

8

Teradata SQL Syntax Guide CREATE VIEW

Syntax viewname

CREATE VIEW

A

AS , ( col_name

)

A

B

dbname

LOCKING DATABASE

LOCK

tname

ACCESS FOR

EXCLUSIVE

IN

EXCL

TABLE

MODE

NOWAIT

SHARE

vname

READ

VIEW

WRITE

ROW B

SELECT SEL

*

DISTINCT ALL

C

,

expr aname AS tname.*

, C FROM

tname

D Single Tables

aname AS

joined_table

JOIN joined_table ON search_cond INNER LEFT RIGHT FULL

CROSS JOIN ( subquery )

Joined Tables

OUTER

single_table

derived_tname

Derived Tables

,

AS (

col_name

) E

D ,

WHERE search_cond GROUP BY

HAVING cond

col_name col_pos expr

E ,

WITH CHECK OPTION ORDER BY

;

expr col_name col_pos

ASC DESC

Teradata RDBMS for UNIX SQL Reference

FF07B077

8-105

Teradata SQL Syntax Guide CREATE VIEW

where:

8-106

Syntax Element . . .

Specifies . . .

viewname

the name of the new view. If viewname is not fully qualified, the default database is used.

col_name

the name of a view column. If more than one column is specified, list the col_names in the order in which each column is to be displayed for the view. It is recommended that a view have a maximum of 50 columns (see Usage Notes).

AS

an introduction to the view definition.

LOCKING

the type of lock to be placed on a database, table, view, or row. This setting will override any default usage lock placed on that object by the Teradata RDBMS (see Usage Notes below, and the section on “LOCKING Modifier”).

DATABASE TABLE VIEW

the name of a database, table, or view that is to be locked

ROW

a row or rows to be locked in accordance with the defining statement (see below).

dbname

the name of the database (or user) to be locked.

tname

the name of the table to be locked.

vname

the name of the view to be locked.

FOR IN

the type of lock to be placed.

ACCESS READ SHARE WRITE EXCLUSIVE

the type of lock to be placed. The SHARE keyword may be used as a synonym for READ.

MODE

an optional keyword.

NOWAIT

that if the indicated lock cannot be obtained, the statement should be aborted. Used for situations where it is not desirable to have a statement wait for resources, possibly also tying up resources in the process of waiting.

SELECT

that the expr(s) to be associated with col_name(s) are to be selected from one or more existing tables and/or views.

DISTINCT

that only one row is to be returned from any set of duplicates that might result from a given expr-list. Two rows are considered duplicates only if each value in one is equal to the corresponding value in the other.

ALL

that all rows, including duplicates, are to be returned in the results of the expr-list. This is the default value.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE VIEW Syntax Element . . .

Specifies . . .

expr

an expression. Aggregate and arithmetic operators may be used with expressions.

*

that all columns of all tables referenced in the FROM clause be returned. When qualified by tname, specifies that all columns of only tname are to be returned. View columns are explicitly enumerated when views are defined; therefore, if a table is changed after a view is defined, those changes will not appear if the SELECT * construct is used.

tname

the name of a table or view. The tname.* in the SELECT list can define the table from which rows are to be returned when two or more tables are referenced in the FROM clause.

aname

a different, temporary name (alias) for the table that is referenced by tname. aname must be used during a self-join operation on the table. aname is also used to name expressions.

AS

an introduction to aname.

FROM

the names of one or more tables, views, or derived tables from which expr is to be derived. See the section FROM list for detail.

joined_table

the name of a joined table.

INNER

a join in which qualifying rows from one table are combined with qualifying rows from another table according to some join condition. Types of inner joins include exclusion, merge, nested, product, and row ID. This is the default join type.

OUTER

a join in which qualifying rows from one table that do not have matches in the other table, are included in the join result. The rows from the first table are extended with nulls.

LEFT OUTER

the table that was listed first in the FROM clause. In a LEFT OUTER JOIN, the rows from the left table that are not returned in the result of the inner join of the two tables are returned in the outer join result, and extended with nulls.

RIGHT OUTER

the table that was listed second in the FROM clause. In a RIGHT OUTER JOIN, the rows from the right table that are not returned in the result of the inner join of the two tables are returned in the outer join result and extended with nulls.

Teradata RDBMS for UNIX SQL Reference

8-107

Teradata SQL Syntax Guide CREATE VIEW Syntax Element . . .

Specifies . . .

FULL OUTER

a join that returns rows from both tables. In a FULL OUTER JOIN, rows from both tables that have not been returned in the result of the inner join are returned in the outer join result, and extended with nulls.

JOIN

an introduction to the name of the second table to participate in the join.

joined_table

the name of the joined table.

ON search_condition

one or more conditional expressions that must be satisfied by the result rows. An ON condition clause is required if the FROM clause specifies outer join syntax.

CROSS JOIN

A CROSS JOIN is an unconstrained, or Cartesian join; it returns all rows from all tables specified in the FROM clause.

single_table

the name of table participating in join. Derived Tables

The derived table allows the FROM list to specify a spool file, comprised of selected data from an underlying table(s). The derived table acts like a viewed table. This is an extension of a current functionality. It is a full ANSI SQL feature.

8-108

(subquery)

SELECT statements.

AS

an optional introductory clause to derived table name.

derived_tname

the name of derived table.

col_name

the column name. The col_name field is for the columnname only; forms such as Databasename.Tablename.Columnname, or Tablename.Columnname should not be used.

WHERE

an introduction to the search condition in the SELECT statement.

search_cond

a conditional search expression that must be satisfied by the row(s) returned by the statement.

GROUP BY

an introduction to a reference to one or more expressions in the select expression list.

col_name

specifies the name(s) of columns used in the GROUP BY clause in the SELECT statement.

col_pos

specifies the numeric position of the column(s) used in the GROUP BY clause.

expr

the expression(s) used in the GROUP BY clause.

HAVING

an introduction to the conditional clause in the SELECT statement.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE VIEW Syntax Element . . .

Specifies . . .

cond

one or more conditional expressions that must be satisfied by the result rows. Aggregate operators may be used with HAVING. HAVING cond selects rows from a single group defined in the select expr-list that has only aggregate results, or it selects rows from the group or groups defined in a GROUP BY clause.

ORDER BY

the order in which result rows are to be sorted.

col_name

the name(s) of columns used in the ORDER BY clause in the SELECT statement. These can be ascending or descending.

col_pos

the numeric position of the column(s) used in the ORDER BY clause. This can be ascending or descending.

expr

specifies the numeric position of the expression in the expr-list either by name, or by means of a constant.

ASC DES

The default order is ASC. * ASC specifies ascending order; DESC specifies descending order. * If a sort option is not given, result values are sorted in ascending order according to the client system’s collating sequence. If ORDER BY is not specified, rows are returned unsorted.

WITH CHECK OPTION

Restricts the rows that can be accessed in the table by an INSERT or UPDATE statement. Refer to the section “WITH CHECK OPTION Clause in Views” below.

Privileges Required

8

If a user other than the owner utilizes a view, then all of the relevant privileges needed by the owner of the view to access underlying tables and views must be held by the owner of the view WITH GRANT OPTION. To create a view, the user must have the CREATE VIEW privilege. The creator also receives the DROP VIEW, INSERT, UPDATE, DELETE, SELECT privileges on the newly created view, WITH GRANT OPTION. To update a table via a view, you must have the update privilege on the view, and the view owner must have the update privilege on the table. To delete rows using a view, you must have the delete privilege on the view and the view owner must have the delete privilege on the table. If updating or deleting using a view, and a search condition exists, you must have SELECT ACCESS rights on the view, and the view owner must have SELECT ACCESS on the objects searched.

Teradata RDBMS for UNIX SQL Reference

8-109

Teradata SQL Syntax Guide CREATE VIEW

Limit On Request Size

WHERE and HAVING Clauses

8

Because expansions of source text in view definitions are made to fully qualify object names and to normalize expressions, it is possible for a view to be defined but to be unusable because of stack overflows in the Syntaxer at execution. In this case, the system returns a 3741 error. The WHERE clause specifies the conditional expression by which rows for the view are to be selected.

8

The HAVING clause selects the groups defined by a GROUP BY clause that satisfy a conditional expression. Joins may only be defined in a HAVING clause of a view when the HAVING clause is part of a subquery.

Caution:

Do not use SELECT * if any underlying table or view might be modified. Names are resolved and columns are defined when the view is defined; therefore, changes to the table are not incorporated into the view definition if the SELECT * construct is used.

Rules for Creating and Using Views

8

In order to avoid unexpected or incorrect results when querying a view, the following rules should be considered when creating the view: ●









8-110

Joins may not be used against a view defined with a GROUP BY clause. An operation on a view that references multiple tables causes the tables to be joined. The efficiency of such an operation depends on how it uses the view (for example, whether it makes use of indexes defined for the tables). A view may be used as a modifier; that is, to insert or update data in the underlying table. A modifier view may reference only one table, and no data descriptions or aggregates may be used in the expressions that reference the table columns. Any user with the right to use a view has the right to override a lock specified for a view. Users of the Performance Monitor/Application Programming Interface who have the MONITOR SESSION privilege can view all locks in the system, and can determine which other users’ locks are blocking other users. If tables referenced by a view are modified to add or remove columns, or change data types of existing columns, then attempts to access the view may result in error messages or unexpected results. This is true whether the view references table columns by name or by SELECT *.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE VIEW

WITH CHECK OPTION Clause in Views

WITH CHECK OPTION pertains to views that can be used to INSERT or UPDATE values in an underlying base table. 8

WITH CHECK OPTION may be used when creating updatable views. Typically a view is created to restrict which columns and which rows the user can access in a table. Row restriction is accomplished by using a WHERE clause when the view is created. The WITH CHECK OPTION is used to restrict which rows in the table can be affected by an INSERT or UPDATE statement The WITH CHECK OPTION clause is inserted at the end of the statement defining the view. WHEN WITH CHECK OPTION is . . .

Example

8

Any INSERT or UPDATE made to the table through the view . . .

used

only creates or modifies rows that satisfy the WHERE clause.

not used

ignores the WHERE clause used in defining the view.

A query specification could state "WHERE column_1 > 10" which restricts the rows that can be seen through the view. The WITH CHECK OPTION clause also restricts the rows that can be inserted by an INSERT or restricts the resulting row of an UPDATE statement.

Updatable Views and WITH CHECK OPTION

The following rules apply to updatable views and WITH CHECK OPTION. 8







If WITH CHECK OPTION is specified, the view should be updatable. In addition, any insert or update to the table through the view will not result in the creation of a row for which the where clause is false. If WITH CHECK OPTION is not included in an updatable view, then any where clause contained in the query defining the view is ignored for any insert or update action through the view. Nested Views: A user could define nested views which only reference a single base table, which may allow the views to be updatable. In this case, the presence of a WITH CHECK OPTION in a view definition causes the WHERE clause for that view, and WHERE clauses of underlying views, to be used in the constraint on INSERT or UPDATE.

Refer to Example 1 and 7, in the ”Examples” section, below.

Teradata RDBMS for UNIX SQL Reference

8-111

Teradata SQL Syntax Guide CREATE VIEW

When defining a view that is not a modifier, one or more aggregate operators (SUM, AVG, MAX, MIN, and COUNT) may be used in the SELECT expr-list or in the conditional expression of a HAVING clause. They may not be used in the conditional expression of a WHERE clause.

Operators

Query expressions (set operators), UNION, INTERSECT, and EXCEPT (MINUS), can not be used to define a view.

SELECT expr Can Include Data Definitions

8

When defining a view that is not a modifier, any expression in the SELECT expr-list may include a data definition (Data Type, FORMAT, TITLE, etc.). Data definitions determine the form of the view display. For example, a TITLE phrase in the CREATE statement of the underlying table or view will not be displayed unless the TITLE phrase is also included in the CREATE VIEW statement. The data definition for a view column may differ from that defined for the associated column of the underlying table or view. However, not all data definitions are relevant to view expressions. For example, range constraints and DEFAULT declarations may not be used in the expression list. The WHERE clause, or the HAVING clause if GROUP BY is specified, should be used to define range constraints in a view.

Locks

When a view is defined with a LOCKING modifier, the specified lock is placed on the underlying table(s) each time the view is referenced in a SQL statement.

Overriding LOCKING FOR READ in a View

A LOCKING modifier which specifies locking for ACCESS is may be used in a CREATE VIEW statement to give ad hoc users and users who will modify the data, concurrent access. See also the “LOCKING Modifier”, later in this chapter.

8

A READ lock in a view can be overridden by placing a “LOCKING FOR ACCESS” modifier on the view. For example, assume that an ad hoc user selects data at the same time a user modifying the data, attempts to modify the data. The READ lock placed by the ad hoc user will prevent the user modifying the data, from accessing the data, because update transactions require a WRITE lock. By creating a view for the ad hoc user that specifies an ACCESS lock, the ad hoc user can not prevent the user modifying the data from completing table modifications.

8-112

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE VIEW

An ACCESS lock allows data to be retrieved during write activities; be aware that a view defined with an ACCESS lock may display inconsistent results. The following examples illustrate the use of CREATE VIEW:

Examples Example 1

8

The following statement creates a view of the Employee table so that it provides access only to the names and job titles of the employees in Department 300: CREATE VIEW Dept300 (Name, JobTitle) AS SELECT Name, JobTitle FROM Employee WHERE DeptNo = 300 WITH CHECK OPTION;

The WITH CHECK OPTION prevents using this view to INSERT a row into Employee through the view, or to update any row of Employee for which DeptNo 300.

Example 2

8

The following statement creates a view of the Department table. Each column in the view is defined with a title. Therefore, the view data is displayed with column titles that differ from those defined for the Department table. CREATE VIEW DEPT (DeptNo, DeptName, Loc, MgrNo) AS SELECT DeptNo(TITLE ’Department Number’), DeptName(TITLE ’Department Name’), Loc (TITLE ’Department Location’), MgrNo(TITLE ’Manager Number’) FROM Department ;

Example 3

8

The following statement creates a view that allows a personnel executive to keep track of employees who have more experience on the job than their supervisors. This statement defines a self-join of the Employee table. The temporary names “Workers” and “Managers”, created in the FROM clause, refer to the two temporary tables participating in the self-join. CREATE VIEW EmpInfo (WorkerName,WorkerYrsExp,Department, ManagerName,ManagerYrsExp) AS SELECT Workers.Name, Workers.YrsExp, Workers.DeptNo, Managers.Name, Managers.YrsExp FROM Employee Workers, Employee Managers WHERE Workers.DeptNo = Managers.DeptNo AND Managers.JobTitle IN (’Manager’, ’Vice Pres’) AND Workers.YrsExp > Managers.YrsExp ;

Example 4

8

The following statement illustrates the use of aggregates in a view definition. The result rows are grouped by department number and include only those rows with an average salary of $35,000 or higher. CREATE VIEW DeptSal (DeptNo, MinSal, MaxSal, AvgSal) AS SELECT DeptNo, MIN(Salary), MAX(Salary), AVG(Salary) FROM Employee GROUP BY DeptNo HAVING AVG(Salary) >= 35000 ;

Teradata RDBMS for UNIX SQL Reference

8-113

Teradata SQL Syntax Guide CREATE VIEW SELECT * FROM DeptSal;

returns: DeptNo -----600 300 700 500

Example 5

MinSal ---------28,600.00 23,000.00 30,000.00 22,000.00

MaxSal --------45,000.00 65,000.00 45,000.00 56,000.00

AvgSal ---------36,650.00 47,666.67 37,666.67 38,285.71

The following request: 8

SELECT DeptNo, MinSal, MinSal+10000, AvgSal from DeptSal WHERE AvgSal > (MinSal + 10000) ;

returns: DeptNo -----500 300

Example 6

8

MinSal ---------22,000.00 23,000.00

(MinSal+10000) ----------------32000.00 33000.00

AvgSal ---------38,285.71 47,666.67

The next example shows how use of a view name in an UPDATE, INSERT, or DELETE statement allows you to add, change, or remove data in the table (or tables) upon which the view is based. For example, updating data via a view changes data in the underlying table. Inserting or deleting rows via a view adds or removes rows from the underlying table. Consider the following Staff_Info view, which gives a personnel clerk retrieval access to employee numbers, names, job titles, department numbers, sex, and dates of birth for all employees except vice presidents and managers: CREATE VIEW staff_info (number, name, position, department, sex, dob) AS SELECT employee.empno, name, jobtitle, deptno, sex, dob WHERE jobtitle NOT IN (’Vice Pres’, ’Manager’)WITH CHECK OPTION ;

If the owner of Staff_Info has the insert privilege on the Employee table, and if the clerk has the insert privilege on Staff_Info, then the clerk may use this view to add new rows to Employee. For example, entering the statement: INSERT INTO staff_info (number, name, position, department, sex, dob) VALUES (10024, ’Crowell N’, ’Secretary’, 200, ’F’, ’Jun 03 1960’) ;

8-114

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide CREATE VIEW

inserts a row in the underlying Employee table that contains the specified information. The constraint on Staff_Info illustrated by the following WHERE clause applies to any insert using this view that includes the WITH CHECK OPTION phrase. WHERE jobtitle NOT IN (’Vice Pres’, ’Manager’)

Therefore, the preceding INSERT statement would fail if the Position entered for Crowell was Vice Pres or Manager. If this view were defined to not include the WITH CHECK OPTION, and a user had UPDATE privilege, that user could update a job title to ‘Vice Pres’ or ‘Manager’. The user would be unable to access the changed row through the view. Entering the following statement changes the department number (from 200 to 300) entered for Crowell in the preceding INSERT statement. UPDATE staff_info SET department = 300 WHERE number = 10024 ;

Entering the following statement removes the entire row for Crowell from Employee. DELETE FROM staff_info WHERE number = 10024 ;

A view is a useful medium for allowing other users access to table data. However, as the preceding examples suggest, granting another user insert, update, and delete privileges on a view means relinquishing some control over your data. Carefully consider granting such privileges. Caution:

In prior releases, all updatable views were created as though they included WITH CHECK OPTION. Starting with V2R2.0, the default is not to constrain updated or inserted values unless the view definition explicitly includes WITH CHECK OPTION.

Teradata RDBMS for UNIX SQL Reference

8-115

Teradata SQL Syntax Guide DATABASE

DATABASE

8

The DATABASE statement establishes a new default database for the current session for all SQL statements that are entered without fully qualified table, view, or macro names. When sessions are logged on an initial default database is set during the logon. Normally, this is the database of the user.

Function

The DATABASE statement is the only way to modify the default database (or user). This default database is used until the end of the session, or until a subsequent DATABASE statement is entered.The DATABASE statement can be included in the startup string for logging on. See also “CREATE USER”. DATABASE is flagged as non-ANSI when the SQL flagger is enabled. where:

Syntax

DATABASE

dbname ; FF07A016

Syntax Element . . .

dbname

2PC Mode Resolving Unqualified References

Example

8

8

Specifies . . .

the name of the default database.

In 2PC mode, since DATABASE is treated as a DDL statement, and as such, it cannot be used. The default database of the executing user, not the creating user, is used to resolve unqualified references to database objects in a macro data definition statement (DDL). It is therefore recommended that object references in a macro data definition statement be fully qualified in the macro body (see “CREATE MACRO” statement). The following statement establishes Personnel as the default database for the current session: DATABASE Personnel;

8-116

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DELETE

DELETE DELETE removes rows from a table. There are two main forms of DELETE:

Function Basic DELETE

DELETE With Implied Join Condition

8

8

8

This form of DELETE is ANSI-compliant and should be used for all delete’s referencing a single table, or referencing tables other than the one deleted from only by subqueries in the condition. This form of DELETE is used if the condition directly references fields in tables other than the one that rows are to be deleted from, that is, if the condition included a Subquery or references a Derived Table. This form is flagged as non-ANSI when the SQL flagger is enabled. It is a Teradata extension to ANSI SQL.

Syntax

DELETE—Basic Form: tname

DELETE

aname

FROM AS

WHERE condition

;

ALL FF07B072

DELETE with implied join: DELETE

del_tname

A FROM ,

A

tname

B

aname AS B WHERE

condition ALL

Teradata RDBMS for UNIX SQL Reference

; FF07R078

8-117

Teradata SQL Syntax Guide DELETE

where: Syntax Element. . . .

Specifies . . .

tname

the table the DELETE is to remove rows from. Used when the DELETE includes a condition that directly references more than one table.

FROM tname

the name of the table to delete rows from (Basic DELETE). This phrase is optional for the Basic DELETE statement.

AS

an optional keyword introducing the alias name of the table to be deleted from.

aname

an optional alias name, used for self-joins.

del_tname

the name of the table to be deleted from.

FROM list

the list of tables and views referenced in the condition. Elements of the list are tnames with optional alias anames (derived table DELETE).

WHERE

n introduction to a conditional clause. See also Chapter 7, “Queries: The SELECT Statement,” “WHERE Clause”.

condition

the conditional expression to be used for determining rows to be deleted. The expression operands can be constants, or references to fields in the specified table or other tables. The conditional also may specify an embedded SELECT operation.

ALL

that all rows to be deleted, resulting in an empty table. This is the default and is used when a WHERE condition is not specified. The ALL option is flagged as non ANSI, when the SQL flagger is enabled; it is a Teradata extension to ANSI SQL.

Privileges Required

8

To delete rows from a table, the user must have the DELETE privilege on the table. If the DELETE statement is a ‘searched’ DELETE, i.e., it includes the WHERE condition, the user must also have SELECT privilege on all tables accessed for checking the condition. Use caution when granting the privilege to delete data through a view. Data in fields that might not be known to the user, is also deleted when a row is deleted through a view.

Locks

8-118

A DELETE operation sets a Write lock for the table or row. 8

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DELETE

Rules for Using DELETE

The following rules apply to using DELETE. 8



● ●

‘Tablename’ (tname) has been added before the FROM clause, to specify the deleted table name if the syntax for DELETE with an implicit join is used. All alias names must be defined in the FROM clause. The activity count in the success response for a DELETE statement reflects the total number of rows deleted.

The abbreviation for the DELETE statement is DEL.

Unconstrained DELETE Processing Time (‘Fast Path’)

When submitted by itself as a transaction, an unconstrained delete (DELETE...ALL) is processed very quickly. 8

An unconstrained DELETE is one where no conditions are attached, i.e, all rows are deleted. When an unconstrained delete is submitted as part of a multistatement transaction, the speed with which it is processed depends on its position in the transaction. To enable “fast path” processing, which defers processing until end transaction time and allows rollback processing to be almost instantaneous, positioning must be as follows, depending on the mode being used.

Teradata RDBMS for UNIX SQL Reference

8-119

Teradata SQL Syntax Guide DELETE

IN this mode . . .

FOR this type of transaction . . .

Positioning must be as follows . . .

ANSI

All

The unconditional DELETE must be immediately followed by a COMMIT. For example, DELETE FROM tname ALL;COMMIT;

Teradata

Implicit (such as a multistatement macro or BTEQ request) Explicit

DELETE...ALL must be the last statement in the request.

DELETE...ALL must be immediately followed by the END TRANSACTION statement that terminates the currently open transaction. This implementation is not available to a Teradata Preprocessor2 application, because the DELETE and END TRANSACTION statements must fall into the same request packet.

Constrained DELETE Processing Time (‘Slow Path’)

Processing time for a constrained delete (DELETE...WHERE) may be longer when: ● 8



The FALLBACK option is specified for the table, because the rows in the secondary copy of the table must also be deleted. Non-unique secondary indexes are defined for the table, because the secondary index subtable(s) also must be updated to account for the rows deleted.

Processing time for a constrained delete may be shortened by specifying a primary index value or a unique secondary index value as the conditional expression of the WHERE clause.

8-120

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DELETE

Deleting Rows Using a View

To delete rows using a view through which the table is accessed: ● 8



● ●



Subqueries in DELETE Statements

The user must have the DELETE privilege on the view. Also, the immediate owner of the view (that is, the database in which the view resides) must have the DELETE privilege on the underlying object (view or base table) whose rows are to be deleted, and the SELECT privilege on all tables that are specified in the WHERE clause. Each column of the view must correspond to a column in the underlying table (that is, none of the columns in the view may be derived using an expression). No two view columns may reference the same table column. The view cannot include a column that contains a range constraint. The expression used to define a view cannot have a data type specified for any column in the view.

The condition of a DELETE statement may include subqueries, referencing the delete table and/or other tables. 8

The implementation of Correlated Subqueries has changed the meaning of some DELETE statements with subqueries. Example: DELETE FROM Publisher WHERE 0 = (SELECT COUNT(*) FROM book WHERE book.PubNum=Publisher.PubNum);

There are two publishers who have books in the library, and two publishers who do not. In previous releases, the subquery was implemented once, comparing all rows of book with all rows of publisher, and in this instance, finding a count of 2. That count was then compared with 0, for each row of publisher, and the condition was always false. With the implementation of Correlated Subqueries, the subquery is executed once for each row of the outer reference, Publisher, and since two publishers have no books in the library, two rows of publisher are deleted. To modify this DELETE to perform as it did in previous releases, change the subquery to include all tables it references in the FROM clause of the subquery. DELETE FROM Publisher WHERE 0 = (SELECT COUNT(*) FROM book, publisher WHERE book.PubNum=Publisher.pubnum);

Now, the condition in the subquery has a local defining reference and the statement does not contain a correlated subquery. The count, determined once, is non-zero and no rows are deleted.

Teradata RDBMS for UNIX SQL Reference

8-121

Teradata SQL Syntax Guide DELETE

The section on Correlated Subqueries in Chapter 9 describes the ruled to specify statements including subqueries that will not provide different responses on this and future releases than were provided by prior releases. The following examples illustrate the use of DELETE:

Examples Example 1

8

To delete rows from the Employee table for employees in Department 500: DELETE FROM Employee WHERE DeptNo=500;

Example 2 Example 3 Example 4

To delete all rows from Employee: 8

DELETE FROM Employee ALL;

To delete the row for employee 10011 from Employee: 8

DELETE FROM Employee WHERE EmpNo=10011;

8

To delete from Employee rows for employees who work in NYC (this operation joins Employee and Department tables): DELETE FROM Employee WHERE Employee.DeptNo = Department.DeptNo AND Department.Location = ’NYC’;

Example 5

8

To delete from Employee rows for managers who have less work experience than their employees (this operation performs a self-join on the Employee table): DELETE FROM Employee AS Managers WHERE Managers.DeptNo = Employee.DeptNo AND Managers.JobTitle IN (’Manager’, ’Vice Pres’) AND Employee.YrsExp > Managers.YrsExp;

8-122

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DELETE

Example 6

8

In the following macro, the SELECT and DELETE statements are structured as a single multistatement request: CREATE MACRO ResUse ( FromDate (DATE, DEFAULT DATE), ToDate (DATE, DEFAULT DATE), FromTime (INTEGER, DEFAULT 0), ToTime (INTEGER, DEFAULT 999999), Proc (VARCHAR(4), DEFAULT ’P’), Secs (SMALLINT, DEFAULT 600) ) AS ( SELECT TheDate (TITLE ’Resource//TheDate’), TheTime (TITLE ’Utilized//TheTime’), Proc, AVERAGE(Hits) (TITLE ’Avg//Hits’), MAXIMUM(Cpu) (TITLE ’Max//CPU’), AVERAGE(Cpu) (TITLE ’Avg//CPU’), MAXIMUM(Disk) (TITLE ’Max//Disk’), AVERAGE(Disk) (TITLE ’Avg//Disk’), MAXIMUM(Host) (TITLE ’Max//Host’), AVERAGE(Host) (TITLE ’Avg//Host’), MAXIMUM(Chan) (TITLE ’Max//Chan’), AVERAGE(Chan) (TITLE ’Avg//Chan’) FROM DBC.ResUseView GROUP BY TheDate, TheTime, Proc WHERE TheDate BETWEEN :FromDate AND :ToDate AND TheTime BETWEEN :FromTime AND :ToTime AND Proc CONTAINS :Proc AND Secs EQ :Secs ORDER BY Proc, TheDate, TheTime; DELETE FROM ResUseView ALL ; ) ;

In Teradata mode, if the preceding macro was executed, and not within a user-generated transaction, the DELETE statement would be processed using the “fast path” method. In ANSI mode, the “fast path” method is used (no WHERE clause) when a COMMIT is included in the macro or COMMIT is used at the end of the request line, and immediately following the execution of the macro, within the request. The last line of the example above would read: DELETE FROM ResUseView ALL ; COMMIT;) ;

The “slow path” method is used when no COMMIT statement is stated in the macro, or the request that executes the macro.

Teradata RDBMS for UNIX SQL Reference

8-123

Teradata SQL Syntax Guide DELETE

Example 7

8

The DELETE...ALL statement in the following BTEQ request also invokes “fast path” processing. Note that the statements are structured as a single multiple-statement request, and therefore, are processed as an implicit transaction. Note that DELETE is the last statement in the series. SELECT * FROM DBC.LogOnOff WHERE LogDate = DATE AND UserName = ’Administrator’ ; SELECT LogTime, UserName, Event, AccountName FROM DBC.LogOnOff WHERE LogDate = DATE AND UserName NOT IN (’Administrator’, ’SecAdmin’, ’Oper’) ; SELECT LogTime, Event, LogicalHostId, IFPNo FROM DBC.LogOnOff WHERE LogDate = DATE AND UserName = ’Oper’ ; DELETE FROM DBC.LogOnOff ALL ;

In ANSI mode, the COMMIT statement must follow the DELETE statement. The last line of the above example would read: UserName = ’Oper’ ; DELETE FROM DBC.LogOnOff ALL ; COMMIT;

8-124

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DELETE DATABASE, DELETE USER

DELETE DATABASE, DELETE USER 8

The DELETE DATABASE or DELETE USER statements delete all data tables, views, and macros from a database or user. The database or user remains on the Teradata RDBMS as a named space of a particular size.

Function

DELETE DATABASE, DELETE USER are flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

DELETE DEL

DATABASE

name ;

USER

FF07R018

where:

Specifies . . .

name

the name of the database or user from which all tables, views, and macros are to be deleted.

When a DELETE DATABASE, DELETE USER statement is executed, an EXCLUSIVE lock is placed on the database or user from which objects are being deleted.

Locks

Privileges Required

Syntax Element . . .

8

To delete objects from a database or user space via DELETE DATABASE or DELETE USER, the user must have the DROP privilege on the referenced database/user. DELETE USER can delete a database, and DELETE DATABASE can delete a user.

Access Rights Are Removed With DELETE

8

The DELETE DATABASE/USER statement also removes all access rights to the objects that were deleted. Because the Dump and Restore utility does not automatically recover these access rights, they must be reestablished following the restore operation.

Teradata RDBMS for UNIX SQL Reference

8-125

Teradata SQL Syntax Guide DELETE DATABASE, DELETE USER

DELETE DATABASE/ USER Does Not Delete Journal Tables After DELETE Structure Remains Intact

Example

The DELETE DATABASE/USER statement will not delete a journal table. The user must enter a MODIFY DATABASE/MODIFY USER statement to remove a journal table. 8

8

After all the data tables, views, and macros have been deleted from a database/user, the structure remains intact (as though its CREATE statement had just been executed) and can be used in a subsequent restore operation. The DELETE DATABASE/USER statement is used when a database or user has been only partially restored and is not in a usable state. The following statement can be used to delete all tables, views, and macros from the UsedCars database: DELETE DATABASE UsedCars;

8-126

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DROP DATABASE, DROP USER

DROP DATABASE, DROP USER

8

The DROP DATABASE or DROP USER statements can drop empty databases or users only; all objects associated with that database or user must have already been dropped.

Function

When a database or user is dropped, the disk space is freed. DROP DATABASE, DROP USER are flagged as not ANSI, when the SQL flagger is enabled.

Syntax

DROP

DATABASE

name

USER

;

FF07A019

where: Syntax Element...

Specifies . . .

name

the name of the database or user that is to be dropped.

To drop a database or user, a user must have the DROP DATABASE or DROP USER privilege on the database or user. The database or user that is being dropped cannot own other databases or users, and must be at the bottom of the database hierarchy.

Privileges

The object type is not checked; DROP DATABASE and DROP USER have the same effect.

Journal Tables

DROP Functions

8

If a database or user contains a journal table in their disk space, then the database/user cannot be dropped until the journal table is removed from the system. A MODIFY DATABASE /USER statement must be used to drop the journal table. The drop operation performs the following functions:

8

● ●

Sets an exclusive lock on the database or user. Adds the disk space that the drop makes available to that of the immediate owner database.

After a database is dropped, it cannot be recovered by using the Dump and Restore utility unless the database is restored.

Teradata RDBMS for UNIX SQL Reference

8-127

Teradata SQL Syntax Guide DROP DATABASE, DROP USER

Example

8-128

The following statement can be used to drop user Jones: DROP USER Jones;

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DROP INDEX

DROP INDEX

8

The DROP INDEX statement deletes a secondary index on a table.

Function

DROP INDEX is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

, DROP INDEX

(

col_name

)

ON

tname

name FF07A053

where: Specifies the name of the. . .

col_name

columns that constitute the index.

name

index to be dropped.

tname

table on which the secondary index is to be dropped.

To drop an index on a table, the user must have the P INDEX or DROP TABLE privilege on the table.

Privileges Why You Might Drop an Index

Syntax Element...

A secondary index is normally dropped after usage patterns for a table have changed and the index is no longer used. 8

Another reason to DROP a UNIQUE SECONDARY INDEX is so that a MultiLoad can be performed on the table; after the MultiLoad completes, the index is recreated. How a DROP Functions The drop operation performs the following functions. Stage

Process

1

Sets an exclusive lock on the table that contains the index.

2

Frees the disk space that was used by the index subtable.

A named index is dropped either by specifying its name or its definition (but not both).

Teradata RDBMS for UNIX SQL Reference

8-129

Teradata SQL Syntax Guide DROP INDEX

UNIQUE Constraints

8

The following examples illustrate the use of DROP INDEX:

Examples Example 1

If an index has been created in the form of an unnamed UNIQUE constraint, it may only be removed by a DROP INDEX statement referencing the column or columns defined for the constraint.

8

The following statement drops a secondary index that was defined on the Name column of the Employee table: DROP INDEX (Name) ON Employee;

Example 2: Dropping Named Indexes

Example 3

This statement drops the existing named index index_1 from table table_5. 8

8

DROP INDEX index_1 ON table_5 ;

This statement drops the existing index on columns field_1 and field_2 from table table_6. DROP INDEX (field_1, field_2) ON table_6 ;

Example 4: Non-Valid

8

The following example demonstrates a non-valid use of dropping an index. DROP INDEX index_1 (field_1, field_2) ON table_8 ;

8-130

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DROP MACRO, TABLE, or VIEW

DROP MACRO, TABLE, or VIEW

8

The DROP MACRO, TABLE, or VIEW statement removes a table and all of its rows, or a view definition, or a macro definition from a database.

Function

DROP MACRO, TABLE or VIEW are flagged as non-ANSI, when the SQL flagger is enabled. MACRO’s are flagged as non-ANSI.

Syntax

DROP

MACRO

name ;

TABLE VIEW

FF07A020

where: Specifies . . .

tname

the name of the table, view, or macro to be dropped.

To use the DROP MACRO, TABLE, or VIEW statement, a user must have the DROP privilege on the referenced table, view, or macro.

Privileges DROP Functions

Syntax Element...

The drop operation performs the following functions. 8

Stage

Referenced Objects

8

Process

1

Sets an exclusive lock on the table, view, or macro.

2

Frees the disk space used by the dropped table, any secondary index subtables, and any fallback copy for use by the owner database.

3

Removes any explicit access rights on the object.

If a Table or View that is referenced by existing Views or Macros is dropped, the Views or Macros continue to exist after the object has been dropped unless they are also explicitly dropped. Attempts to use them fail. If a Table or View of the same name is then created, these Views and Macros operate and give correct results only if the new Table or

Teradata RDBMS for UNIX SQL Reference

8-131

Teradata SQL Syntax Guide DROP MACRO, TABLE, or VIEW

View has the same structure as that dropped. Otherwise, they may fail or provide unexpected results.

Example

8

If a table named Profile was created for temporary use, it could be dropped with the statement: DROP TABLE Profile;

8-132

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DROP STATISTICS

DROP STATISTICS

8

The DROP STATISTICS statement drops the statistical data that was created for one or more columns of a table by a COLLECT STATISTICS statement.

Function

DROP STATISTICS is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

tname

DROP STATISTICS ON

COLUMN

col_name

;

, INDEX ( col_name )

name

FF07A021

where: Syntax Element...

Specifies . . .

tname

the name of the table on which statistics are to be dropped.

COLUMN

that statistics are to be dropped for a single column.

col_name

the name of the column for which statistics are to be dropped.

INDEX

that the statistics to be dropped are for one or more columns that comprise an index on the table.

col_name

names of the columns that constitute the index.

name

the name of the index to be dropped.

Usage Notes

To use the DROP STATISTICS statement for a table, the user must have the DROP TABLE privilege for the table.

DROP STATISTTICS is DDL

DROP STATISTICS is a DDL statement. It must be entered as a single statement request, as the only statement in a macro, or as the last or only statement in a transaction enclosed by BEGIN TRANSACTION and END TRANSACTION statements.

Statistics

8

8

The efficacy of statistics that are collected using COLLECT STATISTICS varies with the types of access used on a table.

Teradata RDBMS for UNIX SQL Reference

8-133

Teradata SQL Syntax Guide DROP STATISTICS

When to Use DROP (and COLLECT) STATISTICS

8

If performance does not seem to be improved by the statistics collected, or if the statistics are suspected of being obsolete, and, therefore, misleading, the DROP STATISTICS statement may be used to remove the statistics. Alternatively, another COLLECT STATISTICS statement may be entered to collect new statistics. If a table has been extensively modified since statistics were last collected for the table, the outdated statistics may cause poor access optimization. Therefore, statistics should be periodically refreshed or dropped. Once a DROP STATISTICS statement is entered, a COLLECT STATISTICS statement that specifies the COLUMN or INDEX option is required if the user wants to regather statistics for that column or index. The COLUMN or INDEX option must be specified because a COLLECT STATISTICS statement that only lists a table name compiles any statistics that were previously collected for a table.

Single Column Index = INDEX (col_name)

8

If an index has a single column, col_name, dropping statistics for COLUMN col_name is the same as dropping statistics for INDEX (col_name). If neither COLUMN nor INDEX is specified, the effect of the statement is to drop any statistics that exist for the table. The following are rules for using named indexes with the COLLECT and DROP STATISTICS statements: ●



The following examples illustrate the use of DROP STATISTICS:

Examples Example 1

You can specify an index name in the DROP STATISTICS statement. To COLLECT or DROP STATISTICS from an unnamed index, you must specify the complete index definition.

8

The following statement drops statistics on an index defined on the EmpNo and Name columns of the Employee table: DROP STATISTICS Employee INDEX(EmpNo,Name);

Example 2

8

The following statement drops any statistics that exist for the Employee tablet. DROP STATISTICS Employee;

8-134

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide DROP STATISTICS

Example 3: Named Index

Example 4: Defined Index

Example 5: Non-Valid

The following example drops any statistics on a named index. 8

DROP STATISTICS ON table_1 INDEX unique_1 ;

The following example drops any statistics on a defined index. 8

DROP STATISTICS ON table_1 INDEX (field_1, field_2) ;

8

This statement is not valid because the index named does not exist for the table. DROP STATISTICS ON table_1 INDEX no_such_index ;

The system returns the error message: The specified constraint name does not exist in table.

Example 6: Non-Valid

8

This statement is not valid because you cannot have both an index name and an index definition in the same DROP STATISTICS statement. DROP STATISTICS ON table_1 INDEX unique_1 (field_1, field_2) ;

The system returns the error message: Only index name or index column(s) allowed in COLLECT/DROP STATISTICS.

Teradata RDBMS for UNIX SQL Reference

8-135

Teradata SQL Syntax Guide ECHO

ECHO Function

8

The ECHO statement returns a fixed character string to the requestor. ECHO is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

'

ECHO '

string

;

'

command

' ;

FF07R022

where:

ECHO and BTEQ

Syntax Element...

Specifies . . .

‘string’

the text to be returned to the requestor.

‘command’

a BTEQ command to be returned to BTEQ. The command must be terminated with a semicolon.

The ECHO statement is used primarily in macros to execute most BTEQ commands during macro execution. The ECHO statement cannot be used with the following BTEQ commands: EXPORT, IMPORT, LOGOFF, LOGON, QUIT, REPEAT, RUN, =

When the Teradata RDBMS encounters an ECHO statement, an ECHO parcel containing ’string’ is sent to the client system. When returned to BTEQ, it is interpreted by BTEQ. If ’command’ is not a BTEQ format command, BTEQ logs the illegal command as an error.

Example

The following ECHO statement could be used in a macro that creates a report through BTEQ. When the Teradata RDBMS encounters the statement during macro execution, the width of a printed page is set to 72 characters. ...ECHO ’SET WIDTH 72;’...

8-136

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide EXECUTE

EXECUTE Function

8

The EXECUTE statement executes a macro that was defined using the CREATE MACRO statement. A macro is a stored set of statements that is executed as an entity before any result is returned to the user. EXECUTE is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

EXECUTE EXEC

macroname

,

;

( param_name = const_expr , (

const_expr

)

) FF07A023

where: Syntax Element...

Specifies . . .

macroname

the name of the macro that is to be executed.

Teradata RDBMS for UNIX SQL Reference

8-137

Teradata SQL Syntax Guide EXECUTE Syntax Element...

Specifies . . .

const_expr

a constant or an expression involving constants that specifies a parameter value. Values are listed in left-to-right order according to the order in which parameter names were established in the CREATE MACRO statement. Values can be null e.g., (a, , b) This is referred to as a positional parameter list. If a value is not supplied for a parameter, a comma must be entered to account for the missing parameter. Any default value defined for that parameter is inserted automatically.

param_name = const_expr

a parameter name (param_name) as defined in the CREATE MACRO statement, and supplies the value for that parameter. This is referred to as a named parameter list. The value may be a constant, or an expression involving constants. If a parameter is omitted, any default value defined for that parameter is inserted automatically.

Privileges

Rules for Executing Macros

8

8

To execute a macro, the user must have EXECUTE privilege on the macro. The creator or any owner of the macro may grant the EXECUTE privilege to another. In addition, the immediate owner of the macro (the database in which the macro resides) must have the necessary privileges on objects named in the statement(s) that are contained in the macro. A data definition statement in a macro is not fully resolved until the macro is submitted for execution. Unqualified references to database objects are then resolved using the default database of the executing user. Therefore, object references in data definition statements in macros should be fully qualified (e.g., databasename.tablename) in the macro body. ● ●

8-138

The number of commas must match the macro definition. Any value in the EXECUTE parameter list may be a constant or an expression involving constants. In this context, DATE, TIME, and USER are considered constants.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide EXECUTE ●





If an error message is returned when a macro is executed, it may be caused by a SQL statement in the macro. The number of parameters used in the calling sequence must be equal to the number of parameters defined. When, for example, two parameters are defined and used, if they are both null, the following statements are all valid, unless defaults have been used in the macro definition: execute macro ’(, 1); execute macro (,); execute macro (null, null);

Macros, NULL Inserts, and Access Logging

8

The following examples illustrate the use of EXECUTE:

Examples Example 1

When a macro attempts to update a row containing a null in a column defined as NO NULLS for that column, the EXECUTE MACRO statement that made the attempt is not logged in the access log even though the logging frequency is set to EACH and ALL objects are specified.

8

The following statement uses a named parameter list to execute macro NewEmp1 (refer to “CREATE MACRO” statement). Note that named parameters can be listed in any order. EXEC NewEmp1 (number=10015, dept=500, name=’Omura H’, sex=’M’, position=’Programmer’);

The row for new employee Omura is inserted in the Employee table.

Example 2

8

This example uses a positional parameter list to execute macro NewEmp2 (refer to “CREATE MACRO” statement). Note that a value is not specified for the dept parameter, which has a macro-defined default value. A comma is entered in the dept position to maintain the integrity of the positional sequence. EXEC NewEmp2 (10021, ’Smith T’, , ’Manager’, ’F’, ’May 8, 1959’, 16);

Example 3

8

When the following statement is processed, the default value for the dept parameter (900) is inserted automatically. The row for new employee Smith is added to the Employee table, and then the Department table is updated by incrementing the value for Department 900 in the EmpCount column. The statement uses a named parameter list to execute a macro named NewHire. Note that the value of the DOH (Date of Hire) column is an expression involving the DATE constant. EXEC NewHire (FlName=’Toby Smith’, Title=’Programmer’, DOH=DATE -1);

Teradata RDBMS for UNIX SQL Reference

8-139

Teradata SQL Syntax Guide EXPLAIN Modifier

EXPLAIN Modifier

8

The EXPLAIN modifier returns a summary of the steps the Teradata RDBMS would perform to resolve a request. The request itself is not entered for processing.

Function

The EXPLAIN modifier is flagged as non-ANSI, when the SQL flagger is enabled. EXPLAIN details what indexes, if any, would be used to process the request, identifies any intermediate spool files that would be generated, shows whether the statements in a transaction would be dispatched in parallel, and so on. EXPLAIN aids users in evaluating a complex query and in developing an alternative processing strategy that may be more efficient.

Syntax

EXPLAIN

request ; FF07A024

Secondary Indexes

References to secondary indexes are based on the following: 8

● ●





Bit Mapping

8

Each secondary index is stored as a subtable. Each row in a secondary index subtable is made up of an index value and one or more row IDs. The subtable row IDs identify the data rows that contain the index value. The subtable for a unique secondary index is a hashed table.

References to bit mapping may appear when complex conditional expressions involving non-unique secondary indexes are applied to a very large table. Such expressions may be resolved by mapping each subtable row ID to a number in the range 0-32767. This number is used as an index into a bit map, in which the bit for each qualifying data row is turned on. The Teradata RDBMS is better able to determine whether the table is a candidate for bit mapping when statistics exist for the table; see also the COLLECT STATISTICS statement.

8-140

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide EXPLAIN Modifier

Only the first 255 characters of a conditional expression are displayed in an EXPLAIN. The entire conditional expression is enclosed in quotation marks. Although the times shown in the EXPLAIN output are indicated as seconds, they are actually arbitrary units of time measure; their value is that they allow you to compare alternate methods of operation. Make the results of EXPLAINs an integral part of your review process; they may indicate inefficiencies in the query structure. Keep the EXPLAIN results with your system documentation; they may be of value when reevaluating the system.

2PC Mode

8

In 2PC mode, if EXPLAIN is used with a request, and if that request is followed by a 2PC function, the SQL portion of the request is explained, but the 2PC function is not. For example, if the following request was submitted: EXPLAIN INSERT tab1(1,2);SELECT * FROM tab1; VOTE

the INSERT and SELECT would be explained; the VOTE would not be explained. The VOTE is, however, sent to the AMPs. VOTE is not specified by users. It is specified by the system administrator. For further information about the EXPLAIN modifier, see the Teradata RDBMS for UNIX Database Design and Administration, Chapter 17, “Designing the Database for Optimum Performance.”

Teradata RDBMS for UNIX SQL Reference

8-141

Teradata SQL Syntax Guide EXPLAIN Modifier

Terms

Terms used in EXPLAIN phrases are described in the following list: 8

Term

Explanation

All-AMPs JOIN step by way of an all-rows scan...

On each AMP on which they reside, spooled rows and/or primary table rows are searched row by row; rows that satisfy the join condition are joined. All-AMPs JOIN step by way of a RowHash match scan...

8-142

1

The first row is retrieved from the first table; that row’s hash code is used to locate a row from the second table.

2

Each row hash match is located and processed as follows:

1

The row hashes are compared; if not equal, the larger row hash is used to read rows from the other table until a row hash match is found, or until the table is exhausted.

2

If match is found, each pair of rows with the same hash code is accessed (one at a time) from the two tables. For each such pair, if the join condition is satisfied, a join result row is produced.

3

After all rows with the same row hash are processed from both tables, one more row is read from each table; the row hashes from these two rows are compared, restarting the compare process.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide EXPLAIN Modifier Term

Explanation

All-AMPs RETRIEVE step by way of an all-rows scan...

All rows of a table are selected row by row on all AMPs on which the table is stored. BMSMS (bit map set manipulation step); intersects the following row id bit maps: 1) 2)

The bit map built for ... by way of index # n... The bit map built for ... by way of index # n... . . . The resulting bit map is placed in Spool n... BMSMS...

Indicates that two or more bit maps are intersected by ANDing them to form one large bit map. index # n...

Identifies, in the order in which they are ANDed, each non-unique secondary index used in the intersection. resulting bit map is placed in Spool n...

Identifies the temporary file in which the large bit map produced by the BMSMS is stored to make it available for use in producing the final result By way of index # n and the bit map in Spool n...

The data row associated with the row ID is accessed only if the associated bit is turned on in the bit map (see Usage Notes). By way of the sort key in spool field1...

Field1 is created to allow a tag sort. Computed globally...

The computation involves all the intermediate spool file data. Condition...

An intermediate condition that joins table rows (as compared with the overall join condition). Duplicated on all AMPs...

A spool file containing intermediate data that is used to produce a result is copied to all AMPs containing data with which the intermediate data is compared.

Teradata RDBMS for UNIX SQL Reference

8-143

Teradata SQL Syntax Guide EXPLAIN Modifier Term

Explanation

Eliminating duplicate rows...

Duplicate rows can exist in spool files; not in tables. A DISTINCT operation. END TRANSACTION step..

This indicates that processing is complete and that any locks on the data may be released. Changes made by the transaction are committed. Estimated size...

This value, based on any statistics collected for a table, is used to estimate the size of the spool file needed to accommodate spooled data. If statistics have not been collected for a table, this estimate may be grossly incorrect (see “COLLECT STATISTICS” statement). Estimated time...

This approximate time is based on average times for the suboperations that comprise the overall operation, and the likely number of rows involved in the operation. The accuracy of the time estimate is also affected by the accuracy of the estimated size. Execute the following steps in parallel...

This identifies a set of steps that are processed concurrently. The explanatory text immediately following the list describes the execution of each step. Join condition...

The overall constraint that governs the join. In the following statement, “Employee.EmpNo = Department.MgrNo” is the overall constraint governing the join. SELECT DeptName, Name FROM Employee, Department WHERE Employee.EmpNo = Department.Mgrno; Last Use...

This term identifies the last reference to a spool file that contains intermediate data that produces a statement’s final result. The file is released following this step Locally on the AMPs...

That portion of spooled intermediate or result data for which an AMP is responsible is stored on the AMP; it is not duplicated on or redistributed to other AMPs that are processing the same request.

8-144

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide EXPLAIN Modifier Term

Explanation

Lock...

The Teradata RDBMS places an Access, Read, Write, or Exclusive lock on a database, table, or row that is to be accessed by a request. Merge join...

One of the types of join processing performed by the Teradata RDBMS. Nested join...

One of the types of join processing performed by the Teradata RDBMS No residual conditions...

Rows are selected in their entirety; there are no specific search conditions. All applicable conditions have been applied to the rows. Product join...

One of the types of join processing performed by the Teradata RDBMS. Redistributed by hash code to all AMPs...

Given the values of an indexed or non-indexed column, rows are sent to the AMPs that are responsible for storing the rows that use these values as a primary index. Single-AMP JOIN step by way of the unique primary index...

A row is selected on a single AMP using the unique primary index for the table. Using a value in the row that hashes to a unique index value in a second table, the first row is joined with a second row located on another AMP. Single-AMP RETRIEVE by way of unique index # n...

A single row of a table is selected using a unique secondary index value that hashes to the AMP on which the table row is stored. Single-AMP RETRIEVE step by way of the unique primary index...

A single row of a table is selected using a unique primary index value that hashes to the single AMP on which the data row is stored. Although not explicitly stated in the LOCK portion of the Explain text, a rowhash lock is required because the table is accessed via the unique primary index.

Teradata RDBMS for UNIX SQL Reference

8-145

Teradata SQL Syntax Guide EXPLAIN Modifier Term

Explanation

SORT to order Spool n by row hash...

Rows in the spool file are sorted by hash code to put them the same order as rows in the primary table, or in another spool file on the same AMP, with which they are to be matched SORT to order Spool n by row hash and the sort key in spool field1 eliminating duplicate rows...

Rows in the spool file are sorted by hash code using a uniqueness sort to eliminate duplicate rows. Uniqueness is based on the data in field1. The contents of field1 depend on the query and may comprise any of the following: ● A concatenation of all the fields in the spool row (used for queries with SELECT DISTINCT or that involve a UNION, INTERSECT, EXCEPT, or MINUS operation). ● A concatenation of the row IDs that identify the data rows from which the spool row was formed (used for complex queries involving subqueries). Some other value that uniquely defines the spool row (used for complex queries involving aggregates and subqueries). SORT to order Spool 1 by the sort key in spool field1...

Rows in the spool file are sorted by the value in field1. The contents of field1 are determined by the column or columns defined in the ORDER BY or WITH...BY clause of the request being processed. Spool n...

Identifies a spool file, which is a temporary file used to contain data during an operation. ● Spool 1 is normally used to hold a result before it is returned to the user. ● Spools 2, 3, etc., contain intermediate data that produces a result. Spool_n.Field...

Identifies the field of the spooled rows that is being used in a join constraint or comparison operation. For example: PERSONNEL.Employee.EmpNo = Spool_2.MgrNo Statement 1...

This term refers to the initiating request.

8-146

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide EXPLAIN Modifier Term

Explanation

The estimated time is nn seconds.

The time shown is not clock seconds; you should consider it to be an arbitrary unit of measure that compares different operations. Two-AMP RETRIEVE step by way of unique index #n...

A row of a table is selected based on a unique secondary index: ● A single row in the unique secondary index subtable is selected using the index value that hashes to the AMP on which the subtable row is stored. ● The hash value in the index row ID determines the AMP on which the data row is stored. We do a BMSMS... (bit map set manipulation step)

BMSMS is a method for handling weakly selective secondary indexes that have been ANDed; NUSI bit mapping. We do an ABORT test ...

An ABORT or ROLLBACK statement was detected. We do a SMS (set manipulation step)...

Combine rows under control of a UNION, EXCEPT, MINUS, or INTERSECT operator. Which is duplicated on all AMPs

Relocating data in preparation for a join. Which is resistributed by hash code to all AMPs

Relocating data in preparation for a join.

Examples showing the use of the EXPLAIN modifier are provided in Chapter 9, and also in Chapter 17 in the manual, Teradata RDBMS for UNIX Database Design and Administration

Teradata RDBMS for UNIX SQL Reference

8-147

Teradata SQL Syntax Guide GIVE

GIVE

8

The GIVE statement transfers ownership of a database or user space to another user.

Function

GIVE is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

GIVE

dborusername

TO

recipientname ; FF07A025

where:

Privileges and Access Rights

8

Syntax Element...

Specifies . . .

dborusername

the name of the database or user whose ownership is being transferred.

TO

an introduction to the name of the recipient.

recipientname

the name of the new immediate owner for the transferred database or user.

The user entering the GIVE statement for a database or user must have DROP DATABASE privileges on the given object, and CREATE DATABASE privileges on the recipient. The GIVE statement does not revoke any explicit access rights that the old ownership hierarchy had on the given database or user. No rights on the given database or user are granted to the new ownership hierarchy as a result of the GIVE statement, nor does the database or user being given receive any explicit rights. The recipient of a GIVE statement cannot be owned by the given object; if A owns B, A cannot be given to B.

Databases and Users Owned Are Also Transferred

8-148

All databases and users owned by the transferred database or user are also transferred. 8

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GIVE

Transfer of Space Allocation

8

A transfer of ownership also transfers the space allocated to the named database or user; this affects space allocation in the Teradata RDBMS as follows: ●



The aggregate number of bytes available to the former owners (the owner who is entering the GIVE statement plus the owners above this owner in the hierarchy) is reduced by the number of bytes in the transferred database. This includes total space allocated to the database, plus that of all databases and users owned by the transferred database. If the transferred database is dropped, then the number of bytes allocated to the new immediate owner of the transferred database is increased by the number of bytes in the transferred database. In addition, the aggregate number of bytes available to owners above this owner in the hierarchy is increased by the number of bytes that had been allocated to the transferred database.

For example, in the following hierarchy: DBC A (60)

B (80)

C (40)

D (30)

F (10) FF07A070

If ownership of database C is transferred to database D, the structure of the hierarchy would change as follows: DBC A (60)

B (80) D (30) C (40) F (10) FF07A071

When database C is transferred, database F (which is owned by C) is also transferred.

Teradata RDBMS for UNIX SQL Reference

8-149

Teradata SQL Syntax Guide GIVE ●

● ● ●

Example

While the number or bytes allocated to database A remains the same, the aggregate number of bytes available to database A is reduced by 50 (the total number of bytes allocated to databases C and F). The number of bytes allocated to databases D and B remains the same. The available number of bytes, however, is increased by 50 (that is, if databases C and F were dropped, the bytes allocated to C and F are transferred to database D).

The following statement may be used to transfer ownership of the Finance database from user Administrator to user Chin. GIVE Finance TO Chin;

8-150

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT

GRANT

8

The GRANT statement has three forms which differ in function and syntax: ●



GRANT (SQL) pertaining to access control Teradata RDBMS database objects, and GRANT (MONITOR) for performance monitoring of the Teradata RDBMS. The MONITOR-related GRANT statement grants system-wide performance monitoring privileges.



GRANT LOGON

Teradata RDBMS for UNIX SQL Reference

8-151

Teradata SQL Syntax Guide GRANT Statement (SQL)

GRANT Statement (SQL) Function

8

8

The GRANT statement (SQL) assigns one or more privileges on a database, user, table, view, or macro to a user or a group of users.

Syntax

, GRANT

ON

ALL PRIVILEGES ,

dbname dbname.objname objname

operation

25

username

TO

A

ALL PUBLIC

ALL BUT A WITH GRANT OPTION

; FF07B055

8-152

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT Statement (SQL)

where: Syntax Element...

Description

ALL [PRIVILEGES]

Grants to the specified user all privileges, that can be granted on the specified object. To include the ability to GRANT, use the WITH GRANT OPTION clause. ● Monitor privileges must be granted with a separate statement. In previous releases, GRANT ALL meant that the user had to have ALL of the appropriate privileges with the privilege to grant them, or else an error would be returned. GRANT ALL now means that ALL privileges owned by the grantor, WITH GRANT OPTION, that would pertain to the type of object, are granted. ●

privilege

An error is returned if the grantor has no privileges WITH GRANT OPTION on the object.

Is one of the following: CHECKPOINT CREATE DATABASE CREATE MACRO CREATE TABLE CREATE USER CREATE VIEW DATABASE DELETE DROP DATABASE, MACRO, TABLE, USER DROP VIEW DUMP EXECUTE INDEX INSERT MACRO REFERENCES [col_name_list or ALL BUT ... col_name_list] RESTORE ROLLBACK DATABASE ROLLFORWARD DATABASE SELECT TABLE UPDATE [col_name_list or ALL BUT ... col_name_list] USER VIEW

Teradata RDBMS for UNIX SQL Reference

8-153

Teradata SQL Syntax Guide GRANT Statement (SQL) Syntax Element...

Description

ANSI SQL supports only SELECT, INSERT, UPDATE, DELETE and REFERENCES as privileges. The other privileges are Teradata RDBMS extensions to ANSI SQL. UPDATE and REFERENCES have table and column level options. See also Usage Notes. The recipient of such a privilege can execute the corresponding statement against the object on which the privilege was granted. If User1 receives the CREATE TABLE privilege on database DbTest, User1 can then execute a CREATE TABLE statement in which the new table is directed to DBTest (where the target database is resolved implicitly, as determined by User1’s default database, or explicitly with a fully qualified table name.) The privileges that do not correspond to an SQL statement are DATABASE, MACRO, TABLE, USER, VIEW, DUMP, and RESTORE. If the DATABASE, MACRO, TABLE, USER, or VIEW keyword is specified without CREATE or DROP, both CREATE and DROP are assumed by default. The CHECKPOINT privilege refers to the execution of both the SQL statement and the Host Utilities (HUT) command. The DUMP and RESTORE privileges refer to the corresponding HUT command executed on the specified object. RESTORE also refers to execution of the HUT commands ROLLBACK, ROLLFORWARD, and DELETE JOURNAL. ALL BUT

Grants to the specified user all privileges that may be granted on the specified object except for those specified in the privilege list. As in ALL, only those privileges, which the grantor owns WITH GRANT OPTION are granted. ALL BUT is a Teradata RDBMS extension to ANSI SQL. Granting on a database or user is not ANSI, it is a Teradata RDBMS extension to ANSI SQL.

8-154

ON

Introduces the clause that specifies the object-name on which privileges are to be granted.

dbname

Specifies the name of a database or user on which the privileges are to be granted. All objects in this database or user space are affected.

dbname.objname

Specifies the name of the owning database and the name of the object (table, view, or macro) on which the privileges are to be granted. Only the named object is affected.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT Statement (SQL) Syntax Element...

Description

objname

Specifies the name of the object (table, view, or macro) on which the privileges are to be granted. The resolver checks for matching database names before checking for object names. If a database and a table have the same name, unpredictable results may occur.

TO

Introduces the clause that specifies the recipient of the GRANT results.

[ALL] username

Specifies the name of a database or user that identifies the recipient. username must be the identifier of a user already defined in the Teradata RDBMS. Up to 25 names can be entered. If ALL is specified, the privileges are to be granted to the named database or user, and to every database or user owned by that database or user now and in the future. ALL DBC is equivalent to PUBLIC. Note: ALL is flagged as non-ANSI when the SQL flagger is enabled.

PUBLIC

Specifies that the privileges are to be granted to all existing and future users of the Teradata RDBMS.

WITH GRANT OPTION

Specifies that the grantee will receive privileges WITH GRANT OPTION.

Object-Name

8

Object-name is the name of a table, view or macro. If the form of the rights option includes a set of column-names then object-name must be for either a table or view. If the object-name is not qualified by either a database-name or username and there is an object with that name both under the current database of the executing user and that of the grantee then it is assumed that the object is that in the current database of the executing user. Also, an unqualified object name is considered to be that of the current database if the name is that of the current database and also the name of a table either within the current database or within the database of the grantee.

REFERENCES

8

The REFERENCES privilege applies at both the table and column level. It is required on the columns implicitly or explicitly referenced in a FOREIGN KEY clause of a CREATE or ALTER TABLE statement. This privilege, with grant authority, is given to the owner and creator of a table by the system. If a column-name list is included with the keyword REFERENCES (or UPDATE) and the specified grantee set already has the right

Teradata RDBMS for UNIX SQL Reference

8-155

Teradata SQL Syntax Guide GRANT Statement (SQL)

specified at the table level with the specified grant authority, then the system accepts the statement but takes no action. This is because possessing UPDATE or REFERENCES (or UPDATE) right at table level allows that action against all columns of the table.

INDEX

Column Level Rights

8

8

The INDEX privilege only applies at table level. The privilege (or DROP TABLE) are required to execute the CREATE or DROP INDEX statements. The system automatically gives this privilege, with grant authority, to the owner and creator of the table. The DBC.AccessRights table now has column defined, which contains the field id of a column on which UPDATE or REFERENCES right has been granted. The column type is a nonnull small integer. If the right defined by the row is not at the column level then the value of the column is zero. Column level rights are implicitly granted to the owner/creator of a table. However, a row is not generated in the DBC.AccessRights table for these column level rights. Rows are not generated in the access rights table for individual columns when the UPDATE or REFERENCES rights are granted at the table level. Rows only exist in the table for rights granted through execution of a GRANT statement at the column level when the grantee does not have the right at the table level. The only exception to this is the case of a user having the right at the table level without GRANT authority and then that user is given the same right on an individual column with grant authority.

Rights Required to Perform UPDATE, DELETE, and INSERT

Any user who needs to perform conditioned updates, deletes, or inserts must be granted SELECT access privileges on all tables where there is a need to read values from the target table. 8

For INSERT, SELECT rights are required only if a table is to be reflexively grown by INSERT-SELECT statements upon itself. Take care granting the privilege to delete data through a view because data in fields that might not be known to the user (and which may be important to the enterprise) may be deleted when a row is deleted through a view.

Who Can Use GRANT?

8-156

8

The user submitting the GRANT statement either must be user DBC or someone DBC has granted privileges to, or must have WITH GRANT OPTION for the privilege to be granted, plus all of the privileges that are to be conferred, on the object.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT Statement (SQL)

If the object is a view or macro, the owner of the view or macro also must have the WITH GRANT OPTION for applicable privileges, plus all other applicable privileges, on the objects referenced by that view or macro. ●



A user need not be related to a grantor through ownership to receive a privilege. If a GRANT statement is at the database or user level, the privilege applies to all objects, both current and future, created in that space. If a REVOKE statement later removes the privilege, the privilege is dropped for all objects, regardless of when they were created. Also, a REVOKE statement at the object level cannot remove from that object a privilege that was granted at the database or user level. For those reasons, it is advisable to grant privileges at the object level rather than at the database level.



Any owner implicitly has the WITH GRANT OPTION on an owned database, user, or object. Therefore, a user can explicitly grant any or all privileges on an owned database, user, or object to any other user. The exception to this is an object that is a view or macro that references a non-owned object on which the user does not have the required WITH GRANT OPTION.

Any automatically or explicitly granted privilege can be revoked using the REVOKE statement. Implicit ownership privileges cannot be revoked.

Logging Access Attempts Restrictions on Granted Privileges

8

8

If it is necessary to maintain a security log of access attempts, see the “BEGIN/END LOGGING” statement. When a user explicitly grants privileges to another user, certain rules determine whether, how, and on what object the requested privilege is implemented. The restrictions that apply to explicitly granted privileges are detailed in Table 8-3. The first column of the table lists the privilege type, the second column describes restrictions if the privilege is granted on a database or user, and the third describes restrictions if the privilege is granted on a table, view, or macro.

Verifying Privileges on Views and Macros

8

When a CREATE VIEW statement is submitted, the Teradata RDBMS verifies that the creator has the SELECT privilege on the underlying tables and views. When a CREATE MACRO statement is submitted, the Teradata RDBMS verifies that the creator has the privileges needed to execute the statements in the macro body.

Teradata RDBMS for UNIX SQL Reference

8-157

Teradata SQL Syntax Guide GRANT Statement (SQL)

The Teradata RDBMS also verifies that the appropriate privileges exist on the target objects for any user who attempts to access a view or execute a macro. This ensures that a change to a target object does not result in a violation of access rights when the view or macro referencing that object is invoked. Additionally, in order to grant to another user any privilege on a view or macro that references objects owned by a third user, the grantor must have the required privilege WITH GRANT OPTION on those objects.

Example

8

Assume Allen creates two objects: a table named Allen.BaseTable, and a view derived from that table named Allen.ViewA. The system verifies that Allen has SELECT right on Allen.BaseTable when ViewA is created. Also assume Allen grants the SELECT privilege on ViewA to Bobby. Bobby then creates Bobby.ViewB, which is derived from (and thus references) Allen.ViewA. The system verifies that Bobby has SELECT right on Allen.ViewA when Bobby.ViewB is created. Bobby now wants to grant the SELECT privilege on ViewB to Chuck. Before Bobby can do this, he must receive from Allen the SELECT privilege WITH GRANT OPTION on Allen.ViewA.

Table 8-3 Restrictions on Granted Privileges Privilege

8-158

Object (Database or User)

Table, View, Macro

CREATE DATABASE CREATE USER

CREATE granted for the specified space.

Not applicable.

CREATE MACRO CREATE TABLE CREATE VIEW

CREATE granted for the object type for the specified space.

Not applicable.

DROP DATABASE DROP USER

DROP granted for the specified space.

Not applicable.

DROP MACRO DROP TABLE DROP VIEW

DROP granted for the object type for the specified space.

DROP granted for the specified macro, table, or view.

DATABASE MACRO TABLE USER VIEW

CREATE and DROP granted for the type for the specified space.

Not applicable.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT Statement (SQL) Privilege

Object (Database or User)

Table, View, Macro

DELETE INSERT SELECT UPDATE

Privilege applies to all tables or views in the specified database.

Privilege applies only to the specified table or view.

UPDATE applies to table or column of the table. The owner of a view must have appropriate privileges on the underlying tables of the view in order for a grantee to use the granted rights on a view.

The owner of the view must have appropriate privileges on the underlying tables of the view.

EXECUTE

Privilege applies to all macros in the specified database.

Privilege applies to the specified macro only.

The owner of the macro must have appropriate privileges on the objects referenced by the macro in order for the grantee to use the privilege on the macro.

The owner of the macro must have the appropriate privileges on the objects referenced by the macro.

ALL

All privileges granted.

Grants EXECUTE and DROP on a macro; DROP, DELETE,INDEX, INSERT, REFERENCES, SELECT, UPDATE, RESTORE, and DUMP on a data table; DROP, DELETE, INSERT, SELECT, and UPDATE on a view; INSERT, DUMP, RESTORE, and CHECKPOINT on a journal table.

DUMP RESTORE

Privilege applies to all tables in the specified database.

Privilege applies to the named data table or journal table only.

CHECKPOINT

Privilege applies to the journal table in the specified database.

Privilege applies to the named journal table.

REFERENCES

Not applicable

on a table or column of the table

INDEX

Not applicable

on a table

The same privilege is required when a macro references an object owned by a user other than the macro’s creator. For example, assume that the macro Bobby.MacroB deletes rows from Allen.ViewA. Bobby must have the GRANT OPTION for the EXECUTE privilege on Allen.ViewA in order to grant the EXECUTE privilege on Bobby.MacroB to another user.

Teradata RDBMS for UNIX SQL Reference

8-159

Teradata SQL Syntax Guide GRANT Statement (SQL)

Case Example (Views)

8

Assume that the view Bobby.ViewB is derived from the view Allen.ViewA, which in turn is derived from Allen.BaseTable. When Bobby enters the following statement: GRANT INSERT, DELETE ON Bobby.ViewB TO Chuck ;

the system checks that the following privileges exist. Stage

Process

1

Bobby has INSERT WITH GRANT OPTION on Bobby.ViewB

2

Bobby has DELETE WITH GRANT OPTION on Bobby.ViewB

3

Bobby has INSERT WITH GRANT OPTION on Allen.ViewA

4

Bobby has DELETE WITH GRANT OPTION on Allen.ViewA

5

Allen has INSERT WITH GRANT OPTION on Allen.BaseTable

6

Allen has DELETE WITH GRANT OPTION on Allen.BaseTable

Of these privileges, two are automatic privileges, and two are explicitly granted privileges. Ownership privileges cannot be revoked. For any other type, however, the Teradata RDBMS could fail to find one or more of the necessary privileges. In this case, an error message is returned to the user submitting the GRANT statement and the statement is not executed. The breakdown of privilege types for this example is as follows. FOR this category of privilege . . .

THIS individual . . .

HAS this privilege . . .

ON this object . . .

Automatic

Bobby

INSERT WITH GRANT OPTION

Bobby.ViewB

DELETE WITH GRANT OPTION Allen

INSERT WITH GRANT OPTION DELETE WITH GRANT OPTION

8-160

Teradata RDBMS for UNIX SQL Reference

Allen.BaseTable

Teradata SQL Syntax Guide GRANT Statement (SQL) FOR this category of privilege . . .

THIS individual . . .

HAS this privilege . . .

ON this object . . .

Explicit

Bobby

INSERT WITH GRANT OPTION

Allen.ViewA

DELETE WITH GRANT OPTION

Scenario (Macros)

Assume that Allen creates the following macro: CREATE MACRO Allen.MacroA AS (INSERT INTO Allen.BaseTable... DELETE FROM Allen.BaseTable...) ;

and that Bobby then creates this macro: CREATE MACRO Bobby.MacroB AS (EXECUTE Allen.MacroA) ;

If Bobby now tries to grant the EXECUTE privilege on MacroB to Chuck, as follows: GRANT EXECUTE, DROP MACRO ON Bobby.MacroB TO Chuck ;

the Teradata RDBMS checks that the following rights exist: THIS individual . . .

HAS this privilege . . .

ON this object . . .

Bobby

EXECUTE WITH GRANT OPTION

Bobby.MacroB

DROP MACRO WITH GRANT OPTION

Allen

EXECUTE WITH GRANT OPTION

Allen.MacroA

INSERT WITH GRANT OPTION

Allen.BaseTable

DELETE WITH GRANT OPTION

Again, if the Teradata RDBMS fails to find one or more of the required automatic or explicit privileges, the statement is not executed and returns an error.

Teradata RDBMS for UNIX SQL Reference

8-161

Teradata SQL Syntax Guide GRANT Statement (SQL)

The following examples illustrate the use of GRANT:

Examples Example 1

8

The following statement grants privileges to a group of users. In this example, all users created under the Finance database are granted the right to SELECT data from the Department table, which is in the Personnel database: GRANT SELECT ON Personnel.Department TO ALL Finance ;

Example 2: PUBLIC

8

The PUBLIC keyword grants privileges to all users of the Teradata RDBMS: GRANT SELECT ON Personnel.Department TO PUBLIC ;

Example 3: SELECT on Any Object Example 4: SELECT, INSERT, UPDATE, and DELETE on Any Object Example 5: CREATE, DROP Users Example 6: GRANT SELECT To ALL

The following statement allows Moffit to retrieve information from any object in the Personnel database: 8

GRANT SELECT ON Personnel TO Moffit ;

The following statement allows Peterson to perform select, insert, update, and delete operations on any object in the Personnel database: 8

GRANT SELECT, INSERT, UPDATE, DELETE ON Personnel TO Peterson ;

The following statement allows Phan to create and drop users in the Personnel database: 8

8

GRANT USER ON Personnel TO Phan ;

The following statement allows all current and future users created under the Personnel database to select data from the Department table: GRANT SELECT ON Personnel.Department TO ALL Personnel ;

Example 7: WITH GRANT OPTION

8

The WITH GRANT OPTION extends the GRANT option for only the indicated privileges. The following statement gives Marston all privileges (that the grantor has) on the space created under his user name which are grantable at the user level, and which the grantor has WITH GRANT OPTION and allows him to grant those privileges (optionally, WITH GRANT OPTION) to other users: GRANT ALL ON Marston TO Marston WITH GRANT OPTION ;

8-162

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT Statement (MONITOR)

GRANT Statement (MONITOR) Function

8

8

The GRANT MONITOR statement grants system-wide performance monitoring privileges. GRANT MONITOR is flagged as non-ANSI when the SQL flagger is enabled.

Syntax

GRANT

MONITOR

TO

A

PRIVILEGES , BUT NOT

ABORTSESSION MONRESOURCE MONSESSION SETRESRATE SETSESSRATE

, ABORTSESSION MONRESOURCE MONSESSION SETRESRATE SETSESSRATE , A

username ALL PUBLIC

WITH GRANT OPTION

; FF07A056

where: Syntax Element...

Description

MONITOR PRIVILEGES

Grants the specified recipients all MONITOR-related privileges. MONITOR [PRIVILEGES] does not permit the user to grant the indicated privilege to others without the WITH GRANT OPTION being specified.

BUT NOT

Grants all of the above grantable privileges except those specified. If the ability to grant these privileges is to be included, the WITH GRANT OPTION must be specified explicitly.

ABORTSESSION

Aborts any outstanding request or ongoing transaction of one or more Teradata RDBMS sessions and, optionally, logs off the sessions.

Teradata RDBMS for UNIX SQL Reference

8-163

Teradata SQL Syntax Guide GRANT Statement (MONITOR)

GRANT and GRANT MONITOR

8

Syntax Element...

Description

MONRESOURCE

Gathers information on how each vproc is performing as well as the availability of each processor.

MONSESSION

Gathers information about logged on sessions and overall system usage on a session-by-session basis.

SETRESRATE

Sets the frequency at which processor resource usage data is updated in the Teradata RDBMS.

SETSESSRATE

Sets the frequency at which session-level performance data is updated in the Teradata RDBMS.

WITH GRANT OPTION

Specifies that the recipient will receive the listed privileges WITH GRANT OPTION.

The SQL and MONITOR forms of GRANT are separate statements; to grant a user all privileges including MONITOR, the grantor must execute both statements, thus: GRANT TO GRANT TO

ALL PRIVILEGES ON object username WITH GRANT OPTION ; MONITOR PRIVILEGES username WITH GRANT OPTION ;

Do not specify the WITH GRANT OPTION if you do not want the recipient of the privilege to be able to grant those privileges to others. ALL PRIVILEGES refers only to database-related privileges. MONITOR PRIVILEGES indicates all monitoring related privileges.

GRANT Versus GRANT MONITOR

8

A major difference between the syntax of the GRANT and GRANT MONITOR privileges is that GRANT MONITOR has no clause specifying what the access right has been granted on. Because GRANT MONITOR allows a user to impact the entire system there is an implicit ‘ON PUBLIC’ clause. It is illegal to specify an ON object clause in any GRANT MONITOR statement. However, it is illegal not to specify an ON object clause in any other GRANT statement. If you violate either one of these restrictions, a failure response occurs. The two sets of GRANT privileges differ in function as well as in syntax. The GRANT privilege relates to controlling access to Teradata RDBMS database objects. The GRANT MONITOR privilege relates to monitoring the Teradata RDBMS itself.

8-164

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT Statement (MONITOR)

GRANT takes effect immediately when the grantee issues his next command. It’s unnecessary to log out to receive the privilege just granted. Caution:

Determine carefully who should be granted MONITOR access rights. MONITOR access rights should be granted only to those users who are cleared to monitor all applications on all sessions. No lower level of MONITOR privileges exists. For example, the database administrator cannot grant user Addams the ability to do session-level monitoring of her applications only. Instead, the database administrator would have to grant Addams the permission to do session-level monitoring of all applications by all sessions. To determine who is currently using the MONITOR partition, issue the following query: SELECT UserName, IFPNo FROM DBC.SessionInfo WHERE Partition = ’MONITOR’

The GRANT statement is used only to assign specific privileges. To transfer ownership of a database or user, see the “GIVE” statement.

Teradata RDBMS for UNIX SQL Reference

8-165

Teradata SQL Syntax Guide GRANT LOGON

GRANT LOGON Function

8

The GRANT LOGON statement gives specific users permission to log on to the Teradata RDBMS from one or more specific client systems. In addition, it may be used to change the current system defaults. This statement can be submitted only by the system administrator or by a trusted user to whom the system administrator has granted the EXECUTE privilege on the DBC.LogonRule macro. GRANT LOGON is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

, GRANT LOGON

ON

hostid ALL

AS DEFAULT , TO

WITH NULL PASSWORD

;

dbname

FROM FF07B027

where: Syntax Element...

Description

hostid

Identifies a mainframe channel connection or a local area network connection that is currently defined to the Teradata RDBMS by the hardware configuration data. The interface need not be operational. The hostid for the Teradata RDBMS console is 0 (zero). For any other connector, hostid is a value from 1 to 1023.

ALL

The ALL keyword, used in place of a hostid, applies to any source through which a logon is attempted, including the Teradata RDBMS console.

AS DEFAULT

Specifies that the current default for the specified hostid(s) is to be changed, without residual conditions, as defined in this GRANT LOGON statement. A statement with AS DEFAULT has no effect on the access granted to or revoked from particular usernames (see Usage Notes). A statement that sets the default for a specific hostid takes precedence over a statement that sets the default for ALL client systems.

8-166

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GRANT LOGON Syntax Element...

Description

TO FROM dbname

Overrides the current default for the specified username(s) on the specified hostid(s). ● The name DBC cannot be specified as a username in a GRANT LOGON statement (see Usage Notes). A statement that includes this name will return an error message. ● The product of the number of hostids times the number of usernames cannot exceed 25.

[...,dbname]

WITH NULL PASSWORD

The initial Teradata RDBMS default is that all logon requests must include a password. The WITH NULL PASSWORD option, in conjunction with a TDP security exit procedure (see Usage Notes), permits a logon string that has no password to be accepted from the specified client system community.

A statement that includes the AS DEFAULT option has no effect on the logon access granted to or revoked from specific usernames; a user named in a GRANT LOGON statement can always access the applicable client system even if that client has a default of REVOKE, and a user named in a REVOKE LOGON statement cannot access the applicable client system even if that client has a default of GRANT.

Privileges

When a GRANT LOGON statement is submitted, the system checks that the requesting user has EXECUTE privilege on the system macro associated with that statement. However, no checks are made on whether the usernames defined in the statement apply to users owned by the requesting user. If the submitted statement cannot be verified because, for example, it specifies an invalid username or an invalid hostid, no action is taken on the statement.

GRANT LOGON for One or More Usernames

If No Logon Control Record for a Username

8

8

When a GRANT LOGON statement is processed for one or more usernames, a logon control record is created for each username/hostid pair specified. Any existing control record for a particular username/hostid pair is replaced. The logon control record(s) created for a particular username will stay in existence until that user is dropped (see “DROP DATABASE, DROP USER”). If there is no logon control record for a specific username, access to the Teradata RDBMS under that name is governed by the current default for the hostid through which the logon is entered.

Teradata RDBMS for UNIX SQL Reference

8-167

Teradata SQL Syntax Guide GRANT LOGON

Logging on as DBC

8

Any attempt to log on to the Teradata RDBMS with username DBC requires a password. If the submitted password is correct, the logon is accepted regardless of the current default for the applicable hostid. This prevents any opportunity to lock out all client systems from the Teradata RDBMS. Attempts to GRANT LOGON for username DBC will result in errors.

Logging on Without a Password?

8

When the Teradata RDBMS is connected to multiple client systems, the initial default is that logon permission is granted to all users from all client system connections, and that all logons must include a password. For a user to log on without a password, two factors must exist: ●



8-168

A GRANT LOGON statement that includes the WITH NULL PASSWORD option must be executed for the defined username or must be the default for the hostid. A security exit installed in the TDP or CLI must acknowledge that the logon string for this username is valid without a password (UNIX CLI and WINCLI support exits).

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide GROUP BY Clause

GROUP BY Clause

8

The GROUP BY clause of the SELECT statement groups result rows by the values in one or more columns. See Chapter 7, “Queries: The SELECT Statement,” “GROUP BY Clause”, for a detailed description of GROUP BY.

Teradata RDBMS for UNIX SQL Reference

8-169

Teradata SQL Syntax Guide HAVING Clause

HAVING Clause The HAVING clause in the SELECT statement specifies a conditional expression that must be satisfied by the rows to be included in the resulting data. The condition may define one or more aggregates (MAX, MIN, AVG, SUM, COUNT), and may be applied to the rows of: ●



A single group defined in the select expr-list, which has only aggregate results One or more groups defined in a GROUP BY clause

Refer to Chapter 7, “Queries: The SELECT Statement,” “HAVING Clause”, for a detailed description of the HAVING clause.

8-170

Teradata RDBMS for UNIX SQL Reference

8

Teradata SQL Syntax Guide HELP

HELP

8

The HELP statement retrieves information from the Teradata RDBMS Data Dictionary, and includes information about:

Function

● ● ● ●

The specified database, table, view, macro, or index; All or the specified columns of a table or view; Any statistics collected for the specified table; The attributes of the current session.

HELP is flagged as non-ANSI when the SQL flagger is enabled. Refer also to HELP (On-line), in the following section.

Syntax

HELP

DATABASE

dbname

USER

username

TABLE

tname

VIEW MACRO

viewname macroname ,

COLUMN COLUMN

;

col_name *

FROM ,

, FROM ,

tname.

COLUMN

tname.col_name

COLUMN

tname. *

INDEX

tname name

STATISTICS

tname

, ( col_name )

tname

SESSION CONSTRAINT

tname.name dbname.

Teradata RDBMS for UNIX SQL Reference

FF07A028

8-171

Teradata SQL Syntax Guide HELP

where:

Usage Notes

8

Syntax Element ...

Specifies . . .

dbname

the name of the database for which help is needed.

username

the name of the user space for which help is needed.

tname

the name of the table for which help is needed.

viewname

the name of the view for which help is needed.

macroname

the name of the macro for which help is needed.

col_name

the name of the column for which help is needed.

FROM

an introduction to the name(s) of one or more tables containing the columns for which help is needed.

*

that help is needed for all columns in a table.

name

the name of the index, if it has been assigned a name.

Except for HELP USER DBC, the user requesting HELP information must either own the specified object or have at least one privilege on that object (HELP USER DBC does not require any rights on user DBC). There is no check of object type; HELP USER and HELP DATABASE have the same effect. The HELP statement allows users to review the attributes of an object or a session without having to construct and execute a query on a Data Dictionary view. The actual output of HELP results are too wide to be shown on a single screen and are sometimes wider than 132 characters. Interactive users can format returns from HELP with BTEQ formatting commands such as [.]SET SIDETITLES and [.]SET FOLDLINE. Various examples of HELP queries are shown following. The contents of HELP returns are explained in the following subsections.

Examples

8-172

8

Note that the format of the results shown in these examples may not always match the actual output.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide HELP

Example 1: HELP DATABASE

The following statement returns the following information about the Personnel database.: 8

HELP DATABASE Personnel; Table/View/Macro Name --------------------Department . .

Example 2: HELP TABLE

8

Kind Comment ---- ------T . .

Protection ----------F . .

Creator Name -------------Administration . .

The following statement returns information about the Department table. HELP TABLE Personnel.Department; Column Name ----------DeptNo DeptName Loc MgrNo

Example 3: HELP MACRO

8

Type ---I2 CV CF I

Comment ------------------------------------Department number Department name Department location Employee number of department manager

The following statement returns information about the NewEmp macro. HELP MACRO NewEmp; Parameter Name -------------Name Number Dept

Example 4: HELP COLUMN

8

Type ---CV I I2

Comment --------------------------------------Employee name, last name first;required Employee number; required Department number; required

The following statement requests information about particular columns in the Employee and Department tables (note that each column reference is fully qualified with its table name): HELP COLUMN Employee.Name, Employee.DeptNo, Department.DeptNo FROM Employee, Department ;

The return from the preceding query lists each column in the order in which it was specified; therefore, the first DeptNo column listed is for the Employee table, the second is for the Department table: Column Name ----------Name DeptNo DeptNo

Type ---CV I2 I2

Nullable -------N Y N

Teradata RDBMS for UNIX SQL Reference

8-173

Teradata SQL Syntax Guide HELP

Example 5: HELP INDEX

8

The response to the following statement shows that one index is defined for the Department table. HELP INDEX Personnel.Department;

Unique -----Y

Example 6: HELP STATISTICS

Primary or Secondary Column Names Index Id Approximate Count --------- --------------- -------- ----------------P DeptNo 1 7

If COLLECT STATISTICS is in effect for the Employee table, this statement may be used to obtain current Employee statistics: 8

HELP STATISTICS Personnel.Employee;

The result shows that statistics were last collected for YrsExp on 85/06/17, when there were 17 unique values for the column: Date Time Unique Values Column Names -------- -------- -------------------- ---------------85/06/17 13:12:45 17 YrsExp

Example 7: HELP SESSION

The following statement shows the current attributes of the user’s session. 8

HELP SESSION ; User Name ---Unm1

Example 8: Formatting HELP Results

8

Account Name ------DBC

Logon Date -------89/05/26

Logon Time -------09:49:09

Current Collation Character DataBase Set -------- --------- --------Unm1 EBCDIC EBCDIC

HELP results may be formatted. For example, this BTEQ input returns session information as demonstrated by the following report. .SIDETITLES .FOLDLINE HELP SESSION; User Name Account Name Logon Date Logon Time Current DataBase Collation Character Set Transaction Semantics

8-174

Teradata RDBMS for UNIX SQL Reference

SPEC1 DBC 89/05/26 09:47:09 SPEC1 MULTINATIONAL SWED_EBCDIC A

Teradata SQL Syntax Guide HELP COLUMN

HELP COLUMN Function

8

8

The following two statements return information about the attributes of the named column(s) in the named table or view. HELP COLUMN cname FROM tname HELP COLUMN tname.cname

The next two statements return this attribute information on all the columns of the named table or view. All column attributes are returned except any default values and comments (for these, use HELP TABLE tname). HELP COLUMN * FROM tname HELP COLUMN tname.*

If a HELP COLUMN statement contains a FROM clause, one or more tables may be referenced in the same statement, as long as each column name is fully qualified (that is, preceded by the table name in the form “tablename.columnname”). HELP COLUMN also allows the use of an expression: HELP COLUMN

expr

If the target is an expression, HELP COLUMN displays the data type of the expression. Note that HELP COLUMN returns index information for single-column indexes only. For multiple-column indexes, the columns are shown in the result as being not used in an index. The HELP COLUMN statement returns the information shown in Table 8-5. Note that for graphic data types, the Max Length represents the number of bytes, not the number of logical characters.

Example

The following shows a sample output display for HELP COLUMN: 8

CREATE TABLE table_1 (field_1 INTEGER NOT NULL PRIMARY KEY, f2 INTEGER CHECK 9f2>0) CHECK 9f20) AND (“f2”0 AND”field_2”>0)

The following example shows the report returned for a table with REFERENTIAL constraints. HELP CONSTRAINT table_1.reference_1;

Example 3

8

Name

Type

FK Columns

Parent Table Name

PK Columns

reference_ 1

REFERENCE

field_1, field_2

table_2

field_3, field_4

The following example shows the report returned for a table with a REFERENTIAL constraint on the primary key. HELP CONSTRAINT table_1.primary_1;

8-180

Name

Type

Columns

primary_1

PRIMARY KEY

field_1, field_2

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide HELP CONSTRAINT

Example 4

8

The following example shows the report returned for a table with a UNIQUE constraint. HELP CONSTRAINT table_1.unique_1;

Example 5

8

Name

Type

Columns

unique_1

UNIQUE

field_3, field_4

The following example shows how the system handles a HELP CONSTRAINT request that doesn’t fit the tabled being analyzed. HELP CONSTRAINT table_1.no_such_check;

The error message, ‘The specified constraint name does not exist in table’, is returned if table table_1 does not have a constraint with the name ‘no_such_check’ exists in table.

Teradata RDBMS for UNIX SQL Reference

8-181

Teradata SQL Syntax Guide HELP DATABASE HELP USER

HELP DATABASE HELP USER When HELP DATABASE or HELP USER is entered, the Teradata RDBMS returns a list of all tables, views, and macros in the specified database, sorted by object name. The list includes the following information about each object: ● ●

● ● ●



8-182

Name - CHAR (30) Type - CHAR (1) ● T indicates table ● V indicates view ● M indicates macro Comment, if available. - VARCHAR (255) nullable see “COMMENT” statement Protection - CHAR (1) ● N indicates none ● F indicates FALLBACK Creator name - CHAR (30)

Teradata RDBMS for UNIX SQL Reference

8

Teradata SQL Syntax Guide HELP INDEX

HELP INDEX Function Example

8

8

8

The HELP INDEX tname statement returns information about all the indexes defined for a table. The following example returns information about the specified index. HELP INDEX tname (cname[,cname...])

The following example returns information about the index named primaryone. HELP INDEX primaryone

Index information from HELP includes the following: ●



● ● ●



Unique? -- CHAR (1) ● N indicates non-unique ● Y indicates unique Primary or Secondary -- CHAR (1) ● P indicates primary ● S indicates secondary Column names -- VARCHAR (512) Index ID (assigned to the index by the system) Approximate count -- FLOAT (number of rows in the index subtable) Name -- CHAR(30) lists the index name, if there is one. This column is NULL if the index is unnamed.

Teradata RDBMS for UNIX SQL Reference

8-183

Teradata SQL Syntax Guide HELP SESSION

HELP SESSION Function

8

The HELP SESSION statement returns the attributes associated with the user of the current session, as follows: ● ● ●











8-184

User Name is the name of the owner of the session. Account Name is the name of the owner of User Name. Logon Date is the date of logon to the Teradata RDBMS, in the format YY/MM/DD. Logon Time is the time of logon to the Teradata RDBMS, in the format HH:MM:SS. Current DataBase is the database currently defined as the default for the session (see “DATABASE” and “CREATE USER” statements). Collation is the sort sequence currently defined for the session (see “SET SESSION COLLATION” and “CREATE USER” statements), e.g., ASCII. Character Set is the character set currently defined for the session, e.g., ASCII Transaction Semantics indicates whether the session is in ANSI (A) or Teradata (T) mode. Not nullable.

Teradata RDBMS for UNIX SQL Reference

8

Teradata SQL Syntax Guide HELP STATISTICS

HELP STATISTICS Function

8

8

The HELP STATISTICS tname statement returns information about statistics that have been collected for the specified table. If a COLLECT STATISTICS statement has not been submitted for the table, a message is returned. HELP STATISTICS information may be useful in determining whether statistics for the table should be updated or dropped (see COLLECT/DROP STATISTICS). The return includes: ● ● ● ●

Date of collection - CHAR (8) Time of collection - CHAR (8) Unique values - VARCHAR (20) Column names - VARCHAR (512)

Teradata RDBMS for UNIX SQL Reference

8-185

Teradata SQL Syntax Guide HELP TABLE HELP VIEW HELP MACRO

HELP TABLE HELP VIEW HELP MACRO Function

8

8

When a HELP TABLE or HELP VIEW statement is entered, the Teradata RDBMS returns a list of the columns or parameters in the specified table, view, or macro. The columns or parameters are listed in the order in which they were defined. The attributes are returned as shown in Table 8-5.

Table 8-5 Table, View, or Macro Attributes Field

Data Type

COLUMN NAME

Nullable?

CHAR (30)

no

CHAR (2)

yes

VARCHAR (255)

yes

CHAR (1)

yes

Comments

Type

I1

BYTEINT

I2

SMALLINT

I

INTEGER

D

DECIMAL (or NUMERIC)

F

FLOAT (or REAL or DOUBLE PRECISION)

DA

DATE

CF

CHAR

CV

VARCHAR

GF

GRAPHIC

GV

VARGRAPHIC

GV

LONG VARGRAPHIC

BF

BYTE

BV

VARBYTE

COMMENT NULLABLE Y

can be NULL

N

is NOT NULL

8-186

Teradata RDBMS for UNIX SQL Reference

Note: GF and GV type abbreviations will be replaced in the next release.

Teradata SQL Syntax Guide HELP TABLE HELP VIEW HELP MACRO Field

Data Type

Nullable?

Comments

FORMAT

CHAR (30)

yes

TITLE

VARCHAR (60)

yes

MAX LENGTH

SMALLINT

yes

DECIMAL TOTAL DIGITS

SMALLINT

DECIMAL FRACTIONAL DIGITS

SMALLINT

yes

If type is decimal.

RANGE LOW

FLOAT

yes

Always null. BETWEEN clause now included in column constraint.

RANGE HIGH

FLOAT

yes

Always null. BETWEEN clause now included in column constraint.

CHAR (1)

yes

If type is CHAR or VARCHAR.

UPPERCASE N

not declared as UPPERCASE or CASESPECIFIC

U

UPPERCASE

C

CASESPECIFIC

B

BOTH TABLE/VIEW

T

table

V

view

DEFAULT VALUE

Table Columns

View Columns

8

8

Maximum amount of storage (in bytes). If type is decimal.

CHAR (1)

VARCHAR (255)

Shows display format.

If HELP TABLE or HELP VIEW

yes

For some larger tables, a request to display all or most of the columns will result in an error message. If this occurs, the user can display the table definition with the SHOW TABLE statement, and may be able to display column attributes by querying the Data Dictionary view named DBC.columns. If a view has more than 50 columns, HELP VIEW returns an empty result. To obtain column information from oversized views, use the HELP statement in the form: HELP COLUMN viewname.*

For details on defining views, refer to the Usage Notes of the “CREATE VIEW” statement, and to Creating Views.

Teradata RDBMS for UNIX SQL Reference

8-187

Teradata SQL Syntax Guide HELP TABLE HELP VIEW HELP MACRO

Macro Parameters

8

If a macro contains no parameters, a message is returned to indicate that no parameters have been defined. HELP MACRO also may be used to obtain a comment that explains how to use the macro.

8-188

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide HELP (OnLine)

HELP (OnLine)

8

The HELP (OnLine) feature allows users to request help on any Teradata SQL syntax or RDBMS host utility syntax topic. This HELP is distinct from the other HELP commands, which provide information on the database. Refer also to the “HELP” statement, described in the previous section.

Function

HELP is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax HELP

' HELP

' ;

' SQL

commandname ' ARCHIVE

commandname ' BULKLOAD

commandname ' DUMP

commandname ' FASTEXPORT

commandname ' FASTLOAD

commandname ' MULTILOAD

commandname ' PMPC

commandname ' TPCCONS

commandname

Teradata RDBMS for UNIX SQL Reference

FF07R029

8-189

Teradata SQL Syntax Guide HELP (OnLine)

where: Syntax Element ...

Description

HELP

HELP ‘HELP‘ ;

HELP ‘HELP’ displays information about using the online help command. SQL ARCHIVE BULKLOAD DUMP FASTEXPORT FASTLOAD MULTILOAD PMPC TPCCONS

SQL or one of the utilities.

commandname

An optional specific command or statement name (in SQL or the utility).

These are the topics about which HELP is requested. If these topics are not followed by a specific command name, a list of commands are displayed for that topic.

The online HELP topics are:

Topics

● ● ● ● ● ● ● ● ● ●

Archive BulkLoad Dump FastExport FastLoad Help MultiLoad PMPC SQL TPCCONS

HELP ‘topic with optional CommandName’ can be typed in upper or lower case. The response for a HELP query on a non-existent topic is, “Query completed. No rows found”.

Response Format

8-190

8

The HELP response is comprised of a set of rows ordered by line number. The maximum length of each row of text is 80 characters. When accessed through BTEQ, no BTEQ formatting is needed in order to view the whole HELP message.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide HELP (OnLine)

Examples

8

This statement . . .

Displays . . .

HELP ‘sql’;

a list of SQL commands.

HELP ‘multiload’;

a list of MultiLoad commands.

HELP ‘sql update’;

the syntax for the UPDATE command.

Teradata RDBMS for UNIX SQL Reference

8-191

Teradata SQL Syntax Guide INSERT

INSERT

8

The INSERT statement adds new rows to a table by directly specifying the row data to be inserted, or by retrieving the new row data from another table.

Function

The INS abbreviation is non-ANSI and is flagged when the SQL Flagger is enabled.

Syntax

, INSERT INS

( expr )

tname INTO

VALUES ,

;

,

( col_name ) VALUES ( expr )

,

subquery

( col_name )

FF07A030

where: Syntax Element ...

Specifies . . .

INTO

a keyword that is required for ANSI compliance. If not used, it is flagged when the SQL flagger is enabled.

tname

the name of the table directly or by means of a view.

VALUES

a keyword that is required for ANSI compliance. When col_name is omitted, the VALUES keyword is optional. It is required when a col_name list is specified.

col_name

8-192

the name of a column for which the insert operation supplies a new row value. col_name may be specified in any order. If a col_name is omitted, any default value defined in the CREATE TABLE or CREATE VIEW statement is used.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide INSERT Syntax Element ...

Specifies . . .

expr

a constant or constant expression to be inserted into the new row for the columns specified in cname. A constant expression is an expression containing only constants (for example, 3+5, or 19000/12). The system values DATE, TIME, and USER may be included in a constant expression. A NULL value may be inserted by specifying the reserved word NULL as an expr value. Values obtained from imported data, with a USING modifier, or as macro parameters, are accepted as constants. If a column name list and an expr list are used, values are assigned by matching cname position with expr position. The two lists must have the same number of items. An element of the expression list can be omitted, in which case it is treated as NULL. This is not allowed in ANSI SQL. that the row or rows to be inserted consist of column values accessed by a query specification.

subquery

If a column name list is not specified, the SELECT statement must query the same number of columns as there are in the table that is receiving new rows. NULLs may be included in the select expr-list for columns that are not to be assigned values. If the INSERT operation includes a column name list, values are assigned by matching cname position with position of items in the select expr-list.

Required Privileges

The following privilege rules apply to the INSERT statement: 8









To insert rows into a table, a user must have the INSERT privilege on the referenced table. To insert rows into a table through a view, the user must have the INSERT privilege on the view. Also, the immediate owner of the view (that is, the database in which the view resides) must have the INSERT privilege on the underlying object (view or base table). To insert rows into a table using a query specification, the user must have the SELECT privilege for the referenced tables or views. When INSERT (with a condition) is executed and it requires READ access to an object for execution, the user executing this DML statement must have SELECT right to the data being accessed.

Teradata RDBMS for UNIX SQL Reference

8-193

Teradata SQL Syntax Guide INSERT

Some Typical INSERT Error Conditions at Japanese Character Sites

An error occurs if the operation attempts to insert: ●

8

● ●



Inserting Into NOT NULL Columns

The following rules apply to an INSERT operation that does not assign values for every column (field) in a new row: 8





Non Valid INSERT Operations

multibyte characters that are not valid for the current character set. multibyte characters that result in an odd number of bytes. graphic data that is greater than 16000 logical characters (onehalf of maxlength) or greater than maxlength. a graphic string having trailing graphic pad characters into a VARGRAPHIC field, and that operation causes the row to become identical to another row (except for the number of graphic pad characters).

8

If the column is not declared as NOT NULL and no default value is declared, NULL values are inserted. If the column is declared as NOT NULL and no default value is declared, an error message is returned.

An INSERT operation causes an error message to be returned if: any of the following are true. ●











The operation attempts to assign a value that will result in a violation of a unique index specification. The operation attempts to insert a row with no value or default for a column that is defined as NOT NULL. The operation attempts to assign a non NULL value that violates a CHECK constraint declared for a column. The operation attempts to assign a value that is of a different numeric type than that declared for the column and the assigned value cannot be converted correctly. The operation attempts to insert a character string having trailing blanks into a VARCHAR field, and that operation causes the row to become identical to another row (except for the number of trailing blanks). In ANSI mode, INSERTing character data, if in order to comply with maximum length of the target column, non-blank characters are truncated from the source data. In Teradata mode, the above insertion is permitted (characters are truncated silently). This could result in improper strings in Japanese character machines.

The Teradata RDBMS ignores trailing blanks on character strings when comparing values for field or row duplication.

8-194

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide INSERT

Valid INSERT Operations

8

An INSERT operation does not return an error message if many of the following are true. ●



Duplicate Rows and INSERT-SELECT

8

If an INSERT using a SELECT subquery will create duplicate rows, the result depends on both the table definition and the mode of the session doing the INSERT. This only applies if no UNIQUE constraints or UNIQUE indexes exist. Otherwise duplicate rows are not allowed, in any case. ●



The INSERT Process

The operation attempts to insert a graphic or character string that is longer or shorter than that declared for the column. The string is silently adjusted and inserted. However, in ANSI mode, if the string is too long a report is generated. The operation uses a query and no rows are returned.

MULTISET Tables. These tables permit duplicate rows and any duplicates created are stored in the table. SET Tables (no duplicate rows permitted), session in either ANSI or Teradata mode. Fully duplicate rows are discarded, leaving a single row image for the duplicates. No warning or error is issued even if the table includes unique indexes.

An INSERT process performs the following functions: 8





Sets a write lock on the row, or on the table in the case of an INSERT with a SELECT subquery. Handles the entire INSERT operation as a transaction in which every value is inserted successfully or no values are inserted. This is to prevent a partial insert from occurring.

The INSERT operation takes more processing time on a table defined with FALLBACK or a secondary index, because the FALLBACK copy of the table or the secondary index subtable also must be changed.

Inserting Rows Through a View

The following rules apply to inserting rows through a view: ● 8

● ●





The user and immediate owner of the view must have the appropriate privileges. The view must reference columns in only one table or view. None of the columns in the view may be derived by using an expression to change values in the underlying table. Each column in the view must correspond one to one with a column in the underlying table or view. The view must include any column in the underlying table or view that is declared as NOT NULL.

Teradata RDBMS for UNIX SQL Reference

8-195

Teradata SQL Syntax Guide INSERT ●



No two view columns may reference the same column in the underlying table. If the statement used to define a view contains a WHERE clause, and WITH CHECK OPTION, all values inserted through that view must satisfy constraints specified in the WHERE clause.

If a view includes a WHERE clause and does not include WITH CHECK OPTION, data may be inserted, which will not be visible through that view. Caution should be used when granting the privilege to insert data through a view. Data in fields not known to the user might be inserted when a row is inserted through a view.

INSERTs That Use Subqueries

8

An INSERT operation that uses a subquery differs from a simple INSERT in that many rows may be inserted in a single operation, and the row values may come from more than one table. The query specification must always include the FROM tname clause. Also, a column name list must be defined in an INSERT statement that includes a subquery when the following conditions exist: ●



Data Takes the Attributes of the INSERT’s New Table

8

The number of columns listed in the query specification differs from the number of columns contained in the table receiving new rows. The order of columns listed in the query specification differs from the order in which columns were defined in the table receiving the new rows.

If the column attributes defined for a new table differ from those of the columns whose data is being inserted via the INSERT statement, the data takes on the attributes of the new table. The source column names may differ from the destination column names in the new table. The data is inserted correctly if the SELECT statement lists the source column names in the same order as the corresponding destination columns in the CREATE TABLE statement. This is true even if the new table has a column that is to contain data derived (either arithmetically or by aggregate operation) from the column data in the existing table.

8-196

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide INSERT

Examples Example 1

The following examples illustrate the use of INSERT. 8

8

The following statement may be used to insert a row for the new employee Smith in the Employee table: INSERT INTO Employee (Name,EmpNo,DeptNo,DOB,Sex,EdLev) VALUES (’SMITH T’,10021,700,460729,’F’,16);

Example 2

8

This example uses a SELECT subquery to insert a row for every employee who has more than ten years of experience into a new table, Promotion, which contains three columns defined as Name, DeptNo, and YrsExp. INSERT INTO Promotion (DeptNum, EmpName, YearsExp) SELECT DeptNo, Name, YrsExp FROM Employee WHERE YrsExp > 10 ;

Example 3

8

The INSERT operation performed in the previous example may also be accomplished by the following statement. Note that a column name list is not given for the Promotion table; therefore, the Employee columns referenced in the SELECT subquery must match, in both quantity and sequence, the columns contained by Promotion. INSERT INTO Promotion SELECT Name, DeptNo, YrsExp FROM Employee WHERE YrsExp > 10 ;

Example 4

8

To add a row to the Employee table for new employee Orebo, you could enter the following statement: INSERT INTO employee (name, empno, deptno, dob, sex, edlev) VALUES (’Orebo B’, 10005, 300, ’Nov 17 1957’, ’M’, 18) ;

In this example, you list the columns that are to receive the data, followed by the data to be inserted, in the same order as the columns are listed. If you don’t specify a field value for a column named, a null value is stored.

Teradata RDBMS for UNIX SQL Reference

8-197

Teradata SQL Syntax Guide INSERT

Example 5

You could achieve the same result using the statement: 8

INSERT INTO employee (10005, ’Orebo B’,300,,,, ’Nov 17 1957’, ’M’,,,18,);

With this example, you don’t specify column names for fields because you enter the field data in the same order as the columns are defined in the Employee table. In all cases, a comma is required as a place marker for any field for which data is not specified. For ANSI compliance, the keyword NULL should be used for nulls, for example, (10005, ‘Ore60B’, 300, NULL, NULL, NULL, ‘Nov 17 1957’, ‘M’, NULL, NULL, 18)

8-198

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide LOCKING Modifier

LOCKING Modifier

8

The LOCKING modifier is used to lock a database, table, view, or row.

Function

The LOCKING modifier overrides the default usage lock that the Teradata RDBMS places on a database, table, view, or row in response to a request. The LOCKING modifier is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax dbname

LOCKING LOCK

DATABASE

tname

ACCESS FOR

EXCLUSIVE

IN

EXCL

TABLE

A MODE

NOWAIT

SHARE

vname VIEW

READ WRITE

ROW

statement

A

; FF07B031

where: Syntax Element ...

Specifies . . .

DATABASE TABLE VIEW

optional keywords that introduce the name of a database, table, or view that is to be locked

ROW

an optional keyword specifying a row or rows to be locked in accordance with the defining statement (see below).

dbname

the name of a database (or user) that is to be locked.

tname

the name of the table to be locked.

vname

the name of the view to be locked.

FOR IN

an introduction to the type of lock to be placed.

Teradata RDBMS for UNIX SQL Reference

8-199

Teradata SQL Syntax Guide LOCKING Modifier Syntax Element ...

Specifies . . .

ACCESS READ SHARE WRITE EXCLUSIVE

the type of lock to be placed. The SHARE keyword may be used as a synonym for READ. The CHECKSUM option is only available with Updatable Cursors through the Preprocessor. Refer to the Teradata Application Programming With Embedded SQL for C, Cobol, and PL/I.

MODE

an optional keyword that may be used for SQL compatibility.

NOWAIT

Indicates that if the indicated lock cannot be obtained, the statement should be aborted. Used for situations where it is not desirable to have a statement wait for resources, possibly also tying up resources in the process of waiting.

statement

Specifies a SQL statement. If the SQL statement is null, the only effect of LOCKING is to lock the specified object. Statement is optional for database, table, and view. Statement is mandatory for row.

When Explicit Locking Is Necessary

Positioning Non-Default Locks

8

8

The Teradata RDBMS automatically places usage locks each time a SQL statement is entered. Therefore, it is never necessary to use the LOCKING modifier. The LOCKING modifier is only needed to override automatic locking when another lock setting is desired. When a lock other than the default lock is needed, the LOCKING modifier should precede the SQL statement that is to be affected by the lock. When the LOCKING modifier is executed, the Teradata RDBMS places the specified lock on the object referenced by the modifier for the duration of the transaction in which the SQL statement resides. If the transaction is a single-statement transaction, the lock is only in effect during the processing of that statement. The Locking modifier can be used to specify a database, table, or row lock.

Using LOCK ROW

8

The use of LOCK ROW prevents possible deadlocks occurring when two simultaneous primary key SELECTs are followed by an UPDATE, DELETE, or INSERT on the same row. For example: User A: BEGIN TRANSACTION; SELECT y FROM t WHERE x = 1; UPDATE t SET y=0 WHERE x = 1;

User B: BEGIN TRANSACTION; SELECT z FROM t WHERE x = 1; UPDATE t SET z = 0 WHERE x = 1;

8-200

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide LOCKING Modifier

The system assigns a ROW READ LOCK when a simple SELECT is done on a Unique Primary Index, Primary Index, or Unique Secondary Index. User A’s UPDATE statement’s ROW WRITE LOCK request will wait for User B’s ROW READ LOCK to be released. The ROW READ LOCK will not be released because User B’s UPDATE statement’s ROW WRITE LOCK request is also waiting for User A’s ROW READ LOCK to be released. Previously, this deadlock was avoided by using the LOCK TABLE modifier: BEGIN TRANSACTION; LOCK TABLE t FOR WRITE SELECT z FROM t WHERE x = 1; UPDATE ....

But locking an entire table across all AMPS is undesirable. The use of LOCK ROW here prevents the need to lock an entire table across all AMPs. The following example illustrates the use of LOCK ROW: User A: BEGIN TRANSACTION; LOCK ROW WRITE SELECT y from t where x=1; UPDATE t SET y=0 where x=1 END TRANSACTION;

User B: BEGIN TRANSACTION; LOCK ROW WRITE SELECT z from t where x=1; UPDATE t set z=0 where x=1;

No deadlock will occur because User B’s LOCK ROW WRITE request is blocked by User A’s LOCK ROW WRITE. User B’s LOCK ROW WRITE statement will complete when User A’s END TRANSACTION statement is complete. The LOCK ROW modifier will only work: ●





For single table retrievals, using primary (NUPIs or UPIs) or unique secondary index (USI) searches. When specified with a SELECT statement, because the LOCK ROW modifier picks up the row hash to be locked from the SELECT statement. To upgrade an existing lock, from READ to WRITE, or from READ TO EXCLUSIVE, or from WRITE to EXCLUSIVE.

The LOCK ROW modifier does not work: ● ●

Where a lock on the target table is already in place. Where an aggregate/DISTINCT/GROUP BY operation is part of the SELECT statement

Teradata RDBMS for UNIX SQL Reference

8-201

Teradata SQL Syntax Guide LOCKING Modifier ●

To downgrade a lock from READ to ACCESS, once that lock is in place. If no row hash lock is in place already, an ACCESS lock can be set instead of a default READ row hash lock.

When two primary key SELECTS are followed by primary key UPDATES and the SELECTs are on different row hashes deadlock occurs. This is because a primary index update requires a table write lock on the target table. For example: User A: BEGIN TRANSACTION; LOCK ROW WRITE SELECT x FROM t WHERE x = 1; (x is UPI, NUPI, or USI) UPDATE t SET x=2 WHERE x=1;

User B: BEGIN TRANSACTION; LOCK ROW WRITE SELECT x FROM t WHERE x=2; UPDATE t SET x=1 WHERE x=2;

Here User B’s SELECT ROW WRITE LOCK will not be queued behind User A’s transaction because it has a different row hash access. Deadlock occurs because User A’s table lock request waits on User B’s row lock, while User B’s table lock request waits on User A’s row lock.

Using Locks with NULL Statements

Multiple Locks

8

8

If the LOCKING modifier is followed by a null SQL statement, the only effect of the modifier is to lock the specified object. While the LOCKING modifier may be used with a null statement, it is best to use the LOCKING modifier with a user generated transaction, or as part of a multi-statement request. Multiple LOCKING modifiers may precede a statement if it is necessary to lock more than one object at the same time. LOCKING TABLE Employee FOR ACCESS LOCKING TABLE Department FOR ACCESS SELECT Name, Loc FROM Employee, Department WHERE (EmpNo=MgrNo);

Referencing the Locked Object

The object that is locked by the LOCKING modifier does not have to be referenced in a subsequent SQL statement. 8

A LOCKING modifier may be executed separately from the SQL statement that it precedes. Therefore, a LOCKING modifier must reference the object on which a lock is being placed; the objects referenced in the SQL statement have no effect on the execution of a LOCKING modifier. When a LOCKING modifier references a view, the specified lock is applied to all underlying base tables. For example, if a view refers to

8-202

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide LOCKING Modifier

tables T_1 and T_2, then a lock on that view would apply to both tables. LOCKING DATABASE is the only way to lock a database or a user.

Include the Keyword for the Locked Object

Be sure to include the keyword for the entity (DATABASE, TABLE, or VIEW) that is to be locked. 8

For example, if there is a database and a table, and if both are named AccntRec, then the following form could be used: LOCKING TABLE AccntRec FOR ACCESS SELECT * FROM AccntRec;

If the TABLE keyword was not included, the lock would have been placed on the AccntRec database.

Locks and Views

8

A LOCKING modifier may be included in a view definition. When a view is defined with a LOCKING modifier, the specified lock is placed on the underlying table(s) each time the view is referenced in a SQL statement. Refer to the “CREATE VIEW” statement for more information on defining views.

When Both the Request and View Referenced Include Locking Clauses

8

Although views are often created to enforce a LOCKING FOR ACCESS rule, any user can override the LOCKING FOR ACCESS by using a LOCKING FOR READ statement on the view. For example: REPLACE VIEW VPROD.AD_ME_INF AS LOCKING TABLE PROD.AD_ME_INF FOR ACCESS SELECT AD_ME_ID AD_ME_DSC_TX FROM PROD.AD_ME_INF;

If an EXPLAIN is done on a SEL COUNT(*) FROM VPROD.AD_ME_INF; statement, the access lock can be seen in statement 1. If you did an EXPLAIN on the statement: LOCKING TABLE VPROD.AD_ME_INF FOR READ SEL COUNT (*) FROM VPROD.AD_ME_INF;

a read lock can be seen in statement 1 of the EXPLAIN.

Teradata RDBMS for UNIX SQL Reference

8-203

Teradata SQL Syntax Guide LOCKING Modifier

This behavior could be considered undesirable because the LOCKING FOR ACCESS statement can be overridden by anyone at anytime. However, certain customers consider this a useful feature, and depend on being able to override locking clauses in views by placing a lock in the request.

What Locks Have Been Set?

8

The EXPLAIN modifier may be used at the beginning of a SQL statement to determine what locks are set when the statement is executed. Users with the performance monitor MONITOR SESSION privilege can view all locks in the system through use of the Performance Monitor/Application Programming Interface (PM/API), and can determine which other users’ locks are blocking their own.

Types of Locks

8-204

8

A LOCKING modifier may be used to specify the following types of locks: Lock Type

Description

ACCESS

used to allow selection of data from a table that may be locked for write access by other users. Because the data selected using ACCESS may be inconsistent because the data is concurrently being modified, this lock should be used only for casual inspection of data. Placing an ACCESS lock requires the SELECT privilege on the specified database or table.

READ

used to ensure data consistency during a read operation (execution of a SELECT statement, for example). A number of users may hold a READ lock on a table at the same time. As long as a read lock is in place, no modification of the table is allowed. Placing a READ lock requires the SELECT privilege on the specified database or table.

WRITE

enables a single user to modify data. As long as the WRITE lock is in place, all other users are locked out except readers who are viewing data using an ACCESS lock. Until a WRITE lock is released, no new READ locks are permitted. Placing a WRITE lock requires an UPDATE, INSERT, or DELETE privilege on the specified database or table.

Teradata RDBMS for UNIX SQL Reference

8

Teradata SQL Syntax Guide LOCKING Modifier Lock Type

Description

EXCLUSIVE

excludes all other users. An EXCLUSIVE lock is rarely used, except to make structural changes to a database. This is the most restrictive lock. Placing an EXCLUSIVE lock requires the DROP privilege on the specified database or table.

CHECKSUM

only used with updatable cursors. Refer to the Teradata

Application Programming with Embedded SQL for C, Cobol, and PL/I

Cancelling a Lock

Examples Example 1

8

The wait for a requested lock may be indefinite, unless the NOWAIT option is specified. When an interactive user does not want to wait for a lock, a BTEQ .ABORT command may be issued to cancel the transaction. The following examples illustrate the use of LOCKING:

8

8

The following LOCKING clause may be used to select data from the Employee table while the table is being modified: LOCKING TABLE Personnel.Employee IN ACCESS MODE SELECT Name, Salary FROM Employee WHERE Salary < 25000 ;

This select operation may do any of the following things. ●

● ●

Example 2

8

Return rows whose data may be updated or deleted an instant later by a concurrent operation initiated by another user who has obtained a WRITE lock. Omit rows that are undergoing a concurrent insert operation. Include rows that were never permanently in the table, because a transaction that was being used to insert new rows was aborted and its new rows backed out.

The system deals properly with the synchronization of base data rows and index subtable rows. However, an ACCESS lock may allow inconsistent results even when secondary indexes are used in conditional expressions, because index constraints are not always rechecked against the data row.

Teradata RDBMS for UNIX SQL Reference

8-205

Teradata SQL Syntax Guide LOCKING Modifier

For example, if a column named “QualifyAccnt” is defined as a secondary index for a table named “AccntRec”, the following request: LOCKING TABLE AccntRec FOR ACCESS SELECT AccntNo, QualifyAccnt FROM AccntRec WHERE QualifyAccnt = 1587;

could return: AccntNo QualifyAccnt ------- -----------1761 4214

In this case, the value “1587" was found in the secondary index subtable, and the corresponding data row was selected and returned. However, the data for account 1761 had been changed by another user while the selection was in process. Returns such as this are possible even if the data is changed only momentarily by a transaction that is ultimately aborted. The ACCESS lock is most useful to those who simply want an overview of data and are not concerned with consistency.

Example 3: LOCK ROW

This example demonstrates the proper use of a row lock. 8

CREATE TABLE customer (cust_id int,phone int, fax int, telex int) PRIMARY INDEX (cust_id), UNIQUE INDEX(fax), INDEX(telex): CREATE TABLE sales (custcode int, zip int, salesvol int);

User A: BEGIN TRANSACTION; LOCK ROW EXCL SELECT phone from customer where cust_id=12345; UPDATE customer set phone=3108292488 where cust_id=12345;

User A’s row lock prevents another user from accessing the same record. In the following, user A’s row lock, in conjunction with Lock Table, prevents user B from accessing the same record: LOCKING TABLE sales for READ, LOCKING ROW for WRITE SELECT telex from customer where fax=0; UPDATE customer set telex=0 where fax=0; SELECT zip from sales where custcode=111; SELECT salesvol from sales where custcode=222; ... END TRANSACTION;

User B: BEGIN TRANSACTION; LOCK ROW WRITE SELECT * from customer where cust_id=12345 INSERT INTO customer (12345, 3108284422, 3108684231, 5555); END TRANSACTION;

User B’s LOCK ROW statement waits until user A’s transaction ends before it can be completed.

8-206

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide MODIFY DATABASE

MODIFY DATABASE

8

The MODIFY DATABASE statement changes the options that were specified when a database was created.

Function

MODIFY DATABASE is flagged as non-ANSI when the SQL flagger is enabled.

Syntax , MODIFY DATABASE

dbname

AS

= number

PERMANENT

;

BYTES

PERM SPOOL = number BYTES ACCOUNT =

acctid

FALLBACK NO

PROTECTION JOURNAL

NO

BEFORE

DUAL AFTER JOURNAL NO DUAL LOCAL NOT LOCAL

tname

DEFAULT JOURNAL TABLE =

dbname. DROP DEFAULT JOURNAL TABLE = tname FF07B058

Teradata RDBMS for UNIX SQL Reference

8-207

Teradata SQL Syntax Guide MODIFY DATABASE

where: Syntax Element ...

Specifies . . .

dbname

the name of the database to be modified.

AS

an introduction to the list of options for modifying the database.

PERMANENT

a keyword introducing the allocated fixed space.

number

a new value (n) for fixed space allocation, in bytes. number may be entered as an integer, a decimal value, or as a floating point value.

BYTES

an optional unit assigned to the fixed space value.

SPOOL

a keyword introducing the allocated spool space.

number

a new value (n) for the maximum space allocation for spool files, in bytes. number may be entered as an integer, a decimal value, or as a floating point value.

BYTES

an optional unit assigned to the spool space.

ACCOUNT

a keyword introducing the account identifier.

acctid

a new identifier for the account to be charged for the space used by this database. acctid can be up to 30 characters (provided they are not Japanese characters, in which case acctid can be up to 30 bytes, using any of the supported character sets), and must be enclosed by apostrophes.

NO FALLBACK PROTECTION

a new default for duplicate copy protection of each data table subsequently created in the database. The current fallback setting for existing data tables remains unchanged. The FALLBACK keyword used alone implies PROTECTION.

8-208

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide MODIFY DATABASE Syntax Element ...

Specifies . . .

NO DUAL

a new default for the number of before change images to be maintained for data tables subsequently created in the database.

BEFORE JOURNAL

The NO keyword terminates any current journaling default. If the JOURNAL keyword is specified without NO or DUAL, single-copy journaling is implied. If journaling is specified, a DUAL journal is maintained for data tables with FALLBACK protection. Existing images are not affected until the corresponding table is updated. This option may appear twice in the same statement: once to specify a BEFORE or AFTER image, and again to specify the alternate type. If only one type is specified, then the current default is modified only for that type. If both BEFORE and AFTER are specified then the two must not conflict. The JOURNAL keyword without AFTER or BEFORE indicates that both types of images are to be maintained. In this case, the current default for either or both types is modified accordingly. For example, if only AFTER JOURNAL is specified, the current default for before-change images remains in effect.

Teradata RDBMS for UNIX SQL Reference

8-209

Teradata SQL Syntax Guide MODIFY DATABASE Syntax Element ...

Specifies . . .

NO DUAL LOCAL NOT LOCAL

the type of image to be maintained for the table; any existing images are not affected until the table is updated. NO, DUAL, LOCAL, or NOT LOCAL: the NO and DUAL options specify the number of after-change images to be maintained for the table.

AFTER JOURNAL

NOT LOCAL and LOCAL specify whether single after-image journal rows for non-fallback data tables are written on the same virtual AMP (LOCAL) as the changed data rows, or on another virtual AMP in the cluster (NOT LOCAL).

DEFAULT JOURNAL TABLE= dbname.

tname DROP DEFAULT JOURNAL TABLE= tname

a redefinition for the current journal table or removes its status as the default for the database being modified. The DROP keyword removes the default status of the journal table currently defined as the default for the database being modified. If the journal table resides in the database being modified, DROP also deletes the table from the system. An error message is returned if the DROP request would delete a journal table that is being used by active data tables. The tname parameter is required if this clause is specified without the DROP keyword. If a database is not specified, then the database being modified is assumed. If the database being modified does not have a journal table, tname is created. If a different database is specified, then it must already exist and tname must have been defined as its default journal table. Specifying this option does not change the status of existing data tables in the modified database.

Several options can be included in a single MODIFY DATABASE statement. No order is imposed, but an error is reported if options are duplicated or conflicting options are included.

Privileges Required

8

To use the MODIFY DATABASE statement, the user must have the DROP DATABASE privilege on the referenced database. MODIFY DATABASE cannot be used to modify users. When a MODIFY DATABASE statement is executed, an exclusive lock is placed on the database that is being modified.

8-210

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide MODIFY DATABASE

If necessary, the defined PERM or SPOOL space is changed to the next higher value that is a multiple of the number of AMPs on the system. One or more options may be changed in the same MODIFY statement. A changed option applies to all subsequent operations that occur for the database. If an existing option is not changed by a MODIFY statement, that option remains the same.

Changing the Default Journal Table Residing in the Modified Database

8

Two modify statements are needed to change a default journal table that resides in the database being modified. The first statement is used to DROP the present journal table from the system. An error message is returned if the journal table is being used by active data tables. After the table is dropped, a second statement is required to define a new default journal table.

Dropped Journal Table Cannot be Restored Two or More Databases/Users Can Share Journal Table

Local Journaling

8

8

8

A dropped journal table cannot be restored. Archives containing data tables that reference the dropped journal are no longer valid and cannot be used. Two or more databases/users can share a journal table. However, if the sharing databases/users retain as their default the name of a journal table that has been dropped from the system, subsequent CREATE TABLE statements that use that default will return an error message. The LOCAL single AFTER image journal is supported analogously to single BEFORE image journal: ●





Access Rights required to create or drop LOCAL single AFTER image journal are the same as for the analogous operation on single BEFORE image journal. DROP DATABASE privilege is required. LOCAL single AFTER image journaling is restricted to nonfallback data tables. Archive/Recovery rules for LOCAL single AFTER image journal and single BEFORE image journal are the same, except that LOCAL single AFTER image journal is used with ROLLFORWARD only, while single BEFORE image journal is used with ROLLBACK only.

MultiLoad, FastLoad and Archive/Recovery are impacted by the use of LOCAL journaling.

Teradata RDBMS for UNIX SQL Reference

8-211

Teradata SQL Syntax Guide MODIFY DATABASE

See also the Teradata RDBMS for UNIX Database Design and Administration, Chapter 12, “Permanent Journaling”.

Examples Example 1

The following examples illustrate the use of MODIFY DATABASE: 8

8

The following statement may be used to change the permanent space allocation for the Personnel database to 6,000,000 bytes: MODIFY DATABASE Personnel AS PERMANENT = 6000000 BYTES;

Example 2

8

Two MODIFY statements are needed to change the default journal table if it resides in the database being modified. For example, suppose the journal table “FinCopy” resides in the Personnel database. To change the default journal table from “FinCopy” to “Jrnl1", the present default journal table must be dropped by entering the statement below. This statement removes “FinCopy” as the default and also drops it from the system. If any existing tables use “FinCopy” as their journal table, the statement returns an error message. MODIFY DATABASE Personnel AS DROP DEFAULT JOURNAL TABLE ;

Example 3

8

Now that the present default journal has been dropped, this statement may be used to define a new default journal table: MODIFY DATABASE Personnel AS DEFAULT JOURNAL TABLE = Jrnl1 ;

Example 4

8

Conversely, if the current journal table does not reside in the database being modified, the following statement could be used to change the default journal table from “FinCopy” to “Jrnl1": MODIFY DATABASE Personnel AS DEFAULT JOURNAL TABLE = Jrnl1 ;

Example 5

8

Change fallback protection and space allocation options on the Finance database as follows. MODIFY DATABASE finance AS PERM = 75000000 ,SPOOL = 150000000 ,NOFALLBACK ,DROP DEFAULT JOURNAL TABLE = finance.journals ;

8-212

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide MODIFY USER

MODIFY USER

8

The MODIFY USER statement changes the options that were specified when a user database was created.

Function

MODIFY USER is flagged as non-ANSI, when the SQL flagger is enabled

Syntax , MODIFY USER

username

AS = number

PERMANENT PERM

BYTES STARTUP = NULL STARTUP = quotestring PASSWORD =

pwd NULL

RELEASE PASSWORD LOCK SPOOL = number BYTES

acctid

ACCOUNT =

(

acctid

,acctid

)

DEFAULT DATABASE = dbname FALLBACK PROTECTION

NO

JOURNAL NO

BEFORE

DUAL AFTER JOURNAL NO DUAL LOCAL NOT LOCAL

tname

DEFAULT JOURNAL TABLE =

dbname. DROP DEFAULT JOURNAL TABLE

= tname COLLATION

ASCII EBCDIC MULTINATIONAL HOST

Teradata RDBMS for UNIX SQL Reference

FF07B059

8-213

Teradata SQL Syntax Guide MODIFY USER

where: Syntax Element ...

Specifies . . .

username

the name of the user to be modified.

AS

an introduction to a list of options for modifying the user.

PERMANENT

a keyword introducing the allocated fixed space.

number

a new value (number) for fixed space allocation, in bytes. number can be entered as an integer, a decimal value, or as a floating point value.

BYTES

an optional unit assigned to fixed space value.

STARTUP =

a keyword introducing the startup null option.

NULL

that any existing startup string for the user is to be deleted.

STARTUP = quotestring

a replacement for the user’s startup string, which consists of one or more SQL statements that are executed when the user logs on to the Teradata RDBMS. A startup quotestring can be up to 255 characters, must be terminated by a semicolon, and be enclosed by apostrophes. A startup Kanji quotestring can be up to 255 bytes, must be terminated by a semicolon, and be enclosed by apostrophes. The USING modifier is not supported, and if a DDL statement is used, no other statement is allowed in the string.

PASSWORD =

a keyword introducing the password assigned.

pwd NULL

a new password for a user or if NULL, that no password is required. A password is considered to be an SQL word and may contain up to 30 characters. If the name is Japanese, refer to the formula discussed in Chapter 4, “Teradata SQL Lexicon,” “Calculating the Length of a Name”.

RELEASE PASSWORD LOCK

keywords to release a user lock. A user row in DBC.DBase becomes locked if the number of successive erroneous logon attempts reaches the site-specified threshold. The user row remains locked to logon attempts until the site-defined time has passed, or until a MODIFY USER statement is submitted to release the lock on the user.

SPOOL

8-214

a keyword introducing spool file space allocation.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide MODIFY USER Syntax Element ...

Specifies . . .

number

a new value (n) for the maximum space allocation for spool files, in bytes. number can be entered as an integer, a decimal value, or as a floating point value.

BYTES

an optional unit for the spool file space value.

ACCOUNT

a keyword introducing the account identifier.

acctid

a replacement for one or more account identifiers (quotestring) associated with the user. Quotestring may be up to 30 characters and must be enclosed by apostrophes. If the acctid is Japanese character, refer to the formula discussed in Chapter 4, “Teradata SQL Lexicon,” “Calculating the Length of a Name”. Japanese character acctids can be up to 30 bytes long. A list of acctids must be separated by commas and enclosed in parentheses. The first (or only) acctid in the list becomes the new default acctid for the user.

NO FALLBACK PROTECTION

a new default for duplicate copy protection of each data table subsequently created in the database. The current fallback setting for existing data tables remains unchanged. The FALLBACK keyword used alone implies PROTECTION.

DEFAULT DATABASE = dbname

a new name for the default database that is established each time the user logs on.

NO FALLBACK PROTECTION

a new default for duplicate copy protection of each data table subsequently created in the database. The current fallback setting for existing data tables remains unchanged. The FALLBACK keyword used alone implies PROTECTION.

Teradata RDBMS for UNIX SQL Reference

8-215

Teradata SQL Syntax Guide MODIFY USER Syntax Element ...

Specifies . . .

NO DUAL BEFORE JOURNAL

a new default for the number of before change images to be maintained for data tables subsequently created in the database. The NO keyword terminates any current journaling default. If the JOURNAL keyword is specified without NO or DUAL, single-copy journaling is implied. If journaling is specified, a DUAL journal is maintained for data tables with FALLBACK protection. Existing images are not affected until the corresponding table is updated. This option may appear twice in the same statement: once to specify a BEFORE or AFTER image, and again to specify the alternate type. If only one type is specified, then the current default is modified only for that type. If both BEFORE and AFTER are specified then the two must not conflict. The JOURNAL keyword without AFTER or BEFORE indicates that both types of images are to be maintained. In this case, the current default for either or both types is modified accordingly. For example, if only AFTER JOURNAL is specified, the current default for before-change images remains in effect.

NO DUAL LOCAL NOT LOCAL AFTER JOURNAL

a change to the type of image to be maintained for the table; any existing images are not affected until the table is updated. NO, DUAL, LOCAL, or NOT LOCAL: the NO and DUAL options specify the number of after-change images to be maintained for the table. NOT LOCAL and LOCAL specify whether single after-image journal rows for non-fallback data tables are written on the same virtual AMP (LOCAL) as the changed data rows, or on another virtual AMP in the cluster (NOT LOCAL).

DEFAULT JOURNAL TABLE= dbname. tname

8-216

a redefinition of the current journal table or removes its status as the default for the database being modified.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide MODIFY USER Syntax Element ...

Specifies . . .

DROP DEFAULT JOURNAL TABLE = tname

removal of the default status of the journal table currently defined as the default for the database being modified. If the journal table resides in the database being modified, DROP also deletes the table from the system. An error message is returned if the DROP request would delete a journal table that is being used by active data tables. The tname parameter is required if this clause is specified without the DROP keyword. If a database is not specified, then the database being modified is assumed. If the database being modified does not have a journal table, tname is created. If a different database is specified, then it must already exist and tname must have been defined as its default journal table. Specifying this option does not change the status of existing data tables in the modified database.

COLLATION

a keyword introducing the collation options, which redefine the user’s default collation. Refer to Appendix G, “International and Japanese Character Support”, for more information about collation options.

ASCII

ASCII collation, which is the default HOST collation for configurations other than an IBM channel-attached host. ASCII here refers to Teradata’s extension to the ASCII standard.

EBCDIC

EBCDIC collation, which is the default HOST collation for an IBM channel-attached host.

MULTINATIONAL

that the default collation for the user is one of the European (diacritical) or Kanji sort sequences (see Usage Notes).

HOST

that the default collation for the user is determined by the collation of the logon client system.

Several options can be included in a single MODIFY DATABASE statement. No order is imposed, but an error is reported if options are duplicated or conflicting options are included.

Privileges Required

8

To use the MODIFY USER statement, the user must have the DROP USER privilege on the referenced user. A user must have DROP USER privileges on himself in order to change PERMANENT, SPOOL, and ACCOUNT parameters.

Teradata RDBMS for UNIX SQL Reference

8-217

Teradata SQL Syntax Guide MODIFY USER

MODIFY USER cannot be used to modify a database. If the user who is entering the MODIFY USER statement is the same as the user who is referenced in the statement, then changing BEFORE JOURNAL, AFTER JOURNAL, DEFAULT JOURNAL TABLE, FALLBACK, PASSWORD, STARTUP, COLLATION, and DEFAULT DATABASE options does not require any access privilege.

MODIFY USER Rules

The following rules apply to the use of MODIFY USER. 8





PERM or SPOOL Space is Changed Accordingly DEFAULT DATABASE

8

8

Any or all options can be changed using the same MODIFY statement. A changed option applies to all subsequent operations that occur for the user. If an existing option is not changed by a MODIFY USER statement, that option remains the same.

If necessary, the defined PERM or SPOOL space is changed to the next higher value that is a multiple of the number of AMPs on the system. If a DEFAULT DATABASE clause is specified, the default database is changed for subsequent user sessions. To change the default database for the current session, a DATABASE statement may be entered. A user’s default database may not be set to NULL. To remove the current DEFAULT DATABASE, a user may set it to the username associated with the MODIFY USER statement. This is the default setting if a DEFAULT DATABASE clause has not been specified.

Two MODIFY Statements Needed to Change Default Journal Table

8

Two MODIFY statements are needed to change a default journal table that resides in the user space being modified. The first statement drops the present journal table from the system. After the table is dropped, a second statement is required to define a new default journal table. An error message is returned if the journal table is being used by active data tables.

Dropped Journal Tables Cannot be Restored

8-218

8

A dropped journal table cannot be restored. Archives containing data tables that reference the journal are no longer valid, and cannot be used.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide MODIFY USER

Two or More Databases/Users Can Share a Journal Table

Collation

8

8

Two or more databases/users can share a journal table. However, if the sharing databases/users retain as their default the name of a journal table that has been dropped from the system, subsequent CREATE TABLE statements that use the corresponding default will return an error message. The COLLATION option determines the ordering of data characters during comparison operations and when sorting data in response to a SELECT query that includes an ORDER BY or WITH...BY clause. The MULTINATIONAL parameter defines that the default collation sequence for the user is one of the International sort orders (see the ORDER BY clause of the “SELECT” statement). During any session, the user can override the COLLATION attribute by executing a SET SESSION COLLATION statement.

Local Journaling

8

The LOCAL single AFTER image journal is supported analogously to single BEFORE image journal: ●





Access Rights required to create or drop LOCAL single AFTER image journal are the same as for the analogous operation on single BEFORE image journal. DROP DATABASE privilege is required. LOCAL single AFTER image journaling is restricted to nonfallback data tables. Archive/Recovery rules for LOCAL single AFTER image journal and single BEFORE image journal are the same, except that LOCAL single AFTER image journal is used with ROLLFORWARD only, while single BEFORE image journal is used with ROLLBACK only.

MultiLoad, FastLoad and Archive/Recovery are impacted by the use of LOCAL journaling. See also the Teradata RDBMS for UNIX Database Design and Administration, Chapter 12, “Permanent Journaling”.

Examples Example 1

The following examples illustrate the use of MODIFY USER: 8

8

The following statement can be used to change the permanent space allocation for user Peterson: MODIFY USER Peterson AS PERMANENT = 6000000 BYTES;

Teradata RDBMS for UNIX SQL Reference

8-219

Teradata SQL Syntax Guide MODIFY USER

Example 2

8

The following statement can be used to change user Chin’s default database and password: MODIFY USER Chin AS DEFAULT DATABASE = Personnel, PASSWORD = Nitram ;

Example 3

8

Two MODIFY statements are needed to change the default journal table if it resides in the user being modified. For example, suppose the journal table “FinCopy” resides in user Jones. To change the default journal table from “FinCopy” to “Jrnl1,” the present default journal table must be dropped and then a new journal table must be created. The first statement removes FinCopy as the default and drops it from the system (if there are any existing tables that use it as their journal table, the statement will return an error): MODIFY USER Jones AS DROP DEFAULT JOURNAL TABLE ;

When the current journal table has been dropped, the second statement creates a new default journal table: MODIFY USER Jones AS DEFAULT JOURNAL TABLE = Jrnl1 ;

Example 4

8

If the current journal table does not reside under the user being modified, the following statement could be used to change the default journal table from FinCopy to Jrnl1: MODIFY USER Peterson AS DEFAULT JOURNAL TABLE = Jrnl1 ;

Example 5

8

You do not need access privileges to change the PASSWORD, STARTUP, FALLBACK, or DEFAULT DATABASE definitions in space you own. Otherwise, the user submitting a MODIFY statement must have the DROP privilege on the user or database being changed. By way of example, although Marks is not the creator of his own space and does not own himself, he can submit a MODIFY USER statement to change his password, modify his startup string, or redirect his default database. Marks may resize Finance, as well. Change password and other options on user Marks: MODIFY USER marks AS PASSWORD = design ,SPOOL = 1500000 ,STARTUP = ’EXEC paystat;’ ,DEFAULT DATABASE = payroll ;

8-220

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ORDER BY Clause

ORDER BY Clause

8

The ORDER BY clause of the SELECT statement specifies how result data is to be sorted. Refer to Chapter 7, “Queries: The SELECT Statement,”, “ORDER BY”, for a detailed description of the ORDER BY clause.

Teradata RDBMS for UNIX SQL Reference

8-221

Teradata SQL Syntax Guide RENAME

RENAME

8

The RENAME statement renames an existing table, view, or macro.

Function

RENAME is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

RENAME

MACRO

oldname

TABLE

TO

newname

AS

VIEW

; FF07A034

where: Specifies . . .

oldname

the existing name for the macro, table, or view.

TO AS

keywords introducing the new name for the macro, table, or view.

newname

the new name for the macro, table, or view.

To use the RENAME statement, the user must have DROP privileges on the macro, table, or view to be renamed, and the appropriate CREATE privileges on the containing database.

Usage Notes







When a table is renamed, only the table name is changed. All statistics and privileges belonging to the table remain with it under the new name. When a macro, table, or view is renamed, an exclusive lock is placed on the item being renamed. To rename a macro, table, or view that is not contained in the same database as the user’s current default database setting, the old and new name must be qualified with the name of the database in which the object exists.

The following statement renames the Employee table:

Example 1 Example 2

Syntax Element ...

RENAME TABLE Employee TO Emp;

8

The following statement renames the Employee table if the user’s current default database setting is other than Personnel: RENAME TABLE Personnel.Employee TO Personnel.Emp;

8-222

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REPLACE MACRO

REPLACE MACRO

8

The REPLACE MACRO statement redefines an existing macro, or, if the specified macro does not exist, creates a new macro.

Function

REPLACE MACRO is flagged as non-ANSI when the SQL flagger is enabled.

Syntax

REPLACE MACRO

AS

macroname

A

, ( param_name

A

(

data_attribute

statement ;

)

) ; FF07A035

where:

Usage Notes

Syntax Element ...

Specifies . . .

macroname

the name of the new macro. If a fully qualified name is not specified, the default database is used.

param_name

the name of a parameter that is replaced with a value during macro execution. When referenced in the macro body, a parameter name is prefixed by the : (colon) character. A parameter may be defined as a constant or a constant expression.

data_attribute

a data description or default definition for a parameter. If a default value is not assigned, the user must specify a value for the parameter at EXECUTE time. Possible data types and attributes are listed in Chapter 5 and in this chapter.

AS

an introduction to a statement or statements that comprise the macro body.

statement;

a SQL statement. Each statement in the macro body must be terminated by a semicolon. Parameter names referenced in the macro body are prefaced by the colon (:) character. The macro body may include EXECUTE statements to invoke previously defined macros.

To replace an existing macro, you must have the DROP MACRO privilege. You must have the CREATE MACRO privilege if the specified macro does not exist.

Teradata RDBMS for UNIX SQL Reference

8-223

Teradata SQL Syntax Guide REPLACE MACRO

In effect, the REPLACE MACRO statement operates as a DROP followed by a CREATE, except for the handling of access rights. All access rights that were granted directly on the original view or macro are retained. If the specified macro does not exist, the REPLACE MACRO statement creates the view or macro. In this case, the REPLACE statement has the same effect as a CREATE MACRO statement. Unqualified object names in macros that contain DML statements are forever bound to the creating user’s default database at the time of creation. See also “CREATE MACRO”.

Limit On Request Size

Example

8

Because expansions of source text in macro definitions are made to fully qualify object names and to normalize expressions, it is possible for a macro to be defined but to be unusable because of stack overflows in the Syntaxer at execution. In this case, the system returns a 3741 error. The following example illustrates the use of REPLACE MACRO. The following statement may be used to replace macro NewEmp (refer to the “CREATE MACRO” and “SHOW MACRO SHOW TABLE SHOW VIEW” statements): REPLACE MACRO NewEmp(name VARCHAR(12) NOT NULL, street CHAR(30), city CHAR(20), number INTEGER NOT NULL, dept SMALLINT DEFAULT 999) AS (INSERT INTO Employee (Name, Street, City, EmpNo, DeptNo) VALUES (:name, :street, :city, :number, :dept); UPDATE Department SET EmpCount = EmpCount + 1 WHERE DeptNo = :dept ; ) ;

8-224

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REPLACE VIEW

REPLACE VIEW Function

8

The REPLACE VIEW statement redefines an existing view, or, if the specified view does not exist, creates a new view. REPLACE VIEW is flagged as non-ANSI when the SQL flagger is enabled.

Syntax

The form of REPLACE VIEW is identical to that of the CREATE VIEW statement, except that the keyword REPLACE is substituted for the keyword CREATE.

Teradata RDBMS for UNIX SQL Reference

8-225

Teradata SQL Syntax Guide REPLACE VIEW

REPLACE VIEW

viewname

A

AS , ( col_name

)

A (

B

dbname

LOCKING DATABASE

LOCK

tname

ACCESS FOR

EXCLUSIVE

IN

EXCL

TABLE

MODE

NOWAIT

SHARE

vname

READ

VIEW

WRITE

ROW B

SEL ECT

*

DISTINCT ALL

C

,

expr aname AS tname.*

, C FROM

tname

D Single Tables

aname AS

joined_table

JOIN joined_table ON search_cond INNER LEFT RIGHT FULL

CROSS JOIN ( subquery )

Joined Tables

OUTER

single_table

derived_tname

Derived Tables

,

AS (

col_name ) E

D ,

WHERE search_cond GROUP BY

col_name col_pos expr )

E ,

WITH CHECK OPTION ORDER BY

;

expr col_name col_pos

8-226

HAVING cond

Teradata RDBMS for UNIX SQL Reference

ASC DESC

FF07B069

Teradata SQL Syntax Guide REPLACE VIEW

where: Syntax Element...

Specifies . . .

viewname

the name of the new view. If viewname is not fully qualified, the default database is used.

col_name

the name of a view column. If more than one column is specified, list the col_names in the order in which each column is to be displayed for the view. It is recommended that a view have a maximum of 50 columns (see Usage Notes).

AS

a keyword that introduces the view definition.

LOCKING

a modifier that can be used to specify the type of lock to be placed on a database, table, view, or row. This setting overrides any default usage lock placed on that object by the Teradata RDBMS (see Usage Notes below, and the section on “LOCKING Modifier” in this chapter).

DATABASE TABLE VIEW

an optional keywords that introduce the name of a database, table, or view that is to be locked

ROW

an optional keyword specifying a row or rows to be locked in accordance with the defining statement (see below).

dbname

the name of the database (or user) to be locked.

tname

the name of the table to be locked.

vname

the name of the view to be locked.

FOR IN

an introduction to the type of lock to be placed. The IN keyword may be used for SQL compatibility.

ACCESS READ SHARE WRITE EXCLUSIVE

the type of lock to be placed. The SHARE keyword may be used as a synonym for READ, for SQL compatibility.

MODE

an optional keyword that may be used for SQL compatibility.

NOWAIT

that if the indicated lock cannot be obtained, the statement should be aborted. Used for situations where it is not desirable to have a statement wait for resources, possibly also tying up resources in the process of waiting.

SELECT

a keyword indicating that the expr(s) to be associated with col_name(s) are to be selected from one or more existing tables and/or views.

DISTINCT

that only one row is to be returned from any set of duplicates that might result from a given expr-list. Two rows are considered duplicates only if each value in one is equal to the corresponding value in the other.

ALL

that all rows, including duplicates, are to be returned in the results of the expression list. This is the default value.

Teradata RDBMS for UNIX SQL Reference

8-227

Teradata SQL Syntax Guide REPLACE VIEW Syntax Element...

Specifies . . .

expr

an expression. Aggregate and arithmetic operators may be used with expressions.

*

that all columns of all tables referenced in the FROM clause be returned. When qualified by tname, specifies that all columns of only tname are to be returned. View columns are explicitly enumerated when views are defined; therefore, if a table is changed after a view is defined, those changes will not appear if the SELECT * construct is used.

tname

the name of a table or view. The tname.* in the SELECT list can define the table from which rows are to be returned when two or more tables are referenced in the FROM clause.

aname

a different, temporary name (alias) for the table that is referenced by tname. aname must be used during a self-join operation on the table. aname is also used to name expressions.

AS

an optional clause introducing aname.

FROM

the names of one or more tables, views, or derived tables from which expr is to be derived. See the section FROM list for detail.

joined_table

the name of a joined table.

INNER

a join in which qualifying rows from one table are combined with qualifying rows from another table according to some join condition. Types of inner joins include exclusion, merge, nested, product, and row ID. This is the default join type.

OUTER

a join in which qualifying rows from one table that do not have matches in the other table, are included in the join result. The rows from the first table are extended with null values.

LEFT OUTER

the table that was listed first in the FROM clause. In a LEFT OUTER JOIN, the rows from the left table that are not returned in the result of the inner join of the two tables are returned in the outer join result, and extended with null values.

RIGHT OUTER

the table that was listed second in the FROM clause. In a RIGHT OUTER JOIN, the rows from the right table that are not returned in the result of the inner join of the two tables are returned in the outer join result, and extended with null values.

FULL OUTER

rows from both tables that have not been returned in the result of the inner join are returned in the outer join result, and extended with nulls.

JOIN

a keyword introducing the name of the second table to participate in the join.

joined_table

the name of the joined table.

ON

one or more conditional expressions that must be satisfied by the result rows. An ON condition clause is required if the FROM clause specifies an outer join.

search_condition

8-228

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REPLACE VIEW Syntax Element...

Specifies . . .

CROSS JOIN

all rows from all tables specified in the FROM clause.

single_table

the name of table participating in join. Derived Tables

The derived table allows the FROM list to specify a spool file, comprised of selected data from an underlying table(s). The derived table acts like a viewed table. This is an extension of a current functionality. It is a full ANSI SQL feature. (subquery)

nested SELECT statements.

AS

an introductory clause to derived table name.

derived_tname

the name of derived table.

col_name

the column name. The col_name field is for the column name only; forms such as Databasename.Tablename.Columnname, or Tablename.Columnname should not be used.

WHERE

a keyword introducing the search condition in the SELECT statement.

search_cond

a conditional search expression that must be satisfied by the row(s) returned by the statement.

GROUP BY

a keyword introducing a reference to one or more expressions in the select expression list. This reference defines a group for which an aggregate operation is specified in the expression list.

col_name

Col_name specifies the name(s) of columns used in the GROUP BY clause in the SELECT statement.

col_pos

the numeric position of the column(s) used in the GROUP BY clause.

expr

the expression(s) used in the GROUP BY clause.

HAVING

a keyword introducing the conditional clause in the SELECT statement.

cond

one or more conditional expressions that must be satisfied by the result rows. Aggregate operators may be used with HAVING. HAVING cond selects rows from a single group defined in the select expr-list that has only aggregate results, or it selects rows from the group or groups defined in a GROUP BY clause.

ORDER BY

a keyword specifying the order in which result rows are to be sorted.

col_name

the name(s) of columns used in the ORDER BY clause in the SELECT statement. These can be ascending or descending.

col_pos

the numeric position of the column(s) used in the ORDER BY clause. This can be ascending or descending.

expr

an expression in the SELECT expr-list, either by name, or by means of a constant that specifies the numeric position of the expression in the expr-list.

Teradata RDBMS for UNIX SQL Reference

8-229

Teradata SQL Syntax Guide REPLACE VIEW Syntax Element...

Specifies . . .

ASC DESC

the sort order. The default order is ASC. ASC specifies ascending order; DESC specifies descending order. If a sort option is not given, result values are sorted in ascending order according to the client system’s collating sequence. If ORDER BY is not specified, rows are returned unsorted.

WITH CHECK OPTION

Restricts the rows that can be accessed in the table by an INSERT or UPDATE statement. Refer to the section “WITH CHECK OPTION Clause in Views” below.

To replace an existing view or macro, you must have the DROP VIEW privilege.

Usage Notes

You must have the CREATE VIEW privilege if the specified view or macro does not exist. In effect, the REPLACE VIEW statement operates as a DROP followed by a CREATE, except for the handling of access rights. All access rights that were granted directly on the original view or macro are retained. If the specified view or macro does not exist, the REPLACE VIEW statement creates the view or macro. In this case, the REPLACE statement has the same effect as a CREATE VIEW statement. Unqualified object names in macros that contain DML statements are forever bound to the creating user’s default database at the time of creation. See also “CREATE VIEW”.

Limit On Request Size

8

The following examples illustrate the use of and REPLACE VIEW:

Examples Example 1

Because expansions of source text in view definitions are made to fully qualify object names and to normalize expressions, it is possible for a view to be defined but to be unusable because of stack overflows in the Syntaxer at execution. In this case, the system returns a 3741 error.

8

To change the department name column in the employee_info view to a department number column, enter: REPLACE VIEW employee_info (Number, Name, Position, Department) AS SELECT employee.empno, name, jobtitle, deptno WHERE jobtitle NOT IN (’vice pres’, ’manager’) ;

You must have the drop privilege on a view to replace it.

8-230

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REPLACE VIEW

If you enter a REPLACE VIEW statement for a view that does not exist, the system creates the view following the specifications in the REPLACE statement.

Example 2

8

You can display and change the current definition of a view by employing the SHOW VIEW statement. As an example, the statement: SHOW VIEW employee_info ;

displays the most recent CREATE or REPLACE statement for the employee_info view in the display area.

Teradata RDBMS for UNIX SQL Reference

8-231

Teradata SQL Syntax Guide REVOKE

REVOKE The REVOKE statement rescinds privileges from one or more users. The privileges may have been conferred automatically or by a previous GRANT statement. Implicit ownership privileges cannot be revoked. The REVOKE statement may be in SQL or MONITOR form. Two forms of REVOKE syntax are described separately on the following pages. A third form, REVOKE LOGON, is discussed in the following section. REVOKE is flagged as not entry level ANSI when the SQL flagger is enabled.

8-232

Teradata RDBMS for UNIX SQL Reference

8

Teradata SQL Syntax Guide REVOKE Statement (SQL)

REVOKE Statement (SQL) Function

8

Syntax

8

8

The SQL form of the REVOKE statement rescinds one or more privileges on a database, user, table, view, or macro to a user or group of users.

REVOKE

ALL

A

ON PRIVILEGES ,

GRANT OPTION FOR

operation ALL BUT ,

dbname

A

dbname.objname

username

TO FROM

objname

ALL PUBLIC FF07A061

where: Syntax Element ...

Description

GRANT OPTION FOR

When GRANT OPTION FOR is specified, then only the grant authority is removed from the specified rights for the specified grantees. REVOKE GRANT OPTION FOR revokes the recipient’s right to grant, but does not revoke the stated privileges themselves.

ALL PRIVILEGES

Revokes from the specified user all privileges, except MONITOR, that may be granted on the specified object, and that are possessed WITH GRANT OPTION by the user executing the REVOKE. To revoke all of a user’s privileges, including MONITOR, the revoker must execute at least two commands, shown as follows: REVOKE ALL PRIVILEGES ON object FROM username ; REVOKE MONITOR PRIVILEGES FROM username ;

ALL PRIVILEGES means only all database privileges. If the ALL PRIVILEGES is used, then only the rights possessed with grant authority are revoked from the grantee. MONITOR PRIVILEGES indicates all monitoring privileges. ANSI SQL requires ALL to be followed by PRIVILEGES.

Teradata RDBMS for UNIX SQL Reference

8-233

Teradata SQL Syntax Guide REVOKE Statement (SQL) Syntax Element ...

Description

ALL BUT

Revokes all privileges from the specified user, except those listed, that may be granted on the specified object, with the exception of GRANT.

privileges

Is one of the following: CHECKPOINT CREATE DATABASE, MACRO, TABLE, USER, CREATE VIEW DATABASE DELETE DROP DATABASE, MACRO, TABLE, USER DROP VIEW DUMP EXECUTE INDEX INSERT MACRO REFERENCES [col_name list or ALL BUT col_name list] RESTORE ROLLBACK DATABASE ROLLFORWARD DATABASE SELECT TABLE UPDATE [col_name list or ALL BUT col_name list] USER VIEW ANSI SQL supports only SELECT, INSERT, UPDATE, DELETE and REFERENCES as privileges. The other privileges are Teradata RDBMS extensions to ANSI SQL. UPDATE and REFERENCES have table and column level options. Refer to the Usage Notes in “GRANT”. Refer also to “Revoking Column Level Rights” in Usage Notes below. ANSI does not support REVOKE on a Database or User, or ALL BUT. These are Teradata RDBMS extensions to ANSI SQL. Any combination of privileges may be specified. However, the user submitting the statement must have all of the specified privileges WITH GRANT OPTION. When specified with the CREATE or DROP privilege, the USER and DATABASE keywords are identical and may be used interchangeably. If the DATABASE, MACRO, TABLE, USER, or VIEW keyword is specified without CREATE or DROP, both CREATE and DROP are revoked.

8-234

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REVOKE Statement (SQL) Syntax Element ...

Description

privileges (continued)

If CHECKPOINT is specified, the privilege is revoked both for the SQL statement and for the Host Utilities (HUT) command. DUMP and RESTORE revoke the privilege to execute the corresponding HUT command. If RESTORE is revoked, then the privilege to execute the HUT commands ROLLBACK, ROLLFORWARD, and DELETE JOURNAL is also revoked.

ON

Introduces the clause that specifies the object-name on which privileges are to be revoked.

dbname

Specifies the name of a database or user on which the privileges are to be revoked. All objects in this database or user space are affected.

dbname.objname

Specifies the name of the owning database and the name of the object (table, view, or macro) on which privileges are being revoked. Only this object is affected.

objname

The name of the table, view, or macro from which privileges are being revoked. The system checks database names before object names; if the object name is not qualified, and if it is the same as a database name, unpredictable results may occur.

TO FROM

Introduces the clause that specifies the recipient of the REVOKE results.

[ALL] username

Identifies the database or user from whom privileges are revoked. Up to 25 names may be specified. ALL username specifies that the privileges are to be granted to or revoked from the named database or user, and every database or user owned by that database or user now and in the future.

For compatibility, ANSI SQL requires the use of FROM.

ALL username is a Teradata RDBMS extension to ANSI SQL. PUBLIC

Specifies that the privileges are to be revoked from all currently defined and future users of the Teradata RDBMS. ALL DBC is equivalent to PUBLIC.

Teradata RDBMS for UNIX SQL Reference

8-235

Teradata SQL Syntax Guide REVOKE Statement (MONITOR)

REVOKE Statement (MONITOR) Function

8

Syntax

8

The MONITOR form rescinds system-wide performance monitoring privileges.

8

MONITOR

REVOKE

TO

GRANT OPTION FOR

A

PRIVILEGES , BUT NOT

ABORTSESSION MONRESOURCE MONSESSION SETRESRATE SETSESSRATE

, ABORTSESSION MONRESOURCE MONSESSION SETRESRATE SETSESSRATE , A

username

TO FROM

ALL PUBLIC

FF07A062

where: Syntax Element ...

Description

GRANT OPTION FOR

When GRANT OPTION FOR is specified, then only the grant authority is removed from the specified rights for the specified grantees. REVOKE GRANT OPTION FOR revokes the recipient’s right to grant, but does not revoke the stated privileges themselves.

MONITOR BUT NOT

8-236

Revokes all of the MONITOR privileges except those specified.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REVOKE Statement (MONITOR) Syntax Element ...

Description

MONITOR PRIVILEGES

Revokes from the specified user all privileges, except MONITOR, that may be granted on the specified object, and that are possessed WITH GRANT OPTION by the user executing the REVOKE. To revoke all of a user’s privileges, including MONITOR, the revoker must execute at least two commands, shown as follows: REVOKE ALL PRIVILEGES ON object FROM username ; REVOKE MONITOR PRIVILEGES FROM username ;

ALL PRIVILEGES means only all database privileges. If the ALL PRIVILEGES is used, then only the rights possessed with grant authority are revoked from the grantee. MONITOR PRIVILEGES indicates all monitoring privileges. ANSI SQL requires ALL to be followed by PRIVILEGES. Monitor Privileges List:

ABORTSESSION

Aborts any outstanding request or ongoing transaction of one or more Teradata RDBMS sessions and, optionally, logs off the sessions.

MONRESOURCE

Gathers information on how each AMP and PE is performing, and each processor’s availability.

MONSESSION

Gathers information about logged on sessions and overall system usage on a session-by-session basis.

SETRESRATE

Sets the frequency at which processor resource usage data is updated in the Teradata RDBMS.

SETSESSRATE

Sets the frequency at which session-level performance data is updated in the Teradata RDBMS. Any combination of privileges can be revoked by a user who has those privileges with GRANT option.

Privileges Required

8

For REVOKE (SQL), the user submitting a REVOKE statement must be the owner of the object, or must have all of the privileges that are to be revoked, on the object, with GRANT option. If the object is a view or macro, the submitting user also must have the applicable privileges, WITH GRANT OPTION, on the objects referenced by the view or macro. For REVOKE (Monitor), the user submitting a REVOKE statement must have all the privileges that are to be revoked with GRANT option.

Teradata RDBMS for UNIX SQL Reference

8-237

Teradata SQL Syntax Guide REVOKE Statement (MONITOR)

Rules for Using REVOKE

8

It is illegal to specify an ON object clause in a REVOKE MONITOR statement. It is illegal not to specify an ON object clause in any other REVOKE statement. If you violate either one of these restrictions, a failure response will occur. REVOKE takes effect immediately when the revokee issues his next command.

To REVOKE Statement and REVOKE Monitor, Execute Both Statements

ALL PRIVILEGES

Which Privileges Can be Revoked?

The SQL and MONITOR forms of REVOKE are separate statements. To revoke all a user’s privileges including MONITOR, the grantor must execute both statements, thus: REVOKE ALL PRIVILEGES ON FROM ; REVOKE MONITOR PRIVILEGES FROM ;

8

8

ALL PRIVILEGES means that only all database-related privileges are affected. MONITOR PRIVILEGES indicates that all monitoring privileges are affected. Ownership privileges cannot be revoked.

8

If a REVOKE statement removes privileges that were granted at the database or user level, the privileges are dropped for all objects, regardless of when they were created. A REVOKE statement at the object level cannot remove from that object a privilege that was granted at the database or user level. A REVOKE of the GRANT OPTION for a privilege immediately rescinds the right to grant that privilege. However, it does not affect privileges that have already been granted by the target user to other users. ●



Revoking Column Level Rights

8-238

8

If a user receives the same privilege from two or more grantors, any grantor who has the necessary privileges may revoke that privilege from the user, and from other grantees; one who revokes a privilege from another need not be the grantor of that privilege. If a privilege that was granted to “ALL name” is revoked from “name,” the privilege is not automatically granted to future users who are owned by “name.”

Revocation of a column level right is only allowed if there is a row in the access rights table for the columns on which the right is to be revoked. This means that if the user has UPDATE or REFERENCES right at the table level, revoking the right on individual columns is not allowed.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REVOKE Statement (MONITOR)

Examples Example 1

The following examples illustrate the use of REVOKE: 8

8

The following statement revokes from UserA any access to any object in the Personnel database: REVOKE ALL PRIVILEGES ON Personnel FROM UserA ;

Example 2

8

The following statement may be used to leave UserA with at least the SELECT privilege on the Department table: REVOKE ALL BUT SELECT ON Personnel.Department FROM UserA;

Example 3

8

The following statement leaves UserA with the SELECT privilege on every object in the Personnel database: REVOKE ALL BUT SELECT ON Personnel FROM UserA ;

Teradata RDBMS for UNIX SQL Reference

8-239

Teradata SQL Syntax Guide REVOKE LOGON

REVOKE LOGON

8

The REVOKE LOGON statement retracts permission to log on to the Teradata RDBMS from one or more specific client systems. It also may be used to change the current system defaults.

Function

This statement can be submitted only by the system administrator or by a user to whom the system administrator has granted the EXECUTE privilege on the DBC.LogonRule macro. REVOKE LOGON is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

,

8

REVOKE LOGON

ON

hostid

AS DEFAULT ,

ALL TO

;

dbname

FROM FF07A036

where: Syntax Element ...

Specifies . . .

hostid

an integer which identifies a mainframe channel connection or a LAN connection that is currently defined to the Teradata RDBMS by the hardware configuration data. The interface need not be operational. The value for the Teradata RDBMS console is 0 (zero). For any other connector, the hostid is a valued from 1 to 1023.

ALL

any source through which a logon is attempted, including the Teradata RDBMS console.

AS DEFAULT

that the current default for the specified hostid(s) is to be changed, without residual conditions, as defined in this REVOKE LOGON statement. A statement with AS DEFAULT has no effect on the access revoked from or granted to particular usernames. A statement that sets the default for a specific hostid takes precedence over a statement that sets the default for ALL client systems.

8-240

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide REVOKE LOGON Syntax Element ...

Specifies . . .

TO FROM

keywords introduced to override the current default for the specified dbname(s) (or username(s)) on the specified hostid(s).

dbname list

the name DBC cannot be specified as a dbname (username) in a REVOKE LOGON statement (see Usage Notes). A statement that includes this name returns an error message. The product of the number of hostids times the number of dbnames (usernames) cannot exceed 25.

Default Logon Permissions

When the Teradata RDBMS is connected to multiple client systems, the initial default is that logon permission is granted to all users from all hostids, and that all logons must include a password. ●



Privileges Required

New Control Record Created WIth Each REVOKE LOGON

8

8

The GRANT LOGON and REVOKE LOGON statements control which users have access from which client system connections. A REVOKE LOGON statement inhibits only future logon attempts; it does not affect users who are currently logged on.

When a REVOKE LOGON statement is submitted, the system checks that the requesting user has EXECUTE privilege on the system macro, DBC.LogonRule. However, no checks are made on whether the dbnames or usernames defined in the statement apply to users owned by the requesting user. If the submitted statement cannot be verified because it specifies an invalid dbname (username) or an invalid hostid, no action is taken on the statement. When a REVOKE LOGON statement is entered for one or more dbnames or usernames, a logon control record is created for each dbname (username)/hostid pair specified. Any existing control record for a particular pair is replaced. The logon control record(s) created for a particular dbname (username) will stay in existence until that user is dropped (see “DROP DATABASE, DROP USER” statement).

Teradata RDBMS for UNIX SQL Reference

8-241

Teradata SQL Syntax Guide REVOKE LOGON

REVOKE LOGON for DBC Gives an Error

AS DEFAULT

8-242

8

8

Any attempt to REVOKE LOGON for username DBC will result in an error. If an attempt is made to log on to the Teradata RDBMS with username DBC and the correct password is submitted, the logon is accepted regardless of the current default for the applicable hostid. This prevents any opportunity to lock out all hosts from user DBC. A statement that includes the AS DEFAULT option has no effect on the logon access granted to or revoked from specific dbnames or usernames. Therefore, a user named in a REVOKE LOGON statement cannot access the applicable client system even if that client system has a default of GRANT, and a user named in a GRANT LOGON statement can always access the applicable client even if that client has a default of REVOKE.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide ROLLBACK

ROLLBACK Function Syntax

8

The ROLLBACK statement terminates the current transaction.

ROLLBACK WORK

quotestring

FROM option

WHERE option

; FF07A037

where: Syntax Element ...

Description

WORK

ROLLBACK is flagged as non-entry level ANSI SQL if WORK is not present and the SQL flagger is enabled.

quotestring

Specifies the text of the message to be returned when the transaction is terminated. This option is not ANSI SQL and is flagged if the SQL flagger is enabled.

FROM option

This may be required if the WHERE clause includes subqueries. The contents of the FROM option are described in Chapter 7, “Queries: The SELECT Statement,” under the “FROM List”. This option is not ANSI SQL and is flagged if the SQL flagger is enabled.

WHERE clause

An optional clause that introduces a conditional expression. The expression may specify an aggregate operation (see Usage Notes) under ABORT. If the WHERE clause is omitted, termination is unconditional. See also Chapter 7, “Queries: The SELECT Statement,” “WHERE Clause” The WHERE condition specifies an expression whose result must be true if termination is to occur. If the result is false, transaction processing continues. This option is not ANSI SQL and is flagged if the SQL flagger is enabled.

Teradata RDBMS for UNIX SQL Reference

8-243

Teradata SQL Syntax Guide ROLLBACK

How ROLLBACK Works

The ROLLBACK statement performs the following actions: 8

Stage

Process

1

Backs out changes made to the database as a result of the transaction.

2

Deletes spooled output for the request.

3

Releases locks associated with the transaction.

4

If the transaction is in the form of a macro or a multi-statement request, bypasses execution of the remaining statements.

5

Returns a failure response to the user.

Refer also to the “ABORT” statement.

ROLLBACK With BTEQ

8-244

8

If you use ROLLBACK in a BTEQ script with either the .SESSION or the .REPEAT command, you must send the ROLLBACK statement along with the repeated SQL statement as one request. If you send the repeated request without the ROLLBACK, one of the requests is eventually blocked by other sessions and the job will hang because of a deadlock.

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide SELECT

SELECT

8

The SELECT statement returns row data in the form of a result table. Refer to Chapter 7, “Queries: The SELECT Statement,” for more detail.

Teradata RDBMS for UNIX SQL Reference

8-245

Teradata SQL Syntax Guide SET SESSION COLLATION

SET SESSION COLLATION

8

The SET SESSION COLLATION statement overrides the collation currently in effect for the session.

Function

SET SESSION is flagged as non-ANSI, when the SQL flagger is enabled.

Syntax

SET SESSION

COLLATION

ASCII

SS

EBCDIC

;

MULTINATIONAL HOST FF07A038

where: Syntax Element ...

Specifies that. . .

ASCII

comparison and sort operations use ASCII collation.

EBCDIC

comparison and sort operations use EBCDIC collation.

MULTINATIONAL

comparison and sort operations use a European (diacritical) character or Kanji character sort sequence (see Usage Notes).

HOST

collation for the session agrees with the collation of the logon client system. This is the default.

SET SESSION Is Not Valid for 2PC Session Collation.

Defining Collation

SET SESSION is not allowed in 2PC sessions. 8

8

The collation for a session determines the ordering of data characters during comparison operations, and when sorting data in response to a SELECT request that includes a WITH...BY or ORDER BY clause. COLLATION can be defined as an attribute of the user (see also “CREATE USER” and “MODIFY USER”). If the attribute is not defined, then collation for the session defaults to that of the logon client system.

8-246

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide SET SESSION COLLATION

The SET SESSION COLLATION statement defines collation after a session is started. Use the HELP SESSION statement to determine the default collation in effect for the current session.

MULTINATIONAL

8

The MULTINATIONAL option is useful only on a European Feature Site or a Japanese character site. MULTINATIONAL sets the collation for the session to an international sort sequence that is compatible with either a diacritical character set or a Japanese character set, as described under the ORDER BY clause of the SELECT statement. Each international sort sequence is defined by the database administrator (and no check is made to ensure that collation is compatible with the character set of the current session) but multibyte character collation sequences cannot be changed.

For Sorting and Comparison, Characters Converted to Uppercase Character Data Sorted in Ascending Order by Default Examples Example 1

8

8

8

The Teradata RDBMS converts data characters to their uppercase values for comparison and sorting operations unless the CASESPECIFIC option is included in the SQL request, or was defined at creation time for the column being queried. Character data is sorted in ascending order unless the DESC (descending) option is included in the SQL request. The results of CASESPECIFIC on sorted results is discussed under the ORDER BY clause of the SELECT statement, in Chapter 7. The following examples illustrate the use of SET SESSION COLLATION: A session can be set to ASCII collation as follows:

8

SET SESSION COLLATION ASCII ;

or SS COLLATION ASCII ;

Teradata RDBMS for UNIX SQL Reference

8-247

Teradata SQL Syntax Guide SET SESSION COLLATION

Example 2

8

Assuming that JobRept contains alphanumeric data, a SELECT that requests a sorted return, as follows: SELECT EmpNo,JobId FROM JobRept ORDER BY JobId ;

produces, in ascending order: EmpNo -----10201 10201 10201 10004 10016 10004 10003 10001

Example 3

JobId -------1001-AP2 1032-AR3 1031-AR2 ENG-0002 ENG-0002 ENG-0003 OE1-0001 PAY-0002

If the session is then set to EBCDIC collation: 8

SET SESSION COLLATION EBCDIC ;

or SS COLLATION EBCDIC ;

the same request returns: EmpNo -----10004 10016 10004 10003 10001 10201 10201 10201

Example 4

JobId -------ENG-000 ENG-0002 ENG-0003 OE1-0001 PAY-0002 1001-AP2 1032-AR3 1031-AR2

If the descending option is added, as follows: 8

SELECT EmpNo,JobId FROM JobRept ORDER BY JobId DESC ;

the return is: EmpNo -----10201 10201 10201 10001 10003 10004 10004 10016

8-248

JobId -------1031-AR2 1032-AR3 1001-AP2 PAY-0002 OE1-0001 ENG-0003 ENG-0002 ENG-0002

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide SHOW MACRO SHOW TABLE SHOW VIEW

SHOW MACRO SHOW TABLE SHOW VIEW Function

8

The SHOW MACRO, SHOW TABLE, and SHOW VIEW statement displays the data definition statement most recently used to create, replace, or modify the specified MACRO, TABLE. or VIEW. SHOW MACRO, TABLE, or VIEW is flagged as non-ANSI by the SQL flagger when the flagger is enabled.

Syntax

SHOW

MACRO TABLE VIEW

macroname tname

;

viewname

FF07A039

where: Syntax element . . .

Displays . . .

macro_name

the most recently executed CREATE MACRO or REPLACE MACRO statement for the named macro.

tname

a standardized CREATE TABLE statement that would create the referenced table. If the table has been modified using the ALTER TABLE statement, or CREATE INDEX statement, modifications are reflected in the CREATE TABLE statement displayed SHOW TABLE displays all index and constraint information for the table. There is an upper limit of 31,744 characters that SHOW TABLE can display. Since a standard display form is used, the result will generally not be identical to the text used to create the table. Its form is such that if the table were dropped and created using the SHOW TABLE output, the result would be a table with structure identical to the one shown

view_name

the most recently executed CREATE VIEW or REPLACE VIEW statement for the named view.

Teradata RDBMS for UNIX SQL Reference

8-249

Teradata SQL Syntax Guide SHOW MACRO SHOW TABLE SHOW VIEW

Privileges Required

To use SHOW MACRO, TABLE, or VIEW, a user must either: 8





Is Data Secure?

If DATABLOCKSIZE or FREESPACE Has Been Modified

Examples Example 1

8

8

8

8

have a privilege on the macro, table, or view, or a privilege on the database containing it, or have SELECT privilege on table RDBMS.TVM (used by database administrators only)

Even database administrators cannot see or alter contents of objects for which they do not have relevant rights, hence the data remains secure. If the DATABLOCKSIZE or FREESPACE (or both) of the table has been modified using the ALTER TABLE or CREATE TABLE statement, the SHOW TABLE command results in the currently defined data block size or percent freespace (or both) being listed in the response. If the data block size or freespace value is not listed, the default value is in effect. See the description of the “ALTER TABLE” or “CREATE TABLE” statement. The following examples illustrate the use of SHOW MACRO, TABLE, or VIEW: The following statement displays the most recently executed definition of the NewEmp macro: SHOW MACRO NewEmp;

The Teradata RDBMS displays: REPLACE MACRO NewEmp( name CHAR(20) NOT NULL, street CHAR(30), city CHAR(20), number INTEGER, dept SMALLINT DEFAULT 999) AS ( INSERT INTO Employee (Name, Street, City, Empno, DeptNo) VALUES (:name, :street, :city, :number, :dept) ; UPDATE Department SET EmpCount = EmpCount + 1 WHERE DeptNo = :dept ; ) ;

Example 2

8

If a SHOW TABLE statement is entered for a table that has been modified, the original CREATE TABLE statement, including all current modifications, is displayed. For example, consider the following statements used to create and subsequently modify the EmpBonus table: CT Personnel.EmpBonus (EmpNo SMALLINT FORMAT ’9(5)’CHECK (EmpNo BETWEEN 10001 AND 32001) NOT NULL, BonusNo SMALLINT FORMAT ’Z9’CHECK (BonusNo BETWEEN 0 and 99)NOT NULL, BonusAmt DECIMAL (6,2) CHECK (BonusAmt BETWEEN 1.00 AND 5000.00) UNIQUE PRIMARY INDEX (EmpNo) ;

8-250

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide SHOW MACRO SHOW TABLE SHOW VIEW

ALTER TABLE EmpBonus ADD DeptNo SMALLINT ;

Now, when a SHOW TABLE statement is entered for the EmpBonus table, for example, SHOW TABLE EmpBonus;

the following CREATE TABLE statement is returned: CREATE SET TABLE Personnel.EmpBonus, FALLBACK (EmpNo SMALLINT FORMAT ’9(5)’ CHECK (EmpNo BETWEEN 10001 AND 32001)NOT NULL, BonusNo SMALLINT FORMAT ’Z9’ CHECK (BonusNo BETWEEN 0 AND 99) NOT NULL, BonusAmt DECIMAL (6,2) CHECK (BonusAmt BETWEEN 1.00 AND 5000.00), DeptNo SMALLINT) UNIQUE PRIMARY INDEX (EmpNo);

The DeptNo column is included in the CREATE TABLE statement. In addition, default values are displayed (for example, FALLBACK).

Example 3: Column Level Named Constraints

The statement in this example names constraints at the column level. 8

CREATE TABLE good_1 ( field_1 INTEGER NOT NULL CONSTRAINT primary_1 PRIMARY KEY, field_2 INTEGER NOT NULL CONSTRAINT unique_1 UNIQUE, field_3 INTEGER CONSTRAINT check_1 CHECK (field_3 > 0), field_4 INTEGER CONSTRAINT reference_1 REFERENCES parent_1 ) ; SHOW TABLE good_1;

results in a display: CREATE SET TABLE Personnel.good_1, NO FALLBACK, NO BEFORE JOURNAL NO AFTER JOURNAL ( field_1 INTEGER NOT NULL, field_2 INTEGER NOT NULL, field_3 INTEGER, field_4 INTEGER, CONSTRAINT check_1 CHECK (field_3 >0), CONSTRAINT reference_1 FOREIGN KEY (field_4) REFERENCES Personnel.PARENT_1 (f2)) UNIQUE PRIMARY INDEX primary_1 (field_1) UNIQUE INDEX unique_1 (field_2);

Teradata RDBMS for UNIX SQL Reference

8-251

Teradata SQL Syntax Guide SHOW MACRO SHOW TABLE SHOW VIEW

Notes on the standard form of display: ●





SHOW TABLE always indicates whether the table was created with SET (no duplicate rows), SET mode or MULTISET (duplicate rows if no uniqueness defined). SHOW TABLE displays fallback and journal status of the table. Named constraints become either table level constraints or are mapped to unique indexes. The display reflects the implementation. In this case, the named constraints on field_1 and field_2 are shown as table level constraints in the display, after the list of fields. The named constraints on field_3 and field_4 become unique indexes. Since a PRIMARY KEY and no PRIMARY INDEX was declared in the CREATE TABLE, the PRIMARY KEY is implemented in the form of a UNIQUE PRIMARY INDEX, and is so listed. The UNIQUE constraint is implemented in the form of a UNIQUE INDEX and is so listed.

Example 4: REFERENCES Constraint

8

Here, Table T1 is created with a referential constraint which specifies that Foreign Key columns (f1) reference the unique primary index columns in table Good_1. CREATE TABLE T1 (f1 integer NOT NULL f2 integer) PRIMARY INDEX (f1) FOREIGN KEY (f1) REFERENCES Good_1; SHOW TABLE T1;

results in display: CREATE SET TABLE T1, NO FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL (f1 integer NOT NULL, f2 integer) PRIMARY INDEX (f1) FOREIGN KEY (f1) REFERENCES Personnel.Good_1(field_1);

8-252

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide SHOW MACRO SHOW TABLE SHOW VIEW

Example 5: SHOW VIEW

8

The following SHOW VIEW example begins with the creation of the view, then progresses to the SHOW VIEW statement and finally presents the result of the report request. CREATE VIEW staff_info (number, name, position, department, sex, dob) AS SELECT employee.empno, name, jobtitle, deptno, sex, dob FROM Employee WHERE jobtitle NOT IN (’Vice Pres’, ’Manager’) WITH CHECK OPTION ;

When the following is entered: SHOW VIEW staff_info;

the following is displayed: CREATE VIEW staff_info (number, name, position, department, sex, dob) AS SELECT employee.empno, name, jobtitle, deptno, sex, dob FROM Employee WHERE jobtitle NOT IN (’Vice Pres’, ’Manager’) WITH CHECK OPTION ;

Teradata RDBMS for UNIX SQL Reference

8-253

Teradata SQL Syntax Guide UPDATE

UPDATE

8

The UPDATE statement changes field values in existing rows of a table.

Function Syntax

UPDATE

tname

A ,

aname

UPD AS

FROM

tname aname AS

, A

SET

col_name = expr ALL

WHERE

cond

FF07A040

where: Syntax Element ...

Specifies . . .

tname

the name of the table to be updated, or the name of a view through which the table is accessed.

AS

an optional keyword introducing aname.

aname

the alias name of a temporary table whose rows are to be updated during a self-join of an actual table (tname). The aname option is a Teradata RDBMS extension to ANSI SQL.

FROM

a keyword introducing the names of one or more tables or view from which expr is to be derived. The FROM syntax is a Teradata RDBMS extension to ANSI SQL.

tname

the name of a table, view or macro. If database of omitted, it is inferred from context.

AS

an optional keyword introducing the aname.

aname

a different, temporary name (alias) for the table that is referenced by tname. aname must be used during a self-join operation on the table.

SET

the names of one or more columns whose data is to be updated, and the expressions that are used for update.

col_name

the column name. The col_name field is for the columnname only; forms such as Databasename.Tablename.Columnname, or Tablename.Columnname should not be used.

8-254

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Syntax Guide UPDATE Syntax Element ...

Specifies . . .

expr

a new value to be placed into the column to be updated. expr may include a constant, a null value (expressed by the reserved word NULL), or an arithmetic expression for calculating the new value. Values in a targeted row before the update may be referenced in an expression.

WHERE

a conditional clause. For more information see Chapter 7, “Queries: The SELECT Statement,” “WHERE Clause”. If WHERE is used, the user must have SELECT access on searched objects.

cond

the conditional expression to be used for determining rows whose values are to be updated. The conditional clause may reference more than one table, or specify an embedded select operation.

ALL

Indicates that all rows in the specified table are to be updated. The ALL option is a Teradata RDBMS extension to ANSI SQL.

Privileges Required

8

To update a table, a user must have the UPDATE privilege on the table or columns to be updated. When executing UPDATE and it requires READ access to an object, the user must have the SELECT right to data being accessed. For example: UPDATE t1 SET f1=1 WHERE f1 1;



Invalid in all releases. ABORT FROM t1 WHERE t1.x1=t2.x2;

9-16

Teradata RDBMS for UNIX SQL Reference

Advanced SQL SELECT COUNT(*)

SELECT COUNT(*)

9

To select the names of the publishers whose book count values in the inventory match the actual count of books, the following query can be used in releases prior to V2R2.0: SELECT pubname, BookCount FROM inventory WHERE (BookCount, pubnum) IN (SELECT count(*), pubnum FROM book GROUP BY pubnum);

But if the book count value of a publisher in the inventory is zero, this publisher name will not be returned even when no books are found for this publisher. This is because no row is returned by the subquery for this publisher. In the current release, the following query with correlated subquery will return the correct answer in this case. SELECT pubname, BookCount FROM inventory WHERE BookCount IN (SELECT count(*) FROM book WHERE book.pubnum=library.pubnum)

Teradata RDBMS for UNIX SQL Reference

9-17

Advanced SQL Referential Integrity

Referential Integrity Introduction

Referential integrity is defined as all the following: 9







The concept of relationships between tables, based on the definition of a primary key and a foreign key. A mechanism that provides for specification of columns within a referencing table that are foreign keys for columns in some other referenced table. Referenced columns must be defined as one of the following: ● Primary key columns ● Unique columns A reliable mechanism which prevents accidental database corruptions when doing inserts, updates, and deletes.

Referential integrity states that a row cannot exist in a table with a non-null value for a referencing column if an equal value does not exist in a referenced column.

Referencing (Child) Table

9

The referencing table is referred to as the Child table, and the specified Child table columns are the referencing columns. Note: Referencing columns must have the same numbers and types of columns, data types and sensitivity as the referenced table keys. COMPRESS is not allowed on either the referenced or referencing column(s) and column level constraints are not compared.

Referenced (Parent) Table

9

A Child table must have a parent table, and the referenced table is referred to as the Parent table. The parent key columns that exist in the Parent table are the referenced columns. Since the referenced columns are defined as unique constraints, the referenced columns must be one of the following: ● ●

9-18

A unique primary index (UPI), not null A unique secondary index (USI), not null

Teradata RDBMS for UNIX SQL Reference

9

Advanced SQL Referential Integrity

Terms Related to Referential Integrity

The following terms are used to explain the Referential Integrity concept: 9

Term

Definition

Child Table

A table, where the referential constraints are defined. A Child table is synonymous with the referencing table.

Parent Table

The table being referenced by a Child table. A Parent table is synonymous with the referenced table.

Primary Key

A primary key uniquely identifies a row of a table.

Foreign Key

A foreign key refers to column(s) in the Child table. A foreign key may consist of up to sixteen different columns

Referential Constraint

The constraint defined on a column or a table to ensure Referential Integrity. For example: CREATE TABLE A (A1 Char(10) REFERENCES B (B1), A2 Integer) PRIMARY INDEX (A1) FOREIGN KEY (A1,A2) REFERENCES C;

This CREATE TABLE statement specifies the following integrity constraints: This constraint . . .

Is this level . . .

1

column. Foreign Key A1 references the Parent Key B1 in table B.

2

table. Composite Foreign Key (A1, A2) references the Parent’s UNIQUE PRIMARY index which must be two columns of type CHAR(10), type INTEGER, and not null.

Teradata RDBMS for UNIX SQL Reference

9-19

Advanced SQL Referential Integrity

Why is Referential Integrity important?

Consider the employee and payroll table. 9

With Referential Integrity, the two tables work together as one. When one table gets updated, the other table should get updated too. The following depicts a new Referential Integrity scenario: Looking for a better career, Mr. Clark Johnson leaves his company. Clark Johnson is deleted from the employee table. The payroll table, however, does not get updated because the payroll clerk simply forgets to do so. Consequently, Mr. Clark Johnson keeps getting paid. Referential integrity would link these two tables, and, depending on the constants defined, the delete from the employee table could not be done until accompanied by a delete from the payroll table. Besides data integrity and data consistency, Referential Integrity has these benefits: Benefit

Increases development productivity

Description

It is not necessary to code SQL statements to enforce referential constraints. The Teradata RDBMS automatically enforces Referential Integrity.

Requires fewer programs to be written

All update activities are programmed to ensure that referential constraints are not violated. The Teradata RDBMS enforces Referential Integrity in all environments, no additional programs are required.

Improves performance

Rules for Assigning Columns as FOREIGN KEYS

9

The FOREIGN KEY columns in the referencing table must be identical in definition with the keys in the referenced table. Corresponding columns must have the same data type and case sensitivity only. ●



9-20

The Teradata RDBMS chooses the most efficient method to enforce the referential constraints.

The Teradata RDBMS COMPRESS option is not allowed on either the referenced or referencing column(s). Column level constraints will not be compared.

Teradata RDBMS for UNIX SQL Reference

Advanced SQL Referential Integrity ●

Circular References Are Allowed

9

A one column FOREIGN KEY cannot reference a single column in a multi-column primary or unique key; the foreign and primary/unique key must contain the same number of columns.

References can be defined as circular in that TableA can reference TableB which can reference TableA. In this case, at least one set of FOREIGN KEYS must be defined on columns which are nullable. If the FOREIGN KEYS in TableA are on columns defined as nullable, then rows could be inserted into TableA with null values for the FOREIGN KEY columns. Once the appropriate rows exist in TableB, the null values of the FOREIGN KEY columns in TableA could then be updated to contain non-null values which match the TableB values.

References Can Be to the Table Itself

CREATE and ALTER TABLE Syntax

Maintaining Foreign Keys

9

9

9

FOREIGN KEY references can also be to the same table that contains the FOREIGN KEY. The referenced columns must be different columns than the FOREIGN KEY, and both the referenced and referencing columns must subscribe to the referential integrity rules. The addition of Referential Integrity affects the syntax and semantics of CREATE TABLE and ALTER TABLE. Refer to these sections in Chapter 8, “Teradata SQL Syntax Guide” for more details. Definition of a FOREIGN KEY requires that the Teradata RDBMS maintain the integrity defined between the referenced and referencing table. Teradata RDBMS verifies that if: ●

A Row is Inserted into Referencing Table & FOREIGN KEY Columns are Not NULL: ● that a row exists in the referenced table with the same values as those in the FOREIGN KEY columns. ● if such a row does not exist, then the Teradata RDBMS returns an error to the attempt to insert the row. Note: If the FOREIGN KEY contains multiple columns, and if any one column value of the FOREIGN KEY is null, then none of the FOREIGN KEY values are validated.



The Value(s) in FOREIGN KEY Column(s) are Updated to NonNULL: ● then a row must exist in the referenced table which contains value(s) equal to the modified value(s) of all of the foreign key column(s).

Teradata RDBMS for UNIX SQL Reference

9-21

Advanced SQL Referential Integrity

If such a row does not exist then the Teradata RDBMS will return an error to the attempt to update the row. Before a Row is Deleted from a Referenced Table: ● that no rows exist in referencing tables with foreign key values equal to those of the row to be deleted. ● If such rows exist then an error will be returned to the attempt to delete the row. Before a Referenced Column in a Referenced Table is Updated: ● that no rows exist in a referencing table with foreign key values equal to those of the referenced columns. ● If such rows exist then an error will be returned to the attempt to update the row. The Structure of Column Defined as FOREIGN KEYS or Referenced by FOREIGN KEYS: ● is not changed so as to violate the rules for definition of a foreign key constraint. ● An ALTER TABLE or DROP INDEX statement attempting to change such a columns structure will cause an error to be returned by the Teradata RDBMS. A table which is referenced by another is not dropped. ●









Before dropping the referenced table, an ALTER TABLE statement must be executed against the referencing table, to drop the FOREIGN KEY reference. ●

If an ALTER TABLE Statement is Executed to Add a FOREIGN KEY Reference: All of the values in the foreign key columns are validated against columns in the referenced table. ● ●





9-22

An error table is generated when ALTER TABLE is parsed. The error table is defined with the same columns and PRIMARY KEY as the target table of the ALTER TABLE statement. The name of the error table is the target table name suffixed with the reference index number. A reference index number is assigned to each foreign key constraint for a table. The number can be determined through use of one of the following system views: ● RI_Child_Tables ● RI_Distinct_Children ● RI_Distinct_Parents ● RI_Parent_Tables

Teradata RDBMS for UNIX SQL Reference

Advanced SQL Referential Integrity ●

The error table is created under the same user/database as the table being altered. If a table already exists with the same name as that generated for the error table then an error is returned to the Alter Table statement. The Teradata RDBMS does not return an error if rows in the referencing table contain values in the foreign key columns which cannot be found in any row of the referenced table. Instead, a row is inserted into the error table for each such row found in the target table. Correction of data values in the referenced or referencing tables so that full referential integrity exists between the two tables is the responsibility of the user. The rows in the error table can be used to define which corrections must be made. Maintenance of the error table is also the responsibility of the user.

Archive and Restore of Tables

9

The Archive (ARC) utility provides for the archive and restore of individual tables. It also provides for copying tables from one Teradata RDBMS to another. When a table is restored or copied into a Teradata RDBMS, the dictionary definition of that table is also restored. The dictionary definitions of both the referenced and referencing table contain the complete definition of a reference. By restoring a single table, it is possible to create an inconsistent reference definition in the Teradata RDBMS. When a table is restored which is either a referenced or referencing table, the reference is marked in the dictionary definitions as inconsistent. A new function in the ARC utility can be used to validate these references once the restore is done. Refer to the Teradata Archive/Recovery for Channel-Attached Systems and the Teradata ASF2 Reference manuals. While a table is marked as inconsistent, no updates inserts or deletes are permitted. The table will be fully usable only when the inconsistencies are resolved. It is possible that the user either intends to or must revert to a definition of a table which results in an inconsistent reference on that table. There is a special option of the ALTER TABLE statement which can be used to remove these inconsistent references from a table. The option is: ALTER TABLE [dbname.]tname DROP INCONSISTENT REFERENCES [;]

Teradata RDBMS for UNIX SQL Reference

9-23

Advanced SQL Referential Integrity

The user executing the statement must have DROP rights on the target table of the statement. The result of this statement is to remove those internal indexes used to establish references which contain inconsistencies. The base data of the table is not modified.

FastLoad and MultiLoad

9

Foreign key references are not supported for any table which is the target table for a FastLoad or MultiLoad. For further details, refer to: ● ● ●

9-24

Teradata RDBMS for UNIX Database Design & Administration Teradata Fastload Reference Teradata Multiload Reference

Teradata RDBMS for UNIX SQL Reference

Advanced SQL Updatable Cursors

Updatable Cursors Cursors

9

9

Cursors are used by the Preprocessor point to the first row accessed by a SQL query. The Preprocessor then increments the cursor as needed. The Preprocessor translates and compiles application programs written in C, COBOL, and PL/I containing embedded Teradata SQL. The Preprocessor output is an executable version of the C, COBOL, and PL/I programs. This output is then also linked to the CLIv2 (Call Level Interface Version 2) program for translation for the Teradata RDBMS.

Updatable Cursors

9

Several features enable ANSI standard updatable cursor functionality: ●





FOR CURSOR option in the SELECT statement. This option is not accessible by the user. WHERE CURRENT OF [cursor name] option in DELETE and UPDATE statements CHECKSUM locking mode option in LOCKING modifier

Refer to the Teradata Application Programming with Embedded Teradata SQL for C, Cobol, and PL/I, for details on implementing these options. Note: The updatable cursor options are not applicable to and cannot be entered via BTEQ. They are only applicable for entry via the Preprocessor.

Teradata RDBMS for UNIX SQL Reference

9-25

Advanced SQL EXPLAIN Modifier: Examples of Complex Queries

EXPLAIN Modifier: Examples of Complex Queries Introduction

9

9

The EXPLAIN modifier terms are defined in Chapter 8 under “EXPLAIN Modifier”. Other examples of using EXPLAIN are provided in Teradata RDBMS for UNIX Database Design and Administration, Chapter 17, “Designing the Database for Optimum Performance”. The Personnel.Employee table has a unique primary index defined on the EmpNo column and a nonunique secondary index defined on the Name column.

Example 1: SELECT

The EXPLAIN modifier that precedes the request: 9

EXPLAIN SELECT Name, DeptNo FROM Employee WHERE EmpNo = 10009;

generates the response: Explanation ----------------------------------------------------1) First, we do a single-AMP RETRIEVE step from Personnel.Employee by way of the unique primary index “PERSONNEL.Employee.EmpNo = 10009" with no residual conditions. The estimated time for this step is 0.04 seconds. -> The row is sent directly back to the user as the result of statement 1. The total estimated time is 0.04 seconds.

Example 2: SELECT with WHERE on Non-Unique Index

Here, the WHERE condition is based on a column that is defined as a non-unique index. Note that the Teradata RDBMS places a READ lock on the table. The EXPLAIN modifier that precedes the request: 9

EXPLAIN SELECT EmpNo, DeptNo FROM Employee WHERE Name = ’Smith T’;

generates the response: Explanation -------------------------------------------------------------------1) First, we lock a distinct PERSONNEL."pseudo table" for read on a RowHash to prevent global deadlock for PERSONNEL.employee. 2) Next, we lock PERSONNEL.employee for read. 3) We do an all-AMPs RETRIEVE step from PERSONNEL.employee by way of an all-rows scan with a condition of ("PERSONNEL.employee.Name = 'Smith t'") into Spool 1, which is built locally on the AMPs. The size of Spool 1 is estimated to be 2 rows. The estimated time for this step is 0.03 seconds. -> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0 hours and 0.03 seconds.

9-26

Teradata RDBMS for UNIX SQL Reference

Advanced SQL EXPLAIN Modifier: Examples of Complex Queries

Example 3: SELECT with WHERE on Unique Secondary Index

9

Assume that the Employee table has another column (SocSecNo), and that SocSecNo is defined as a unique secondary index. If the WHERE condition is based on this column, then the EXPLAIN modifier that precedes this request: EXPLAIN SELECT Name, EmpNo FROM Employee WHERE SocSecNo = ’123456789’;

generates this response: Explanation ----------------------------------------------------1) First, we do a two-AMP RETRIEVE step from PERSONNEL.Employee by way of unique index # 20 “PERSONNEL.Employee.socSecNo = 123456789" with no residual conditions. The estimated time for this step is 0.09 seconds. -> The row is sent directly back to the user as the result of statement 1. The total estimated time is 0.09 seconds.

Example 4: SELECT With WHERE Based on a Join

9

Here, the WHERE clause defines an equality constraint that governs a join. The rows of the Department table are copied to a spool file for use in the join operation. An EXPLAIN modifier preceding the request, as follows: EXPLAIN SELECT DeptName, Name FROM Employee, Department WHERE Employee.EmpNo = Department.MgrNo ;

produces the following response: Explanation -------------------------------------------------------------------1) First, we lock a distinct PERSONNEL."pseudo table" for read on a RowHash to prevent global deadlock for PERSONNEL.department. 2) Next, we lock a distinct PERSONNEL."pseudo table" for read on a RowHash to prevent global deadlock for PERSONNEL.employee. 3) We lock PERSONNEL.department for read, and we lock PERSONNEL.employee for read. 4) We do an all-AMPs RETRIEVE step from PERSONNEL.department by way of an all-rows scan with no residual conditions into Spool 2, which is redistributed by hash code to all AMPs. Then we do a SORT to order Spool 2 by row hash. The size of Spool 2 is estimated to be 8 rows. The estimated time for this step is 0.11 seconds. 5) We do an all-AMPs JOIN step from Spool 2 (Last Use) by way of a RowHash match scan, which is joined to PERSONNEL.employee. Spool 2 and PERSONNEL.employee are joined using a merge join, with a join condition of ("PERSONNEL.employee.EmpNo = Spool_2.MgrNo"). The result goes into Spool 1, which is built locally on the AMPs. The size of Spool 1 is estimated to be 8 rows. The estimated time for this step is 0.07 seconds. -> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0 hours and 0.19 seconds.

Teradata RDBMS for UNIX SQL Reference

9-27

Advanced SQL EXPLAIN Modifier: Examples of Complex Queries

Example 5: SELECT With WHERE Based on Subquery

Here, the constraint that governs the join is defined by a subquery, and the ORDER BY clause specifies a sorted result. If the request is preceded by the following EXPLAIN: 9

EXPLAIN SELECT Name, EmpNo FROM Employee WHERE EmpNo IN (SELECT EmpNo FROM Charges) ORDER BY Name ;

The result returned is: Explanation -------------------------------------------------------------------1) First, we lock a distinct PERSONNEL."pseudo table" for read on a RowHash to prevent global deadlock for PERSONNEL.charges. 2) Next, we lock a distinct PERSONNEL."pseudo table" for read on a RowHash to prevent global deadlock for PERSONNEL.employee. 3) We lock PERSONNEL.charges for read, and we lock PERSONNEL.employee for read. 4) We do an all-AMPs RETRIEVE step from PERSONNEL.charges by way of an all-rows scan with no residual conditions into Spool 2, which is redistributed by hash code to all AMPs. Then we do a SORT to order Spool 2 by row hash and the sort key in spool field1 eliminating duplicate rows. The size of Spool 2 is estimated to be 12 rows. The estimated time for this step is 0.15 seconds. 5) We do an all-AMPs JOIN step from PERSONNEL.employee by way of an all-rows scan with no residual conditions, which is joined to Spool 2 (Last Use). PERSONNEL.employee and Spool 2 are joined using an inclusion merge join, with a join condition of ( "PERSONNEL.employee.EmpNo = Spool_2.EmpNo"). The result goes into Spool 1, which is built locally on the AMPs. Then we do a SORT to order Spool 1 by the sort key in spool field1. The size of Spool 1 is estimated to be 12 rows. The estimated time for this step is 0.07 seconds. -> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0 hours and 0.23 seconds.

Example 6: Large Table SELECT with More Complex Condition

9

Assume that a table named Main is a very large table and that its columns NumA, NumB, Kind, and Event are each defined as non-unique secondary indexes. The following request uses these indexes to apply a complex conditional expression. If the EXPLAIN modifier precedes the request, as follows: EXPLAIN SELECT COUNT(*) FROM Main WHERE NumA=’101’ AND NumB=’02’ AND Kind=’B’ AND Event=’001’;

A possible response shows that bit mapping would be used: Explanation --------------------------------------------------------1) First, we lock TESTING.Main for read. 2) Next, we do a BMSMS (bit map set manipulation) step that intersects the following row id bit maps: 1) The bit map built for TESTING.Main by way of index # 12 “TESTING.Main.Kind = ’B’”. 2) The bit map build for TESTING.Main by way of index # 8 “TESTING.Main.NumB = ’02’”. 3) The bit map built for TESTING.Main by way of index

9-28

Teradata RDBMS for UNIX SQL Reference

Advanced SQL EXPLAIN Modifier: Examples of Complex Queries

3)

4)

5) ->

Example 7: Implicit Multistatement Transaction, INSERT

# 16 “TESTING.Main.Event = ’001’”. The resulting bit map is placed in Spool 3. The estimated time for this step is 17.77 seconds. We do a SUM step to aggregate from TESTING.Main by way of index # 4 “TESTING.Main.NumA = ’101’” and the bit map in Spool 3 (Last Use) with a residual condition of (“(TESTING.Main.NumB = ’02’) and ((TESTING.Main.Kind = ’B’) and (TESTING.Main.Event = ’001’))”). Aggregate Intermediate Results are computed globally, then placed in Spool 2. We do an all-AMPs RETRIEVE step from Spool 2 (Last Use) by way of an all-rows scan into Spool 1, which is built locally on the AMPs. The size of Spool 1 is estimated to be 20 rows. The estimated time for this step is 0.11 seconds. Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request. The contents of Spool 1 are sent back to the user as a result of statement 1.

In the following BTEQ multi-statement request, which is treated as an implicit transaction, the statements are processed concurrently. Therefore, an EXPLAIN modifier preceding this request: 9

EXPLAIN INSERT Charges (30001, ’AP2-0004’, 890825, 45.0) ; INSERT Charges (30002, ’AP2-0004’, 890721, 12.0) ; INSERT Charges (30003, ’AP2-0004’, 890811, 2.5) ; INSERT Charges (30004, ’AP2-0004’, 890831, 37.5 ; INSERT Charges (30005, ’AP2-0004’, 890825, 11.0) ; INSERT Charges (30006, ’AP2-0004’, 890721, 24.5) ; INSERT Charges (30007, ’AP2-0004’, 890811, 40.5) ; INSERT Charges (30008, ’AP2-0004’, 890831, 32.0 ; INSERT Charges (30009, ’AP2-0004’, 890825, 41.5) ; INSERT Charges (30010, ’AP2-0004’, 890721, 22.0) ;

Teradata RDBMS for UNIX SQL Reference

9-29

Advanced SQL EXPLAIN Modifier: Examples of Complex Queries

produces the following possible response (Teradata mode): Explanation --------------------------------------------------------1) First, we execute the following steps in parallel. 1) We do an INSERT into PERSONNEL.charges. 2) We do an INSERT into PERSONNEL.charges. 3) We do an INSERT into PERSONNEL.charges. 4) We do an INSERT into PERSONNEL.charges. 5) We do an INSERT into PERSONNEL.charges. 6) We do an INSERT into PERSONNEL.charges. 7) We do an INSERT into PERSONNEL.charges. 8) We do an INSERT into PERSONNEL.charges. 9) We do an INSERT into PERSONNEL.charges. 10) We do an INSERT into PERSONNEL.charges. 2) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request. -> No rows are returned to the user as the result of statement 1. No rows are returned to the user as the result of statement 2. No rows are returned to the user as the result of statement 3. No rows are returned to the user as the result of statement 4. No rows are returned to the user as the result of statement 5. No rows are returned to the user as the result of statement 6. No rows are returned to the user as the result of statement 7. No rows are returned to the user as the result of statement 8. No rows are returned to the user as the result of statement 9. No rows are returned to the user as the result of statement 10.

Example 8: ANSI Versus Teradata Mode

This example shows the EXPLAIN differences between running the session in ANSI versus Teradata mode. 9

EXPLAIN UPDATE Employee set deptno = 650 WHERE deptno = 640;

ANSI mode produces the following result: Explanation -------------------------------------------------------------------1) First, we lock a distinct PERSONNEL."pseudo table" for write on a RowHash to prevent global deadlock for PERSONNEL.employee. 2) Next, we lock PERSONNEL.employee for write. 3) We do an all-AMPs UPDATE from PERSONNEL.employee by way of an all-rows scan with a condition of ("PERSONNEL.employee.DeptNo = 640"). -> No rows are returned to the user as the result of statement 1.

9-30

Teradata RDBMS for UNIX SQL Reference

Advanced SQL EXPLAIN Modifier: Examples of Complex Queries

Teradata mode produces the following result: Explanation -------------------------------------------------------------------1) First, we lock a distinct PERSONNEL."pseudo table" for write on a RowHash to prevent global deadlock for PERSONNEL.employee. 2) Next, we lock PERSONNEL.employee for write. 3) We do an all-AMPs UPDATE from PERSONNEL.employee by way of an all-rows scan with a condition of ("PERSONNEL.employee.DeptNo = 640"). 4) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request. -> No rows are returned to the user as the result of statement 1.

In ANSI mode the transaction is not COMMITed, therefore it is not ended, whereas in Teradata mode, no COMMIT is required to end the transaction.

Teradata RDBMS for UNIX SQL Reference

9-31

Advanced SQL EXPLAIN Modifier: Examples of Complex Queries

9-32

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Keywords

Appendix A

Teradata SQL Keywords

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Keywords

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Keywords Teradata SQL Keywords A

Teradata SQL Keywords

A

The Teradata SQL keywords, which are reserved for use in SQL statements are listed below. For abbreviations, the full spelling follows the abbreviation, in parentheses). ABORT ABORTSESSION ABS ACCOUNT ADD ADD_MONTHS AFTER ALIAS ALL ALTER AMP AND ANY AS ASC ASCII AUTHORIZATION AVE (AVERAGE) AVERAGE AVG (AVERAGE) BACKUP BEFORE BEGIN BETWEEN BOTH BT (BEGIN TRANSACTION) BUT BY BYTE BYTEINT BYTES CASE CASESPECIFIC CAST CD (CREATE DATABASE) CHAR (CHARACTERS) CHARACTER (CHARACTERS) CHAR2HEXINT CHARACTERS CHARS (CHARACTERS) CHECK CHECKPOINT CLUSTER

Teradata RDBMS for UNIX SQL Reference

A-1

Teradata SQL Keywords Teradata SQL Keywords CM (CREATE MACRO) COALESCE COLLATION COLLECT COLUMN COMMENT COMMIT COMPRESS CONSTRAINT COUNT CREATE CROSS CS (CASESPECIFIC) CT (CREATE TABLE) CURRENT CURSOR CV (CREATE VIEW) DATABLOCKSIZE DATABASE DATE DAY DEC (DECIMAL) DECIMAL DEFAULT DEL (DELETE) DELETE DESC DESCRIPTOR DIAGNOSTIC DISTINCT DOUBLE DROP DUAL DUMP EACH EBCDIC ECHO ELSE END EQ ( = ) ERRORFILES ERRORTABLES ESCAPE ET (END TRANSACTION) EXCEPT EXCL (EXCLUSIVE) EXCLUSIVE EXEC (EXECUTE) EXECUTE EXISTS EXP

A-2

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Keywords Teradata SQL Keywords EXPLAIN EXTRACT FALLBACK FASTEXPORT FIRST FLOAT FOR FOREIGN FORMAT FREESPACE FROM FULL GE GIVE GO GOTO GRANT GRAPHIC GROUP GT

( >= )

( > )

HASH HASHAMP HASHBAKAMP HASHBUCKET HASHROW HAVING HELP HOUR IMMEDIATE IN INCONSISTENT INDEX INDICATOR INNER INS (INSERT) INSERT INT (INTEGER) INTEGER INTERSECT INTO IS JOIN JOURNAL KEY LAST LE

( =’

GIVE

Non ANSI Extension

Resource Management Feature

GRAPHIC

Non ANSI Extension

Data type

GRANT

ANSI(E)

With Extensions

GRANT OPTION

Non ANSI Extension

Logon control

GRANT OPTION FOR

ANSI(I)

Option for REVOKE

GROUP BY

ANSI(E)

With extensions

GT

Non ANSI

Use ‘>’

HASHAMP

Non ANSI Extension

Row distribution function

HASHBAKAMP

Non ANSI Extension

Row distribution function

HASHBUCKET

Non ANSI Extension

Row distribution function

HASHROW

Non ANSI Extension

Row distribution function

HAVING

ANSI(E)

Query phrase

HELP

Non ANSI Extension

User information function

HOUR

ANSI(I)

See EXTRACT

IMMEDIATE

Non ANSI Extension

Data storage control feature

IN

ANSI(E)

Comparison operator

INDEX

Non ANSI Extension

Privilege for CREATE and DROP INDEX

INNER

ANSI(I)

Join type

INS

Non ANSI

Use INSERT

INSERT

ANSI(E)

Statement

INT

ANSI(E)

Data type

INTEGER

ANSI(E)

Data type

Teradata RDBMS for UNIX SQL Reference

Teradata SQL Versus ANSI SQL Teradata SQL Versus ANSI SQL: Summarized Teradata SQL Syntax or Function

ANSI SQL Equivalence

Notes

INTERSECT

ANSI(I)

Query expression operator

INTO

ANSI(E)

Optional in INSERT for Teradata RDBMS, required for ANSI.

IS IN

Non ANSI

Use IN

IS NOT IN

Non ANSI

Use NOT IN

IS NOT NULL

ANSI(E)

Predicate

IS NULL

ANSI(E)

Predicate

JOIN

ANSI(I)

Specify joined tables

JOURNAL

Non ANSI Extension

Data protection control

LAST

Non ANSI Extension

Access logging control

LE

Non ANSI

Use ‘ ?

F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 7A 5E 4C 7E 6E 6F

70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F

p q r s t u v w x y z { | } ~ DEL

97 98 99 A2 A3 A4 A5 A6 A7 A8 A9 C0 6A D0 A1 07

B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF

B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC 9D BE BF

F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF

29 2A 2B 2C 30 31 33 34 36 38 FA FB FC FD FE FF

Teradata RDBMS for UNIX SQL Reference

E-5

ASCII Hexadecimal Translation Tables About This Appendix

E-6

Teradata RDBMS for UNIX SQL Reference

Data Conversion

Appendix F

Data Conversion

Teradata RDBMS for UNIX SQL Reference

Data Conversion

Teradata RDBMS for UNIX SQL Reference

Data Conversion Data Conversion F

Data Conversion

6

The Teradata RDBMS converts data from client format to Teradata RDBMS format. The internal format of data as it is stored on the Teradata RDBMS and on the AT&T 3B-series, Bull HN Worldwide Information Systems, IBM, DEC VAX/VMS, PC, and UTS client computers is described in the following set of tables.

Teradata RDBMS for UNIX SQL Reference

F-1

Data Conversion Teradata RDBMS Internal Format

Teradata RDBMS Internal Format

Data Type

F-2

Format

BYTEINT

8-bit signed two’s complement binary number (1 byte)

SMALLINT

16-bit signed two’s complement binary number, least significant byte first (2 bytes)

INTEGER

32-bit signed two’s complement binary number, least significant byte first (4 bytes)

FLOAT

64-bit IEEE floating point number, low-order byte first, with 1 bit for the fraction’s sign, 11 bits for exponent, 52 bits for unsigned fraction

DECIMAL(x,y)

8-bit, 16-bit, 32-bit, or 64-bit signed two’s complement binary integer, scaled by 10 ** y (stored as follows: 1-2 digits in 1 byte, 3-4 digits in 2 bytes, 5-9 digits in 4 bytes, 10-18 digits in 8 bytes; least significant byte first)

CHAR(n)

n bytes containing mixed single byte character/multibyte character characters.

VARCHAR(n)

16-bit integer (2 bytes, least significant byte first) containing actual count k, depending on the character set, followed by k bytes of mixed single byte character/multibyte character characters or k ASCII characters (total of k + 2 bytes) (actual length k, where 0