[英]Cannot create table in SQL Compact with Foreign Key
First time using this database because I need a type that's portable and so far it's been a headache. 第一次使用该数据库,因为我需要一种可移植的类型,到目前为止,这一直令人头疼。 I can't seem to figure out what's wrong with the code.
我似乎无法弄清楚代码出了什么问题。
Here's what I'm trying to run - it's in Spanish but you get the gist of it: 这是我要运行的内容-使用西班牙语,但是您可以理解其中的要点:
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
)
I get the error: 我收到错误:
--------------------------- Microsoft Visual Studio --------------------------- SQL Execution Error.
--------------------------- Microsoft Visual Studio -------------------- ------- SQL执行错误。
Executed SQL statement: create table UsuarioRol
执行的SQL语句:创建表UsuarioRol
(
(
UsuarioRolId int primary key identity(1,1),
UsuarioRolId int主键身份(1,1),
Nombre nvarchar(64) not null,
Nombre nvarchar(64)不为null,
NivelAutoridad int not null
NivelAutoridad int不为null
)
)
create table Usuario
创建表Usuario
(
(
UsuarioId int primary key identity(1,1),,
UsuarioId int主键标识(1,1),
UsuarioRolId int foreign key references Usua... Error Source: SQL Server Compact ADO.NET Data Provider Error Message: There was an error parsing the query.
UsuarioRolId int外键引用使用方法...错误源:SQL Server Compact ADO.NET数据提供程序错误消息:解析查询时出错。 [ Token line number = 8,Token line offset = 1,Token in error = create ]
[令牌行号= 8,令牌行偏移量= 1,令牌错误=创建]
--------------------------- OK Help
--------------------------- OK帮助
I don't understand what might be wrong in the syntax. 我不了解语法中可能有什么问题。 Am I missing something here?
我在这里错过了什么吗?
Even tried this, and I get the same error. 甚至尝试了这个,我也遇到同样的错误。
Running the exact same TSQL on a regular ol' SQL Server database, runs perfectly. 在常规的SQL Server数据库上运行完全相同的TSQL,可以完美运行。
Can I conclude that SQL Compact doesn't support foreign keys? 我可以得出结论,SQL Compact不支持外键吗?
I'm not sure if that syntax is supported with SQL Server CE. 我不确定SQL Server CE是否支持该语法。 The following should work:
以下应该有效:
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
Update: 更新:
Actually, what you had should work, just remove "foreign key" in the syntax: 实际上,您应该使用的内容只要删除语法中的“外键”即可:
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
Or this should also work: 或这也应该工作:
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
Source: http://msdn.microsoft.com/en-us/library/ms173393(v=SQL.110).aspx 来源: http : //msdn.microsoft.com/zh-cn/library/ms173393(v=SQL.110).aspx
使用SQl Server Compact一次只能运行一条语句,因此根据所使用的工具,您至少必须将GO和换行符分开。
Not this, 不是这个,
UsuarioRolId int foreign key references UsuarioRol(UsuarioRolId),
but this. 但是这个。
UsuarioRolId int references UsuarioRol(UsuarioRolId),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.