Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Database Programming Sections 12 – Sequences, Indexes, and Synonymns."— Presentation transcript:

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

2 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 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

4 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 5 Example of a Sequence  CREATE SEQUENCE emp_emp_id_seq INCREMENT BY 10 START WITH 300 MAXVALUE 99999 NOCACHE NOCYCLE;  300 310 320 330 340 350 360.....99999 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

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

7 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

8 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 9 Using a Sequence to INSERT  INSERT INTO employees VALUES (emp_emp_id_seq.NEXTVAL, ‘Kramer’, ‘Wilson’, ‘KWILSON’, ‘803.245.4642’, ’11-FEB-87’, ‘AD_ASST’, 5000, NULL, 101, 10);

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

11 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

12 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;

13 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

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

15 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

16 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.

17 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

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

19 When to Create an INDEX Marge Hohly19

20 WHEN NOT to create an index Marge Hohly20

21 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

22 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

23 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

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

25 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

26 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.

27 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


Download ppt "Database Programming Sections 12 – Sequences, Indexes, and Synonymns."

Similar presentations


Ads by Google