You need to specify all the fields in the insert

When performing an insert, say, from C # to a SQL Server table (using parameterized sql statements), do I need to specify each field of the table in the insert statement?

I noticed that the fields that I do not specify in the insert by default have the default values ​​set in the table. I don’t know if this is good or bad in my insert statement to leave the fields and allow the default to take care of setting up the fields that I don’t need. This should be fine, because it works.

+3
source share
6 answers

You need to specify all those fields for which you want to insert a value. You do not have to specify all the fields in the table!

As you noticed, any of the fields that you do not specify and which have a default restriction for them will be set to the specified default value. This is the "good thing" (tm) - for sure! This allows you to write less T-SQL embed code - all set defaults will already be set. I believe this is a great feature of SQL Server (and many other relational databases) - you can initialize things like “last modified” date fields, “today” when pasting without specifically adding these fields to the INSERT statement.

Any fields that are neither part of your INSERT statement nor have a default value will be left NULL.

, NOT NULL, INSERT ( NON NULL), .

+5

, # .NET.

, INSERT, , NULL NULL. .

- . , , , . , , .

- INSERT - , , .

+2

, insert, .

0

, , , , . , - .

0

defaults.

0

This is normal if the default values ​​are values ​​that are correct for the record you are inserting. My concern is that you are talking about them as fields that you do not need. You must take care of what happens to them; this can lead to serious data problems that do not care. These fields are for some reason; you need to understand what the default values ​​mean and whether they are appropriate.

0
source

Source: https://habr.com/ru/post/1733078/


All Articles