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

Hi i've got here a simple program, but it's not working properly. When i
Here's my situation: using PHP and MySQL as my backend, jQuery and jsTree for
This should be relatively simple, here is my situation; I have copied the trunk
i have this situation. I am using a simple tab menu from here .
I have a simple situation here. lets face html code first => <form name=geoKey
So here's the situation. I want to make a simple game for android. It's
I have to be missing something simple here but it escapes me. After the
I hope I am not missing something very simple here. I have done a
I think I'm just missing something simple here but here is what I am
Here's my situation; I've built a very simple web app that looks up a

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.