简体   繁体   English

MySQL中一个FOREIGN列的PRIMARY和INDEX键

[英]PRIMARY and INDEX keys for one FOREIGN column in MySQL

I used MySQL Workbench to prepare a database layout and exported it to my database using phpMyAdmin. 我使用MySQL Workbench来准备数据库布局,并使用phpMyAdmin将其导出到数据库中。 When looking at one table, I got the following warning: 当查看一张桌子时,出现以下警告:

PRIMARY and INDEX keys should not both be set for column gid 不应同时为列gid设置PRIMARY和INDEX键

gid is a foreign index which is the primary key of a different table, and which is also part of the primary key of the current table. gid是一个外索引,它是另一个表的主键,并且也是当前表的主键的一部分。 So I have it as part of the primary key, and Workbench created an index for the foreign key entry. 因此,我将其作为主键的一部分,Workbench为外键条目创建了索引。 So why is that warning appearing, should I ignore it, or should I rethink my database layout? 那么,为什么会出现该警告,我应该忽略它还是应该重新考虑数据库布局?

This is a very simplified example of the used structure, which produces the warning: 这是所用结构的非常简化的示例,它会产生警告:

CREATE  TABLE IF NOT EXISTS `test_groups` (
  `gid` INT NOT NULL ,
  `gname` VARCHAR(45) NULL ,
  PRIMARY KEY (`gid`) );

CREATE  TABLE IF NOT EXISTS `test_users` (
  `gid` INT NOT NULL ,
  `uid` INT NOT NULL ,
  `name` VARCHAR(45) NULL ,
  PRIMARY KEY (`gid`, `uid`) ,
  INDEX `gid` (`gid` ASC) ,
  CONSTRAINT `gid`
    FOREIGN KEY (`gid` )
    REFERENCES `test_groups` (`gid` )
    ON DELETE CASCADE
    ON UPDATE CASCADE);

edit I tried deleting the additional index for gid in phpMyAdmin and it seems to work. 编辑我试图删除phpMyAdmin中gid的其他索引,它似乎可以工作。 The cascade action still happens when changing something in the groups table, so I guess the foreign-relation is intact even without the index. 当更改组表中的某些内容时,级联操作仍然会发生,因此即使没有索引,我也认为外部关系是完整的。

But why does MySQL Workbench force me to keep that index? 但是,为什么MySQL Workbench迫使我保留该索引? I cannot manually remove it there as long as the foreign key is there. 只要外键在那里,我就不能手动将其删除。

Nothing wrong with that. 没有错。 Even if it were the entire primary key of the current table its still potentially correct. 即使它是当前表的整个主键,它也仍然可能正确。 Indeed, unless you're one of those "programmers" who only ever uses autoincrement columns for primary keys, you're going to see this reported a lot. 确实,除非您是只使用自动增量列作为主键的“程序员”之一,否则您会看到很多报道。

I solved that problem now. 我现在解决了这个问题。 It seems that the default database storage engine was set to MyISAM on my server, so because I didn't specify it explicitely, all foreign key relations just discarded (without saying so though). 似乎默认的数据库存储引擎在我的服务器上设置为MyISAM,所以因为我没有明确指定它,所以所有外键关系都被丢弃了(尽管没有这么说)。 After converting it to InnoDB I no longer get the warning, so it seems that everything is working as it should. 将其转换为InnoDB之后,我不再收到警告,因此似乎一切都在正常进行。

However in this special case, I'll stick to MyISAM, and leave the foreign key relations outside for now, because I want to auto increment the second attribute in that multi-key (and that is not supported by InnoDB), and that's a bit more useful for this application than having foreign keys (especially when having data where updating and deleting will be done very rarely). 但是,在这种特殊情况下,我将坚持使用MyISAM,暂时不使用外键关系,因为我想自动增加该多键中的第二个属性(并且InnoDB不支持该属性),这就是对于此应用程序,它比具有外键更为有用(尤其是在具有很少执行更新和删除操作的数据时)。

Also in regards to MySQL Workbench, that behaviour still seems to be a bit buggy, and it was already reported . 另外,关于MySQL Workbench,该行为似乎仍然有些问题,并且已经有报道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM