Existing tables in MySQL can face the issue of having non-unique fields that can lead to data integrity problems. To rectify this, it becomes necessary to enforce uniqueness on such fields.
To make an existing field unique, the following syntax can be used:
ALTER TABLE <table_name> ADD UNIQUE (<field_name>);
Note that for MySQL 5.7.4 or later, the IGNORE clause for ALTER TABLE is removed, so the syntax becomes:
ALTER TABLE <table_name> ADD UNIQUE (<field_name>);
Important: For versions 5.7.4 and above, it's essential to remove duplicate entries before adding the unique constraint, as the IGNORE keyword is no longer supported.
The above is the detailed content of How to Make an Existing Field Unique in MySQL?. For more information, please follow other related articles on the PHP Chinese website!