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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.