我有一个表,表名SysUser,其中已有大量数据,
现在我用SQL语句添加一列,命名为OperAddress并有默认值,
语句如下:
ALTER TABLE dbo.SysUser ADD OperAddress INT DEFAULT 1 NOT NULL
这样的写,SQL会自动会创建一个CHECK约束,但是约束名却是系统自动产生,比如上条语句产生的约束名为“DF__SysUser__OperAddress__1F653F45”
现在的问题是:
我要用这条语句
ALTER TABLE dbo.SysUser ADD OperAddress INT DEFAULT 1 NOT NULL
如何指定Default的约束名
6 个解决方案
#1
alter table dbo.SysUser
add constraint df_OperAddress default 1 for OperAddress
add constraint df_OperAddress default 1 for OperAddress
#2
大哥,不行呀。
我执行这条语句的时候就已经绑定了DEFAULT
ALTER TABLE dbo.SysUser ADD OperAddress INT DEFAULT 1 NOT NULL
我要达到的效果是一条语句,即添加一个不为NULL的一列,又创建默认约束,并且默认约束的约束名由我指定。
#3
use tempdb
go
create table SysUser(ID int identity)
go
ALTER TABLE dbo.SysUser ADD OperAddress INT not null constraint DF_SysUser_OperAddress default(1)
go
insert SysUser values (default)
go
select * from SysUser
/*
ID OperAddress
1 1
*/
go
create table SysUser(ID int identity)
go
ALTER TABLE dbo.SysUser ADD OperAddress INT not null constraint DF_SysUser_OperAddress default(1)
go
insert SysUser values (default)
go
select * from SysUser
/*
ID OperAddress
1 1
*/
#4
create table sysuser(id int)
insert into sysuser select 1
go
ALTER TABLE dbo.SysUser ADD OperAddress INT CONSTRAINT DF_1 DEFAULT 1 NOT NULL
go
select name,type,type_desc,create_date from sys.objects where name='DF_1'
go
drop table sysuser
/*
name type type_desc create_date
---------- ---- ---------------------------- -----------------------
DF_1 D DEFAULT_CONSTRAINT 2011-02-23 21:28:52.780
(1 行受影响)
*/
#5
CREATE TABLE [tb](cDxCode varchar(20))
--建立时自定义约束名
ALTER TABLE dbo.tb
ADD field_name INT NOT NULL CONSTRAINT DF_MyName DEFAULT(1)
--修改约束名(先删再增)
ALTER TABLE dbo.tb
DROP CONSTRAINT DF_MyName
ALTER TABLE dbo.tb
ADD DEFAULT(1) FOR field_name
--查看表中的约束
EXEC sp_helpconstraint 'dbo.tb'
#6
ALTER TABLE T ADD v INT DEFAULT(1) NOT NULL;
#1
alter table dbo.SysUser
add constraint df_OperAddress default 1 for OperAddress
add constraint df_OperAddress default 1 for OperAddress
#2
大哥,不行呀。
我执行这条语句的时候就已经绑定了DEFAULT
ALTER TABLE dbo.SysUser ADD OperAddress INT DEFAULT 1 NOT NULL
我要达到的效果是一条语句,即添加一个不为NULL的一列,又创建默认约束,并且默认约束的约束名由我指定。
#3
use tempdb
go
create table SysUser(ID int identity)
go
ALTER TABLE dbo.SysUser ADD OperAddress INT not null constraint DF_SysUser_OperAddress default(1)
go
insert SysUser values (default)
go
select * from SysUser
/*
ID OperAddress
1 1
*/
go
create table SysUser(ID int identity)
go
ALTER TABLE dbo.SysUser ADD OperAddress INT not null constraint DF_SysUser_OperAddress default(1)
go
insert SysUser values (default)
go
select * from SysUser
/*
ID OperAddress
1 1
*/
#4
create table sysuser(id int)
insert into sysuser select 1
go
ALTER TABLE dbo.SysUser ADD OperAddress INT CONSTRAINT DF_1 DEFAULT 1 NOT NULL
go
select name,type,type_desc,create_date from sys.objects where name='DF_1'
go
drop table sysuser
/*
name type type_desc create_date
---------- ---- ---------------------------- -----------------------
DF_1 D DEFAULT_CONSTRAINT 2011-02-23 21:28:52.780
(1 行受影响)
*/
#5
CREATE TABLE [tb](cDxCode varchar(20))
--建立时自定义约束名
ALTER TABLE dbo.tb
ADD field_name INT NOT NULL CONSTRAINT DF_MyName DEFAULT(1)
--修改约束名(先删再增)
ALTER TABLE dbo.tb
DROP CONSTRAINT DF_MyName
ALTER TABLE dbo.tb
ADD DEFAULT(1) FOR field_name
--查看表中的约束
EXEC sp_helpconstraint 'dbo.tb'
#6
ALTER TABLE T ADD v INT DEFAULT(1) NOT NULL;