Jump to content

mysql on delete cascade error


kalster

Recommended Posts

I am trying to delete child nodes using the parent_id, id. like the following...

 

id | parent_id

1  | 0

2  | 1

3  | 2

 

deleting id 2 should also delete id 3. How to get the follow code to work?

 

i am using mysql 5.1. this code gives a query syntax error on line number... but no error message. 

"CREATE TABLE test(
  `id` INT(20) NOT NULL AUTO_INCREMENT,
  `parent_id` INT( NOT NULL,
  `title` TEXT NOT NULL,
  FOREIGN KEY (parent_id) REFERENCES test (id) ON DELETE CASCADE,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 ";

when i use the following code, there is no errors.

"CREATE TABLE test(
  `id` INT(20) NOT NULL AUTO_INCREMENT,
  `parent_id` INT( NOT NULL,
  `title` TEXT NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=myisam DEFAULT CHARSET=utf8 ";

notice in the above code that i have changed the word INNODB to MYISAM for the ENGINE and removed the FOREIGN KEY. this code works but no "on delete cascade".

Link to comment
Share on other sites

Foreign keys can only reference unique fields, so the PRIMARY KEY must come before the FOREIGN KEY. Otherwise MySQL thinks that id is a non-unique field.

 

Declaring parent_id as NOT NULL also makes no sense, because then you won't be able to insert any rows at all. What's the parent of the very first row? It can't have one.

Link to comment
Share on other sites

Whatever strange reason you may have for fumbling with self-referencing records, don't do it. It's weird, and it comes with a lot of problems. For example, you won't be able to delete the row (unless you temporarily remove the foreign key and then add it again).

 

Just use NULL like everybody else. In this case, NULL means that there is no parent.

 

I also have no idea why you quote the number 1. Numbers are not strings. Yes, MySQL is sloppy and lets you do it, but it's still poor practice.

Link to comment
Share on other sites

One table will suffice if you use NULL to represent no parent as Jaques stated

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `col1` int(11) DEFAULT NULL,
  `parent_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk1` (`parent_id`),
  CONSTRAINT `fk1` 
    FOREIGN KEY (`parent_id`) 
    REFERENCES `test` (`id`) 
    ON DELETE CASCADE 
    ON UPDATE NO ACTION
)

mysql> select * from test;
+----+------+-----------+
| id | col1 | parent_id |
+----+------+-----------+
|  1 |  222 |      NULL |   (top level, no parent)
|  2 |  333 |         1 |
|  3 |   44 |      NULL |   (top level, no parent)
|  4 |   22 |         1 |
|  5 |   32 |         2 |
|  6 |   21 |         3 |
|  7 |   43 |         3 |
+----+------+-----------+
7 rows in set (0.00 sec)

mysql> DELETE FROM test WHERE id = 1;
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+------+-----------+
| id | col1 | parent_id |
+----+------+-----------+
|  3 |   44 |      NULL |
|  6 |   21 |         3 |
|  7 |   43 |         3 |
+----+------+-----------+
3 rows in set (0.00 sec)
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.