是否允许多个外键?

时间:2014-03-19 20:50:37

标签: mysql

我有多个表,我想要多个外键。

Users Table
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| idUsers  | int(11)     | NO   | PRI | NULL    |       |
| Username | varchar(45) | YES  |     | NULL    |       |
| Password | varchar(45) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

Friends Table
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| idUser   | int(11)     | YES  | MUL | NULL    |       |
| idFriend | int(11)     | YES  | MUL | NULL    |       |
| Status   | varchar(45) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

Groups Table
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| idGroup   | int(11)     | NO   | PRI | NULL    |       |
| GroupName | varchar(45) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

Members Table
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| idGroup  | int(11) | YES  | MUL | NULL    |       |
| idMember | int(11) | YES  | MUL | NULL    |       |
+----------+---------+------+-----+---------+-------+    

这是我的Members Table的外键,因此idGroup和idMember都对应于Users表idUsers。

ALTER TABLE `GroupOrder`.`Friends` 
ADD CONSTRAINT `idFriend`
FOREIGN KEY (`idFriend`)
REFERENCES `GroupOrder`.`Users` (`idUsers`)
ON DELETE CASCADE
ON UPDATE CASCADE;

我想在Friends表中创建另一个与Users表对应的外键。我做错了吗?每次我这样做都会出错。

ALTER TABLE `GroupOrder`.`Friends` 
ADD CONSTRAINT `idFriend`
FOREIGN KEY (`idUser` , `idFriend`)
REFERENCES `GroupOrder`.`Users` (`idUsers` , `idUsers`)
ON DELETE CASCADE
ON UPDATE CASCADE;

错误

ERROR 1005: Can't create table 'GroupOrder.#sql-2fce_1e' (errno: 150)
SQL Statement:
ALTER TABLE `GroupOrder`.`Friends` 
ADD CONSTRAINT `idFriend`
  FOREIGN KEY (`idUser` , `idFriend`)
  REFERENCES `GroupOrder`.`Users` (`idUsers` , `idUsers`)
  ON DELETE CASCADE
  ON UPDATE CASCADE

ERROR: Error when running failback script. Details follow.

ERROR 1050: Table 'Friends' already exists
SQL Statement:
CREATE TABLE `Friends` (
  `idUser` int(11) DEFAULT NULL,
  `idFriend` int(11) DEFAULT NULL,
  `Status` varchar(45) DEFAULT NULL,
  KEY `idUser_idx` (`idFriend`),
  KEY `idUser_idx1` (`idUser`,`idFriend`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 个答案:

答案 0 :(得分:0)

我假设朋友是从用户到用户的多对多映射。这种意味着友谊可以是单向的,但这并不一定是错误的。

您已将朋友的idfriend映射到用户的idusers,现在将朋友中的iduser映射到用户的idusers。

ALTER TABLE `GroupOrder`.`Friends` 
ADD CONSTRAINT `idFriend2`
FOREIGN KEY (`idUser`)
REFERENCES `GroupOrder`.`Users` (`idUsers`)
ON DELETE CASCADE
ON UPDATE CASCADE;

现在,您可能希望在朋友中强制执行iduser和idfriend的唯一性,这样您就不会重复。

alter table `grouporder`.friends`
add unique(oduser, idfriend);