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 6341713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:05:34+00:00 2026-05-24T20:05:34+00:00

I am using DB2 DBMS. Scenario 1: myTable has a composite key (key1, key2)

  • 0

I am using DB2 DBMS.

Scenario 1:

myTable has a composite key (key1, key2) where both key1 and key2 are foreign keys from yourTable.

I want to insert new data from yourTable into myTable, but only if the key1, key2 combination does not already exist in myTable.

insert into myTable(key1, key2, someData)
values(x, y, z)
where NOT EXISTS (want to check if composite key is not already present)

Scenario 2:

I put data into a java object from yourTable with properties data1, data2, and data.

I want to insert the above data with the check as in Scenario1. data1 + data2 should not already be present in myTable.

How do I achieve this? I don’t think we can use a SELECT statement inside the insert statement.

insert into myTable(key1, key2, data)
values(data1, data2, data)
where (data1 + data2 are already not present in myTable)

How can I achieve this?

  • 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-24T20:05:35+00:00Added an answer on May 24, 2026 at 8:05 pm
    insert into mytable(...)
    select ...
    from yourtable y
    left join mytable m
    on y.key1 = m.key1 and y.key2 = m.key2
    where m.key is null
    

    or

    insert into mytable(...)
    select ...
    from yourtable y
    where not exists (select 1 from mytable m where y.key1 = m.key1 and y.key2 = m.key2)
    

    for your 2nd scenario, it’d look similar to the above query

    insert into mytable(...)
    select ...
    where not exists (select 1 from mytable m where javakey1 = m.key1 and javakey2 = m.key2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hibernate criteria, using DB2 dialect, generates the following SQL with composite keys in the
I am using DB2 in this case, but I'm figuring this has a generic
I am using command db2 restore db S18 from /users/intadm/s18backup/ taken at 20110913113341 on
As a developer using DB2 for the first time, I'm not familiar with what
I'm running an SQL query against a mainframe DB2 database using the OdbcDataReader class
Hi im using sqlalchemy on a db2 table with 500k rows. using plain sql
I'm trying to create DB2 trigger which is using the procedure VOTES_COUNT but when
We're using DB2 9.1 on Linux and SQL Server 2005, and the IBM db2
I am currently using DB2 . I do not know much about load statement.
I am using IBM DB2 V 9.1.0.356. I am using DB2 JDBC driver version

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.