Teradata imp

download Teradata imp

of 258

  • date post

  • Category


  • view

  • download


Embed Size (px)



Transcript of Teradata imp

  • 1. undefinedIntroduction to Teradata SQL(Module 1)ObjectivesAfter completing this module, you should be able to: Describe the structure of a Relational Database Management System (RDBMS). Explain the role of Structured Query Language (SQL) in accessing a Relational Database. List the three categories of SQL statements and describe their function.What is an RDBMS?Data is organized into tables in a relational database management system (RDBMS). Rows in thetable represent instances of an entity, in this case an employee or a department. Columnsrepresent the data fields which comprise the rows. Relations between tables occur when acolumn in one table also appears as a column in another.Here is an example of two related tables:

2. Can you answer the following questions using the tables above? Question 1:What is the department name for employee 1004? Question 2:Who is the manager of employee 1004?What is SQL?Structured Query Language (SQL) is the industry standard language for communicating withRelational Database Management Systems. SQL is used to look up data in relational tables,create tables, insert data into tables, grant permissions and many other things.Structured Query Language is used to define the answer set that is returned from the TeradataDatabase.SQL is a non-procedural language, meaning it contains no procedural-type statements such asthose listed here:GO TOPERFORMDO LOOPOPEN FILE 3. CLOSE FILEEND OF FILESQL CommandsSQL statements commonly are divided into three categories: 1. Data Definition Language (DDL) - Used to define and create database objects such astables, views, macros, databases, and users. 2. Data Manipulation Language (DML) - Used to work with the data, including such tasksas inserting data rows into a table, updating an existing row, or performing queries on thedata. The focal point of this course will be on SQL statements in this category. 3. Data Control Language (DCL) - Used for administrative tasks such as granting andrevoking privileges to database objects or controlling ownership of those objects. DCLstatements will not be covered in detail in this course. For complete Data ControlLanguage coverage, please see the NCR Customer Education "Teradata DatabaseAdministration" course.Data Definition Language (DDL) Examples SQL statementFunctionCREATE Define a table, view, macro, index, trigger or stored procedure. DROPRemove a table, view, macro, index, trigger or stored procedure.ALTERChange table structure or protection definition.Data Manipulation Language (DML) SQL statementFunction SELECT Select data from one or more tables. INSERT Place a new row into a table. UPDATE Change data values in one or more existing rows. DELETE Remove one or more rows from a table.Data Control Language (DCL) SQL statementFunctionGRANT Give user privileges. REVOKE Remove user privileges.GIVETransfer database ownership.Relational Concepts 4. The figure below illustrates six rows of a much larger table.Some interesting things to note about this table: The intersection of a row and a column is a data value. Data values come from a particular domain. Domains represent the pool of legal values for a column. For example, the data values in the EMPLOYEE NUMBER and MANAGER EMPLOYEE NUMBER come from the same domain because both columns come from the pool of employee numbers. The domain of employee numbers might be defined as the pool of integers greater than zero. The EMPLOYEE NUMBER column is marked PK, which indicates that this column holds the Primary Key. The next 3 columns are marked FK, which stands for Foreign Key. The purpose of the Primary Key is to uniquely identify each record. No two values in a primary key column can be identical. A Foreign Key represents the Primary Key of another table. Relationships between tables are formed through the use of Foreign Keys. 5. Teradata SQL (Module 2)ObjectivesAfter completing this module, you should be able to: Describe the uses of the Teradata SQL SELECT statement. Retrieve data from a relational table using the SELECT statement. Use the SQL ORDER BY and DISTINCT options. Set a default database using the DATABASE command. Write SQL statements using recommended coding conventions. Name database objects according to Teradata SQL rules.SELECTStructured Query Language (SQL) consists of three types of statements, previously definedas:Data Definition Language (DDL)- Used to create, drop and modify objectsData Manipulation Language(DML)- Used to add, delete, update and read data rows in a tableData Control Language(DCL)- Used to implement security and control on database objects.Our focus in this course will be mostly on the DML portion of the language. We will first look at theSELECT statement.The SELECT statement allows you to retrieve data from one or more tables. In its most common form,you specify certain rows to be returned as shown.SELECT *FROM employeeWHEREdepartment_number = 401;The asterisk, "*", indicates that we wish to see all of the columns in the table. The FROM clausespecifies from which table in our database to retrieve the rows. The WHERE clause acts as a filterwhich returns only rows that meet the specified condition, in this case, records of employees indepartment 401.Note: SQL does not require a trailing semicolon to end a statement but the Basic Teradata Query(BTEQ) utility that we use to enter SQL commands does require it. All examples in this course willinclude the semicolon. 6. What if we had not specified a WHERE clause.SELECT * FROM employee;This query would return all columns and all rows from the employee table.Instead of using the asterisk symbol to specify all columns, we could name specific columnsseparated by commas:SELECT employee_number ,hire_date ,last_name ,first_nameFROM employeeWHEREdepartment_number = 401;employee_numberhire_date last_namefirst_name-------------------- --------------------------------1004 76/10/15JohnsonDarlene1003 76/07/31Trader James1013 77/04/01Phillips Charles1010 77/03/01Rogers Frank1022 79/03/01MachadoAlbert1001 76/06/18Hoover William1002 76/07/31BrownAlanUnsorted ResultsResults come back unsorted unless you specify that you want them sorted in a certain way. Howto retrieve ordered results is covered in the next section.ORDER BY ClauseUse the ORDER BY clause to have your results displayed in a sorted order. Without the ORDERBY clause, resulting output rows are displayed in a random sequence.SELECTemployee_number,last_name,first_name,hire_dateFROMemployeeWHERE department_number = 401ORDER BYhire_date;Sort DirectionIn the example above, results will be returned in ascending order by hire date. Ascending orderis the default sort sequence for an ORDER BY clause. To explicitly specify ascending or 7. descending order, add ASC or DESC, to the end of the ORDER BY clause. The following is anexample of a sort using descending sequence.ORDER BY hire_date DESC;Naming the Sort ColumnYou may indicate the sort column by naming it directly (e.g., hire_date) or by specifying itsposition within the SELECT statement. Since hire_date is the fourth column in the SELECTstatement, the following ORDER BY clause is equivalent to saying ORDER BY hire_date.ORDER BY 4;Multiple ORDER BY ColumnsAn ORDER BY clause may specify multiple columns. No single column in an ORDER BY clauseshould exceed a length of 4096 bytes, otherwise it will be truncated for sorting purposes.The order in which columns are listed in the ORDER BY clause is significant. The column namedfirst is the major sort column. The second and subsequent are minor sort columns. In thefollowing example, results are sorted by department number in ascending order. Wheremultiple records share the same department number, those rows are sorted by job_code inascending order. The following are examples:SELECTemployee_number,department_number,job_codeFROMemployeeWHERE department_number < 302ORDER BYdepartment_number,job_code;employee_numberdepartment_numberjob_code-------------------- -------------------------------- 801 1001111001025 2012111001021 2012221011019 3013111001006 3013121011008 301312102Note: Each column specified in the ORDER BY clause can have its own sort order, eitherascending or descending.SELECT employee_number ,department_number ,job_codeFROM employeeWHEREdepartment_number < 302ORDER BY department_number ASC ,job_code DESC; 8. employee_number department_number job_code ------------------------------------------ ----------801100 111100 1021201 222101 1025201 211100 1008301 312102 1006301 312101 1019301 311100DISTINCTThe DISTINCT operator will consolidate duplicate output rows to a single occurrence.Example Without DISTINCTSELECTdepartment_number,job_codeFROMemployeeWHERE department_number = 501; department_number job_code ---------------------- ----------501 512101501 512101501 511100Note: Two people in department 501 have the same job code (512101). If our purpose issimply to find out which job codes exist in department 501, we could use DISTINCT to avoidseeing duplicate rows.Example With DISTINCTSELECTDISTINCT department_number,job_codeFROMemployeeWHERE department_number = 501; department_numberjob_code --------------------------------501 511100501 512101Note: DISTINCT appears directly after SELECT, and before the first named column. It mayappear to apply only to the first column, but in fact, DISTINCT applies to all columns named inthe query. Two rows in our result example set both have department_number 501. Thecombination of department_number and job_code are distinct since the job codes differ.Naming Database ObjectsAll Teradata objects, such as tables, must be assigned a name by the user when they are 9. created.These rules for naming objects are summarized as follows:Names are composed of: a-z A-Z 0-9 _ (underscore) $ #Names are limited to 30 characters.Names cannot begin with a number.Teradata names are not case-sensitive.Examples of valid names:AccountsAccounts_2005accounts_over_$2000Account#EquivalenceAccounts