Inserting Data into Some Columns

You can add data to some, but not all, of the columns in a row by specifying only those columns and the data for those columns. All the columns that are not in the column list must be defined to allow null values. The skipped columns can also accept defaults. If you skip a column that has a DEFAULT constraint, the default is used. You can use the DEFAULT keyword to instruct SQL Server to use the default value for a named column. (For details on defaults and null values, see Working with Tables and the INSERT statement in the Microsoft SQL Server Transact-SQL Reference.)

For example, this statement adds data to only two columns in the stores table in the pubs database:

INSERT INTO stores (stor_id, stor_name)
VALUES ('1229', 'Mary''s Books')

This form of the statement can also be used to skip over timestamp data.

The order in which you list the column names must match the order in which you list the values. This example produces the same results as the preceding one:

INSERT INTO stores (stor_name, stor_id)
VALUES('Mary''s Books', '1229')

Either of the INSERT statements puts 1756 in the pub_id column and The Health Center in the pub_name column. (Note that because the pub_id column in publishers has a unique index, you can't execute both of these example INSERT statements; the second attempt to insert a pub_id value of 1756 produces an error message.)