chapter 6 additional database objects (up to p.195 and all in the pptx file)

61
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.218 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane, WA 99258 USA [email protected]

Upload: tuari

Post on 23-Feb-2016

62 views

Category:

Documents


0 download

DESCRIPTION

Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file). Jason C. H. Chen , Ph.D. Professor of MIS School of Business Gonzaga University Spokane, WA 99258 USA [email protected]. Objectives. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 1

Chapter 6Additional Database Objects

(up to p.218 and all in the pptx file)

Jason C. H. Chen, Ph.D.Professor of MIS

School of BusinessGonzaga University

Spokane, WA 99258 [email protected]

Page 2: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 2

Objectives• Define the purpose of a sequence and state how it can

be used in a database• Explain why gaps may appear in the integers

generated by a sequence• Use the CREATE SEQUENCE command to create a

sequence• Call and use sequence values• Identify which options cannot be changed by the

ALTER SEQUENCE command• Delete a sequence• Create indexes with the CREATE INDEX command• Explain the main index structures: B-tree and bitmap

Page 3: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 3

Objectives (continued)• Verify index use with the explain plan• Introduce variations on conventional indexes,

including a function-based index and an index organized table

• Verify index existence via the data dictionary• Rename an index with the ALTER INDEX

command• Remove an index using the DROP INDEX

command• Create and remove a public synonym

Page 4: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 4

Database Objects• An object is anything that has a name and

defined structure• Includes:

– Table – stores data– Sequence – generates sequential integers (for

the pk) by the system (e.g., Oracle) automatically

– Index – allows users to quickly locate specific records

– Synonym – alias for other database objects

Page 5: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 5

Sequences• Used for internal control purposes by

providing sequential integers for auditing• Used to generate unique value for primary

key column– Surrogate key = no correlation with actual row

contents

Page 6: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 6

Creating a Sequence

• Use the CREATE SEQUENCE command • Various intervals are allowed – Default: 1• You can specify the starting number – Default: 1

CREATE SEQUENCE sequencename[INCREASE BY value][START WITH value][{MAXVALUE value | MAXVALUE}][{MINVALUE value | MINVALUE}][{CYCLE | NOCYLE}][{ORDER | NOORDER}][{CACHE value | NOCACHE}];

Figure 6-1 Syntax of the CREATE SEQUENCE command

Page 7: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 7

Creating a Sequence (continued)• Can specify MINVALUE for decreasing sequence

and MAXVALUE for increasing sequence• Numbers can be reused if CYCLE is specified

– The options determine whether Oracle should begin reissuing values from the sequence after reaching the minimum or maximum value.

• ORDER clause is used in application cluster environment

• Use CACHE to pregenerate integers – Default: 20

Page 8: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 8

ORDERS Data Files

Page 9: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 9

Refresh the Database• 1. Go to Blackboard and download (two)

data files from Oracle chapter6 and save under c:\oradata\chapter6\

• 2. Run the following script file– Start c:\oradata\chapter6\JLDB_Build_6.sql

Page 10: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 10

Creating a Sequence (continued)

Figure 6-3 Syntax of the INSERT command

Page 11: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 11

Creating a Sequence (continued)

Figure 6-4 Query USER-OBJECTS to verify existing sequence

Page 12: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 12

Creating a Sequence (continued)

• To verify the settings for options of a sequence, query USER_SEQUENCES data dictionary view

Next Number to issueFigure 6-5 Verifying sequence option settings

SELECT SEQUENCE_NAME FROM USER_SEQUENCES;SEQUENCE_NAME------------------------------ORDERS_ORDER#_SEQ

Page 13: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 13

Using Sequence Values

• NEXTVAL – generates integer

Figure 6-6 Inserting a row, using a sequence to provide a PRIMARY KEY value

Page 14: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 14

Using Sequence Values (continued)

Figure 6-7 Order added, using a sequence value for the Order#

Page 15: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 15

Using Sequence Values (continued)

• CURRVAL – contains last integer generated by NEXTVAL

Figure 6-8 Using CURRVAL to insert an order detail row

cpk

INSERT INTO orderitems (order#, item#, isbn, quantity, paideach)VALUES (orders_order#_seq.CURRVAL, 1, 8117949391, 1, 8.50);

Page 16: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 16

Using Sequence Values (continued)

Figure 6-9 Verify the CURRVAL value

Page 17: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 17

Using Sequence Values (continued)

Figure 6-9 Verify the CURRVAL value

Page 18: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 18

Setting and Altering Sequence DefinitionsOracle 12c provides a new feature that allows a sequence CURRVAL or NEXTVAL to be set as the default value of a column.

SQL> -- chapter 6, Figure 6-10; p. 192SQL> CREATE SEQUENCE test_defval_seq 2 INCREMENT BY 1 3 START WITH 100 4 NOCACHE 5 NOCYCLE;Sequence created.SQL>SQL> CREATE TABLE test_defval 2 (col1 NUMBER DEFAULT test_defval_seq.NEXTVAL, 3 Col2 NUMBER);Table created.

SQL> DESCRIBE test_defval; Name Null? Type -------------------------------- -------- --------------- COL1 NUMBER COL2 NUMBER

Now that the objects and the default column value setting is in place, execute the Insert statements in Figure 6-11 (next slide) to confirm the effect of different styles of Insert statements on the default value setting.

Page 19: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 19

SQL> -- chapter 6, Figure 6-11; p. 193SQL> INSERT INTO test_defval (col1, col2) 2 VALUES (DEFAULT, 350);1 row created.

SQL> INSERT INTO test_defval (col2) 2 VALUES (355);1 row created.

SQL> INSERT INTO test_defval (col1, col2) 2 VALUES (222, 360);1 row created.

SQL> -- chapter 6, Figure 6-12; p. 193SQL> SELECT * 2 FROM test_defval;

COL1 COL2---------- ---------- 100 350 101 355 222 360

Page 20: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 20

Altering Sequence Definitions

• Use ALTER SEQUENCE command to change the settings for a sequence

• START WITH value cannot be altered – drop the sequence and re-create it

• Changes cannot make current integers invalid

Page 21: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 21

ALTER SEQUENCE Command Example

Figure 6-14 Command to change the INCREMENT BY setting for a sequence

Page 22: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 22

Checking Values on Sequences and SYSDATE

Figure 6-14, 6-15 Using the DUAL table

Figure 6-15 New setting for the ORDERS_ORDER#_SEQ sequence

ALTER SEQUENCE orders_order#_seqINCREMENT BY 10;

SELECT *FROM user_sequences;

Page 23: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 23

Checking Values on Sequences

Figure 6-17 Testing sequence values with the DUAL table Why

1041?

Page 24: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 24

Removing a Sequence

• Use the DROP SEQUENCE command to delete a sequence

• Previous values generated are not affected by removing a sequence from a database

Figure 6-19 Dropping the ORDERS_ORDER#_SEQ sequence

Page 25: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 25

Removing a Sequence (continued)

Figure 6-20 Verify that the sequence is removed

Page 26: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 26

Indexes –Query Optimization

• If database query activity (e.g., process speed) is the priority– Indexes should be considered

• If database operations involve more DML actions than query actions– Index creation should be minimized

• An index stores frequently referenced values and ROWIDs

• Can be based on one column, multiple columns, functions, or expressions

Page 27: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 27

B-Tree Index

Figure 6-25 B-tree index organization

Page 28: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 28

B-Tree Index (continued)• Implicitly create an index by PRIMARY

KEY and UNIQUE constraints• Explicitly create an index by using the

CREATE INDEX command

For example:SELECT index_name FROM user_indexes;-- result on the next slide

Page 29: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 29

Examples on Query Optimization• Note that the following slides are not in the

text.• Please study them thoroughly and practice

all examples (SQL commands are available in the ‘Ch6Queries.sql’.

Page 30: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 30

INDEX_NAME------------------------------ACCTBONUS_AMID_PKBOOKAUTHOR_PKORDERITEMS_PKBOOKS_ISBN_PKBOOKS_COST_IDXAUTHOR_AUTHORID_PKPUBLISHER_PUBID_PKORDERS_ORDER#_PKCUSTOMERS_ZIP_DESC_IDXCUSTOMERS_ZIP_IDXCUSTOMERS_CUSTOMER#_PKEMPLOYEES_EMPNO_PKPT_CHARG_PATIENTNO_ITEMCODE_PKITEM_ITEM_CODE_PKROOM_ROOM_NO_PKACCOMODATION_ACCOM_ID_PKDOCTOR_PHYSCIAN_ID__PKPATIENT_PATIENT_NO_PKLOCATION_LOC_ID_PK

INDEX_NAME------------------------------ENROLLMENT_PKCOURSE_SECTION_CSEC_ID_PKCOURSE_COURSE_ID_PKTERM_TERM_ID_PKSTUDENT_S_ID_PKSYS_IL0000110103C00010$$FACULTY_F_ID_PKPUBLISHER3_PUBID_PKPUBLISHER2_PUBID_PKREPCONTRACTS_PKBOOKSTORES_ID_PKBOOKSTORES_NAME_UKSTOREREPS_ID_PK

32 rows selected.

For example:SELECT index_name FROM user_indexes;

What specific information is displayed on the output?Answer: _____Since they are all implicitly indexed.

Page 31: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 31

Implicitly Index (cont.)

-- enter in SQL DEVELOPERSELECT table_name, index_name, index_typeFROM user_indexes;

Page 32: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 32

Optimizing Query Processing -A Quick Way to Speed Access to Your data

• Indexes may be used to improve the efficiency of data searches to meet particular search criteria after the table has been in use for some time. Therefore, the ability to create indexes quickly and efficiently at any time is important.

• SQL indexes can be created on the basis of any selected attributes

Page 33: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 33

BOOKS

ISBN Title PubDate PubID Cost Retail Discount CategoryVARCHAR2(10) VARCHAR2(30) DATE NUMBER(2) NUMBER(5,2) NUMBER(5,2) NUMBER(4,2) VARCHAR2(12)

BOOKS

Optimizing Query Processing – an example (not in the text)

Page 34: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 34

-- chapter 6, (about the same as Figure 6-36; p. 210)-- 'BOOKS' must be in upper caseSELECT table_name, index_name, index_typeFROM user_indexesWHERE table_name = 'BOOKS';

ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE2147428890 SHORTEST POEMS 21.85 LITERATURE

7 rows selected.

SELECT isbn, title, cost, categoryFROM booksWHERE cost > 20;

Optimizing Query Processing

Page 35: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 35

Optimizing Query Processing (conti.)

Syntax:

CREATE INDEX name_of_indexON table_name (field_to_be_indexed);

For example (but, do not enter the SQL command now):CREATE INDEX books_cost_idxON books (cost);

Note that this statement defines an index called books_cost_idx for the cost column in the books table. This index ensures that in the next example SQL only needs to look at row in the database that satisfy the WHERE condition, and is, therefore, quicker to produce an answer.

Page 36: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 36

Optimizing Query Processing (conti.)

Type the following SQL:SELECT isbn, title, cost, categoryFROM booksWHERE cost > 20;ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE2147428890 SHORTEST POEMS 21.85 LITERATURE

7 rows selected.

CREATE INDEX books_cost_idx ON books (cost); SQL> CREATE INDEX books_cost_idx ON books (cost);

Index created.

Page 37: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 37

Optimizing Query Processing (conti.)

SELECT isbn, title, cost, categoryFROM booksWHERE cost > 20;ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER2147428890 SHORTEST POEMS 21.85 LITERATURE8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE

7 rows selected.

What is the difference on the output between these two versions?

Page 38: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 38

Before “INDEX”.ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE2147428890 SHORTEST POEMS 21.85 LITERATURE

7 rows selected.

After “INDEX”.ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER2147428890 SHORTEST POEMS 21.85 LITERATURE8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE

7 rows selected.

Page 39: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 39

SET TIMING ON and OFFDROP INDEX books_cost_idx ;

SET TIMING ON;

SELECT isbn, title, cost, categoryFROM booksWHERE cost > 20;

CREATE INDEX books_cost_idx ON books (cost);

SELECT isbn, title, cost, categoryFROM booksWHERE cost > 20;

SET TIMING OFF; (results are shown on the next slide)

Page 40: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 40

SQL> SET TIMING ON;SQL>SQL> SELECT isbn, title, cost, category 2 FROM books 3 WHERE cost > 20;

ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE2147428890 SHORTEST POEMS 21.85 LITERATURE

7 rows selected.

Elapsed: 00:00:00.02SQL>SQL> CREATE INDEX books_cost_idx ON books (cost);

Index created.

Elapsed: 00:00:00.00

Page 41: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 41

SQL> SELECT isbn, title, cost, category 2 FROM books 3 WHERE cost > 20;

ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER2147428890 SHORTEST POEMS 21.85 LITERATURE8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE

7 rows selected.

Elapsed: 00:00:00.01SQL>SQL> SET TIMING OFF;

Page 42: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 42

Index on Query Optimization (conti.)

-- chapter 6, (about the same as Figure 6-36; p. 210 )-- 'BOOKS' must be in upper caseSELECT table_name, index_name, index_typeFROM user_indexesWHERE table_name = 'BOOKS';

Page 43: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 43

Query Optimization: DROP INDEX

DROP INDEX books_cost_idx; DROP INDEX books_cost_desc_idx;

-- chapter 6, (about the same as Figure 6-36; p. 210 )-- 'BOOKS' must be in upper caseSELECT table_name, index_name, index_typeFROM user_indexesWHERE table_name = 'BOOKS';

Page 44: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 44

Index on Query Optimization (conti.)-- default is ‘ASCENDING’CREATE INDEX books_cost_desc_idxON books (cost DESC);

SELECT isbn, title, cost, categoryFROM booksWHERE cost > 20;ISBN TITLE COST CATEGORY---------- ------------------------------ ---------- ------------1915762492 HANDCRANKED COMPUTERS 21.8 COMPUTER2147428890 SHORTEST POEMS 21.85 LITERATURE8843172113 DATABASE IMPLEMENTATION 31.4 COMPUTER4981341710 BUILDING A CAR WITH TOOTHPICKS 37.8 CHILDREN9959789321 E-BUSINESS THE EASY WAY 37.9 COMPUTER3957136468 HOLY GRAIL OF ORACLE 47.25 COMPUTER2491748320 PAINLESS CHILD-REARING 48 FAMILY LIFE

7 rows selected.

Why the output is still displayed as “ascending” order?Internally, the books table has been indexed as ‘descending’ order (using pointer); however, the display is on the ‘ascending’ (default) order.

Page 45: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 45

Index on Query Optimization (conti.)

-- chapter 6, (about the same as Figure 6-30; p. 199 )-- 'BOOKS' must be in upper caseSELECT table_name, index_name, index_typeFROM user_indexesWHERE table_name = 'BOOKS';

Page 46: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 46

Query Optimization: DROP INDEX

DROP INDEX books_cost_idx; DROP INDEX books_cost_desc_idx;

-- chapter 6, (about the same as Figure 6-30; p. 199 )-- 'BOOKS' must be in upper caseSELECT table_name, index_name, index_typeFROM user_indexesWHERE table_name = 'BOOKS';

Page 47: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 47

Summary on Optimizing Query Processing (cont.)

hThe indexes are defined so as to optimize the processing of SELECT statements.

hAn index is never explicitly referenced in a SELECT statement; the syntax of SQL does not allow this;

hDuring the processing of a statement, SQL itself determines whether an existing index will be used;

hAn index may be created or deleted at any time;hWhen updating, inserting or deleting rows, SQL also

maintains the indexes on the tables concerned. This means that, on the one hand, the processing time for SELECT statements is reduced, while, on the other hand, the processing time for update statements (such as INSERT, UPDATE and DELETE) can increase.

Page 48: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 48

Your Turn …• Practice the following examples (from the

text)

Page 49: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 49

CREATE INDEX Command Examples

Figure 6-24 Creating an index on the Zip column

Page 50: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 50

The Explain Plan

Figure 6-26 View the explain plan indicating a full table scan

Page 51: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 51

The Explain Plan (continued)

Figure 6-27 View the explain plan indicating a full table scan (using an optimizer hint)

optimizer hint

Page 52: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 52

UNIQUE INDEX

Figure 6-28Explicitly creating a unique index

-- chapter 6, Figure 6-28; p. 205CREATE UNIQUE INDEX books_title_idxON books (title);

Unique index should be used on appropriate columns because this type of index assists performance by verifying that duplicate values aren’t entered in a column during DML operation.

Page 53: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 53

CREATE INDEX Command Examples

Figure 6-30 Creating a composite index

• The composite index could improve the performance of queries that include a search condition on both the Lastname and Firstname column. • Generally, it is more efficient than creating two separate single-

column indexes because less I/O is required to read a single index.

-- chapter 6, Figure 6-29; p. 205CREATE INDEX customers_zip_desc_idx ON customers (zip DESC);

Page 54: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 54

Verifying Composite Index

SELECT customer#, lastname, firstname, city, state, zipFROM customers;

-- chapter 6, Figure 6-30; p. 206CREATE INDEX customer_name_idxON customers (lastname, firstname);

SELECT customer#, lastname, firstname, city, state, zipFROM customers;

After tested, no significant performance can be found since it is a very small database.

Any performance improvement?

Page 55: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 55

• Practice all the examples in the text.• A Script file is available on the Bb (file

name: Ch6Queries.sql)• After completing all examples, do the HW.

Page 56: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 56

Homework - Hands-On Assignments

Read and Practice all examples on Chapters 6• 1. Run the script files (in the folder \oradata\

chapter5\): JLDB_Build_6.sql• 2. Read Oracle assignment and create a script file

Oracle_ch6_Lname_Fname.sql for questions (#1 to #5 and #9; pp.222-223) on “Hands-on Assignments”. Use appropriate COLUMN statements to produce readable outputs if needed.

• 3. Execute and test one problem at a time and make sure they are all running successfully.

• 4. When you done, spool the script files (see next slide for spooling instructions) and UPLOAD the spooled file (Oracle_ch6_Spool_Lname_Fname.txt) to Bb by the deadline .

Upload the spooled file (*.txt) to the Bb (under “Assignments & Projects”) by the deadline.

Page 57: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 57

Summary• A sequence can be created to generate a series of integers• The values generated by a sequence can be stored in any table• A sequence is created with the CREATE SEQUENCE command• Gaps in sequences might occur if the values are stored in various

tables, if numbers are cached but not used, or if a rollback occurs• A value is generated by using the NEXTVAL pseudocolumn• The CURRVAL pseudocolumn is NULL until a value is generated

by NEXTVAL• The USER_OBJECTS data dictionary object can be used to

confirm the existence of all schema objects• The USER_SEQUENCES data dictionary object is used to view

sequence settings

Page 58: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 58

Summary (continued)• The ALTER SEQUENCE command is used to modify an existing

sequence; the only settings that can’t be modified are the START WITH option and any option that would be invalid because of previously generated values

• The DUAL table is helpful for testing sequence value generation• The DROP SEQUENCE command deletes an existing sequence• An index can be created to speed up the query process• DML operations are always slower when indexes exist• Oracle 11g creates an index for PRIMARY KEY and UNIQUE

constraints automatically• An explicit index is created with the CREATE INDEX command• An index can be used by Oracle 11g automatically if a query

criterion or sort operation is based on a column or an expression used to create the index

Page 59: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 59

Summary (continued)

• The two main structures for indexes are B-tree and bitmap• The explain plan can verify whether an index is used in a

query• Function-based indexes are used to index an expression or

the use of functions on a column or columns• An index organized table is a table stored in a B-tree

structure to combine the index and table into one database object

• Information about an index can be retrieved from the USER_INDEXES and USER_IND_COLUMNS views

• An index can be dropped with the DROP INDEX command• An index can be renamed with the ALTER INDEX command

Page 60: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 60

Summary (continued)

• Except for a name change, an index can’t be modified; it must be deleted and then re-created

• A synonym provides a permanent alias for a database object• A public synonym is available to any database user• A private synonym is available only to the user who created

it• A synonym is created by using the CREATE SYNONYM

command• A synonym is deleted by using the DROP SYNONYM

command• Only a user with DBA privileges can drop a public synonym

Page 61: Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file)

Dr. Chen, Oracle Database System (Oracle) 61

END OF CHAPTER 6