34781 cannon
DESCRIPTION
34781 CannonTRANSCRIPT
Introduction to Teradata SQL
Gary Cannon
Sr. Consulting EngineerStrategic Warehousing and [email protected]
September 7, 2006
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 2
Gary CannonEducation Specialist
Gary Cannon, joined NCR in June 1970. He worked in various IT related
positions through October 1997 when he joined Teradata Customer Education to teach UNIX and Teradata courses.
Gary presents the Introduction to Teradata, SQL, Application Utilities and Physical Database Design courses.
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 3
Agenda
Present an introduction to Teradata SQL
Identify many of the unique features of Teradata SQL
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 4
ANSI SQL Standards
• Three defined SQL standards:
SQL-89 (SQL 1)
SQL-92 (SQL 2)- Entry Level- Intermediate Level- Full Level
SQL-99 (SQL 3)- Core- Enhanced
• Teradata SQL is certified at the SQL-92 Entry level with some Intermediate, some Full and some SQL-99 Core features
• New Teradata SQL features are added using the ANSI SQL-99 standard
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 5
Object Names - Rules and Qualification
Creating Object Names: ANSI Teradata SQL
Legal Characters A-Z, 0 -9, _
(underscore) Same as ANSI, plus: a -z, #, $
First character A-Z Any except 0 -9
Last character Can’t be _ (Underscore)
Any
Length 18 characters 30 characters
Case sensitivity N/A No
• The syntax for qualifying an object name is:
[[ databasename. ] tablename. ] columnname columnname (unqualified) tablename.columnname (partially qualified) databasename.tablename.columnname (fully qualified)
• Comments are delimited by /* and */
SELECT * FROM table1 /* select all columns */;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 6
Object Names - Case Sensitivity
• ANSI compliant object names and keywords are upper case only
• Teradata accepts mixed case object names and keywords
Example table:
CREATE TABLE Table1 ( Col1 INTEGER ,Col2 CHAR(10) ,Col3 SMALLINT) UNIQUE PRIMARY INDEX (Col1);
Example statements that are valid in Teradata:
SELECT COL1 FROM TABLE1;
SELECT Col1 FROM Table1;
SeLeCt COl1 From table1;
select col1 from table1;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 7
Object Names - Double Quotes
SELECT * FROM table; *** Failure 3707 Syntax error, expected something like a name between the 'from' keyword and the 'table' keyword.
SELECT * FROM "table"; *** Failure 3807 Object 'table' does not exist.
SELECT * FROM table one; *** Failure 3707 Syntax error, expected something like a name between the 'from' keyword and the 'table' keyword.
SELECT * FROM "table one"; *** Failure 3807 Object 'table one' does not exist.
• Double quotes around an object name overrides naming rules
• Double quotes allow reserved words to be used as object names
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 8
SQL Command Abbreviations
BEGIN TRANSACTION BTEND TRANSACTION ET
CREATE MACRO CMCREATE TABLE CT
DELETE DELINSERT INSSELECT SELUPDATE UPD
• Several SQL Commands may be abbreviated in Teradata
SELECT command examples:
SELECT * FROM table1;
SEL * FROM table1;
SEL table1.*;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 9
Built-In Functions
• Several system built-in functions are available in Teradata
ACCOUNT - contains the users account stringDATABASE - contains the current default databaseDATE - contains the current dateSESSION - contains the current session-idTIME - contains the current timeUSER - contains the user name
SELECT example:
SELECT ACCOUNT, DATABASE, DATE, SESSION, TIME, USER;
Account Database Date Session Time User--------- --------- ----------- -------- --------- -------$M_USR_&L USER01 2004/08/12 1756743 14:45:57 USER01
• Changing your default database
DATABASE new_database_name;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 10
HELP Command
• The HELP Command provides information on database objects
Databases and Users:
HELP DATABASE databasename; HELP USER username;
Database Objects:
HELP TABLE tablename; HELP VIEW viewname; HELP MACRO macroname; HELP Database Example:
HELP DATABASE user01;
Table/View/Macro name Kind Comment --------------------- ---- ------------------------ Table1 T ? View1 V ? Macro1 M ?
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 11
HELP SESSION Command
• The HELP SESSION Command provides information about your session
HELP SESSION; (partial output)
User Name USER01Account Name $M_USR_&LLogon Date 04/08/12Logon Time 19:20:35Current DataBase USER01Collation ASCIICharacter Set ASCIITransaction Semantics TeradataCurrent DateForm IntegerDateSession Time Zone 00:00Default Character Type LATINExport Latin 1Export Unicode 1Export Unicode Adjust 0Export KanjiSJIS 1Export Graphic 0Default Date Format yyyy/mm/dd ...
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 12
HELP 'SQL' Command
HELP 'SQL'; (HELP 'SQL DELETE'; for DELETE syntax rules)
DBS SQL COMMANDS:ABORT ALTER FUNCTION ALTER PROCEDUREALTER TABLE ALTER TRIGGER BEGIN LOGGINGBEGIN QUERY LOGGING BEGIN TRANSACTION CALLCHECKPOINT COLLECT DEMOGRAPHICS COLLECT STATISTICSCOMMENT COMMIT CREATE DATABASECREATE FUNCTION CREATE HASH INDEX CREATE INDEXCREATE JOIN INDEX CREATE MACRO CREATE PROCEDURECREATE PROFILE CREATE ROLE CREATE TABLECREATE TRIGGER CREATE USER CREATE VIEW ...
DBS SQL FUNCTIONS:ABS ADD_MONTHS ACOSACOSH ASIN ASINHATAN ATANH ATAN2AVERAGE BYTES CAST ...
• The HELP 'SQL' Command provides information about SQL Commands
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 13
SHOW Command
• The SHOW Command provides information on how objects were created
Database Objects:
SHOW TABLE tablename; - returns the create table command
SHOW VIEW viewname; - returns the create view command SHOW MACRO macroname; - returns the create macro
command
Example SHOW TABLE:
SHOW TABLE table1; ---------------------------------------------------------- CREATE SET TABLE USER01.table1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT ( col1 INTEGER, col2 CHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC, col3 SMALLINT) UNIQUE PRIMARY INDEX ( col1 );
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 14
EXPLAIN Command
The EXPLAIN text provides: An English translation of the AMP steps A row count and time estimates for comparison only Which indexes, if any will be used Whether individual steps will process in parallel
EXPLAIN SELECT * FROM Table1;
Explanation 1) First, we lock a distinct USER01."pseudo table" for read on a RowHash to prevent global deadlock for USER01.table1. 2) Next, we lock USER01.table1 for read. 3) We do an all-AMPs RETRIEVE step from USER01.table1 by way of an all-rows scan with no residual conditions into Spool 1 (group_amps), which is built locally on the AMPs. The size of Spool 1 is estimated with low confidence to be 16 rows. The estimated time for this step is 0.56 seconds. 4) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request. -> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0.56 seconds.
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 15
Equivalent Comparison Operators
OPERATOR EXTENSION = EQ
<> NE ^= NOT=
> GT
< LT
>= GE
<= LE
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 16
Case Sensitivity - Comparisons
• Teradata is not case sensitive on character comparisons
SELECT col2 FROM table2 WHERE col2 = 'abc';
col2----------aBcABCABcabc
• Use CASESPECIFIC (or CS) to force ANSI standard case sensitivity
SELECT col2FROM table2 WHERE col2 (CASESPECIFIC) = 'abc';
col2----------abc
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 17
Additional Data Types
DATA TYPE DEFINITION BYTE 1 to 64,000 bytes fixedVARBYTE 1 to 64,000 bytes variable
BYTEINT 1 byte ranging in value from -128 to +127
DATE 4 bytes stored as:
YYMMDD for dates 1999-12-31 and earlier or YYYMMDD for dates 2000-01-01 and beyond
((YEAR - 1900) * 10000) + (MONTH * 100) + DAY)
GRAPHIC 1 to 32,000 characters (each 2 bytes) fixedVARGRAPHIC 1 to 32,000 characters (each 2 bytes) variable
LONG VARCHAR same as VARCHAR(64000)
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 18
Calculator
• Teradata provides a built-in calculator
Calculation Result Comment
SELECT 2*250; 500 Simple multiplication
SELECT 1.01 + 2.2; 3.21 Uses greatest possible precision
SELECT 10/3.000; 3.333 Rounds off
SELECT 10/6.000; 1.667 Rounds up
• Teradata will display the order of evaluation
SELECT 3 * 5 + 6 / 3 - 8 * 2 + 3;
((((3*5)+(6/3))-(8*2))+3)------------------------- 4
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 19
ADD_MONTHS Function
Query Results
SELECT DATE; 01/08/07
ADD_MONTHSSELECT ADD_MONTHS (DATE, 2); 2001-10-07
SELECT ADD_MONTHS (DATE, 12*14); 2015-08-07
SELECT ADD_MONTHS (DATE, -11); 2000-09-07
ADD_MONTHS - literal dateSELECT ADD_MONTHS ('2001-07-31', 2); 2001-09-30
SELECT ADD_MONTHS ('2003-12-31', 2); 2004-02-29
SELECT ADD_MONTHS ('2003-12-31', 14); 2005-02-28
SELECT ADD_MONTHS('2004-01-31',1) AS "January 31 + 1 Month" ,ADD_MONTHS('2004-02-29',1) AS "February 29 + 1 Month";
January 31 + 1 Month February 29 + 1 Month -------------------- --------------------- 2004-02-29 2004-03-29
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 20
Data Conversion with CAST
SEL CAST(50500.75 AS INTEGER) AS "Truncated" ,CAST(50500.75 AS DECIMAL(6,0)) AS "Rounded";
Truncated Rounded----------- -------- 50500 50501.
SEL CAST(6.74 AS DECIMAL(2,1)) AS "Drop Precision" ,CAST(6.75 AS DECIMAL(2,1)) AS "Round Up" ,CAST(6.85 AS DECIMAL(2,1)) AS "Round Down to Even";
Drop Precision Round Up Round Down to Even-------------- -------- ------------------ 6.7 6.8 6.8
SEL 'Teradata'(char(4)) as "Truncate with CAST" ,'Teradata'(char(4),UPPERCASE) as "Truncate & UPPERCASE";
Truncate with CAST Truncate & UPPERCASE ------------------ ---------------------- Tera TERA
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 21
Questions?
Raise your virtual hand to let us know you have a question
Type your question in this box I am not clear about the use of triggers
I am not clear about the use of triggersJohn Q Public
Questions can be submitted at any time. We will answer questions using the text chat window at several scheduled breaks throughout the session.
Then Click Send
Select All Participants from the ‘Send to’ drop down
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 22
Multiple Column Match Subquery
• Teradata allows subqueries to match on more than one column
• Subqueries may be nested up to 64 levels
SELECT col1 ,col2 ,col3FROM table1WHERE (col1, col3)IN (SELECT col1 ,col2 FROM table2);
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 23
Renaming Columns
• Teradata provides several ways to rename or title columns
• (TITLE '') returns the title as a zero length string
SELECT current_date todays_date ,current_date AS todays_date ,current_date (NAMED todays_date) ,current_date AS "Today's Date" ,current_date (TITLE 'Todays''s Date') ,current_date (TITLE '');
todays_date todays_date todays_date Today's Date Todays's Date----------- ----------- ----------- ------------ ------------- 2004/08/14 2004/08/14 2004/08/14 2004/08/14 2004/08/14 2004/08/14
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 24
Formatting Numerics
• Teradata provides the ability to format numerics
$ Fixed or floating dollar sign9 Decimal digit (no zero suppress)Z Zero-suppressed decimal digit, Comma—inserted where specified. Decimal point position- Dash character—inserted where specified/ Slash character—inserted where specified% Percent character—inserted where specified
Numeric
formatting
symbols:
SELECT 123 (FORMAT '99999999'), 123 (FORMAT '9(8)') ,000005 (FORMAT 'Z(5)9'), 1234.56 (FORMAT '$$$,$$9.99') ,5 (FORMAT 'Z9%'), 2225551212 (FORMAT '999/999-9999');
123 123 5 1234.56 5 2225551212 -------- -------- ------ ---------- --- ------------ 00000123 00000123 5 $1,234.56 5% 222/555-1212
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 25
Formatting Dates
• Teradata provides the ability to format dates
Y Year as YYYY/Y4 (2004) or YY (04)M Month as MMMM/M4 (August), MMM/M3 (Aug) or MM (08)D Day as DDD/D3 (day of the year) or DD (day of the month)E Day of the week as EEEE/E4 (Monday) or EEE/E3 (Mon), Comma—inserted where specified. Decimal point position- Dash character—inserted where specified/ Slash character—inserted where specifiedB Blank position—inserted where specified
Date
formatting
symbols:
SELECT current_date (FORMAT 'YY/MM/DD') ,current_date (FORMAT 'MMMBDD,BYYYY') ,current_date (FORMAT 'M4BDD,BY4') ,current_date (FORMAT 'YYDDD');
Date Date Date Date-------- ------------ ------------------ -----04/08/14 Aug 14, 2004 August 14, 2004 04227
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 26
Using FORMAT with SQL Assistant
• Any specified FORMAT must always be applied to a character string
• If the column is not a character data type, the FORMAT will be ignored.
• FORMAT syntax must precede the specified conversion data type syntax.
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 27
Attribute Functions
TYPE
TITLE
FORMAT
NAMED
CHARACTERS
There are five specified attribute functions used to determine attributes for columns and expressions:
Query Results
SELECT DISTINCT TYPE (col1)FROM table1;
INTEGER
SELECT DISTINCT TITLE (col1)FROM table1;
col1
SELECT DISTINCT FORMAT (col1)FROM table1;
9(10)
SELECT DISTINCT NAMED (col1)FROM table1;
col1
SELECT DISTINCT CHARACTERS (col2)FROM table1;
10
Examples:
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 28
Inner Join Syntax
• Teradata supports the ON Join and WHERE Join Syntax for Inner Joins
SELECT A.col1 ,B.col2FROM table1 A INNER JOIN table2 BON A.col1 = B.col1;-----
SELECT A.col1 ,B.col2FROM table1 A, table2 BWHERE A.col1 = B.col1;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 29
Outer Join Syntax
• Teradata supports the ON Join Syntax for Outer Joins
SELECT A.col1 ,B.col2FROM table1 A LEFT OUTER JOIN table2 BON A.col1 = B.col1WHERE A.col2 = 'value';-----
SELECT A.col1 ,B.col2FROM table1 A RIGHT OUTER JOIN table2 BON A.col1 = B.col1;-----
SELECT A.col1 ,B.col2FROM table1 A FULL OUTER JOIN table2 BON A.col1 = B.col1;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 30
Multi-Table Join Syntax
• Teradata supports the ability to join up to 64 tables (63 join conditions)
SELECT A.col1 ,B.col2 ,C.col3 ,D.col4 ,E.col5FROM table1 A INNER JOIN table2 BON A.col1 = B.col1 LEFT OUTER JOIN table3 CON A.col1 = C.col1 RIGHT OUTER JOIN table4 DON A.col1 = D.col1 FULL OUTER JOIN table5 EON A.col1 = E.col1;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 31
UNION ALL Operator
• Teradata supports the ALL keyword with the UNION Operator to sustain duplicate rows
SELECT col1 FROM table1UNION ALLSELECT col1 FROM table2;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 32
UPDATE Using Subqueries or Joins
UPDATE table1 SET col3 = col3 + 1 WHERE col1 IN (SELECT col1 FROM table2 WHERE col2 LIKE '%update%');
UPDATE table1 SET table1.col3 = table1.col3 + 1 WHERE table1.col1 = table2.col1 AND table2.col2 LIKE '%update%';
Using a Subquery:
Using a Join:
• Updates with joins and subqueries allow a table’s rows to be updated based on information in another table
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 33
DELETE Using Subqueries or Joins
DELETE table1 WHERE col1 IN (SELECT col1 FROM table2 WHERE col2 LIKE '%delete%');
DELETE table1 WHERE table1.col1 = table2.col1 AND table2.col2 LIKE '%delete%';
Using a Subquery:
Using a Join:
• Deletes with joins and subqueries allow a table’s rows to be deleted based on information in another table
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 34
Upsert - UPDATE ELSE INSERT
• The Upsert operation will attempt to update a single row based on its Primary Index value. If the update fails because the row does not exist the row will be inserted.
Table definition:
CREATE TABLE Table1 ( Col1 INTEGER ,Col2 CHAR(10) ,Col3 SMALLINT) UNIQUE PRIMARY INDEX (Col1);
Upsert example:
UPDATE table1 SET col3 = 123 WHERE col1 = 100 ELSE INSERT INTO table1 VALUES (100,'ABC',123);
The INSERT and UPDATE must reference the same single row of the same table. The UPDATE must use the Primary Index of the table. The target table may be a table or a view.
The INSERT and UPDATE must reference the same single row of the same table. The UPDATE must use the Primary Index of the table. The target table may be a table or a view.
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 35
Questions?
Raise your virtual hand to let us know you have a question
Type your question in this box I am not clear about the use of triggers
I am not clear about the use of triggersJohn Q Public
Questions can be submitted at any time. We will answer questions using the text chat window at several scheduled breaks throughout the session.
Then Click Send
Select All Participants from the ‘Send to’ drop down
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 36
Macro Definition
• Macros contain one or more prewritten SQL statements.
• Macros are a Teradata extension to ANSI SQL.
• Macros are stored in the Teradata Data Dictionary.
• To execute a macro requires the user to have the EXEC privilege on the macro.
• Explicit privileges on the tables or views used by the macro are not needed by the executing user.
• Macros may contain only one DDL statement - the last or the only statement in the macro.
Macro-related commands:
CREATE MACRO macroname AS( . . . );
Define a macro and storeit in the DD.
EXECute macroname; Execute statements withina macro.
SHOW MACRO macroname; Display a macro.
REPLACE MACRO macroname AS(. . . );
Apply changes to a macroor create a new macro.
DROP MACRO macroname; Remove a macrodefinition from the DD.
EXPLAIN EXEC macroname; Display EXPLAIN text forthe macro's execution.
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 37
Macro Examples
Create the macro:
CREATE MACRO macro1 AS ( SELECT col1, col2 FROM table1; );
Execute the macro:
EXEC[UTE] macro1;
Modify the macro:
REPLACE MACRO macro1 AS ( SELECT col1, col2, col3 FROM table1; );
Explain the macro:
EXPLAIN EXEC macro1;
Remove the macro:
DROP MACRO macro1;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 38
Parameterized Macros
• Parameterized macros allow substitutable variables• Values for these variables are supplied at runtime
Create the macro:
CREATE MACRO macro2 (val1 INTEGER, val2 INTEGER) AS ( SELECT * FROM table1 WHERE col1 = :val1 AND col3 = :val2; );
Execute the Macro:
EXEC macro2 (100,200);
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 39
SAMPLE Function
• The SAMPLE function allows sampling of data based on the actual number of rows or a percentage of the rows
Sample a number of rows:
SELECT * FROM table1 SAMPLE 10;
Sample a percentage of rows:
SELECT * FROM table1 SAMPLE .25;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 40
SAMPLE Function - SAMPLEID• SAMPLEID assigns an ID value (i.e. 1, 2, 3, 4 …) to a specific sample set
Sample a number of rows:
SELECT sampleid, col1 FROM table1 SAMPLE 10, 20, 30;
Sample a percentage of rows:
SELECT sampleid, col1 FROM table1 SAMPLE .25, .25, .50 ORDER BY 1;
SampleId col1 ----------- --------------- 1 1024 1 1008 2 1004 2 1011 3 1021 3 1017 3 1006 3 1018
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 41
NULLIFZERO and ZEROIFNULL
• NULLIFZERO will return NULL if the specified value is zero
SELECT NULLIFZERO(col3) FROM table1;
• ZEROIFNULL will return zero if the specified value is NULL
SELECT ZEROIFNULL(col3) FROM table1;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 42
Views and LOCKING FOR ACCESS
• LOCKING FOR ACCESS will apply a "dirty" read lock on the target table
CREATE VIEW view1 asLOCKING TABLE table1 FOR ACCESSSELECT col1 AS c1 ,col2 AS c2 ,col3 AS c3FROM table1;
SELECT c1, c2 FROM view1ORDER BY c1;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 43
CHARACTER_LENGTH Function
• The CHARACTER_LENGTH function counts the number of characters in the specified string
• The CHARACTER_LENGTH function may also be expressed using non-ANSI compliant variants:
CHARACTERS
CHARS
CHAR
SELECT col1, col2FROM table1WHERE CHARACTER_LENGTH (col2) > 5;
SELECT col1, col2 FROM table1 WHERE CHAR(col2) > 5;
Teradata, a division of NCR -- Proprietary & Confidential, Do not distribute 44