Weitere ähnliche Inhalte
Ähnlich wie Lesson01 学会使用基本的SQL语句 (20)
Kürzlich hochgeladen (20)
Lesson01 学会使用基本的SQL语句
- 1. Oracle OCP 考试系列培训
之
1Z0-007 Lesson1
www.OracleOnLinux.cn
1-1 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 2. 1
Retrieving Data Using
the SQL SELECT Statement
1-2 Copyright © 2011, www.OracleOnLinux . Part rights reserved.
OracleOnLinux
OracleOnLinux.cn
- 3. Objectives
After completing this lesson, you should be able to do
the following:
• List the capabilities of SQL SELECT statements
• Execute a basic SELECT statement
• Differentiate between SQL statements and
iSQL*Plus commands
SQL*
1-3 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 4. Lesson Agenda
• Basic SELECT statement
• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
alternative quote operator, and the DISTINCT keyword
• DESCRIBE command
1-4 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 5. Capabilities of SQL SELECT Statements
Projection Selection
Table 1 Table 1
Join
Table 1 Table 2
1-5 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 6. Basic SELECT Statement
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
• SELECT identifies the columns to be displayed
• FROM identifies the table containing those columns
1-6 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 7. Selecting All Columns
SELECT *
FROM departments;
1-7 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 8. Selecting Specific Columns
SELECT department_id, location_id
FROM departments;
1-8 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 9. Writing SQL Statements
• SQL statements are not case-sensitive.
• SQL statements can be on one or more lines.
• Keywords cannot be abbreviated or split
across lines.
• Clauses are usually placed on separate lines.
• Indents are used to enhance readability.
• In iSQL*Plus, SQL statements can optionally be
SQL*
terminated by a semicolon (;). Semicolons are
required if you execute multiple SQL statements.
• In SQL*plus, you are required to end each SQL
SQL*
statement with a semicolon (;).
1-9 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 10. Column Heading Defaults
• iSQL*Plus:
SQL*
– Default heading alignment: Center
– Default heading display: Uppercase
• SQL*Plus:
SQL*
– Character and Date column headings are
Left- aligned
– Number column headings are right-aligned
– Default heading display: Uppercase
1-10 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 11. Arithmetic Expressions
Create expressions with number and date data by
using arithmetic operators.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
1-11 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 12. Using Arithmetic Operators
SELECT last_name, salary, salary + 300
FROM employees;
…
1-12 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 13. Operator Precedence
SELECT last_name, salary, 12*salary+100
12*
FROM employees; 1
…
SELECT last_name, salary, 12*(salary+100)
12*
FROM employees; 2
…
1-13 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 14. Defining a Null Value
• A null is a value that is unavailable, unassigned,
unknown, or inapplicable.
• A null is not the same as a zero or a blank space.
SELECT last_name, job_id, salary, commission_pct
FROM employees;
…
…
1-14 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 15. Null Values
in Arithmetic Expressions
Arithmetic expressions containing a null value
evaluate to null.
SELECT last_name, 12*salary,12*salary*(1+commission_pct)
12* 12* salary*
FROM employees;
…
…
1-15 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 16. Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name (There can
also be the optional AS keyword between the
column name and alias.)
• Requires double quotation marks if it contains
spaces or special characters or if it is case-
sensitive
• Can not be used in Where clause
1-16 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 17. Using Column Aliases
SELECT last_name AS name, commission_pct comm
FROM employees;
…
SELECT last_name "Name" , salary*12 "Annual Salary"
salary*
FROM employees;
…
1-17 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 18. Concatenation Operator
A concatenation operator:
• Links columns or character strings to other
columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character
expression
SELECT last_name||job_id AS "Employees"
FROM employees;
…
1-18 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 19. Concatenation Operator
SELECT last_name||'''s'||salary
FROM employees;
SELECT last_name||chr(39)||'s'||salary
FROM employees;
SELECT ASCII('''')
FROM dual;
SELECT ASCII('?')
FROM dual;
1-19 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 20. Literal Character Strings
• A literal is a character, a number, or a date that is
included in the SELECT statement.
• Date and character literal values must be enclosed
by single quotation marks.
• Each character string is output once for each
row returned.
1-20 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 21. Using Literal Character Strings
SELECT last_name ||' is a ' ||job_id
AS "Employee Details"
FROM employees;
…
1-21 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 22. Duplicate Rows
The default display of queries is all rows, including
duplicate rows.
SELECT department_id
FROM employees; 1
…
SELECT DISTINCT department_id
FROM employees; 2
…
1-22 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 23. SQL and iSQL*Plus Interaction
SQL*
SQL statements
Oracle
server
Internet
browser
iSQL*Plus
SQL* Query results
commands
Formatted report
Client
1-23 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 24. SQL Statements Versus
iSQL*Plus Commands
SQL*
SQL iSQL*Plus
SQL*
• A language • An environment
• ANSI standard • Oracle-proprietary
• Keyword cannot be • Keywords can be
abbreviated abbreviated
• Statements manipulate • Commands do not allow
data and table definitions manipulation of values in
in the database the database
• Runs on a browser
• Centrally loaded; does not
have to be implemented
on each machine
SQL iSQL*Plus
SQL*
statements commands
1-24 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 25. Overview of iSQL*Plus
SQL*
After you log in to iSQL*Plus, you can:
SQL*
• Describe table structures
• Enter, execute, and edit SQL statements
• Save or append SQL statements to files
• Execute or edit statements that are stored in
saved script files
1-25 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 26. Logging In to iSQL*Plus
SQL*
From your browser environment:
1-26 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 27. iSQL*Plus Environment
SQL*
8 9
7
1
6
2 3 4 5
1-27 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 28. Displaying Table Structure
Use the iSQL*Plus DESCRIBE command to display the
SQL*
structure of a table:
DESC[RIBE] tablename
1-28 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 30. Interacting with Script Files
SELECT last_name, hire_date, salary
FROM employees; 1
2
1-30 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 33. Interacting with Script Files
D:TEMPemp_data.sql
2
3
1-33 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 37. Setting the Output Location Preference
2
1
1-37 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 38. Summary
In this lesson, you should have learned how to:
• Write a SELECT statement that:
– Returns all rows and columns from a table
– Returns specified columns from a table
– Uses column aliases to display more descriptive
column headings
• Use the iSQL*Plus environment to write, save, and
SQL*
execute SQL statements and iSQL*Plus
SQL*
commands
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
1-38 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 39. Practice 1: Overview
This practice covers the following topics:
• Selecting all data from different tables
• Describing the structure of tables
• Performing arithmetic calculations and specifying
column names
• Using iSQL*Plus
SQL*
1-39 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 40. Practice 1
Q1: A SELECT statement can be used to perform these
three functions:
1. Choose rows from a table.
2. Choose columns from a table.
3. Bring together data that is stored in different tables by
creating a link between them.
Which set of keywords describes these capabilities?
A. difference, projection, join
B. selection, projection, join
C. selection, intersection, join
D. intersection, projection, join
E. difference, projection, product
1-40 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 41. Practice 1
Q2: Evaluate this SQL statement:
SELECT e.EMPLOYEE_ID,e.LAST_NAME,
e.DEPARTMENT_ID, d.DEPARTMENT_NAME
FROM EMPLOYEES e, DEPARTMENTS d
WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID;
In the statement, which capabilities of a SELECT statement are
performed?
A. selection, projection, join
B. difference, projection, join
C. selection, intersection, join
D. intersection, projection, join
E. difference, projection, product
1-41 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 42. Practice 1
Q3: Evaluate this SQL statement:
SELECT ename, sal, 12 *sal+100
12*
FROM emp;
The SAL column stores the monthly salary of the employee.
Which change must be made to the above syntax to calculate
the annual compensation as "monthly salary plus a monthly
bonus of $100, multiplied by 12"?
A. No change is required to achieve the desired results.
B. SELECT ename, sal, 12 *(sal+100) FROM emp;
12*
C. SELECT ename, sal, (12 *sal)+100 FROM emp;
(12*
D. SELECT ename, sal+100, *12 FROM emp;
sal+100,*
1-42 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 43. Practice 1
Q4: Which SQL statement generates the alias Annual
Salary for the calculated column SALARY *12?
SALARY*
A. SELECT ename, salary *12 'Annual Salary'
salary*
FROM employees;
B. SELECT ename, salary *12 "Annual Salary"
salary*
FROM employees;
C. SELECT ename, salary *12 AS Annual Salary
salary*
FROM employees;
D. SELECT ename, salary *12 AS INITCAP("ANNUAL
salary*
SALARY")
FROM employees
1-43 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 44. Practice 1
Q5: The CUSTOMERS table has these columns:
CUSTOMER_ID NUMBER(4) NOT NULL
CUSTOMER_NAME VARCHAR2(100) NOT NULL
CUSTOMER_ADDRESS VARCHAR2(150)
CUSTOMER_PHONE VARCHAR2(20)
You need to produce output that states "Dear Customer
customer_name, ".
The customer_name data values come from the
CUSTOMER_NAME column in the CUSTOMERS table.
Which statement produces this output?
1-44 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 45. Practice 1
A. SELECT dear customer, customer_name,
FROM customers;
B. SELECT "Dear Customer", customer_name || ','
FROM customers;
C. SELECT 'Dear Customer ' || customer_name ','
FROM customers;
D. SELECT 'Dear Customer ' || customer_name || ','
FROM customers;
E. SELECT "Dear Customer " || customer_name || ","
FROM customers;
F. SELECT 'Dear Customer ' || customer_name || ',' ||
FROM customers;
1-45 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
- 46. Practice 1
Q6: Which two are attributes of iSQL *Plus? (Choose two.)
iSQL*
A. iSQL*Plus commands cannot be abbreviated.
iSQL*
B. iSQL*Plus commands are accessed from a browser.
iSQL*
C. iSQL*Plus commands are used to manipulate data in
iSQL*
tables.
D. iSQL*Plus commands manipulate table definitions in
iSQL*
the database.
E. iSQL*Plus is the Oracle proprietary interface for
iSQL*
executing SQL statements.
1-46 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.