Skip to content
Advertisement

How to create unique key base on three field?

I have two foreign keys in table:

userId
visitorId

Also field action

I want to add constraints on these three field, that userId + visitorId + action will be unique.

How to do that in Workbench, MYSQL?

Advertisement

Answer

You can create a unique constraint:

alter table t add constraint unq_t_userid_visitorid_action
    unique (userid, visitorid, action);

You can also do this by creating a unique index:

create unique index unq_t_userid_visitorid_action on t(userid, visitorid, action);

You should be able to create an index through the Workbench GUI interface.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement