Smart Systems for Home

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

SQL Server Error 1758: Fix the Single Constraint Rule

· 5 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

Error 1758 means your ALTER TABLE statement is trying to add more than one constraint in a single command, and SQL Server only allows one per ALTER TABLE. The fix is to split the statement into one ALTER TABLE per constraint, or to combine the logic into a single constraint definition where that makes sense. Nothing needs to be installed; you just need access to run the corrected T-SQL.

What You'll Need

  • The original failing T-SQL statement (from your script, migration file, or query window).
  • Permission to run ALTER TABLE on the target table (typically db_owner or ALTER permission).
  • A way to test on a development or staging copy of the database before running against production.
  • A backup or a scripted rollback plan if the table holds important data.

Step-by-Step

  1. Read the full error message. Error 1758 usually reads something like "Only a single constraint can be added to a table in a single ALTER TABLE statement." Note the line number and the statement it points to. That tells you exactly which ALTER TABLE is at fault.
  2. Locate the offending statement. Open the script or stored procedure and find the ALTER TABLE that contains more than one ADD clause, for example ALTER TABLE dbo.Orders ADD CONSTRAINT PK_Orders PRIMARY KEY (OrderID), CONSTRAINT UQ_Orders UNIQUE (OrderNumber);. The comma between the two ADD clauses is what triggers the error.
  3. Split it into separate statements. Rewrite so each ALTER TABLE adds exactly one constraint: ALTER TABLE dbo.Orders ADD CONSTRAINT PK_Orders PRIMARY KEY (OrderID); ALTER TABLE dbo.Orders ADD CONSTRAINT UQ_Orders UNIQUE (OrderNumber); Run them one after another, not as a single batch with a shared ADD.
  4. Handle mixed operations separately. If the statement also adds a column, a default, or a check, put each in its own ALTER TABLE. A column add and a constraint add cannot share one ADD list either.
  5. Check for a single constraint that covers the logic. If you were trying to add a composite primary key plus a foreign key, those are genuinely two constraints and must be split. If you were adding two check conditions that could be expressed as one, combine them with AND inside a single CHECK constraint instead.
  6. Test in a non-production database. Run the corrected script against a copy. Confirm the constraints appear under the table's Keys or Constraints node in SQL Server Management Studio (SSMS) or Azure Data Studio, or query sys.objects filtered by the table's object_id.
  7. Apply to production. Run the statements in order. If the table is large, adding a primary key or unique constraint can take time and locks; schedule it in a maintenance window and watch for blocking.
  8. Verify and record. Re-run your original test query or application flow. Keep the corrected script in source control so the same error does not return on the next deployment.

Common Mistakes

  • Leaving a trailing comma. After splitting, a stray comma before the semicolon still produces a syntax error, which can look similar at a glance.
  • Splitting but reusing the same constraint name. Each constraint in a schema needs a unique name; if you copy-paste, rename the second one.
  • Assuming the error is about data. Error 1758 is a syntax/statement-shape error, not a duplicate-key or NULL data error. Fix the statement first, then deal with any data issues that surface.
  • Forgetting dependent objects. Adding a primary key to a table referenced by foreign keys may require the referencing tables to be handled in the right order.
  • Running the whole batch in one transaction without checking. If one statement fails, the rest may be rolled back or left half-applied depending on your error handling. Use explicit transactions and check results.

Tips

  • Write one ALTER TABLE per constraint as a habit; it makes scripts easier to read and easier to debug when one fails.
  • Name every constraint explicitly rather than letting SQL Server generate names, so future scripts and rollbacks are predictable.
  • Use IF NOT EXISTS checks against sys.objects or sys.key_constraints if your script may be re-run.
  • Keep DDL scripts in version control and review them in pull requests; this error is almost always caught at review time.
  • For very large tables, consider adding the constraint during a low-traffic window and monitor lock waits.

Troubleshooting

If the error persists after splitting, check the exact statement the error points to; sometimes the failing line is a generated script or a dynamic SQL string, not the file you edited. If you get a different error after splitting, such as a duplicate key or NULL violation, that is a data problem: clean or de-duplicate the data, then re-run. If the constraint name already exists, drop or rename the old one first. If the table is referenced by foreign keys and the add fails, check the referencing tables' definitions. If you are unsure whether a constraint was applied, query sys.key_constraints and sys.check_constraints joined to sys.tables for the table name.

Frequently Asked Questions

Can I add a primary key and a unique constraint in one ALTER TABLE?

No. SQL Server allows only one constraint per ALTER TABLE statement, so use two separate statements.

Does error 1758 mean my data is corrupted?

No. It is a statement-shape error: the ALTER TABLE contains more than one constraint. Fix the T-SQL, then check for data issues separately.

Can I combine two CHECK conditions into one constraint?

Yes, if they apply to the same row and can be expressed together with AND inside a single CHECK constraint. Otherwise split them into separate ALTER TABLE statements.

Why did my script work before and now fails?

Likely the script was edited, regenerated, or a tool combined statements. Compare the current script with the last known-good version and split any combined ADD clauses.

Does this error apply to Azure SQL Database and SQL Server on Linux?

Yes. The one-constraint-per-ALTER TABLE rule is part of the T-SQL language and applies across SQL Server versions and Azure SQL Database.

How do I confirm the constraint was added?

In SSMS or Azure Data Studio, expand the table's Keys or Constraints node, or query sys.key_constraints and sys.check_constraints filtered by the table.