Mysql multiples columns with one references

How can I make 3 columns have a field reference? The code below is incorrect.

CREATE TABLE `example` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,
`userid_do1` int(11) DEFAULT NULL,
`userid_do2` int(11) DEFAULT NULL,
`userid_do3` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`),
 CONSTRAINT `example_ibfk_1` FOREIGN KEY (`userid_do1,userid_do2,userid_do3`) REFERENCES `usertable` (`id`),
) ENGINE=InnoDB

Upvotes: 0

Views: 24

Answers (1)

John Woo
John Woo

Reputation: 263723

you need to create one constraint for each column that references to another table's column.

CONSTRAINT example_ibfk_1 FOREIGN KEY (userid_do1) REFERENCES usertable(id),
CONSTRAINT example_ibfk_2 FOREIGN KEY (userid_do2) REFERENCES usertable(id),
CONSTRAINT example_ibfk_3 FOREIGN KEY (userid_do3) REFERENCES usertable(id)

Upvotes: 2

Related Questions