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 27: Which is the equivalent to the REPLACE statement below? – MySQL Question of the Day

Skip to content

By urs in mysql questions

 

create table
  monthly_sum (yearmonth char(6) primary key,msum decimal (10,2));
insert into monthly_sum
  values ('201001',100),('201002',200),('201003',400),('201004',500);

 
Which is the equivalent to the REPLACE statement below?
 

replace into monthly_sum values ('201001',1202.13);

a) insert into monthly_sum values ('201001',1202.13)
     ON DUPLICATE KEY UPDATE msum=1202.13;

b) update monthly_sum set msum=1202.13 where yearmonth='201001';

c) insert into monthly_sum select '201001',1202.13 from dual
     where not exists (select 1 from monthly_sum where yearmonth='201001');

d) delete from monthly_sum where yearmonth='201001';
   insert into monthly_sum values ('201001',1202.13);

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

Tags: , ,

Comment Feed

One Response


  1.  
    Answer:
     
    d
     
    A REPLACE does a DELETE and an INSERT. Answer a does an INSERT and an UPDATE.
    c only inserts, if the row does not exist.
     

You must be logged in to post a comment.