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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:24:15+00:00 2026-06-16T00:24:15+00:00

Assume that there are two tables in a database. First one is X, second

  • 0

Assume that there are two tables in a database. First one is X, second one is Y. Y table has a foreignkey on X table. So, if there is a record on Y table, it’s foreignkey related column value must exist on X table. This is the default behavior.

   X         Y
-------   -------
  ID        ID
            XID <--- Foreignkey to X table.

Now, for all records in X table i want to guarantee that there must be at least one record on Y table. If there isn’t, it should add one automatically. How can i do 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-06-16T00:24:16+00:00Added an answer on June 16, 2026 at 12:24 am

    There are two different issues here.

    1. Managing declarative constraints.
    2. Managing inserts, updates, and deletes.

    The “normal” way to guarantee a row exists in y for every row in x is to include foreign keys in both tables. Each table references the other.

    create table x (
      x_id integer primary key,
      y_id integer not null
    );
    
    create table y (
      y_id integer primary key,
      x_id integer not null references x (x_id)
    );
    
    alter table x
    add constraint one_to_one
    foreign key (y_id)
      references y (y_id) deferrable initially deferred;
    
    begin transaction;
    insert into x values (1, 100);
    insert into y values (100, 1);
    commit;
    

    Although this guarantees that every “x” has a “y”, so to speak, it doesn’t guarantee that every “x” has a different “y”, or that every “x” has its own “y”. I’m not sure whether that’s your requirement.

    This isn’t quite what you’re looking for, because you have a 1:N relationship between your two tables. You could make it work, but I think you’d have to write a trigger to manage changes to table “y”. For example, if the user deletes from “y” the row that “x” holds a foreign key reference to, you’d need to update “x” to reference a different row in “y”.

    But this leads to the second issue: managing inserts, updates, and deletes. There are several ways to do that.

    • The client is responsible for providing all the statements necessary to keep the database consistent. (With the possible exception of that foreign key problem above, which might better be handled in a trigger on “y”.)
    • Clients have no direct access to base tables. All changes are through stored procedures.
    • Clients have no direct access to base tables. All changes are through updatable views.

    Stored procedures and updatable views can give you some degree of automatically adding a row in “y”, but you have to write procedural code to do it.

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

Sidebar

Related Questions

I have a database table called users This table has two columns (that are
Let's assume I have a database with two tables: categories and articles . Every
I have two MySQL database tables that are meant to hold data for eshop
I need to synchronize two tables. Let's assume that the tables contain following columns:
Assume that there's no problem with my header file and some included library. I'm
We all know that most applications out there assume class names to follow the
Assume that we have two sets: A=(a_1,a_2,...,a_m) and B=(b_1,b_2,...,a_n) (Not necessarily of same size).
Assume that I have a backbone model that has a bunch of boolean attributes:
I have a database with two tables: trips and days . Each entry in
I have a movie database (postgreSQL). One of tables contains actors and movie titles.

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.