Is there a way to insert a new record to a table which doesn't have an auto-increment ID without specifically entering the ID. I just want the ID to be lastId+1.
INSERT INTO lists VALUES (id,'KO','SPH', '5')
//new id
Is there a way to insert a new record to a table which doesn't have an auto-increment ID without specifically entering the ID. I just want the ID to be lastId+1.
INSERT INTO lists VALUES (id,'KO','SPH', '5')
//new id
Try this:
Alternatively:
If you want not specify ID your field must be autoincrement.
If you use guid as id (sequence) you must specify the field but the value is calculated by DBMS
Don't do that! EVER! Don't even think about doing that!
This WRONG solution may seems (it doesn't) to work for you:
BUT, if someone try to insert at the same time as you, you both would get the same
id
, which will cause an invalid result. You really should use asequence
or some more reliable mechanism (an auxiliary table is common when you can't have holes in the sequence, but it has some drawbacks [it will lock]). You can even useserial
data type to make it easier (it creates a sequence underneath):If you really, really, REALLY, can't create and use a sequence, you can do as the above, but you will have to handle the exception (assuming the
id
field is PK or UK, and using a read committed transaction), something like that (in PL/pgSQL):But again, I highly recommend you to avoid it: use a sequence and be happy... =D
Also, I know it is a example, but use explicit columns list on
INSERT INTO
clause.