Sunteți pe pagina 1din 21

Module 1

Introduction
After completing this module, you will be able to: Define the role of SQL in accessing a Relational Database. Retrieve data from a relational table using the basic SELECT statement.

SQL: Structured Query Language

A complete data access and maintenance language Designed for Relational Database Management System (RDBMS) An industry standard for relational databases A non-procedural language Three levels of ANSI compliant SQL:
Entry
Intermediate Full

ANSI vs. Teradata SQL

Teradata SQL is compliant with entry-level ANSI SQL. Teradata extensions to ANSI compliant SQL add value. Some capabilities are available using either ANSI compliant syntax or Teradata syntax. Most intermediate and full ANSI standards are not implemented in Teradata V2R2.0.
ANSI SQL Teradata SQL SQL AS
intermed. full entry

Rename a column

NAMED CM
Create a macro

SQL is a Set-Level Language

Select the set of all employees in the employee table.

Select the set of all employees in the employee table who are in department 401.

Select the set of all employees in the employee table who earn more than $35,000.

Select the set of all employees in the employee table who are in department 401and who earn more than $35,000.

SQL statements define the answer set to be returned.

SQL Commands

Data Definition Language (DDL)


Actions CREATE DROP ALTER Objects Tables Views Macros

Data Manipulation Language (DML)


Actions SELECT INSERT UPDATE DELETE Objects Rows

Data Control Language (DCL)


Action GRANT REVOKE GIVE Privileges UPDATE Privileges DELETE Privileges SELECT Privileges Objects Tables Views Macros

Table A Table with A Rows and Columns With Rows and Columns
EMPLOYEE ( PARTIAL ) LAST NAME FIRST NAME HIRE DATE BIRTH SALARY DATE AMOUNT

MANAGER EMPLOYEE EMPLOYEE DEPARTMENT JOB CODE NUMBER NUMBER NUMBER PK 1006 1008 1005 1004 1007 1003 FK 1019 1019 0801 1003 1005 0801 FK 301 301 403 401 403 401 FK 312101 312102 431100 412101 432101 411100

Stein Kanieski Ryan Johnson Villegas Trader

John Carol Loretta Darlene Arnando James

761015 770201 761015 761015 770102 760731

531015 580517 550910 460423 370131 470619

2945000 2925000 3120000 3630000 4970000 3785000

Column names have been folded and capitalized for visibility. EMPLOYEE NUMBER HIRE DATE Exception: BIRTH DATE is birthdate is is employee_number. hire_date

NOTE: Syntax and result examples shown throughout this text relate to the (partial) sample table shown with them. On pages where no sample table is shown, results relate to the entire table as shown in Appendix B.

Table Relationships
What is the department name for employee 1004? Who is the manager of that employee? What happens if manager 1003 quits?
EMPLOYEE MANAGER EMPLOYEE EMPLOYEE DEPARTMENT JOB CODE NUMBER NUMBER NUMBER PK 1006 1008 1005 1004 1007 1003 FK 1019 1019 0801 1003 1005 0801 FK 301 301 403 401 403 401 FK 312101 312102 431100 412101 432101 411100 Stein Kanieski Ryan Johnson Villegas Trader John Carol Loretta Darlene Arnando James 761015 770201 761015 761015 770102 760731 531015 580517 550910 460423 370131 470619 2945000 2925000 3120000 3630000 4970000 3785000 LAST NAME FIRST NAME HIRE DATE BIRTH SALARY DATE AMOUNT

Table Relationships

DEPARTMENT DEPARTMENT DEPARTMENT NAME NUMBER PK 501 301 302 403 402 401 201 marketing sales research and development product planning education software support customer support technical operations 80050000 46560000 22600000 93200000 30800000 98230000 29380000 MANAGER BUDGET EMPLOYEE AMOUNT NUMBER FK 1017 1019 1016 1005 1011 1003 1025

PK = PRIMARY KEY FK = FOREIGN KEY

A Simple SQL SELECT

Obtain the employee numbers for all the employees in the Employee table of the Customer Service Database.
SQL Query SELECT FROM ; employee_number employee

Query Results employee_number 1004 1003 1006 1005 1008 1007

EMPLOYEE
EMPLOYEE NUMBER PK 1006 1008 1005 1004 1007 1003 MANAGER EMPLOYEE NUMBER FK 1019 1019 0801 1003 1005 0801 DEPARTMENT NUMBER FK 301 301 403 401 403 401 JOB CODE FK 312101 312102 431100 412101 432101 411100 LAST NAME FIRST NAME HIRE DATE BIRTH DATE SALARY AMOUNT

Stein Kanieski Ryan Johnson Villegas Trader

John Carol Loretta Darlene Arnando James

761015 770201 761015 761015 770102 760731

531015 580517 550910 460423 370131 470619

2945000 2925000 3120000 3630000 4970000 3785000

SELECT All Columns and All Rows

Obtain all columns of information for all the employees in Employee table of the Customer Service Database.
SELECT FROM ; * employee

employee_number manager_employee_number 1008 1019 1006 1019 1005 801 1004 1003 1007 1005 1003 801

department_number 301 301 403 401 403 401

job_code 312102 312101 431100 412101 432101 411100

last_name Kanieski Stein Ryan Johnson Villegas Trader

first_name Carol John Loretta Darlene Arnando James

hire_date 77/02/01 76/10/15 76/10/15 76/10/15 77/01/02 76/07/31

birthdate salary_amount 58/05/17 29250.00 53/10/15 29450.00 55/09/10 31200.00 46/04/23 36300.00 37/01/31 49700.00 47/06/19 37850.00

EMPLOYEE
EMPLOYEE NUMBER PK 1006 1008 1005 1004 1007 1003 MANAGER EMPLOYEE NUMBER FK 1019 1019 0801 1003 1005 0801 DEPARTMENT NUMBER FK 301 301 403 401 403 401 JOB CODE FK 312101 312102 431100 412101 432101 411100 LAST NAME FIRST NAME HIRE DATE BIRTH DATE SALARY AMOUNT

Stein Kanieski Ryan Johnson Villegas Trader

John Carol Loretta Darlene Arnando James

761015 770201 761015 761015 770102 760731

531015 580517 550910 460423 370131 470619

2945000 2925000 3120000 3630000 4970000 3785000

WHERE Clause

The WHERE clause is used to qualify the rows returned by a SELECT statement. Example:
SELECT employee_number ,hire_date ,last_name ,first_name employee department_number = 401

FROM WHERE ;

employee_number hire_date 1004 76/10/15 1003 76/07/31

last_name Johnson Trader

first_name Darlene James

ORDER BY Clause

The ORDER BY clause specifies the column(s) to be used for sorting the result.
SELECT employee_number ,last_name ,first_name ,hire_date FROM employee WHERE department_number = 401 ORDER BY hire_date ; employee_number last_name first_name hire_date 1003 Trader James 76/07/31 1004 Johnson Darlene 76/10/15

Alternatives to above example:


ORDER BY hire_date ASC; ORDER BY 4;
ORDER BY hire_date DESC;

(ascending sort) (numeric designator)


(descending sort)

Multiple ORDER BY Columns

First column specified is major sort column. Second and subsequent columns are minor sort columns.
SELECT employee_number ,department_number ,job_code FROM employee WHERE department_number < 302 ORDER BY department_number ,job_code ; employee_number 801 1025 1021 1019 1006 1008 department_number 100 201 201 301 301 301 job_code 111100 211100 222100 311100 312100 312100

DISTINCT Option

Without DISTINCT option:


SELECT FROM WHERE ; department_number ,job_code employee department_number = 501

department_number 501 501 501

job_code 512101 512101 511100

With DISTINCT option:


SELECT
FROM WHERE ;

DISTINCT department_number
,job_code employee department_number = 501

department_number 501 501

job_code 511100 512101

Naming Requirements and Qualifications

Naming rules: Database names and user names must be unique within the Teradata RDBMS. TABLE, VIEW, and MACRO names must be unique within a database/user. Column names must be unique within a table. The syntax for qualifying a name is: [ [ databasename. ] tablename. ] columnname Example:
NAME EMPLOYEE.NAME PAYROLL.EMPLOYEE.NAME (unqualified) (partially qualified) (fully qualified)

PAYROLL EMPLOYEE
NUM NAME SALARY

PERSONNEL DEPARTMENT
NUM NAME BUDGET

MFG EMPLOYEE
NUM NAME SALARY

DEPARTMENT
NUM NAME BUDGET

Naming RulesTeradata Extensions

Use Teradata extensions to improve readability beyond the ANSI rules: Names are composed of:
ANSI A-Z 0-9 _ (underscore) Teradata extensions a-z # $

Names must start with:


ANSI A-Z Teradata extensions _ (underscore) a-z # $

Names must not end with:


ANSI _ (underscore) Teradata extensions N/A (may end with any valid character)

Names are limited to:


ANSI 18 Teradata extensions 30

Recommended Coding Conventions

Do this:
SELECT last_name ,first_name ,hire_date ,salary_amount FROM employee WHERE department_number = 401 ORDER BY last_name ;

Not this:
select last_name, first_name, hire_date salary_amount from employee where department_number = 401 order by last_name;
FIND THE ERROR IN THIS REQUEST! TRY TO ADD FORMATTING TO THE HIRE DATE!

Course materials use Teradata Extensions for object names in order to improve readability: Lower and mixed case Up to 30 characters

Default Database

A database may contain: Tablescontain data. Viewsare previously defined windows to the data. Macrosare stored SQL statements. The SQL Parser looks in your default database for the tables, views, or macros you refer to in your SQL statement. Example: .logon johnc; Password: xyz The normal default database isjohnc. Your user profile may be modified to choose a different default database.

Changing the Default Database

Once logged on, you change the default database by using the DATABASE command. Example:
DATABASE payroll;

The default database is payroll until: User logs off A new Database command is issued

You can override a default DATABASE in a single SQL statement by specifying the database you wish to query: Example:
SELECT FROM * payroll.employee

Customer Service Database

CUSTOMER customer_number customer_name parent_customer_number sales_employee_number

TYPE NULLS SIZE I CF I I Y N Y Y -9(10) X(30) -9(10) -9(10)


JOB job_code TYPE NULLS SIZE I CV D D Y N Y Y -9(10) X(40) -9(5).99 -9(5).99

CONTACT contact_number contact_name area_code I CV I2 Y N N -9(10) X(30) -9(10)

description hourly_billing_rate hourly_cost_rate

LOCATION

phone extension last_call_date

I I DA

N Y N

-9(10) -9(10) YY/MM/DD

location_number customer_number first_address_line city state

I I CF CV CF I CF CF

Y N N N N N Y Y

-9(10) -9(10) X(30) X(30) X(15) -9(10) X(30) X(30)

DEPARTMENT department_number department_name budget_amount I2 CF D Y N Y Y -9(5) X(30) -9(9).99 -9(10)

zip_code second_address_line third_address_line

manager_employee_number I

LOCATION_EMPLOYEE location_number I I N N -9(10) -9(10)

EMPLOYEE employee_number I Y Y Y Y N N N N N -9(10) -9(10) -9(10) -9(10) x(20) X(30) YY/MM/DD YY/MM/DD -9(6).99

employee_number

manager_employee_number I department_number job_code last_name first_name hire_date birthdate salary_amount I I CF CV DA DA D

LOCATION_PHONE location_number area_code phone extension description comment_line I I2 I I CV CV Y N N Y N Y -9(10) -9(5) -9(10) -9(10) X(40) X(32000)

KEY

EMPLOYEE_PHONE employee_number area_code phone extension comment_line I I2 I I CF N N N Y Y -9(10) -9(5) -9(10) -9(10) X(72)

I I2 D

integer small integer decimal

DA date CF character, fixed CV character, variable

Lab 1
The exercises in Lab 1 are to be done as a pencil and paper workshop. Write your SQL statements as if you were going to enter them on-line. Lab1_1 Select all columns for all departments from the department table.

Lab 1_2 Request a report of employee last and first names and salary for all of manager 1019's employees. Order the report in last name ascending sequence.

Lab 1_3 Modify the request in Lab1_2 to show department number instead of first name and make it for manager 801's employees instead.

Note: Save Lab 1, as you enter actually enter and run these exercises, using BTEQ scripts, for Lab 2.

Refer to Appendix B for information on the tables and columns used in all of the lab exercises in this book.

Lab 1Optional

Lab 1_4 (Optional) Prepare a report of the department numbers and the manager's employee numbers for everyone in the employee table. Now add the DISTINCT option to the same report.

S-ar putea să vă placă și