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 30: Which of of the following UPDATE-statements are valid? – MySQL Question of the Day

Skip to content

By urs in mysql questions

 
(Find all correct answers)
 
See the following scenario:
 

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 goal is to set the amount in stock to zero for all product ids listed in out_of_stock.
Which of of the following UPDATE-statements are valid?
 

a) update stock set amount=0
     where prod_id in (select prod_id from out_of_stock);
 
b) update stock set amount=0
     where exists (select 1 from out_of_stock where prod_id=stock.prod_id);
 
c) update stock v1
     inner join out_of_stock v2 on (v1.prod_id=v2.prod_id) set v1.amount=0;
 
d) update stock v1 , out_of_stock v2
     set v1.amount=0 where v1.prod_id=v2.prod_id ;
 
e) update stock
     set amount=ifnull((select 0 from out_of_stock
                where prod_id=stock.prod_id),amount);

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

Tags: , ,

Comment Feed

One Response


  1.  
    Answers:
     
    a b c d e
     
    All of the updates will work.
     
    b and e do a dependent sub-query and are probably the least effecient.
    (e is actually an example how not to do it, but valid nevertheless.)
    c and d use inner joins, each with a different syntax. Statement a works with a sub-query.
     

You must be logged in to post a comment.