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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:31:47+00:00 2026-06-16T14:31:47+00:00

I have a Table called Couple which has the following Fields CoupleId, HusbandId ,

  • 0

I have a Table called Couple which has the following Fields CoupleId, HusbandId , WifeId ,
StartDate , EndDate

The previous Table contains informations about couple, The CoupleId is a primary key

HusbandId and WifeId are Foreign keys form another Table Named Person

StartDate and EndDate represent the start/end date of marriage that happens between a male with HusbandId and female with WifeId

in my problem i have a condition that w female can’t married to two or more male at same time “Synchronous marriage”

and i have the following query

SELECT
   DISTINCT A.WifeID
FROM
   Couple A
   INNER JOIN Couple B
      ON A.WifeID = B.WifeID
      AND A.HusbandID <> B.HusbandID
      AND A.StartDate < B.EndDate
      AND A.EndDate > B.StartDate;

which returns any female who has Synchronous marriage ” Married to two or more male at same time”

I want to write trigger which fire when somebody want to insert or update the couple
table ,and i want this trigger allow the modification only if it make no fault information on it ( for example if someone insert a row on couple table and this row make the wife with #4 married to tow or more person at same time, this insertion must not complete because it make wrong info in the table)

can any one help me at this point ?

  • 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-16T14:31:48+00:00Added an answer on June 16, 2026 at 2:31 pm

    You can solve this without a trigger. Make a Composite primary key of these three keys in the Couples table like so:

    PRIMARY KEY(CoupleId, HusbandId, WifeId)
    

    This will ensure the uniqueness of the HusbandId and WifeId for each coupleId.

    Here is a demo for the schema design for these two tables:

    SQL Fiddle Demo.


    If you want to do this using a TRIGGER, try this:

    CREATE TRIGGER WhenInsertOrUpdate
    ON Couples
    INSTEAD OF INSERT, UPDATE
    AS 
         IF (NOT EXISTS( SELECT c.CoupleId
                         FROM Couples c
                         INNER JOIN Inserted i  ON c.HusbandId = i.HusbandId
                                               AND c.WifeId = i.WifeId
                                               AND c.EndDate IS NULL))
        BEGIN
           INSERT INTO Couples
           SELECT  CoupleId, HusbandId, WifeId, StartDate, Enddate
           FROM Inserted
        END
        ELSE BEGIN
           --- You can put here an update..
        END
    
    GO;
    

    Updated SQL Fiddle Demo using a trigger.

    This is just an example, you have to provide an update statement in the ELSE block if you need.

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

Sidebar

Related Questions

I have a table called Products in my database. It has a couple of
i have table called as Support, which have a field named Name and contains
I have a table called users which currently contains column money of type integer
I have table called posts in my DB. Each post has field called social_network
I have a table called TPM_TASKS which contain all tasks, as well as a
I have a database containing a table named Couple which representing data couples. This
I have a table called player and after inserting the data into there, which
I have table called emp . The table contains three columns empid , ename
I have table called stats . In am inserting yes or no in the
I have table called Buttons. Buttons table i have column button_number . Table contain

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.