Is there a way to make an entire MySQL row unique

后端 未结 5 640
醉梦人生
醉梦人生 2021-02-14 04:46

I have a table in MySQL with 30 columns and thousands of entries.
Is there a way I could make every row unique, that means if a row already exists, I should not be able to e

相关标签:
5条回答
  • 2021-02-14 05:04

    You need a composite primary key.

    A composite primary key tells MySQL that you want your primary key to be a combination of fields.

    More info here: Why use multiple columns as primary keys (composite primary key)

    0 讨论(0)
  • 2021-02-14 05:10

    You may create UNIQUE key on all the columns, not individual unique keys on each column. This means that the combination of the values will be unique - exactly what you need. But please note, that if any column allows null value, if the column contains null value, it will be counted as unique, even if another row contains the same values, with null for the same value.

    0 讨论(0)
  • 2021-02-14 05:17

    You can make a unique index that includes all of the columns in your table

    ALTER TABLE buyers ADD UNIQUE idx_row_unique(first_name,last_name,...);
    

    This way you can keep a unique AUTO INCREMENT primary key for join purposes, while still ensuring that all of the data in your table is unique.

    0 讨论(0)
  • 2021-02-14 05:18

    You can hash the data and set the hash value as your PK, this will ensure that the rows are unique.

    0 讨论(0)
  • 2021-02-14 05:21

    You can make a unique index on more than one column. Just put all the columns in the index. If the columns are large you may run into issues with the maximum length of an index, but try it first and see.

    0 讨论(0)
提交回复
热议问题