开发者社区 问答 正文

如何在SQL Server中创建外键?

我从未为SQL Server编写过“手工编码”的对象创建代码,而SQL Server和Postgres之间的外键解密似乎并不相同。到目前为止,这是我的SQL:

drop table exams;
drop table question_bank;
drop table anwser_bank;

create table exams
(
    exam_id uniqueidentifier primary key,
    exam_name varchar(50),
);
create table question_bank
(
    question_id uniqueidentifier primary key,
    question_exam_id uniqueidentifier not null,
    question_text varchar(1024) not null,
    question_point_value decimal,
    constraint question_exam_id foreign key references exams(exam_id)
);
create table anwser_bank
(
    anwser_id           uniqueidentifier primary key,
    anwser_question_id  uniqueidentifier,
    anwser_text         varchar(1024),
    anwser_is_correct   bit
);

当我运行查询时,出现以下错误:

消息8139,级别16,状态0,第9行外键中引用列的数量与表'question_bank'的引用列的数量不同。

您能发现错误吗?

展开
收起
心有灵_夕 2019-12-26 22:06:23 903 分享 版权
1 条回答
写回答
取消 提交回答
  • create table question_bank
    (
        question_id uniqueidentifier primary key,
        question_exam_id uniqueidentifier not null,
        question_text varchar(1024) not null,
        question_point_value decimal,
        constraint fk_questionbank_exams foreign key (question_exam_id) references exams (exam_id)
    );
    
    2019-12-26 22:06:38
    赞同 展开评论