Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL压缩双外键问题_Sql_Sql Server Ce_Webmatrix - Fatal编程技术网

SQL压缩双外键问题

SQL压缩双外键问题,sql,sql-server-ce,webmatrix,Sql,Sql Server Ce,Webmatrix,以下是数据库的基本版本: 问题 uid-主键-int qid-主键-标识-bigint img-nchar postdate-日期时间 标题-nchar 用户配置文件 电子邮件-nchar UserId-主键-idendity-int 投票 qid-主键-bigint uid-主键-int votedate-日期时间 投票位 我遇到的问题是,我希望投票的uid是UserTable的外键,而投票的qid是Questions的外键(显然是qid)。当我尝试添加与WebMatrix的关系时,我不断得到

以下是数据库的基本版本:

问题
uid-主键-int
qid-主键-标识-bigint
img-nchar
postdate-日期时间
标题-nchar

用户配置文件
电子邮件-nchar
UserId-主键-idendity-int

投票
qid-主键-bigint
uid-主键-int
votedate-日期时间
投票位


我遇到的问题是,我希望投票的uid是UserTable的外键,而投票的qid是Questions的外键(显然是qid)。当我尝试添加与WebMatrix的关系时,我不断得到错误“引用的表必须有主键或候选键”。我做错了什么

外键必须引用另一个表中的唯一键。从您的问题来看,不清楚您是否打算将item1或item2作为PK,或者(item1,item2)的组合是否唯一。如果是组合,则这是来自另一个表的外键的唯一有效链接

问题的主键由两列组成,因此要创建从投票到问题的FK,需要两列才能连接到它。不过,创建一个只有一列的简单PK会更好。然后,你的FK就可以工作了

Votes qid - primary key - bigint uid - primary key - int votedate - datetime vote - bit Questions qid - primary key - identity - bigint uid - int img - nchar postdate - datetime title - nchar 投票 qid-主键-bigint uid-主键-int votedate-datetime 投票位 问题 qid-主键-标识-bigint uid-int img-nchar postdate-日期时间 标题-nchar
您可以针对问题(uid、qid)创建索引,但不要将其设置为PK。

外键必须引用另一个表中的唯一键。从您的问题来看,不清楚您是否打算将item1或item2作为PK,或者(item1,item2)的组合是否唯一。如果是组合,则这是来自另一个表的外键的唯一有效链接

问题的主键由两列组成,因此要创建从投票到问题的FK,需要两列才能连接到它。不过,创建一个只有一列的简单PK会更好。然后,你的FK就可以工作了

Votes qid - primary key - bigint uid - primary key - int votedate - datetime vote - bit Questions qid - primary key - identity - bigint uid - int img - nchar postdate - datetime title - nchar 投票 qid-主键-bigint uid-主键-int votedate-datetime 投票位 问题 qid-主键-标识-bigint uid-int img-nchar postdate-日期时间 标题-nchar
您可以在问题(uid、qid)上创建索引,但不要将其作为主键。

不熟悉WebMatrix,因此我不知道它是否特别存在组合键问题


然而,我确实注意到,问题中的主键是(uid,qid),这与投票中的qid(本身)是问题的外键是不兼容的

不熟悉WebMatrix,所以我不知道它是否特别存在组合键问题

create table UserProfile (
      UserID  integer identity primary key
    , Email   nvarchar(512)
);

create table Question (
      QuestionID integer identity primary key
    , OwnerID    integer 
    , PostDate   datetime
    , Title      nvarchar(1000)
);
alter table Question
    add constraint fk1_Question foreign key (OwnerID) references UserProfile (UserID); 


create table Vote (
      UserID      integer
    , QuestionID  integer
    , VoteDate    datetime
);
alter table Vote
    add constraint pk1_Vote primary key (UserID, QuestionID)
  , add constraint fk1_Vote foreign key (UserID)      references UserProfile (UserID);
  , add constraint fk2_Vote foreign key (QuestionID)  references Question (QuestionID);
然而,我确实注意到,问题中的主键是(uid,qid),这与投票中的qid(本身)是问题的外键是不兼容的

create table UserProfile (
      UserID  integer identity primary key
    , Email   nvarchar(512)
);

create table Question (
      QuestionID integer identity primary key
    , OwnerID    integer 
    , PostDate   datetime
    , Title      nvarchar(1000)
);
alter table Question
    add constraint fk1_Question foreign key (OwnerID) references UserProfile (UserID); 


create table Vote (
      UserID      integer
    , QuestionID  integer
    , VoteDate    datetime
);
alter table Vote
    add constraint pk1_Vote primary key (UserID, QuestionID)
  , add constraint fk1_Vote foreign key (UserID)      references UserProfile (UserID);
  , add constraint fk2_Vote foreign key (QuestionID)  references Question (QuestionID);


我也遇到了同样的问题,并意外地找到了解决办法


您需要确保主键索引表的字段顺序与关系中的字段顺序相同。

我遇到了相同的问题,意外地找到了解决方案


您需要确保主键索引表的字段顺序与关系中的字段顺序相同。

嗯,很多事情。为什么不发布真实的示例,使用表名和列名,而不是通用的
表项
,这样我们(I)可以提供帮助。指出哪个部分是FK,哪个部分是PK,它正试图链接以将名称更改为原始名称。希望它能帮你翻倍外键。。。一路上…嗯,很多事情。为什么不发布真实的示例,使用表名和列名,而不是通用的
表项
,这样我们(I)可以提供帮助。指出哪个部分是FK,哪个部分是PK,它正试图链接以将名称更改为原始名称。希望它能帮你翻倍外键。。。一路上。。。