我遇到MySQL组成的独特密钥问题.
它由URL,整数值和日期字段组成.
但是当我尝试插入行时,我得到一个例外:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'http://cars.auto.ru/cars/used/sale/16152870-c13f1.html-2012-02-1' for key 'one_a_day_idx'
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,组合索引被截断了64个字符,因此它不再是唯一的(我每天从外部源检索一次数据)
但最简单的是插入了记录,但抛出了关于约束违规的例外情况
有一个类似的问题在这里,但只能劝是使用SHOW CREATE TABLE找出指数的实际长度.
显示创建表显示:
| auto_ru_sale | CREATE TABLE `auto_ru_sale` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`template` int(11) NOT NULL,
`region` varchar(128) NOT NULL,
`URI` varchar(128) NOT NULL,
`subType` varchar(128) NOT NULL,
`cost` int(11) NOT NULL,
`productionYear` int(11) NOT NULL,
`engineVolume` int(11) NOT NULL,
`transmitionType` varchar(1) NOT NULL,
`run` int(11) NOT NULL,
`evaluationDate` date NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `one_a_day_idx` (`template`,`URI`,`evaluationDate`),
KEY …Run Code Online (Sandbox Code Playgroud)