mysql - 如何让 MySQL 表相互更新?

标签 mysql

我是编程新手,请原谅我的无知。我正在尝试让 MySQL 表相互更新。例如:当我将数据插入一个表的主键列时,它不会出现在另一表的外键列中。

有人对这个问题有什么建议吗?

谢谢!

其他信息:我使用 phpMyAdmin 创建表,然后添加 SQL 代码来创建外键。代码示例如下。

ALTER TABLE CourseSchedule
ADD FOREIGN KEY (CourseId)
REFERENCES Course(CourseId)

最佳答案

我相信我们这里对documentation有一个轻微的误解。 。

For storage engines supporting foreign keys, MySQL rejects any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.

When an UPDATE or DELETE operation affects a key value in the parent table that has matching rows in the child table, the result depends on the referential action specified using ON UPDATE and ON DELETE subclauses of the FOREIGN KEY clause. MySQL supports five options regarding the action to be taken, listed here

您的外键实际上的作用是确保您无法将与 Courses 表中的条目不对应的值插入 CourseSchedule 表中。

举个例子,假设您尝试在每个星期三 9:00 安排一门 Python 类(class),但您的类(class)表中实际上没有 Python 条目。然后 mysql 将拒绝创建该条目。 Mysql 不能做相反的事情。它不知道有关您的 python 类(class)的详细信息。因此它无法自动为您在“类(class)”表中创建条目。同样,如果 oyu 在类(class)表中创建了一个 entr。 Mysql 无法自动为您创建 CourseSchedule,因为它不知道应该安排在什么时间。

关于mysql - 如何让 MySQL 表相互更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32684850/

相关文章:

mysql - 试图查询 rails 数据库

php - 选择嵌套查询 CodeIgniter

mysql - 对现有 mysql 表进行分区

PHP mysql_query() 无法查询请求的 sql

php - 如何检查键数组是否具有相同的值或多个值

mysql - 从 MySQL 导入大量数据到 Elasticsearch

sql - 从 Oracle 迁移到 MySQL 的工具

php - 计算查询中的行数

php - 无需登录表单即可登录 phpmyadmin

Mysql 在使用 Count 时只返回一行