Teradata SQL Assistant/Web Edition Hands-On SQL...

23
Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web Edition This product is designed to provide a simple way to execute and manage your queries against a Teradata, or other ODBC compliant database. SQL Assistant Web stores your queries for easy re- use, and provides you with an audit trail that shows the steps that produced your current results. Hands-On SQL Assistant! In this tutorial, experience the simplicity and ease of use of this powerful browser-based query tool. This hands-on session will show you some of the ways in which you can use this tool in order to maximize your efficiency. Hands-on activities will include connecting to a Teradata database, executing a query, and manipulating the results. With the new write capabilites you will also be able to create new databases, make copies of existing databases, and put data into your own databases. Lesson In this short lesson we will: 1. Start Teradata SQL Assistant/Web Edition 2. Connect to a Teradata system 3. Demonstrate SQL Assistant Web functions 1. Start Teradata SQL Assistant/Web Edition To start SQL Assistant Web click on Execute Teradata SQL Assistant/Web Edition.

Transcript of Teradata SQL Assistant/Web Edition Hands-On SQL...

Page 1: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

Teradata SQL Assistant/Web Edition

This product is designed to provide a simple way to execute and manage your queries against a Teradata, or other ODBC compliant database. SQL Assistant Web stores your queries for easy re-use, and provides you with an audit trail that shows the steps that produced your current results.

Hands-On SQL Assistant!

In this tutorial, experience the simplicity and ease of use of this powerful browser-based query tool. This hands-on session will show you some of the ways in which you can use this tool in order to maximize your efficiency. Hands-on activities will include connecting to a Teradata database, executing a query, and manipulating the results. With the new write capabilites you will also be able to create new databases, make copies of existing databases, and put data into your own databases.

Lesson

In this short lesson we will:

1. Start Teradata SQL Assistant/Web Edition 2. Connect to a Teradata system 3. Demonstrate SQL Assistant Web functions

1. Start Teradata SQL Assistant/Web Edition

To start SQL Assistant Web click on Execute Teradata SQL Assistant/Web Edition.

Page 2: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

2. Login to Teradata SQL Assistant/Web Edition

Login using your SQL Login, Password, Default Database (if applicable), and click OK. This is the login you entered when you created your Teradata SQL Assistant/Web Edition account.

Since you have access to all databases, and you do not have an assigned Default Database, you may enter one on the login screen.

Entering a default database here is the same as using the „database‟ command in SQL Assistant (i.e. typing in and executing „database db_watson‟ to access db_watson). Imagine having a row of 3 mimes, a red mime, a blue mime, and a green mime, that obey your commands. By default, you are looking at the red mime. Any commands you issue apply only to the mime you are focusing on. In order to issue commands to the blue mime, you need to „focus‟ on that mime. Likewise, to issue commands to the green mime, you will need to adjust your „focus‟ again. In the context of a Teradata database, this is exactly what the „database‟ command does.

Page 3: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

3. Teradata SQL Assistant/Web Edition

SQL Assistant consists of a left frame to show the tree and a main window divided into 3 areas.

A Query section where you enter the SQL query you wish to execute.

A History section where information about previously executed queries are displayed.

And an Answer Set section where your query results are displayed.

The tree on the left is used to view the databases. By default, you will see DBC and your login name. The database named after your login name has been granted perm space, which allows you to write data to the database. This includes the ability to define databases, create table, and insert data. Further specifics regarding your perm space will be discussed later in this document.

If you entered a Default Database in Step 2, it will also appear in the tree at the left. If you want to add additional databases, click Add above the tree on the left, and enter the desired database.

Adding a database via this method allows you to view the database under the database tree. In order to access the database you will need to use the database command (i.e. „database db_watson‟) to direct your SQL commands to that particular database.

If you click on the + sign next to the database you can expand to see the tables, views, Macros, and Procedures.

Page 4: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

As an example, add the database „db_watson‟ (if not already there). Click on the + sign next to db_watson, then click on the + sign next to Tables to expand.

Page 5: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

4. Executing Queries

The top section is used to enter your query. If you have a query stored (query in a text format on your hard drive) you can use the Browse to search. You can Load the query into the query section and click Execute. You may also run the query by clicking Run. Once you have written a query, you may save it by clicking on .

Enter your query in the query window, verify the maximum number of rows you wish to return and click the button. If you want to execute multiple queries in parallel, click the button.

When you execute a query, you will see your query in the History section. If there is an error, you will see a brief description of the error on the bottom left of your screen. If you want to see more pages of your history, click on the numbers located above the Clear History button. You may also clear the entire history, or save it to a file.

The results of your query will be displayed the Answer Set section. You can sort the answer set by clicking on the underlined column name of the screen.

If you want to run the query again, you can click on Select and your query will appear in the Query section. Then click Execute. If you would like to add a brief notation about the query, click on Edit next to the query, and add your comments.

5. When you have finished using Teradata SQL Assistant/Web Edition, please click on the button on the top of the screen to disconnect. You may use your browser to go back to the Teradata University Network home page.

Page 6: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

Notes on Write Access and Data Loading

Write Access

A new feature on TUN Web is write access. Students are automatically assigned the permanent space they need to create tables, indexes, and all database structures as well as copy and insert data. This perm space appears in the directory tree with a name that is the student‟s login id. Write access to their perm space is automatically granted to students when they register for a course. To use this space, students must change from the default database for your course to their perm space by issuing the „database <database_name>‟ command, referring to their perm space name as the <database_name>. Students may manage the rights for other students to use their perm space through SQL GRANT and REVOKE commands.

Instructor accounts are granted similar privileges but with the additions of more permanent space and select access on student accounts in your courses.

Batch Loading

The following are instructions for how to load data into your perm space from an Excel spreadsheet, a Microsoft Access database, or from another Teradata database. We will illustrate below how to do this from each source.

From Excel:

1. Open your spreadsheet with data

2. Click on the first cell, cell A1

Step 2

Page 7: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

3. In the menu bar, go to Insert->Columns

4. Enter “insert into <mytable> values(“, replacing <mytable> with your desired table name and omitting the quotation marks as shown in the screen capture below for the example table „sample_table‟.

Step 4

5. Select and highlight the first column, „A‟

Page 8: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

6. In the menu bar, go to Edit->Fill->Down

Step 6

Page 9: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

7. Click on the empty cell at the end of the first data row.

8. Enter “);” without the quotation marks.

Steps 7 and 8

9. Now highlight all of the cells in the column from the first data row to the last either by clicking and dragging on that cell or by clicking on that cell, holding down the „shift‟ key, and clicking on the last row.

10. In the menu bar, go to Edit->Fill->Down. This places a closed parenthesis at the end of each row.

11. In the menu bar, go to File->Save As…

Note: Teradata Data Types have different requirements. For example, in the dataset above, character strings are enclosed by quotation marks (i.e. „Sam‟). For more information about data types, please reference the “Teradata User Manuals: Data Types and Literals” PDF document available via the “Teradata Database User Manuals” link on the main page.

Page 10: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

12. Select CSV (Comma delimited) (*.csv) as the File Type

Step 12

13. Save the file

Page 11: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

14. Now open the file you saved in Notepad. Note pad can be found by clicking on Start, Run, type „notepad‟, and press enter.

15. In the menu bar, go to Edit->Replace

16. Under “Find what” type in “(,” without the quotation marks

17. Under “Replace with” type in”(” without the quotation marks

Page 12: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

Steps 16 and 17

18. Click on the “Replace All” button

19. In the menu bar, go to Edit->Replace

20. Under “Find what” type in “,)” without the quotation marks

21. Under “Replace with” type in”)” without the quotation marks

22. Click on the “Replace All” button

23. In the menu bar, go to File->Save

Page 13: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

24. Login to the SQL Assistant Web Edition utility and under Default Database type in your SQL username. Alternatively, if you are already logged-in to SQL Assistant, issue the „database‟ command to switch to your perm space as the database to use.

Step 24

Page 14: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

25. If you have not already done so, create a table for your data by executing the proper SQL commands to create the table, indexes, and constraints you wish to use. For example, to create a table, type in the SQL statement in the query window and click on the button, as illustrated below:

Step 25

Page 15: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

26. Click on the button at the top. This will bring up a new window. Then click on „Browse‟.

27. Open your CSV file that you saved earlier

Steps 26 and 27

28. Click the „Run‟ button to execute the statements in your .csv file

29. Verify that the data were loaded correctly by executing the following SQL statement without the quotation marks and where <mytable> is replaced by the name of the table you created: “select * from <mytable> order by 1;”

The „order by 1‟ clause sorts the answer set alphabetically by the first column.

Step 29

Page 16: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

From Microsoft Access:

1. Run Microsoft Access

2. Open your Microsoft Access MDB file.

3. In Tables view, select/highlight a table

4. In the menu bar, go to File->Export

Step 4

Page 17: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

5. Specify the filename and location you want to export to

6. Select data type: “Microsoft Excel 97-2003 (*.xls)”

Steps 5 and 6

7. Click Export

8. Run Microsoft Excel

9. Open the file you just created from Export

Page 18: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

10. Click on Cell A1

Step 10

Page 19: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

11. In the menu bar, go to Edit->Delete->Entire Row

Step 11

12. Save and Continue with Step 2 of the Microsoft Excel instructions above.

Page 20: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

From Teradata (table copy):

1. These instructions are meant to copy all data from a table customer_t located in a database db_pvfc to your SQL account. Both customer_t and db_pvfc should be replaced by the actual names of the table and database you wish to copy from.

2. Login to the SQL Assistant Web Edition utility and under Default Database type in your SQL username

Step 2

Page 21: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

3. For illustration purposes, assume you wanted to create your own copy of the customer table from the db_pvfc database stored on the SQL Assistant server.

Enter the following SQL statement without the quotation marks in the query window and click the „Execute‟ button:

“create table customer_t as db_pvfc.customer_t with no data;”

Step 3

4. Now repeat Step 3 with the following statement instead (this will make a complete copy of the customer_t table; you can, of course, write any SQL select statement to copy only select rows):

“insert into customer_t select * from db_pvfc.customer_t;”

5. Verify that the table was copied correct by executing the following statement:

“select * from customer_t order by 1;”

Page 22: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

From Teradata (to create your own copy of one of the existing databases available with SQL Assistant):

1. These instructions are meant to copy all tables within a database db_pvfc_std to your SQL account. Note that db_pvfc_std should be replaced by the name of the database you wish to copy from.

2. To start, download and save the following text file: http://tunweb.teradata.ws/tunstudent/tunscripts/copy_db_pvfc_std.txt

This text file is essentially a preassembled script file with a set of SQL commands to copy an existing database.

Additionally, script files for each of the existing databases have been generated and are available through the „Database Descriptions‟ link on the bottom of the SQL Assistant login page.

3. Login to the SQL Assistant Web Edition utility and under Default Database type in your SQL username

4. Click on the „Browse‟ button. Locate and open copy_db_pvfc_std.txt.

5. Click on the „Run‟ button to execute the SQL statements within copy_db_pvfc_std.txt

Page 23: Teradata SQL Assistant/Web Edition Hands-On SQL …academic.udayton.edu/davesalisbury/classtuf/MIS385/assign/sql... · Teradata SQL Assistant Web Hands-On Lesson Teradata SQL Assistant/Web

Teradata SQL Assistant Web Hands-On Lesson

That is all for our quick tour of the Teradata SQL Assistant/Web Edition.

In conclusion, we hope that we have given you a brief insight into some of the capabilities of Teradata SQL Assistant/Web Edition, and that you will be motivated to learn more of its capabilities. The SQL Assistant help file (located in the upper right hand corner of the SQL Assistant screen), and the User‟s Guide (located on the Teradata University Network home page under the Teradata SQL Assistant/Web Edition executable) will give you a detailed description of the product.