Generate Auto Id In Postgresql
User Table: ID Name 1 usr1 2 usr2 3 usr3 In the above table, ID is a primary key. My requirement is while inserting data into the table, I would like to spe
Solution 1:
Use the built-in data type serial or bigserial.
createtable users (
id serial primary key,
name varchar(100) notnullunique-- ?
);
Name the column(s); omit the name of the serial or bigserial column.
insertinto users (name) values ('usr4');
The general rule is that you have to supply one value for each column used in an INSERT statement. If you don't specify column names, you have to supply a value for every column, including "Id", and you have to supply them in the order the columns appear in the table.
If you specify column names, you can omit columns that have defaults and columns that are nullable, and you can put the column names in any order. The order of the values has to match the order of the column names you specify.
Post a Comment for "Generate Auto Id In Postgresql"