[英]Cannot create table in SQL Compact with Foreign Key
第一次使用該數據庫,因為我需要一種可移植的類型,到目前為止,這一直令人頭疼。 我似乎無法弄清楚代碼出了什么問題。
這是我要運行的內容-使用西班牙語,但是您可以理解其中的要點:
create table UsuarioRol
(
UsuarioRolId int primary key identity(1,1),
Nombre nvarchar(64) not null,
NivelAutoridad int not null
)
create table Usuario
(
UsuarioId int primary key identity(1,1),
UsuarioRolId int foreign key references UsuarioRol(UsuarioRolId),
Login nvarchar(64) not null,
Password nvarchar(64) not null
)
我收到錯誤:
--------------------------- Microsoft Visual Studio -------------------- ------- SQL執行錯誤。
執行的SQL語句:創建表UsuarioRol
(
UsuarioRolId int主鍵身份(1,1),
Nombre nvarchar(64)不為null,
NivelAutoridad int不為null
)
創建表Usuario
(
UsuarioId int主鍵標識(1,1),
UsuarioRolId int外鍵引用使用方法...錯誤源:SQL Server Compact ADO.NET數據提供程序錯誤消息:解析查詢時出錯。 [令牌行號= 8,令牌行偏移量= 1,令牌錯誤=創建]
--------------------------- OK幫助
我不了解語法中可能有什么問題。 我在這里錯過了什么嗎?
甚至嘗試了這個,我也遇到同樣的錯誤。
在常規的SQL Server數據庫上運行完全相同的TSQL,可以完美運行。
我可以得出結論,SQL Compact不支持外鍵嗎?
我不確定SQL Server CE是否支持該語法。 以下應該有效:
create table UsuarioRol
(
UsuarioRolId int primary key identity(1,1),
Nombre nvarchar(64) not null,
NivelAutoridad int not null
);
GO
create table Usuario
(
UsuarioId int primary key identity(1,1),
UsuarioRolId int,
Login nvarchar(64) not null,
Password nvarchar(64) not null
)
GO
ALTER TABLE [Usuario] ADD CONSTRAINT [FK_Usario_UsarioRol]
FOREIGN KEY ([UsuarioRolId]) REFERENCES [UsuarioRol]([UsuarioRolId]);
GO
更新:
實際上,您應該使用的內容只要刪除語法中的“外鍵”即可:
create table UsuarioRol
(
UsuarioRolId int primary key identity(1,1),
Nombre nvarchar(64) not null,
NivelAutoridad int not null
);
GO
create table Usuario
(
UsuarioId int primary key identity(1,1),
UsuarioRolId int references UsuarioRol(UsuarioRolId),
Login nvarchar(64) not null,
Password nvarchar(64) not null
);
GO
或這也應該工作:
create table UsuarioRol
(
UsuarioRolId int primary key identity(1,1),
Nombre nvarchar(64) not null,
NivelAutoridad int not null
);
GO
create table Usuario
(
UsuarioId int primary key identity(1,1),
UsuarioRolId int,
Login nvarchar(64) not null,
Password nvarchar(64) not null,
foreign key (UsuarioRolId) references UsuarioRol (UsuarioRolId)
);
GO
來源: http : //msdn.microsoft.com/zh-cn/library/ms173393(v=SQL.110).aspx
使用SQl Server Compact一次只能運行一條語句,因此根據所使用的工具,您至少必須將GO和換行符分開。
不是這個,
UsuarioRolId int foreign key references UsuarioRol(UsuarioRolId),
但是這個。
UsuarioRolId int references UsuarioRol(UsuarioRolId),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.