phpMyAdmin 中消失的外键

发布于 2024-07-09 15:24:50 字数 754 浏览 6 评论 0原文

我正在 mysql 中创建一个新表,并尝试向其中一个字段添加外键约束。

CREATE TABLE `onlineorder` (
  `receiptid` varchar(10) NOT NULL default '',
  `delivereddate` date default NULL,
  `cid` int(10) NOT NULL,
  `card#` int(10) default NULL,
  `expire` date default NULL,
  PRIMARY KEY  (`receiptid`),
  FOREIGN KEY (receiptid) REFERENCES purchase
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

但是,在它创建之后,我进入 phpMyAdmin 并导出该表。 似乎外键约束已经消失了。

CREATE TABLE `onlineorder` (
  `receiptid` varchar(10) NOT NULL default '',
  `delivereddate` date default NULL,
  `cid` int(10) NOT NULL,
  `card#` int(10) default NULL,
  `expire` date default NULL,
  PRIMARY KEY  (`receiptid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

phpMyAdmin 是否删除了外键或者我在这里做错了什么?

I am creating a new table inside mysql and I am trying to add a foreign key constraint to one of the fields.

CREATE TABLE `onlineorder` (
  `receiptid` varchar(10) NOT NULL default '',
  `delivereddate` date default NULL,
  `cid` int(10) NOT NULL,
  `card#` int(10) default NULL,
  `expire` date default NULL,
  PRIMARY KEY  (`receiptid`),
  FOREIGN KEY (receiptid) REFERENCES purchase
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

However, after it creates it, I go into phpMyAdmin and export the table. and it seems like the foreign key constraint has disappeared.

CREATE TABLE `onlineorder` (
  `receiptid` varchar(10) NOT NULL default '',
  `delivereddate` date default NULL,
  `cid` int(10) NOT NULL,
  `card#` int(10) default NULL,
  `expire` date default NULL,
  PRIMARY KEY  (`receiptid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Does phpMyAdmin get rid of foreign keys or am I doing something wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫弦 2024-07-16 15:24:50

您需要使用InnoDB引擎来拥有外键。

参考: http://dev.mysql.com /doc/refman/5.0/en/innodb-foreign-key-constraints.html

You need to use the InnoDB engine to have foreign keys.

Ref: http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html

冷了相思 2024-07-16 15:24:50

当您使用 MyISAM 存储引擎定义表时,它接受 FOREIGN KEY 约束,但会默默地丢弃它们。 它会解析它们,但不会将约束存储在表元数据中,因此无法强制执行约束。 当您要求查看数据库对表定义的想法时,正如您所发现的那样,它对该约束一无所知。

CHECK 约束也会发生同样的情况(无论存储引擎如何); 它解析语法并接受它,但随后忽略它。

恕我直言,这对于 MySQL 产品来说是一件可怕的事情。 它接受标准 SQL,没有错误,给您留下这样的印象:它将以标准方式支持约束。 但事实并非如此! 甚至 SHOW WARNINGS 也没有显示 MySQL 已经忽略了该约束。

如果您使用 InnoDB 存储引擎,它确实会注意外键约束。

更新:InnoDB 从 2019 年 4 月 25 日发布的 8.0.16 开始支持检查约束。

When you define a table with the MyISAM storage engine, it accepts FOREIGN KEY constraints, but it silently throws them away. It parses them, but does not store the constraint in the table metadata, and subsequently cannot enforce the constraint. When you ask to look at the database's idea of the table definition, it knows nothing about that constraint, as you have found out.

The same thing happens with CHECK constraints (regardless of the storage engine); it parses the syntax and accepts it, but then ignores it.

IMHO, this is a terrible thing for the MySQL product to do. It accepts standard SQL with no error, leaving you with the impression that it's going to support the constraint in the standard way. But it doesn't! Not even SHOW WARNINGS reveals that MySQL has disregarded the constraint.

If you use the InnoDB storage engine, it does heed the foreign key constraint.

Update: InnoDB supports check constraints as of 8.0.16, released 2019-04-25.

且行且努力 2024-07-16 15:24:50

此外,PHPMyAdmin 会删除提取任何约束,直到创建所有表并插入数据为止。 如果您使用 PHPMySql 来转储表,您将在文件末尾找到 UPDATE 表部分,其中包含所有 ADD CONSTRAINT 条目。

Also, PHPMyAdmin removes extracts any CONSTRAINTS until after all the tables have been created and the data has been inserted. If you use as PHPMySql to dump the table for you, you'll find at the end of the file an UPDATE table section with all your ADD CONSTRAINT entries.

嘿哥们儿 2024-07-16 15:24:50

当您使用 phpmyadmin 管理表时,有两种类型的约束:

  • 内部:当您使用 phpmyadmin 设计器设置约束时,例如存储为内部的约束,该约束将不会包含在导出中。
  • innoDB:导出中包含的这些约束请查看有关它的链接视频

[视频:设置外键约束][1]

There are two type of constraints when you managing your tables with phpmyadmin:

  • internal: when you set constraints with phpmyadmin designer for example the constraints stored as internal,that will not be included in export.
  • innoDB: these constraints included in export check out linked video about it

[Video: Setting up a foreign key constraint][1]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文