我试图创建一个名为“Receita”的表,其中包含一个名为Farmaco的表中的外键
但是因为我继续得到这个错误而且我没有真正接受它
"There is already an object named 'Farmaco' in the database."
下面是我创建两个表的鳕鱼
if not exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[Receita]'))
begin
CREATE TABLE Receita(
IDReceita int NOT NULL
CHECK(IDReceita>0),
IDFarmaco int,
CONSTRAINT PK_IDReceita PRIMARY KEY (IDReceita),
CONSTRAINT FK_IDFarmaco FOREIGN KEY (IDFarmaco)
REFERENCES Farmaco (IDFarmaco)
ON UPDATE CASCADE,
);
end
和
if not exists (select * from dbo.sysobjects
where id=object_id(N'[dbo.Farmaco]'))
begin
CREATE TABLE Farmaco(
IDFarmaco int NOT NULL
CHECK(IDFarmaco>0),
IDMedico int,
Tipo varchar(50)
CONSTRAINT PK_IDFarmaco PRIMARY KEY (IDFarmaco),
CONSTRAINT FK_IDMedico7 FOREIGN KEY (IDMedico)
REFERENCES Médico (IDMedico)
ON UPDATE CASCADE,
);
end
谢谢
答案 0 :(得分:0)
我已经看过你的片段,你必须先在“dbo.Receita”之前创建表“dbo.Farmaco”,这样才能将dbo.Farmaco表作为外键引用 dbo.Receita表。
希望这会对你有所帮助。
答案 1 :(得分:0)
if not exists (
select * from dbo.sysobjects where id=object_id(N'[dbo.Farmaco]')
)
,应该是
if not exists (
select * from dbo.sysobjects where id=object_id(N'[dbo].[Farmaco]')
)
查看表名周围的括号!
所以问题是,即使Farmaco表存在,在第二个脚本中if
条件也始终为真。因此,您不断收到错误消息。