0

Say I have two columns: GroupName in one table, and Username in another. How do I constrain the values of these columns so that any value added to one column is unique across both columns? Preferably in a "non-messy" way.

Garikai
  • 405
  • 2
  • 15

1 Answers1

0

There is no simple way with the structure that you mention. Of course, there is a way by implementing triggers, but that is cumbersome.

An alternative data model fixes the problem, though:

create table things (
    thingsId int auto_increment primary key,
    name text,
    type text,
    . . .
    check (type in ('group', 'user')),
    unique (name)
);

This puts each type of thing into a single column and then imposes the unique constraint.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786