removing row from a table cause delete in another one

67 views Asked by At

I have 2 tables: the categories table and the products table.

there aren't direct relationships with external keys so no dependencies in SQL. Products table contains a field called category_id just to do joins with categories table in my views.

Here the problem:

when i execute this procedure in php:

begin

SELECT @lft := lft, @rgt := rgt, @wdt := rgt - lft + 1
FROM categorie
WHERE id = id_categoria;

DELETE FROM categorie WHERE lft BETWEEN @lft AND @rgt;

UPDATE categorie SET rgt = rgt - @wdt WHERE rgt > @rgt;
UPDATE categorie SET lft = lft - @wdt WHERE lft > @rgt;

end

the category and all its leafs are removed correctly, but the products rows which contain in field 'category_id' one of the removed categories id are removed as well!

It is an SQL problem or just something that i didn't consider?

0

There are 0 answers