开发者

Inserting Char value into SQL table

I created a SQL table an enforced check constraints on it, but now when I try to insert data I get an error message.

create table BranchTel
(
    BrRegNo varchar(10) REFERENCES Branch(BrRegNo), 
    TelNo char(12)
    PRIMARY KEY(BrRegNo)
)
ALTER TABLE BranchTel Add Constraint BranchTelTelNo
Check(TelNo LIKE '[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')

Insert statement

insert into BranchTel values('BG-205','940112571963')

Error message

The INSERT statement conflicted with the CHECK constraint "BranchTelTelNo". The conflict occurred in database "StudentDetails", table "dbo.BranchTel", column 'TelNo'. The statement has been term开发者_如何学JAVAinated. Insert statement insert into BranchTel values('BG-205','94-011-2571963') Error message String or binary data would be truncated. The statement has been terminated.

Please help me


Your check constraint is 14 characters long (you need to count the - as well), while the field size is 12.

Additionally, 940112571963 does not conform to the pattern xx-xxx-xxxxxxx you have defined in your check constraint.

You need to change the field size to 14 and when inserting make sure the dashes are in the right place:

insert into BranchTel values('BG-205','94-011-2571963')


Insert statement insert into BranchTel values('BG-205','94-011-2571963') Error message String or binary data would be truncated. The statement has been terminated.

Here the value 94-011-2571963 length is greater than 12 which obviously violates the check constraint.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜