REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
Oracle sequences (AUTONUMBER)
In Oracle/PLSQL, you can create autonumbering using a sequence. A sequence is an Oracle object that is used to generate a number sequence. This can be useful when you need to create a unique number as a primary key.
CREATE SEQUENCE
Syntax
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;
- sequence_name of the sequence you want to create.
CREATE SEQUENCE supplier_seq
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;
This code will create a sequence object called supplier_seq. The first number of the sequence is 1, each subsequent number will be incremented by 1 (i.e., 2,3,4, …). This will cache up to 20 values for performance.
If you omit the MAXVALUE parameter, your default sequence will be to:
MAXVALUE 999999999999999999999999999
In this way, you can simplify the CREATE SEQUENCE. By writing the following:
CREATE SEQUENCE supplier_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
Now that you have created a sequence object to auto-number the counter field, we will look at how to get a value from that sequence object. To get the next value, you need to use NEXTVAL.
For example, you can use NEXTVAL:
supplier_seq.NEXTVAL;
This will allow you to extract the next value from the supplier_seq sequence. You need to use the NEXTVAL clause in your SQL query. For example:
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(supplier_seq.NEXTVAL, 'Kraft Foods');
This insert request will insert a new entry into the table (suppliers). The Supplier_id field will be assigned the following number from the supplier_seq sequence. The supplier_name field will be set to ‘Kraft Foods’.
DROP SEQUENCE
Once you have created a sequence in Oracle, you may need to remove it from the database.
Syntax
DROP SEQUENCE sequence_name;
- sequence_name is the name of the sequence you want to delete.
Let’s look at an example of how to remove a sequence in Oracle.
DROP SEQUENCE supplier_seq;
This example will remove the sequence supplier_seq.
FAQS
Q: When creating a sequence, what do the cache and nocache options mean? For example, you could create a sequence with the cache 20 option as follows:
CREATE SEQUENCE supplier_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
Or you could create the same sequence, but with the nocache option:
CREATE SEQUENCE supplier_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
NOCACHE;
A:As for the sequence, the cache option determines how many sequence values will be stored in memory for quick access.
The disadvantage of creating a sequence with cache is that if a system failure occurs, all cached sequence values that were not used will be lost. This will cause gaps in the values assigned to the sequence. When the system recovers, Oracle will cache new numbers from where the sequence was interrupted, ignoring the lost sequence values.
Note: To recover lost sequence values, you can always execute the ALTER SEQUENCE command to reset the counter to the correct value. nocache means that none of the sequence values are stored in memory. This option may reduce performance, however, you should not experience any gaps in the values assigned to the sequence.
Q: How do I set the lastvalue value to an Oracle sequence?
A: You can change the lastvalue for an Oracle sequence by executing the ALTER command in the sequence.
For example, if the last value of the Oracle sequence in use was 100 and you want the next value to be 225. You must execute the following commands.
ALTER SEQUENCE seq_name
IT'S BY 124;
SELECT seq_name.nextval FROM dual;
ALTER SEQUENCE seq_name.
IT'S BY 1;
The next sequence value to use is now 225.
SQL tutorial: SEQUENCE in Oracle Database
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...