1 / 26

Database Programming

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

lars-franks
Download Presentation

Database Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Hohly

  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 Marge Hohly

  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 Hohly

  5. Example of a Sequence • CREATE SEQUENCE emp_emp_id_seqINCREMENT BY 10START WITH 300MAXVALUE 9999NOCACHENOCYCLE; • 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 Marge Hohly

  6. Confirm sequence • SELECT sequence_name, min_value, max_value, Increment_by, last_numberFROM user_sequences; Marge Hohly

  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 Marge Hohly

  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 Hohly

  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); Marge Hohly

  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 calused of an UPDATE statement Marge Hohly

  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 Hohly

  12. Modifying & Deleting a Sequence • ALTER SEQUENCE emp_emp_id_seqINCREMENT BY 5MAXVALUE 9999NOCACHENOCYCLE: • DROP SEQUENCE emp_emp_id_seq; Marge Hohly

  13. 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 Marge Hohly

  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 Hohly

  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 Hohly

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

  17. 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 Hohly

  18. Example of an INDEX • CREATE INDEX index_nameON table_name(column….,column); • CREATE INDEX d_cds_name_email_idxON d_clients(last_name, email); • DROP INDEX d_cds_name_email_idx; Marge Hohly

  19. When to Create an INDEX Marge Hohly

  20. WHEN NOT to create an index Marge Hohly

  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 Hohly

  22. Confirming indexes • SELECT ic.index_name,ic.column_name, ic.column_position col_pos, ix.uniquenessFROM user_indexes ix, user_ind_columns icWHERE ic.index_name = ix.index_name AND ic.table_name = ‘Employees’; Marge Hohly

  23. Function-based indexes • CREATE INDEX upper_last_name_idx ON employees (UPPER(last_name)); • SELECT *FROM employeesWHERE UPPER(last_name)=‘King’; Marge Hohly

  24. 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 Hohly

  25. Example of a SYNONYM • CREATE [PUBLIC] SYNONYM synonym_nameFOR object; • CREATE SYNONYM empFOR ussc_bhs_sql01_s02.employees; • PUBLIC: creates a synonym accessible to all users (we don’t have the privilege to use PUBLIC in HTML_DB) • 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. Marge Hohly

  26. Remove synonym • DROP [PUBLIC] SYNONYMname_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 Hohly

More Related