1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

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

Discussão em 'Outras Linguagens' iniciado por Stack, Janeiro 7, 2021.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página