MySQL:特定参考的问题
我有两张桌子。上面是一个包含 IP 范围及其各自国家/地区属性的表(IPGEO 表)。另一个是一个表,它仅跟踪每个用户访问该网站的最后一个国家/地区。我们的想法是,如果用户突然从另一个国家访问该网站,我会通过电子邮件通知用户。
现在来看实际的表格。我有这两个:
包含 IP 范围的 IPGeo 表
CREATE TABLE IF NOT EXISTS `politiker_lu`.`IPGeo` (
`IPFrom` INT(11) NOT NULL ,
`IPTo` INT(11) NOT NULL ,
`code2` VARCHAR(2) NOT NULL ,
`code3` VARCHAR(3) NOT NULL ,
`Country` VARCHAR(45) NOT NULL ,
INDEX `index1` (`IPFrom` ASC) ,
INDEX `index2` (`IPTo` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
然后,还有一个 user_geo 表,用于跟踪用户访问网站的最后一个国家/地区。
CREATE TABLE IF NOT EXISTS `politiker_lu`.`user_geo` (
`fi_user` INT(10) UNSIGNED NOT NULL ,
`fi_country` VARCHAR(3) NOT NULL ,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (`fi_user`) ,
INDEX `fk_user_geo_1` (`fi_user` ASC) ,
CONSTRAINT `fk_user_geo_1`
FOREIGN KEY (`fi_user` )
REFERENCES `politiker_lu`.`user` (`id_user` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
这些是现有的表格。我现在想按如下方式引用这两个表:
ALTER TABLE `politiker_lu`.`user_geo`
ADD CONSTRAINT `fk_user_geo_IPGeo1`
FOREIGN KEY (`fi_country` )
REFERENCES `politiker_lu`.`IPGeo` (`code3` )
ON DELETE CASCADE
ON UPDATE CASCADE
, ADD INDEX `fk_user_geo_IPGeo1` (`fi_country` ASC) ;
但是该语句失败,错误代码为 150。两个表都是 utf8,两个列具有相同的数据类型。我在这里错过了一些重要的东西吗?
注意
表 user
存在并具有所有引用,实际上与问题无关。我保留它是为了不需要对语句进行太多编辑。
I have two tables. On is a table that contains IP Ranges and their respective country attributions (IPGEO table). The other is a table which simply keeps track of the last country the site was accessed from on a per user basis. The idea is that, if the user suddenly accesses the site from another country, I notify the user about this via email.
Now for the actual tables. I have these two:
The IPGeo table that contains the IP ranges
CREATE TABLE IF NOT EXISTS `politiker_lu`.`IPGeo` (
`IPFrom` INT(11) NOT NULL ,
`IPTo` INT(11) NOT NULL ,
`code2` VARCHAR(2) NOT NULL ,
`code3` VARCHAR(3) NOT NULL ,
`Country` VARCHAR(45) NOT NULL ,
INDEX `index1` (`IPFrom` ASC) ,
INDEX `index2` (`IPTo` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
Then, there is the user_geo table that tracks the last country from which the user accessed the site.
CREATE TABLE IF NOT EXISTS `politiker_lu`.`user_geo` (
`fi_user` INT(10) UNSIGNED NOT NULL ,
`fi_country` VARCHAR(3) NOT NULL ,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (`fi_user`) ,
INDEX `fk_user_geo_1` (`fi_user` ASC) ,
CONSTRAINT `fk_user_geo_1`
FOREIGN KEY (`fi_user` )
REFERENCES `politiker_lu`.`user` (`id_user` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
These are the tables as they exist. I now want to reference these two tables as follows:
ALTER TABLE `politiker_lu`.`user_geo`
ADD CONSTRAINT `fk_user_geo_IPGeo1`
FOREIGN KEY (`fi_country` )
REFERENCES `politiker_lu`.`IPGeo` (`code3` )
ON DELETE CASCADE
ON UPDATE CASCADE
, ADD INDEX `fk_user_geo_IPGeo1` (`fi_country` ASC) ;
That Statement however fails with errno 150. Both tables are utf8, both columns have the same data-type. Am I missing something vital here?
Notes
The table user
exists and has all the references and is actually irrelevant to the problem. I left it so that I didn't need to edit the statement too much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 IPGeo.code3 列上添加唯一索引或主键,以便使用外键引用它。
您可以通过运行
show engine innodb status\G
并查看LATEST FOREIGN KEY ERROR
部分来查看该错误。该错误可能看起来像这样:You need to add a unique index or a primary key on the IPGeo.code3 column in order to reference it with a foreign key.
You can see the error by running
show engine innodb status\G
and looking under theLATEST FOREIGN KEY ERROR
section. The error probably looks something like this: