首页 文章

如何INSERT包含下划线并且也是外键的字段

提问于
浏览
0

如何在外键字段中插入包含下划线的数据?例如 . 这有效:

INSERT INTO [childTable] (1, 'ABC')

但这不起作用:

INSERT INTO [childTable] (1, '_ABC')

因为它得到这个错误:

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_childTable_parentTable". The conflict occurred in database "X", table "parentTable", column 'SomeField'".

我们有两个由外键相关的表:

CREATE TABLE parentTable (
    Id int,
    SomeField [char] (4)
)

CREATE TABLE childTable (
    SomeField [char] (4)
)

ALTER TABLE childTable CONSTRAINT FK_childTable_parentTable
FOREIGN KEY SomeField
REFERENCES parentTable (SomeField)

并且parentTable肯定包含带有'_ABC'数据的记录 .

如果我暂时删除或禁用外键约束,我可以插入记录确定并恢复外键确定,但有没有办法逃避下划线或其他一些更好的解决方案?

环境:SQL Server Management Studio针对Microsoft SQL Server 2008 R2服务器和数据库,排序顺序设置为Latin1_General_BIN .

1 回答

  • 0

    简单地说 - 如果你的外键错误与你的父表中存在ABC并且_ABC不存在 . 它与下划线无关 .

    检查父表中是否存在_ABC . 如果它没有添加它 - 那么你的子表中的插入将正常工作 .

相关问题