1

I have three tables (all InnoDB with primary keys having int(7) for type)

tblcustomers: primary key is customer_id

tblorders: primary key is order_id (has field for customer_id called order_customer)

tblorder_detail: primary key is order_detail_id (has field for order_id called order_id)

I want a to create a relationship so that deleting a customer deletes their order history.

I used the following statement to alter the Orders table:

ALTER TABLE `tblorders`
  ADD CONSTRAINT `FK_myKey` FOREIGN KEY (`order_customer`) REFERENCES `tblcustomers` (`customer_id`) ON DELETE CASCADE ON UPDATE CASCADE;

This was successful and deleting a customer deletes their associated orders.

I then tried the following and I get an error:

ALTER TABLE `tblorder_detail`
  ADD CONSTRAINT `FK_myKey` FOREIGN KEY (`order_id`) REFERENCES `tblorders` (`order_id`) ON DELETE CASCADE ON UPDATE CASCADE

This is the error (wtc-ecommerce is the name of the db):

1005 - Can't create table 'wtc-ecommerce.#sql-5d2_c2' (errno: 121)

Reading through other SO posts, seems like everything is correctly configured, so I'm lost.

Thanks

4

0 回答 0