Unable to add constraint.

Troubleshooting SQL Server

Troubleshooting

Unable to add constraint.

Appears when a new constraint has failed on existing data or your constraint expression contains an error. Compare the ODBC error text that appears in the Save Incomplete dialog box with the error text shown below to determine the appropriate solution.

ODBC error text

[Microsoft][ODBC SQL Server Driver][SQL Server]Data exists in table '[table name]', database '[database name]', that violates CHECK constraint '[constraint name]' being added. ALTER command has been aborted.

[Microsoft][ODBC SQL Server Driver][SQL Server]Unable to create constraint. See previous errors.

Explanation

Existing data does not match the check constraint.

Action
  • Change the data (for example, by using SQL Server Enterprise Manager) to match the constraint.

  • Clear the Check existing data on creation check box in the Tables property page for the check constraint in question.

  • Change the constraint expression in the Tables property page for the check constraint in question.
ODBC error text

[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column '[column name]' specified in constraint definition.

[Microsoft][ODBC SQL Server Driver][SQL Server]Unable to create constraint. See previous errors.

Explanation
  • The text value in the check constraint expression on the Tables property page is not enclosed in single quotation marks (').

  • A column that participates in the check constraint has been renamed. For example, if the original constraint had the expression (cityname = 'Paris') and you renamed the column to city, you would see the same error except that 'cityname' would be the invalid column shown in the error message.
Action

Correct the expression and save the table.

ODBC error text

[Microsoft][ODBC SQL Server Driver][SQL Server]Line [line number]: Incorrect syntax near '[operator]'.

Explanation

The expression defined for the check constraint (in the Tables property page) or the default constraint (in the Default Value cell) is not valid Transact-SQL syntax. For example, the check constraint expression 'city equals Paris' was typed instead of 'city = Paris'.

Action

Correct the expression and save the table.

See Also

Database Designer

Filtering Rows with WHERE and HAVING

Query Fundamentals

Database Objects