Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
Question 25: Which of of the following DELETE-statements are valid? – MySQL Question of the Day

Skip to content

By urs in mysql questions

 
(Find all correct answers)
 
See the scenario below:
 

create table stock (prod_id int primary key, amount decimal(10,2));
create table out_of_stock (prod_id int key);
 
insert into stock values (1,100),(2,200),(3,10),(4,10);
insert into out_of_stock values (1),(3);

 
The idea is to delete the rows in the stock table for all product ids listed in the out-of-stock table.
 

a) delete from stock where prod_id
     in (select prod_id from out_of_stock);
 
b) delete stock v1 from stock v1
     inner join out_of_stock v2 on (v1.prod_id=v2.prod_id) ;
 
c) delete from stock using stock
     inner join out_of_stock v2 on (stock.prod_id=v2.prod_id) ;
 
d) delete stock.* from stock , out_of_stock
     where stock.prod_id=out_of_stock.prod_id ;

 
[ Updating Data (10%) - The DELETE Statement ]
 

Tags: , ,

Comment Feed

One Response


  1.  
    Answers:
     
    a b c d
     
    All the deletes work. The USING syntax is also valid but harder to read, therefore I would recommend the syntax of b & d. Multi-table deletes can be very useful! b, c and d all use inner joins whereas a sub-query is used in a.
     

You must be logged in to post a comment.