0

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
  • The code looks fine - and I cannot reproduce your problem here on my local SQL Server - works just fine (as expected). There must be something else causing these issues..... – marc_s Jan 07 '21 at 17:13
  • @marc_s when I run the query it returns "Commands completed successfully.", even tho the error's still there.. It was supposed to show some kind of error trying to run it, right? – DiogoFolques Jan 07 '21 at 17:17
  • what is exact complete error message? your query looks ok – eshirvana Jan 07 '21 at 18:15
  • "even though the error is still there" -- what do you mean by this? If you run the script and it runs successfully, when and where are you seeing the error? – pmbAustin Jan 07 '21 at 21:28

0 Answers0