I don't completely understand the difference between Oracle's sequence and MySql's auto_increment.
I am a mysql guy, but know nothing of oracle. I'm hoping someone can shine a quick light on this for me.
The difference between auto-increment columns in SQL Server and sequences in Oracle is that: In SQL Server, you mark a column as an auto-increment column and SQL Server automatically generates new values for the column when you insert a new row.
IDENTITY columns were introduced in Oracle 12c, allowing for simple auto increment functionality in modern versions of Oracle. Using the IDENTITY column is functionally similar to that of other database systems.
Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.
MySQL server already provides two auto increment variables: auto_increment_increment and auto_increment_offset, which can be used to generate different auto increment values on each member.
A sequence is a distinct database object in Oracle.
In MySQL, when you have an autoincrement column and you INSERT a new row in a table, you simply don't mention the autoincrement column and MySQL puts it there. You can then insert the same number into another table by referencing LAST_INSERT_ID()
.
INSERT INTO person (name, date) /*MySQL*/
VALUES ('joe', '2015-01-01');
INSERT INTO contact (person_id, phone)
VALUES (LAST_INSERT_ID(), '555-1212');
In Oracle, you can populate an id column by mentioning a sequence object's .nextval
property. You can get the just-used value of that sequence by referencing its .currval
property.
INSERT INTO person (id, name, date) -- Oracle
VALUES (person_seq.nextval, 'joe', '2015-01-01');
INSERT INTO contact (id, person_id, phone)
VALUES (contact_seq.nextval, person_seq.currval, '555-1212');
Each time you mention the sequence's .nextval
property, it's guaranteed to give a new number.
Sequence objects are pretty cool when you need unique numbers that aren't directly related to the primary key of some table or other. You can do this in MySQL, but it's a kludge: If you create the following table:
CREATE TABLE sequence ( /*MySQL*/
sequence_id BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sequence_id`)
)
Then issue these three queries one after the other:
INSERT INTO sequence () VALUES (); /*MySQL*/
DELETE FROM sequence WHERE sequence_id < LAST_INSERT_ID();
SELECT LAST_INSERT_ID() AS sequence;
The third query is guaranteed to return a unique sequence number. This guarantee holds even if you have dozens of different client programs connected to your database. (The DELETE
query merely keeps this otherwise pointless table from taking up too much space.)
With Oracle, you create the sequence
create sequence seq --Oracle
and then just do
SELECT seq.nextval FROM DUAL --Oracle
to get a new sequence number and that's it. It too guarantees uniqueness even with dozens of connected client programs.
Similarly, if you need the value of a sequence you just generated with .nextval
, you can issue this command and get it.
SELECT seq.currval FROM DUAL --Oracle
As with MySQL's LAST_INSERT_ID()
, this is handled session by session so another client using the sequence won't make you get their number instead of yours.
Bottom line: both DBMSs can generate unique integers. The integrity of both schemes is designed to be preserved across server stops and restarts. The Oracle sequence is more flexible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With