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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:09:05+00:00 2026-05-16T12:09:05+00:00

Here is a simple situation. (My apologies my SQL is quite rusty.) (I am

  • 0

Here is a simple situation. (My apologies my SQL is quite rusty.)

(I am using MySQL and Java but it should not matter too much.)

Let’s say I have two tables.

Here is a simple SQL statement:

A:

insert into patient (patient_id) values (16)

Now there is another table person, which has a person_id and that has to be constrained to match patient_id.

B:

CONSTRAINT `person_id_for_patient` FOREIGN KEY (`patient_id`) REFERENCES `person` (`person_id`) ON UPDATE CASCADE)

What I am looking for is some pre-built tool, solution, etc., that might allow one to expand an insert statement such as A in order to satisfy constraints automatically.

In other words, that would automatically expand A into:

insert into person (person_id) values (16)
insert into patient (patient_id) values (16)

Any ideas?

Thank you
Misha

  • 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-16T12:09:06+00:00Added an answer on May 16, 2026 at 12:09 pm

    Consider these 2 solutions to handle your business logic requirements:

    • Strongly suggest creating a stored procedure to insert into both the person and the patient tables.
    CREATE PROCEDURE CreatePerson(IN newID int)
    BEGIN
       insert into person (person_id) values (newID);
       insert into patient (patient_id) values (newID);
       --as many statements as you need. perhaps wrap in a TRANSACTION.
    END;
    

    This will help control the business logic. You’ll have to enforce that all code NOT directly insert into the person using adhoc SQL. How easy/hard this is depends on your environment.

    • Create a trigger for after insert on the person table to automatically insert into patient. Creating triggers in MySQL. Triggers will definitely solve your dev problem here, but generally aren’t overall a great solution, as you tend to ‘forget’ that they’re there. Later, when you are investigating a defect/problem in your database, you may overlook the hidden logic in the trigger. Read more opinions on triggers.

    Either way, with either of these two, you’ll get the creation of a record into the patient after person.

    The question then becomes – does this business rule need enforcing the other way around? What happens when a process tried to write into the patient table? Does it require that a match exist in the person table?

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

Sidebar

Related Questions

Here's the situation: I'm developing a simple application with the following structure: FormMain (startup
I'm probably missing something simple here, but I can't find the answer elsewhere. I
I am probably overlooking something really simple here but I am trying to redirect
No doubt I'm missing something really simple here but I just can't see the
A simple situation here, If I got three threads, and one for window application,
I bet this has been answered many times over, but... For a simple situation
I hope I am not missing something very simple here. I have done a
Here is some simple Perl to count the number of times a value occurs
Here's a simple (hopefully) L10N question: Do all locales want this format: Sunday, Nov
Here is a simple scenario with table characters: CharacterName GameTime Gold Live Foo 10

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.