Error
Error Code: 1169

MySQL Error 1169: Unique Constraint Violation

📦 MySQL
📋

Description

Error 1169 occurs when an attempt is made to insert or update data into a MySQL table, but the operation would result in a duplicate value in a column (or set of columns) that has been defined with a UNIQUE constraint. This error prevents data integrity violations by enforcing uniqueness where specified.
💬

Error Message

Can't write, because of unique constraint, to table '%s'
🔍

Known Causes

4 known causes
⚠️
Duplicate Data Entry
Attempting to insert a new row where a value in a column with a UNIQUE constraint already exists in another row in the table.
⚠️
Updating to Existing Value
Modifying an existing row's unique column to a value that is already present in a different row within the same table.
⚠️
Batch Operation Duplicates
Executing a multi-row INSERT or UPDATE statement where at least two rows in the batch, or a row in the batch and an existing row, violate a unique constraint.
⚠️
Application Logic Error
The application code attempts to write data without first checking for existing unique values, leading to a constraint violation.
🛠️

Solutions

4 solutions available

1. Identify and Correct Duplicate Data medium

Find and remove or modify existing rows that violate the unique constraint.

1
Identify the table and the columns involved in the unique constraint. The error message usually specifies the table name. You'll need to infer the columns from the table schema or recent data insertion attempts.
2
Query the table to find rows that have the same values in the columns covered by the unique constraint. Replace `your_table_name` and `column1`, `column2` with your actual table and column names.
SELECT column1, column2, COUNT(*) FROM your_table_name GROUP BY column1, column2 HAVING COUNT(*) > 1;
3
Once duplicate rows are identified, decide whether to delete the duplicates or update them to have unique values. If deleting, be cautious and ensure you're removing the correct rows. For example, to delete duplicates, keeping one of them (e.g., the one with the lowest ID if an `id` column exists):
DELETE t1 FROM your_table_name t1 INNER JOIN your_table_name t2 WHERE t1.id > t2.id AND t1.column1 = t2.column1 AND t1.column2 = t2.column2;
4
If updating, modify the duplicate values to be unique before attempting the insert again.
UPDATE your_table_name SET column2 = 'new_unique_value' WHERE id = some_id;

2. Temporarily Disable and Re-enable Unique Constraint advanced

Disable the constraint, perform the operation, and then re-enable it. Use with extreme caution.

1
Identify the name of the unique constraint. You can find this by describing the table: `DESCRIBE your_table_name;` or by querying `SHOW CREATE TABLE your_table_name;` and looking for `UNIQUE KEY` definitions. Let's assume the constraint name is `uq_column1_column2`.
SHOW CREATE TABLE your_table_name;
2
Disable the unique constraint. Replace `your_table_name` and `uq_column1_column2` with your actual table and constraint names.
ALTER TABLE your_table_name DISABLE KEYS;
3
Perform your data insertion or update operation that was failing.
INSERT INTO your_table_name (column1, column2) VALUES ('value1', 'value2');
4
Re-enable the unique constraint. This will also re-validate the existing data against the constraint.
ALTER TABLE your_table_name ENABLE KEYS;

3. Modify or Drop the Unique Constraint medium

Alter the constraint to be less restrictive or remove it if it's no longer needed.

1
Identify the name of the unique constraint. Use `SHOW CREATE TABLE your_table_name;` to find the constraint name (e.g., `uq_column1_column2`).
SHOW CREATE TABLE your_table_name;
2
If the constraint is too restrictive (e.g., it's unique on columns that should allow duplicates), you can modify it. This typically involves dropping the existing constraint and creating a new one. First, drop the constraint. Replace `your_table_name` and `uq_column1_column2`.
ALTER TABLE your_table_name DROP INDEX uq_column1_column2;
3
If you still need a unique constraint but on a subset of columns or with different properties, create a new one. For example, to create a unique constraint on `column1` only:
ALTER TABLE your_table_name ADD UNIQUE INDEX uq_column1 (column1);
4
If the unique constraint is no longer required, you can drop it entirely.
ALTER TABLE your_table_name DROP INDEX uq_column1_column2;

4. Ensure Application Logic Handles Uniqueness medium

Modify your application code to check for existing data before attempting to insert.

1
Before executing an `INSERT` statement, perform a `SELECT` query to check if a record with the proposed unique values already exists. Replace `your_table_name`, `column1`, `column2`.
SELECT COUNT(*) FROM your_table_name WHERE column1 = 'value1' AND column2 = 'value2';
2
In your application code (e.g., Python, Java, PHP), check the result of the `SELECT` query. If the count is greater than 0, do not proceed with the `INSERT` and handle the situation (e.g., inform the user, update the existing record if appropriate).
if count == 0:
    # Proceed with INSERT statement
3
Alternatively, consider using `INSERT IGNORE` or `INSERT ... ON DUPLICATE KEY UPDATE` if your MySQL version supports it and your use case allows for it. `INSERT IGNORE` will silently discard duplicate rows. `INSERT ... ON DUPLICATE KEY UPDATE` will update the existing row if a duplicate is found.
INSERT IGNORE INTO your_table_name (column1, column2) VALUES ('value1', 'value2');

-- Or for updating:
INSERT INTO your_table_name (column1, column2) VALUES ('value1', 'value2') ON DUPLICATE KEY UPDATE column2 = 'new_value';
🔗

Related Errors

5 related errors