LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-03-2008, 07:53 AM   #1
Balakrishnan84
Member
 
Registered: Feb 2007
Location: Bangalore, India
Distribution: Fedore Core 6
Posts: 41

Rep: Reputation: 15
Question Mysql insert on duplicate return ?


Hello All,

I have a question regarding Mysql query. Is it possible to insert if no duplicate found and if found, return the value. Something like, "insert into <table> values(..) on duplicate select a column in the record tht causes the duplication. Please let me know incase it is available. I dont want to run multiple queries to perform this.
Thanks in advance.

For example,

Table : N A B C All are unique, N- auto increment/primary key
so if we insert a record and if it is duplicate, is it possible to get the corresponding auto-incremented value for the record already existing?

Regards,
Bala
 
Old 01-03-2008, 10:38 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You get to do a "select", check whether or not it returned any rows and, if not, then do an "insert". It sounds like you might also wish to do this inside a transaction (so you can do a *second* "select", and roll back if another "insert" occurs concurrently).

PS:
And, of course, you might have the luxury of being able to add a "unique" constraint to the column(s) in question, assuring that you'll *never* have duplicates...

Last edited by paulsm4; 01-03-2008 at 11:13 AM.
 
Old 01-04-2008, 01:43 AM   #3
Balakrishnan84
Member
 
Registered: Feb 2007
Location: Bangalore, India
Distribution: Fedore Core 6
Posts: 41

Original Poster
Rep: Reputation: 15
Question Thanks for your idea.

As you mentioned above, I defined the columns as unique and im using "select" to check this operation. But it is time consuming, because of our table has nearly 15million records. If there is duplicate while insert, mysql gives error with columns that caused duplicates, instead would it be possible to print the auto incremented column?

For example,

If if insert a record and if it is success then i can use last_insert_id() to get the auto incremented value.
Similarly, if my insert fails because of duplicate , is there any such function to return the already existing value? Just a thought. Because running select again to get that value takes much time .

Thanks and Regards,
Bala
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I Insert Carrage Return in a QString? binarybob0001 Programming 4 11-23-2007 01:47 AM
Duplicate root user in MySQL buddhahat Linux - Newbie 4 08-29-2004 10:34 PM
Mysql insert problem don_dimo Linux - Software 3 11-01-2003 09:27 PM
Why MySQL Error on INSERT INTO....? pnh73 Programming 2 11-01-2003 05:53 PM
Error: Acct: Couldn't insert SQL accounting START record - Duplicate entry '15212' fo ethanchic Linux - Software 0 04-11-2003 10:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration