Issues with creating a foreign key

  • Hello, my name is Di, and its the second time I wrote a topic for this forum, I am having some issues for writing a query where I need to create a foreign key, I already search on some places at internet, but none of then helped me, can you guys help me?

    Attachments:
    You must be logged in to view attached files.
  • The error seems to indicate that the "idCategoria" column doesn't exist in one of the two tables.  Check each of the two tables to make sure that the column does actually exist in both tables.  I strongly suspect that the column you're looking for in the Categoria table is actually just "ID" (a lot of people make such a choice in their designs).

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I made idCategoria only on table categoria, I was gonna make an Alter Table on usuario for creating the foreign key, it means then that I need to create idCategoria on table usuario before trying to set it as a foreign key ?

  • Di wrote:

    I made idCategoria only on table categoria, I was gonna make an Alter Table on usuario for creating the foreign key, it means then that I need to create idCategoria on table usuario before trying to set it as a foreign key ?

    Yes

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • --Deleted. Once again, the site created two versions of my post

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Di wrote:

    I made idCategoria only on table categoria, I was gonna make an Alter Table on usuario for creating the foreign key, it means then that I need to create idCategoria on table usuario before trying to set it as a foreign key ?

    Ok.. then I'm confused.  Why would you knowingly try to use a column name that you know doesn't exist in a Foreign Key?

    At this point, I have to ask... what are you trying to accomplish with the Foreign Key?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Di wrote:

    I made idCategoria only on table categoria, I was gonna make an Alter Table on usuario for creating the foreign key, it means then that I need to create idCategoria on table usuario before trying to set it as a foreign key ?

    I've never tried it without having the columns already setup but, to the best of my knowledge, FK constraints aren't defer-able.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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