PostgreSQL unique without AK and non-null restriction(s)

In PostgreSQL, and in fact any “SQL standards”-complaint DBMS, one can insist on uniqueness for non-null values and at the same time allow repetition on null values.

For instance, the following runs just fine:

create table uniqueness (
col int unique
);

insert into uniqueness(col) values(null);
insert into uniqueness(col) values(1);
insert into uniqueness(col) values(null);
insert into uniqueness(col) values(2);

– returns 4
select count(*) from uniqueness

However, within TDM there doesn’t appear to be a way to effect the above when designing a table. Selecting the “Unique” checkbox but unselecting the “Not Null” doesn’t appear to be allowed.

From the PG docs (my emphasis in bold):

In general, a unique constraint is violated if there is more than one row in the table where the values of all of the columns included in the constraint are equal. However, two null values are never considered equal in this comparison. That means even in the presence of a unique constraint it is possible to store duplicate rows that contain a null value in at least one of the constrained columns. This behavior conforms to the SQL standard, but we have heard that other SQL databases might not follow this rule. So be careful when developing applications that are intended to be portable.

-Pete

Hello Pete,

TDM allow it, but you need to set option "Allow Null Attributes in Keys" in options. By default is off.
Go to Main Menu - Settings - Options - Physical Model - General.

Daril

Hi Daril,

Thanks for the quick reply. That’s great to hear! Next time feel free to add RTFM to your response; my bad for not reading the fine print.

-Pete