Msg 547 The ALTER TABLE statement conflicted with the CHECK constraint "".

创建约束出现下面的错误:

 

Msg 547, Level 16,State 0, Line 2

The ALTER TABLEstatement conflicted with the CHECK constraint "chk_id". The conflictoccurred in database "test", table "dbo.test_CONSTRAINT",column 'ID'.

 

这个错误说明现有的表中数据超过了约束的限制,所以无法插入。一般我们需要将表中数据梳理完之后重新创建,但是可能存在一种状况比如以前确实只有5位的数据,现在升级系统后变为8位以前的数据还要保留。解决这个问题可以使用WITH NOCHECK参数。

 

CREATE TABLE test_CONSTRAINT

   (

   ID INT

   )

  --插入一条记录误

  INSERT INTO test_CONSTRAINTVALUES(100000)

  select * fromtest_CONSTRAINT

 

 --创建约束(只允许-10000

ALTER TABLE dbo.test_CONSTRAINTADDCONSTRAINT  id_checkCHECK(id between 1and 10000);

 

Msg 547, Level 16,State 0, Line 2

The ALTER TABLEstatement conflicted with the CHECK constraint "chk_id". The conflictoccurred in database "test", table "dbo.test_CONSTRAINT",column 'ID'.

 

--WITH NOCHECK 以避免根据现有行验证该约束,从而允许添加该约束

ALTER TABLE dbo.test_CONSTRAINTWITHNOCHECK

ADD CONSTRAINT id_checkCHECK(id between 1and 10000);

  

-- 重新启用约束

ALTER TABLE dbo.test_CONSTRAINTCHECKCONSTRAINTid_check;

 

--插入违反约束的数据报错

 INSERT INTO test_CONSTRAINTVALUES(100000)

 

Msg 547, Level 16,State 0, Line 1

The INSERTstatement conflicted with the CHECK constraint "id_check". Theconflict occurred in database "test", table"dbo.test_CONSTRAINT", column 'ID'.

The statement hasbeen terminated.

 

更多信息:http://msdn.microsoft.com/zh-cn/library/ms179491(v=sql.105).aspx

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值