Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL server 2014/16.

Want create a table. The table has some columns that need support:
1.Can contain multiple nulls.
2.If not null, then must be unique.
3.The columns has no relationship.
4.Not entire row is unique. Each column is unique.

How to make the CREATE TABLE command?

It seems SQL server can only support one null when using UNIQUE.

Below example is what I wanted:
CREATE TABLE UniqueTest (
col1 int,
col2 int unique null
);

INSERT INTO UniqueTest VALUES (1, 1);
-- SUCCESS

INSERT INTO UniqueTest VALUES (2, 2);
-- SUCCESS

INSERT INTO UniqueTest VALUES (3, 2);
-- FAIL

INSERT INTO UniqueTest VALUES (4, NULL);
-- SUCCESS

INSERT INTO UniqueTest VALUES (5, NULL);
-- SUCCESS


I had searched internet, lots of articles are discussed in old SQL server version, e.g. 2005/2008.
I think the SQL server 2014/2016 has a new CREATE TABLE option to meet my requirement, but I don't know.

What I have tried:

The solution from internet would be:
create index on specific column.

But:
1.My table will has 10~30 columns that need unique and nulls. If each column create an index, then will be 10~30 indexes in a table. Is it possible?
2.Lots indexes will lower efficiency. Is it?
Posted
Updated 24-May-17 3:05am
v2

1 solution

If I understand the question correctly you could use an unique index to enforce the logic.

Consider the following test
SQL
CREATE TABLE UniqueTest (
   col1 int,
   col2 int
);

CREATE UNIQUE INDEX U_UniqueTest ON UniqueTest (col1, col2);

-- Test variations

INSERT INTO UniqueTest VALUES (1, 1);
-- SUCCESS

INSERT INTO UniqueTest VALUES (1, 2);
-- SUCCESS

INSERT INTO UniqueTest VALUES (1, NULL);
-- SUCCESS

INSERT INTO UniqueTest VALUES (NULL, 1);
-- SUCCESS

INSERT INTO UniqueTest VALUES (NULL, NULL);
-- SUCCESS

-- RE-RUN
INSERT INTO UniqueTest VALUES (1, 1);
-- Cannot insert duplicate key row in object 'dbo.UniqueTest' with unique index 'U_UniqueTest'. The duplicate key value is (1, 1).

INSERT INTO UniqueTest VALUES (1, 2);
-- Cannot insert duplicate key row in object 'dbo.UniqueTest' with unique index 'U_UniqueTest'. The duplicate key value is (1, 2).

INSERT INTO UniqueTest VALUES (1, NULL);
-- Cannot insert duplicate key row in object 'dbo.UniqueTest' with unique index 'U_UniqueTest'. The duplicate key value is (1, <NULL>).

INSERT INTO UniqueTest VALUES (NULL, 1);
-- Cannot insert duplicate key row in object 'dbo.UniqueTest' with unique index 'U_UniqueTest'. The duplicate key value is (<NULL>, 1).

INSERT INTO UniqueTest VALUES (NULL, NULL);
-- Cannot insert duplicate key row in object 'dbo.UniqueTest' with unique index 'U_UniqueTest'. The duplicate key value is (<NULL>, <NULL>).
 
Share this answer
 
Comments
Wendelius 24-May-17 7:18am    
I see you copied my solution to your question. I take it there were some issues with it, so can you explain what problems you faced?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900