Professional Documents
Culture Documents
Module 14
ANSI
Inter. Full
CASE
WITH BY FORMAT
Teradata
AS
NAMED
V2R2 allows for sessions to operate in either ... - BTET (Teradata) mode - ANSI mode All syntax, both ANSI and Teradata extensions, is accepted in either mode.
.SET SESSION TRANSACTION BTET; - requires BT to start explicit transaction - requires ET to end explicit transaction - requires neither for implicit transactions
Must be entered prior to LOGON. To change session mode, must LOGOFF first. Affects: - Transaction protocol - Default case sensitivities - Data conversions
BTET mode: A transaction is by definition implicit. Each request is an implicit transaction. BEGIN and END TRANSACTION are used to create larger transactions out of individual requests. BT begins an explicit Txn. ET commits the currently active Txn. 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 of active Txn Locks are accumulated following a BT until an ET is issued.
Transaction Examples
ANSI Mode BTET Mode (explicit) BT Upd A Upd B ET (both commit) BT Upd A Upd B (fails) (both rollback) BTET Mode (implicit) Upd A (A commits) Upd B (B commits)
Upd A Upd B COMMIT (both commit) Upd A Upd B (fails) COMMIT (A commits)
BT Upd A Upd B ROLLBACK (both rollback) BT Upd A Upd B LOGOFF (both rollback)
.SHOW CONTROL;
[SET] FOLDLINE = OFF ALL [SET] FOOTING = NULL [SET] FORMAT = OFF [SET] FORMCHAR = OFF [SET] HEADING = NULL [SET] INDICDATA = OFF [SET] NOTIFY = OFF [SET] NULL =? [SET] OMIT = OFF ALL [SET] PAGEBREAK = OFF ALL [SET] PAGELENGTH = 55 [SET] QUIET = OFF [SET] RECORDMODE = OFF [SET] RETCANCEL = OFF [SET] RETLIMIT = No Limit [SET] RETRY = ON [SET] RTITLE = NULL [SET] SECURITY = NONE [SET] SEPARATOR = two blanks [SET] SESSION CHARSET = ASCII [SET] SESSION SQLFLAG = NONE [SET] SESSION TRANSACTION = BTET [SET] SESSIONS =1 [SET] SIDETITLES = OFF for the normal report. [SET] SKIPDOUBLE = OFF ALL [SET] SKIPLINE = OFF ALL [SET] SUPPRESS = OFF ALL [SET] TDP = l5442 [SET] TITLEDASHES = ON for the normal report. And, it is ON for results of WITH clause number: 1 2 3 4 5 6 7 8 9. [SET] UNDERLINE = OFF ALL [SET] WIDTH = 75
Note: To produce this format, use BTEQ settings: .SET SIDETITLES .SET FOLDLINE To return to the default settings: .SET DEFAULTS
HELP Examples
HELP 'SQL';
*** Query completed. 35 rows found. One column returned. *** Total elapsed time was 1 second. On-Line Help --------------------------------------------------------------------------DBS SQL COMMANDS: ABORT BEGIN TRANSACTION COMMIT CREATE INDEX CREATE USER DELETE DROP DATABASE DROP TABLE ECHO EXECUTE GRANT : ALTER TABLE CHECKPOINT COMMENT CREATE MACRO CREATE VIEW DELETE DATABASE DROP INDEX DROP VIEW END LOGGING EXPLAIN GRANT LOGON : BEGIN LOGGING COLLECT STATISTICS CREATE DATABASE CREATE TABLE DATABASE DELETE USER DROP MACRO DROP STATISTICS END TRANSACTION GIVE HELP :
HELP 'SQL UPDATE'; *** Query completed. 10 rows found. One column returned. *** Total elapsed time was 1 second. On-Line Help --------------------------------------------------------------------------UPD[ATE] tablename [ [AS] aname ] [ FROM tname [ [AS] aname] ] [... ,tname [ [AS] aname ] ] ; SET columnname = expr [ ... ,columnname = expr]
JOIN ON Where:
Column or expression name Table or view name Alias for table or view name Criteria for the join All matching rows Table to the left is used to qualify, table on the right has nulls when rows do not match. Table to the right is used to qualify, table on the left has nulls when rows do not match. Both tables are used to qualify and extended with nulls. Cartesian product
Comparisons
(CS) result
SELECT first_name , last_name FROM employee WHERE last_name(CS) LIKE %Ra%; first_name last_name Larry Ratzlaff Peter Rabbit
Comparisons
Explicit CS result
SELECT first_name , last_name FROM employee WHERE last_name(CS) LIKE %Ra%;
first_name last_name Larry Ratzlaff Peter Rabbit
SELECT test_num, comment FROM test_results WHERE comment LIKE %95a%% ESCAPE a;
SELECT filename, filesize FROM file_table WHERE filename LIKE %ANSIb_SQLb_% ESCAPE b;
filename ANSI_SQL_QRY3
filesize 2068
Data Types
Teradata INTEGER SMALLINT BYTEINT DATE TIME TIME WITH ZONE TIMESTAMP TIMESTAMP WITH ZONE FLOAT FLOAT FLOAT DECIMAL(n,m) DECIMAL(n,m) CHAR(n) VARCHAR(n) CHAR VARYING(n) LONG VARCHAR BYTE(n) VARBYTE(n) GRAPHIC(n) VARGRAPHIC(n) LONG VARGRAPHIC ANSI (Entry) equivalent INTEGER SMALLINT
SET MULTISET
DATABLOCKSIZE Maximum multi-row blocksize for table in: BYTES KILOBYTES (or KBYTES) Rounded to nearest sector (512) Increments of 1024
(6144) (65024)
FREESPACE
Column Attributes
Attribute UPPERCASE CASESPECIFIC FORMAT TITLE NAMED/AS COMPRESS COMPRESS NULL COMPRESS value WITH DEFAULT DEFAULT DATE DEFAULT TIME NOT NULL DEFAULT value DEFAULT USER DEFAULT NULL
ANSI
Teradata Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes
Indexes
Primary index - one or more columns used to distribute and locate the rows of a table (one per table). Primary index(PI) choice will affect distribution, access and thus performance. Indexes (primary or secondary) may be used to enforce uniqueness. Indexes (primary or secondary) may be used to improve access. Indexes (primary or secondary) may be unique or nonunique.
Primary Key column(s) must be defined NOT NULL. Referenced columns must be defined as unique. Up to 64 foreign keys may be defined on a table. User must have REFERENCES right on referenced table.
What is a Trigger?
A Trigger may be defined as: One or more stored SQL statements associated with a table An event driven procedure attached to a table An object in a database, like tables, views and macros Any of the following SQL statements may be applied to triggers: CREATE DROP SHOW ALTER RENAME REPLACE HELP
TRIGGERS
Triggers may not be used in conjunction with: The FastLoad utility The MultiLoad utility Updateable Cursors (Preprocessor environment) DELETE DATABASE
DELETE USER
Privileges are required to create and drop triggers. GRANT REVOKE CREATE DROP TRIGGER
Lab Environment
DBC
UNIX userids u____01 u____02 u____xx
Note: ___ Instructor Initials
Teradata_Factory AU
Accounts Customer Trans Lab3_1_1 Lab3_1_2
Accounts Customer Trans
____Students
T____01 T____02
Accounts Customer Trans
T____xx
Accounts Customer Trans
JOIN ON
Where: cname tname aname condition INNER JOIN LEFT OUTER JOIN Column or expression name Table or view name Alias for table or view name Criteria for the join All matching rows Table to the left is used to qualify, table on the right has nulls when rows do not match. Table to the right is used to qualify, table on the left has nulls when rows do not match. Both tables are used to qualify and extended with nulls. Cartesian product
Inner Joins
An inner join returns an output row for each successful match between the join tables. SELECT D.Department_Number AS Dept ,Department_Name ,Last_name ,E.Department_Number AS EmpDept FROM Department D INNER JOIN Employee E ON E.Department_Number = D.Department_Number;
Dept Department_Name 402 software support 100 executive 501 marketing sales 301 research and development 301 research and development
Information about employees and their department names where the employees department number matches the existing departments. No information about employees who have no department number or an invalid department number. No information about departments which have no employees assigned to them.
Outer Joins
An outer join returns matching rows and nonmatching rows.
SELECT D.Department_Number AS Dept ,Department_Name ,Last_name ,E.Department_Number AS EmpDept FROM Employee E FULL OUTER JOIN Department D ON E.Department_Number = D.Department_Number;
Dept 600 402 ? 501 301 ? 100 301 Department_Name new department software support ? marketing sales research and development ? executive research and development Last_Name ? Crane James Runyon Kanieski Green Trainer Stein EmpDept ? 402 111 501 301 ? 100 301
FROM ON
;
department_number 402 111 501 301 ? 100 301 department_name software support ? marketing and sales research and develop ? executive research and develop last_name Crane James Runyon Stein Green Trainer Kanieski
In addition to output from inner join: Shows employees with null departments. Shows employees with invalid departments.
FROM
ON
;
department_name new department software support executive marketing sales research and develop research and develop
Returns all employees including: employees with invalid departments employees with null departments employees with invalid job codes employees with null job codes
ON Clause Placement
The placement of the ON clause in the SELECT is important. The rules are: The first ON clause (from left to right) is evaluated first. An ON clause applies to the immediately preceding join operation. The same SELECT example could be written as: SELECT last_name AS Employee ,department_name AS Dept ,description AS Job FROM Department D RIGHT OUTER JOIN Employee E LEFT OUTER JOIN Job J ON E.Job_Code = J.Job_Code ON D.Department_Number = E.Department_Number; Using parenthesis can clarify the reading of the statement. SELECT last_name AS Employee ,department_name AS Dept ,description AS Job FROM (Department D RIGHT OUTER JOIN (Employee E LEFT OUTER JOIN Job J ON E.Job_Code = J.Job_Code ) ON D.Department_Number = E.Department_Number);
Cross Joins
A cross join is a join that requires no join condition. Cross joins are sometimes called product joins. Each participating row of one table is joined with each participating row of another table. Cross join syntax does not allow an ON clause.
A WHERE clause may restrict which rows participate from either table.
SELECT e.employee_number ,d.department_number FROM employee e CROSS JOIN department d WHERE e.employee_number = 1008;
employee_number department_number --------------------------- ---------------------------1008 501 1008 301 1008 402 1008 201 1008 302 1008 600 1008 401 1008 100 1008 403
One employee matched with nine departments results in nine rows of output. Output is not meaningful.
Cartesian Products
A completely unconstrained cross join is called a Cartesian Product. Each row of one table is joined to each row of another table. A Cartesian product results from a CROSS JOIN without a WHERE clause.
SELECT e.employee_number ,d.department_number FROM employee e CROSS JOIN department d;
Each employee row (26) matched with each department row (9) yields 234 rows of output. An 8,000,000 row table and a 50,000 row table would yield a 400,000,000,000 row answer set. The output of a Cartesian product is rarely meaningful. Cartesian products can also result from an INNER JOIN with improper aliasing or improper join conditions.
SELECT employee.employee_number ,d.department_number FROM employee e INNER JOIN department d ON e.department_number = d.department_number;
Thank You!!!