tags:

views:

34

answers:

2

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.

+1  A: 

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

Haim Evgi
+1  A: 

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

SQLMenace
the Both tables must be InnoDB tables , and the performance of innodb is less than myisam
Haim Evgi
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.
ASD
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" (http://dev.mysql.com/doc/refman/5.1/en/alter-table.html) instead of recreating the tables.
Kai Chan
I tried altering the table with innoDB engine. But its not working
ASD