Other database objects.

Sequences.

A sequence is a named sequential number generator. Sequences are often used for artificial keys.

Creating sequences.

To create a sequence Oracle uses the CREATE SEQUENCE command with the following syntax:
CREATE SEQUENCE [schema.]sequence_name
  [ INCREMENT BY integer_value ]
  [ START WITH integer_value ]
  [ MAXVALUE integer_value | NOMAXVALUE ]
  [ minvalue integer_value | nominvalue ]
  [ CYCLE | NOCYCLE ]
  [ CACHE integer_value | NOCACHE]
  [ ORDER | NOORDER ];
Where: Examples:
CREATE SEQUENCE MyFirstSequence;
CREATE SEQUENCE MySecondSequence
  INCREMENT BY 3
  START WITH 100;
CREATE SEQUENCE MyThirdSequence
  MAXVALUE 40
  MINVALUE 30
  INCREMENT BY 3
  CACHE 3
  CYCLE;

To drop a sequence, use the DROP SEQUENCE command. The syntax of the command is simple

DROP SEQUENCE sequence_name;

Using sequences.

To use a sequence we can use two of its properties: NextVal and CurrVal. The NextVal attached to a sequence name tells Oracle that you want the next available sequence number from this particular sequence. This is guaranteed to be unique; Oracle will not give it to anyone else, even if it gets a request at the same time. For example,
INSERT INTO test_table (MyFirstSeq.nextval, 'first value');

Potential problems with sequences

To use the same number more than once (such as insert into related tables), CurrVal value is used. Although, this value is set after the first use of NextVal; that is, initially this value is NULL. Please execute the following commands as an example:
CREATE SEQUENCE TestSeq;
SELECT TestSeq.currtval FROM dual;
SELECT TestSeq.nextval FROM dual;
SELECT TestSeq.currtval FROM dual;
DROP SEQUENCE TestSeq;

Another problem can occur when we reached MAXVALUE on an ascending sequence (or MINVALUE on a descending sequence). If the sequence is set to NOCYCLE, Oracle will raise an exception if we try to select nextval after the sequence reaches MAXVALUE. To illustrate this, execute the following commands

CREATE SEQUENCE TestMaxSeq MAXVALUE 5 NOCYCLE;
SELECT TestMaxSeq.nextval FROM employees;
DROP SEQUENCE TestMaxSeq;

Oracle dictionary about sequences

To view all your sequences you can use inner Oracle view USER_SEQUENCES that includes the following information about the sequences of the user running the select statement:

  • name of the sequence
  • min value
  • max value
  • increment value
  • cyclic or not
  • ordered or not
  • size of the cache
  • last number given.

    If you are interested in all sequences created in Oracle or in sequences of a particular user you may use service Oracle table ALL_SEQUENCES that contains all the information above and the name of the user who own a sequence.

    Altering sequences

    To change already defined sequence we can use the ALTER SEQUENCE command. The general syntax of the command is
    ALTER SEQUENCE [schema.]sequence_name
      [ INCREMENT BY integer_value ]
      [ MAXVALUE integer_value | NOMAXVALUE ]
      [ minvalue integer_value | nominvalue ]
      [ CYCLE | NOCYCLE ]
      [ CACHE integer_value | NOCACHE]
      [ ORDER | NOORDER ];
    
    It's easy to notice that we can alter any characteristic of a sequence but the start value. This value can be set only by the CRAETE SEQUENCE command.

    Here is an example:

    CREATE SEQUENCE TestSeq;
    ALTER SEQUENCE TestSeq MAXVALUE 9999 CYCLE;
    

    Synonyms.

    Synonyms are aliases for other database objects. A synonym can point to a table, view, sequence, procedure, function, or package. Unlike views or stored SQL, synonyms do not become invalid if the objects they point to are dropped. Synonyms can be

    To create a synonym we use the CREATE SYNONYM statement. Here is the syntax

    CREATE [PUBLIC] SYNONYM synonym_name
       FOR [schema.]object[@db_link];
    
    For example
    CREATE PUBLIC SYNONYM employees FOR ist466.employees;
    

    To remove a synonym use the DROP SYNONYM statement:

    DROP [ PUBLIC ] SYNONYM synonym_name;
    

    Use the keyword PUBLIC to remove public synonyms.

    When a code references an unqualified table, view, procedure, function, or package, Oracle resolves the object references in this order:

    1. An object owned by the current user.
    2. A private synonyms own by the current user.
    3. A public synonym.
    For example, if user Bob has a table employees, then the query
    SELECT *
      FROM employees;
    
    executed by Bob will select from his own table, not from the table ist466.employees referenced employees.
    Private synonym can be referenced by anyone who has privileges on the underlying object. Privileges cannot be granted on a synonym, only on the underlying object.

    Indexes.

    Indexes are data structure that can offer improved performance in obtaining specific rows over the default full-table scan. Oracle provides two types of indexes: B-tree and bitmap.

    Oracle usually retrieves rows from a table in only one of two ways:

    Both B-tree and bitmap indexes map column data to ROWIDs for the column of interest, but they do it in different ways. When one or more indexes are accessed, Oracle will use the known column values to find the corresponding ROWIDs. The rows are then retrieved by ROWID. Indexes may improve the performance of SELECT, UPDATE, and DELETE operations. An index can be used if a leading subset of the indexed columns appear in the SELECT or WHERE clause.

    B-tree indexes are the most common index type (this is the default type). These indexes can be either UNIQUE or nonunique and either simple (one column) or concatenated (multiple columns). These indexes provide the best performance on high-cardinality columns (columns that have many distinct values). B-tree indexes offer an efficient method to retrieve a small number of interesting rows. However, if more than about 5 percent of the table must be examined, a full-table scan is the preferred method.

    B-tree indexes are beneficial only if a small percentage of a table is expected to be returned in query results. Because having an index requires that Oracle first examine the index to identify records that meet the criteria and then to retrieve the rows from the actual tables, large result sets require Oracle to do additional work. Basically, a large result set requires Oracle to scan the full table. The general rule is

    If more than five percent of a table's rows are expected to be returned by a query, then an index probably will not speed up row retrieval.

    Bitmap indexes are primarily used for decision-support systems or static data, because they do not support row-level locking. Like B-tree indexes bitmap indexes can be simple (one column) or concatenated (multiple columns), but in practice, bitmap indexes are almost always simple. Bitmap indexes are best used for low- to medium-cardinality columns, where multiple bitmap indexes can be combined with AND and OR conditions. Each key value has a bitmap, which contains a TRUE, FALSE, or NULL value for every row in the table.

    For example, the bitmap indexes for the column sex and marital_status in the employees table will look like
     ROWID   Gender   Mar.Status Thus, if we execute the query
    SELECT fname, lname
      FROM employees
     WHERE sex = 'F'
           AND
           mar_status = 'married';
    
    we are interested only in the rows that have 1 in both bitmap indexes. If we have these two bitmap indexes, Oracle doesn't have to scan the entire table. It's enough to read the two index tables and find the addressed of the rows we are interested in.
    AAAHtZAAIAAAAUaAAA 0 1
    AAAHtZAAIAAAAUaAAB 0 0
    AAAHtZAAIAAAAUaAAC 1 1
    AAAHtZAAIAAAAUaAAD 0 1
    AAAHtZAAIAAAAUaAAE 1  
    AAAHtZAAIAAAAUaAAF 1  
    AAAHtZAAIAAAAUaAAG 1 0
    AAAHtZAAIAAAAUaAAH 1 0
    AAAHtZAAIAAAAUaAAI 0 1
    AAAHtZAAIAAAAUaAAJ 1 0
    AAAHtZAAIAAAAUaAAK 0  
    AAAHtZAAIAAAAUaAAL 0 1
    AAAHtZAAIAAAAUaAAM 0 0
    AAAHtZAAIAAAAUaAAN 0 1
    AAAHtZAAIAAAAUaAAO 1 1
    AAAHtZAAIAAAAUaAAP 0 0
    AAAHtZAAIAAAAUaAAQ 1 0
    AAAHtZAAIAAAAUaAAR 0 0
    AAAHtZAAIAAAAUaAAS 0 0
    AAAHtZAAIAAAAUaAAT 1 1
    AAAHtZAAIAAAAUaAAU 0 0
    AAAHtZAAIAAAAUaAAV 1  
    AAAHtZAAIAAAAUaAAW 0 0
    AAAHtZAAIAAAAUaAAX 0 0
    AAAHtZAAIAAAAUaAAY 1 0
    AAAHtZAAIAAAAUaAAZ 1  
    AAAHtZAAIAAAAUaAAa 1 0

    Creating indexes

    First of all, we need to remember that Oracle automatically creates indexes for columns with PRIMARY KEY and UNIQUE constraints. To create index for other columns, we need to use the CREATE INDEX statement. Simple syntax of the statement is
    CREATE [ UNIQUE | BITMAP ] INDEX index_name
      ON [schema.]table_name ( index_expression_list );
    
    where index_expression_list is a list of column names, or some expressions of columns. Please note that we cannot use the keywords BITMAP and UNIQUE together. These are the several examples on creating indexes:
    CREATE BITMAP INDEX employees_sex_indx ON employees (sex);
    CREATE INDEX employees_fullname_indx
      ON employees ( CONCAT(lname,fname) );
    CREATE UNIQUE INDEX employees_name_phone_indx
      ON employees ( lname, phone );
    
    Depending on the type of index, the most appropriate circumstances for creating an index are as follows: Indexes created can be viewed in the USER_INDEXES Oracle dictionary table. For example,
    SELECT index_name "Index", index_type "Type",
           table_owner ||'.'|| table_name "Table",
           table_type "Type", uniqueness
      FROM user_indexes;
    
    This table contains all the indexes created by the current user. Table ALL_INDEXES contains information about all indexes in the database.

    To remove an index, use DROP INDEX stateement

    DROP INDEX index_name;