Sql-server – Insert data into a column where other columns have constraints

sql serversql-server-2012

I want to insert data into the zipcode column of the employee3 table. SQL says write – INSERT INTO Employee3 (ZipCode) VALUES (28279);, however I keep getting this error message:

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'EMPLOYEEID', table
'AdventureWorks2012.dbo.EMPLOYEE3'; column does not allow nulls. 

INSERT fails.

I understand other columns have "NOT NULL" CONSTRAINTS, but I only want to INSERT data to the ZipCode column. Is there anyway around having to update the other columns?

Best Answer

You cannot insert a row without specifying values for columns that have NOT NULL constraints. What would be the point of having NOT NULL constraints on those columns if it wasn't enforced?

You would need to remove the NOT NULL constraint clause for each column in the table in order to insert a row that only contains the Zip code value.

This begs the question: Are you really trying to insert a row that ONLY has a zip code, and nothing else? Or are you actually trying to modify the zip code for already existing rows in the table? If that is the case, you need to use an UPDATE <TABLE> statement, not an INSERT INTO statement.