I have these two tables:
CREATE TABLE `car_shop` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`selling_brands` varchar(25) DEFAULT NULL,
`some_col` varchar(25) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `cars` (
`car_id` int(11) NOT NULL AUTO_INCREMENT,
`car_make` varchar(25) DEFAULT NULL,
PRIMARY KEY (`car_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
The second one is populated with following:
+--------+----------+
| car_id | car_make |
+--------+----------+
| 1 | BMW |
| 2 | Audi |
| 3 | Toyota |
+--------+----------+
I need to add such a constraint that column selling_brands would accept only values populated in car table (BMW, Audi, Toyota).
I imagine it that way:
+----+----------------+----------+
| id | selling_brands | some_col |
+----+----------------+----------+
| 1 | BMW, Audi | shop 1 |
| 2 | Toyota | shop 2 |
+----+----------------+----------+
I was trying to add constraint as follows, but it doesn't work:
ALTER TABLE car_shop
ADD FOREIGN KEY (selling_brands)
REFERENCES cars(car_make)
I'm getting: ERROR: (conn:24) Cannot add foreign key constraint Error Code: 1215