Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2007
    Posts
    16

    Unanswered: What Am I Doing Wrong?

    I'm trying to create a database using a given ERD. I've created all the tables along with added constraints for the primary keys (i'm using SQL to do this).
    However, when I come to add the foreign key constraint, a message is popping up telling me the table already exists. How do I create the relationships between the tables correctly? Thanks

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    How are you adding the foreign key?
    George
    Home | Blog

  3. #3
    Join Date
    Nov 2007
    Posts
    16
    I'm writing them like this:

    CONSTRAINT ViewingAppointment_AppointmentCode_pk PRIMARY KEY (AppointmentCode),
    CONSTRAINT ViewingAppointment_PotentialBuyerCode_fk FOREIGN KEY (PotentialBuyerCode) REFERENCES PotentialBuyer (PotentialBuyerCode),
    CONSTRAINT ViewingAppointment_HouseCode_fk FOREIGN KEY (HouseCode) REFERENCES SaleProperty (HouseCode),
    CONSTRAINT ViewingAppointment_StaffName_fk FOREIGN KEY (StaffName) REFERENCES StaffMember (StaffName));


    The tables i'm referencing to are already created, with each table all having primary keys

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Aren't we missing an "ALTER TABLE ..."?
    George
    Home | Blog

  5. #5
    Join Date
    Nov 2007
    Posts
    16
    Ah right. This is a sample of my code. There's an error message appearing saying 'Syntax error in field definition'

    PotentialBuyerCode VARCHAR (10),
    ALTER TABLE ViewingAppointment
    ADD CONSTRAINT ViewingAppointment_AppointmentCode_pk PRIMARY KEY (AppointmentCode),
    ADD CONSTRAINT ViewingAppointment_PotentialBuyerCode_fk FOREIGN KEY (PotentialBuyerCode) REFERENCES PotentialBuyer (PotentialBuyerCode),
    ADD CONSTRAINT ViewingAppointment_HouseCode_fk FOREIGN KEY (HouseCode) REFERENCES SaleProperty (HouseCode),
    ADD CONSTRAINT ViewingAppointment_StaffName_fk FOREIGN KEY (StaffName) REFERENCES StaffMember (StaffName));

Posting Permissions

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