問題: SQL Server允許將尾隨空格添加到外鍵!外鍵允許的尾隨空格
這種行爲當然會導致應用程序中出現各種不需要的行爲。這怎麼能停下來?
實施例:兩個表中1:n的關係:
create table products
(
pid nvarchar(20) primary key
;)
create table sales
(
pid nvarchar(20) references products(pid),
units int
);
現在插入主鍵'A'
:
insert into products (pid) values ('A');
現在,插入外鍵:
-- 'A' is accepted, as expected:
insert into sales (pid, units) values ('A', 23);
-- 'B' is declined, as expected:
insert into sales (pid, units) values ('B', 12);
-- 'A ' (with a trailing space)
-- This is ACCEPTED, but of course this is NOT EXPECTED !!
insert into sales (pid, units) values ('A ', 12);
檢查您的'ANSI_PADDING'設置。 https://msdn.microsoft.com/en-us/library/ms187403.aspx –
另請參見http://dba.stackexchange.com/q/56876 –
@ShannonSeverance我不希望刪除尾部空格。我只希望'A'和'A'受到不同的待遇! –