Sunteți pe pagina 1din 31

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 Platform SQL Compatibilities

TOS DBC/1012 DBC/SQL DB2, SQL/DS, ANSI

TOS 3600 Teradata SQL DB2, SQL/DS, ANSI


(V1R5.1) (Outer Join added)

UNIX 5100 Teradata SQL DB2, SQL/DS, ANSI


(V2R1) (Similar to V1R5.1)

UNIX 53xx Teradata SQL ANSI (major syntax


Win 2000 (V2R2 to V2R5) change)

Two levels of ANSI SQL99 compliance:


Three ANSI standards: – Core level (CoreSQL99) - previously Entry level
– ANSI SQL-89 (SQL1) – Enhanced level (EnhancedSQL99)
– ANSI SQL-92 (SQL2)
– ANSI SQL-99 (SQL3) 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:


CASE
– All existing Teradata features from V1.
Enhanced Macros
functions INNER JOIN WITH BY – ANSI compatible ways to perform
and OUTER JOIN FORMAT many Teradata V1 features.
features AS NAMED – Completely new features based on
ANSI standard.

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; .SET SESSION TRANSACTION ANSI;


– requires neither for implicit – requires COMMIT to end transaction
transactions
– requires BT to start explicit
transaction
– requires ET to end explicit
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 - rollback of active Txn
– Session abort - 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 - explicit rollback of active Txn
– Session abort - rollback of active Txn
– SQL statement failure - rollback current statement only
Transaction Mode Examples
ANSI Mode BTET Mode (explicit) BTET Mode (implicit)

BT;
UPDATE A … ; UPDATE A … ; UPDATE A … ; (A commits)
UPDATE B … ; UPDATE B … ; UPDATE B … ; (B commits)
COMMIT; ET;
(Both commit) (Both commit)

UPDATE A … ; BT; UPDATE A … ; (A commits)


UPDATE B … ; (Fails) UPDATE A … ; UPDATE B … ; (Fails)
COMMIT; UPDATE B … ; (Fails) (Rollback B)
(A commits) (Both rollback)

BT;
UPDATE A … ; UPDATE A … ; (No explicit ROLLBACK
UPDATE B … ; UPDATE B … ; in implicit Txn)
ROLLBACK ; ROLLBACK ;
(Both rollback) (Both rollback)

BT;
UPDATE A … ; UPDATE A … ; UPDATE A … ; (A commits)
UPDATE B … ; UPDATE B … ; UPDATE B … ; (B commits)
LOGOFF; LOGOFF; LOGOFF;
(Both rollback) (Both rollback)
Multi-Statement Requests

UPDATE
UPDATEDept
Dept SET
SETSalary_Change_Date
Salary_Change_Date==CURRENT_DATE
CURRENT_DATE
;;UPDATE
UPDATEManager
Manager SET
SETSalary_Amt
Salary_Amt==Salary_Amt
Salary_Amt**1.06
1.06
;;UPDATE
UPDATEEmployee
Employee SET
SETSalary_Amt
Salary_Amt==Salary_Amt
Salary_Amt**1.04
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 As entered (default)
UPPERCASE

Comparisons UPPER, LOWER


CASESPECIFIC (CS)
NONCASESPECIFIC (NOT CS) – (Teradata default)

Default (NOT CS) result Explicit (CS) result


SELECT first_name,last_name SELECT first_name, last_name
FROM Employee FROM Employee
WHERE last_name LIKE '%Ra%'; WHERE last_name(CS) LIKE '%Ra%';

first_name last_name first_name last_name


--------------- --------------- --------------- ---------------
Robert Crane Larry Ratzlaff
James Trader Peter Rabbit
I.B. Trainer
Larry Ratzlaff
Peter Rabbit
CASE Sensitivity Issues – ANSI Mode

Teradata Rules – Column Attributes


Storage None
(As entered is default)

Comparisons UPPER, LOWER


(Case specific is default)

Default (CS) result Explicit (CS) result


SELECT first_name,last_name SELECT first_name, last_name
FROM Employee FROM Employee
WHERE last_name LIKE '%Ra%'; WHERE last_name(CS) LIKE '%Ra%';

first_name last_name first_name last_name


--------------- --------------- --------------- ---------------
Larry Ratzlaff Larry Ratzlaff
Peter Rabbit Peter Rabbit
Using ANSI Blind Test

Teradata mode (NOT CS) ANSI mode – case blind test (CS)
SELECT first_name, last_name SELECT first_name, last_name
FROM Employee FROM Employee
WHERE last_name LIKE '%Ra%'; WHERE UPPER(last_name)
LIKE UPPER('%Ra%');

first_name last_name first_name last_name


--------------- --------------- --------------- ---------------
Robert Crane Robert Crane
James Trader James Trader
I.B. Trainer I.B. Trainer
Larry Ratzlaff Larry Ratzlaff
Peter Rabbit Peter Rabbit
Setting the SQL Flagger

Teradata sessions have an additional selectable attribute to flag ANSI SQL


non-compliance.

SQLFLAG setting

ENTRY – flags ANSI core incompatibilities


NONE – turns off flagger

BTEQ Example
.SET SESSION SQLFLAG ENTRY; – flags non-core level ANSI syntax

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; .logoff


.logon tfact01,tfact01; .set session sqlflag none;
sel date; .logon tfact01,tfact01;
*** Query completed. One row found. sel date;
*** One column returned.
*** Total elapsed time was 1 second. *** Query completed. One row found.
*** One column returned.
sel date; *** Total elapsed time was 1 second.
$
*** SQL Warning 5836 Token is not an entry level ANSI Current Date
Identifier or Keyword.
-------------------
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;
BTEQ Note: To produce this format in
*** Help information returned. One row. BTEQ, use these BTEQ settings:
*** Total elapsed time was 1 second.
.SET SIDETITLES
User Name TFACT04 .SET FOLDLINE
Account Name $M_9038_&D&H
Logon Date 04/02/20
To return to the default settings:
Logon Time 10:43:16
.SET DEFAULTS
Current DataBase TFACT01
Collation ASCII
Character Set ASCII
Transaction Semantics Teradata Notes:
Current DateForm ANSIDate • V2R5 HELP SESSION displays
Session Time Zone 00:00
Default Character Type LATIN
many more parameters than
: : previous releases.
Default Date Format YY/MM/DD • However, to see SQLFLAGGER
: :
Currency Name US Dollars
setting, use SHOW CONTROL
Currency $ command.
: :
Default Timestamp format YYYY-MM-DDBHH:MI:SS.S(F)Z
Current Role TF_Student
Logon Account $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 calendar_date DATE UNIQUE (Standard Teradata date)


System Calendar: 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. Note:


System calendar includes
Underlying table is Sys_calendar.Caldates:
Jan 1, 1900 through
– Has one column called ‘cdate’ - DATE data type. Dec. 31, 2100.
– Has one row for each date of calendar.
– Unique Primary Index is cdate.
– Each level of view adds intelligence to date.
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 December 2003
day_of_month 15
S M T W T F S
day_of_year 349 1 2 3 4 5 6
day_of_calendar 37969 7 8 9 10 11 12 13
weekday_of_month 3 14 15 16 17 18 19 20
week_of_month 2 21 22 23 24 25 26 27
week_of_year 50 28 29 30 31
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: 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 Sys_Calendar.Calendar

Item_id Join calendar_date


salesdate :
sales :
SQL: quarter_of_year =4?
:
SELECT ds.itemid, SUM(ds.sales) year-of_calendar = 2003 ?
FROM Sys_Calendar.Calendar sc
INNER JOIN Daily_Sales ds
ON sc.calendar_date = ds.salesdate
AND sc.quarter_of_year = 4
AND sc.year_of_calendar = 2003
Salesdate is joined to the system calendar.
AND ds.itemid = 10
GROUP BY 1; Calendar determines if this date meets
criteria:
Result: 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 Prodid, Sumsales, RANK(sumsales) AS "Rank"


FROM (SELECT prodid, sum(sales) FROM Salestbl GROUP BY 1)
AS tmp (prodid, sumsales)
QUALIFY RANK (sumsales) <= 3;

Result: Prodid Sumsales Rank


--------- --------------- --------
A 170000.00 1
C 115000.00 2
D 110000.00 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 • Materialized in spool


derived tables: • No Data Dictionary access or transaction locks
• Table definition kept in cache
• Designed for optimal performance

• Is local to the session, not the query


Different from
• Can be used with multiple queries in the session
derived tables:
• Dropped manually anytime or automatically at session end
• Requires CREATE VOLATILE TABLE statement

Example: CREATE Considerations:

CREATE VOLATILE TABLE vt_deptsal • LOG indicates that a transaction journal


, LOG is maintained.
(deptno SMALLINT • NO LOG allows for better performance.
,avgsal DEC(9,2) • PRESERVE ROWS indicates keep table
,maxsal DEC(9,2) rows at TXN end.
,minsal DEC(9,2)
,sumsal DEC(9,2) • DELETE ROWS indicates delete all table
,empcnt SMALLINT) rows at TXN end.
ON COMMIT PRESERVE ROWS; • 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.

Examples: CREATE VOLATILE TABLE username.table1 (Explicit)


CREATE VOLATILE TABLE table1 (Implicit)
CREATE VOLATILE TABLE databasename.table1 (Error)

Multiple • Each session can use the same VT name (local to session).
Sessions: • VT name cannot duplicate existing object name for this user.
– Perm or Temp table names
– View names
– Macro names
– Trigger names

FALLBACK: Electable but not often useful for VTs. VTs don’t survive a system reset.

Options not • Permanent Journaling • Referential Integrity • Named Indexes


permitted: • CHECK constraints • Column compression
• Column default values • Column titles
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.

Global Temporary Tables Each instance of global temp table is local to a session.
are similar to Volatile Tables: 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.

Global Temporary Tables are Base definition is permanent and kept in DD.
different from Volatile Tables: 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.
Creating Global Temporary Tables

CREATE GLOBAL TEMPORARY TABLE gt_deptsal Base table definition stored in DD/D
(deptno SMALLINT Default is ON COMMIT DELETE ROWS
,avgsal DEC(9,2)
,maxsal DEC(9,2)
,minsal DEC(9,2)
,sumsal DEC(9,2)
,empcnt SMALLINT);

ALTER TABLE gt_deptsal, ALTER TABLE can be done to change


ON COMMIT PRESERVE ROWS; defaults.

INSERT INTO gt_deptsal Table is now materialized.


SELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal) Row is inserted in DBC.Temptables.
,SUM(sal) ,COUNT(emp)
FROM emp
GROUP BY 1;

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 WHEN expr_1 THEN result_1
WHEN expr_2 THEN result_2
: : : :
[ELSE result_n]
END

• 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:


CASE WHEN condition_1 THEN value-expr_1
WHEN condition_2 THEN value-expr_2
: : : :
[ELSE value-expr_n]
END

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 THEN item_price * (1 - .50)
WHEN item_season = 'summer' THEN item_price * (1 - .25)
WHEN item_season = 'spring' THEN item_price * (1 - .33)
ELSE NULL
END AS "Sale//Price"
FROM Inventory_Table
WHERE item_season IN ('spring' OR 'summer');
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) = EXTRACT (YEAR FROM Current_Date)
THEN Sales
ELSE 0
END) AS YTD_Qty
,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = EXTRACT (YEAR FROM Current_Date)
AND EXTRACT (MONTH FROM sales_dt) = EXTRACT (MONTH FROM Current_Date)
THEN Sales
ELSE 0
END) AS MTD_Qty
,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = (EXTRACT (YEAR FROM Current_Date) - 1)
AND EXTRACT (MONTH FROM sales_dt) LE EXTRACT (MONTH FROM Current_Date)
AND EXTRACT (DAY FROM sales_dt) LE EXTRACT (DAY FROM Current_Date)
THEN Sales
ELSE 0
END) AS LastYear_YTD_Qty
,SUM (CASE WHEN EXTRACT (YEAR FROM sales_dt) = (EXTRACT (YEAR FROM Current_Date) - 1)
AND EXTRACT (MONTH FROM sales_dt) = EXTRACT (MONTH FROM Current_Date)
AND EXTRACT(DAY FROM sales_dt) LE EXTRACT (DAY FROM Current_Date)
THEN Sales
ELSE 0
END) AS LastYear_MTD_Qty
The Output:
FROM Sales_History;
YTD_Qty MTD_Qty LastYear_YTD_Qty LastYear_MTD_Qty
404 212 250 145
V2R5 – New Features

Single Version of the Truth Strategic Decision Making


• Partitioned Primary Indexes • Analytic extensions: Extended Windows
• Cylinder read functions and multiple aggregate
• Value List Compression distincts
• 2048 Columns, 64 Column-Indexes • Random Stratified Sampling
• identity Column • Join Elimination
• Extended Transitive Closure
• Partial Group By
Trusted, Integrated Environment and • Early Group By
Administration • Derived Table Rewrite
• Index Wizard • Very Large SQL
• Statistics Wizard and Collection
Enhancements
• Database Query Log Tactical and Event Decision Making
• SQL Assistant - WEB Edition • Partial Covering Join Index
• Availability Enhancements • Global Index
• Sparse Index
Data Freshness • Join index Extensions
• ODS Workload Optimization
• Continuous Update and Manageability • Stored Procedure Enhancements
• Faster Join Index Update
• Join Update Performance
• Bulk Update Performance
• Teradata Warehouse Builder
Enhancements
Teradata Limits (Different Releases)

V2R3 V2R4 V2R4.1 V2R5.0

Maximum Vdisk Size 112 GB 112 GB 1.26 TB 1.26 TB

Maximum Block Size (sectors) 127 127 255 255

Number of concurrent users 32 KB 4 GB 4 GB 4 GB

Concrete Step Size 64 KB 1 MB 1 MB 1 MB

Explain output text max size 64 KB No limit No limit No limit

Max size character string constant 255 32 KB 32 KB 32 KB

Number of spool tables per query 512 2048 2048 2048


Number of levels of query nesting
(subqueries, nested views. Etc.) 10 64 64 64
Maximum # of columns in a Table 256 256 256 2048

Maximum # of columns in an Index 16 16 16 64

Column limit for COLLECT STATISTICS 40 40 40 512

Maximum SQL Statement Limit Size 64K 64K 64K 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

S-ar putea să vă placă și