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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:58:14+00:00 2026-06-16T04:58:14+00:00

I need to introduce a many-to-many relationship between two tables, which both have an

  • 0

I need to introduce a many-to-many relationship between two tables, which both have an integer for primary key, in a SQL Server database. How is this best done in T-SQL?

Consider the following two example table definitions for which there should be a many-to-many relationship:

CREATE TABLE [dbo].[Authors] (
    [Id]        INT            IDENTITY (1, 1) NOT NULL,
    CONSTRAINT [PK_Versions] PRIMARY KEY CLUSTERED ([Id] ASC)
);

CREATE TABLE [dbo].[Books] (
    [Id]      INT           NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
  • 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-16T04:58:15+00:00Added an answer on June 16, 2026 at 4:58 am

    The traditional way is to use an additional many:many (junction) table, which links to both tables:

    CREATE TABLE [dbo].[AuthorsBooks] (
        -- Optionally, we can give the table its own surrogate PK
        [Id]      INT IDENTITY(1,1) NOT NULL,
        AuthorId INT NOT NULL,
        BookId INT NOT NULL,
    
        -- Referential Integrity
        FOREIGN KEY(AuthorId) REFERENCES Authors(Id),
        FOREIGN KEY(BookId) REFERENCES Books(Id),
    
        -- PK is either the surrogate ...
        PRIMARY KEY CLUSTERED ([Id] ASC)
        -- ... Or the compound key
        -- PRIMARY KEY CLUSTERED (AuthorId, BookId)
    );
    

    One moot point is whether you want the compound key AuthorId, BookId to be the Primary Key, or whether to add your own new Surrogate – this is usually a subjective preference.

    Some of the points to consider whether going for a compound primary key or a new surrogate key for the Junction table:

    • Without the surrogate, external tables linking to the junction table would need to store both compound keys (i.e. would need to retain both AuthorId and BookId as foreign keys).
    • So a new surrogate offers the potential benefit of a narrower primary key, which then means any tables linking to this junction table will have a single, narrower foreign key.
    • However, with the compound keys, there can be an optimisation benefit that tables can join directly to the underlying Books or Authors tables without first joining to the junction table.

    The following diagram hopefully makes the case of the compound key clearer (the middle table Nationality is a junction table of PersonCountry):

    Composite Foreign Key

    Edit

    Usage is straightforward – if the link exists in the many:many table, then the relationship is deemed to exist. To test the existence, you ‘join through’ the link table e.g.

    -- Find all books written by AuthorId 1234
    SELECT b.* 
      FROM Books b 
      INNER JOIN AuthorsBooks ab
         ON b.Id = ab.BookId
      WHERE ab.AuthorId = 1234;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a database, I saw many tables in which the Primary-Key(PK) is AUTO_INCREMENT type.
Need help with a query that I wrote: I have three tables Company id
In PL/SQL (or many other languages), I can have IN OUT or OUT parameters,
Ok, I now introduce the problem, I have 3 tables Company , CompanyName ,
I am working on a website hwere i need to introduce security for reasons,
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
I have a website which I have been working on creating very rapidly, and
I'm trying to introduce a many-to-one kind of mapping inside a YAML configuration file
I have a need to transfer large streams of data from a web service.

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.