I have a employee table which has 10 columns and have to create a unique key constraint for id, name, address, mobile
In the above case address might can come as null and mobile can come as null. However when they comes the uniqueness should be maintained.
first, i created a unique constraint by combining all the above keys and following is observed.
Actual behaviour in MySQL.
001-Thiagu-NULL-900000 - Accepted
001-Thiagu-NULL-900000 - Accepted
001-Thiagu-0001-900000 - Accepted
001-Thiagu-0001-900000 - Rejected - Duplicate Record
Expected behaviour in all the databases
001-Thiagu-NULL-900000 - Accepted
001-Thiagu-NULL-900000 - Rejected - Duplicate Record
001-Thiagu-0001-900000 - Accepted
001-Thiagu-0001-900000 - Rejected - Duplicate Record
Basically the similar should be considered for duplication no matter whether the value exist as NULL or Not.
To overcome this problem i dropped the idea of combining and creating unique by adding columns to the unique constraint and come up with a new column of string type with unique constraint.
One each insert of the record i manually construct and give the value on any insert so that uniqueness will be maintained.
Is that would be the right approach or any other way to fix in the above first approach which i am not sure.
The created constraint should work for MySQL, SQL Server, Oracle and Postgres.