Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2007
    Posts
    14

    Unanswered: Error whilst adding foreign key

    I've been getting what I think is a bizarre error message whilst trying to create a foreign key between two tables.

    tblDailyComments has a primary key of DiaryID
    tblDailyOperatingData has a column DiaryID.

    Both are Int (4).

    Yet when I run the following:


    ALTER TABLE tblDailycomments
    ADD CONSTRAINT FK_tbldailycomments_tblDailyOpData
    FOREIGN KEY REFERENCES
    tblDailyOperatingData(DiaryID)

    I'm getting the error message:

    Server: Msg 8139, Level 16, State 1, Line 1
    Number of referencing columns in foreign key differs from number of referenced columns, table 'tblDailycomments'.

    I also tried to create the relationship in a database diagram and this brought back errors as well.

    Any help would be much appreciated.

    Peter

  2. #2
    Join Date
    May 2004
    Location
    Seattle
    Posts
    1,313
    what columns are in the PK of the tblDailyOperatingData table? perhaps you mean to create it the other way around:

    ALTER TABLE tblDailyOperatingData
    ADD CONSTRAINT FK_tblDailyOperatingData_tbldailycomments
    FOREIGN KEY REFERENCES
    tblDailycomments (DiaryID)

    also, why put tbl in front of all your table names? pronounI verbHate lameCodingConventionHungarian.
    Last edited by jezemine; 01-04-08 at 03:01.

  3. #3
    Join Date
    Nov 2005
    Posts
    122
    Code:
    ALTER TABLE tblDailycomments
    ADD CONSTRAINT FK_tbldailycomments_tblDailyOpData
    FOREIGN KEY (PutYourFKColumnNameHere) REFERENCES 
    tblDailyOperatingData(DiaryID)
    See my changes in blue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •