Lesson01 学会使用基本的SQL语句

46
1-1 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved. Oracle OCP Oracle OCP Oracle OCP Oracle OCP 考试系列培训 1Z0-007 Lesson1 1Z0-007 Lesson1 1Z0-007 Lesson1 1Z0-007 Lesson1 www.OracleOnLinux.cn www.OracleOnLinux.cn www.OracleOnLinux.cn www.OracleOnLinux.cn

description

Oracle OCP考试之007第1章,学会使用基本的SQL语句!

Transcript of Lesson01 学会使用基本的SQL语句

Page 1: Lesson01 学会使用基本的SQL语句

1-1 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Oracle OCPOracle OCPOracle OCPOracle OCP 考试系列培训之

1Z0-007 Lesson11Z0-007 Lesson11Z0-007 Lesson11Z0-007 Lesson1www.OracleOnLinux.cnwww.OracleOnLinux.cnwww.OracleOnLinux.cnwww.OracleOnLinux.cn

Page 2: Lesson01 学会使用基本的SQL语句

1111Copyright © 2011, www.OracleOnLinuxOracleOnLinuxOracleOnLinuxOracleOnLinux.cn. Part rights reserved.1-2

Retrieving Data Using Retrieving Data Using Retrieving Data Using Retrieving Data Using the SQL the SQL the SQL the SQL SELECTSELECTSELECTSELECT Statement Statement Statement Statement

Page 3: Lesson01 学会使用基本的SQL语句

1-3 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

ObjectivesObjectivesObjectivesObjectives

After completing this lesson, you should be able to do After completing this lesson, you should be able to do After completing this lesson, you should be able to do After completing this lesson, you should be able to do the following:the following:the following:the following:• List the capabilities of SQL List the capabilities of SQL List the capabilities of SQL List the capabilities of SQL SELECTSELECTSELECTSELECT statements statements statements statements• Execute a basic Execute a basic Execute a basic Execute a basic SELECTSELECTSELECTSELECT statement statement statement statement• Differentiate between SQL statements and Differentiate between SQL statements and Differentiate between SQL statements and Differentiate between SQL statements and

iiiiSQLSQLSQLSQL****Plus commandsPlus commandsPlus commandsPlus commands

Page 4: Lesson01 学会使用基本的SQL语句

1-4 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Lesson AgendaLesson AgendaLesson AgendaLesson Agenda

• Basic Basic Basic Basic SELECTSELECTSELECTSELECT statement statement statement statement• Arithmetic expressions and NULL values in the Arithmetic expressions and NULL values in the Arithmetic expressions and NULL values in the Arithmetic expressions and NULL values in the

SELECT statementSELECT statementSELECT statementSELECT statement• Column aliasesColumn aliasesColumn aliasesColumn aliases• Use of concatenation operator, literal character strings, Use of concatenation operator, literal character strings, Use of concatenation operator, literal character strings, Use of concatenation operator, literal character strings,

alternative quote operator, and the DISTINCT keywordalternative quote operator, and the DISTINCT keywordalternative quote operator, and the DISTINCT keywordalternative quote operator, and the DISTINCT keyword• DESCRIBE commandDESCRIBE commandDESCRIBE commandDESCRIBE command

Page 5: Lesson01 学会使用基本的SQL语句

1-5 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Capabilities of SQL Capabilities of SQL Capabilities of SQL Capabilities of SQL SELECTSELECTSELECTSELECT Statements Statements Statements Statements

SelectionSelectionSelectionSelectionProjectionProjectionProjectionProjection

Table 1Table 1Table 1Table 1 Table 2Table 2Table 2Table 2

Table 1Table 1Table 1Table 1Table 1Table 1Table 1Table 1

JoinJoinJoinJoin

Page 6: Lesson01 学会使用基本的SQL语句

1-6 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Basic Basic Basic Basic SELECTSELECTSELECTSELECT Statement Statement Statement Statement

• SELECTSELECTSELECTSELECT identifies the columns to be displayed identifies the columns to be displayed identifies the columns to be displayed identifies the columns to be displayed• FROMFROMFROMFROM identifies the table containing those columns identifies the table containing those columns identifies the table containing those columns identifies the table containing those columns

SELECT SELECT SELECT SELECT ****|{[DISTINCT] |{[DISTINCT] |{[DISTINCT] |{[DISTINCT] columncolumncolumncolumn||||expressionexpressionexpressionexpression [ [ [ [aliasaliasaliasalias],...}],...}],...}],...}FROM FROM FROM FROM table;table;table;table;

Page 7: Lesson01 学会使用基本的SQL语句

1-7 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Selecting All ColumnsSelecting All ColumnsSelecting All ColumnsSelecting All Columns

SELECT SELECT SELECT SELECT ****FROM departments;FROM departments;FROM departments;FROM departments;

Page 8: Lesson01 学会使用基本的SQL语句

1-8 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Selecting Specific ColumnsSelecting Specific ColumnsSelecting Specific ColumnsSelecting Specific Columns

SELECT department_id, location_idSELECT department_id, location_idSELECT department_id, location_idSELECT department_id, location_idFROM departments;FROM departments;FROM departments;FROM departments;

Page 9: Lesson01 学会使用基本的SQL语句

1-9 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Writing SQL StatementsWriting SQL StatementsWriting SQL StatementsWriting SQL Statements

• SQL statements are not case-sensitive. SQL statements are not case-sensitive. SQL statements are not case-sensitive. SQL statements are not case-sensitive. • SQL statements can be on one or more lines.SQL statements can be on one or more lines.SQL statements can be on one or more lines.SQL statements can be on one or more lines.• Keywords cannot be abbreviated or splitKeywords cannot be abbreviated or splitKeywords cannot be abbreviated or splitKeywords cannot be abbreviated or split

across lines.across lines.across lines.across lines.• Clauses are usually placed on separate lines.Clauses are usually placed on separate lines.Clauses are usually placed on separate lines.Clauses are usually placed on separate lines.• Indents are used to enhance readability.Indents are used to enhance readability.Indents are used to enhance readability.Indents are used to enhance readability.• In In In In iiiiSQLSQLSQLSQL****Plus, SQL statements can optionally be Plus, SQL statements can optionally be Plus, SQL statements can optionally be Plus, SQL statements can optionally be

terminated by a semicolon (;). Semicolons are terminated by a semicolon (;). Semicolons are terminated by a semicolon (;). Semicolons are terminated by a semicolon (;). Semicolons are required if you execute multiple SQL statements. required if you execute multiple SQL statements. required if you execute multiple SQL statements. required if you execute multiple SQL statements.

• In SQLIn SQLIn SQLIn SQL****plus, you are required to end each SQL plus, you are required to end each SQL plus, you are required to end each SQL plus, you are required to end each SQL statement with a semicolon (;).statement with a semicolon (;).statement with a semicolon (;).statement with a semicolon (;).

Page 10: Lesson01 学会使用基本的SQL语句

1-10 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Column Heading DefaultsColumn Heading DefaultsColumn Heading DefaultsColumn Heading Defaults

• iiiiSQLSQLSQLSQL****Plus:Plus:Plus:Plus:– Default heading alignment: CenterDefault heading alignment: CenterDefault heading alignment: CenterDefault heading alignment: Center– Default heading display: UppercaseDefault heading display: UppercaseDefault heading display: UppercaseDefault heading display: Uppercase

• SQLSQLSQLSQL****Plus:Plus:Plus:Plus:– Character and Date column headings are Character and Date column headings are Character and Date column headings are Character and Date column headings are Left- aligned Left- aligned Left- aligned Left- aligned– Number column headings are right-alignedNumber column headings are right-alignedNumber column headings are right-alignedNumber column headings are right-aligned– Default heading display: UppercaseDefault heading display: UppercaseDefault heading display: UppercaseDefault heading display: Uppercase

Page 11: Lesson01 学会使用基本的SQL语句

1-11 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Arithmetic ExpressionsArithmetic ExpressionsArithmetic ExpressionsArithmetic Expressions

Create expressions with number and date data by Create expressions with number and date data by Create expressions with number and date data by Create expressions with number and date data by using arithmetic operators.using arithmetic operators.using arithmetic operators.using arithmetic operators.

MultiplyMultiplyMultiplyMultiply****DivideDivideDivideDivide////

SubtractSubtractSubtractSubtract----AddAddAddAdd++++DescriptionDescriptionDescriptionDescriptionOperatorOperatorOperatorOperator

Page 12: Lesson01 学会使用基本的SQL语句

1-12 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

SELECT last_name, salary, salary + 300SELECT last_name, salary, salary + 300SELECT last_name, salary, salary + 300SELECT last_name, salary, salary + 300FROM employees;FROM employees;FROM employees;FROM employees;

Using Arithmetic OperatorsUsing Arithmetic OperatorsUsing Arithmetic OperatorsUsing Arithmetic Operators

…………

Page 13: Lesson01 学会使用基本的SQL语句

1-13 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

SELECT last_name, salary, 12SELECT last_name, salary, 12SELECT last_name, salary, 12SELECT last_name, salary, 12****salary+100salary+100salary+100salary+100FROM employees;FROM employees;FROM employees;FROM employees;

Operator PrecedenceOperator PrecedenceOperator PrecedenceOperator Precedence

SELECT last_name, salary, 12SELECT last_name, salary, 12SELECT last_name, salary, 12SELECT last_name, salary, 12****(salary+100)(salary+100)(salary+100)(salary+100)FROM employees;FROM employees;FROM employees;FROM employees;

…………

…………

1111

2222

Page 14: Lesson01 学会使用基本的SQL语句

1-14 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Defining a Null ValueDefining a Null ValueDefining a Null ValueDefining a Null Value

• A null is a value that is unavailable, unassigned, A null is a value that is unavailable, unassigned, A null is a value that is unavailable, unassigned, A null is a value that is unavailable, unassigned, unknown, or inapplicable.unknown, or inapplicable.unknown, or inapplicable.unknown, or inapplicable.

• A null is not the same as a zero or a blank space.A null is not the same as a zero or a blank space.A null is not the same as a zero or a blank space.A null is not the same as a zero or a blank space.

SELECT last_name, job_id, salary, commission_pctSELECT last_name, job_id, salary, commission_pctSELECT last_name, job_id, salary, commission_pctSELECT last_name, job_id, salary, commission_pctFROM employees;FROM employees;FROM employees;FROM employees;

…………

…………

Page 15: Lesson01 学会使用基本的SQL语句

1-15 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

SELECT last_name, 12SELECT last_name, 12SELECT last_name, 12SELECT last_name, 12****salary,salary,salary,salary,12121212****salarysalarysalarysalary****(1+commission_pct)(1+commission_pct)(1+commission_pct)(1+commission_pct)FROM employees;FROM employees;FROM employees;FROM employees;

Null Values Null Values Null Values Null Values in Arithmetic Expressionsin Arithmetic Expressionsin Arithmetic Expressionsin Arithmetic Expressions

Arithmetic expressions containing a null value Arithmetic expressions containing a null value Arithmetic expressions containing a null value Arithmetic expressions containing a null value evaluate to null.evaluate to null.evaluate to null.evaluate to null.

…………

…………

Page 16: Lesson01 学会使用基本的SQL语句

1-16 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Defining a Column AliasDefining a Column AliasDefining a Column AliasDefining a Column Alias

A column alias:A column alias:A column alias:A column alias:• Renames a column headingRenames a column headingRenames a column headingRenames a column heading• Is useful with calculationsIs useful with calculationsIs useful with calculationsIs useful with calculations• Immediately follows the column name (There can Immediately follows the column name (There can Immediately follows the column name (There can Immediately follows the column name (There can

also be the optional also be the optional also be the optional also be the optional ASASASAS keyword between the keyword between the keyword between the keyword between the column name and alias.)column name and alias.)column name and alias.)column name and alias.)

• Requires double quotation marks if it contains Requires double quotation marks if it contains Requires double quotation marks if it contains Requires double quotation marks if it contains spaces or special characters or if it is case-spaces or special characters or if it is case-spaces or special characters or if it is case-spaces or special characters or if it is case-sensitivesensitivesensitivesensitive

• Can not be used in Where clauseCan not be used in Where clauseCan not be used in Where clauseCan not be used in Where clause

Page 17: Lesson01 学会使用基本的SQL语句

1-17 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Using Column AliasesUsing Column AliasesUsing Column AliasesUsing Column Aliases

SELECT last_name "Name" , salarySELECT last_name "Name" , salarySELECT last_name "Name" , salarySELECT last_name "Name" , salary****12 "Annual Salary"12 "Annual Salary"12 "Annual Salary"12 "Annual Salary"FROM employees;FROM employees;FROM employees;FROM employees;

SELECT last_name AS name, commission_pct commSELECT last_name AS name, commission_pct commSELECT last_name AS name, commission_pct commSELECT last_name AS name, commission_pct commFROM employees;FROM employees;FROM employees;FROM employees;

…………

…………

Page 18: Lesson01 学会使用基本的SQL语句

1-18 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Concatenation OperatorConcatenation OperatorConcatenation OperatorConcatenation Operator

A concatenation operator:A concatenation operator:A concatenation operator:A concatenation operator:• Links columns or character strings to other Links columns or character strings to other Links columns or character strings to other Links columns or character strings to other

columns columns columns columns • Is represented by two vertical bars (||)Is represented by two vertical bars (||)Is represented by two vertical bars (||)Is represented by two vertical bars (||)• Creates a resultant column that is a character Creates a resultant column that is a character Creates a resultant column that is a character Creates a resultant column that is a character

expressionexpressionexpressionexpressionSELECT last_name||job_id AS "Employees"SELECT last_name||job_id AS "Employees"SELECT last_name||job_id AS "Employees"SELECT last_name||job_id AS "Employees"FROM FROM FROM FROM employees;employees;employees;employees;

…………

Page 19: Lesson01 学会使用基本的SQL语句

1-19 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Concatenation OperatorConcatenation OperatorConcatenation OperatorConcatenation Operator

SELECT last_name||chr(39)||'s'||salary SELECT last_name||chr(39)||'s'||salary SELECT last_name||chr(39)||'s'||salary SELECT last_name||chr(39)||'s'||salary FROM FROM FROM FROM employees;employees;employees;employees;

SELECT last_name||'''s'||salary SELECT last_name||'''s'||salary SELECT last_name||'''s'||salary SELECT last_name||'''s'||salary FROM FROM FROM FROM employees;employees;employees;employees;

SELECT ASCII('''') SELECT ASCII('''') SELECT ASCII('''') SELECT ASCII('''') FROM FROM FROM FROM dual;dual;dual;dual;

SELECT ASCII('?') SELECT ASCII('?') SELECT ASCII('?') SELECT ASCII('?') FROM FROM FROM FROM dual;dual;dual;dual;

Page 20: Lesson01 学会使用基本的SQL语句

1-20 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Literal Character StringsLiteral Character StringsLiteral Character StringsLiteral Character Strings

• A literal is a character, a number, or a date that is A literal is a character, a number, or a date that is A literal is a character, a number, or a date that is A literal is a character, a number, or a date that is included in the included in the included in the included in the SELECTSELECTSELECTSELECT statement. statement. statement. statement.

• Date and character literal values must be enclosed Date and character literal values must be enclosed Date and character literal values must be enclosed Date and character literal values must be enclosed by single quotation marks.by single quotation marks.by single quotation marks.by single quotation marks.

• Each character string is output once for eachEach character string is output once for eachEach character string is output once for eachEach character string is output once for eachrow returned.row returned.row returned.row returned.

Page 21: Lesson01 学会使用基本的SQL语句

1-21 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Using Literal Character StringsUsing Literal Character StringsUsing Literal Character StringsUsing Literal Character Strings

…………

SELECT last_name ||' is a ' ||job_id SELECT last_name ||' is a ' ||job_id SELECT last_name ||' is a ' ||job_id SELECT last_name ||' is a ' ||job_id AS "Employee Details" AS "Employee Details" AS "Employee Details" AS "Employee Details"FROM employees;FROM employees;FROM employees;FROM employees;

Page 22: Lesson01 学会使用基本的SQL语句

1-22 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Duplicate RowsDuplicate RowsDuplicate RowsDuplicate Rows

The default display of queries is all rows, including The default display of queries is all rows, including The default display of queries is all rows, including The default display of queries is all rows, including duplicate rows.duplicate rows.duplicate rows.duplicate rows.SELECT department_idSELECT department_idSELECT department_idSELECT department_idFROM employees;FROM employees;FROM employees;FROM employees;

…………

SELECT DISTINCT department_idSELECT DISTINCT department_idSELECT DISTINCT department_idSELECT DISTINCT department_idFROM employees;FROM employees;FROM employees;FROM employees;

…………

1111

2222

Page 23: Lesson01 学会使用基本的SQL语句

1-23 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

SQL and SQL and SQL and SQL and iiiiSQLSQLSQLSQL****Plus InteractionPlus InteractionPlus InteractionPlus Interaction

SQL statementsSQL statementsSQL statementsSQL statements

Query resultsQuery resultsQuery resultsQuery resultsiiiiSQLSQLSQLSQL****PlusPlusPlusPlus commands commands commands commands

ClientClientClientClientFormatted reportFormatted reportFormatted reportFormatted report

Internet Internet Internet Internet browserbrowserbrowserbrowser

OracleOracleOracleOracleserverserverserverserver

Page 24: Lesson01 学会使用基本的SQL语句

1-24 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

SQL Statements Versus SQL Statements Versus SQL Statements Versus SQL Statements Versus iiiiSQLSQLSQLSQL****Plus Commands Plus Commands Plus Commands Plus Commands

SQLSQLSQLSQLstatementsstatementsstatementsstatements

SQL SQL SQL SQL • A languageA languageA languageA language• ANSI standardANSI standardANSI standardANSI standard• Keyword cannot be Keyword cannot be Keyword cannot be Keyword cannot be

abbreviatedabbreviatedabbreviatedabbreviated• Statements manipulate Statements manipulate Statements manipulate Statements manipulate

data and table definitions data and table definitions data and table definitions data and table definitions in the databasein the databasein the databasein the database

iiiiSQLSQLSQLSQL****PlusPlusPlusPlus• An environmentAn environmentAn environmentAn environment• Oracle-proprietaryOracle-proprietaryOracle-proprietaryOracle-proprietary• Keywords can be Keywords can be Keywords can be Keywords can be

abbreviatedabbreviatedabbreviatedabbreviated• Commands do not allow Commands do not allow Commands do not allow Commands do not allow

manipulation of values in manipulation of values in manipulation of values in manipulation of values in the databasethe databasethe databasethe database

• Runs on a browserRuns on a browserRuns on a browserRuns on a browser• Centrally loaded; does not Centrally loaded; does not Centrally loaded; does not Centrally loaded; does not

have to be implemented have to be implemented have to be implemented have to be implemented on each machineon each machineon each machineon each machine

iiiiSQLSQLSQLSQL****PlusPlusPlusPluscommandscommandscommandscommands

Page 25: Lesson01 学会使用基本的SQL语句

1-25 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Overview of Overview of Overview of Overview of iiiiSQLSQLSQLSQL****PlusPlusPlusPlus

After you log in to After you log in to After you log in to After you log in to iiiiSQLSQLSQLSQL****Plus, you can:Plus, you can:Plus, you can:Plus, you can:• Describe table structuresDescribe table structuresDescribe table structuresDescribe table structures• Enter, execute, and edit SQL statementsEnter, execute, and edit SQL statementsEnter, execute, and edit SQL statementsEnter, execute, and edit SQL statements• Save or append SQL statements to files Save or append SQL statements to files Save or append SQL statements to files Save or append SQL statements to files • Execute or edit statements that are stored in Execute or edit statements that are stored in Execute or edit statements that are stored in Execute or edit statements that are stored in

saved script files saved script files saved script files saved script files

Page 26: Lesson01 学会使用基本的SQL语句

1-26 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Logging In to Logging In to Logging In to Logging In to iiiiSQLSQLSQLSQL****PlusPlusPlusPlus

From your browser environment: From your browser environment: From your browser environment: From your browser environment:

Page 27: Lesson01 学会使用基本的SQL语句

1-27 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

iiiiSQLSQLSQLSQL****Plus EnvironmentPlus EnvironmentPlus EnvironmentPlus Environment

6666

3333 4444 5555

1111

2222

8888 9999

7777

Page 28: Lesson01 学会使用基本的SQL语句

1-28 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Displaying Table StructureDisplaying Table StructureDisplaying Table StructureDisplaying Table Structure

Use the Use the Use the Use the iiiiSQLSQLSQLSQL****Plus Plus Plus Plus DESCRIBEDESCRIBEDESCRIBEDESCRIBE command to display the command to display the command to display the command to display the structure of a table:structure of a table:structure of a table:structure of a table:

DESC[RIBE] DESC[RIBE] DESC[RIBE] DESC[RIBE] tablenametablenametablenametablename

Page 29: Lesson01 学会使用基本的SQL语句

1-29 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Displaying Table StructureDisplaying Table StructureDisplaying Table StructureDisplaying Table Structure

DESCRIBE employeesDESCRIBE employeesDESCRIBE employeesDESCRIBE employees

Page 30: Lesson01 学会使用基本的SQL语句

1-30 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Interacting with Script FilesInteracting with Script FilesInteracting with Script FilesInteracting with Script Files

SELECT last_name, hire_date, salarySELECT last_name, hire_date, salarySELECT last_name, hire_date, salarySELECT last_name, hire_date, salaryFROM employees;FROM employees;FROM employees;FROM employees; 1111

2222

Page 31: Lesson01 学会使用基本的SQL语句

1-31 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Interacting with Script FilesInteracting with Script FilesInteracting with Script FilesInteracting with Script Files

Page 32: Lesson01 学会使用基本的SQL语句

1-32 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Interacting with Script FilesInteracting with Script FilesInteracting with Script FilesInteracting with Script Files

1111

Page 33: Lesson01 学会使用基本的SQL语句

1-33 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Interacting with Script FilesInteracting with Script FilesInteracting with Script FilesInteracting with Script Files

22223333

D:\TEMP\emp_data.sqlD:\TEMP\emp_data.sqlD:\TEMP\emp_data.sqlD:\TEMP\emp_data.sql

Page 34: Lesson01 学会使用基本的SQL语句

1-34 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

iiiiSQLSQLSQLSQL****Plus History PagePlus History PagePlus History PagePlus History Page

1111

2222

3333

Page 35: Lesson01 学会使用基本的SQL语句

1-35 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

iiiiSQLSQLSQLSQL****Plus History PagePlus History PagePlus History PagePlus History Page

3333

4444

Page 36: Lesson01 学会使用基本的SQL语句

1-36 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Setting Setting Setting Setting iiiiSQLSQLSQLSQL****Plus PreferencesPlus PreferencesPlus PreferencesPlus Preferences

22223333

1111

Page 37: Lesson01 学会使用基本的SQL语句

1-37 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Setting the Output Location PreferenceSetting the Output Location PreferenceSetting the Output Location PreferenceSetting the Output Location Preference

1111

2222

Page 38: Lesson01 学会使用基本的SQL语句

1-38 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

SummarySummarySummarySummary

In this lesson, you should have learned how to: In this lesson, you should have learned how to: In this lesson, you should have learned how to: In this lesson, you should have learned how to: • Write a Write a Write a Write a SELECTSELECTSELECTSELECT statement that: statement that: statement that: statement that:

– Returns all rows and columns from a tableReturns all rows and columns from a tableReturns all rows and columns from a tableReturns all rows and columns from a table– Returns specified columns from a tableReturns specified columns from a tableReturns specified columns from a tableReturns specified columns from a table– Uses column aliases to display more descriptive Uses column aliases to display more descriptive Uses column aliases to display more descriptive Uses column aliases to display more descriptive

column headingscolumn headingscolumn headingscolumn headings• Use the Use the Use the Use the iiiiSQLSQLSQLSQL****Plus environment to write, save, and Plus environment to write, save, and Plus environment to write, save, and Plus environment to write, save, and

execute SQL statements and execute SQL statements and execute SQL statements and execute SQL statements and iiiiSQLSQLSQLSQL****Plus Plus Plus Plus commandscommandscommandscommands

SELECT SELECT SELECT SELECT ****|{[DISTINCT] |{[DISTINCT] |{[DISTINCT] |{[DISTINCT] column|expressioncolumn|expressioncolumn|expressioncolumn|expression [ [ [ [aliasaliasaliasalias],...}],...}],...}],...}FROM FROM FROM FROM table;table;table;table;

Page 39: Lesson01 学会使用基本的SQL语句

1-39 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice 1: OverviewPractice 1: OverviewPractice 1: OverviewPractice 1: Overview

This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:• Selecting all data from different tablesSelecting all data from different tablesSelecting all data from different tablesSelecting all data from different tables• Describing the structure of tablesDescribing the structure of tablesDescribing the structure of tablesDescribing the structure of tables• Performing arithmetic calculations and specifying Performing arithmetic calculations and specifying Performing arithmetic calculations and specifying Performing arithmetic calculations and specifying

column namescolumn namescolumn namescolumn names• Using Using Using Using iiiiSQLSQLSQLSQL****PlusPlusPlusPlus

Page 40: Lesson01 学会使用基本的SQL语句

1-40 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice Practice Practice Practice 1111

QQQQ1111: A SELECT statement can be used to perform these : A SELECT statement can be used to perform these : A SELECT statement can be used to perform these : A SELECT statement can be used to perform these three functions:three functions:three functions:three functions:1. Choose rows from a table.1. Choose rows from a table.1. Choose rows from a table.1. Choose rows from a table.2. Choose columns from a table.2. Choose columns from a table.2. Choose columns from a table.2. Choose columns from a table.3. Bring together data that is stored in different tables by 3. Bring together data that is stored in different tables by 3. Bring together data that is stored in different tables by 3. Bring together data that is stored in different tables by creating a link between them.creating a link between them.creating a link between them.creating a link between them.Which set of keywords describes these capabilities?Which set of keywords describes these capabilities?Which set of keywords describes these capabilities?Which set of keywords describes these capabilities?A. difference, projection, joinA. difference, projection, joinA. difference, projection, joinA. difference, projection, joinB. selection, projection, joinB. selection, projection, joinB. selection, projection, joinB. selection, projection, joinC. selection, intersection, joinC. selection, intersection, joinC. selection, intersection, joinC. selection, intersection, joinD. intersection, projection, joinD. intersection, projection, joinD. intersection, projection, joinD. intersection, projection, joinE. difference, projection, productE. difference, projection, productE. difference, projection, productE. difference, projection, product

Page 41: Lesson01 学会使用基本的SQL语句

1-41 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice Practice Practice Practice 1111

QQQQ2222: Evaluate this SQL statement: : Evaluate this SQL statement: : Evaluate this SQL statement: : Evaluate this SQL statement:

SELECT e.EMPLOYEE_ID,e.LAST_NAME,SELECT e.EMPLOYEE_ID,e.LAST_NAME,SELECT e.EMPLOYEE_ID,e.LAST_NAME,SELECT e.EMPLOYEE_ID,e.LAST_NAME,e.DEPARTMENT_ID, d.DEPARTMENT_NAME e.DEPARTMENT_ID, d.DEPARTMENT_NAME e.DEPARTMENT_ID, d.DEPARTMENT_NAME e.DEPARTMENT_ID, d.DEPARTMENT_NAME FROM EMPLOYEES e, DEPARTMENTS d FROM EMPLOYEES e, DEPARTMENTS d FROM EMPLOYEES e, DEPARTMENTS d FROM EMPLOYEES e, DEPARTMENTS d WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID; WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID; WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID; WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID;

In the statement, which capabilities of a SELECT statement are In the statement, which capabilities of a SELECT statement are In the statement, which capabilities of a SELECT statement are In the statement, which capabilities of a SELECT statement are performed? performed? performed? performed? AAAA. selection, projection, join . selection, projection, join . selection, projection, join . selection, projection, join B. difference, projection, join B. difference, projection, join B. difference, projection, join B. difference, projection, join C. selection, intersection, join C. selection, intersection, join C. selection, intersection, join C. selection, intersection, join D. intersection, projection, join D. intersection, projection, join D. intersection, projection, join D. intersection, projection, join E. difference, projection, productE. difference, projection, productE. difference, projection, productE. difference, projection, product

Page 42: Lesson01 学会使用基本的SQL语句

1-42 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice Practice Practice Practice 1111

QQQQ3333: Evaluate this SQL statement: : Evaluate this SQL statement: : Evaluate this SQL statement: : Evaluate this SQL statement: SELECT ename, sal, 12 SELECT ename, sal, 12 SELECT ename, sal, 12 SELECT ename, sal, 12****sal+100 sal+100 sal+100 sal+100 FROM emp; FROM emp; FROM emp; FROM emp; The SAL column stores the monthly salary of the employee. The SAL column stores the monthly salary of the employee. The SAL column stores the monthly salary of the employee. The SAL column stores the monthly salary of the employee. Which change must be made to the above Which change must be made to the above Which change must be made to the above Which change must be made to the above syntax to calculate syntax to calculate syntax to calculate syntax to calculate the annual compensation as "monthly salary plus a monthly the annual compensation as "monthly salary plus a monthly the annual compensation as "monthly salary plus a monthly the annual compensation as "monthly salary plus a monthly bonus of $100, multiplied by 12"? bonus of $100, multiplied by 12"? bonus of $100, multiplied by 12"? bonus of $100, multiplied by 12"? A. No change is required to achieve the desired results. A. No change is required to achieve the desired results. A. No change is required to achieve the desired results. A. No change is required to achieve the desired results. BBBB. SELECT ename, sal, 12. SELECT ename, sal, 12. SELECT ename, sal, 12. SELECT ename, sal, 12****(sal+100) FROM emp; (sal+100) FROM emp; (sal+100) FROM emp; (sal+100) FROM emp; C. SELECT ename, sal, (12C. SELECT ename, sal, (12C. SELECT ename, sal, (12C. SELECT ename, sal, (12****sal)+100 FROM emp; sal)+100 FROM emp; sal)+100 FROM emp; sal)+100 FROM emp; D. SELECT ename, sal+100,D. SELECT ename, sal+100,D. SELECT ename, sal+100,D. SELECT ename, sal+100,****12 FROM emp; 12 FROM emp; 12 FROM emp; 12 FROM emp;

Page 43: Lesson01 学会使用基本的SQL语句

1-43 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice Practice Practice Practice 1111

QQQQ4444: : : : Which SQL statement generates the alias Annual Which SQL statement generates the alias Annual Which SQL statement generates the alias Annual Which SQL statement generates the alias Annual Salary for the calculated column SALARYSalary for the calculated column SALARYSalary for the calculated column SALARYSalary for the calculated column SALARY****12? 12? 12? 12? A. SELECT ename, salaryA. SELECT ename, salaryA. SELECT ename, salaryA. SELECT ename, salary****12 'Annual Salary' 12 'Annual Salary' 12 'Annual Salary' 12 'Annual Salary' FROM employees; FROM employees; FROM employees; FROM employees; B. SELECT ename, salaryB. SELECT ename, salaryB. SELECT ename, salaryB. SELECT ename, salary****12 "Annual Salary" 12 "Annual Salary" 12 "Annual Salary" 12 "Annual Salary" FROM employees; FROM employees; FROM employees; FROM employees; C. SELECT ename, salaryC. SELECT ename, salaryC. SELECT ename, salaryC. SELECT ename, salary****12 AS Annual Salary 12 AS Annual Salary 12 AS Annual Salary 12 AS Annual Salary FROM employees; FROM employees; FROM employees; FROM employees; D. SELECT ename, salaryD. SELECT ename, salaryD. SELECT ename, salaryD. SELECT ename, salary****12 AS INITCAP("ANNUAL 12 AS INITCAP("ANNUAL 12 AS INITCAP("ANNUAL 12 AS INITCAP("ANNUAL SALARY") SALARY") SALARY") SALARY") FROM employees FROM employees FROM employees FROM employees

Page 44: Lesson01 学会使用基本的SQL语句

1-44 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice Practice Practice Practice 1111

QQQQ5555: : : : The CUSTOMERS table has these columns: The CUSTOMERS table has these columns: The CUSTOMERS table has these columns: The CUSTOMERS table has these columns: CUSTOMER_ID NUMBER(4) NOT NULL CUSTOMER_ID NUMBER(4) NOT NULL CUSTOMER_ID NUMBER(4) NOT NULL CUSTOMER_ID NUMBER(4) NOT NULL CUSTOMER_NAME VARCHAR2(100) NOT NULL CUSTOMER_NAME VARCHAR2(100) NOT NULL CUSTOMER_NAME VARCHAR2(100) NOT NULL CUSTOMER_NAME VARCHAR2(100) NOT NULL CUSTOMER_ADDRESS VARCHAR2(150) CUSTOMER_ADDRESS VARCHAR2(150) CUSTOMER_ADDRESS VARCHAR2(150) CUSTOMER_ADDRESS VARCHAR2(150) CUSTOMER_PHONE VARCHAR2(20) CUSTOMER_PHONE VARCHAR2(20) CUSTOMER_PHONE VARCHAR2(20) CUSTOMER_PHONE VARCHAR2(20) You need to produce output that states "Dear Customer You need to produce output that states "Dear Customer You need to produce output that states "Dear Customer You need to produce output that states "Dear Customer customer_name, ". customer_name, ". customer_name, ". customer_name, ". The customer_name data values come from the The customer_name data values come from the The customer_name data values come from the The customer_name data values come from the CUSTOMER_NAME column in the CUSTOMERS table. CUSTOMER_NAME column in the CUSTOMERS table. CUSTOMER_NAME column in the CUSTOMERS table. CUSTOMER_NAME column in the CUSTOMERS table. Which statement produces this output? Which statement produces this output? Which statement produces this output? Which statement produces this output?

Page 45: Lesson01 学会使用基本的SQL语句

1-45 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice Practice Practice Practice 1111

A. SELECT dear customer, customer_name, A. SELECT dear customer, customer_name, A. SELECT dear customer, customer_name, A. SELECT dear customer, customer_name, FROM customers; FROM customers; FROM customers; FROM customers; B. SELECT "Dear Customer", customer_name || ',' B. SELECT "Dear Customer", customer_name || ',' B. SELECT "Dear Customer", customer_name || ',' B. SELECT "Dear Customer", customer_name || ',' FROM customers; FROM customers; FROM customers; FROM customers; C. SELECT 'Dear Customer ' || customer_name ',' C. SELECT 'Dear Customer ' || customer_name ',' C. SELECT 'Dear Customer ' || customer_name ',' C. SELECT 'Dear Customer ' || customer_name ',' FROM customers; FROM customers; FROM customers; FROM customers; DDDD. SELECT 'Dear Customer ' || customer_name || ',' . SELECT 'Dear Customer ' || customer_name || ',' . SELECT 'Dear Customer ' || customer_name || ',' . SELECT 'Dear Customer ' || customer_name || ',' FROM customers; FROM customers; FROM customers; FROM customers; E. SELECT "Dear Customer " || customer_name || "," E. SELECT "Dear Customer " || customer_name || "," E. SELECT "Dear Customer " || customer_name || "," E. SELECT "Dear Customer " || customer_name || "," FROM customers; FROM customers; FROM customers; FROM customers; F. SELECT 'Dear Customer ' || customer_name || ',' || F. SELECT 'Dear Customer ' || customer_name || ',' || F. SELECT 'Dear Customer ' || customer_name || ',' || F. SELECT 'Dear Customer ' || customer_name || ',' || FROM customers; FROM customers; FROM customers; FROM customers;

Page 46: Lesson01 学会使用基本的SQL语句

1-46 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

Practice Practice Practice Practice 1111

QQQQ6666: Which two are attributes of iSQL: Which two are attributes of iSQL: Which two are attributes of iSQL: Which two are attributes of iSQL****Plus? (Choose two.) Plus? (Choose two.) Plus? (Choose two.) Plus? (Choose two.) A. iSQLA. iSQLA. iSQLA. iSQL****Plus commands cannot be abbreviated. Plus commands cannot be abbreviated. Plus commands cannot be abbreviated. Plus commands cannot be abbreviated. BBBB. iSQL. iSQL. iSQL. iSQL****Plus commands are accessed from a browser. Plus commands are accessed from a browser. Plus commands are accessed from a browser. Plus commands are accessed from a browser. C. iSQLC. iSQLC. iSQLC. iSQL****Plus commands are used to manipulate data in Plus commands are used to manipulate data in Plus commands are used to manipulate data in Plus commands are used to manipulate data in tables. tables. tables. tables. D. iSQLD. iSQLD. iSQLD. iSQL****Plus commands manipulate table definitions in Plus commands manipulate table definitions in Plus commands manipulate table definitions in Plus commands manipulate table definitions in the database. the database. the database. the database. EEEE. iSQL. iSQL. iSQL. iSQL****Plus is the Oracle proprietary interface for Plus is the Oracle proprietary interface for Plus is the Oracle proprietary interface for Plus is the Oracle proprietary interface for executing SQL statements. executing SQL statements. executing SQL statements. executing SQL statements.