Retrieve last row inserted into table with non sequential primary key - MySql

4

Is it possible to know which last row was inserted into a MySql table where the primary key is not sequential?

I have a table where the primary key is made up of two columns that are FK, so they do not follow a sequence.

When executed the query select last_insert_id(); is returned 0 (It makes sense, because no ID has been entered anyway).

The best way out is to put a same ID column?

    
asked by anonymous 23.08.2016 / 16:08

1 answer

1

Add a column with the insertion date:

ALTER TABLE tabela ADD insercao DATETIME DEFAULT CURRENT_TIMESTAMP;

Or an auto-increment id:

ALTER TABLE tabela ADD id INTEGER NOT NULL AUTO_INCREMENT;
    
31.10.2016 / 20:45