If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > insert update statement to avoid duplicate

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-24-05, 00:08
popskie popskie is offline
Registered User
 
Join Date: Oct 2004
Location: In cousin's house
Posts: 303
insert update statement to avoid duplicate

hi,
i have a table below and corresponding field.
table1
code // it's a unique field.
des // varchar

insert into table1(code,des) values('001','description') on duplicate key update des = 'new description';
the sql above is used to insert data but if already exist it execute update my problem is , what if my field code is not unique. How to execute a insert / update sql.

Anyone can help me.
Popskie
Reply With Quote
  #2 (permalink)  
Old 09-24-05, 07:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by popskie
what if my field code is not unique.
well, you started out saying that it is unique

anyhow, the mysql manual says that if it is not unique, only one row will be updated

you should probably run a few queries on a test table to become familiar with how it works
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-25-05, 21:29
popskie popskie is offline
Registered User
 
Join Date: Oct 2004
Location: In cousin's house
Posts: 303
Is their any other way beside on duplicate key. Ok i'll explain to you clearly.
My scenario is there is a insert / update procudere with two filtering expression.

ex.
if id and code not exist.
insert into table1(id,code,name)values(value1,value2,value3)
else
update table1 set name = value3 where id =value1 and code = value2


Is this posible in mysql with only a single shot and take this two statement execute to the server.
Reply With Quote
  #4 (permalink)  
Old 09-25-05, 21:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
define your key with two fields
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 09-25-05, 22:14
popskie popskie is offline
Registered User
 
Join Date: Oct 2004
Location: In cousin's house
Posts: 303
ok thanks r937. I thought mysql primary has only 1 field.
Reply With Quote
  #6 (permalink)  
Old 09-26-05, 06:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
that's right, keys can have more than one column

PRIMARY KEY (index_col_name,...)

the "dot dot dot" is the secret
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On