Oracle Database supports several types of index: Normal indexes and bitmap indexes. Domain indexes are instances of an application-specific index of type indextype. You can use sequences to automatically generate primary key values.
Oracle Database supports several types of index: Normal indexes and bitmap indexes. Domain indexes are instances of an application-specific index of type indextype. You can use sequences to automatically generate primary key values.
Oracle Database supports several types of index: Normal indexes and bitmap indexes. Domain indexes are instances of an application-specific index of type indextype. You can use sequences to automatically generate primary key values.
Use the CREATE INDEX statement to create an index on:
One or more columns of a table, a partitioned table, an index-organized table, or a cluster One or more scalar typed object attributes of a table or a cluster A nested table storage table for indexing a nested table column An index is a schema object that contains an entry for each value that appears in the indexed column(s) of the table or cluster and provides direct, fast access to rows. Oracle Database supports several types of index: Normal indexes. (By default, Oracle Database creates B-tree indexes.) Bitmap indexes, which store rowids associated with a key value as a bitmap Partitioned indexes, which consist of partitions containing an entry for each value that appears in the indexed column(s) of the table Function-based indexes, which are based on expressions. They enable you to construct queries that evaluate the value returned by an expression, which in turn may include built-in or user-defined functions. Domain indexes, which are instances of an application-specific index of type indextype Creating an Index: Example The following statement shows how the sample index ord_customer_ix on the customer_id column of the sample table oe.orders was created: CREATE INDEX ord_customer_ix ON orders (customer_id);
Compressing an Index: Example To create the ord_customer_ix_demo index with the COMPRESS clause, you might issue the following statement: CREATE INDEX ord_customer_ix_demo ON orders (customer_id, sales_rep_id) COMPRESS 1;
The index will compress repeated occurrences of customer_id column values. CREATE SEQUENCE Purpose Use the CREATE SEQUENCE statement to create a sequence, which is a database object from which multiple users may generate unique integers. You can use sequences to automatically generate primary key values. When a sequence number is generated, the sequence is incremented, independent of the transaction committing or rolling back. If two users concurrently increment the same sequence, then the sequence numbers each user acquires may have gaps, because sequence numbers are being generated by the other user. One user can never acquire the sequence number generated by another user. Once a sequence value is generated by one user, that user can continue to access that value regardless of whether the sequence is incremented by another user. Sequence numbers are generated independently of tables, so the same sequence can be used for one or for multiple tables. It is possible that individual sequence numbers will appear to be skipped, because they were generated and used in a transaction that ultimately rolled back. Additionally, a single user may not realize that other users are drawing from the same sequence. Once a sequence is created, you can access its values in SQL statements with the CURRVAL pseudocolumn, which returns the current value of the sequence, or the NEXTVAL pseudocolumn, which increments the sequence and returns the new value. Creating a Sequence: Example
The following statement creates the sequence customers_seq in the sample schema oe. This sequence could be used to provide customer ID numbers when rows are added to the customers table. CREATE SEQUENCE customers_seq START WITH 1000 INCREMENT BY 1 NOCACHE NOCYCLE;
CYCLE
Specify CYCLE to indicate that the sequence continues to generate values after reaching either its maximum or minimum value. After an ascending sequence reaches its maximum value, it generates its minimum value. After a descending sequence reaches its minimum, it generates its maximum value.
NOCYCLE
Specify NOCYCLE to indicate that the sequence cannot generate more values after reaching its maximum or minimum value. This is the default.
The first reference to customers_seq.nextval returns 1000. The second returns 1001. Each subsequent reference will return a value 1 greater than the previous reference.
SQL - Sequences/Auto Increments
Sequences A sequence is a database object that generates numbers in sequential order. Applications most often use these numbers when they require a unique value in a table such as primary key values. Some database management systems use an "auto number" concept or "auto increment" setting on numeric column types. Both the auto numbering columns and sequences provide a unique number in sequence used for a unique identifier. The following list describes the characteristics of sequences: Sequences are available to all users of the database Sequences are created using SQL statements (see below) Sequences have a minimum and maximum value (the defaults are minimum=0 and maximum=2 63 -1); they can be dropped, but not reset Once a sequence returns a value, the sequence can never return that same value While sequence values are not tied to any particular table, a sequence is usually used to generate values for only one table Sequences increment by an amount specified when created (the default is 1) Creating a Sequence To create sequences, execute a CREATE SEQUENCE statement in the same way as an UPDATE or INSERT statement. The sequence information is stored in a data dictionary file in the same location as the rest of the data dictionary files for the database. If the data dictionary file does not exist, the SQL engine creates the file when it creates the first sequence. In legacy dictionaries, the new file name is SEQUENCE.DD. The format of this file remains proprietary and subject to change, so do not depend on the record layout or format of the data. In Journaled Filesystem databases, this information is also proprietary and subject to change. The format for a CREATE SEQUENCE statement is as follows: CREATE SEQUENCE sequence_name [INCREMENT BY #] [START WITH #] [MAXVALUE # | NOMAXVALUE] [MINVALUE # | NOMINVALUE] [CYCLE | NOCYCLE] Variable Description INCREMENT BY The increment value. This can be a positive or negative number. START WITH The start value for the sequence. MAXVALUE The maximum value that the sequence can generate. If specifying NOMAXVALUE, the maximum value is 2 63 -1. MINVALUE The minimum value that the sequence can generate. If specifying NOMINVALUE, the minimum value is -2 63 . CYCLE Specify CYCLE to indicate that when the maximum value is reached the sequence starts over again at the start value. Specify NOCYCLE to generate an error upon reaching the maximum value. Dropping a Sequence To drop a sequence, execute a DROP SEQUENCE statement. Use this function when a sequence is no longer useful, or to reset a sequence to an older number. To reset a sequence, first drop the sequence and then recreate it. Drop a sequence following this format: DROP SEQUENCE my_sequence Using a Sequence Use sequences when an application requires a unique identifier. INSERT statements, and occasionally UPDATE statements, are the most common places to use sequences. Two "functions" are available on sequences: NEXTVAL: Returns the next value from the sequence. CURVAL: Returns the value from the last call to NEXTVAL by the current user during the current connection. For example, if User A calls NEXTVAL and it returns 124, and User B immediately calls NEXTVALgetting 125, User A will get 124 when calling CURVAL, while User B will get 125 while calling CURVAL. It is important to understand the connection between the sequence value and a particular connection to the database. The user cannot call CURVAL until making a call to NEXTVAL at least once on the connection. CURVAL returns the current value returned from the sequence on the current connection, not the current value of the sequence. Examples To create the sequence: CREATE SEQUENCE customer_seq INCREMENT BY 1 START WITH 100
To use the sequence to enter a record into the database: INSERT INTO customer (cust_num, name, address) VALUES (customer_seq.NEXTVAL, 'John Doe', '123 Main St.')
To find the value just entered into the database: SELECT customer_seq.CURVAL AS LAST_CUST_NUM
SAVEPOINT Statement The SAVEPOINT statement names and marks the current point in the processing of a transaction. With the ROLLBACK TO statement, savepoints undo parts of a transaction instead of the whole transaction. For more information, see "Overview of Transaction Processing in PL/SQL". The SQL SAVEPOINT statement can be embedded as static SQL in PL/SQL. For syntax details on the SQL SAVEPOINT statement, see Oracle Database SQL Reference. Usage Notes A simple rollback or commit erases all savepoints. When you roll back to a savepoint, any savepoints marked after that savepoint are erased. The savepoint to which you roll back remains. You can reuse savepoint names within a transaction. The savepoint moves from its old position to the current point in the transaction. If you mark a savepoint within a recursive subprogram, new instances of the SAVEPOINT statement are executed at each level in the recursive descent. You can only roll back to the most recently marked savepoint. An implicit savepoint is marked before executing an INSERT, UPDATE, or DELETE statement. If the statement fails, a rollback to the implicit savepoint is done. Normally, just the failed SQL statement is rolled back, not the whole transaction; if the statement raises an unhandled exception, the host environment (such as SQL*Plus) determines what is rolled back. Examples Example Using SAVEPOI NT With ROLLBACK CREATE TABLE emp_name AS SELECT employee_id, last_name, salary FROM employees; CREATE UNIQUE INDEX empname_ix ON emp_name (employee_id);
DECLARE emp_id employees.employee_id%TYPE; emp_lastname employees.last_name%TYPE; emp_salary employees.salary%TYPE; BEGIN SELECT employee_id, last_name, salary INTO emp_id, emp_lastname, emp_salary FROM employees WHERE employee_id = 120; UPDATE emp_name SET salary = salary * 1.1 WHERE employee_id = emp_id; DELETE FROM emp_name WHERE employee_id = 130; SAVEPOINT do_insert; INSERT INTO emp_name VALUES (emp_id, emp_lastname, emp_salary); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN ROLLBACK TO do_insert; DBMS_OUTPUT.PUT_LINE('Insert has been rolled back'); END;
Reusing a SAVEPOI NT With ROLLBACK CREATE TABLE emp_name AS SELECT employee_id, last_name, salary FROM employees; CREATE UNIQUE INDEX empname_ix ON emp_name (employee_id);
DECLARE emp_id employees.employee_id%TYPE; emp_lastname employees.last_name%TYPE; emp_salary employees.salary%TYPE; BEGIN SELECT employee_id, last_name, salary INTO emp_id, emp_lastname, emp_salary FROM employees WHERE employee_id = 120; SAVEPOINT my_savepoint; UPDATE emp_name SET salary = salary * 1.1 WHERE employee_id = emp_id; DELETE FROM emp_name WHERE employee_id = 130; SAVEPOINT my_savepoint; -- move my_savepoint to current poin INSERT INTO emp_name VALUES (emp_id, emp_lastname, emp_salary); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN ROLLBACK TO my_savepoint; DBMS_OUTPUT.PUT_LINE('Transaction rolled back.'); END; /
SAVEPOINT, ROLLBACK TO SAVEPOINT, and RELEASE SAVEPOINT Syntax SAVEPOINT identifier ROLLBACK [WORK] TO [SAVEPOINT] identifier RELEASE SAVEPOINT identifier InnoDB supports the SQL statements SAVEPOINT, ROLLBACK TO SAVEPOINT, RELEASE SAVEPOINT and the optional WORK keyword for ROLLBACK. The SAVEPOINT statement sets a named transaction savepoint with a name of identifier. If the current transaction has a savepoint with the same name, the old savepoint is deleted and a new one is set. The ROLLBACK TO SAVEPOINT statement rolls back a transaction to the named savepoint without terminating the transaction. Modifications that the current transaction made to rows after the savepoint was set are undone in the rollback, but InnoDB does not release the row locks that were stored in memory after the savepoint. (For a new inserted row, the lock information is carried by the transaction ID stored in the row; the lock is not separately stored in memory. In this case, the row lock is released in the undo.) Savepoints that were set at a later time than the named savepoint are deleted. If the ROLLBACK TO SAVEPOINT statement returns the following error, it means that no savepoint with the specified name exists: ERROR 1305 (42000): SAVEPOINT identifier does not exist The RELEASE SAVEPOINT statement removes the named savepoint from the set of savepoints of the current transaction. No commit or rollback occurs. It is an error if the savepoint does not exist. All savepoints of the current transaction are deleted if you execute a COMMIT, or a ROLLBACK that does not name a savepoint. A new savepoint level is created when a stored function is invoked or a trigger is activated. The savepoints on previous levels become unavailable and thus do not conflict with savepoints on the new level. When the function or trigger terminates, any savepoints it created are released and the previous savepoint level is restored.
CREATE FUNCTION Purpose Use the CREATE FUNCTION statement to create a standalone stored function or a call specification. A stored function (also called a user function or user defined function) is a set of PL/SQL statements you can call by name. Stored functions are very similar to procedures, except that a function returns a value to the environment in which it is called. User functions can be used as part of a SQL expression. A call specification declares a Java method or a third-generation language (3GL) routine so that it can be called from SQL and PL/SQL. The call specification tells Oracle Database which Java method, or which named function in which shared library, to invoke when a call is made. It also tells the database what type conversions to make for the arguments and return value. Creating a Function: Examples
The following statement creates the function get_bal on the sample table oe.orders (the PL/SQL is in italics): CREATE FUNCTION get_bal(acc_no IN NUMBER) RETURN NUMBER IS acc_bal NUMBER(11,2); BEGIN SELECT order_total INTO acc_bal FROM orders WHERE customer_id = acc_no; RETURN(acc_bal); END; /
The get_bal function returns the balance of a specified account. When you call the function, you must specify the argument acc_no, the number of the account whose balance is sought. The datatype of acc_no is NUMBER. The function returns the account balance. The RETURN clause of the CREATE FUNCTION statement specifies the datatype of the return value to be NUMBER. The function uses a SELECT statement to select the balance column from the row identified by the argument acc_no in the orders table. The function uses a RETURN statement to return this value to the environment in which the function is called. The function created in the preceding example can be used in a SQL statement. For example: SELECT get_bal(165) FROM DUAL;