Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

757 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

18 Experts available now in Live!

Get 1:1 Help Now