1

我需要一些帮助来建立我的两个表之间的连接

这些是“idPatient 是外键”的表 在此处输入图像描述

我这样填表“tanden”

public void addTandenToDatabase(int id, int fdi, String voorstelling, String toestand) {
    String insertSql = "insert into tanden(id, fdi, voorstelling, toestand) values (:idVal, :fdiVal, :voorstellingVal, :toestandVal)";
    try (Connection con = sql2o.open()) {
        con.setRollbackOnException(false);
        con.createQuery(insertSql)
                .addParameter("idVal", id)
                .addParameter("fdiVal", fdi)
                .addParameter("voorstellingVal", voorstelling)
                .addParameter("toestandVal", toestand)
                .executeUpdate();
    }
}

一切都很好地添加了,但 idPatient 保持为空

在此处输入图像描述

4

3 回答 3

4

如果您想为它设置价值,您应该包括idPatient在您的。insert'外键'并不意味着它会自动设置值。

于 2016-05-27T08:49:28.087 回答
1

您在 tanden 表中的 id 列应设置为主键和自动增量,并且您必须在插入中设置 idPatient

insert into tanden(idPatient, fdi, voorstelling, toestand) values(:idVal,:fdiVal, :voorstellingVal, :toestandVal)";

(你在子表中设置的idPatient已经必须存在于父表中

于 2016-05-27T08:49:38.037 回答
1

您必须通过从患者表中获取将 idPatient 列值插入到 tanden 表中。

于 2016-05-27T08:45:19.983 回答