When performing an insert lets say from C# into a SQL Server table (using parameterized sql statements), do you need to specify every table field in the insert statement?
I noticed that the fields that I do not specify in the insert default to the defaults set in the table. I don’t know if that’s good or bad in my insert statement to leave out fields and let the defaults take care of setting the fields I don’t care about. It must be ok because it works.
You need to specify all those field for which you want to insert a value. You do not have to specify all fields in the table!
As you noticed, any of the fields that you do not specify and that do have a default constraint on them will be set to that defined default value. It’s a “good thing” ™ – for sure! This allows you to write less T-SQL insert code – all the defined defaults will be set already. I find this to be a great feature of SQL Server (and lots of other relational databases, too) – you can initialize things like “last modified” date fields to “today” upon insert without having to specifically add those fields to your INSERT statement.
Any fields that are neither part of your INSERT statement, nor have a default value defined, will be left NULL.
Any fields that are defined as NOT NULL must be either part of the list of fields in your INSERT statement (so that you give them a specific NON NULL value), or they have to have a default constraint on them.