SQL Features

After completing this module, you will be able to:

• State the purpose and function of the session setting flags. • Recognize differences in transaction modes for Teradata
and ANSI.

• Distinguish between ANSI and Teradata case sensitivities. • Describe 2 features of the System Calendar. • Describe how space is allocated for “volatile” and “global”
temporary tables.

Teradata SQL
OS TOS TOS UNIX Platform SQL Compatibilities DB2, SQL/DS, ANSI DB2, SQL/DS, ANSI (Outer Join added) DB2, SQL/DS, ANSI (Similar to V1R5.1) ANSI (major syntax change)

DBC/1012 DBC/SQL 3600 5100 Teradata SQL (V1R5.1) Teradata SQL (V2R1) Teradata SQL (V2R2 to V2R5)

UNIX 53xx Win 2000

Three ANSI standards: – ANSI SQL-89 (SQL1) – ANSI SQL-92 (SQL2) – ANSI SQL-99 (SQL3)

Two levels of ANSI SQL99 compliance: – Core level (CoreSQL99) - previously Entry level – Enhanced level (EnhancedSQL99) Teradata SQL(Version 2): – Teradata ANSI-92 compliant - Entry level – Certified by US Government and NIST – Has many SQL-99 core and enhanced features

SQL Version 2 Differences
ANSI Teradata
Teradata Version 2 consists of: – All existing Teradata features from V1. – ANSI compatible ways to perform many Teradata V1 features. – Completely new features based on ANSI standard.

Enhanced functions and features

CASE INNER JOIN OUTER JOIN AS

Macros WITH BY FORMAT NAMED

V2R2 (and later) allows for sessions to operate in either ... – BTET (Teradata) mode – ANSI mode All syntax, both ANSI and Teradata extensions, is accepted in either mode. The same syntax might function differently in each mode. – Transaction protocol behavior – CREATE TABLE SET or MULTISET default – Case sensitivity and collating sequences – Certain data conversion and display functions

SQL Session Modes
Transaction mode setting ANSI – uses ANSI mode (Commit mode) BTET – uses standard Teradata mode (Begin Txn – End Txn mode) BTEQ Examples
.SET SESSION TRANSACTION BTET; – requires neither for implicit transactions – requires BT to start explicit transaction – requires ET to end explicit transaction .SET SESSION TRANSACTION ANSI; – requires COMMIT to end transaction

Must be entered prior to LOGON. To change session mode, must LOGOFF first. Session Mode affects: – – – – Transaction protocol CREATE TABLE defaults Default case sensitivities Data conversions

Transaction Modes – Teradata
.SET SESSION TRANSACTION BTET; BTET mode characteristics: • CREATE TABLE default – SET table • A transaction is by definition implicit. • Each request is an implicit transaction. BT / ET Statements • BEGIN TRANSACTION (BT) and END TRANSACTION (ET) statements are used to create larger transactions out of individual requests. • BT; – begins an explicit Txn. • ET; – commits the currently active Txn. • Locks are accumulated following a BT until an ET is issued. • A DDL statement must be the last statement before an ET. • A rollback occurs when any of the following occur: – ROLLBACK WORK - explicit rollback of active Txn
– SQL statement failure – Session abort - rollback of active Txn - rollback of active Txn

Transaction Modes – ANSI
.SET SESSION TRANSACTION ANSI; ANSI mode characteristics:

• • • • • • • •

CREATE TABLE default – MULTISET table A transaction is committed only by an explicit COMMIT. COMMIT WORK will commit the currently active Txn. Transactions are by definition explicit. Statement following a COMMIT automatically starts a new Txn. A DDL statement must be the last statement before a COMMIT. Locks are accumulated until a COMMIT is issued. A rollback occurs when any of the following occur:
– ROLLBACK WORK – Session abort – SQL statement failure - explicit rollback of active Txn - rollback of active Txn - rollback current statement only

Transaction Mode Examples
ANSI Mode UPDATE A … ; UPDATE B … ; COMMIT; (Both commit) UPDATE A … ; UPDATE B … ; (Fails) COMMIT; (A commits) BTET Mode (explicit) BT; UPDATE A … ; UPDATE B … ; ET; (Both commit) BT; UPDATE A … ; UPDATE B … ; (Fails) (Both rollback) BT; UPDATE A … ; UPDATE B … ; ROLLBACK ; (Both rollback) BT; UPDATE A … ; UPDATE B … ; LOGOFF; (Both rollback) BTET Mode (implicit) UPDATE A … ; (A commits) UPDATE B … ; (B commits)

UPDATE A … ; (A commits) UPDATE B … ; (Fails) (Rollback B)

UPDATE A … ; UPDATE B … ; ROLLBACK ; (Both rollback)

(No explicit ROLLBACK in implicit Txn)

UPDATE A … ; UPDATE B … ; LOGOFF; (Both rollback)

UPDATE A … ; (A commits) UPDATE B … ; (B commits) LOGOFF;

Multi-Statement Requests
UPDATE Dept UPDATE Dept ;;UPDATE Manager UPDATE Manager ;;UPDATE Employee UPDATE Employee SET Salary_Change_Date = CURRENT_DATE SET Salary_Change_Date = CURRENT_DATE SET Salary_Amt = Salary_Amt **1.06 SET Salary_Amt = Salary_Amt 1.06 SET Salary_Amt = Salary_Amt **1.04 ;; SET Salary_Amt = Salary_Amt 1.04

This is an example of 1 request - 3 statements. This one request is considered an “implicit transaction”. Notes:

• A semi-colon at the end of a line defines the end of the request (BTEQ convention). • You cannot mix DDL and DML within a single request.
The 3 table-level write locks (in this example) will be:

• Acquired in TID order. • Held until done.
Advantage: Minimizes deadlocks at the table level when many users execute requests on the same tables. This applies for all types of requests:

• Multi-statement requests (as above) • Single-statement DDL or DML requests • Macros

CASE Sensitivity Issues – Teradata Mode
Teradata Rules – Column Attributes
Storage Comparisons As entered (default) UPPERCASE UPPER, LOWER CASESPECIFIC (CS) NONCASESPECIFIC (NOT CS) – (Teradata default)

Default (NOT CS) result
SELECT first_name,last_name FROM Employee WHERE last_name LIKE '%Ra%'; first_name --------------Robert James I.B. Larry Peter last_name --------------Crane Trader Trainer Ratzlaff Rabbit

Explicit (CS) result
SELECT first_name, last_name FROM Employee WHERE last_name(CS) LIKE '%Ra%'; first_name --------------Larry Peter last_name --------------Ratzlaff Rabbit

CASE Sensitivity Issues – ANSI Mode
Teradata Rules – Column Attributes
Storage Comparisons None (As entered is default) UPPER, LOWER (Case specific is default)

Default (CS) result
SELECT first_name,last_name FROM Employee WHERE last_name LIKE '%Ra%'; first_name --------------Larry Peter last_name --------------Ratzlaff Rabbit

Explicit (CS) result
SELECT first_name, last_name FROM Employee WHERE last_name(CS) LIKE '%Ra%'; first_name --------------Larry Peter last_name --------------Ratzlaff Rabbit

Using ANSI Blind Test

Teradata mode (NOT CS)
SELECT FROM WHERE first_name --------------Robert James I.B. Larry Peter first_name, last_name Employee last_name LIKE '%Ra%'; last_name --------------Crane Trader Trainer Ratzlaff Rabbit

ANSI mode – case blind test (CS)
SELECT FROM WHERE first_name --------------Robert James I.B. Larry Peter first_name, last_name Employee UPPER(last_name) LIKE UPPER('%Ra%'); last_name --------------Crane Trader Trainer Ratzlaff Rabbit

Setting the SQL Flagger
Teradata sessions have an additional selectable attribute to flag ANSI SQL non-compliance. SQLFLAG setting ENTRY NONE BTEQ Example .SET SESSION SQLFLAG ENTRY; – flags non-core level ANSI syntax – flags ANSI core incompatibilities – turns off flagger

Must be entered prior to LOGON. To change session mode, must LOGOFF first. Affects: – Warnings generated for ANSI non-compliance – No effect on command execution

For example: DATE is not ANSI standard. CURRENT_DATE is ANSI standard.

SQLFLAG Example
.set session sqlflag entry; .logon tfact01,tfact01; sel date; *** Query completed. One row found. *** One column returned. *** Total elapsed time was 1 second. sel date; $
*** SQL Warning 5836 Token is not an entry level ANSI Identifier or Keyword.

.logoff .set session sqlflag none; .logon tfact01,tfact01; sel date; *** Query completed. One row found. *** One column returned. *** Total elapsed time was 1 second. Current Date ------------------2004-02-20

sel date; $
*** SQL Warning 5818 Synonyms for Operators or Keywords are not ANSI.

sel date; $
*** SQL Warning 5821 Built-in values DATE and TIME are not ANSI.

sel date; $
*** SQL Warning 5804 A FROM clause is required in ANSI Query Specification.

Current Date ------------------2004-02-20

HELP SESSION Command
HELP SESSION; *** Help information returned. One row. *** Total elapsed time was 1 second. User Name Account Name Logon Date Logon Time Current DataBase Collation Character Set Transaction Semantics Current DateForm Session Time Zone Default Character Type : Default Date Format : Currency Name Currency : Default Timestamp format Current Role Logon Account

BTEQ Note: To produce this format in BTEQ, use these BTEQ settings: .SET SIDETITLES .SET FOLDLINE

TFACT04 $M_9038_&D&H To return to the default settings: 04/02/20 10:43:16 .SET DEFAULTS TFACT01 ASCII ASCII Notes: Teradata ANSIDate • V2R5 HELP SESSION displays 00:00 many more parameters than LATIN previous releases. : YY/MM/DD • However, to see SQLFLAGGER : setting, use SHOW CONTROL US Dollars command. $ : YYYY-MM-DDBHH:MI:SS.S(F)Z TF_Student $M_9038_&D&H

Why A System Calendar?
The Truth Is ...
SQL has limited ability to do date arithmetic. There is a need for more complex, calendar-based calculations.

I’d Like To Know … How does this quarter compare to same quarter last year?
How many shoes do we sell on Sundays vs. Saturdays? During which week of the month do we sell the most pizzas?

Some Good News

Extends properties of DATE data type by joining to Calendar. Easily joined to other tables, i.e., dimension of a star schema. High performance - limited I/O. Has advantages over user-defined calendars.

Standard Usage

Statistics are created for materialized table for join planning. Only necessary rows are materialized for the calendar.

Calendar Table Layout
Columns from the System Calendar:
calendar_date DATE UNIQUE (Standard Teradata date) day_of_week BYTEINT, (1-7, where 1 = Sunday) day_of_month BYTEINT, (1-31) day_of_year SMALLINT, (1-366) day_of_calendar INTEGER, (Julian days since 01/01/1900) weekday_of_month BYTEINT, (nth occurrence of day in month) week_of_month BYTEINT, (partial week at start of month is 0) week_of_year BYTEINT, (0-53) (partial week at start of year is 0) week_of_calendar INTEGER, (0-n) (partial week at start is 0) month_of_quarter BYTEINT, (1-3) month_of_year BYTEINT, (1-12) month_of_calendar INTEGER, (1-n) (Starting Jan, 1900) quarter_of_year BYTEINT, (1-4) quarter_of_calendar INTEGER, (Starting Q1, 1900) year_of_calendar SMALLINT, (Starting 1900)

System Calendar is a 4-level nested view of dates. Underlying table is Sys_calendar.Caldates: – Has one column called ‘cdate’ - DATE data type. – Has one row for each date of calendar. – Unique Primary Index is cdate. – Each level of view adds intelligence to date.

Note: System calendar includes Jan 1, 1900 through Dec. 31, 2100.

One Row in the Calendar
SELECT * FROM Sys_calendar.Calendar WHERE calendar_date = '2003-12-15' ;
calendar_date 2003-12-15 day_of_week 2 day_of_month 15 day_of_year 349 day_of_calendar 37969 weekday_of_month 3 week_of_month 2 week_of_year 50 week_of_calendar 5424 month_of_quarter 3 month_of_year 12 month_of_calendar 1248 quarter_of_year 4 quarter_of_calendar 416 year_of_calendar 2003 Note:

December 2003
S 7 14 21 28 M 1 8 15 22 29 T 2 9 16 23 30 W 3 10 17 24 31 T 4 11 18 25 F 5 12 19 26 S 6 13 20 27

SELECT CURRENT_DATE is the ANSI standard equivalent of SELECT DATE.

Using the Calendar
Show total sales of item 10 reported in Q4 of 2003. Daily_Sales table Item_id salesdate sales SQL: SELECT FROM INNER JOIN ON AND AND AND GROUP BY Result: Join Sys_Calendar.Calendar calendar_date : : quarter_of_year : year-of_calendar

=4?

ds.itemid, SUM(ds.sales) = 2003 ? Sys_Calendar.Calendar sc Daily_Sales ds sc.calendar_date = ds.salesdate sc.quarter_of_year = 4 sc.year_of_calendar = 2003 Salesdate is joined to the system calendar. ds.itemid = 10 Calendar determines if this date meets 1; criteria: Is it a Quarter 4 date? itemid Sum(sales) Is it a 2003 date? ------------ ---------------If yes on both, add this sales amount to 10 4050.00 result.

Temporary Table Choices
Derived Tables • Local to the query (table and columns are named within query) • Incorporated into SQL query syntax (populated in query via SELECT in FROM) • Materialized in SPOOL – Spool rows are discarded when query finishes • No data dictionary involvement • Commonly used with aggregation Volatile Tables • Local to a session – uses SPOOL space • Uses CREATE VOLATILE TABLE syntax • Discarded automatically at session end • No data dictionary involvement (Global) Temporary Tables • Local to a session – uses TEMPORARY space • Uses CREATE GLOBAL TEMPORARY TABLE syntax • Materialized instance of table discarded at session end • Creates and keeps table definition in data dictionary

Derived Tables Revisited
Get top three selling items across all stores:
SELECT FROM Prodid, Sumsales, RANK(sumsales) AS "Rank" (SELECT prodid, sum(sales) FROM Salestbl GROUP BY 1) AS tmp (prodid, sumsales) QUALIFY RANK (sumsales) <= 3; Result: Prodid --------A C D Sumsales --------------170000.00 115000.00 110000.00 Rank -------1 2 3

• Derived table name is “tmp”.
– The table is required for this query but no others. – The query will be run only one time with this data.

• Derived column names are “prodid” and “sumsales”. • Table is created in spool using the inner SELECT. • SELECT statement is always in parenthesis following “FROM”.

Volatile Tables
Similar to derived tables: • • • • • • • • Materialized in spool No Data Dictionary access or transaction locks Table definition kept in cache Designed for optimal performance Is local to the session, not the query Can be used with multiple queries in the session Dropped manually anytime or automatically at session end Requires CREATE VOLATILE TABLE statement CREATE Considerations:

Different from derived tables:

Example: CREATE VOLATILE TABLE vt_deptsal , LOG (deptno SMALLINT ,avgsal DEC(9,2) ,maxsal DEC(9,2) ,minsal DEC(9,2) ,sumsal DEC(9,2) ,empcnt SMALLINT) ON COMMIT PRESERVE ROWS;

• LOG indicates that a transaction journal • • • •
is maintained. NO LOG allows for better performance. PRESERVE ROWS indicates keep table rows at TXN end. DELETE ROWS indicates delete all table rows at TXN end. Volatile tables do not survive a system restart.

Volatile Table Restrictions
Restrictions: • Up to 64 volatile tables are allowed on a single session. • Each must have a unique name. • Volatile tables are always qualified by the session’s userid. CREATE VOLATILE TABLE username.table1 (Explicit) CREATE VOLATILE TABLE table1 (Implicit) CREATE VOLATILE TABLE databasename.table1 (Error) • Each session can use the same VT name (local to session). • VT name cannot duplicate existing object name for this user. – Perm or Temp table names – View names – Macro names – Trigger names Electable but not often useful for VTs. VTs don’t survive a system reset.
• Permanent Journaling • CHECK constraints • Column default values • Referential Integrity • Column compression • Column titles • Named Indexes

Examples:

Multiple Sessions:

FALLBACK: Options not permitted:

Global Temporary Tables
Global Temporary Tables: Are created using CREATE GLOBAL TEMPORARY command. Require a base definition which is stored in the DD. Are materialized by first SQL DML statement to access table. Each instance of global temp table is local to a session. Materialized tables are dropped automatically at session end. Have LOG and ON COMMIT PRESERVE/DELETE options. Materialized table contents aren’t sharable with other sessions. Base definition is permanent and kept in DD. Requires DML privileges necessary to materialize the table. Space is charged against an allocation of “temporary space” - CREATE USER TEMPORARY parameter. User can materialize up to 32 global tables per session. Tables can survive a system restart.

Global Temporary Tables are similar to Volatile Tables:

Global Temporary Tables are different from Volatile Tables:

Creating Global Temporary Tables
CREATE GLOBAL TEMPORARY TABLE gt_deptsal (deptno SMALLINT ,avgsal DEC(9,2) ,maxsal DEC(9,2) ,minsal DEC(9,2) ,sumsal DEC(9,2) ,empcnt SMALLINT);

Base table definition stored in DD/D Default is ON COMMIT DELETE ROWS

ALTER TABLE gt_deptsal, ON COMMIT PRESERVE ROWS;

ALTER TABLE can be done to change defaults.

INSERT INTO gt_deptsal SELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal) ,SUM(sal) ,COUNT(emp) FROM emp GROUP BY 1;

Table is now materialized. Row is inserted in DBC.Temptables.

DELETE FROM gt_deptsal;

Table remains materialized until it is dropped.

CASE Feature
• The CASE feature allows for conditional processing of returned rows. • There are two forms of the CASE expression - Valued and Searched. • Each row is evaluated against each WHEN clause. – First match returns a result for the row. – If no match, ELSE result is produced for that row.

Valued CASE format: CASE value-expr [ELSE result_n] END WHEN WHEN : expr_1 expr_2 : THEN THEN : result_1 result_2 :

Example: Create a report entitled “Autumn sale” that shows spring items marked 33% off and summer items marked 25% off. SELECT item_number ,item_description ,item_price AS “Current//Price” ,CASE item_season WHEN 'spring' THEN item_price * (1 - .33) WHEN 'summer' THEN item_price * (1 - .25) ELSE NULL END AS "Sale//Price" FROM Inventory_Table;

CASE Feature (cont.)
In a Searched CASE statement, you do not specify an expression to test. You specify multiple, arbitrary, search conditions that can return different results. Searched CASE format: WHEN WHEN : [ELSE value-expr_n] END CASE condition_1 condition_2 : THEN THEN : value-expr_1 value-expr_2 :

Example: Repeat the previous page query, and mark down by 50% the summer items with inventories of less than 3. SELECT item_number ,item_description ,item_price AS “Current//Price” ,CASE WHEN item_season = 'summer' AND item_count < 3 WHEN item_season = 'summer' WHEN item_season = 'spring' ELSE NULL END AS "Sale//Price" FROM Inventory_Table WHERE item_season IN ('spring' OR 'summer');

THEN item_price * (1 - .50) THEN item_price * (1 - .25) THEN item_price * (1 - .33)

Example of CASE – Single Pass Over Data
Four separate SQL statements can be used to generate the 4 totals. Instead of four passes over the data using the WHERE clause, execute one pass using CASE.
SELECT SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = THEN Sales ELSE 0 END) AS YTD_Qty ,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = AND EXTRACT (MONTH FROM sales_dt) = THEN Sales ELSE 0 END) AS MTD_Qty ,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = AND EXTRACT (MONTH FROM sales_dt) LE AND EXTRACT (DAY FROM sales_dt) LE THEN Sales ELSE 0 END) AS LastYear_YTD_Qty ,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = AND EXTRACT (MONTH FROM sales_dt) = AND EXTRACT(DAY FROM sales_dt) LE THEN Sales ELSE 0 END) AS LastYear_MTD_Qty The Output: FROM Sales_History; EXTRACT (YEAR FROM Current_Date)

EXTRACT (YEAR FROM Current_Date) EXTRACT (MONTH FROM Current_Date)

(EXTRACT (YEAR FROM Current_Date) - 1) EXTRACT (MONTH FROM Current_Date) EXTRACT (DAY FROM Current_Date)

(EXTRACT (YEAR FROM Current_Date) - 1) EXTRACT (MONTH FROM Current_Date) EXTRACT (DAY FROM Current_Date)

YTD_Qty

MTD_Qty LastYear_YTD_Qty LastYear_MTD_Qty 212 250 145

404

V2R5 – New Features
Single Version of the Truth Strategic Decision Making

• • • • •

Partitioned Primary Indexes Cylinder read Value List Compression 2048 Columns, 64 Column-Indexes identity Column

• Analytic extensions: Extended Windows • • • • • • •
functions and multiple aggregate distincts Random Stratified Sampling Join Elimination Extended Transitive Closure Partial Group By Early Group By Derived Table Rewrite Very Large SQL

Trusted, Integrated Environment and Administration

• Index Wizard • Statistics Wizard and Collection • • •
Enhancements Database Query Log SQL Assistant - WEB Edition Availability Enhancements

Tactical and Event Decision Making

Data Freshness

• • • • •

Continuous Update and Manageability Faster Join Index Update Join Update Performance Bulk Update Performance Teradata Warehouse Builder Enhancements

• • • • • •

Partial Covering Join Index Global Index Sparse Index Join index Extensions ODS Workload Optimization Stored Procedure Enhancements

Teradata Limits (Different Releases)
V2R3 Maximum Vdisk Size Maximum Block Size (sectors) Number of concurrent users Concrete Step Size Explain output text max size Max size character string constant Number of spool tables per query Number of levels of query nesting (subqueries, nested views. Etc.) Maximum # of columns in a Table Maximum # of columns in an Index Column limit for COLLECT STATISTICS Maximum SQL Statement Limit Size 112 GB 127 32 KB 64 KB 64 KB 255 512 10 256 16 40 64K V2R4 112 GB 127 4 GB 1 MB No limit 32 KB 2048 64 256 16 40 64K V2R4.1 1.26 TB 255 4 GB 1 MB No limit 32 KB 2048 64 256 16 40 64K V2R5.0 1.26 TB 255 4 GB 1 MB No limit 32 KB 2048 64 2048 64 512 1 MB

Review Questions
1. Which BTEQ setting controls Teradata Vs. ANSI mode? _____________________________ 2. Which commands will not work in ANSI mode? _______________ 3. True or False. The SQL Flagger is just a warning device and doesn’t affect command execution 4. True or False. Failure of an individual request in COMMIT mode causes the entire transaction to be rolled back. 5. True or False. Logging off during an explicit transaction without either a COMMIT or ET will always result in a ROLLBACK. 6. True or False. HELP SESSION will show the session mode and the status of the SQL Flagger. 7. Where does a Volatile Temporary table get its space from? _____________ 8. Where does a Global Temporary table get its space from? _____________

Module 18: Review Question Answers
1. Which BTEQ setting controls Teradata Vs. ANSI mode? .SET SESSION TRANSACTION 2. Which commands will not work in ANSI mode? BT, ET 3. True or False. The SQL Flagger is just a warning device and doesn’t affect command execution. 4. True or False. Failure of an individual request in COMMIT mode causes the entire transaction to be rolled back. 5. True or False. Logging off during an explicit transaction without either a COMMIT or ET will always result in a ROLLBACK. 6. True or False. HELP SESSION will show the session mode and the status of the SQL Flagger. 7. Where does a Volatile Temporary table get its space from? Spool 8. Where does a Global Temporary table get its space from? Temporary

Sign up to vote on this title
UsefulNot useful