Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5980883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:51:53+00:00 2026-05-22T21:51:53+00:00

I need to insert a record to table if the record doesn’t exist, and

  • 0

I need to insert a record to table if the record doesn’t exist, and to update a record if the record exists in the table.
Of course, I can write:
p-code:

SELECT * FROM table1 WHERE id='abc' by JDBC
if(exists)
   UPDATE table1 SET ... WHERE id='abc' by JDBC;
else
   INSERT INTO table1... by JDBC;

However, I don’t think the code is elegant.
Alternatively, I can also write it in this way:
p-code:

int row = Statement.executeUpdate("INSERT INTO table1...", 2);
if(row==0)
  update table1 SET ... WHERE id='abc' by JDBC;

Do you think the latter way is better and faster? Thanks!

EDIT: in MYSQL

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:51:54+00:00Added an answer on May 22, 2026 at 9:51 pm

    It depends on what type of database your are using and whether or not you can take advantage of database specific features. MySQL for instance lets you do the following:

    INSERT INTO territories (code, territory) VALUES ('NO', 'Norway')
    ON DUPLICATE KEY UPDATE territory = 'Norway'
    

    However, the above is not standard (SQL-92) compliant. That is, it will most likely not work on all databases. In other words, you would have to stick with the code as you have written it. It might not look that elegant, but it is probably the most safe solution to go with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to insert records into a table that has no primary key using
I am currently working in C#, and I need to insert a new record
now i need to insert some data from the sqlserver into a word,i know
I need to be able to insert/update objects at a consistent rate of at
I need to create an SQL query to insert some data into a table
I need a quick no for DELETE/UPDATE/INSERT, since 3p reporting tool allows users to
I'm trying to do the classic Insert/Update scenario where I need to update existing
I am using sqlite with python. When i insert into table A i need
I have an application where I need to INSERT an auto_increment value from a
I need to insert 800000 records into an MS Access table. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.