I defined an attribute in my table of type serial4, so when I insert a new row, the attribute is automatically incremented:
myTable (id serial4, name varchar(25), whatever whatever, ...);
To insert a new entry, I just strip off the id attribute in the association list:
insert into myTable (name, whatever, ...) values ('foo', ...);
Now, if my table has a large number of attributes, the association list is exhausting to write.
I just want to write:
insert into myTable values (...);
But if the first attribute of myTable is the serial4 id. What should I write in the VALUES list to get the id being incremented +1 from the last inserted value?
insert into myTable values (?, 'foo', ...);
The
INSERTcommand accepts the key wordDEFAULT:The
serialpseudo data type (synonym:serial4) is just a notational convenience for anintegercolumn that retrieves its default values from an attachedSEQUENCE. By using theDEFAULTkey word, the next value from the sequence is inserted like any other default value.Details in the manual.
Aside: it’s typically not advisable to omit the target column list in persisted code. Any later change to the table will break that code it confusing ways.