Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
I have a relational database in which I have demand table. Now I want to add one more field into my table but if I unchecked the 'Allow Null' checkbox then the table save and if I checked the 'Allow Null' it will show me a dialog box containing the error like below..

What I have tried:

C#
'Employees' table saved successfully
'Items' table saved successfully
'Demands' table
- Unable to modify table.  
Cannot insert the value NULL into column 'Purchased_Date', table 'Inventory_Aamir.dbo.Tmp_Demands'; column does not allow nulls. INSERT fails.
The statement has been terminated.


C#
TITLE: Microsoft SQL Server Management Studio
------------------------------

User canceled out of save dialog
 (MS Visual Database Tools)

------------------------------
BUTTONS:

OK
------------------------------
Posted
Updated 22-Dec-16 23:31pm
Comments
Tomas Takac 23-Dec-16 5:06am    
I don't quite follow but when you are adding a new column to a table then it either has to allow nulls or have a default value specified.

1 solution

When you add a column to a table which has existing data, the resulting table must be consistent: what SQL does is effectively create a new table, insert all the old records, then delete the old table and rename the new. If your new columns cannot take a null value, then the insert to the new table fails - because SQL has no other data to insert in that column - so the "add column" operation itself fails to maintain the DB consistency.

You either need to allow null values on the new column, or set the DEFAULT value for the column to a reasonable value so SQL can use that for existing rows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900