alter table tablename alter column columnname default 'value'
#1正确 alter table tablename alter column columnname default 'value'
Create table tablename(columnname nvarchar(50)) go alter table tablename add constraint UQ_tablenam_columnname default 'value' for columnname;
关键字 'default' 附近有语法错误。
if object_id('tb','U') is not null drop table tb go create table tb ( id int identity(1,1), name varchar(10) ) go alter table tb add constraint tb_name_def default 'abc' for name
[ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] (column [ ASC | DESC ] [ ,...n ] ) [ WITH FILLFACTOR =fillfactor [ WITH ( <index_option>[ , ...n ] ) ] [ ON { partition_scheme_name (partition_column_name ... ) | filegroup | "default" } ] | FOREIGN KEY (column [ ,...n ] ) REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | DEFAULT constant_expression FOR column [ WITH VALUES ] | CHECK [ NOT FOR REPLICATION ] (logical_expression ) }
alter table tb add constraint XXOO default 'xx' for oo --就是加了一个约束,默认值也是一种约束
如果改变的话需要先删除了alter table tb drop XXOOalter table tb add constraint XXOO default 'xoxo' for oo
alter table tablename alter column columnname default 'value'
go
alter table tablename add constraint UQ_tablenam_columnname default 'value' for columnname;
if object_id('tb','U') is not null
drop table tb
go
create table tb
(
id int identity(1,1),
name varchar(10)
)
go
alter table tb add constraint tb_name_def default 'abc' for name
{
{ PRIMARY KEY | UNIQUE }
[ CLUSTERED | NONCLUSTERED ]
(column [ ASC | DESC ] [ ,...n ] )
[ WITH FILLFACTOR =fillfactor
[ WITH ( <index_option>[ , ...n ] ) ]
[ ON { partition_scheme_name (partition_column_name ... )
| filegroup | "default" } ]
| FOREIGN KEY
(column [ ,...n ] )
REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ]
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ NOT FOR REPLICATION ]
| DEFAULT constant_expression FOR column [ WITH VALUES ]
| CHECK [ NOT FOR REPLICATION ] (logical_expression )
}
--就是加了一个约束,默认值也是一种约束