Those who have used PostgresSQL usually gets lazy when write the SQL code to auto-numeric fields, but we miss that feature when we are in a MySQL database.
As we know, MySQL have auto-increment fields, that are very useful too, but we miss that feature when we are in a PostgreSQL database.
I will try to explain how to simulate sequences like PostgreSQL in MySQL. But in what situations we would need sequences in MySQL? some ideas:
- When we need more than one auto-numeric field in the same table.
- When we need a global counter to use in more than one table, procedures, etc.
- I can’t think more situations, but I’m sure at some point we can found one.
OK let’s start:
We need to create a table to store the current sequence value (if this table is created inside mysql database is not bad idea)
create table _sequence ( …[Read more]