今天我决定在PHPMyAdmin中自动完成我的工作,所以我创建了很多关系,但是有一个问题。例如,我有两张桌子:foodRaws和过敏原。在foodRaws表中,我有列,如名称,标签等..和过敏原。所以我在foodRaws.allergens和allerrgens.ID之间建立了关系。现在,当我正在编辑或插入foodRaws时,我可以直接从过敏原中选择ID到foodRws.allergens字段。但只有一个?例如,当我选择过敏原ID 6并且我想添加ID 7时它会替换ID 6.但我需要创建类似“6; 7”的东西。所以我创建了这个字段数据类型SET('1','2',...,'14'),但它以相同的方式工作,我不能为这个字段选择多于1个ID。你有任何想法如何解决这个问题吗?
表:
CREATE TABLE IF NOT EXISTS `raws` (
`id` int(10) unsigned NOT NULL,
`nameCzech` varchar(50) COLLATE utf8_czech_ci NOT NULL,
`nameEng` varchar(50) COLLATE utf8_czech_ci NOT NULL,
`tags` varchar(100) COLLATE utf8_czech_ci NOT NULL,
`allergens` set('','1','2','3','4','5','6','7','8','9','10','11','12','13','14') COLLATE utf8_czech_ci DEFAULT NULL
) ENGINE=MyISAM AUTO_INCREMENT=78 DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
CREATE TABLE IF NOT EXISTS `allergens` (
`id` enum('1','2','3','4','5','6','7','8','9','10','11','12','13','14') COLLATE utf8_czech_ci NOT NULL,
`nameCzech` varchar(50) COLLATE utf8_czech_ci NOT NULL,
`nameEng` varchar(50) COLLATE utf8_czech_ci NOT NULL,
`detailCzech` varchar(150) COLLATE utf8_czech_ci NOT NULL,
`detailEng` varchar(150) COLLATE utf8_czech_ci NOT NULL,
`img` varchar(15) COLLATE utf8_czech_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
和数据:
INSERT INTO `allergens` (`id`, `nameCzech`, `nameEng`, `detailCzech`, `detailEng`, `img`) VALUES
('1', 'Obiloviny obsahující lepek', 'Cereals containing gluten', 'pšenice, žito, ječmen, oves, špalda, kamut nebo jejich hybridní odrůdy', 'wheat, rye, barley, oats, spelled, kamut or their hybrids', 'cerreals'),
('2', 'Korýši', 'Crustaceans', 'a výrobky z nich', 'and products made from them', 'crustaceans'),
('3', 'Vejce', 'Eggs', 'a výrobky z nich', 'and products made from them', 'eggs'),
('4', 'Ryby', 'Fishs', 'a výrobky z nich', 'and products made from them', 'fish'),
('5', 'Arašídy', 'Peanuts', 'a výrobky z nich', 'and products made from them', 'peanuts'),
('6', 'Sójové boby', 'Soya beans', 's výjimkou zcela rafinovaného sójového oleje a tuků', 'except for fully refined soybean oil and fats', 'soya'),
('7', 'Mléko', 'Milk', 'a mléčné výrobky', 'and products made from milk', 'milk'),
('8', 'Suché skořápkové plody', 'Dry nuts', 'mandle, lískové ořechy, vlašské ořechy, kešu ořechy, pekanové ořechy, para ořechy, pistácie, ořechy makadamie a queensland', 'almonds, hazelnuts, walnuts, cashews, pecans, Brazil nuts, pistachios, macadamia nuts and Queensland', 'nuts'),
('9', 'Celer', 'Celery', 'a výrobky z celeru', 'and products made from celery', 'celery'),
('10', 'Hořčice', 'Mustard', 'a výrobky z hořčice', 'and products made from mustard', 'mustard'),
('11', 'Sezamová semena', 'Sesame seeds', 'a výrobky z nich', 'and products made from them', 'sesame'),
('12', 'Oxid siřičitý', 'Sulphur dioxide', 'a siřičitany v koncentracích vyšších než 10mg/kg nebo 10mg/l', 'and sulphites at concentrations greater than 10mg/kg or 10mg/l', 'sulphurs'),
('13', 'Vlčí bob', 'Big-leaved Lupine', 'a výrobky z vlčího bobu', 'and products made from lupine', 'lupine'),
('14', 'Měkkýši', 'Molluscs', 'a výrobky z nich', 'and products made from them', 'molluscs');
INSERT INTO `raws` (`id`, `nameCzech`, `nameEng`, `tags`, `allergens`) VALUES
(1, 'Vepřová plec bez kosti', 'Pork shoulder, boneless', '2', ''),
(2, 'Olej rostlinný', 'Vegetable oil', '', ''),
(3, 'Cibule loupaná', 'Peeled onion', '', ''),
(4, 'Paprika mletá', 'Paprika powder', '', ''),
.
.
.
(29, 'Majonéza', 'Mayonnaise', '', ''),
.
.
.
在ID 29中我需要过敏原3,6和7.如果PHPMYAdmin允许我只添加一个,如果我有数据类型SET?
答案 0 :(得分:0)
这样可以更好地建模为many-to-many relationship。
您需要添加包含列的链接表(例如)allergen_id
和raw_id
。然后,您可以在raws
中的每个条目中为该表中包含多个记录,并将每个记录附加到allergens
中的多个记录,而不使用SET
数据类型