MySQL:特定参考的问题

发布于 2024-10-07 09:50:03 字数 1541 浏览 1 评论 0原文

我有两张桌子。上面是一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2024-10-14 09:50:03

您需要在 IPGeo.code3 列上添加唯一索引或主键,以便使用外键引用它。

您可以通过运行 show engine innodb status\G 并查看 LATEST FOREIGN KEY ERROR 部分来查看该错误。该错误可能看起来像这样:

Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

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 the LATEST FOREIGN KEY ERROR section. The error probably looks something like this:

Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文