我創建的表稱爲test
與列名爲code
:添加CHECK約束已填充表
create table test(
code char(3) not null);
我然後填入下列數據表:
insert into test values ('A12');
insert into test values ('B23');
insert into test values ('C45');
然後我改變了列使它成爲字符(4):
alter table test
alter column code char(4) not null;
然後,我添加了一個「X」 xisting數據,使之成爲4個字符長:
update test
set code='X'+code
where LEN(code)=3;
到目前爲止好,但後來當我嘗試添加一個檢查約束:
alter table test
add constraint codeCheck check (code like 'A-Z''A-Z''0-9''0-9');
我得到這個錯誤:
The ALTER TABLE statement conflicted with the CHECK constraint "codeCheck".
我知道錯誤意味着現有數據違反了我試圖添加到表中的檢查約束,但爲什麼?
以及我該如何做,使現有數據和檢查約束不會互相違反?
請使用{}按鈕而不是HTML標記來格式化您的帖子。 –