K
K
Konstantin2018-11-18 04:36:55
MySQL
Konstantin, 2018-11-18 04:36:55

How to guarantee the uniqueness of a record using a complex key?

There is such a simple table, with 3 foreign keys. How can I make it impossible to re-create an entry with the same values ​​for these three keys? Create a complex key based on them?
And how to do it in the Workbench environment, just additionally specify each one as a primary key?
5bf0c22d31784012836469.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-11-18
@Junart1

CREATE TABLE user_roles(
   user_id INT NOT NULL,
   role_id INT NOT NULL,
   PRIMARY KEY(user_id,role_id),
   FOREIGN KEY(user_id) REFERENCES users(user_id),
   FOREIGN KEY(role_id) REFERENCES roles(role_id)
);

www.mysqltutorial.org/mysql-primary-key
https://dev.mysql.com/doc/refman/8.0/en/multiple-c...

R
Rsa97, 2018-11-18
@Rsa97

IMHO, wrong requirement. What prevents a person in one organization from transferring, say, from programmers to loaders, and then back to programmers? Here you have a double of this trio of fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question