I have two tables containing similar data, I want to delete from one of the tables where the it contains same data as the other table. I used the script below an ran into bogus query that got my mysql server restarted. How to I do this better?
DELETE table1.link FROM table1 INNER JOIN table2 WHERE table1.link=table2.link 01 November 2011Comment
ogugua belonwu>>>hold on, will be back with a reply<<< 0001 November 2011
andy chuksOk, Thanks in Advance 0002 November 2011
shegun babsThat's not the proper way to use DELETE in SQL queries. You can only use SELECT that way.
You'll have to delete both records with separate SQL statements otherwise you should use a foreign key constraints to reference the foreign key and ON DELETE CASCADE.
e.g
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;
this way if you delete a record from "parent" table with id = 5,
another record from "child" table with parent_id = 5 is also delete too. 0014 April 2012
ogugua belonwu@shegun babs, in this case a foreign key constraint will not work as there will be records that do not obey the foreign key on second table. 0014 April 2012
DELETE table1.link FROM table1 INNER JOIN table2 WHERE table1.link=table2.link 01 November 2011Comment
You'll have to delete both records with separate SQL statements otherwise you should use a foreign key constraints to reference the foreign key and ON DELETE CASCADE.
e.g
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;
this way if you delete a record from "parent" table with id = 5,
another record from "child" table with parent_id = 5 is also delete too.