5 messages in com.mysql.lists.mysqlRe: Foreign key contraints, on delete...
FromSent OnAttachments
Andrew DeFaria12 Jan 2004 10:53 
Victoria Reznichenko13 Jan 2004 06:13 
Andrew DeFaria13 Jan 2004 12:04 
Victoria Reznichenko15 Jan 2004 04:30 
Andrew DeFaria16 Jan 2004 10:57 
Subject:Re: Foreign key contraints, on delete cascade not working?
From:Victoria Reznichenko (vict@ensita.net)
Date:01/13/2004 06:13:34 AM
List:com.mysql.lists.mysql

Andrew DeFaria <ADeF@salira.com> wrote:

I created the following .sql file to demonstrate a problem I'm having. According to the manual:

If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row.

However:

drop database if exists MYDB;

create database MYDB;

use MYDB;

create table user ( userid varchar (8) not null, name tinytext not null, primary key (userid) ) type=innodb; -- user

create table useropts ( userid varchar (8) not null, name tinytext, value varchar (128), key user_index (userid), foreign key (userid) references user (userid) on delete cascade ) type=innodb; -- useropts

insert into user values ("userA", "User A"); insert into useropts values ("userA", "option", "value");

select * from user; select * from useropts;

delete from user;

select * from useropts; select * from user;

$ mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 35215 to server version: 4.0.10-gamma

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> source MYDB.sql Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Database changed Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

+--------+--------+ | userid | name | +--------+--------+ | userA | User A | +--------+--------+ 1 row in set (0.00 sec)

+--------+--------+-------+ | userid | name | value | +--------+--------+-------+ | userA | option | value | +--------+--------+-------+ 1 row in set (0.00 sec)

Query OK, 1 row affected (0.00 sec)

+--------+--------+-------+ | userid | name | value | +--------+--------+-------+ | userA | option | value | +--------+--------+-------+ 1 row in set (0.00 sec)

Empty set (0.00 sec)

As you can see I when I delete from user (the parent table) the useropts (child table) entry remains. Shouldn't it be deleted?

Heikki Tuuri asked me to look at my innodb variables and I found:

mysql> show variables like "%innodb%"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | have_innodb | NO | +---------------+-------+ 1 row in set (0.00 sec)

But I still must ask: Why is that? How do I turn it on?

Do you use 3.23.xx version? You should install MySQL-Max binary if you want to use InnoDB: http://www.mysql.com/doc/en/InnoDB_in_MySQL_3.23.html