"No primary or candidate keys in referenced table", what am I not seeing?

39 Views Asked by At

I'm trying to create two simple tables and I'm getting this error on the foreign key. Can't figure out why, what I'm doing is pretty much straight forward. Maybe it's just that I'm too much of a newbie to see something obvious. The error is on "REFERENCES Empregado(CodEmp)"

USE Zurrapa

if not exists (select * from dbo.sysobjects 
               where id = object_id(N'[dbo].[Empregado]') )
begin
  CREATE TABLE Empregado (
    CodEmp int NOT NULL 
        CHECK (CodEmp >= 1),
    Nome nvarchar (50) NOT NULL ,
    Função nvarchar (30) NOT NULL ,            
    Salario decimal(10,2) NOT NULL
        DEFAULT 0.0 
        CHECK (Salario >= 0.0)

    CONSTRAINT PK_CodEmp PRIMARY KEY (CodEmp)
  ); 
end

-- ............................................................................

if not exists (select * from dbo.sysobjects 
               where id = object_id(N'[dbo].[Bar]') )
begin
  CREATE TABLE Bar (
    CodBar int NOT NULL 
        CHECK (CodBar >= 1),                    
    LocalizaçãoBar nvarchar(30) NOT NULL,
    CodEmpResp int NOT NULL
        CHECK (CodEmpResp >=1)

    CONSTRAINT PK_CodBar PRIMARY KEY (CodBar), 
    CONSTRAINT LocalizaçãoBar UNIQUE (LocalizaçãoBar),
    CONSTRAINT FK_CodEmpResp FOREIGN KEY (CodEmpResp) 
        REFERENCES Empregado(CodEmp)
        ON UPDATE CASCADE 
  ); 
    
end
0

There are 0 best solutions below