指定了默认值
若增加了 not null限制,则自动将所有的新列值赋予默认值。
或者不加 not null限制,但指定 with values修饰,也会自动将所有的新列值赋予默认值。
只有指定了默认值,既没有not null,有没有with values时,才只将新insert的列赋值。
参考:column_definition
USE tempdb;
GO
--Safety Check
IF OBJECT_ID('dbo.test','U') IS NOT NULL
DROP TABLE dbo.test;
GO
--Create the test table
CREATE TABLE dbo.test ( Col1 INT );
GO
--Insert some test data
INSERT INTO dbo.test ( Col1 )
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5;
GO
--Add a new, NOT NULL column with default constraint
--Use with WITH VALUES clause to also populate the columns
ALTER TABLE dbo.test
ADD col2 INT NOT NULL DEFAULT(0),
col3 INT DEFAULT(0) WITH VALUES,
col4 INT DEFAULT(0)
;
GO
SELECT *
FROM test