Given the following table
CREATE TABLE example (
id int NOT NULL IDENTITY(1,1),
strA NVARCHAR(10) NULL,
strB NVARCHAR(10) NULL,
CONSTRAINT [pk_id] PRIMARY KEY (id)
CONSTRAINT [unq_str_combo] UNIQUE (strA, strB)
CONSTRAINT [chk_one_is_null] CHECK ((strA IS NULL AND strB IS NOT NULL) OR (strA IS NOT NULL AND strB IS NULL))
);
How does SQL decide if a combination of values in the columns strA and strB are a unique combo?
Should any of the following INSERT
statements fail?
INSERT INTO dbo.example (strA, strB) VALUES ('abc', NULL); -- A
INSERT INTO dbo.example (strA, strB) VALUES (NULL, 'def'); -- B
INSERT INTO dbo.example (strA, strB) VALUES (NULL, 'abc'); -- C
INSERT INTO dbo.example (strA, strB) VALUES ('def', NULL); -- D
Is there a way during table creation (or by ALTER TABLE
) to add a check or some other constraint such that the INSERT
statements for C
and D
fail?