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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:53:51+00:00 2026-05-25T18:53:51+00:00

I have a database using MySQL 2005. I have two tables, Enrolment and AlertMsg.

  • 0

I have a database using MySQL 2005. I have two tables, Enrolment and AlertMsg.

The primary keys for enrolment are two columns, UnitCode and StudentID. Both these two columns are foreign keys to another table.

The primary keys for AlertMsg are three columns, UnitCode, StudentID and AlertNo.

When I try to make a foreign key with UnitCode in the AlertMsg table, like so:

ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment(UnitCode)

I get the following error:

SQL Execution Error.

Executed SQL statement: ALTER TABLE AlertMsg

ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment (UnitCode)
Error Source: .Net SqlClient Data Provider
Error Message: There are no primary or candidate keys in the referenced table 'Enrolment' that match the referencing column list in the foreign key 'FK_AlertMsg_UnitCo_571DF1D5'.
Could not create constraint. See previous errors.

After some searching it seems that this is because UnitCode isn’t a primary key of Enrolment. But Enrolment’s table definition seems to show that it is. I’m a bit of a newbie at SQL, so I assume that if the far left column of the table definition has a key in it, it means its a primary key of the table.

Can anyone help? Thanks in advance.

  • 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-25T18:53:51+00:00Added an answer on May 25, 2026 at 6:53 pm

    The primary keys for enrolment are two columns, UnitCode and
    StudentID. Both these two columns are foreign keys to another table.

    The primary keys for AlertMsg are three columns, UnitCode, StudentID
    and AlertNo.

    You say “the primary keys for …”. Well actually it is probably not multiple keys but one key that is create using multiple columns. Those columns together is what needs to be unique in your table and therefore you can not just reference one of the columns in a foreign key.

    If you want this statement to work

    ALTER TABLE AlertMsg
    ADD FOREIGN KEY (UnitCode)
    REFERENCES Enrolment(UnitCode)
    

    The values in UnitCode in table Enrolment needs to be unique and you need to add a unique constraint on that column. I guess that is not the case so you can’t add the necessary unique constraint.

    You should probably use this instead:

    ALTER TABLE AlertMsg
    ADD FOREIGN KEY (UnitCode, StudentID)
    REFERENCES Enrolment(UnitCode, StudentID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that using database keys in a URL is a bad thing
I have a huge database with 100's of tables and stored procedures. Using SQL
I am trying to connect to a database using MySQL C++ Connector. I have
I have a drop down list that is dynamically generated using a mySQL database
i am using mysql as my database server. I want to have a backup
I have written a database application using a binary file as storage. it is
I have some existing code that retrieves data from a database using ADO.NET that
I have a database that contains a date and we are using the MaskedEditExtender
I have a database file that is generated on a PC using Sqlite. This
I need to create a Firebird Database programmatically using DBExpress. I have done this

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.