34,838
社区成员




--在建表时设置default属性
if object_id('dbo.tb') is not null
drop table dbo.tb
create table tb
(
ID int identity
,Col1 varchar(20)
,Col2 varchar(20) default 'BB'
);
insert into tb(col1) values ('AA')
select * from tb;
(1 行受影响)
ID Col1 Col2
----------- -------------------- --------------------
1 AA BB
--建好表后再设置Default
if object_id('dbo.tb') is not null
drop table dbo.tb
create table tb
(
ID int identity
,Col1 varchar(20)
,Col2 varchar(20)
);
insert into tb(col1,Col2) values ('AA','')
alter table tb
add constraint DF_tb_Col2 default ('BB') for Col2
insert into tb(col1) values ('CC')
select * from tb;
drop table tb;
ID Col1 Col2
----------- -------------------- --------------------
1 AA
2 CC BB
CREATE TABLE
[ database_name.[ owner ] .| owner.] table_name
( { < column_definition >
| column_name AS computed_column_expression
| < table_constraint > ::= [ CONSTRAINT constraint_name ] }
| [ { PRIMARY KEY | UNIQUE } [ ,...n ]
)
[ ON { filegroup | DEFAULT } ]
[ TEXTIMAGE_ON { filegroup | DEFAULT } ]
< column_definition > ::= { column_name data_type }
[ COLLATE < collation_name > ]
[ [ DEFAULT constant_expression ]
没明白是什么意思