SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Downloaden Sie, um offline zu lesen
Oracle OCP 考试系列培训
                之
         1Z0-007 Lesson1
                  www.OracleOnLinux.cn




1-1   Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
1
                 Retrieving Data Using
              the SQL SELECT Statement




1-2   Copyright © 2011,   www.OracleOnLinux . Part rights reserved.
                              OracleOnLinux
                              OracleOnLinux.cn
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.
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.
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.
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.
Selecting All Columns


      SELECT *
      FROM   departments;




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


      SELECT department_id, location_id
      FROM   departments;




1-8        Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
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.
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.
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.
Using Arithmetic Operators


       SELECT last_name, salary, salary + 300
       FROM   employees;




       …




1-12        Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Logging In to iSQL*Plus
                                   SQL*


       From your browser environment:




1-26        Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
iSQL*Plus Environment
                     SQL*
                                                                   8        9


                                                   7




1
                                                                            6

       2        3           4        5



1-27       Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
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.
Displaying Table Structure


       DESCRIBE employees




1-29        Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
Interacting with Script Files




       SELECT last_name, hire_date, salary
       FROM   employees;                                   1


                           2



1-30       Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
Interacting with Script Files




1-31   Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
Interacting with Script Files




             1



1-32   Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
Interacting with Script Files




       D:TEMPemp_data.sql



       2
                                                                            3


1-33       Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
iSQL*Plus History Page
                SQL*

                                                                        3




                                                                   2




  1



1-34   Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
iSQL*Plus History Page
                    SQL*




                                                  3




       4



1-35       Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
Setting iSQL*Plus Preferences
                      SQL*




                                                                   1




       2
                                                                            3




1-36       Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
Setting the Output Location Preference
                                                                      2




                                                            1



1-37     Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
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.
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.
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.
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.
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.
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.
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.
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.
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.

Weitere ähnliche Inhalte

Was ist angesagt?

Lesson10
Lesson10Lesson10
Lesson10
renguzi
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
Understanding
Understanding Understanding
Understanding
Arun Gupta
 
Php 5 3-study_guide_v1a
Php 5 3-study_guide_v1aPhp 5 3-study_guide_v1a
Php 5 3-study_guide_v1a
luckysher007
 

Was ist angesagt? (20)

SQL SELECT Statement
 SQL SELECT Statement SQL SELECT Statement
SQL SELECT Statement
 
Lesson04 学会使用分组函数
Lesson04 学会使用分组函数Lesson04 学会使用分组函数
Lesson04 学会使用分组函数
 
Les10
Les10Les10
Les10
 
Les09
Les09Les09
Les09
 
Les01
Les01Les01
Les01
 
Les02
Les02Les02
Les02
 
Lesson06 使用子查询
Lesson06 使用子查询Lesson06 使用子查询
Lesson06 使用子查询
 
Les06
Les06Les06
Les06
 
Lesson10
Lesson10Lesson10
Lesson10
 
Lesson05 从多表中查询数据
Lesson05 从多表中查询数据Lesson05 从多表中查询数据
Lesson05 从多表中查询数据
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Understanding
Understanding Understanding
Understanding
 
Android de la A a la Z XML Ulises Gonzalez
Android de la A a la Z  XML Ulises GonzalezAndroid de la A a la Z  XML Ulises Gonzalez
Android de la A a la Z XML Ulises Gonzalez
 
Les04
Les04Les04
Les04
 
Dare to build vertical design with relational data (Entity-Attribute-Value)
Dare to build vertical design with relational data (Entity-Attribute-Value)Dare to build vertical design with relational data (Entity-Attribute-Value)
Dare to build vertical design with relational data (Entity-Attribute-Value)
 
Analytical Functions for DWH
Analytical Functions for DWHAnalytical Functions for DWH
Analytical Functions for DWH
 
EMF-IncQuery 0.7 Presentation for Itemis
EMF-IncQuery 0.7 Presentation for ItemisEMF-IncQuery 0.7 Presentation for Itemis
EMF-IncQuery 0.7 Presentation for Itemis
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Php 5 3-study_guide_v1a
Php 5 3-study_guide_v1aPhp 5 3-study_guide_v1a
Php 5 3-study_guide_v1a
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
 

Ähnlich wie Lesson01 学会使用基本的SQL语句

Lesson08
Lesson08Lesson08
Lesson08
renguzi
 
Lesson09
Lesson09Lesson09
Lesson09
renguzi
 
Lesson07
Lesson07Lesson07
Lesson07
renguzi
 

Ähnlich wie Lesson01 学会使用基本的SQL语句 (20)

Les01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.pptLes01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.ppt
 
Database Management Systems SQL And DDL language
Database Management Systems SQL And DDL languageDatabase Management Systems SQL And DDL language
Database Management Systems SQL And DDL language
 
SQL, consultas rapidas y sencillas, oracle
SQL, consultas rapidas y sencillas, oracleSQL, consultas rapidas y sencillas, oracle
SQL, consultas rapidas y sencillas, oracle
 
Lesson03 学会使用单行函数
Lesson03 学会使用单行函数Lesson03 学会使用单行函数
Lesson03 学会使用单行函数
 
Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句
 
plsql les02
 plsql les02 plsql les02
plsql les02
 
Lesson08
Lesson08Lesson08
Lesson08
 
Introduction of Oracle
Introduction of Oracle Introduction of Oracle
Introduction of Oracle
 
Oracle Baisc Tutorial
Oracle Baisc TutorialOracle Baisc Tutorial
Oracle Baisc Tutorial
 
Lesson09
Lesson09Lesson09
Lesson09
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
War of the Indices- SQL vs. Oracle
War of the Indices-  SQL vs. OracleWar of the Indices-  SQL vs. Oracle
War of the Indices- SQL vs. Oracle
 
plsql les01
 plsql les01 plsql les01
plsql les01
 
Introduction to Oracle SQL Database Systems.ppt
Introduction to Oracle SQL Database Systems.pptIntroduction to Oracle SQL Database Systems.ppt
Introduction to Oracle SQL Database Systems.ppt
 
Lesson07
Lesson07Lesson07
Lesson07
 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data Base
 
plsql Les08
plsql Les08 plsql Les08
plsql Les08
 
Les08
Les08Les08
Les08
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

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.
  • 29. Displaying Table Structure DESCRIBE employees 1-29 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.
  • 31. Interacting with Script Files 1-31 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
  • 32. Interacting with Script Files 1 1-32 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.
  • 34. iSQL*Plus History Page SQL* 3 2 1 1-34 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
  • 35. iSQL*Plus History Page SQL* 3 4 1-35 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
  • 36. Setting iSQL*Plus Preferences SQL* 1 2 3 1-36 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.