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
 New to SQL Server Programming
 Help with code, can't see what is wrong

Author  Topic 

alexviseu
Starting Member

8 Posts

Posted - 2006-06-28 : 08:39:27
Here is part of the code:
CREATE table Fornecedor (
cod int CONSTRAINT RI_79 PRIMARY KEY,
NC int ,
nome VARCHAR(100) ,
telefone int ,
morada VARCHAR(100) ,
CONSTRAINT RI_78 CHECK(cod>0),
CONSTRAINT RI_80 CHECK(telefone>0),
CONSTRAINT RI_81 CHECK(telefone is not null),
CONSTRAINT RI_83 CHECK(morada is not null),
CONSTRAINT RI_85 CHECK(nome is not null),
CONSTRAINT RI_86 UNIQUE(nome),
CONSTRAINT RI_87 CHECK(NC>0),
CONSTRAINT RI_88 CHECK(NC is not null),
CONSTRAINT RI_89 UNIQUE(NC)
);

CREATE table Encomenda (
cod int CONSTRAINT Fornecedor_não_existe_I FOREIGN KEY REFERENCES Fornecedor(cod),
número_ordem int ,
loja int CONSTRAINT Loja_não_existe_IV FOREIGN KEY REFERENCES Loja(loja),
data_pedido CHAR(8) ,
data_prevista CHAR(8) ,
data_entrega CHAR(8) ,
CONSTRAINT RI_117_72 PRIMARY KEY(cod,número_ordem),
CONSTRAINT RI_71 CHECK(número_ordem>0),
CONSTRAINT RI_74 CHECK(data_pedido is not null),
CONSTRAINT RI_76 CHECK(data_prevista is not null),
CONSTRAINT RI_118 CHECK(loja is not null)
);

CREATE table Encomenda_Disco (
cod int CONSTRAINT Encomenda_não_existe_I FOREIGN KEY REFERENCES Encomenda(cod),
número_ordem int CONSTRAINT Encomenda_não_existe_II FOREIGN KEY REFERENCES Encomenda(número_ordem),
ref int CONSTRAINT Disco_não_existe_VIII FOREIGN KEY REFERENCES Disco(ref),
quantidade int ,
CONSTRAINT RI_121_122_123 PRIMARY KEY(cod,número_ordem,ref),
CONSTRAINT RI_90 CHECK(quantidade>0),
CONSTRAINT RI_91 CHECK(quantidade is not null)
);


Returns me this error when I try to create Encomenda_Disco:
There are no primary or candidate keys in the referenced table 'Encomenda' that match the referencing column list in the foreign key 'Encomenda_não_existe_I'.

I have checked this code for more than an hour, and I'm starting to get prety anoyed with this. Can someone be kind enough to point me the error? :)

nr
SQLTeam MVY

12543 Posts

Posted - 2006-06-28 : 08:40:51
A foreign key needs to reference a unique index.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

alexviseu
Starting Member

8 Posts

Posted - 2006-06-28 : 08:52:10
Ok, Now I see what the problem was. Thanks a lot. :)
Go to Top of Page

CSK
Constraint Violating Yak Guru

489 Posts

Posted - 2006-06-28 : 08:53:00
quote:
Originally posted by alexviseu

Here is part of the code:
CREATE table Fornecedor (
cod int CONSTRAINT RI_79 PRIMARY KEY,
NC int ,
nome VARCHAR(100) ,
telefone int ,
morada VARCHAR(100) ,
CONSTRAINT RI_78 CHECK(cod>0),
CONSTRAINT RI_80 CHECK(telefone>0),
CONSTRAINT RI_81 CHECK(telefone is not null),
CONSTRAINT RI_83 CHECK(morada is not null),
CONSTRAINT RI_85 CHECK(nome is not null),
CONSTRAINT RI_86 UNIQUE(nome),
CONSTRAINT RI_87 CHECK(NC>0),
CONSTRAINT RI_88 CHECK(NC is not null),
CONSTRAINT RI_89 UNIQUE(NC)
);

CREATE table Encomenda (
cod int CONSTRAINT Fornecedor_não_existe_I FOREIGN KEY REFERENCES Fornecedor(cod),
número_ordem int ,
loja int CONSTRAINT Loja_não_existe_IV FOREIGN KEY REFERENCES Loja(loja), data_pedido CHAR(8) ,
data_prevista CHAR(8) ,
data_entrega CHAR(8) ,
CONSTRAINT RI_117_72 PRIMARY KEY(cod,número_ordem),
CONSTRAINT RI_71 CHECK(número_ordem>0),
CONSTRAINT RI_74 CHECK(data_pedido is not null),
CONSTRAINT RI_76 CHECK(data_prevista is not null),
CONSTRAINT RI_118 CHECK(loja is not null)
);

CREATE table Encomenda_Disco (
cod int CONSTRAINT Encomenda_não_existe_I FOREIGN KEY REFERENCES Encomenda(cod),
número_ordem int CONSTRAINT Encomenda_não_existe_II FOREIGN KEY REFERENCES Encomenda(número_ordem),
ref int CONSTRAINT Disco_não_existe_VIII FOREIGN KEY REFERENCES Disco(ref),
quantidade int ,
CONSTRAINT RI_121_122_123 PRIMARY KEY(cod,número_ordem,ref),
CONSTRAINT RI_90 CHECK(quantidade>0),
CONSTRAINT RI_91 CHECK(quantidade is not null)
);


Returns me this error when I try to create Encomenda_Disco:
There are no primary or candidate keys in the referenced table 'Encomenda' that match the referencing column list in the foreign key 'Encomenda_não_existe_I'.

I have checked this code for more than an hour, and I'm starting to get prety anoyed with this. Can someone be kind enough to point me the error? :)



where the column Loja(loja) is availbale in ur previous table
-- KK
Go to Top of Page
   

- Advertisement -