SlideShare uma empresa Scribd logo
1 de 49
Baixar para ler offline
Oracle OCP 考试系列培训
                 之
          1Z0-007 Lesson10
                    www.OracleOnLinux.cn




10-1   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
10
Creating Other Schema Objects




Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Objectives


       After completing this lesson, you should be able to do
       the following:
        • Create simple and complex views
        • Retrieve data from views
        • Create, maintain, and use sequences
        • Create and maintain indexes
        • Create private and public synonyms




10-3          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Database Objects


       Object            Description

       Table             Basic unit of storage; composed of rows
       View              Logically represents subsets of data from
                         one or more tables
       Sequence          Generates numeric values
       Index             Improves the performance of some
                         queries
       Synonym           Gives alternative names to objects




10-4      Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
What Is a View?
       EMPLOYEES table




10-5     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Advantages of Views


        To restrict                                            To make complex
       data access                                               queries easy




         To provide                                                 To present
            data                                                different views of
       independence                                               the same data




10-6          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Simple Views and Complex Views


       Feature                         Simple Views            Complex Views
       Number of tables                One                     One or more
       Contain functions               No                      Yes
       Contain groups of data          No                      Yes
       DML operations                  Yes                     Not always
       through a view




10-7             Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating a View


       •   You embed a subquery in the CREATE VIEW
           statement:
       CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
                           [FORCE|NOFORCE
                                  NOFORCE]
         [(alias[, alias]...)]
        AS subquery
       [WITH CHECK OPTION [CONSTRAINT constraint]]
       [WITH READ ONLY [CONSTRAINT constraint]];

       •   The subquery can contain complex SELECT syntax.




10-8         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating a View


       •   Create the EMPVU80 view, which contains details
           of employees in department 80:
       CREATE VIEW empvu80
        AS SELECT employee_id, last_name, salary
           FROM    employees
           WHERE   department_id = 80;
       View created.
       •   Describe the structure of the view by using the
           iSQL*Plus DESCRIBE command:
            SQL*

       DESCRIBE empvu80




10-9         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating a View


        •   Create a view by using column aliases in the
            subquery:
        CREATE VIEW salvu50
         AS SELECT employee_id ID_NUMBER, last_name NAME,
                    salary*12 ANN_SALARY
                    salary*
            FROM    employees
            WHERE   department_id = 50;
        View created.

        •   Select the columns from this view by the given
            alias names:




10-10         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Retrieving Data from a View


        SELECT *
        FROM   salvu50;




                                                                P85 Q138


10-11         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Modifying a View


        •   Modify the EMPVU80 view by using a CREATE OR
            REPLACE VIEW clause. Add an alias for each
            column name:
        CREATE OR REPLACE VIEW empvu80
          (id_number, name, sal, department_id)
        AS SELECT employee_id, first_name || ' '
                   || last_name, salary, department_id
           FROM    employees
           WHERE   department_id = 80;
        View created.
        •   Column aliases in the CREATE OR REPLACE VIEW
            clause are listed in the same order as the columns
            in the subquery.


10-12         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating a Complex View


    Create a complex view that contains group functions
    to display values from two tables:
        CREATE OR REPLACE VIEW dept_sum_vu
          (name, minsal, maxsal, avgsal)
        AS SELECT   d.department_name, MIN(e.salary),
                    MAX(e.salary),AVG(e.salary)
           FROM     employees e JOIN departments d
           ON       (e.department_id = d.department_id)
           GROUP BY d.department_name;
        View created.




10-13         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Rules for Performing
                     DML Operations on a View

        •   You can usually perform DML operations
            on simple views.
        •   You cannot remove a row if the view contains the
            following:
            –   Group functions
            –   A GROUP BY clause
            –   The DISTINCT keyword
            –   The pseudocolumn ROWNUM keyword




10-14           Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Rules for Performing
                DML Operations on a View

    You cannot modify data in a view if it contains:
     • Group functions
     • A GROUP BY clause
     • The DISTINCT keyword
     • The pseudocolumn ROWNUM keyword
     • Columns defined by expressions




10-15      Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Rules for Performing
               DML Operations on a View

    You cannot add data through a view if the view
    includes:
     • Group functions
     • A GROUP BY clause
     • The DISTINCT keyword
     • The pseudocolumn ROWNUM keyword
     • Columns defined by expressions
     • NOT NULL columns in the base tables that are not
         selected by the view




10-16     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Using the WITH CHECK OPTION Clause


        •    You can ensure that DML operations performed on
             the view stay in the domain of the view by using
             the WITH CHECK OPTION clause:
        CREATE OR REPLACE VIEW empvu20
        AS SELECT     *
           FROM     employees
           WHERE    department_id = 20
           WITH CHECK OPTION CONSTRAINT empvu20_ck ;
        View created.

        •    Any attempt to change the department number for
             any row in the view fails because it violates the
             WITH CHECK OPTION constraint.
                                                                   P84 Q137


10-17          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Denying DML Operations


        •   You can ensure that no DML operations occur by
            adding the WITH READ ONLY option to your view
            definition.
        •   Any attempt to perform a DML operation on any
            row in the view results in an Oracle server error.




10-18         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Denying DML Operations


        CREATE OR REPLACE VIEW empvu10
            (employee_number, employee_name, job_title)
        AS SELECT     employee_id, last_name, job_id
           FROM     employees
           WHERE    department_id = 10
           WITH READ ONLY ;
        View created.




10-19         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Removing a View


    You can remove a view without losing data because a
    view is based on underlying tables in the database.
        DROP VIEW view;

        DROP VIEW empvu80;
        View dropped.




10-20         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Practice 10: Overview of Part 1


    This practice covers the following topics:
     • Creating a simple view
     • Creating a complex view
     • Creating a view with a check constraint
     • Attempting to modify data in the view
     • Removing views



                                                          P69 Q109




10-21     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Sequences


        Object            Description

        Table             Basic unit of storage; composed of rows
        View              Logically represents subsets of data from
                          one or more tables
        Sequence          Generates numeric values
        Index             Improves the performance of some
                          queries
        Synonym           Gives alternative names to objects




10-22      Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Sequences


    A sequence:
     • Can automatically generate unique numbers
     • Is a sharable object
     • Can be used to create a primary key value
     • Replaces application code
     • Speeds up the efficiency of accessing sequence
        values when cached in memory


                                    2       4       6       8       10
                                1       3       5       7       9




10-23     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
CREATE SEQUENCE Statement:
                          Syntax

    Define a sequence to generate sequential numbers
    automatically:

        CREATE SEQUENCE sequence
               [INCREMENT BY n]
               [START WITH n]
               [{MAXVALUE n | NOMAXVALUE}]
                              NOMAXVALUE}]
               [{MINVALUE n | NOMINVALUE}]
                              NOMINVALUE}]
               [{CYCLE | NOCYCLE}]
                         NOCYCLE}]
               [{CACHE n | NOCACHE}];




                                                           P87 Q145

10-24         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating a Sequence


        •   Create a sequence named DEPT_DEPTID_SEQ to
            be used for the primary key of the DEPARTMENTS
            table.
        •   Do not use the CYCLE option.
        CREATE SEQUENCE dept_deptid_seq
                        INCREMENT BY 10
                        START WITH 120
                        MAXVALUE 9999
                        NOCACHE
                        NOCYCLE;
        Sequence created.




10-25         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
NEXTVAL and CURRVAL Pseudocolumns


        •    NEXTVAL returns the next available sequence
             value. It returns a unique value every time it is
             referenced, even for different users.
        •    CURRVAL obtains the current sequence value.
        •    NEXTVAL must be issued for that sequence before
             CURRVAL contains a value.




10-26          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
10-27   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Using a Sequence


        •   Insert a new department named “Support” in
                                           Support”
            location ID 2500:
        INSERT INTO departments(department_id,
                    department_name, location_id)
        VALUES      (dept_deptid_seq.NEXTVAL,
                    'Support', 2500);
        1 row created.

        •   View the current value for the DEPT_DEPTID_SEQ
            sequence:
        SELECT      dept_deptid_seq.CURRVAL
        FROM        dual;



10-28            Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Caching Sequence Values


        •   Caching sequence values in memory gives faster
            access to those values.
        •   Gaps in sequence values can occur when:
            – A rollback occurs
            – The system crashes
            – A sequence is used in another table




10-29         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Modifying a Sequence


    Change the increment value, maximum value,
    minimum value, cycle option, or cache option:

        ALTER SEQUENCE dept_deptid_seq
                       INCREMENT BY 20
                       MAXVALUE 999999
                       NOCACHE
                       NOCYCLE;
        Sequence altered.




10-30         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Guidelines for Modifying
                           a Sequence

        •   You must be the owner or have the ALTER
            privilege for the sequence.
        •   Only future sequence numbers are affected.
        •   The sequence must be dropped and
            re-created to restart the sequence at a different
            number.
        •   Some validation is performed.
        •   To remove a sequence, use the DROP statement:
        DROP SEQUENCE dept_deptid_seq;
        Sequence dropped.




10-31         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Indexes


        Object            Description

        Table             Basic unit of storage; composed of rows
        View              Logically represents subsets of data from
                          one or more tables
        Sequence          Generates numeric values
        Index             Improves the performance of some
                          queries
        Synonym           Gives alternative names to objects




10-32      Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Indexes


    An index:
     • Is a schema object
     • Can be used by the Oracle server to speed up the
        retrieval of rows by using a pointer
     • Can reduce disk I/O by using a rapid path access
        method to locate data quickly
     • Is independent of the table that it indexes
     • Is used and maintained automatically by the
        Oracle server




10-33     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
How Are Indexes Created?


        •   Automatically: A unique index is created
            automatically when you define a PRIMARY KEY or
            UNIQUE constraint in a table definition.




        •   Manually: Users can create nonunique indexes on
            columns to speed up access to the rows.




10-34         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating an Index


        •   Create an index on one or more columns:

        CREATE INDEX index
        ON table (column[, column]...);


        •   Improve the speed of query access to the
            LAST_NAME column in the EMPLOYEES table:

        CREATE INDEX emp_last_name_idx
        ON           employees(last_name);
        Index created.




10-35         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Index Creation Guidelines

        Create an index when:
            A column contains a wide range of values
            A column contains a large number of null values
            One or more columns are frequently used together in a WHERE
            clause or a join condition
            The table is large and most queries are expected to retrieve less
            than 2% to 4% of the rows in the table
        Do not create an index when:
            The columns are not often used as a condition in the query
            The table is small or most queries are expected to retrieve more
            than 2% to 4% of the rows in the table
            The table is updated frequently
            The indexed columns are referenced as part of an expression



10-36          Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
什么时候创建索引


        如果出现下列情况,考虑创建索引:
        • 包含了大量不同值的列
        • 包含了大量空值的列
        • 一个或者多个列经常被一起出现在 WHERE
                          WHERE条件中或者
          作为连接的条件出现
        • 表的数据量很大,而且对表的查询经常是得到表中数据
          的2%到4%。




                                                           P92 Q154


10-37      Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
什么时候不要创建索引


        如果出现下列情况,不需要创建索引:
        • 一个很小的表
        • 列很少被用于查询的条件中出现
        • 表上的大多数查询是得到表中大于4%的数据
        • 表中的数据经常发生变动
        • 要被索引的列被作为条件表达式的一部分或出现在函
          数中(索引无效)




10-38      Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
查询索引信息


        •   通过USER_INDEXES 数据字典可以得到索引的定义和
              USER_INDEXES
            唯一性.
        •   通过USER_IND_COLUMNS 数据字典可以得到索引的
              USER_IND_COLUMNS
            名称,表名和列名.

        SELECT      ic.index_name, ic.column_name,
                    ic.column_position col_pos,ix.uniqueness
        FROM        user_indexes ix, user_ind_columns ic
        WHERE       ic.index_name = ix.index_name
        AND         ic.table_name = 'EMPLOYEES';




10-39           Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Removing an Index


        •   Remove an index from the data dictionary by
            using the DROP INDEX command:

        DROP INDEX index;

        •   Remove the UPPER_LAST_NAME_IDX index from
            the data dictionary:
        DROP INDEX emp_last_name_idx;
        Index dropped.
        •   To drop an index, you must be the owner of the
            index or have the DROP ANY INDEX privilege.



10-40         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Synonyms


        Object            Description

        Table             Basic unit of storage; composed of rows
        View              Logically represents subsets of data from
                          one or more tables
        Sequence          Generates numeric values
        Index             Improves the performance of some
                          queries
        Synonym           Gives alternative names to objects




10-41      Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Synonyms


    Simplify access to objects by creating a synonym
    (another name for an object). With synonyms, you can:
     • Create an easier reference to a table that is owned
        by another user
     • Shorten lengthy object names
        CREATE [PUBLIC] SYNONYM synonym
        FOR    object;




10-42         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating and Removing Synonyms


        •   Create a shortened name for the DEPT_SUM_VU
            view:
        CREATE SYNONYM d_sum
        FOR dept_sum_vu;
        Synonym Created.

        •   Drop a synonym:
        DROP SYNONYM d_sum;
        Synonym dropped.




10-43         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Creating and Removing Synonyms


        •   创建一个同义词时,基表可以不存在;
        •   创建公共同义词时,需要相关权限;
        •   公共同义词所有用户都能看到,但不一定能看到基表,取决于权
            限;
        •   私有、公共同义词同名时,用户看到的同义词默认为私有同义词;




            P63 Q100 同义词概念,P85 Q139




10-44        Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Summary


    In this lesson, you should have learned how to:
     • Create, use, and remove views
     • Automatically generate sequence numbers by
         using a sequence generator
     • Create indexes to improve query retrieval speed
     • Use synonyms to provide alternative names for
         objects




10-45     Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Practice 10: Overview of Part 2


    This practice covers the following topics:
     • Creating sequences
     • Using sequences
     • Creating nonunique indexes
     • Creating synonyms

        •   P59 Q93




10-46         Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
10-47   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
10-48   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
10-49   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Mais conteúdo relacionado

Mais procurados

Java EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureJava EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureArun Gupta
 
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...Arun Gupta
 
TDC 2011: The Java EE 7 Platform: Developing for the Cloud
TDC 2011: The Java EE 7 Platform: Developing for the CloudTDC 2011: The Java EE 7 Platform: Developing for the Cloud
TDC 2011: The Java EE 7 Platform: Developing for the CloudArun Gupta
 
Utilize the Full Power of GlassFish Server and Java EE Security
Utilize the Full Power of GlassFish Server and Java EE SecurityUtilize the Full Power of GlassFish Server and Java EE Security
Utilize the Full Power of GlassFish Server and Java EE SecurityMasoud Kalali
 
The State of Java under Oracle at JCertif 2011
The State of Java under Oracle at JCertif 2011The State of Java under Oracle at JCertif 2011
The State of Java under Oracle at JCertif 2011Arun Gupta
 
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OS
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OSWhat’s New For SQL Optimization In DB2 9 And DB2 10 For z/OS
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OSSurekha Parekh
 
Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5Vishwash Gaur
 
JEE Course - JEE Overview
JEE Course - JEE  OverviewJEE Course - JEE  Overview
JEE Course - JEE Overviewodedns
 
Whats Cool in Java E 6
Whats Cool in Java E 6Whats Cool in Java E 6
Whats Cool in Java E 6Arun Gupta
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsVirtual Nuggets
 
01.egovFrame Training Book II
01.egovFrame Training Book II01.egovFrame Training Book II
01.egovFrame Training Book IIChuong Nguyen
 
MS PowerPivot & PerformancePoint 2010 Introduction
MS PowerPivot & PerformancePoint 2010 IntroductionMS PowerPivot & PerformancePoint 2010 Introduction
MS PowerPivot & PerformancePoint 2010 IntroductionVeriPoint LLC
 
L0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget ToolkitL0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget ToolkitTonny Madsen
 
02.egovFrame Development Environment training book
02.egovFrame Development Environment training book02.egovFrame Development Environment training book
02.egovFrame Development Environment training bookChuong Nguyen
 
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Behind the Scenes at Spring Batch | Dave SyerSpring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Behind the Scenes at Spring Batch | Dave SyerJAX London
 
Java secure development part 3
Java secure development   part 3Java secure development   part 3
Java secure development part 3Rafel Ivgi
 
04.egovFrame Runtime Environment Workshop
04.egovFrame Runtime Environment Workshop04.egovFrame Runtime Environment Workshop
04.egovFrame Runtime Environment WorkshopChuong Nguyen
 

Mais procurados (20)

Java EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureJava EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for future
 
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ Silicon Val...
 
Les06
Les06Les06
Les06
 
TDC 2011: The Java EE 7 Platform: Developing for the Cloud
TDC 2011: The Java EE 7 Platform: Developing for the CloudTDC 2011: The Java EE 7 Platform: Developing for the Cloud
TDC 2011: The Java EE 7 Platform: Developing for the Cloud
 
Utilize the Full Power of GlassFish Server and Java EE Security
Utilize the Full Power of GlassFish Server and Java EE SecurityUtilize the Full Power of GlassFish Server and Java EE Security
Utilize the Full Power of GlassFish Server and Java EE Security
 
The State of Java under Oracle at JCertif 2011
The State of Java under Oracle at JCertif 2011The State of Java under Oracle at JCertif 2011
The State of Java under Oracle at JCertif 2011
 
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OS
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OSWhat’s New For SQL Optimization In DB2 9 And DB2 10 For z/OS
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OS
 
Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5
 
L0033 - JFace
L0033 - JFaceL0033 - JFace
L0033 - JFace
 
JEE Course - JEE Overview
JEE Course - JEE  OverviewJEE Course - JEE  Overview
JEE Course - JEE Overview
 
Whats Cool in Java E 6
Whats Cool in Java E 6Whats Cool in Java E 6
Whats Cool in Java E 6
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggets
 
01.egovFrame Training Book II
01.egovFrame Training Book II01.egovFrame Training Book II
01.egovFrame Training Book II
 
MS PowerPivot & PerformancePoint 2010 Introduction
MS PowerPivot & PerformancePoint 2010 IntroductionMS PowerPivot & PerformancePoint 2010 Introduction
MS PowerPivot & PerformancePoint 2010 Introduction
 
Sql9e ppt ch02
Sql9e ppt ch02 Sql9e ppt ch02
Sql9e ppt ch02
 
L0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget ToolkitL0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget Toolkit
 
02.egovFrame Development Environment training book
02.egovFrame Development Environment training book02.egovFrame Development Environment training book
02.egovFrame Development Environment training book
 
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Behind the Scenes at Spring Batch | Dave SyerSpring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
 
Java secure development part 3
Java secure development   part 3Java secure development   part 3
Java secure development part 3
 
04.egovFrame Runtime Environment Workshop
04.egovFrame Runtime Environment Workshop04.egovFrame Runtime Environment Workshop
04.egovFrame Runtime Environment Workshop
 

Destaque

Lesson05 从多表中查询数据
Lesson05 从多表中查询数据Lesson05 从多表中查询数据
Lesson05 从多表中查询数据renguzi
 
Lesson06 使用子查询
Lesson06 使用子查询Lesson06 使用子查询
Lesson06 使用子查询renguzi
 
Lesson08
Lesson08Lesson08
Lesson08renguzi
 
Lesson09
Lesson09Lesson09
Lesson09renguzi
 
Lesson07
Lesson07Lesson07
Lesson07renguzi
 
Lesson03 学会使用单行函数
Lesson03 学会使用单行函数Lesson03 学会使用单行函数
Lesson03 学会使用单行函数renguzi
 

Destaque (6)

Lesson05 从多表中查询数据
Lesson05 从多表中查询数据Lesson05 从多表中查询数据
Lesson05 从多表中查询数据
 
Lesson06 使用子查询
Lesson06 使用子查询Lesson06 使用子查询
Lesson06 使用子查询
 
Lesson08
Lesson08Lesson08
Lesson08
 
Lesson09
Lesson09Lesson09
Lesson09
 
Lesson07
Lesson07Lesson07
Lesson07
 
Lesson03 学会使用单行函数
Lesson03 学会使用单行函数Lesson03 学会使用单行函数
Lesson03 学会使用单行函数
 

Semelhante a Oracle OCP考试系列培训之1Z0-007 Lesson10数据库对象创建

Semelhante a Oracle OCP考试系列培训之1Z0-007 Lesson10数据库对象创建 (20)

Sql views
Sql viewsSql views
Sql views
 
Les11.ppt
Les11.pptLes11.ppt
Les11.ppt
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
Les10
Les10Les10
Les10
 
Creating other schema objects
Creating other schema objectsCreating other schema objects
Creating other schema objects
 
Les11
Les11Les11
Les11
 
Useful PL/SQL Supplied Packages
Useful PL/SQL Supplied PackagesUseful PL/SQL Supplied Packages
Useful PL/SQL Supplied Packages
 
Sql xp 08
Sql xp 08Sql xp 08
Sql xp 08
 
Les10
Les10Les10
Les10
 
View
ViewView
View
 
6232 b 04
6232 b 046232 b 04
6232 b 04
 
Application_Performance_V1
Application_Performance_V1Application_Performance_V1
Application_Performance_V1
 
Application_Performance_V1
Application_Performance_V1Application_Performance_V1
Application_Performance_V1
 
Application_Performance_V1
Application_Performance_V1Application_Performance_V1
Application_Performance_V1
 
plsql les02
 plsql les02 plsql les02
plsql les02
 
ZZ BC#7.5 asp.net mvc practice and guideline refresh!
ZZ BC#7.5 asp.net mvc practice  and guideline refresh! ZZ BC#7.5 asp.net mvc practice  and guideline refresh!
ZZ BC#7.5 asp.net mvc practice and guideline refresh!
 
Sightly_techInsight
Sightly_techInsightSightly_techInsight
Sightly_techInsight
 
07 qmds2005 session10
07 qmds2005 session1007 qmds2005 session10
07 qmds2005 session10
 
Chapt7.ppt
Chapt7.pptChapt7.ppt
Chapt7.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
 

Último

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 

Último (20)

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 

Oracle OCP考试系列培训之1Z0-007 Lesson10数据库对象创建

  • 1. Oracle OCP 考试系列培训 之 1Z0-007 Lesson10 www.OracleOnLinux.cn 10-1 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 2. 10 Creating Other Schema Objects Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 3. Objectives After completing this lesson, you should be able to do the following: • Create simple and complex views • Retrieve data from views • Create, maintain, and use sequences • Create and maintain indexes • Create private and public synonyms 10-3 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 4. Database Objects Object Description Table Basic unit of storage; composed of rows View Logically represents subsets of data from one or more tables Sequence Generates numeric values Index Improves the performance of some queries Synonym Gives alternative names to objects 10-4 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 5. What Is a View? EMPLOYEES table 10-5 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 6. Advantages of Views To restrict To make complex data access queries easy To provide To present data different views of independence the same data 10-6 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 7. Simple Views and Complex Views Feature Simple Views Complex Views Number of tables One One or more Contain functions No Yes Contain groups of data No Yes DML operations Yes Not always through a view 10-7 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 8. Creating a View • You embed a subquery in the CREATE VIEW statement: CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [FORCE|NOFORCE NOFORCE] [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]]; • The subquery can contain complex SELECT syntax. 10-8 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 9. Creating a View • Create the EMPVU80 view, which contains details of employees in department 80: CREATE VIEW empvu80 AS SELECT employee_id, last_name, salary FROM employees WHERE department_id = 80; View created. • Describe the structure of the view by using the iSQL*Plus DESCRIBE command: SQL* DESCRIBE empvu80 10-9 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 10. Creating a View • Create a view by using column aliases in the subquery: CREATE VIEW salvu50 AS SELECT employee_id ID_NUMBER, last_name NAME, salary*12 ANN_SALARY salary* FROM employees WHERE department_id = 50; View created. • Select the columns from this view by the given alias names: 10-10 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 11. Retrieving Data from a View SELECT * FROM salvu50; P85 Q138 10-11 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 12. Modifying a View • Modify the EMPVU80 view by using a CREATE OR REPLACE VIEW clause. Add an alias for each column name: CREATE OR REPLACE VIEW empvu80 (id_number, name, sal, department_id) AS SELECT employee_id, first_name || ' ' || last_name, salary, department_id FROM employees WHERE department_id = 80; View created. • Column aliases in the CREATE OR REPLACE VIEW clause are listed in the same order as the columns in the subquery. 10-12 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 13. Creating a Complex View Create a complex view that contains group functions to display values from two tables: CREATE OR REPLACE VIEW dept_sum_vu (name, minsal, maxsal, avgsal) AS SELECT d.department_name, MIN(e.salary), MAX(e.salary),AVG(e.salary) FROM employees e JOIN departments d ON (e.department_id = d.department_id) GROUP BY d.department_name; View created. 10-13 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 14. Rules for Performing DML Operations on a View • You can usually perform DML operations on simple views. • You cannot remove a row if the view contains the following: – Group functions – A GROUP BY clause – The DISTINCT keyword – The pseudocolumn ROWNUM keyword 10-14 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 15. Rules for Performing DML Operations on a View You cannot modify data in a view if it contains: • Group functions • A GROUP BY clause • The DISTINCT keyword • The pseudocolumn ROWNUM keyword • Columns defined by expressions 10-15 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 16. Rules for Performing DML Operations on a View You cannot add data through a view if the view includes: • Group functions • A GROUP BY clause • The DISTINCT keyword • The pseudocolumn ROWNUM keyword • Columns defined by expressions • NOT NULL columns in the base tables that are not selected by the view 10-16 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 17. Using the WITH CHECK OPTION Clause • You can ensure that DML operations performed on the view stay in the domain of the view by using the WITH CHECK OPTION clause: CREATE OR REPLACE VIEW empvu20 AS SELECT * FROM employees WHERE department_id = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck ; View created. • Any attempt to change the department number for any row in the view fails because it violates the WITH CHECK OPTION constraint. P84 Q137 10-17 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 18. Denying DML Operations • You can ensure that no DML operations occur by adding the WITH READ ONLY option to your view definition. • Any attempt to perform a DML operation on any row in the view results in an Oracle server error. 10-18 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 19. Denying DML Operations CREATE OR REPLACE VIEW empvu10 (employee_number, employee_name, job_title) AS SELECT employee_id, last_name, job_id FROM employees WHERE department_id = 10 WITH READ ONLY ; View created. 10-19 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 20. Removing a View You can remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW view; DROP VIEW empvu80; View dropped. 10-20 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 21. Practice 10: Overview of Part 1 This practice covers the following topics: • Creating a simple view • Creating a complex view • Creating a view with a check constraint • Attempting to modify data in the view • Removing views P69 Q109 10-21 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 22. Sequences Object Description Table Basic unit of storage; composed of rows View Logically represents subsets of data from one or more tables Sequence Generates numeric values Index Improves the performance of some queries Synonym Gives alternative names to objects 10-22 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 23. Sequences A sequence: • Can automatically generate unique numbers • Is a sharable object • Can be used to create a primary key value • Replaces application code • Speeds up the efficiency of accessing sequence values when cached in memory 2 4 6 8 10 1 3 5 7 9 10-23 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 24. CREATE SEQUENCE Statement: Syntax Define a sequence to generate sequential numbers automatically: CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] NOMINVALUE}] [{CYCLE | NOCYCLE}] NOCYCLE}] [{CACHE n | NOCACHE}]; P87 Q145 10-24 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 25. Creating a Sequence • Create a sequence named DEPT_DEPTID_SEQ to be used for the primary key of the DEPARTMENTS table. • Do not use the CYCLE option. CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE; Sequence created. 10-25 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 26. NEXTVAL and CURRVAL Pseudocolumns • NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. • CURRVAL obtains the current sequence value. • NEXTVAL must be issued for that sequence before CURRVAL contains a value. 10-26 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 27. 10-27 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 28. Using a Sequence • Insert a new department named “Support” in Support” location ID 2500: INSERT INTO departments(department_id, department_name, location_id) VALUES (dept_deptid_seq.NEXTVAL, 'Support', 2500); 1 row created. • View the current value for the DEPT_DEPTID_SEQ sequence: SELECT dept_deptid_seq.CURRVAL FROM dual; 10-28 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 29. Caching Sequence Values • Caching sequence values in memory gives faster access to those values. • Gaps in sequence values can occur when: – A rollback occurs – The system crashes – A sequence is used in another table 10-29 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 30. Modifying a Sequence Change the increment value, maximum value, minimum value, cycle option, or cache option: ALTER SEQUENCE dept_deptid_seq INCREMENT BY 20 MAXVALUE 999999 NOCACHE NOCYCLE; Sequence altered. 10-30 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 31. Guidelines for Modifying a Sequence • You must be the owner or have the ALTER privilege for the sequence. • Only future sequence numbers are affected. • The sequence must be dropped and re-created to restart the sequence at a different number. • Some validation is performed. • To remove a sequence, use the DROP statement: DROP SEQUENCE dept_deptid_seq; Sequence dropped. 10-31 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 32. Indexes Object Description Table Basic unit of storage; composed of rows View Logically represents subsets of data from one or more tables Sequence Generates numeric values Index Improves the performance of some queries Synonym Gives alternative names to objects 10-32 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 33. Indexes An index: • Is a schema object • Can be used by the Oracle server to speed up the retrieval of rows by using a pointer • Can reduce disk I/O by using a rapid path access method to locate data quickly • Is independent of the table that it indexes • Is used and maintained automatically by the Oracle server 10-33 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 34. How Are Indexes Created? • Automatically: A unique index is created automatically when you define a PRIMARY KEY or UNIQUE constraint in a table definition. • Manually: Users can create nonunique indexes on columns to speed up access to the rows. 10-34 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 35. Creating an Index • Create an index on one or more columns: CREATE INDEX index ON table (column[, column]...); • Improve the speed of query access to the LAST_NAME column in the EMPLOYEES table: CREATE INDEX emp_last_name_idx ON employees(last_name); Index created. 10-35 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 36. Index Creation Guidelines Create an index when: A column contains a wide range of values A column contains a large number of null values One or more columns are frequently used together in a WHERE clause or a join condition The table is large and most queries are expected to retrieve less than 2% to 4% of the rows in the table Do not create an index when: The columns are not often used as a condition in the query The table is small or most queries are expected to retrieve more than 2% to 4% of the rows in the table The table is updated frequently The indexed columns are referenced as part of an expression 10-36 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 37. 什么时候创建索引 如果出现下列情况,考虑创建索引: • 包含了大量不同值的列 • 包含了大量空值的列 • 一个或者多个列经常被一起出现在 WHERE WHERE条件中或者 作为连接的条件出现 • 表的数据量很大,而且对表的查询经常是得到表中数据 的2%到4%。 P92 Q154 10-37 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 38. 什么时候不要创建索引 如果出现下列情况,不需要创建索引: • 一个很小的表 • 列很少被用于查询的条件中出现 • 表上的大多数查询是得到表中大于4%的数据 • 表中的数据经常发生变动 • 要被索引的列被作为条件表达式的一部分或出现在函 数中(索引无效) 10-38 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 39. 查询索引信息 • 通过USER_INDEXES 数据字典可以得到索引的定义和 USER_INDEXES 唯一性. • 通过USER_IND_COLUMNS 数据字典可以得到索引的 USER_IND_COLUMNS 名称,表名和列名. SELECT ic.index_name, ic.column_name, ic.column_position col_pos,ix.uniqueness FROM user_indexes ix, user_ind_columns ic WHERE ic.index_name = ix.index_name AND ic.table_name = 'EMPLOYEES'; 10-39 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 40. Removing an Index • Remove an index from the data dictionary by using the DROP INDEX command: DROP INDEX index; • Remove the UPPER_LAST_NAME_IDX index from the data dictionary: DROP INDEX emp_last_name_idx; Index dropped. • To drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege. 10-40 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 41. Synonyms Object Description Table Basic unit of storage; composed of rows View Logically represents subsets of data from one or more tables Sequence Generates numeric values Index Improves the performance of some queries Synonym Gives alternative names to objects 10-41 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 42. Synonyms Simplify access to objects by creating a synonym (another name for an object). With synonyms, you can: • Create an easier reference to a table that is owned by another user • Shorten lengthy object names CREATE [PUBLIC] SYNONYM synonym FOR object; 10-42 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 43. Creating and Removing Synonyms • Create a shortened name for the DEPT_SUM_VU view: CREATE SYNONYM d_sum FOR dept_sum_vu; Synonym Created. • Drop a synonym: DROP SYNONYM d_sum; Synonym dropped. 10-43 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 44. Creating and Removing Synonyms • 创建一个同义词时,基表可以不存在; • 创建公共同义词时,需要相关权限; • 公共同义词所有用户都能看到,但不一定能看到基表,取决于权 限; • 私有、公共同义词同名时,用户看到的同义词默认为私有同义词; P63 Q100 同义词概念,P85 Q139 10-44 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 45. Summary In this lesson, you should have learned how to: • Create, use, and remove views • Automatically generate sequence numbers by using a sequence generator • Create indexes to improve query retrieval speed • Use synonyms to provide alternative names for objects 10-45 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 46. Practice 10: Overview of Part 2 This practice covers the following topics: • Creating sequences • Using sequences • Creating nonunique indexes • Creating synonyms • P59 Q93 10-46 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 47. 10-47 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 48. 10-48 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  • 49. 10-49 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.