多列上的MySQL外键?

时间:2013-04-30 17:44:35

标签: 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$$;

1 个答案:

答案 0 :(得分:0)

我认为您的建议完全有效。一定要选择enum 您的table_name字段的数据类型;这样,不同的表名得到 MySQL透明地映射到整数值。这将使用比VARCHAR字段更少的内存 和选择与特定表相关的错误会更快。额外的 INDEX / KEY超过table_name record_id将进一步提升效果。您可以按如下方式创建表:

CREATE TABLE IF NOT EXISTS `error_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `table_name` enum('sites','tables','doors') CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
  `record_id` int(11) NOT NULL DEFAULT '0',
  `error_message` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
  `is_active` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `table_name` (`table_name`,`record_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;

table_name选择错误非常简单:

SELECT * FROM error_logs WHERE table_name = 'sites'

如上所述添加其他实体和表格时,请务必适当延长table_name enum

我觉得在你的情况下稍微逊色的替代将为每个主表使用单独的id字段:

CREATE TABLE IF NOT EXISTS `error_logs2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cards_id` int(11) DEFAULT NULL,
  `doors_id` int(11) DEFAULT NULL,
  `sites_id` int(11) DEFAULT NULL,
  `error_message` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
  `is_active` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `cards_id` (`cards_id`),
  KEY `doors_id` (`doors_id`),
  KEY `sites_id` (`sites_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

在这里,只有一个id字段包含一个值,另外两个将是NULL。用于插入与sites表和记录id 1相关的错误:

INSERT INTO `error_logs2` (`id`, `cards_id`, `doors_id`, `sites_id`, `error_message`, `is_active`)
VALUES (NULL, NULL, NULL, '1', 'Exception has occurred: Division by zero.', '1')

选择网站错误的工作原理如下:

SELECT * FROM error_logs2 WHERE sites_id IS NOT NULL

但是,为了能够将错误与新实体相关联,您必须向error_logs2表添加新列。但是,为一个特定实体和id选择错误只需要一次字段比较,例如:

SELECT * FROM error_logs2 WHERE cards_id = 5