user3463248
user3463248

Reputation: 65

MySQL - Update if all records are equal EXCEPT ID

I have a database, each row assigned an AUTO_INCREMENT ID. I am trying to only INSERT a row when it does not exist already, however currently I am finding using INSERT IGNORE that the database adds a new row, I believe because the ID's are equal? How would I get a new row to only be inserted when all columns are equal except for the ID?

Upvotes: 0

Views: 222

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269623

You would create a unique index on the rest of the columns:

create unique index idx_table_allcols on table(col1, col2, . . . );

Then your insert statement will check that new values are being inserted. The INSERT IGNORE will not insert the record and will not generate an error.

Upvotes: 1

Related Questions