If you want to avoid that the ID of what you are inserting matches existing records, in the database you can use AUTO_INCREMENT
when defining your table. For example:
CREATE TABLE usuarios (
id int AUTO_INCREMENT,
nombre varchar(256) NOT NULL,
apellidos varchar(256) NOT NULL,
PRIMARY KEY (id),
);
In this way when you insert a new record you just have to specify the fields nombre
and apellidos
, the ID would be generated automatically.
If you can not use AUTO_INCREMENT
you would have to do a SELECT MAX(id) FROM usuarios
to recover the id
bigger than you have and then when inserting add +1 to the value of said id. Although this last option would not be highly recommended if many users are going to use the application simultaneously.