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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:47:39+00:00 2026-06-04T08:47:39+00:00

Let’s say have have two tables, A and B. A contains fields: id, a1,

  • 0

Let’s say have have two tables, A and B.
A contains fields: id, a1, a2.
B contains fields: id, b1, b2.

Asume a one to many relationship from table A to B based on the id field.

Now let’s say I want to create a unique constraint on table b that prohibits me from inserting duplicate records based on b1, b2, and a1. Is that possible?

  • 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-04T08:47:41+00:00Added an answer on June 4, 2026 at 8:47 am

    You would have to have a copy of a1 appear in B in order to enforce this as a constraint. If you don’t want this visible to your users, you would create a table (say, _B) that contains all of Bs columns + a1, and then create a view (with suitable insert triggers to populate a1) called B that hides this column.

    You should also create a superkey of A over id and a1, and use a foreign key constraint (possibly with ON UPDATE CASCADE) from B over both of these columns to ensure that this copied column correctly matches the corresponding value in A.

    Whether you then decide that this should be the only foreign key, or continue to maintain the “correct” one (over just id) in addition is a matter of taste.


    E.g.:

    CREATE TABLE dbo.A (ID int not null,a1 int not null,a2 int not null,
        constraint PK_A PRIMARY KEY (id),
        constraint UQ_A_a1_check UNIQUE (id,a1)
    )
    CREATE TABLE dbo._B (ID int not null,a1 int not null,b1 int not null,b2 int not null,
        constraint PK_B PRIMARY KEY (a1,b1,b2),
        constraint FK_B_A FOREIGN KEY (id) references A (id),
        constraint DRI_B_A_a1_check FOREIGN KEY (id,a1) references A (id,a1) on update cascade
    )
    GO
    CREATE VIEW dbo.B
    WITH SCHEMABINDING
    AS
        select id,b1,b2 from dbo._B
    GO
    CREATE TRIGGER T_B_I on dbo.B instead of insert
    AS
        INSERT INTO dbo._B (id,a1,b1,b2)
        SELECT i.id,a.a1,i.b1,i.b2
        FROM
           inserted i
              inner join
           dbo.A a
              on
                 i.id = a.id
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I've got two tables: one with customer data, one with location data.
Let's say we have a table that has 2 million rows. It has two
Let's say I have a structure named vertex with a method that adds two
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say I have table with column 'URL' whrere I store urls like this
Let's say I have a javascript array with a bunch of elements (anywhere from

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.