Jump to content

Having trouble altering table to insert foreign keys


Nomadic

Recommended Posts

MySQL server version: 5.1.30

 

MySQL statement:

ALTER TABLE table1 ADD FOREIGN KEY
(`g_id`) REFERENCES table2 (`g_id`)
ON DELETE CASCADE ON UPDATE CASCADE;

 

Errors that MySQL returns to the client: #1452 - Cannot add or update a child row: a foreign key constraint fails

 

Table structure:

CREATE TABLE `table1` (
`g_id` int(10) unsigned NOT NULL,
`inee_id` mediumint( unsigned NOT NULL,
`iner_id` mediumint( unsigned NOT NULL,
PRIMARY KEY (`g_id`,`inee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

 

I am trying to add foreign keys to various tables associating with the id's of the primary tables with cascade on update and delete. This is so that when entries get updated or deleted on the main tables it will cascade through and do the same to all the dependant tables without me having to worry about forgetting something. The problem is that I am new to foreign key use in mysql and all the differences from what I am used to has me lost with how to actually input a proper foreign key.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.