我有以下脚本在 MySQL 5.1 版中创建一个表,以引用其他 3 个表。所有 3 个表都是使用 InnoDB 创建的,并且所有 3 个表的 ID 列都定义为 INT。
我已经成功创建了其他引用 ACCOUNT 和 PERSON 的表,但是,这是第一个引用 ADDRESS 的表,所以我在下面也包含了该表的定义,如运行。
我得到的错误是 ERROR 1005 (HY000) with errno 150,据我所知与外键创建有关。
失败的脚本是(为简单起见删除了额外的列):
CREATE TABLE WORK_ORDER (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ACCOUNT_ID INT NOT NULL,
CUSTOMER_ID INT NOT NULL,
SALES_ID INT,
TRADES_ID INT,
LOCATION_ID INT NOT NULL,
INDEX CUST_INDEX(CUSTOMER_ID),
INDEX SALES_INDEX(SALES_ID),
INDEX TRADES_INDEX(TRADES_ID),
INDEX ACCOUNT_INDEX(ACCOUNT_ID),
INDEX LOCATION_INDEX(LOCATION_ID),
FOREIGN KEY (CUSTOMER_ID) REFERENCES PERSON(ID) ON DELETE CASCADE,
FOREIGN KEY (SALES_ID) REFERENCES PERSON(ID) ON DELETE SET NULL,
FOREIGN KEY (TRADES_ID) REFERENCES PERSON(ID) ON DELETE SET NULL,
FOREIGN KEY (ACCOUNT_ID) REFERENCES ACCOUNT(ID) ON DELETE CASCADE,
FOREIGN KEY (LOCATION_ID) REFERENCES ADDRESS(ID) ON DELETE SET NULL
) ENGINE=InnoDB;
下面是用于创建 ADDRESS 表的 SQL 语句(为简单起见,删除了额外的列)。
CREATE TABLE ADDRESS (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
PERSON_ID INT NOT NULL,
ACCOUNT_ID INT NOT NULL,
ADDRESS_L1 VARCHAR(50),
ADDRESS_L2 VARCHAR(50),
CITY VARCHAR(25),
PROVINCE VARCHAR(20),
POSTAL_CODE VARCHAR(6),
COUNTRY VARCHAR(25),
INDEX CUST_INDEX(PERSON_ID),
INDEX ACCOUNT_INDEX(ACCOUNT_ID),
FOREIGN KEY (ACCOUNT_ID) REFERENCES ACCOUNT(ID) ON DELETE CASCADE,
FOREIGN KEY (PERSON_ID) REFERENCES PERSON(ID) ON DELETE CASCADE
) ENGINE=InnoDB;
我在这里浏览了几个处理类似问题的问题,但大多数似乎是重复的定义和不匹配的字段类型,以及一些没有将 InnoDB 用于其中一个或另一个表。然而,这些似乎都不是问题。有任何想法吗?