无法使用外键在SQL Compact中创建表
第一次使用这个数据库是因为我需要一个可移植的类型,到目前为止,这是一个令人头痛的问题。我似乎不知道代码出了什么问题 下面是我想说的内容——是西班牙语,但你可以理解其中的要点:无法使用外键在SQL Compact中创建表,sql,tsql,sql-server-ce,Sql,Tsql,Sql Server Ce,第一次使用这个数据库是因为我需要一个可移植的类型,到目前为止,这是一个令人头痛的问题。我似乎不知道代码出了什么问题 下面是我想说的内容——是西班牙语,但你可以理解其中的要点: create table UsuarioRol ( UsuarioRolId int primary key identity(1,1), Nombre nvarchar(64) not null, NivelAutoridad int not null ) create table Usuario ( UsuarioI
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语句:创建表USUARIROL
(
USUARIROLID int主键标识(1,1)
Nombre nvarchar(64)不为空
NivelAutoridad int不为空
)
创建表Usuario
(
UsuarioId int主键标识(1,1)
USUARIROLID int外键引用Usua…错误源:SQL
Server Compact ADO.NET数据提供程序错误消息:出现错误
正在分析查询。[令牌行编号=8,令牌行偏移量=1,令牌
错误=创建]
---------------------------好的,救命
我不明白语法中可能有什么错误。我是不是遗漏了什么
甚至试过这个,我也犯了同样的错误
在普通的SQL Server数据库上运行完全相同的TSQL,运行非常完美
我可以断定SQL Compact不支持外键吗?使用SQL Server Compact一次只能运行一条语句,因此根据您使用的工具,您必须至少使用GO和new line分隔。我不确定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
来源:不是这个
UsuarioRolId int foreign key references UsuarioRol(UsuarioRolId),
但是这个
UsuarioRolId int references UsuarioRol(UsuarioRolId),