开发者

TSQL is with error?

create table Tramite
(
ID int primary key identity(1,1),
IDPersona int foreign key references Persona(ID),
Cantidad nvarchar(20),
Precio int,
NumeroDeControl int,
ReciboDeControl int,
NumeroVisa int,
NumeroVisaRecibido int,
PrecioVisa int,
NumeroVisaAutorizado int
)

It says the restriccion isn't valid. Any ideas why?

I am certain I have a table called Persona. I'm running this statement fr开发者_开发问答om within Microsoft Visual Studio 2010. Maybe I need to specify something else?

Why wouldn't this run?

http://i.stack.imgur.com/8Ex7Y.png


create table Tramite 
( 
ID int primary key identity(1,1), 
IDPersona int constraint FK_Tramite_Persona foreign key references Persona(ID), 
Cantidad nvarchar(20), 
Precio int, 
NumeroDeControl int, 
ReciboDeControl int, 
NumeroVisa int, 
NumeroVisaRecibido int, 
PrecioVisa int, 
NumeroVisaAutorizado int 
) 


Take the "foreign key references Persona(ID)" out and just run this after your CREATE TABLE statement.

ALTER TABLE [Tramite] WITH CHECK ADD  CONSTRAINT [Tramite_Persona_FK1] FOREIGN KEY([IDPersona]) REFERENCES [Persona] ([ID])

ALTER TABLE [Tramite] CHECK CONSTRAINT [Tramite_Persona_FK1]


I don't believe you need "foreign key references", only "references", but I'm not sure if that is causing your issue. The examples on msdn only use "references", you can see that Here.


Like Thomas said in his comment on your question, your statement works if a Persona table exists, and has a primary or candidate key that can be referenced by the foreign key constraint.

The following statements work in my environment (SQL Server 2008 R2).

create table Persona (ID int not null primary key)
go
create table Tramite
(
  ID int primary key identity(1,1),
  IDPersona int foreign key references Persona(ID),
  Cantidad nvarchar(20),
  Precio int,
  NumeroDeControl int,
  ReciboDeControl int,
  NumeroVisa int,
  NumeroVisaRecibido int,
  PrecioVisa int,
  NumeroVisaAutorizado int
)
go

Perhaps you should try creating a mock Persona table as a test. If it succeeds, and you believe this table should already exist, then perhaps you created the Persona table under a different schema. I didn't see any schema names assigned to the tables in your screen shot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜