Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 56 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
56
Dung lượng
360,64 KB
Nội dung
SelectingRows 1 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder1Ć2 Schedule: Timing Topic 40 minutes Lecture 25 minutes Practice 65 minutes Total Class Management Note: Files required for lesson are: Demonstration: l1prec1.sql, l1prec2.sql, l1alias.sql, l1null1.sql Practice: None SelectingRows 1Ć3 Objectives In order to extract data from the database you need to use the Structured Query Language (SQL) SELECT command. You may need to restrict the columns that are displayed. This lesson explains all of the commands you will use to perform these actions. You will want to create SELECT statements that can be used time and time again. In this lesson you will also see how to save your statements for later use. At the end of this lesson, you should be able to D Write a SELECT statement to query the database. D Perform arithmetic calculations using SQL arithmetic operators. D Handle null values. D Specify alternative column headings using aliases. D Concatenate columns. D Edit SQL statements in the SQL*Plus buffer and create command files. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder1Ć4 SelectingRows 1Ć5 The Basic Query Block A SELECT statement retrieves information from the database, implementing all algebraic operators. Syntax SELECT [DISTINCT] {*,column [alias], } FROM table; where: SELECT is a list of at least one column. DISTINCT suppresses duplicates. * selects all columns. column selects the named column. alias gives selected columns a different heading. FROM table specifies the table containing the columns. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder1Ć6 SelectingRows 1Ć7 The Basic Query Block continued Writing SQL Commands By following these simple rules and guidelines, you will be able to construct valid statements that are easy both to read and to edit. D SQL commands may be entered on one or many lines. D Clauses are usually placed on separate lines for readability and ease of editing. D Tabs and indents can be used to make code more readable. D Command words cannot be split across lines or abbreviated. D Keywords and commands typically are entered in uppercase; all other words, such as table names and columns, are entered in lowercase. D SQL commands are not case sensitive, unless indicated. D An SQL command is entered at the SQL prompt, and subsequent lines are numbered. This is called the SQL buffer. D Only one statement can be current at any time within the buffer, and the statement can be executed in a number of ways: D Place a semicolon (;) at the end of last clause. D Place a semicolon or slash on the last line in the buffer. D Place a slash at the SQL prompt. D Issue a SQL*Plus RUN command at the SQL prompt. For more information, see Oracle Applications: Coding Standards, Release 10G. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder1Ć8 Simplest SELECT statement contains the following two clauses: SelectingRows 1Ć9 The Basic Query Block continued In its simplest form, a SELECT statement must include the following: D A SELECT clause, which specifies the columns to be displayed. D A FROM clause, which specifies the table containing the columns listed in the SELECT clause. Selecting All Columns and Rows The asterisk (*) selects all columns from the table. Example List all columns and all rows from the S_DEPT table. SQL> SELECT * 2 FROM s_dept; ID NAME REGION_ID ------- --------------- --------- 10 Finance 1 31 Sales 1 32 Sales 2 33 Sales 3 34 Sales 4 35 Sales 5 41 Operations 1 42 Operations 2 43 Operations 3 44 Operations 4 45 Operations 5 50 Administration 1 12 rows selected. Class Management Note: Let the students know that the details of all the tables are given in Appendix B. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder1Ć10 [...]... the Selection of Duplicate Rows Unless you indicate otherwise, SQL*Plus displays the results of a query without eliminating duplicate rows Example Displaying All Rows Display all department names in the S_DEPT table SQL> SELECT 2 FROM name s_dept; NAME -Finance Sales Sales Sales Sales Sales Operations 12 rows selected The DISTINCT Keyword To eliminate duplicate rows in the result, include... NVL(date_column,‘01-JAN-95’) CHAR or VARCHAR2 NVL(character_column,‘Unavailable’) SelectingRows 1Ć29 SQL> SELECT 2 FROM DISTINCT name s_dept; Class Management Note: DEMO PURPOSE: Show all the rows Then compare the result by using the DISTINCT keyword 1 At the prompt, enter: SELECT name FROM s_dept; 2 Show the students the duplicate rows in the output 3 At the prompt, enter: SELECT DISTINCT name FROM s_dept;... immediately after the SELECT command word Example Displaying Unique Rows Display all unique department names in the S_DEPT table SQL> SELECT 2 FROM DISTINCT name s_dept; NAME Administration Finance Operations Sales SelectingRows 1Ć31 1Ć32 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Preventing the Selection of Duplicate Rows continued DISTINCT with Multiple Columns You can specify... improve clarity For example, the expression above can be written as (12 * salary) + 100 with no change in the result SelectingRows 1Ć17 Class Management Note: DEMO: l1prec1.sql, l1prec2.sql PURPOSE: l1prec1.sql demonstrates the previous page’s example with no parentheses Note the first couple rows of totals Then, execute demo file l1prec2.sql, which demonstrates using the parentheses to override the rules... Clerk VP, Operations Warehouse Manager Stock Clerk Warehouse Manager Stock Clerk Warehouse Manager Stock Clerk Warehouse Manager Stock Clerk Warehouse Manager President VP, Administration 21 rows selected SelectingRows 1Ć33 1Ć34 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder SQL*Plus Commands In this lesson, you saw how SQL commands are executed within a product called SQL*Plus SQL*Plus... comply with the ANSI SQL 92 standard You can use this method of achieving mixed case column headings for now In a later section, you can format headings using the SQL*Plus COLUMN command section SelectingRows 1Ć21 1Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder The Concatenation Operator You can link columns to other columns, arithmetic expressions, or constant values to create... MidoriNagayama MarkQuick-To-See AudryRopeburn MollyUrguhart The AS keyword before the alias name makes the SELECT clause easier to read Class Management Note: The resulting column is VARCHAR2 datatype SelectingRows 1Ć23 1Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Literal Character String A literal is any character, expression, or number included in the SELECT list that is not a column... Molly Urguhart, Warehouse Manager Class Management Note: Point out to students that there is a blank space between single quotes in the middle of first_name and last_name, in the select statement SelectingRows 1Ć25 Class Management Note: Make sure everyone is aware of exactly what a null is DEMO: l1null1.sql PURPOSE: Demonstrates why sales representatives have values and everyone else has a null No... Representative Sales Representative Sales Representative Sales Representative Stock Clerk 140 186.25 151.5 228.75 253.75 For more information, see Oracle7 Server SQL Reference, Release 7.3, “Elements of SQL.” SelectingRows 1Ć27 1Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Managing Null Values continued NVL Function In order to convert a null value to an actual value, use the NVL function... These are the arithmetic operators available in SQL You may use arithmetic operators in any clause of a SQL statement except the FROM clause Operators Description + Add - Subtract * Multiply / Divide SelectingRows 1Ć13 1Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Arithmetic Expressions continued Example Use the multiplication operator to display annual salary figures and their commission . the SELECT clause. Selecting All Columns and Rows The asterisk (*) selects all columns from the table. Example List all columns and all rows from the S_DEPT. Oracle: SQL and PL/SQL Using Procedure Builder1Ć10 Selecting Rows 1Ć11 The Basic Query Block continued Selecting Specific Columns You restrict the query