Solved

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

Posted on 2013-11-07
5
483 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

785 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