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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:39:23+00:00 2026-06-18T09:39:23+00:00

I have a table which I use to link two more tables together in

  • 0

I have a table which I use to link two more tables together in a 1 to many relationship.

TableA
id
name

TableB
id
name

LinkTable
TableA_id
TableB_id

Basically, one of TableA can have many of TableB. Very simple. The problem I have now is to create a constraint that follows the rules of this relation ship as such:

LinkTable
TableA_id    TableB_id
1            1
1            2
1            3
2            1
2            2
2            3

I want to create a unique constraint which combines both the columns together as the unique value. So in the link table above, with this new constraint, I can

INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 4);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 5);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (2, 6);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (3, 1);

With out any problems

And if I try to insert:

INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 1);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 3);

The constraint will fire because there is already a row with 1,1 and 1,3. How would I create a postgres constraint to do this? If i set a unique constraint to both rows, then I cannot have more then one TableA_id the same and more then one TableB_id the same.

What is the solution?

  • 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-18T09:39:24+00:00Added an answer on June 18, 2026 at 9:39 am

    have you tried setting both columns as unique?

    ALTER TABLE LinkTable 
    ADD CONSTRAINT LinkTable_Unique UNIQUE (TableA_id, TableB_id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Hibernate with Java. I have two tables which are associated with foreign
Let's say I have two tables, and both their primary identifiers use the name
I have a database table which is full-text indexed and i use the CONTAINS-function
i have a table which has many fields but i want to get count
I am using SQL Server 2008. I have two tables User ( UserID, Name,
I have two tables that look like this: Products: id category name description active
I have two tables with the following definitions: CREATE TABLE [dbo].[Shows] ( [Id] UNIQUEIDENTIFIER
I have a table which has two varchar(Max) columns Column 1 Column 2 -----------------------
I have a table which repeats an image link to a file download for
Using cakephp, I have a generic address table, which I want to link to

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.