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(idbetween 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(idbetween 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.


本文转自 lzf328 51CTO博客,原文链接:

http://blog.51cto.com/lzf328/1031390


相关文章
|
关系型数据库 MySQL
Mysql 建表时报错 invalid ON UPDATE clause for 'create_date' column
原文:Mysql 建表时报错 invalid ON UPDATE clause for 'create_date' column 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
3274 0
|
关系型数据库 MySQL 数据库
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
199 0
|
存储 关系型数据库 MySQL
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
2856 0
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
155 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
|
SQL Oracle 关系型数据库
SQL FOREIGN KEY Constraint on CREATE TABLE
SQL FOREIGN KEY Constraint on CREATE TABLE
81 1
|
存储 关系型数据库 MySQL
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
|
Oracle 关系型数据库 OLAP
[20170421]impdp SKIP_CONSTRAINT_ERRORS
[20170421]impdp导入问题data_options=SKIP_CONSTRAINT_ERRORS.txt --//一般年前我们经常要做一些导入导出操作,经常会遇到主键冲突问题.
1463 0
|
关系型数据库 MySQL 数据库
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
1140 0
|
关系型数据库 MySQL
Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails
Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails
214 0