PostgreSQL Auto Increment

PostgreSQL has the data types smallserial, serial and bigserial;these data types are not real data types, but use a notable feature to create unique identifier columns. These is similar to Auto Increment property.
If you want a serial column to be a unique constraint or a primary key, it should not be specified like any other data type.
The type name serial creates an integer column. The type name bigserial creates a bigint column. Type name shortserial creates a small column


Syntax :

The basic syntax SERIAL dataype is as follows

CREATE TABLE tablename (
colname SERIAL
);


Example :

Consider the COMPANY table to be created-

lfcdb=# CREATE TABLE COMPANY(
ID  SERIAL PRIMARY KEY,
NAME           TEXT      NOT NULL,
AGE            INT       NOT NULL,
ADDRESS        CHAR(50),
SALARY         REAL
);
-----------------------------------------
\\Now, insert the following records into table COMPANY −
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'Prateek', 22, 'Mysore', 20000.00 );
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ('Vaibhav', 20, 'Udaipur', 15528.00 );
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ('Yash', 24, 'Mumbai', 22000.00 );
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'Jugal', 23, 'Delhi', 25000.00 );
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY)
VALUES ( 'Prasun', 19, 'Kolkata', 30000.00 );
------------------------------------------
\\This will insert seven records into the table COMPANY
id | name     | age | address    | salary
----+-------+-----+------------+--------
1 | Prateek  |  22 | Mysore     |  20000
2 | Vaibhav  |  20 | Udaipur    |  15528
3 | Yash     |  24 | Mumbai     |  22000
4 | Jugal    |  23 | Delhi      |  25000
5 | Prasun   |  19 | Kolkata    |  30000





Visit :


Discussion


* You must be logged in to add comment.