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
 General SQL Server Forums
 Data Corruption Issues
 Error 1776

Author  Topic 

Quis25
Starting Member

2 Posts

Posted - 2010-11-06 : 17:46:21
I know that error 1776 is the result of your foreign key not being in another table, but I am getting that same error with it there.

create table PET(
PetID Int not null identity (1,1),
Name Char(20) not null,
Type Char(30) not null,
Breed Char(30) not null,
DOB DateTime not null,
OwnerID Int not null
Constraint PetID_PK primary key (PetID, Breed),
Constraint OwnerID_FK foreign key (OwnerID)
References PET_Owner (OwnerID)
on update cascade,
);



create table breed(
BreedName Char(30) not null,
MinWeight Int not null,
MaxWeight Int not null,
AverageLifeExpectancy Int not null
Constraint BreedName_PK primary key (BreedName),
Constraint BreedName_FK foreign key (BreedName)
References PET (Breed)
on update cascade
);



If there is anyone that could help with this please share your knowledge....

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-11-06 : 18:55:57
I'd say you have the constraint in the wrong table. Looks like it should be more like this:

create table PET(
PetID Int not null identity (1,1),
Name Char(20) not null,
Type Char(30) not null,
Breed Char(30) not null,
DOB DateTime not null,
OwnerID Int not null
Constraint PetID_PK primary key (PetID, Breed),
Constraint OwnerID_FK foreign key (OwnerID)
References PET_Owner (OwnerID)
on update cascade,
Constraint BreedName_FK foreign key (Breed)
References breed (BreedName)

);



create table breed(
BreedName Char(30) not null,
MinWeight Int not null,
MaxWeight Int not null,
AverageLifeExpectancy Int not null
Constraint BreedName_PK primary key (BreedName),
Constraint BreedName_FK foreign key (BreedName)
References PET (Breed)


);

If this doesn't solve it for you, post the code that generates the error.

As a side note, I never allow cascading foreign keys. It is a terrible practice that introduces difficult to track down bugs. It is also the lazy way out of actually programming your updates and deletes. And it can cause history to change, which is never acceptable. In IT, we guarentee two things above all: (1) data reliability and (2) data availability. Everything else we do is secondary. END RANT
Go to Top of Page

Quis25
Starting Member

2 Posts

Posted - 2010-11-06 : 19:03:09
Thank you, right after I switched the reference keys the command finally executed successfully.
Go to Top of Page
   

- Advertisement -