我遇到了一个问题,最简单的解决方案是添加一个跨越mysql中多个表的唯一索引。这可能吗?
出于这个问题的目的,我将有三张桌子; status
,tracks
和events
。 status
和tracks
表都有一个自动递增ID(T_ID
或S_ID
)。来自它们的信息通过触发器添加到events
表中。问题是tracks
和status
中可能存在相同的自动递增ID,这意味着它们在events
中可能会多次使用相同的ID。
tracks
;
CREATE TABLE `tracks` (
`ID` int(11) NOT NULL,
`url` varchar(200) COLLATE latin1_general_ci NOT NULL,
`name` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`T_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`T_ID`),
UNIQUE KEY `url` (`url`),
UNIQUE KEY `T_ID` (`T_ID`),
KEY `ID` (`ID`),
CONSTRAINT `tracks_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
status
CREATE TABLE `status` (
`ID` int(11) NOT NULL,
`status` varchar(300) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`S_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`S_ID`),
UNIQUE KEY `S_ID` (`S_ID`),
KEY `ID` (`ID`),
CONSTRAINT `status_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `artists` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
events
CREATE TABLE `events` (
`ID` int(11) NOT NULL,
`action` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`E_ID` int(11) NOT NULL,
KEY `ID` (`ID`),
CONSTRAINT `events_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='This table shows what the artist has done and is used feed'
答案 0 :(得分:2)
同意,你不能在多个表之间建立索引。
但是,请考虑通过向事件表添加“源”列并对源键和外键的组合进行唯一约束来更改架构。这样可以防止从同一个源插入重复键。