Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 1760: Fix Cannot Create Constraint

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server error 1760 means the constraint you are trying to create is not allowed on that column because its definition conflicts with the column's data type or nullability. The fix is to change the constraint type, the column definition, or both so they are compatible. The most common case is a DEFAULT constraint on a column that already has an identity property, or a PRIMARY KEY or UNIQUE constraint on a column that allows NULLs. The error text usually reads: "Constraints of type '%ls' cannot be created on table '%ls' because the column '%ls' is an identity column." The exact wording varies by SQL Server version, but the cause is always a mismatch between the constraint and the column.

What You'll Need

  • SQL Server Management Studio (SSMS) or Azure Data Studio connected to the database, with permission to alter the table (db_owner or ALTER on the table).
  • The name of the table and column from the error message.
  • A way to check the column definition: the table design view, or a query against the system catalog.
  • A backup or a tested script if the table holds production data, because dropping and recreating a column can lose data.

Step-by-Step

  1. Read the full error message. It names the constraint type, the table, and the column. If you only see the generic message, run the failing statement again in SSMS and read the Messages tab. The type is usually DEFAULT, PRIMARY KEY, UNIQUE, FOREIGN KEY, or CHECK.
  2. Inspect the column definition. In Object Explorer, expand the database, expand Tables, right-click the table, and choose Design. Select the column and look at its properties. Alternatively, run a query against sys.columns and sys.types to see the data type, nullability, and whether it is an identity column. For example: SELECT name, is_nullable, is_identity, system_type_id FROM sys.columns WHERE object_id = OBJECT_ID('dbo.YourTable');
  3. Match the constraint to the column. If the error mentions an identity column, you cannot add a DEFAULT constraint to it. Identity columns generate their own values, so a default is redundant and blocked. If the error mentions a PRIMARY KEY or UNIQUE constraint, check whether the column allows NULLs. Primary keys require NOT NULL; unique constraints also reject NULLs in a single-column unique constraint unless a filtered unique index is used instead.
  4. Choose the correct fix. For an identity column with a DEFAULT constraint, remove the DEFAULT clause from your CREATE TABLE or ALTER TABLE statement. For a PRIMARY KEY on a nullable column, either alter the column to NOT NULL first (after filling any NULLs) or choose a different column. For a UNIQUE constraint on a nullable column, use a unique filtered index instead, or make the column NOT NULL. For a FOREIGN KEY, confirm the referenced column is a primary key or unique key and that data types match.
  5. Fix the data before changing nullability. If you need to make a column NOT NULL, update existing NULLs first. For example: UPDATE dbo.YourTable SET YourColumn = 0 WHERE YourColumn IS NULL; Then run ALTER TABLE dbo.YourTable ALTER COLUMN YourColumn int NOT NULL; Adjust the data type and default value to match your schema.
  6. Apply the change in a transaction. Wrap the ALTER TABLE statements in BEGIN TRANSACTION and COMMIT so you can roll back if something fails. Test on a development copy first. If the table is large, adding a NOT NULL column with a default can take time and lock the table; schedule it during a maintenance window.
  7. Re-run the original statement. After correcting the column or the constraint definition, execute your CREATE TABLE or ALTER TABLE statement again. If it still fails, go back to step 1 and check whether another constraint in the same statement has the same problem.

Common Mistakes

  • Adding a DEFAULT to an identity column. Identity columns already generate values; SQL Server rejects the default. Remove the DEFAULT clause.
  • Creating a PRIMARY KEY on a nullable column. The column must be NOT NULL. Update NULLs and alter the column first.
  • Assuming the error is about the table name. The message names the column; read it carefully. The problem is almost always the column definition.
  • Forgetting to check existing constraints. A column may already have a default or be part of another key. Query sys.default_constraints and sys.key_constraints before adding a new one.
  • Altering a column with dependent objects. Indexes, statistics, or foreign keys may block the change. Drop and recreate them, or use the appropriate ALTER syntax.

Tips

  • Use the table design view in SSMS to see column properties at a glance; it flags identity and nullability clearly.
  • Script out the table before changing it: right-click the table, choose Script Table as, CREATE To, and save the file. That gives you a rollback reference.
  • Prefer named constraints so future errors are easier to trace: CONSTRAINT DF_YourTable_YourColumn DEFAULT (0).
  • If you need a unique constraint that allows multiple NULLs, use a filtered unique index: CREATE UNIQUE INDEX IX_YourTable_YourColumn ON dbo.YourTable(YourColumn) WHERE YourColumn IS NOT NULL;
  • Check the SQL Server version because error message wording and some constraint behaviors differ slightly between versions and editions.

Troubleshooting

If the error persists after you remove the DEFAULT clause, confirm you are altering the correct table and column. Run sp_help 'dbo.YourTable' to list columns, types, and existing constraints. If the error changes to a different constraint type, fix that one next; a single statement can trigger multiple 1760 errors. If you cannot alter the column because of dependencies, drop the dependent index or foreign key, make the change, and recreate it. If the table is replicated, schema changes may need to be made at the publisher or with replication disabled; check with your DBA. If you are unsure whether a column is an identity column, query sys.identity_columns. For production tables, always test the full script on a copy first and keep a backup; dropping a column deletes its data permanently.

Frequently Asked Questions

What does SQL Server error 1760 actually mean?

It means the constraint you are creating is not valid for the column because of its data type or nullability. The most common case is a DEFAULT constraint on an identity column.

Can I add a DEFAULT to an identity column?

No. Identity columns generate their own values, so SQL Server blocks a DEFAULT constraint on them. Remove the DEFAULT clause from your statement.

How do I fix a PRIMARY KEY constraint on a nullable column?

Make the column NOT NULL first. Update any NULL values, then run ALTER TABLE ... ALTER COLUMN ... NOT NULL, and then add the primary key.

Does error 1760 happen with foreign keys too?

Yes, if the referenced column is not a primary key or unique key, or if the data types do not match. Check both tables and align the definitions.

Will fixing this error lose data?

Not if you only change the constraint definition. Altering a column to NOT NULL requires updating NULLs first, and dropping a column deletes its data, so back up and test before making structural changes.