Thursday, August 29, 2013

Schema conversion - AUTO_INCREMENT (MySQL) and SEQUENCE (PostgreSQL)

Auto increment is achieved in MySQL using the keyword AUTO_INCREMENT. It is achieved in PostgreSQL using a SEQUENCE statement. Consider the following problem: Create a table with a field, to auto increment starting with the value 5.

MySQL
CREATE TABLE hello (
    idHello int NOT NULL AUTO_INCREMENT
) AUTO_INCREMENT=5;


PostgreSQL
CREATE SEQUENCE hello_idHello_sequence START 5;
CREATE TABLE hello (
    idHello integer NOT NULL DEFAULT nextval('hello_idHello_sequence')
);
ALTER SEQUENCE hello_idHello_sequence OWNED BY hello.idHello;

No comments:

Post a Comment