Making an Existing Field Unique in MySQL
In some instances, an existing table may require a field to be uniquely indexed. This ensures that each row in the table has a distinct value for that field, preventing duplicate entries.
How to Implement a Unique Constraint
To make a field unique, you can use the following SQL statement:
ALTER IGNORE TABLE [mytbl_name] ADD UNIQUE ([column_name]);
MySQL 5.7.4 and Later
For MySQL versions 5.7.4 and later, the IGNORE clause has been removed. As such, the syntax for adding a unique constraint becomes:
ALTER TABLE [mytbl_name] ADD UNIQUE ([column_name]);
Important Note
Before adding a unique constraint, it's crucial to remove any duplicate entries that may exist in the table. The IGNORE clause, which was previously used to ignore duplicate rows, is no longer supported.
Reference:
For more information on creating unique indexes, refer to the official MySQL documentation.
The above is the detailed content of How to Make an Existing MySQL Field Unique?. For more information, please follow other related articles on the PHP Chinese website!