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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:44:43+00:00 2026-05-12T06:44:43+00:00

I’m running into an issue with a join: getting back too many records. I

  • 0

I’m running into an issue with a join: getting back too many records. I added a table to the set of joins and the number of rows expanded. Usually when this happens I add a select of all the ID fields that are involved in the join. That way it’s pretty obvious where the expansion is happening and I can change the ON of the join to fix it. Except in this case, the table that I added doesn’t have an ID field. This is a problem. But perhaps I’m wrong.

Should every table in a database have an IDENTITY field that’s used as the PK? Are there any drawbacks to having an ID field in every table? What if you’re reasonably sure this table will never be used in a PK/FK relationship?

When having an identity column is not a good idea?

Surrogate vs. natural/business keys

Wikipedia Surrogate Key article

  • 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-05-12T06:44:44+00:00Added an answer on May 12, 2026 at 6:44 am

    There are two concepts that are close but should not be confused: IDENTITY and PRIMARY KEY

    Every table (except for the rare conditions) should have a PRIMARY KEY, that is a value or a set of values that uniquely identify a row.

    See here for discussion why.

    IDENTITY is a property of a column in SQL Server which means that the column will be filled automatically with incrementing values.

    Due to the nature of this property, the values of this column are inherently UNIQUE.

    However, no UNIQUE constraint or UNIQUE index is automatically created on IDENTITY column, and after issuing SET IDENTITY_INSERT ON it’s possible to insert duplicate values into an IDENTITY column, unless it had been explicity UNIQUE constrained.

    The IDENTITY column should not necessarily be a PRIMARY KEY, but most often it’s used to fill the surrogate PRIMARY KEYs

    It may or may not be useful in any particular case.

    Therefore, the answer to your question:

    The question: should every table in a database have an IDENTITY field that’s used as the PK?

    is this:

    No. There are cases when a database table should NOT have an IDENTITY field as a PRIMARY KEY.

    Three cases come into my mind when it’s not the best idea to have an IDENTITY as a PRIMARY KEY:

    • If your PRIMARY KEY is composite (like in many-to-many link tables)
    • If your PRIMARY KEY is natural (like, a state code)
    • If your PRIMARY KEY should be unique across databases (in this case you use GUID / UUID / NEWID)

    All these cases imply the following condition:

    You shouldn’t have IDENTITY when you care for the values of your PRIMARY KEY and explicitly insert them into your table.

    Update:

    Many-to-many link tables should have the pair of id‘s to the table they link as the composite key.

    It’s a natural composite key which you already have to use (and make UNIQUE), so there is no point to generate a surrogate key for this.

    I don’t see why would you want to reference a many-to-many link table from any other table except the tables they link, but let’s assume you have such a need.

    In this case, you just reference the link table by the composite key.

    This query:

    CREATE TABLE a (id, data)
    CREATE TABLE b (id, data)
    CREATE TABLE ab (a_id, b_id, PRIMARY KEY (a_id, b_id))
    CREATE TABLE business_rule (id, a_id, b_id, FOREIGN KEY (a_id, b_id) REFERENCES ab)
    
    SELECT  *
    FROM    business_rule br
    JOIN    a
    ON      a.id = br.a_id
    

    is much more efficient than this one:

    CREATE TABLE a (id, data)
    CREATE TABLE b (id, data)
    CREATE TABLE ab (id, a_id, b_id, PRIMARY KEY (id), UNIQUE KEY (a_id, b_id))
    CREATE TABLE business_rule (id, ab_id, FOREIGN KEY (ab_id) REFERENCES ab)
    
    SELECT  *
    FROM    business_rule br
    JOIN    a_to_b ab
    ON      br.ab_id = ab.id
    JOIN    a
    ON      a.id = ab.a_id
    

    , for obvious reasons.

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

Sidebar

Ask A Question

Stats

  • Questions 154k
  • Answers 154k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer example of an audit trigger from https://www.postgresql.org/docs/current/static/plpgsql-trigger.html CREATE TABLE emp… May 12, 2026 at 10:28 am
  • Editorial Team
    Editorial Team added an answer The standard form with my bug fixed is: if ((value… May 12, 2026 at 10:28 am
  • Editorial Team
    Editorial Team added an answer I believe that: $self->{myArray} returns a reference. You want to… May 12, 2026 at 10:28 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.