Prevent Duplicate SQL entries

后端 未结 3 888
情深已故
情深已故 2020-12-19 20:01

I want to be able to prevent duplicate SQL text field rows. That is, if row 1 has the name field already defined as \"John Smith\", I don\'t want it to be able to add anothe

相关标签:
3条回答
  • 2020-12-19 20:04

    Take a look here, you'll need unique.

    0 讨论(0)
  • 2020-12-19 20:16
    CREATE UNIQUE INDEX idxname ON tablename (fieldname);
    

    Adding this index will ensure that no duplicate entries for fieldname field will be recorded into tablename table.

    You will get a MySQL error with the second client. You should handle this in your PHP code, and put up the form again (instead of just displaying the error message).

    An other possibility (for more complex sitations) is the LOCK functionality. If you lock the table before checking and then you insert your record a concurrent operation (in the second browser window) will be delayed until you release the locks. Then the record will be already saved, so the second PHP script will see it and handle the sitation.

    0 讨论(0)
  • 2020-12-19 20:18

    DISTINCT can also be used to select unique rows.

    ...
    select distinct *
      from table1
    ...
    
    0 讨论(0)
提交回复
热议问题