create table test3
(
id int PRIMARY KEY,
id2 int
);
create unique index ndx_id2 on test3 (id2);
爲唯一指標的目的,進入了很多空值,所有的NULL
值從所有其他NULL
值視爲不同,因此獨特。這是對SQL-92標準的兩種可能的解釋之一(標準中的語言是不明確的),PostgreSQL,MySQL,Firebird和Oracle的解釋是其中之一。如何允許在該領域具有唯一約束在SQL Server
Informix和Microsoft SQL Server遵循標準的其他解釋。
INSERT INTO test3(id, id2) VALUES (1, null);
INSERT INTO test3(id, id2) VALUES (2, null);
第二插入返回
重複的值不能被插入到一個唯一索引。 [表名 = TEST3,約束名稱= ndx_id2]
錯誤SQL Server,但成功地增加了記錄到其他的DBMS,SQLite的例如。
如何允許在字段中使用唯一約束在SQL Server中輸入大量空值?
您的腳本不允許爲空值。相反,我需要添加很多空值。示例:1,2,null,null,null,null,3. – user2217261 2013-04-06 15:32:39
您應該被允許插入多個空值 - 請參見[SQL Fiddle]上的示例(http://sqlfiddle.com/#!6/dbf85/ 1/0) – Andomar 2013-04-06 17:44:09
@ user2217261:[Works for me](http://sqlfiddle.com/#!3/fa6dc/3)。 – 2013-04-06 17:44:21