3
Personal it has been a long time since I look for a way to do this, what would be the correct way to make an auto increment that depends on another field as in the example below?
id_empresa | id_pedido
1 | 1
1 | 2
1 | 3
1 | 4
2 | 1
2 | 2
3 | 1
I’ve already thought about making only one Rigger and process for all tables, only I don’t know how to do.
A coworker told me to do it this way:
1 This field would not be part of the primary key, so it would be a PK serial id, integer id id FK PK, integer num_ped.
2 I create a sequential table only to reference these ids, for example a record in this table would be, (request, 1) and its columns would be (table, sequence).
3 Every time I place an order I do an UPDATE with RETURNING in the sequential table.
4 Receiving this value I give an INSERT in the requested table
then it wouldn’t be a
id
, but a code that varies according to another column.. therefore, it cannot be auto-incrementable and must be controlled manually (at least as far as I understand postgresql).– rLinhares
@How am I supposed to handle it manually without any competition problems? I need to get this result, would it work smoothly if I did it in an sql only, an Insert referring to the maximum value +1? No need to block the table?
– Felipebros
these two fields are of the same table? if yes, would not need to block it, since the sgbd does so at the time of Insert.
– rLinhares
@But will it do that, block the table, at the time of INSERT INTO request(id_company, id_request) VALUES (1,(SELECT max(id_request) + 1 ON-DEMAND))
– Felipebros
yes, can go that’s success =p Rovann posted as reply ;)
– rLinhares