I am using an API that returns items to me, and then storing those items in a MySQL database. However, the API does not return a unique ID for each item, so I risk storing the same item twice unless I check for duplicates.
I think the most efficient way to prevent duplicates is having a unique index in MySQL. However, since MySQL cannot have unique indexes for strings, I need to convert one of the strings (probably the URL) to an integer to store in the unique index column. Am I correct in that assumption? How would I go about accomplishing this with PHP?
Thank you.
You can have a VARCHAR-type column that is indexed UNIQUE, it will not be auto-increment though. You could then use REPLACE INTO for your update/insert queries.
Alternately, you can still use an auto-increment integer ID column in combination with a unique index VARCHAR column, you’ll just have to check for a duplication before (or after) an INSERT query to handle the case where a string ID duplicates an existing one.