Sei sulla pagina 1di 5

Teradata Extensions to

SQL
Structured Query Language (SQL)
Structured Query Language (SQL) is the language used to access the
database. It is sometimes referred to as a Fourth Generation Language
(4GL) to differentiate it from Third Generation Languages such as
FORTRAN and COBOL, though it is quite different from other 4GLs. It
acts as an intermediary between the user and the database. SQL defines
the answer set that is returned from the database.

SQL is different from other computer languages. Its statements resemble


English-like structures. It provides powerful, set-oriented database
manipulation including structural modification, data retrieval,
modification, and security functions.

SQL is a non-procedural language. Because of its set orientation, it


does not require IF, GOTO, OPEN, DO, FOR NEXT, or PERFORM
statements.

We will describe three important subsets of SQL: the Data Definition


Language, the Data Manipulation Language, and the Data Control
Language.

Data Definition Language (DDL)


DDL allows a user to define the database objects and the relationships
that exist among them. Examples of DDL uses are creating or modifying
tables and views.

Data Manipulation Language (DML)


DML consists of the statements that manipulate, change, or retrieve
the data rows of the database. If the DDL defines the database, the DML
lets the user change the information contained in the database. The DML
is the most commonly used subset of SQL. It is used to select, update,
delete, and insert rows.

Data Control Language (DCL)


Data Control Language is used to restrict or permit a user's access. It
can selectively limit a user's ability to retrieve, add, or modify data. It is
used to grant and revoke access privileges on tables and views.

User Assistance
User Assistance commands allow you to list the objects in a database or
the characteristics of a table, see how a query will execute, or show the
details of your system. They vary widely from vendor to vendor.
Structured Query Language (SQL)

The language used to access the database


 fourth-generation language
 non-procedural (no IF, GO TO, OPEN, PERFORM)
 set-oriented

SQL consists of:


Data Definition Language (DDL)
– Defines database objects (tables, users, views, macros)

 CREATE
 DROP
 ALTER

Data Manipulation Language (DML)


– Manipulates rows and data values
 INSERT
 SELECT
 UPDATE
 DELETE

Data Control Language (DCL)


– Grants and revokes access rights
 GRANT
 REVOKE

User Assistance
– Teradata SQL also includes Teradata Extensions to SQL
 HELP
 SHOW
 EXPLAIN
Getting Information about Objects
To perform a query, you need to know something about the table
structure. Two SQL commands that can help are:

HELP TABLE—displays information about database objects


(users/databases, tables, views, and macros) and session
Characteristics used to create a table.

SHOW TABLE—displays the data definition language (DDL)


associated with database objects (tables, views, or macros) used to
create a table.
Data types are described in detail later in the course.
You may need to know what objects exist in a specific database or user.
Use this command:

HELP DATABASE - to get a list of all objects contained in that


database, i.e., HELP DATABASE DBC; gives you a list of all tables,
views, macros, and triggers stored in database DBC.

HELP SESSION - provides information about the current User's


session.

Getting Information about Objects


HELP DATABASE DBC;
Column Name Type Comment
Access Log V The DBC.AccessLog view supplies
Diskspace V The DBC.DiskSpace view provides
Events V The DBC.Events view provides inf
Logon Rule M The LogonRule macro exists to all
Session Info V The DBC.SessionInfo view provide
Tables V Each row of the DBC.Tables view
User Rights V The DBC.UserRights view gives th
Users V Each row of the DBC.Users view

SHOW TABLE employee;

CREATE SET TABLE CUSTOMER_SERVICE.employee


, FALLBACK,
NO BEFORE JOURNAL,
NO AFTER JOURNAL
(
employee_number INTEGER,
manager_employee_number INTEGER,
department_number INTEGER,
job_code INTEGER,
last_name CHAR(20) NOT CASESPECIFIC NOT NULL,
first_name VARCHAR(30) NOT CASESPECIFIC NOT NULL,
hire_date DATE NOT NULL,
birthdate DATE NOT NULL,
salary_amount DECIMAL(10,2) NOT NULL)
UNIQUE PRIMARY INDEX ( employee_number );

The EXPLAIN Facility


The EXPLAIN facility allows you to preview how Teradata will execute a
requested query. It returns a summary of the steps the Teradata RDBMS
would perform to execute the request. EXPLAIN also discloses the
strategy and access method to be used, how many rows will be involved,
and its cost in minutes and seconds. Use EXPLAIN to evaluate a query
performance and to develop an alternative processing strategy that may
be more efficient. EXPLAIN works on any SQL request. The request is
fully parsed and optimized, but not run. The complete plan is returned
to the user in readable English statements.

EXPLAIN provides information about locking, sorting, row selection


criteria, join strategy and conditions, access method, and parallel step
processing.

EXPLAIN is useful for performance tuning, debugging, pre-validation of


requests, and for technical training.

Note: With V2R4, the size of the EXPLAIN text is unlimited.

The following is an example of an EXPLAIN on a very simple query:


EXPLAIN SELECT last_name, department_number FROM employee;
Explanation (full)
---------------------------------------------------------------------------
1) First, we lock a distinct CUSTOMER_SERVICE."pseudo table" for
read on a RowHash to prevent global deadlock for
CUSTOMER_SERVICE.employee.
2) Next, we lock CUSTOMER_SERVICE.employee for read.
3) We do an all-AMPs RETRIEVE step from
CUSTOMER_SERVICE.employee by way of an all-rows scan
with no residual conditions into Spool 1, which is built locally on
the AMPs. The size of Spool 1 is estimated to be 24 rows. The
estimated time for this step is 0.15 seconds.
4) Finally, we send out an END TRANSACTION step to all AMPs
involved in processing the request.
-> The contents of Spool 1 are sent back to the user as the result of
statement 1. The total estimated time is 0 hours and 0.15 seconds.

Potrebbero piacerti anche