Auto increment Foreign Key Column in sql

  • I have a column for ID  in remarks table which is linked to customers table ID column

    The primary key is auto increment, but how to make foreign key auto incremental .

  • An Auto incrementing foreign key? What is your goal here?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • You don't.
    Insert the row into the remarks table, obtain the ID assigned to that row, put that into the customer table.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster - Thursday, March 16, 2017 5:20 AM

      obtain the ID assigned to that row,  

    Wats this ID u meant here.

  • Unforutnatedba - Thursday, March 16, 2017 10:42 AM

    GilaMonster - Thursday, March 16, 2017 5:20 AM

      obtain the ID assigned to that row,  

    Wats this ID u meant here.

    The customer ID. From your original post, it appears you're saying that the remarks table has a foreign key that refers to the customer table. Thus the Customer ID field in the Remarks table needs to contain the ID for the assigned customer for that remark. It does not auto increment.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • A foreign key is a link to a specific record in another table  (or another record in the same table).  Creating a foreign key field that is auto-incrementing would create a link to an arbitrary (and possibly non-existent) record, which would defeat the whole purpose of having a foreign key in the first place.

    What problem are you trying to solve that you think that you need to do this?

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply