Top Banner
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.
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

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 Teradataand ANSI.

Distinguish between ANSI and Teradata case sensitivities. Describe 2 features of the System Calendar. Describe how space is allocated for volatile and globaltemporary tables.

Teradata SQLOS 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 DifferencesANSI TeradataTeradata 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 ModesTransaction 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 ExamplesANSI 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 RequestsUPDATE 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 ModeTeradata Rules Column AttributesStorage Comparisons As entered (default) UPPERCASE UPPER, LOWER CASESPECIFIC (CS) NONCASESPECIFIC (NOT CS) (Teradata default)

Default (NOT CS) resultSELECT 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) resultSELECT first_name, last_name FROM Employee WHERE last_name(CS) LIKE '%Ra%'; first_name --------------Larry Peter last_name --------------Ratzlaff Rabbit

CASE Sensitivity Issues ANSI ModeTeradata Rules Column AttributesStorage Comparisons None (As entered is default) UPPER, LOWER (Case specific is default)

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

Explicit (CS) resultSELECT 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 FlaggerTeradata 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 CommandHELP 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.

Id 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 LayoutColumns 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 CalendarSELECT * 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 2003S 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 CalendarShow 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 ChoicesDerived 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 RevisitedGet 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)