You are on page 1of 29

Basic SQL & SQL Assistant Features

After completing this module, you should be able to:

Logon and use SQL Assistant to submit SQL to Teradata.


Use the Explorer Tree GUI to drill-down into database object
information.
Apply basic settings to SQL Assistant for tailoring its usage.
Compare and contrast certain SQL Assistant features with SQL
alternatives like HELP; SHOW; Session/System Variables.
Identify databases, tables, and views referenced in the labs of this
course.
Review of Relational Concepts

TABLE Rows and columns

ROW A record

COLUMN A field

DATA VALUE Specific column for a specific row

DOMAIN The real world values on which data values in a


column or columns must be based

PRIMARY KEY Column(s) that uniquely identify a row

FOREIGN KEY Column(s) that serve as a primary key elsewhere


in the system.
A Table with Rows and Columns

EMPLOYEE
MGR
EMP EMP DEPT JOB LAST FIRST HIRE BIRTH SAL
NUM NUM NUM CODE NAME NAME DATE DATE AMT
PK FK FK FK
1006 1019 301 312101 Stein John 761015 531015 2945000
1008 1019 301 312102 Kanieski Carol 770201 580517 2925000
1005 0801 403 431100 Ryan Loretta 761015 550910 3120000
1004 1003 401 412101 Johnson Darlene 761015 460423 3630000
1007 1005 403 432101 Villegas Arnando 770102 370131 4970000
1003 0801 401 411100 Trader James 760731 470619 3785000

EMP NUM is named employee_number


MGR EMP NUM is named manager_employee_number
DEPT NUM is named department_number
JOB CODE is named job_code
LAST NAME is named last_name
FIRST NAME is named first_name
HIRE DATE is named hire_date
BIRTH DATE is named birthdate
SAL AMT is named salary_amount
Teradata Object Naming Conventions

Teradata naming rules may be used as follows on all Teradata database objects:
Names are composed of the following characters:
A - Z (Upper or lowercase)
0-9
#, $, _
Names are limited to 30 characters.
Names may not begin with a numeric value.

Examples of named objects:

Account_Table
Financials_2001_DB
Sales_$_Column
#_of_Years_Column
SQL Assistant

SQL Assistant is a Windows-based front-end tool for submitting SQL to Teradata.

SQL Assistant has the following properties:


Is Windows-based.

Is ODBC-compliant.
(Can be used to access any ODBC-compliant Database.)
Saves information about previous query result sets.
Permits retrieval of previously used queries.
Supports DDL, DML and DCL commands.
Supports ANSI or BTET transaction protocols.
May be used to import or export data.
(Not covered by this course.)
Allows many options for tuning how it works.
Logging On Through SQL Assistant

The sequence for logging on to a Teradata system varies depending on how SQL
Assistant is set up. In our example, a user name appears, requiring only a
password.
Connect
Plug

To connect (logon) to Teradata using SQL


Assistant:

Click on the Connect plug


Click on File or Machine Data Source.
Double-click the appropriate system.
Fill in the logon information.
Click OK
Windows of SQL Assistant
Connect
Disconnect Clear Explorer Submit
Plugs Query Tree Query

SQL Assistant is a
Windows based GUI to
Teradatas data dictionary.

You can navigate through it


using standard windows Query Window
features like:

- Right-clicking on areas
- Double-clicking on fields
- Clicking on drop-downs Explorer
- Click and drag from Tree
explorer tree to query Window Results Window
window.

Our discussion of this tool


will be introductory.

Feel free to experiment with History Window


using it during labs!
Listing Database Objects

SQL HELP commands may be used to retrieve information on objects.


Here we are contrasting SQL Command vs. Explorer Tree information for objects in
a database.

To submit SQL, click


on this or press F5.

Explorer Tree
Information on
Employee_Sales.

Contrast SQL Request

The result of either


of the 2 following
commands.

HELP DATABASE Employee_Sales;


HELP USER Employee_Sales;
HELP TABLE Command

The HELP TABLE command returns information about a database or user object.
Qualifications for SQL are databasename.tablename.columnname

Explorer Tree
information on table
Employee.
SQL Request

Cursor on column
Last_Name shows
hidden information.

Result of HELP
TABLE command on
table Employee.
Other SQL HELP Commands

Some other SQL HELP commands.

Discuss the various Databases and Users:


levels of qualification HELP DATABASE Employee_Sales;
shown on the right. HELP USER Dave_Jones;

Database Objects:
HELP TABLE Employee;
HELP VIEW Emp_Views.emp;
For a complete list of
HELP MACRO payroll_3;
HELP objects, issue the
HELP COLUMN Employee_Sales.employee.last_name;
following:
HELP INDEX employee;
HELP STATISTICS employee;
HELP 'SQL HELP';
HELP CONSTRAINT employee.over_21;
HELP VOLATILE TABLE vol_tab1;
We will take a closer look
HELP JOIN INDEX Employee_Sales.Employee;
at this special syntax in a
HELP HASH INDEX Employee_Sales.Department;
later module.
HELP TRIGGER trigger1;
HELP PROCEDURE proc1;
HELP FUNCTION func1;

The 3 different levels for referencing objects are: Database, Table, and Column.
Setting a Default Database via SQL

The default database assignment remains for a session until it is changed.


The original logon default is reacquired again at next logon.
Mouse-over the top of the Explorer Tree, as shown below, to display defaults shown.

Logged on as user DLM.

Default database
is DLM.

Changes the After issuing the database default request shown, which
default database to database will be checked for the following?
Employee_Sales. HELP TABLE Employee;

Clicking on SQL
Statement in History
will recall it to the Query
Window later.
Setting a Default Database via SQL
Assistant
A default database can be assigned through SQL Assistant like this.

You can override the default


database of your logon from
within SQL Assistant by setting it
here.

The pathway to this screen is:

TOOLS
DEFINE ODBC DATA SOURCE
SYSTEM DSN
CONFIGURE

This may also be typed


here at each logon.
The Teradata SHOW Command

The SHOW command may be used to provide the definition of an object.


In this example, we are getting the definition for a table in database
Employee_Sales.

Contrast the SQL


SHOW TABLE
(on the right) with
the Explorer Tree Result of submitting
technique below. the SHOW TABLE.

Right-Click
Show Definition

Both will display the


table definition into the
Answerset Window
Other SQL SHOW Commands

Some other SQL SHOW commands.


Discuss the various Database Objects:
levels of qualification SHOW TABLE Employee;
shown on the right. SHOW VIEW Emp_Views.emp;
SHOW MACRO payroll_3;
SHOW TRIGGER Employee_Sales.trig1;
SHOW PROCEDURE proc1;
SHOW FUNCTION func1;

For a complete list of objects referenced by the SHOW command you can issue the
following:

HELP 'SQL SHOW';

We will take a closer look at this special syntax in a later module.


Session Information via SELECT

One method for retrieving information about your current session is to reference the
session variables below. (These are sometimes referred to as Built-in Functions)

The SELECT is the main focus of the next module.

Logon User Session Number Database Default Logon Account String


Session Information via HELP SESSION

Another method for listing session information is to issue the request shown below.
Note that the scroll bar indicates more information is available for viewing.
Tools Options Query Tab

TOOLS
OPTIONS
QUERY

Check this to allow for


submitting queries you
wish by highlighting them.

(For complete information, refer to: Teradata SQL Assistant for Microsoft Windows - User Guide)
Tools Options Code Editor Tab

TOOLS
OPTIONS
CODE EDITOR

The Code Editor allows you to


customize how you Query
Window displays your SQL.

Among other things, you can:


Convert keywords to uppercase
as you type.
Display line numbers as shown.
Use Left Margin to change the
background for number lines.
Use Background to change the
background for you SQL Code.
Tools Options Answerset Tab

TOOLS
OPTIONS
ANSWERSET

Check this and choose an


alternating line color for your
result.

For a large answer set, this


prompts you to continue after
this many rows are returned.
Tools Options History Tab

TOOLS
OPTIONS
HISTORY

These are the default settings.


Shortcuts to Typing Object Names

The following key-stroke combinations may be used in conjunction with the


Explorer Tree as shortcuts to typing.

01 Click-Drag

02 Click-Ctrl-Drag

03 Ctrl-Ctrl-Ctrl-Drag

04 Click-Shift-Click-Drag
Commenting Lines of SQL

ANSI Standard method


for commenting lines
one at a time.

Teradata method for


commenting blocks of
lines.

Only the highlighted SQL gets


submitted.
(The HELP DATABASE)

Note how SQL


Assistant italicizes the
lines and changes the
color.
Logging on to Multiple Systems

Note the 2 explorer tree


references.

One for each system. Note


The Employee_Sales Database

Database containing
tables used in the labs.

Note that columns include:


- Names
- Data Types
- NULL/NOT NULL Attributes

Note that index information


is available as well.

The explorer tree diagram


on the right lists all of the
tables used in labs for this
course.

Two tables have their


columns listed. These two
tables are very commonly
used.
The Emp_Views Database

Database containing
views used in the labs.

Note that columns include:


- Names
- Data Types
- NULL/NOT NULL Attributes

Note that index information


is not available.

The explorer tree diagram


on the right lists all of the
views used in labs for this
course.

For the labs, always check


your default database!
Module 1: Summary

SQL Assistant is an ODBC-Based tool used to access any ODBC


Compliant database.
SQL Assistant provides GUI access to the Teradata Dictionary.
SQL commands like SHOW and HELP can also be used to provide
dictionary information.
History can be used to recall a previously issued request to the Query
Window.
One must always be mindful of their current database default setting.
A database default is set via a SQL request.
There are 2 databases used for labs: Employee_Sales (tables) and
Emp_Views (views).
Module 1: Review Questions

True or False:

1. A user can have only one default database set at a time.


True
2. The following is a valid SQL request HELP DATABASE;
False a database name is required with this syntax
3. The HELP TABLE command returns table index information.
False
4. You can click and drag information from the Explorer Tree to the Query
Window.
True
5. The following is a valid column name _abc_
True
6. For the following request, the database will assume that the object
Employee is a database name HELP COLUMN Employee.Last_Name;
False it assumes it to be a table name
7. The following is a valid SQL request SHOW COLUMN
Employee.Last_Name;
False you can not perform a SHOW on a column
Module 1: Lab Exercises

1. Logon to SQL Assistant and review the option in the TOOLS pull down
menu.

Note the Function Key assignments for Execute and EXPLAIN.


Click on the List Tables option Tables OK

To which SQL command is this result set similar?

Next, see if you can successfully perform the List Columns option.
Choose a database and a table name based on what you have learned about
the lab environment.

2. Review the options in the View pull down menu.

Note the options for Explorer Tree and History. Click on each of these
and note the indentations to show whether they are turned on or not.
Module 1: Lab Exercises (continued)

3. Try each of the following in the order shown, and note if it fails by looking
at the bottom-left portion of the utility screen. For those that fail, double-
click the Notes field for the failed request in the History Window.

HELP DATABASE;
HELP DATABASE yourusername;
DATABASE yourusername;
SHOW TABLE Employee;
DATABASE Employee_Sales;
SHOW TABLE Employee;

SHOW TABLE Emp_VIEWS.Employee;


SHOW VIEW Emp_VIEWS.Employee;

4. Practice dragging and dropping columns from the Explorer Tree window
onto the Query Window.

You might also like