0

I have set foreign key integrity in a table. But i am able to delete data from the master table, which is reference in child table.. What would be the problem Actually it should say error on deletion like all referenced table data has to be deleted.

2
  • 2
    Which storage engine are you using? "For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it." (MySQL Reference Manual at dev.mysql.com/doc/refman/5.1/en/ansi-diff-foreign-keys.html) Commented Jan 24, 2010 at 15:57
  • The storge engine is ENGINE=MyISAM Commented Jan 24, 2010 at 16:01

2 Answers 2

1

Did you specify ON DELETE CASCADE ? when you created your FK? Don't know which engine you are using either

example

CREATE TABLE parent (id INT NOT NULL,
                     PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
                    INDEX par_ind (parent_id),
                    FOREIGN KEY (parent_id) REFERENCES parent(id)
                      ON DELETE CASCADE
) ENGINE=INNODB;

More here http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

Sign up to request clarification or add additional context in comments.

4 Comments

the Both tables must be InnoDB tables , and the performance of innodb is less than myisam
Actually i have created table plainly and i have data now. Did you mean that i need to recreate all the table for setting up the foreign key.
The foreign key constraints are there. MySQL just do not enforce them unless the storage engine is set to InnoDB. You can do an "ALTER TABLE [table name] ENGINE = InnoDB" (dev.mysql.com/doc/refman/5.1/en/alter-table.html) instead of recreating the tables.
I tried altering the table with innoDB engine. But its not working
1

mysql dont do it for you ,

you need declare trigger on delete action

before delete trigger example :

http://www.java2s.com/Code/Oracle/Trigger/Createabeforedeletetrigger.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.