TERADATA
Introduction: The teradata is a relational data base management system which is
specially designed to build an enterprise DWH.
The teradata uses parallelism to store (or) manage tera bytes of
business data.
A teradata is required to build decision supporting system.
Teradata supports to run on windows and UNIX operating systems.
Teradata has unlimited parallelism with unlimited scalability. It
makes teradata worse time data base to build Decision Supporting System
(DSS).
TERADATA
The teradata supports massively parallel processing with shared
nothing architecture.
COMPUTING ARCHITECTURES: There are two scalable hardwares that can processes and manages
the data
i.
ii.
SMP (Symmetric Multi Processor (or) Shared Memory
Processor)
MPP (Massively Parallel Processor)
Symmetric Multi Processor (SMP): Its a shared memory architect.
Limited parallelism up to 64 processors.
Limited scalability up to 64 processors.
MASSIVELY PARALLEL PROCESSOR (MPP): Unlimited parallelism
Unlimited scalability
Linear expendability
Massively parallel processing with shared nothing architect
Linear parallelism
Ex: - Tera data, Metezza, Green plum (all are data bases)
TERADATA
Integration of hundreds and thousands of uni processors
Data stage (ETL Tool)
TERADATA SERVER ARCHITECTURE: Teradata Client: A Teradata client is an application that allows you to prepare SQL
request.
There are two teradata queries submitting utilities.
i.
Teradata SQL Assistant
ii.
BTEQ (Basic Teradata Query)
TERADATA
COMPONENTS & ARCHITECTURE: -
Parsing Engine: A parsing engine is made up of following software components
i.
Parser
ii.
Optimizer
iii.
Dispatcher
Parser: It performs the following activities when the request is received from
the client
I.
It validates the users access right to the objects.
II.
It interprets the SQL into the machine language.
III.
Validates the SQL query for a syntactical check.
Optimizer: The optimized role is to prepare least expensive query execution
plan.
Least expensive is measured in terms of system resource usage.
Dispatcher: -
MESSAGE PASSING LAYER (MPL): The message passing layer manages the dialogues between parsing
engine access module processor (AMP).
TERADATA
The dispatcher dispatches the query execution plan to the
messaging passing layer (network).
It makes teradata parallelism possible.
Merging the answers-sets back to the parsing engine.
The message passing layer is a combination of
a. BYNET Software
b. BYNET Hardware for MPP
c. Parallel data base extensions software
ACCESS MODULE PROCESSORS: It processes the SQL requests.
Access Module Processor stores and retrieve rows to and from disks.
Access Module Processor performs the following tasks
1. Finding the rows requested
2. Lock management
3. Accounting
4. Recovery processing
5. Sorting the rows
6. Aggregating the rows
7. Joining the data
8. Output conversions
9. Creating Answers-sets for client
VIRUTIAL DISK (V-Disk): It is a combination of inter connected physical storage units.
TERADATA
Each AMP manages portion of total data
TERADATA COMPITITIVE ADVANTAGES: Scalability: This is the only RDBMS which confirms the performance
improvement by adding more hardware.
Maintenance: When you add hardware the data gets distributed automatically.
Massively Parallel Processing: It works based on massively parallel processing architecture.
Shared Nothing Architecture: Using special software and hardware components, teradata gets the
data faster.
Mature Optimizer: It handles complex queries up to 64 joins per query.
Unlimited Parallelism: -
It can add hundreds and thousands of processors to achieve the
linear parallelism.
It provides a single version of business.
Low CTO (Total Cost of Ownership): Ease of setup, maintenance, administration expansion.
Parallel Load and Unload Utilities: -
TERADATA INSTALLATION PROCEDURE: Click on SETUP
Click on Main Menu
TERADATA
Robust, parallel and scalable load and unload utilities such as BTEQ,
Fast load, Multi Load, T-pump and Fast Export.
Click on Install Teradata Express
Click on Installation Menu
Install the following components one after the other
i.
Teradata BYNET Driver
ii.
Teradata Database
iii.
Teradata Express Tools
iv.
Teradata Tools and Utilities
v.
Teradata Warehouse Miner
CREATION ODBC CONNECTIONS: Start settings Control Panel Administrative Tools Data
Sources (ODBCS)
Select a system DSN
Click on add
Select a driver Teradata
Click on Finish
Enter the following details
Name: - TD_USER {our wish}
IP address: - 127.0.0.1 {for local system}
User Name: - TDUSER
Password: - TDUSER
Click OK.
Indexes are used to access rows from a table without having to
search the whole table. In the teradata RDBMS, an index is made up of
one (or) more columns in a table. Once teradata indexes are selected they
are maintained by the system.
In the Teradata RDBMS, there are two types of indexes
TERADATA
TERADATA INDEXES: -
o Primary indexes define the way the data is distributed.
o Primary indexes and Secondary indexes are used to locate the
data rows more efficiency than searching the whole table.
PRIMARY INDEXES: When the primary indexes for the table is well chosen the table rows
are evenly distributed across the multiple AMPs for the best performance.
The primary index is created at the time of creating table.
A primary index created on single column (or) combination of
columns.
There are two types of primary indexes,
I.
II.
Unique Primary Index
Non Unique Primary Index
Note: - Changing the choice of the primary index requires dropping and
recreating the table.
UNIQUE PRIMARY INDEX (UPI): Syntax: - Create Table Sample_1
(COL A_INTEGER
, COL B_INTEGER
, COL C_INTEGER)
Unique Primary Index (COL_B);
A unique primary index results in distributing the roes very uniform.
TERADATA
Maximum efficiency for parallel operations.
NON-UNIQUE PRIMARY INDEX (NUPI): Row distribution is less uniform.
** Rows with the same primary index value distributed to the same
AMP.
TERADATA
HASH MAP: A hash map is uniquely configured for each system.
It is an array of 65,536 entries which associates with the specific
AMPs.
The system will check for the duplicates unique primary index value
on row inserts.
A new row is discarded if already row exist with the UPI value.
STRUCTURE QUERY LANUAGUE: - (SQL)
It is a relational language designed for relational database
management system to access and maintains databases.
It is a fourth generation non-procedural set oriented language for
relational databases.
The objects in the relational database are created and maintain
using relational language.
TERADATA
The objects definitions are stored in a data dictionary (or) data
directory.
SQL-SUB COMPONENTS: DATA DEFINTION LANGUAGE (DDL): The DDL allows the users to define the database objects and create
relationship among database objects. (Tables, Views, Triggers, Procedures,
Macros)
The following SQL command defines DDL.
SQL Statement
CREATE
DROP
ALTER
Function
It defines a database object
(Tables, Views, Procedures, Macros,
Triggers (or) Stored Procedure).
It removes a Tables, Views,
Procedures, Macros, Triggers (or)
Stored Procedure.
It changes a database object
definition
DATA MANIPULATION LANGUAGE (DML): The DML consists of a statement that lets the user to insert, update
and delete the data rows of database.
If DDL defines the database the DML lets the user change the
information contained in the database.
10
The DML is the most commonly used sub-set of SQL.
The following commands define DML.
INSERT: - It places a new record into a table.
UPDATE: - It manipulates the data values in one (or) more existing
records.
DELETE: - It removes one (or) more rows from table.
DATA RETRIEVEAL LANGUAGE (DRL): -
The following command defines DRL
SELECT: - It selects the data from one (or) more tables.
DATA CONTROL LANGUAGE (DCL): -
TERADATA
It lets the user to select the data rows from one (or) more tables.
It is used to restrict (or) permit user access to the database
The following commands define DCL.
GRANT: - It gives user privileges on database objects.
REVOKE: - It removes user privileges on database objects.
TERADATA DATABASE: A teradata database is a group of objects such as Tables, Views,
Procedures, Macros, Triggers (or) Stored Procedure.
Every database is created with the following attributes
PERM SPACE: - (Permanent Space) a space which requires to stores the
data in the objects such as tables.
TEMP SPACE: - (Temporary Space) a space which requires to temporarily
stored the query results (TEMP tables).
SPOOL SPACE: - A space which is required to process user request.
A teradata database is created using CREATE database command.
A database is empty until objects are created.
A teradata administrator creates database using component.
Procedure: - (Creation of Database)
11
Start Teradata Service
Start Programs Teradata Administrator
Select the data source TDADIM
Click on OK
From TOOLS Menu select CREATE
Click on Database
Perm Space: - 50MB
Spool Space: - 40MB
Click on CREATE
TERADATA
Enter the Database Name: - SALESEDW (our wish)
TERADATA - USER: A teradata user is a database with an assigned password.
A teradata user may logon to teradata and access the objects within
in itself, can access other databases for which it has access rights.
**A user can access the database using ODBC.
A user is created with following attributes
I.
PERM Space
II.
SPOOL Space
III.
TEMP Space
A user is created using Create User Command.
A user is empty until objects are created.
Procedure: - (Creation of User)
Start Programs Teradata Administrator
Select the data source TDADMIN
Click on OK
From Tools Menu select CREATE
Click on User
12
Enter the User Name: - BOA (Our Wish)
Password: - BOA
PERM Space: - 50MB
SPOOL Space: - 40MB
Click on CREATE
GRANTING PERMISSIONS TO ACCESS OBJECTS: From Tools Menu Grant/Revoke
Select Objects Rights
From database select database (BOAEDW)
TERADATA
Procedure: -
From Tab TO/FROM USER
Select User: - BOA
Select ALL
Click on GRANT
Click on CLOSE
CREATION OF ODBC: Start Settings Control Panel Administrative Tools Data
Source ODBC
Select System DSN Tab
Select ADD
Select the drive Teradata
Click on Finish
Name: - BOAEDW_ODBC (Our Wish)
IP Address: - 127.0.0.1
User Name: - BOA
Password: - BOA
Default Database: - BOAEDW
13
Click on OK
CREATION OF INFORMATICA WITH TERADATA: Source Definition: EMP (Oracle)
DEPT (Oracle)
Target Definition: -
DEPT (Teradata)
Creation of Pass through Mapping
Creation of writer connection using ODBC
TERADATA
EMP (Teradata)
Procedure: Open the client Power Center Work-flow Manager
From Connections Menu select Relational
Select the type ODBC
Click on NEW
Enter the following details
Name: - TDUSER
User Name: - TDUSER
Password: - TDUSER
Connect String: - TDUSER
Click on OK
Create on Session
Double click the Session, select the Mapping Tab
From the left window select the Source
Set Reader connection to SCOTT
From the left window select the Target
Set Writer connection to TDUSER, select the Target load type Normal
14
From Properties
Attribute
Value
Table Name Prefix
NIPUNAEDW (Database Name)
Click on APPLY
Click on OK
CONSTRAINTS: -
If the given values satisfy the condition the database will accept the
record else it gets rejected. Teradata supports the following constraints
I.
Not Null
II.
Unique
III.
Primary key
IV.
Check
V.
Foreign key/ Reference key
TERADATA
Constraints are the Conditions (or) Business Rule defines on one (or)
more attributes to variable the values given by the user.
NOT NULL CONSTRAINTS: Create Table DEMO
(EMPNO NUMERIC (4) NOT NULL,
ENAME VARCHAR (10),
SAL NUMERIC (7, 2))
Insert into DEMO values (NULL, SMITH, 800)
Insert into DEMO values (7369, SMITH, 800)
Insert into DEMO values (7489, NULL, 800)
Select * from DEMO
EMPNO
ENAME
SAL
7489
800
7369
SMITH
800
15
We can use NULL constraint on any column only when table is
created.
CHECK CONSTRAINT: To insert a constraint we use CHECK Constraint.
Syntax: ALTER TABLE DEMO
ADD constraint CHK_EN_EN CHECK (ENAME IS NOT NULL)
Insert into DEMO values (7521, NULL, 1000)
Insert into DEMO values (7521, WARD, 1000)
To copy the records from one table to other table without NULL
command is
TERADATA
Update DEMO set ENAME= , where EMPNO=7489
Insert into DEMO select * from DEMO;
UNIQUE CONSTRAINT: Create Table DEMO
(EMPNO NUMERIC (4) UNIQUE,
ENAME VARCHAR (10),
SAL NUMERIC (7, 2))
Command does not works without NOT NULL constraint
Create Table DEMO
(EMPNO NUMERIC (4) NOT NULL UNIQUE,
ENAME VARCHAR (10),
SAL NUMERIC (7, 2))
Insert into DEMO values (7521, WARD, 1250)
Insert into DEMO values (7521, SMITH, 1250) Record not
inserted
Insert into DEMO values (7369, SMITH, 1000)
Insert into DEMO values (NULL, ALLEN, 850) Record not inserted
16
PRIMARY KEY: To identify a record UNIQELY from a database
Create Table STUD (this table is not create without NOT NULL)
(STNO NUMERIC (3) CONSTRAINT PK_STNO PRIMARY KEY,
SNAME VARCHAR (12),
MKS NUMERIC (13))
(STNO NUMERIC (3) NOT NULL CONSTRAINT PK_STNO PRIMARY KEY,
SNAME VARCHAR (12),
MKS NUMERIC (3))
TERADATA
Create Table STUD (this is using NOT NULL)
Insert into STUD values (5, SMITH, 25)
Insert into STUD values (7, ALLEIN, 50)
Insert into STUD values (NULL, WARD, 40) invalid record not
Stored
Insert into STUD values (7, WARD, 40) invalid record not Stored
Create a Table STUDENT (combination of Secondary (or) Alternate
Key)
(BCODE VARCHAR (3) NOY NULL,
ROLLNO NUMERIC (3) NOT NULL,
SNAME VARCHAR (10),
MKS NUMERIC (3),
CONSTRAINT PK_BR PRIMARY KEY (BCODE, ROLLNO))
NOTE: **NOT NULL can be used in table level
Combination of two Primary Keys is known as SUPER KEY
When a Constraint is defined at end of database description is called
as Table Level Constraint.
17
When a Constraint is defined at the TOP of database description is
called as Column Level Constraint.
Insert into STUDENT values (B1,101, SMITH, 35)
Insert into STUDENT values (B1,102, ALLEIN, 40)
Insert into STUDENT values (B1,103, WARD, 50)
Insert into STUDENT values (B2,101, SMITH, 45)
Insert into STUDENT values (B2,102, ALLEIN, 50)
Insert into STUDENT values (B1,101, STEEVE, 50) record not
inserted
Create Table DEMO
TERADATA
Insert into STUDENT values (B2,103, WARD, 55)
EMPNO NUMERIC (4) NOT NULL CONSTRAINT PK_ENO PRIMARY KEY,
ENAME VARCHAR (10),
JOB VARCHAR (10),
SAL NUMERIC (7, 2),
CONSTRAINT CHK_SAL CHECK (SAL BETWEEN 2000 AND 5000),
DEPTNO NUMERIC (2))
Insert into STUDENT values (7001, SMITH, CLERK, 800, 20)
below bound
Insert into STUDENT values (7001, SMITH, CLERK, 800, 20)
above bound
Insert into STUDENT values (7001, SMITH, CLERK, 4000, 20)
Insert into STUDENT values (7002, ALLEIN, SALESMAN, NULL, 20)
ALTER Table DEMO
ADD CONSTRAINT CHK_SAL_NULL CHECK (SAL is not NULL)
Update DEMO set SAL=2800 where EMPNO=7002
Insert into STUDENT values (7003, SMITH, HYETFJ, 4500, 30)
ALTER Table DEMO
18
ADD CONSTRAINTS CHK_JOB
CHECK (JOB IN (ANALYST, CLERK, MANAGER, SALESMAN))
Update DEMO set JOB=ANALYST where EMPNO=7003
NOTE: Constraint cant be altered, so drop the Constraint and ADD a new
constraint with requirement.
Insert into STUDENT values (7005, STELLA, STENO, 2500, 30)
CHECK (JOB IN (ANALYST, SALESMAN, CLERK, MANAGER, STENO))
Update DEMO set JOB=STENO, where EMPNO=7005
FOREIGN KEY: -
TERADATA
ADD Constraint CHK_JOB
It must relate to a Primary Key (or) Unique.
Create Table DEPT1
(DEPTNO NUMERIC (2) NOT NULL CONSTRAINT PK_DNO PRIMARY
KEY,
DNAME VARCHAR (10))
Insert into DEPT1 values (10, ACCOUNTING)
Insert into DEPT1 values (20, RESEARCH)
Insert into DEPT1 values (10, SALES)
Create Table EMP1
(EMPNO NUMERIC (4) NOT NULL PRIMARY KEY,
ENAME VARCHAR (10),
SAL NUMERIC (7, 2),
DEPTNO NUMERIC (2)
CONSTRAINT FK_DNO REFERENCES DEPT1 (DEPTNO))
Insert into DEPT1 values (7002, ALLEIN, 1500, 40)
Insert into EMP1 values (40, OPERATIONS)
19
Unless child record is not deleted (or) inserted master record is not going
to delete (or) insert.
JOINS AND TYPES OF JOINS: A join is a concept in a relational database theory.
A join is a technique for accessing the data from one (or) more
tables in a single answer set.
The tables should be join based on Common Column Values.
Joins can be used to make a view of data from more than one table.
The following are the types of joins
I.
Inner Join (Equi Join)
II.
Left-Outer Join
III.
Right-Outer Join
IV.
Full-Outer Join
V.
Cross Join
VI.
Cartesian Join
VII.
Accidental Cartesian Join
VIII.
Self Join (Recursive Join)
TERADATA
The each row in the answer set contains data from columns of more
than one table.
INNER JOIN: It combines the data records from multiple tables based on Equality
Match.
An Inner Join represents the intersection of the two sets based on
common set of values called Join values.
Non ANSI Syntax: Select EMP.EMPNO, EMP.ENAME, EMP.JOB, EMP.SAL, EMP.DEPTNO,
EMP.DNAME, DEPT.LOC from EMP, DEPT where
EMP.DEPTNO=DEPT.DEPTNO
ANSI Syntax: -
20
Select E.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, D.DNAME,
D.LOC from EMP E INNER JOIN DEPT D on EMP.DEPTNO=DEPT.DEPTNO
EX: -
EMPNO
1245
1389
ENAME
JOB
SAL
DEPTN
O
SIVA
CLERK
5000
10
DEPTN
O
NARAYANA
MANAGE
R
8000
20
10
SALES
HYD
20
ACCOUNT
S
BAN
30
MARKET
CHE
40
OPERATIO
NS
MU
M
1429
SURYA
CLERK
5000
30
1587
NANI
CLERK
5000
50
DNAME
LOC
TERADATA
EMP DETAILS
DEPT DETAILS
Results of Inner Join: EMPNO
ENAME
JOB
SAL
DEPTNO
DNAME
LOC
1245
SIVA
CLERK
5000
10
SALES
HYD
1389
NARAYAN
A
MANAGE
R
8000
20
ACCOUN
TS
BAN
1429
SURYA
CLERK
5000
30
MARKET
CHE
OUTER JOIN: An Outer Join forces to combine the records even though there is no
Equality Match.
As a result an Outer Join can produce more number of records than
Inner Join.
Formulae: Outer Join= Inner Join + Non-Matching Records
LEFT OUTER JOIN: A Left Outer Join returns Inner Join rows and non-matching rows from
Left table.
21
ANSI Syntax: Select E.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, D.DNAME,
D.LOC from EMP E LEFT OUTER JOIN DEPT D on
EMP.DEPTNO=DEPT.DEPTNO
EMPNO
ENAME
JOB
SAL
DEPTNO
DNAME
LOC
1245
SIVA
CLERK
5000
10
SALES
HYD
1389
NARAYAN
A
MANAGE
R
8000
20
ACCOUN
TS
BAN
1429
SURYA
CLERK
5000
30
MARKET
CHE
1587
NANI
CLERK
5000
50
NULL
NULL
TERADATA
EX: -
RIGHT OUTER JOIN: A Right Outer Join returns Inner Join rows and non-matching rows
from Left table.
ANSI Syntax: Select E.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, D.DNAME,
D.LOC from EMP E RIGHT OUTER JOIN DEPT D on
EMP.DEPTNO=DEPT.DEPTNO
EX: EMPNO
ENAME
JOB
SAL
DEPTNO
DNAME
LOC
1245
SIVA
CLERK
5000
10
SALES
HYD
1389
NARAYAN
A
MANAGE
R
8000
20
ACCOUNT
S
BAN
1429
SURYA
CLERK
5000
30
MARKET
CHE
NULL
NULL
NULL
NULL
40
OPERATIO
NS
MUM
FULL OUTER JOIN: A Full Outer Join returns non-matching rows from both tables.
ANSI Syntax: -
22
Select E.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, D.DNAME,
D.LOC from EMP E FULL OUTER JOIN DEPT D on
EMP.DEPTNO=DEPT.DEPTNO
EMPNO
ENAME
JOB
SAL
DEPTNO
DNAME
LOC
1245
SIVA
CLERK
5000
10
SALES
HYD
1389
NARAYAN
A
MANAGE
R
8000
20
ACCOUNT
S
BAN
1429
SURYA
CLERK
5000
30
MARKET
CHE
1587
NANI
CLERK
5000
50
NULL
NULL
NULL
NULL
NULL
NULL
40
OPERATIO
NS
MUM
TERADATA
EX: -
JOINS USING ALIAS: An Alias is: A temporary name for a table (or) view
Defined in the FROM clause
Useful for abbreviating long table names
Required to join a table to itself
CROSS JOIN: 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 allows ON clause
A WHERE clause may be used to restrict which row participates from
either table
ANSI Syntax: Select E.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, D.DNAME,
D.LOC from EMP E CROSS JOIN DEPT D where E.SAL>2000
23
Non-ANSI Syntax: Select EMP.EMPNO, EMP.ENAME, EMP.JOB, EMP.SAL, EMP.DEPTNO,
DEPT.DNAME, DEPT.LOC from EMP, DEPT where EMP.SAL>2000
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.
The output of Cartesian product is rarely meaningful.
Cartesian product can also result from an Inner Join with improper
Aliasing (or) Join conditions.
TERADATA
A Cartesian product results from a Cross Join without a WHERE
clause
Non-ANSI Syntax: Select EMP.EMPNO, EMP.ENAME, EMP.JOB, EMP.SAL, EMP.DEPTNO,
DEPT.DNAME, D.LOC from EMP, DEPT
ACCIDENTAL CARTESIAN PRODUCTS: A Cartesian product results from an Inner Join with improper
aliasing.
Improper Join condition in a Join can result Accidental Cartesian Join.
Non-ANSI Syntax: Select EMP.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, D.DNAME,
D.LOC from EMP E FULL INNER JOIN DEPT D on
EMP.DEPTNO=DEPT.DEPTNO
Select E.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, D.DNAME,
D.LOC from EMP E FULL INNER JOIN DEPT D on 3=3
INNER JOIN ON MULTIPLE TABLES: A Join can have up to 64 participating tables (or) views.
The number of participating tables (or) views determines the
number of require Join conditions.
An n-table Join requires n-1 Join conditions.
Omitting a Join condition will result in a Cartesian product Join.
24
SELF JOIN: A Join which is made on the same table is known as Self Join.
A Self Join is also known as Recursive relationship.
EX: Each Employee has a manager, who is also an Employee
Syntax: -
, E.ENAME AS EMP_NAME
, M.MGR AS MANAGER_NUMBER
, M.ENAME AS MANAGER_NAME
TERADATA
Select E.EMPNO as EMP_NUMBER
From EMP E JOIN EMP M on E.MGR=M.EMPNO
SET OPERATORS: The following key words are available as ANSI standard set
operators
I.
UNION
II.
UNION ALL
III.
INTERSECT
IV.
EXCEPT (MINUS is a NON-ANSI synonym)
UNION: The Union operator allows combining the two data sets into a single
answer set.
The Union set operator eliminates duplicates.
Syntax: SELECT * FROM EMP UNION SELECT * FROM EMP;
UNION ALL: It allows duplicates in the answer set
25
Syntax: SELCET * FROM EMP UNION ALL SELECT * FROM EMP;
SELECT * FROM EMP (N) UNION ALL SELECT * FROM EMP WHERE
ROWNUM < (N/2+1)
Here N is number of records
INTERSECT: -
Syntax: CREATE a Table EMP1 as (SELECT * FROM EMP WHERE DEPTNO=20)
WITH DATA;
SELECT * FROM EMP INTERSECT SELECT * FROM EMP1;
TERADATA
The Intersect operator captures common content of two data sets
into a single answer set.
EXCEPT: The Except operator produces the difference in contents of the two
sets
Syntax: CREATE a Table EMP1 as (SELECT * FROM EMP WHERE DEPTNO=20)
WITH DATA;
SELECT * FROM EMP EXCEPT SELECT * FROM EMP1;
AGGREGATE FUNCTIONS: The Aggregation is a process of calculating the summaries for a
group of records.
Use the following Aggregate Functions to calculate the summaries
(Aggregations)
o SUM
o AVERAGE
o MAX
o MIN
o COUNT
26
Syntax: i)
SELCET DEPTNO, SUM (SAL) FROM EMP GROUP BY DEPTNO
ORDER BY DEPTNO
ii)
TARGET (DEPTNO, DNAME, SUMSAL)
Thumb Rule: If the select statement contains aggregates, other than aggregates
appears in Group By clause.
iii)
TERADATA
SELECT EMP.DEPTNO, DEPT.DNAME, SUM (SAL) FROM EMP, DEPT WHERE
EMP.DEPTNO=DEPT.DEPTNO GROUP BY EMP.DEPTNO, DEPT.DNAME
SELECT DEPTNO, JOB, SUM (SAL) FROM EMP GROUP BY 1,2
(DEPTNO, JOB) ORDER BY 1,2 (DEPTNO, JOB)
HAVING CLAUSE: It is used to make a restriction on summary records
Syntax: SELECT DEPTNO, SUM (SAL), MAX (SAL), MIN (SAL), AVG (SAL) FROM
EMP GROUP BY DEPTNO ORDER BY DEPTNO HAVING AVG (SAL) > 3000
Some of Conditions without using Sub-Query: SELECT * FROM EMP WHERE ENAME=JONES
SELECT * FROM EMP WHERE SAL>2975 {request for finding sal >
JONES in the table}
SELECT T1.EMPNO, T1.ENAME, T1.JOB, T1.SAL, T1.DEPTNO FROM
EMP T1, EMP T2 WHERE T2.ENAME=JONES AND T1.SAL>T2.SAL
SELECT T1 * FROM EMP T1, EMP T2 WHERE T2.ENAME=JONES AND
T1.SAL>T2.SAL
Insert into EMP values (7935, JONES, MANGER, 7839,
17/07/2011, 2000, NULL, 20)
27
SELECT T1.EMPNO, T1.ENAME, T1.JOB, T1.SAL, T1.DEPTNO FROM
EMP T1, EMP T2 WHERE T2.ENAME=JONES AND T1.SAL>T2.SAL
ORDER BY {if table contains another employee namely Jones}
CONCEPT OF SUB-QUERY: When a query is dependent on the output of another query for its
predicate (condition) then we use the Sub-queries.
The query which makes use of output written by the sub-query is
called Main (or) Parent (or) Route query.
When two (or) more sub-queries are used in the predicate of route
query then it is called as multiple sub-queries.
TERADATA
A sub-query is a query used in the predicate of another query.
When a sub-query is used in the predicate of another sub-query
then it is called as nested sub-query.
Note: The sub-query can be on the same table which is used in the main
query (or) it can be on any other table of database.
SELECT SAL FROM EMP WHERE ENAME=JONES
SELECT * FROM EMP WHERE SAL>2975
SELECT * FROM EMP
WHERE SAL (SELECT SAL FROM EMP WHERE ENAME=JONES)
SELECT * FROM EMP WHERE JOB=(SELECT JOB FROM EMP WHERE
ENAME=BLAKE) AND ENAME1=BLAKE
SELECT * FROM EMP WHERE SAL> (SELECT SAL FROM EMP WHERE
ENAME=JONES) OR JOB= (SELECT JOB FROM EMP WHERE
ENAME=SMITH) {multiple sub-query}
Where a query to find the Employees from DEPTNO=20 whose JOB is
same as anyone of the JOB from the DEPT=SALES?
Solution: SELECT DEPTNO FROM DEPT WHERE DNAME=SALES
28
SELECT DISTINCT JOB FROM EMP WHERE DEPTNO=30
SELECT * FROM EMP WHERE DEPTNO=20 AND JOB IN (CLERK,
MANGER, SALESMAN)
SELECT * FROM EMP WHERE DEPTNO=20 AND JOB IN (SELECT
DITINCT JOB FROM EMP) WHERE DEPTNO= (SELECT DEPTNO FROM
DEPT WHERE DNAME=SALES)) {nested sub-query}
Solution: SELECT * FROM EMP WHERE DEPTNO=30 ORDER BY SAL
SELECT * FROM EMP WHERE DEPTNO=20 ORDER BY SAL
TERADATA
Write a query to find the list of all the employees from DEPTNO=20
whose salary is greater than any one of the salary from the
DEPTNO=30?
SELECT * FROM EMP WHERE DEPTNO=20 AND SAL> (SELECT MIN
(SAL) FROM EMP WHERE DEPTNO=30)
SELECT * FROM EMP WHERE DEPTNO=20 AND SAL> (SELECT MAX
(SAL) FROM EMP WHERE DEPTNO=30)
SELECT * FROM EMP WHERE DEPTNO=20 AND SAL>ANY (SELECT
SAL FROM EMP WHERE DEPTNO=30)
SELECT * FROM EMP WHERE DEPTNO=20 AND SAL>ALL (SELECT
SAL FROM EMP WHERE DEPTNO=30)
SELECT * FROM EMP (MAX SAL OF ONE EMPLOYEE) WHERE SAL=
(SELECT MAX (SAL) FROM EMP)
SELECT * FROM EMP (MAX SAL OF SECOND SALES)
WHERE SAL= (SELECT MAX (SAL) FROM EMP WHERE SAL< (SELECT
MAX (SAL) FROM EMP WHERE SAL> (SELECT MAX (SAL) FROM EMP)))
VIEWS: A view is like a window into one (or) more tables. It provides
customized access to base tables by,
Restricting which columns are visible from base tables
Restricting which rows are visible from base tables
Combining columns and rows from several base tables
29
A view is virtual table
It may define a subset of row of row of a table
It may define a subset of a table
It may reference more than one table
View definitions are stored in the data dictionary, not in the users
own space
Simple View: view.
A view is created using CREATE VIEW command.
It does not occupy any space because it does not contain any data
on its own.
TERADATA
A view which is created from single base table is known as simple
Syntax: DATABASE SALES
CREATE VIEW V_EMP_TEST AS SELECT * FROM EMP
NOTE: - We can update the base tables (or) you can insert the records into
base tables using simple views.
Accessing a record using following command
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE
EMPNO=7369;
Updating a base table (view) using following command
UPDATE V_EMP_TEST SET SAL=SAL+100 WHERE EMPNO=7369
Inserting a view a record using following command view to base
table
SELECT * FROM V_EMP_TEST (to check record in base table)
INS INTO V_EMP_TEST (EMPNO, ENAME, SAL, DEPTNO) VALUES
(8243, SIVA, 4500, 30)
SELECT COUNT (*) FROM EMP (after inserting record)
Complex Views (or) Join Views: -
30
A view which is created from more than one table is known as
complex view (or) join view.
A join view is a view contains columns from more than one table.
A join condition is a typical part of a view definition.
A join view consists of columns from more than one table.
Warning!
Updating of base tables may not be done via join views
CREATE VIEW EMP_JOIN_VIEW AS SELECT E.EMPNO, E.ENAME, E.JOB,
E.SAL, E.DEPTNO, D.DNAME, D.LOC FROM EMP E INNER JOIN DEPT D ON
E.DEPTNO=D.DEPTNO
TERADATA
Syntax: -
SELECT * FROM EMP_JOIN_VIEW;
Views to Rename columns: You may create a view that renames columns.
This is often used to shorten long column names, making queries
easier to create.
Syntax: CREATE VIEW EMP_VIEW (ENO, NAME, JOB, SALARY, DNO) AS
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE
DEPTNO=30;
SELECT * FROM EMP_VIEW;
Aggregate Views: Aggregate views are views which contain aggregate expressions (or)
derived columns (scrubbing).
Aggregate and derived columns in the views must be assigned
names (Aliases).
Syntax: CREATE VIEW V_AGGR AS SELECT DEPTNO, SUM (SAL) AS SUMSAL,
AVG (SAL) AS AVGSAL FROM EMP GROUP BY DEPTNO
Join on Views: -
31
Views may participate in join operations just as tables do.
The total number of tables and views which may be used in a single
SQL statement is 64.
NON-ANSI Syntax: SELECT V.DEPTNO, T.DNAME, V.SUMSAL, V.AVGSAL FROM V_AGGR V,
DEPT T WHERE V.DEPTNO=T.DEPTNO
SELECT V.DEPTNO, T.DNAME, V.SUMSAL, V.AVGSAL FROM V_AGGR V
INNER JOIN DEPT T ON V.DEPTNO=T.DEPTNO
View with CHECK Option: -
TERADATA
ANSI Syntax: -
In a view definition, the WITH CHECK Option limits the ability of
users with update and inserts privileges on the view.
With the CHECK Option in effect, the user may not update (or) insert
rows whose value will violate the constraints of the WHERE CLAUSE in the
view definition.
Syntax: CREATE VIEW V_EMP_CHECK AS SELECT EMPNO, ENAME, JOB, SAL,
DEPTNO FROM EMP WHERE SAL>1500 WITH CHECK OPTION
INSERT Into V_EMP_CHECK Values (2000, SIVA, CLERK, 1200, 10)
INSERT Into V_EMP_CHECK Values (2000, SIVA, CLERK, 1500, 10)
INSERT Into V_EMP_CHECK Values (2000, SIVA, CLERK, 1501, 10)
Restrictions on Views: An Index cannot be create on View.
View cannot contain an Order by clause.
Derived an Aggregated columns must be assigned a name using
AS clause.
32
A view cannot be used for update operations if it contains data from
more than one table (Join Views).
A view cannot be used for update operations if it contains derived
columns, a distinct clause and Group by clause.
Advantages of Views: o An additional level of security.
o Controls read and update privileges.
o Are unaffected if a column is added to a table.
o Are unaffected if a column is dropped unless the dropped column is
referenced by the view.
TERADATA
o Simplify end users access data.
Suggestions: o Create Views to insure that all users access to tables is via Views.
o Create Views which do complex joins (or) Aggregations to simplify
end user coding requirements.
o Use access locks when creating views to maximize data availability
to users.
SUB QUERIES: A query which is defined within the main query is known as sub
query.
The sub queries are created to simplify the long list values.
There are two types of sub queries
I.
Traditional sub query
II.
Correlated sub query
Traditional sub queries: Traditional sub queries are read from the bottom up. The lowest
level query is resolved and the results are fed to the next level of query.
A source statement may reference up to 64 tables (or) Views, thus
there may be up to 64 levels of sub query.
Sub query Processing: o Resolve bottom level query first
33
o Use results as input to next level
o Repeat until top level reached
Syntax: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE
DEPTNO IN (SELECT DEPTNO FROM DEPT)
DISTINCT used for deleting duplicates
RESTRICT used for finding duplicates
o List out names of employees drawing highest salary
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERESAL=
(SELECT MAX (SAL) FROM EMP)
TERADATA
Sub queries in Aggregates: -
o List out names of employees drawing salary greater than average
salary of company
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE SAL >
(SELECT AVG (SAL) FROM EMP)
o List out names of employees drawing lowest salary from each
company
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE SAL IN
(SELECT MIN (SAL) FROM EMP GROUP BY DEPTNO)
Sub query with WHERE Clause: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE
DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME = MILLER)
Sub query with Qualifier ANY: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE SAL
> ANY (SELECT SAL FROM EMP WHERE DEPTNO=20)
The teradata server evaluates all the values which are written by
the sub query.
Sub query with Qualifier SOME: It picks up the lowest value from list of values written by the sub
query.
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP WHERE SAL
> SOME (SELECT SAL FROM EMP WHERE DEPTNO=20)
34
Correlated Sub queries: Correlated sub queries are processed differently from traditional sub
queries.
Correlated sub queries expand the functionality of traditional sub
queries.
Correlated sub queries are processed using top down approach.
Each row of the outer query is selected and compared to the criteria
in the sub query.
Usually correlated sub query is sub query whose outer query results
are processed as against sub query result.
The sub query result is analyzed for each row of outer query.
TERADATA
When the row matches with the inner query than outer query
returns the row.
Ex: - Find the employees with the highest salary in each department
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP EE WHERE
SAL = (SELECT MAX (SAL) FROM EMP EM WHERE EE DEPTNO= EM
DEPTNO)
Note: - correlated sub queries uses shared spool technique to which
makes them correlated sub queries are more fast and efficient.
HELP Utilities: Teradata SQL contains extensions beyond ANSI stand SQL.
There are many tools available to provide the Help for a teradata
user (or) programmer.
HELP - Command: The HELP command provides the help for any defined teradata
database object
HELP TABLE
HELP VIEW
HELP MACRO
HELP COLUMN
HELP PROCEDURE etc
SHOW Command: -
35
The SHOW command displays the data definition (DDL) associates
with database objects (Tables, Views, Macros, Triggers, Joins, Indexes, and
Stored Procedures).
COMMAND
RETURNS
SHOW TABLE TABLENAME
CREATE TABLE STATEMENT
SHOW VIEW VIEWNAME
CREATE VIEW STATEMENT
SHOW MACRO MACRONAME
CREATE MACRO STATEMENT
SHOW TRIGGER TIRGGERNAME
CREATE TRIGGER STATEMENT
SHOW JOIN INDEX INDEXNAME
CREATE JOIN INDEX STATEMENT
SHOW PROCEDURE
PROCEDURENAME
CREATE PROCEDURE STATEMENT
TERADATA
SHOW command displays how an object was created
EXPLAIN Command: Use an EXPLAIN command to provide query execution plan which is
prepared by optimizer.
Syntax: -
EXPLAIN SELECT * FROM EMP WHERE COMM IS NOT NULL
Teradata Logical and Conditional Operators: Logical Operators: AND, OR, BETWEEN AND are known as logical operators to defines
the conditions in WHERE Clause.
The multiple conditions can be combined using logical operators.
AND Syntax: SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE SAL > 2000
AND DEPTNO = 30
OR Syntax: SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE SAL > 2000
OR DEPTNO = 30
36
SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE (DEPTNO =
10 OR DEPTNO = 20) AND (SAL>2000)
BETWEEN AND Syntax: It returns the range of values between two given values
SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE SAL BETWEEN
1500 AND 2500
Conditional Operators: -
It verifies the NULLS on the given columns
Syntax: - SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE COMM IS
NULL
TERADATA
ISNULL: -
IS NOT NULL Syntax: SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE COMM IS NOT
NULL
IN Operator: SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE DEPTNO IN
(10, 20)
IN NOT NULL: SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE DEPTNO NOT
IN (20, 30)
LIKE Operator: SELECT EMPNO, SAL, JOB, DEPTNO FROM EMP WHERE ENAME LIKE
S%
Teradata SQL Extensions Date Arithmetics: The current date and time can be displayed with the following
extensions
SELECT DATE
SELECT CURRENT_DATE
SELECT TIME
37
SELECT CURRENT_TIME
SELECT CURRENT_TIMESTAMP (displays both date & time)
Date Arithmetic: Syntax: SELECT EMPNO, ENAME, SAL, JOB, DEPTNO, HIREDATE,
(DATE_HIREDATE)/365 AS EXP FROM EMP
Date Extracts: -
SELECT EXTRACT (YEAR FROM CURRENT_DATE)
SELECT EXTRACT (MONTH FROM CURRENT_DATE)
SELECT EXTRACT (MONTH FROM CURRENT_DATE+30)
TERADATA
Extract is a function
SELECT EXTRACT (DAY FROM CURRENT_DATE)
SELECT EXTRACT (DAY FROM CURRENT_DATE+2)
SELECT EXTRACT (HOUR FROM CURRENT_TIME)
DATE Arithmetic in WHERE Clause: o Displays employees experience greater than 26 years
SELECT EMPNO, ENAME, SAL, JOB, DEPTNO, (DATE_HIREDATE)/365
AS EXPERIENCE FROM EMP WHERE (DATE_HIREDATE)/365 >= 26
String Functions: String Operator: Pipe is a concatenate operator which puts two string together
Syntax: - SELECT ENAME || , || ENAME AS EMPLOYEE FROM EMP WHERE
DEPTNO=20
Sub String Function: It returns the part of the string from character string
Syntax: SELECT EMPNO, ENAME, SAL, DEPTNO FROM EMP WHERE SUBSTR
(ENAME, 1, 1) = S
38
Note: We can use string literals in a sub string function to return the part
of the string.
Syntaxes: SELECT SUBSTR (CATALOG, 5, 3)
SELECT SUBSTRING (CATALOG FROM 5 FOR 3)
Character Function: -
Syntax: SELECT EMPNO, ENAME, SAL, JOB, DEPTNO FROM EMP WHERE CHAR
(ENAME) >= 5
TERADATA
Count the number of characters in a string.
TRIM Function: It removes the spaces (or) blanks from the both sides of the string
Syntax: SELECT EMPNO, ENAME, SAL, JOB, DEPTNO FROM EMP WHERE CHAR
(TRIM (ENAME)) >= 6
Position Function: It locates a character position in a string
Syntax: - SELECT POSITION (M IN SMITH)
MACROS: Macro contains one (or) more prewritten SQL statements
Macros are a Teradata extension to ANSI SQL
Macros are stored in the Teradata data dictionary
Macros can be executed from any SQL front ends
I.
SQL Assistant
II.
BTEQ
39
The following are the Macro related commands
i)
Create Macro: It creates a Macro and stores in the data dictionary
ii)
Execute Macro Name: Executes the SQL statements within the Macro
iii)
Show macro: -
A Macro is created for frequently occurring queries (or) set of
operations.
Macros are similar to Stored Procedures in oracle with no control
flow statements [while loop, for loop etc].
TERADATA
It displays how the Macro was created
All Macro statements are treated as a single transaction.
Re-optimized the execution time.
Accept users provided parameters.
Note: In views the data definition can be update used a command
called REPLACE.
Syntax: REPLACE VIEW EMP_VIEW AS SELECT EMPNO, ENAME, JOB, SAL,
DEPTNO FROM EMP WHERE DEPTNO=10;
Syntax to create a Macro: CREATE MACRO M_EMP20 AS (SELECT EMPNO, ENAME, JOB, SAL,
DEPTNO FROM EMP WHERE DEPTNO=20 ;);
Macro Execution: A Macro is executed using EXECUTE command.
Syntax: - EXECUTE M_EMP
To display how a macro was created we use SHOW command
Syntax: - SHOW Macro M_EMP
Updating Macro definition: -
40
Use the REPLACE command to update an existing macro definition
Syntax: REPLACE MACRO M_EMP AS (SELECT EMPNO, ENAME, JOB, SAL,
COMM, DEPTNO FROM EMP WHERE DEPTNO=10 ;);
Parameterized Macros: Parameterized macro allows substitute variables.
Values for these variables are supplied at runtime.
Each name and it is associated data type are separated by comma
from the next name and it is associated data type.
The order is important.
TERADATA
Macros may have more than one parameter.
Creation of Parameterized Macro (Single Parameter)
i)
Create Macro M_DEPT_LIST (DNO INTEGER) AS (SELECT EMPNO,
ENAME, JOB, SAL, DEPTNO FROM EMP WHERE DEPTNO=: DNO ;);
Execution of Macro: - EXECUTE M_DEPT_LIST (20);
ii)
Create Macro Sales.get_Customers (V-Nation varchar (30)) AS
(SELECT C_CUSTKEY, C_NAME FROM Sales. CLIENT a, Sales. AREA
b WHERE a.C_NATION KEY = b.N_NATION KEY and b.N_NAME
=:V_NATION ;);
EXEC Sales.get_Customers (INDIA)
Here Sales is our database where Macro gets stored.
iii)
Create Parameterized Macro to insert a record
CREATE MACRO NEW_EMP ( ENO INTEGER,
NAME VARCHAR (10),
SAL DECIMAL (7, 2),
DNO INTEGER)
AS (INSERT INTO EMP (EMPNO, ENAME, SAL, DEPTNO)
VALUES
(:ENO, :NAME, :SAL, :DEPTNO););
41
EXEC NEW_EMP (7666, SIVA, 4500, 10);
EXEC NEW_EMP (7666, , 4500, 10);
EXEC NEW_EMP (ENO=7666, NAME= SIVA, SAL= 4500, DNO= 10);
SELECT EMPNO, ENAME, SAL, DEPTO FROM EMP;
CREATE MACRO NEW SALE (BOOKNUM SMALL INT, SDATE DATE, QTY
SMALLINT)AS (SELECT :BOOKNUM (TITLE BOOK), :SDATE (TITLE DATE,
FORMAT MMMBDDBYYYY), QTY (TITLE NBR SOLD) FROM
BOOK_SALES_HISTROY;
INSERT INTO BOOK_SALES_HISTORY (BOOK_NUMBER, SALES_DATE,
SALES_QUANTITY) VALUES (: BOOKNUM, :SDATE, :QTY););
EXEC NEWSALE (123, 20000130, 6);
TERADATA
Sample Macro: -
EXEC NEWSALE (QTY=6, BOOKNUM=123, SDATE=20000130);
Note: You can create a Macro to preserve Integrity
Table level constraints are defined at last column in the table.
Column level constraint are defined at specifically create to a
column.
To check multiple columns we use table level constraints.
Create a Macro to Preserve Integrity: CREATE TABLE EMP TEST (EMP_ID INTEGER NOT NULL, NAME
VARCHAR (10), SAL DECIMAL (7, 2), DNO INTEGER, CONSTRAINT CHECK
(SAL>1000 AND DNO BETWEEN 10 AND 40) ;);
SHOW TABLE EMP TEST;
CREATE A MACRO NEW_EMPLYEE (ENO INTEGER, NAME VARCHAR
(10), SAL DECIMAL (7, 2), DNO INTEGER)
AS (ROLLBACK WORK INVALID SALARY WHERE :SAL<=1000;
ROLLBACK WORK INVALID DEPARTMENT NUMBER WHERE
:DNO>40;
INSERT INTO EMP TEST (EMP_ID, NAME, SAL, DNO) VALUES (:ENO,
:NAME, :SAL, :DNO) ;);
42
EXEC NEW_EMPLOYEE (7888, SIVA, 8000, 40)
BTEQ (Basic Teradata Query)
The BTEQ is a interactive and batch SQL utility.
It can be used for Import and Export of data.
It can be used as report writer.
Useful for error handling.
SQL statements in the BTEQ scripts do not have begin period and
have trialing semi colon.
Interactive Utility: -
TERADATA
All BTEQ commands begin with the period (.).
Procedure1: Access the command prompt
Type BTEQ and press ENTER to invoke interactive environment
.LOGON 127.0.0.1/TDUSER (press ENTER)
** Logon successfully completed
Enter the following SQL request
SELECT EMPNO, ENAME, JOB, SAL, COMM, DEPTNO FROM Sales.EMP
WHERE COMM IS NOT NULL (press ENTER)
To EXIT from Interactive environment
Type .QUIT (press ENTER)
Procedure2: To access BTEQ
Start Programs Teradata Client Teradata BTEQ
Select ASCII character set
Click on OK
Batch Utility: -
43
It can execute multiple SQL statements with the BTEQ scripts.
Note: - We can use SQL as a single statement request as well as multi
statement request
EX: - BTEQ.txt
.LOGON 127.0.0.1/TDUSER, TDUSER;
Database Sales;
Create table BTEQ_CUST, NO FALLBACK, NO BEFORE JOURNAL,
COMPANY NAME VARCHAR (40) NOT NULL)
UNIQUE PRIMARY INDEX (CUSTOMERID);
INSERT BTEQ_CUST VALUES (C0001, HSBC);
TERADATA
NO AFTER JOURNAL (CUSTOMERID CHAR (5) NOT NULL,
INSERT BTEQ_CUST VALUES (C0002, ATT);
:QUIT;
Running the BTEQ Script: Open the command prompt
Type the following syntax to run the script
BTEQ <C:\BTEQ\First.txt
To define the error handling type the following syntax
BTEQ <C;\BTEQ\First.txt> C:\BTEQ\Error.txt
Exporting data: Use .EXPORT command to export the data from Teradata database
to host file.
BTEQ Report Writer: Send query result to the file in the field format.
.LOGON 127.0.0.1/TDUSER, TDUSER;
Database Sales;
.Export Report File C:\BTEQ\EMP.txt
Select EMPNO, ENAME, JOB, SAL, DEPTNO from EMP
44
.EXPORT RESET;
.QUIT;
Exporting Data Only: .LOGON 127.0.0.1/TDUSER, TDUSER;
Database Sales
.Export Data File C:\BTEQ\EMP1.txt
.EXPORT RESET
.QUIT
Get Data in Excel Format: -
TERADATA
Select EMPNO, ENAME, JOB, SAL, DEPTNO from EMP
.LOGON 127.0.0.1/TDUSER, TDUSER;
Database Sales
.Export Data File C:\BTEQ\EMPTEST.txt
Select EMPNO, ENAME, JOB, SAL, DEPTNO from EMP
.EXPORT RESET
.QUIT
BTEQ COMMANDS: Limit: Sets limit on number of rows capture
.Export DIF
Output converted to data interchange format such as Excel
.EXPORT RESET
Reverses the effect of previous export and closes the output file.
Importing the Data: It imports the data from Teradata host to database (loading).
.Reporting Star (*): -
45
Causes BTEQ to read records from the file until end of fill (eof).
Procedure: Define the Input data file
.LOGON 127.0.0.1/TDUSER, TDUSER;
Database Sales
.Import Variant , file = C:\BTEQ\EMP.txt
Using
EMP_ID (Integer)
EMP_NAME (Varchar (10))
TERADATA
.Report *
EMP_JOB (Varchar (10))
EMP_SAL (Decimal (7, 2)
EMP_DEPTNO (Integer)
INSERT INTO EMP (EMPNO, ENAME, JOB, SAL, DEPTNO) VALUES
(:EMP_ID, :EMP_NAME, :EMP_JOB, :EMP_SAL, :EMP_DEPTNO)
.QUIT
BTEQ Teradata Extensions: WITH, WITHBY are Teradata extensions to ANSI standard SQL.
WITH: - It is used to produce grand totals for all the records.
WITHBY: - It is used to generate sub totals for each group.
WITH, WITHBY clauses are supported only in BTEQ does not
supported by SQL assistance.
WITHBY Syntax: - SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM
SALES.EMP WITH SUM (SAL) BY DEPTNO
Output: -
46
TERADATA
Syntax for GRAND TOTAL: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM SALES.EMP WITH
SUM (SAL) (TITLE GRAND TOTAL) ORDER BY EMPNO
Output: -
WITH, WITHBY and ORDER BY: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM SALES.EMP WITH
SUM (SAL) BY DEPTNO WITH SUM (SAL) (TITLE GRAND TOTAL) ORDER BY
DEPTNO
INTERMEDIATE TABLES (INTERIM TABLES): Interim tables are permanent tables that are used to produce
aggregates if those aggregates are used to further computations. Such as
showing the difference between each Salary and Average Salary.
Interim tables must be created Populated, Queried and Detected by
the user when no longer needed.
47
They are considered as permanent tables in that we required
permanent space just like as any permanent table. Once they have served
their purpose for interim computations they are discarded unless they
may service another query requests.
A considerable amount of a system overhead is involved in the
creation and dropping of interim table.
When the table is dropped all such entries in the data dictionary
must be cleared and permanent space returns to the available unused
location.
Syntax: -
TERADATA
Once the definition of tables has been written to the data dictionary
access rights for the table must also be written to the approximate
dictionary tables.
Find the Employee with Salary greater than company Average
Create table MY_TEMP (AVG SAL DECIMAL (8, 2))
INSERT INTO MY_TEMP SELECT AVG (SAL) FROM EMP
SELECT E.EMPNO, E.ENAME, E.JOB, E.SAL, E.DEPTNO, T.AVGSAL
FROM EMP E, MY_TEMP T WHERE E.SAL>T.AVGSAL
DROP TABLE MY_TEMP
DERIVED TABLES: A Derived table is a named temporary table obtained from sub
query.
A Derived table allows you to directly access the derived results in
other parts of overall SQL statements.
Derived tables allow the user benefits of Interim tables but within
the syntax of single SQL statements.
Conceptually the approach is same of for four separate SQL
statements it may be achieved in single SQL statements.
Derived tables are temporary tables that are created and dropped in
part of query.
They permits the use of aggregates in WHERE clause
48
SELECT EMPNO, ENAME, SAL, AVGSAL FROM (SELECT AVG (SAL)
FROM EMP) MY_TEMP (AVGSAL), EMP WHERE SAL>AVGSAL ORDER BY 3
OUTPUT ATTRIBUTES: Output attributes defines and alias name for columns. The following
are known as Output attributes AS, TITLE and NAMED.
AS: -
NAMED: SELECT EMPNO, ENAME, JOB, SAL, SAL*0.15 (NAMED TAX), DEPTNO
FROM EMP ORDER BY TAX
TERADATA
SELECT EMPNO, ENAME, JOB, SAL, SAL*0.15 AS TAX, DEPTNO FROM
EMP ORDER BY TAX
AS is an ANSI standard Output attribute
The following are the Teradata SQL extensions to define Output
attributes
i)
Title
ii)
Named
SELECT EMPNO, ENAME, JOB, SAL, SAL*0.15 AS TAX, DEPTNO FROM
EMP ORDER BY 5
SELECT EMPNO, ENAME, JOB, SAL, SAL*0.15 (TITLE TAX), DEPTNO
FROM EMP ORDER BY 5
OLAP FUNCTIONS: RANK: a) Simple Ranking: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO, RANK () OVER
(ORDER BY SAL DESC) AS RANKING FROM EMP QUALIFY RANKING
<=3
b) Dense Ranking: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO, RANK () OVER
(PARTITION BY DEPTNO ORDER BY SAL DESC) AS RANKING FROM
EMP QUALIFY RANKING <=3
SAMPLE: -
49
The Sample is an OLAP function which arbitrarily reads the sample
data in a two different modes
i)
Record mode
ii)
Percent mode
Syntax: SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP SAMPLE 5
SELECT EMPNO, ENAME, JOB, SAL, DEPTNO FROM EMP SAMPLE 0.25
SELECT EMPNO, ENAME, JOB, SAL, SUM (SAL) OVER (ORDER BY
EMPNO ROWS UNBOUNDED PRECEDING) AS CSUM FROM EMP
SQL FUNCTIONS: -
TERADATA
RUNNING TOTALS: -
NULLIF Statement: The NULLIF statement an ANSI standard function which provides a
method for substituting nulls for a prescribed value.
Because NULLIF uses CASE logic it is generally advisable to rename
the column (s) being output so as to avoid the default title applied by the
CASE statement.
NULLIF for Division: If a division expression involving a denominator equal to zero and
divide by zero error will be returned.
If a denominator equals the NULL values then the result is also a
NULL and no error is generated.
The NULLIF expression can be used to connect possible zero value
denominator to a NULL value, thus preventing the divide by zero error.
NULLIF Syntax: SELECT EMPNO, ENAME, JOB, SAL, NULLIF (COMM, 0) AS COMM,
DEPTNO FROM EMP
ZERO NULLIF: -
50
SELECT EMPNO, ENAME, JOB, SAL, NULLIF ZERO (COMM) AS COMM,
DEPTNO FROM EMP
ZERO IFNULL: It is an ANSI standard SQL function which substitutes zero in place of
NULL values.
The above same operation can also be achieved using COLAESCE
function.
SELECT EMPNO, ENAME, JOB, SAL, COMM, SAL+ZERO IFNULL
(COMM) AS TOTALSAL FROM EMP
SELECT EMPNO, ENAME, JOB, SAL, COMM, SAL+COLAESCE (COMM,
0) AS TOTALSAL FROM EMP
TERADATA
Syntaxes: -
Length function: It counts the number of characters in a string.
Syntax: SELECT EMPNO, ENAME, LENGTH (ENAME) AS CNT FROM EMP
VOLATILE TEMPORARY TABLE: Volatile table have much in common with derived tables.
There are materialized in spool space are unknown to the data
dictionary.
Unlike derived tables, volatile tables may be used repeatedly
throughout the session.
They may be dropped at any time manually (or) automatically at the
session end.
Volatile tables required their own CREATE syntax.
The table definition is kept in cache (spool space) and not
permanently written to disk.
Volatile tables do not survive a system restart.
Syntax: Create Volatile Table VT_DEPTSAL (
51
DEPTNO INTEGER
SUMSAL DECIMAL (7, 2)
AVGSAL DECIMAL (7, 2)
MINSAL DECIMAL (7, 2)) ON COMMIT PRESERVE ROWS
INSERT INTO VT_DEPTSAL SELECT DEPTNO, SUM (SAL), AVG (SAL),
MIN (SAL) FROM EMP GROUP BY 1
SELECT T.EMPNO, T.ENAME, T.SAL, T.DEPTNO FROM EMP T INNER
JOIN VT_DEPTSAL V ON T.DEPTNO=V.DEPTNO WHERE SAL=MIN (SAL)
ORDER BY 3
TERADATA
Show all employees who makes MIN. salary in each department
Show all employees who makes MAX. salary in each department
SELECT T.EMPNO, T.ENAME, T.SAL, T.DEPTNO FROM EMP T INNER
JOIN VT_DEPTSAL V ON T.DEPTNO=V.DEPTNO WHERE SAL=MAX (SAL)
ORDER BY 3
GLOBAL TEMPORARY TABLES: Global temporary tables unlike volatile and derived tables have a
definition stored in the data dictionary.
The table itself is materialized by the first SQL DML statement which
accesses the table.
Typically an INSERT SELECT (or) an INSERT.
Like volatile, global temporary tables are local to a session.
The table instance may be dropped explicitly (or) it will be
automatically dropped at the end of the session.
A special type of space called temporary space is used for global
temporary tables.
Like perm space, temporary space is sustained during a system
restart.
Global temporary tables are thus able to survive a system restart.
Creating Temporary Tables: -
52
Temporary tables are created using the CREATE GLOBAL
TEMPORARY TABLE command.
This stores the base definition of the table in the data dictionary.
**Temporary tables may be altered by the ALTER command to
change any attributes of the tables, similar to perm table.
Syntax: Create Global Temporary Table GT_DEPTSAL
SUMSAL DECIMAL (7, 2),
AVGSAL DECIMAL (7, 2),
TERADATA
DEPTNO INTEGER,
MINSAL DECIMAL (7, 2),
MAXSAL DECIMAL (7, 2);
Alter table GT_DEPTSAL ON Commit preserve rows
INSERT INTO GT_DEPTSAL SELECT (DEPTNO, SUM (SAL), AVG (SAL),
MIN (SAL), MAX (SAL) FROM EMP GROUP BY 1
SELECT T.EMPNO, T.ENAME, T.SAL, T.DEPTNO FROM EMP T INNET
JOIN GT_DEPTSAL G ON T.DEPTNO=G.DEPTNO WHERE SAL=MINSAL
ORDER BY 3
CASE EXPRESSIONS: CASE Statement: The CASE statement is used to return alternate values based on
search conditions.
There are two forms of the CASE statements,
-
Valued
Searched
A CASE statement is an ANSI standard features which allows for
conditional processing of rows returned by a query.
The ability to perform procedural logic significantly enhances the
power of SQL.
53
The CASE statement will in many cases reduce the need for Interim
(or) Temporary tables and also the need to write special logic within client
applications (or) stored procedures.
First match return a result for that row.
Valued CASE Expressions: -
Specify a single expression to test for equality
List the possible values for the test expression
List the results returned by each possible value
Syntax: -
TERADATA
In a Valued CASE statement you must
Calculate the fraction of total salary of all employees represented by
salaries of Deptno 10
SELECT SUM (CASE DEPTNO
WHEN 10 THEN SAL
ELSE 0
END)/SUM (SAL) FROM EMP
Get total salary for Deptno 10, 20
SELECT SUM (CASE DEPTNO
WHEN 10 THEN SAL
WHEN 20 THEN SAL
ELSE 0
END) AS TOTAL_10_20 FROM EMP
Searched CASE Statement: The searched CASE statement is an alternative format which allows
greater flexibility in CASE statement.
Instead of testing on a single column (or) expression, this format
allows for testing on multiple conditions each of which can involve
54
different Columns (or) Expressions. If may not also test for conditions
other than equality.
In a searched CASE statement you do not specify expression to test.
You may specify multiple, arbitrary, search conditions that can returns
different results.
Syntax: Categorize each Employee by SAL range
CASE
WHEN SAL<1000
TERADATA
SELECT ENAME,
THEN UNDER 1K
WHEN SAL<2000
THEN UNDER 2K
WHEN SAL<3000
THEN UNDER 3K
ELSE >=4K
END
FROM EMP ORDER BY SAL
55