我从来没有为 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 行
中的引用列数
外键与数量不同
引用的列、表
“问题库”。
你能发现错误吗?
如果您只想自己创建约束,则可以使用 ALTER TABLE
alter table MyTable
add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn ) references MyOtherTable(PKColumn)
我不会推荐 Sara Chipps 提到的用于内联创建的语法,只是因为我宁愿命名自己的约束。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)