428 12 2MB
English Pages 913 Year 1997
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