2

it does not seem to be possible to declare a NULLABLE column, and to create a UNIQUE constraint. Is there a way to deal with this case? I need a column that can contain NULL values or UNIQUE values. Maybe a trigger could do this? Thanks

I'm using DashDB

yelo3
  • 5,613
  • 5
  • 26
  • 23

2 Answers2

1

You don't mention your DB2 platform and version...that could make a difference.

But try adding a UNIQUE WHERE NOT NULL index..

CREATE UNIQUE WHERE NOT NULL INDEX myindex ON mytable (columnn1)

Charles
  • 21,637
  • 1
  • 20
  • 44
  • Version is DashDB on IBM Bluemix cloud (don't know which version it is exactly). – yelo3 Jan 19 '17 at 08:08
  • Actually I've noticed an error in my question I previously wrote "index" but I meant "constraint" – yelo3 Jan 19 '17 at 13:03
1

It is possible - check out the option

EXCLUDE NULL KEYS

in the CREATE INDEX syntax here

MichaelTiefenbacher
  • 3,805
  • 2
  • 11
  • 17
  • Actually I've noticed an error in my question I previously wrote "index" but I meant "constraint" – yelo3 Jan 19 '17 at 13:03
  • Currently you can't do this with a unique constraint. However since a unique constraint is a logical construct (its physical implementation is enforced via a unique index), creating the unique index as @MichaelTiefenbacher suggests will result in the same effect. – Ian Bjorhovde Jan 19 '17 at 17:35