How to check if a value already exists to avoid duplicates?

前端 未结 17 1069
小鲜肉
小鲜肉 2020-12-02 23:13

I\'ve got a table of URLs and I don\'t want any duplicate URLs. How do I check to see if a given URL is already in the table using PHP/MySQL?

17条回答
  •  悲哀的现实
    2020-12-02 23:42

    To guarantee uniqueness you need to add a unique constraint. Assuming your table name is "urls" and the column name is "url", you can add the unique constraint with this alter table command:

    alter table urls add constraint unique_url unique (url);
    

    The alter table will probably fail (who really knows with MySQL) if you've already got duplicate urls in your table already.

提交回复
热议问题