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:
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;
INSERT INTO test_table (MyFirstSeq.nextval, 'first value');
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;
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:
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.
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;
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:
SELECT * FROM employees;executed by Bob will select from his own table, not from the table ist466.employees referenced employees.
Oracle usually retrieves rows from a table in only one of two ways:
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
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 |
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:
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;