【问题标题】:Can't INSERT values into tables无法将值插入表中
【发布时间】:2013-05-27 11:21:28
【问题描述】:

我在 MySQL 数据库中有 3 个表:

CREATE TABLE bank(
idBank int(11) NOT NULL PRIMARY KEY auto_increment,
nameBank varchar(50)
);
CREATE TABLE region(
idRegion int(11) NOT NULL PRIMARY KEY auto_increment,
address varchar(50) NOT NULL,
district varchar(30) NOT NULL,
city varchar(50) NOT NULL,
tel varchar(15) NOT NULL
);
CREATE TABLE branch(
idBranch int(11) NOT NULL PRIMARY KEY auto_increment,
idBank int(11) NOT NULL,
idRegion int(11) NOT NULL,
quantity int(50) NULL,
president varchar(60) NULL,
FOREIGN KEY (idBank) REFERENCES bank (idBank),
FOREIGN KEY (idRegion) REFERENCES region (idRegion)
);

当我尝试将值插入表时,它适用于前两个,但不能记录到分支表中。为什么?

【问题讨论】:

  • 您的 INSERT 代码在哪里?
  • 请提供一些代码,我们不知道你的代码有什么问题
  • 请确保您尝试插入分行的 idBank 和 idRegion 在相应的银行和地区表中。

标签: mysql sql foreign-key-relationship sql-insert


【解决方案1】:

你尝试插入什么,你得到了什么错误? 由于您的表具有外键约束,这意味着您不能在引用表中不存在外键值的这些表中插入新行。 英文:如果没有对应的银行所属的银行,则不能在分支表中添加记录,区域也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多