'invalid for creating a default constraint' error when trying to add a constraint to an existing table

TechCrave

I want to add a default constraint using ALTER TABLE in SQL Server, but I received the below error message instead.

Column 'StartDate' in table 'Register' is invalid for creating a default constraint.

I know that I can declare a constraint when I create a table, but that isn't the situation I am in. I hope someone can help me ;)

Here is my alter statement:

ALTER TABLE [dbo].[Register]
  ADD CONSTRAINT [Register_StartDate] DEFAULT (GETDATE()) FOR StartDate 

And this is my create table script:

CREATE TABLE [dbo].[Register]
  (
     [ID]        [INT] IDENTITY(1, 1) NOT NULL,
     /* ....*/
     [StartDate] [DATETIME] NULL
  ) 

Edited: Fixed: I forgot that the [StartDate] field doesn't even exist in the table. My bad!

Martin Smith

As far as I'm aware there are two possible circumstances that might cause this error.

Attempting to add a default constraint to:

  1. A computed column.
  2. A column that doesn't exist at all!

For the table definition

CREATE TABLE dbo.Register
  (
     ID INT IDENTITY(1, 1) NOT NULL,
     Computed AS 'Foo'
  ) 

Both of the following statements fail with error 1752.

ALTER TABLE dbo.Register
  ADD CONSTRAINT C1 DEFAULT 'X' FOR Computed

ALTER TABLE [dbo].[Register]
  ADD CONSTRAINT [Register_StartDate] DEFAULT (GETDATE()) FOR StartDate 

There are various other conditions on which it is not permissible to add a default constraint to a column but these all have their own unique error numbers and messages.

+------------------------------------+--------------+
|               Reason               | Error number |
+------------------------------------+--------------+
| Column is IDENTITY                 |         1754 |
| Column is timestamp/rowversion     |         1755 |
| Sparse Column                      |         1791 |
| Default constraint already present |         1781 |
+------------------------------------+--------------+

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP MySQL Error creating table: Cannot add foreign key constraint

From Dev

table view disappears when I add a constraint

From Dev

MYSQL ADD CONSTRAINT ERROR

From Dev

SQL oracle add check constraint to an existing table

From Dev

Add a column value from another table as default constraint

From Dev

Default Constraint causing an error

From Dev

Having error for alter table to add constraint

From Dev

How to add default constraint on an existing column in SQL Server

From Dev

add unique constraint to existing table column

From Dev

Add DEFAULT constraint with a generated name

From Dev

Laravel 7, SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed when trying to add a relationship

From Dev

unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails

From Dev

Knex migration down error when trying to drop constraint and alter column

From Dev

Add a default constraint to an existing field with values

From Dev

T-SQL to add a column bound to a existing default constraint

From Dev

SQL oracle add check constraint to an existing table

From Dev

Add existing constraint to a new table

From Dev

Nothing changes when i try to add Check Constraint in my Table

From Dev

How to add default constraint on an existing column in SQL Server

From Dev

add constraint using different table

From Dev

mysql - cannot add foreign key constraint when creating a table

From Dev

How to add extra constraint to table?

From Dev

How to avoid 'unique constraint' error message when creating a view

From Dev

Microsoft SQL Server Management Studio - Trying to add FK constraint, getting error: "Invalid column..."

From Dev

Why can't I add a constraint when creating the table?

From Dev

Add DEFAULT CONSTRAINT at the end of a CREATE TABLE

From Dev

I am getting following error when I try to add constraint

From Dev

When I try to create a table in MySQL, it gives me error code 1215: “Cannot add foreign key constraint”

From Dev

Getting an error when trying to add foreign key constraint using Laravel migrations

Related Related

  1. 1

    PHP MySQL Error creating table: Cannot add foreign key constraint

  2. 2

    table view disappears when I add a constraint

  3. 3

    MYSQL ADD CONSTRAINT ERROR

  4. 4

    SQL oracle add check constraint to an existing table

  5. 5

    Add a column value from another table as default constraint

  6. 6

    Default Constraint causing an error

  7. 7

    Having error for alter table to add constraint

  8. 8

    How to add default constraint on an existing column in SQL Server

  9. 9

    add unique constraint to existing table column

  10. 10

    Add DEFAULT constraint with a generated name

  11. 11

    Laravel 7, SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed when trying to add a relationship

  12. 12

    unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails

  13. 13

    Knex migration down error when trying to drop constraint and alter column

  14. 14

    Add a default constraint to an existing field with values

  15. 15

    T-SQL to add a column bound to a existing default constraint

  16. 16

    SQL oracle add check constraint to an existing table

  17. 17

    Add existing constraint to a new table

  18. 18

    Nothing changes when i try to add Check Constraint in my Table

  19. 19

    How to add default constraint on an existing column in SQL Server

  20. 20

    add constraint using different table

  21. 21

    mysql - cannot add foreign key constraint when creating a table

  22. 22

    How to add extra constraint to table?

  23. 23

    How to avoid 'unique constraint' error message when creating a view

  24. 24

    Microsoft SQL Server Management Studio - Trying to add FK constraint, getting error: "Invalid column..."

  25. 25

    Why can't I add a constraint when creating the table?

  26. 26

    Add DEFAULT CONSTRAINT at the end of a CREATE TABLE

  27. 27

    I am getting following error when I try to add constraint

  28. 28

    When I try to create a table in MySQL, it gives me error code 1215: “Cannot add foreign key constraint”

  29. 29

    Getting an error when trying to add foreign key constraint using Laravel migrations

HotTag

Archive