我還在學習MySQL,所以請忍受我...多列上的MySQL外鍵?
我有幾個表(下面概述),我想記錄錯誤消息。我不是指查詢錯誤,而只是我的消息,如「連接到站點失敗」,「在站點xyz卡讀取錯誤」等...
我想到有一個表「error_logs」跟蹤我的錯誤,只有三列:
- ID
- ERROR_MESSAGE
- IS_ACTIVE
我遇到的問題,然而,就是每個錯誤會涉及到創紀錄在'卡','門'或'網站'表,我想跟蹤消息屬於哪條記錄(來自各種表格)。
所以,我的問題是什麼將是最好的方法來解決這個問題?我曾考慮添加兩個額外的列,'record_id'(它將引用另一個表中的一個id)和'table'(它將跟蹤消息所屬的表)。另外,另一個警告是,很可能會有額外的「主要」表格。
這裏是我迄今表:
CREATE TABLE `statuses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1$$;
INSERT INTO statuses SET name = 'error';
INSERT INTO statuses SET name = 'pre-install';
INSERT INTO statuses SET name = 'validate';
INSERT INTO statuses SET name = 'active';
INSERT INTO statuses SET name = 'disabled';
CREATE TABLE `sites` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`status_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `sites_status_id_idx` (`status_id`),
CONSTRAINT `sites_status_id` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$;
CREATE TABLE `doors` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(255) NOT NULL,
`site_id` int(11) NOT NULL,
`status_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `doors_site_id_idx` (`site_id`),
KEY `doors_status_id_idx` (`status_id`),
CONSTRAINT `doors_status_id` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE,
CONSTRAINT `doors_site_id` FOREIGN KEY (`site_id`) REFERENCES `sites` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$;
CREATE TABLE `card_types` (
`id` int(11) NOT NULL,
`manufacturer` varchar(45) DEFAULT NULL,
`sequance` varchar(32) DEFAULT NULL,
`auth_code` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$;
CREATE TABLE `cards` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`number` int(11) NOT NULL,
`type_id` int(11) NOT NULL,
`status_id` int(11) NOT NULL,
`is_oem` bit(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `number_UNIQUE` (`number`),
KEY `cards_type_id_idx` (`type_id`),
KEY `cards_status_id_idx` (`status_id`),
CONSTRAINT `cards_status_id` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE,
CONSTRAINT `cards_type_id` FOREIGN KEY (`type_id`) REFERENCES `card_types` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$;
CREATE TABLE `card_assignments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`door_id` int(11) NOT NULL,
`status_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `card_assignments_door_id_idx` (`door_id`),
KEY `card_assignments_status_id_idx` (`status_id`),
CONSTRAINT `card_assignments_door_id` FOREIGN KEY (`door_id`) REFERENCES `doors` (`id`) ON UPDATE CASCADE,
CONSTRAINT `card_assignments_status_id` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$;
馬塞勒斯,非常感謝您的意見!我將利用您的初始建議並創建表格,如同列表中列出的enum數據類型一樣。謝謝。 – Kate 2013-04-30 21:54:45
不客氣,很高興幫助:) – Marcellus 2013-05-01 14:19:58