Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Foreign key problems

Author  Topic 

shub2079
Starting Member

4 Posts

Posted - 2001-12-06 : 12:09:19
I'm getting an error from SQL Server 2000 and I don't understand why it is happening. I'm creating a bunch of tables, I'll only show the trouble makers though:

 
create table "Queue" (
"FolderID" int not null,
"QueryName" char(32) not null,
"QueueName" char(32) not null,
"DocType" char(12) not null,
"CreatedBy" char(12) not null,
"UpdatedBy" char(12) not null,
"DateCreated" datetime default GetDate() not null,
"DateUpdated" datetime default GetDate() not null,
"Notes" text null,
"QueryFolderID" int not null,
"GridName" char(32) not null,
"GridFolderID" int not null)

go

alter table "Queue"
add constraint "Queue_PK" primary key ("QueueName", "FolderID")

go

create table "Query" (
"FolderID" int not null,
"QueryName" char(32) not null,
"Application" char(12) not null,
"Library" char(12) not null,
"DocType" char(12) not null,
"Code" text not null,
"CreatedBy" char(12) not null,
"UpdatedBy" char(12) not null,
"DateCreated" datetime default GetDate() not null,
"DateUpdated" datetime default GetDate() not null,
"Notes" text null)

go

alter table "Query"
add constraint "Query_PK" primary key ("FolderID", "QueryName")


go


The problem happens when I try to create the foreign key. . .

 
alter table "Queue"
add constraint "Query_Queue_FK1" foreign key (
"QueryName",
"QueryFolderID")
references "Query" (
"QueryName",
"FolderID") on update no action on delete no action

go


When this alter table command executes I get this error:
"There are no primary or candidate keys in the referenced table 'Query' that match the referencing column list in the foreign key 'Query_Queue_FK1'."

Both the reference fields are part of the primary key for for Query however so I don't know what the problem is.

Thanks in advance.

Stan

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2001-12-06 : 12:40:46
when you define the primary key in Query you have the columns in this order

("FolderID", "QueryName")

but you have the opposite order in the foreign key definition

("QueryName","FolderID")


Go to Top of Page

shub2079
Starting Member

4 Posts

Posted - 2001-12-06 : 13:25:31
Thanks, I guess I didn't realize that the order was important there. I should have known better I suppose

Go to Top of Page
   

- Advertisement -