By decipherinfosysThis project needed to support having multiple NULL values in the column and still have a UNIQUE constraint. That is allowed by Oracle but not in SQL Server and DB2 LUW. There is a way to make this work in SQL Server and DB2 LUW also but that requires a work-around. Consider this table:
CREATE TABLE TEST_UQ (COL1 INT IDENTITY(1,1) PRIMARY KEY, COL2 NVARCHAR(10) NULL)
GO
In this table, COL1 has been declared as the primary key but we want a UNIQUE constraint to be put on COL2 as well. Please note that COL2 is a nullable column and that SQL Server does not allow multiple NULL values in a UNIQUE index and treats them the same way. We can test it out prior to proceeding with the work-around:
Let us create a unique index first:
CREATE UNIQUE INDEX TEST_UQ_IND_1 ON TEST_UQ (COL2)
GO
Now, let us try to insert these values:
insert into test_uq (col2) values (‘abc’);
insert into test_uq (col2) values (‘xyz’);
insert into test_uq (col2) values (Null);
All three will go in. After that, try to insert the NULL value again:
insert into test_uq (col2) values (Null);
and you will get the error:
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object ‘dbo.test_uq’ with unique index ‘TEST_UQ_IND_1′.
The statement has been terminated.
The work-around is to have a computed column and define the unique constraint on it. Here is how you can do that:
1) First, let’s drop the existing unique index:
drop index test_uq.TEST_UQ_IND_1
2) Next, let’s add the computed column:
ALTER TABLE TEST_UQ ADD COL3 AS (CASE WHEN COL2 IS NULL THEN CAST(COL1 AS NVARCHAR(10)) ELSE COL2 END);
In this command, we are stating that whenever the value for COL2 is null, replace it with the primary key after casting it to the same data-type as that of COL2. By doing so, we will mae sure that COL3 is always NOT NULL and always have unique values. This approach will work well in this case as there should never be a clash of the values between COL1 and COL2. For example, what-if you needed to do this on a column that was also an interger data-type column? In that case, chances of clashes of the data can arise. If you suspect a clash, you can have additional logic like: (CASE WHEN COL2 IS NULL then -1 * COL1 ELSE COL2 END). That way, you can still maintain the logic and the uniqueness.
3) Now, create the unique index on this column:
CREATE UNIQUE INDEX TEST_UQ_IND_1 ON TEST_UQ (COL3)
GO
4) Next, let’s try to insert the NULL value again:
insert into test_uq (col2) values (Null);
This time it will go through. If we examine the contents of the table:
COL1 COL2 COL3 ———– ———- ———- 1 abc abc 2 xyz xyz 3 NULL 3 5 NULL 5
As you can see, we have allowed multiple NULL values now for COL2 and still maintained the uniqueness. We can next try to insert the value “abc” again and see if that preserves our uniqueness criteria:
insert into test_uq (col2) values (‘abc’);
This time, we will get an error:
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object ‘dbo.test_uq’ with unique index ‘TEST_UQ_IND_1′.
The statement has been terminated.
So, using this work-around, one can preserve the same behavior as Oracle. This might be useful to you as well in case you are working on a project that requires conversion from Oracle to SQL Server or Oracle to DB2 LUW.