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

  • Home
  • SEARCH
  • 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 8147575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:23:55+00:00 2026-06-06T14:23:55+00:00

I have a relational table with two foreign-keys pointing to same table. The first

  • 0

I have a relational table with two foreign-keys pointing to same table.

The first foreign-key column should always be the minimum of a foreign property and the second one the maximum.

I am new to SQL Server and I read that a insert using the inserted temporary table should be used but how should I set the minimum and maximum relational childs not using a loop to iterate over the inserted entries.

Tables:

table Child
{
    int id;

    int value;
}

table Parent    
{
    int id;

    // foreign-keys A enforce minimum child value!
    int childA;
    int childB;
}

MySql Trigger

CREATE TRIGGER parent_beforeInsert
BEFORE INSERT ON Parent
    FOR each ROW
    BEGIN
        DECLARE childAValue AS INT;
        DECLARE childBValue AS INT;

        SET childAValue = (SELECT value FROM Child WHERE ID = NEW.childA);
        SET childBValue = (SELECT value FROM Child WHERE ID = NEW.childB);

        -- only check if b > a since reverse is wished behavior
        IF childBValue > childAValue
        BEGIN
            -- swap values              
            DECLARE newChildA AS INT = NEW.childA
            SET NEW.childA = NEW.childB;
            SET NEW.childB = newChildA;
        END;
    END;
  • 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-06T14:23:57+00:00Added an answer on June 6, 2026 at 2:23 pm

    Solved it using conditional select query.
    Not so elegant, especially if many properties need to set but it works.

    INSERT INTO Parent (childA_ID, childB_ID) 
        SELECT CASE WHEN childA.value <= childB.value THEN childA.ID ELSE childB.ID END AS childA_ID,
                CASE WHEN childA.value > childB.value THEN childA.ID ELSE childB.ID END AS childB_ID
            FROM inserted AS p, 
                Child AS childA, 
                Child AS childB,
            WHERE childA.ID = p.childA 
                AND childB.ID = p.childB;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi have a table named category which has two foreign keys to the table
I have table messages with two foreign key: user_id_from and user_id_to . I need
I have two tables with primary foreign key relationship. The table in foreign key
I have a many-to-many relation R (two column binding table, both columns are foreign
I have three tables Author, Book and AuthorBook. AuthorBook table only contains two foreign
I have two classes with many-to-many relation so I created a join table in-between
i have two tables: CREATE TABLE public.auctions ( id VARCHAR(255) NOT NULL, auction_value_key VARCHAR(255)
I have two table to be joined using hbm file. The scenario is as
I have a relational database with a Client table, containing id , name ,
I have following two entities public class User { [Key] public int Id {

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.