Database insert and update-query whether the primary key exists and insert statements are implemented using an SQL statement.

Source: Internet
Author: User

If you insert a record into the database, you always need to check whether the record exists. If it does not exist, you need to insert it. If it exists, you can give up or give a prompt,

 

Can we use a record?

 

I found the SQL usage of DB2 and Oracle:

 

DB2:

 

Merge into schema. table as t using table (values ('keyvalue') S (keyname) on (T. keyname = S. keyname) When not matched then insert (keyname, contentlname) values (?,?);

 

ORACLE:

 

Insert into schema. Table (keyname, contentlname) Select ?,? From dual where not exists (select * from Schema. Table where keyname = ?);

 

Preparestatement is used here.

Maybe you have used MySQL or sqlserver, and the idea is basically the same.

 

Valid coding can give you confidence, make you feel strong!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.