Database Programming Sections 12 – Sequences, Indexes, and Synonymns.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Chapter Thirteen Sequences Dr. Chitsaz Objectives: Sequence objects Create and use sequences Application of sequences.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
11-1 Copyright © Oracle Corporation, All rights reserved. Different type of keys.
Database Programming Sections 13. Marge Hohly  1. Which statements are True about the following sequence? The sequence was used to generate numbers.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Introduction to Structured Query Language (SQL)
Managing Schema Objects
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Copyright س Oracle Corporation, All rights reserved. 13 Other Database Objects.
SQL's Data Definition Language (DDL) – View, Sequence, Index.
Chapter 6 Additional Database Objects
13 Other Database Objects Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Objectives After completing this lesson, you should be able to do the following: Categorize the main database objects Review the table structure List.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
12 Copyright © Oracle Corporation, All rights reserved. Other Database Objects.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.
Oracle Sequences Sequences are an independent object in the database (not a data type) Sequences have a name and can be used anywhere a value is expected.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
Chapter 5 Sequences.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
CHAPTER 9 Views, Synonyms, and Sequences. Views are used extensively in reporting applications and also to present subsets of data to applications. Synonyms.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
George Mpopo | Rosebank College ADVANCED DATABASES WITH ORACLE 11g FOR ADDB7311 LEARNING UNIT 2 of 7.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1 (SQL) Other Database Objects Asif Sohail University of the.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
Session 1 Module 1: Introduction to Data Integrity
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
Creating Indexes Database Systems Objectives Distinguish between the indexes that are created automatically and those that are created manually.
Other database objects (Sequence and view). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically.
SQL Creating and Managing Tables
ITEC 313 Database Programming
Oracle Certified 1z0-047 Exam Questions
Creating Other Schema Objects
Other Database Objects
SQL Creating and Managing Tables
Creating Other Schema Objects
SQL Creating and Managing Tables
Chapter 5 Sequences.
Managing Objects with Data Dictionary Views
Chapter 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
Database Programming Sections 11-12–Sequences, Indexes & Synonyms, Controlling User Access, Creating & Revoking Object Privileges 11/25/08.
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
CH 4 Indexes.
Chapter 2 Views.
Contents Preface I Introduction Lesson Objectives I-2
IST 318 Database Administration
Other Database Objects
Presentation transcript:

Database Programming Sections 12 – Sequences, Indexes, and Synonymns

What is a sequence?  A SEQUENCE is a shareable object used to automatically generate unique numbers.  Can be used by multiple users  Often used to create primary-key values  Incremented or decremented by an internal ORACLE routine  Reduces amount of code you need to write. Marge Hohly2

3 The Syntax for Creating a Sequence  CREATE SEQUENCE sequence_name [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}];  Sequence_name – the name of sequence generator (object)  INCREMENT BY n – interval between sequence numbers where n is an integer (if omitted n is 1)  START WITH n – specifies the first sequence number to be generated (if omitted start with 1)  MAXVALUE n – specifies the maximum value the sequence can generate  NOMAXVALUE – specifies a maximum value of 10^27 for ascending and -1 for descending  MINVALUE n – specifies the minimum value the sequence can generate  NOMINVALUE – specifies a minimum value of 1 for ascending and -10^27 for descending  CYCLE – whether the sequence continues to generate values after reaching its max or min value  NOCYCLE – the default if CYCLE is not specified  CACHE n – specifies how many values the Oracle Server preallocates and keeps in memory (default is 20) if the sequence values are cached, they will be lost if there is a system failure  NOCACHE – does not cache any values

The syntax for creating a sequence  CREATE SEQUENCE sequence_name [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}]; Marge Hohly4

5 Example of a Sequence  CREATE SEQUENCE emp_emp_id_seq INCREMENT BY 10 START WITH 300 MAXVALUE NOCACHE NOCYCLE;  ask for NEXTVAL = 300 – it becomes CURRVAL the number just generated in HTMLDB once you return the NEXTVAL from the sequence you no longer have the “session” and the database no longer knows what’s the CURVAL

Confirm sequence  SELECT sequence_name, min_value, max_value, Increment_by, last_number FROM user_sequences; Marge Hohly6

7 NEXTVAL and CURRVAL  NEXTVAL is a pseudocolumn used to return the next available sequence value  CURRVAL is a pseudocolumn used to obtain the last-used sequence value  NEXTVAL must be issued before CURRVAL contains a value  NEXTVAL and CURRVAL must be qualified with a sequence name: emp_emp_id_seq.nextval

NEXTVAL & CURRVAL  INSERT INTO departments (department_id, department_name, location_id) VALUES (departments_seq.NEXTVAL, ‘Support’, 2500);  Inserts a new department into the department table  Uses DEPARTMENT_SEQ sequence to generate new department number Marge Hohly8

9 Using a Sequence to INSERT  INSERT INTO employees VALUES (emp_emp_id_seq.NEXTVAL, ‘Kramer’, ‘Wilson’, ‘KWILSON’, ‘ ’, ’11-FEB-87’, ‘AD_ASST’, 5000, NULL, 101, 10);

Using a SEQUENCE  You can use NEXTVAL and CURRVAL in the following contexts: The SELECT list of the SELECT statement that is not part of the subquery The SELECT list of a subquery is an INSERT statement The VALUES clause of an INSERT statement The SET clause of an UPDATE statement Marge Hohly10

Using a SEQUENCE  Cannot use NEXTVAL and CURRVAL in the following contexts: The SELECT list of a view A SELECT statement with the DISTINCT keyword A SELECT statement with GROUP BY, HAVING, or ORDER BY clauses A subquery in a SELECT, DELETE, or UPDATE The DEFAULT expression in a CREATE TABLE or ALTER TABLE statement Marge Hohly11

Marge Hohly12 Modifying & Deleting a Sequence  ALTER SEQUENCE emp_emp_id_seq INCREMENT BY 5 MAXVALUE 9999 NOCACHE NOCYCLE:  DROP SEQUENCE emp_emp_id_seq;

Marge Hohly13 Sequence Gaps  Gaps (nonsequential numbers) can be generated by: rolling back a statement containing a sequence, the number is lost a system crash. If the sequence caches values into the memory and the system crashes, these values are lost. the same sequence being used for multiple tables. If you do so, each table can contain gaps in the sequential numbers

Modifying a Sequence  Use the ALTER SEQUENCE statement  ALTER SEQUENCE runner_id_seq INCREMENT BY 1 MAXVALUE NOCACHE NOCYCLE;  Some validation occurs: Example if MAXVALUE is less than current sequence, it cannot execute.  Error code Marge Hohly14

ALTER SEQUENCE guidelines  You must be the owner or have ALTER privilege for the sequence  Only future sequence numbers are affected by the ALTER SEQUENCE statement  The START WITH option cannot be changed using the ALTER SEQUENCE statement. Marge Hohly15

Marge Hohly16 What is an Index?  A schema object that can speed up the retrieval of rows by using a POINTER (isles in a grocery store)  If you do not have an index on the column you’re selecting, then a full table scan occurs  Unique Index – Automatically created when you define a column in a table to have a PRIMARY KEY or a UNIQUE KEY constraint.  Non-Unique Index – An index that a user can create to speed up access to the rows For example, to optimize joins, you can create an index on the FOREIGN KEY column, which speeds up the search to match rows to the PRIMARY KEY column.

Marge Hohly17 Example of an INDEX  WHEN TO CREATE AN INDEX The 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-4% of the rows.  WHEN NOT TO CREATE AN INDEX The table is small The columns are not often used as a condition in the query Most queries are expected to retrieve more than 2-4% of the rows in the table The table is updated frequently – DML required index updates The indexed columns are referenced as part of an expression

Marge Hohly18 Example of an INDEX  CREATE INDEX index_name ON table_name(column….,column);  CREATE INDEX d_cds_name_ _idx ON d_clients(last_name, );  DROP INDEX d_cds_name_ _idx;

When to Create an INDEX Marge Hohly19

WHEN NOT to create an index Marge Hohly20

When NOT to create an index  Usually not worth creating when: Table is small Columns are not often used as a condition in the query Most queries are expected to retrieve more than 2-4% of the rows in table Table is updated frequently Indexed columns are referenced as part of an expression Marge Hohly21

Composite Index  Created on multiple columns in a table  Columns can be in any order  Columns need not be adjacent  Speeds up retrieval when: WHERE reference all columns or leading portion of columns  Null values not included in composite index Marge Hohly22

Confirming indexes  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’; Marge Hohly23

Function-based indexes  CREATE INDEX upper_last_name_idx ON employees (UPPER(last_name));  SELECT * FROM employees WHERE UPPER(last_name)=‘King’; Marge Hohly24

Removing an INDEX  You can not modify indexes  To change them you must delete and create again  To DROP an index you must be the owner or have DROP ANY INDEX privilege.  If you drop a table it automatically drops constrains and indexes, but views and sequences remain. Marge Hohly25

Marge Hohly26 Example of a SYNONYM  CREATE [PUBLIC] SYNONYM synonym_name FOR object;  CREATE SYNONYM emp FOR ussc_bhs_sql01_s02.employees; PUBLIC: creates a synonym accessible to all users (we don’t have the privilege to use PUBLIC in APEX) synonym_name: is the name of the synonym to be created object: identifies the object for which the synonym is created -A private synonym name must be distinct from all other objects owned by the same user.

Remove synonym  DROP [PUBLIC] SYNONYM name_of_synonym;  DROP SYNONYM dj_titles;  Guidelines: Object cannot be contained in a package A private synonym name must be distinct from all other objects owned by the same user. Marge Hohly27