Solved

SQL code for Access database adding multipe foreign keys to a table

Posted on 2013-11-07
5
480 Views
Last Modified: 2013-11-07
I am trying to code a table in Access that has multipe foreign keys. This is what I have so far:

CREATE TABLE Activities
(
[actID] Counter NOT NULL,
[stoID] Int NOT NULL, 
[TaskOrderID] int NOT NULL,
[actdesc] Varchar(100) NOT NULL,
[acttypeID] Int NOT NULL,
PRIMARY KEY (actID, stoID, TaskOrderID),
FOREIGN KEY (stoID) REFERENCES SubTaskOrders (stoID),
FOREIGN KEY (TaskOrderID) REFERENCES TaskOrders (TaskOrderId),
FOREIGN KEY (acttypeid) REFERENCES ActivityType (acttypeid)
);

Open in new window


I am getting the error message: “No unique index found for the referenced field of the primary table.”

I know I must be coding this wrong, but I am also not sure what this error message means, or why I am getting it.

I can't wait to hear what I am doing wrong!

Thank you in advance!
0
Comment
Question by:Megin
  • 3
  • 2
5 Comments
 
LVL 84
ID: 39632135
That means one or more of your referenced tables (SubTaskOrders, TaskOrder or ActivityType) do not have a Unique Constraint on them, or more specifically on the field that you're referencing from those tables. For example. SubTaskOrders.stoID may not be defined as a "Unique Constraint".
0
 

Author Comment

by:Megin
ID: 39632144
So, making something a primary key is not enough to satisfy that?
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39632153
Not necessarily.

Also, be aware I'm referring to the "child" tables, and not your Activities table. In other words, the table named TaskOrders must have a Unique constraint, and that constraint must include the field you're referencing as a foreign key.
0
 

Author Comment

by:Megin
ID: 39632157
Also, I am looking for the code I have to alter a table, because I am assuming that is the only way I am going to be able to change a field to for this Unique Constraint. But the examples are for adding keys, deleting columns, etc. What would be proper code be to add this particular thing to the table?
0
 

Author Closing Comment

by:Megin
ID: 39632211
I just went in and figured out the Alter table syntax to add UNIQUE to all my primary keys. It looks like that worked.

Thank you for the help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now