一个带有2个参考表的外键 [英] One Foreign Key with 2 References Table

查看:87
本文介绍了一个带有2个参考表的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用带有两个参考表的外键?

is it possible to have a foreign key with two reference tables?

这是脚本,我们在插入该表时遇到了问题

here's the script and we're having a problem inserting into this table

CREATE TABLE Class(
    idNumber varchar(30) not null,
    day varchar(10) not null,
    time varchar(20) not null,
    foreign key(idNumber) REFERENCES ThesisMember(idNumber),
    foreign key(idNumber) REFERENCES Faculty(idNumber),
    PRIMARY KEY(idNumber,day,time)
);

CONSTRAINT `class_ibfk_1`
    FOREIGN KEY (`idNumber`) REFERENCES `thesismember` (`idNumber`)) 

SQL语句:

INSERT INTO `thesis`.`class`
    (`idNumber`, `day`, `time`) VALUES ('9990', 'F', '0940-1110')

学院"表的idNumber为"9990".

the Faculty table has the "9990" idNumber.

推荐答案

FK约束要求字段'idNumber'的每个实例都出现在所有引用的表中.您声明只有一个被引用的表具有要插入的值,因此在INSERT尝试中当然会发生错误.

The FK constraint requires that every instance of the field 'idNumber' occur in ALL referenced tables. You state that only one of the referenced tables has the value to be inserted, so of course an error occurs on the INSERT attempt.

这篇关于一个带有2个参考表的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆