Reputation: 13
Like the title says I'm getting the following error when inserting this query into the MySQL Command Line Client
Query
ALTER TABLE physician ADD UNIQUE (ssn);
Error code
ERROR 1062 (23000): Duplicate entry '' for key 'physician.ssn'
I don't know why I'm pulling a duplicate entry error as this shouldn't be a duplicate entry. Any help is appreciated.
Upvotes: 1
Views: 6050
Reputation: 15361
A unique constraint will only allow unique values for that column. Since you already have many rows with NULL, MySQL will not allow you to add the constraint.
You have 2 options:
The syntax would be:
CREATE UNIQUE INDEX physician_ssn_idx ON physician(ssn);
physician_ssn_idx
will be the name of the newly created index, which can be anything you want it to be. If you have been creating index/key using a naming scheme you might want to adopt that. Ultimately, the names of indexes don't effect the functionality at all, and you don't need to know them in order to make use of them.
Upvotes: 1
Reputation: 1780
What it's telling you is that it cannot add this unique constraint. The reason is that in the physician table, there are already entries with same SSN values. You can delete those duplicate records, then run your ALTER statement.
Upvotes: 2