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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:33:09+00:00 2026-06-03T15:33:09+00:00

In a database that contains many tables, I need to write a SQL script

  • 0

In a database that contains many tables, I need to write a SQL script to insert data if it is not exist.

Table currency

| id     | Code    | lastupdate | rate      |
+--------+---------+------------+-----------+
| 1      | USD     | 05-11-2012 | 2         |
| 2      | EUR     | 05-11-2012 | 3         |

Table client

| id     | name    | createdate | currencyId|
+--------+---------+------------+-----------+
| 4      | tony    | 11-24-2010 | 1         |
| 5      | john    | 09-14-2010 | 2         |

Table: account

| id     | number  | createdate | clientId  |
+--------+---------+------------+-----------+
| 7      | 1234    | 12-24-2010 | 4         |
| 8      | 5648    | 12-14-2010 | 5         |

I need to insert to:

  1. currency (id=3, Code=JPY, lastupdate=today, rate=4)
  2. client (id=6, name=Joe, createdate=today, currencyId=Currency with Code 'USD')
  3. account (id=9, number=0910, createdate=today, clientId=Client with name 'Joe')

Problem:

  1. script must check if row exists or not before inserting new data
  2. script must allow us to add a foreign key to the new row where this foreign related to a row already found in database (as currencyId in client table)
  3. script must allow us to add the current datetime to the column in the insert statement (such as createdate in client table)
  4. script must allow us to add a foreign key to the new row where this foreign related to a row inserted in the same script (such as clientId in account table)

Note: I tried the following SQL statement but it solved only the first problem

INSERT INTO Client (id, name, createdate, currencyId)
SELECT 6, 'Joe', '05-11-2012', 1
WHERE not exists (SELECT * FROM Client where id=6);

this query runs without any error but as you can see I wrote createdate and currencyid manually, I need to take currency id from a select statement with where clause (I tried to substitute 1 by select statement but query failed).

This is an example about what I need, in my database, I need this script to insert more than 30 rows in more than 10 tables.

any help

  • 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-06-03T15:33:10+00:00Added an answer on June 3, 2026 at 3:33 pm

    You wrote

    I tried to substitute 1 by select statement but query failed

    But I wonder why did it fail? What did you try? This should work:

    INSERT INTO Client (id, name, createdate, currencyId)
    SELECT 
        6, 
        'Joe', 
        current_date, 
        (select c.id from currency as c where c.code = 'USD') as currencyId
    WHERE not exists (SELECT * FROM Client where id=6);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that contains data for many clients. Currently, we insert tens
I need to insert many sql rows into oracle database very fast. IndexData is
I have a Database that contains data about articles , structures and manufacturers .
Scenario I am building a database that contains a series of different tables. These
I have a database table TravelRequest that contains, amongst other things, the fields SignOffName
I am working on a database that contains 3 tables: A list of companies
I have a database with 3 tables. The first table contains students with fields
I have a large database that contains many urls, there are many domains repeating
I have a SQLite database that contains a huge set of log messages. I
I'm using a database that contains contacts (fields like name, address, ...). If i'm

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.