Question

here is my table....

 create table c1(id int primary key,name varchar(20));
 create table c2(iid int primary key,ename varchar(20),foreign key(iid) references       c1(id) on update cascade on delete cascade);

 insert into c1 values(1,'a');
 insert into c1 values(2,'b');

 insert into c2 values(1,'a')
 insert into c2 values(2,'b')

 update c1 set id=3 where id=1;
 update c1 set name='hello' where id=2;

when i updating something nothing happens..

What is actually cascade, does it actually update values for c2's tables ID when I update on c1..

what i actually think is that above update query should change the value of c2's id value where id=1...

if i am wrong.. pls tell me how to update values in c2 when i update on c1... Thank you...

Was it helpful?

Solution

In MySQL this is supported if the storage engine is InnoDB. Other storage engines like MyISAM do not enforce the constraint (although MySQL allows the "references" syntax, but it has no effect: http://dev.mysql.com/doc/refman/5.7/en/example-foreign-keys.html ).

Check the storage engine for, e.g., the c1 table:

SHOW CREATE TABLE c1

Check which storage engine is used by default:

SHOW VARIABLES LIKE 'storage_engine'

Check that the foreign key checks are actually enforced:

SHOW VARIABLES LIKE 'foreign_key_checks'

If the storage engine is not InnoDB, you can convert your MyISAM tables to InnoDB, or (since this is a toy example) you can just recreate the tables. If you recreate the tables, you can either specify the storage engine at the end of your CREATE TABLE command, or you can set it before creating the tables, e.g.

SET storage_engine=MYISAM;

Similarly, if the foreign_key_checks variable is off, you can switch it on in this way:

SET foreign_key_checks=1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top