我是一只新蜜蜂 我使用过 sql server 2000,之前我的问题是在 sql server 2000 中创建两个表时说 location 和 projects 表 projects 有一个外键引用 location 表,当在 location 中插入值时,项目也更新了不需要在项目表的外键中插入相似的值为什么当我使用插入命令在位置插入值时在 mysql 中是不可能的,而在项目上使用 select 命令时它不显示外键中的值请检查下面的代码
mysql> create table location(
-> id int not null,
-> primary key(id))
-> engine=innodb;
Query OK, 0 rows affected (0.11 sec)
mysql> create table projects(
-> id int,
-> location_id int,
-> foreign key(location_id) references location(id) on update cascade on del
ete cascade)
-> engine=innodb;
Query OK, 0 rows affected (0.31 sec)
mysql> insert into location values('1')
Query OK, 1 row affected (0.34 sec)
mysql> select * from location;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> select * from projects;
Empty set (0.00 sec)
正如您在上面的代码中看到的,在 sql server 2000 中该值可能反射(reflect)在子表中,为什么在 MySql 中不可能当我在位置表中插入时,项目表会自动选择值并在我查询项目表时在选择查询中显示它
最佳答案
从我在示例中看到的情况来看,您没有在项目表中插入任何内容来执行级联更新或删除。
我建议作为一个更好的示例来演示您在执行上述步骤后执行以下操作之后的行为:
insert into projects values (1,1);
select * from projects;
update location set id = 2 where id = 1;
select * from projects;
您最终应该看到的是,最初插入的项目行中的 location_id 将等于 1,然后在位置更新后,项目中的 location_id 应更改为 2。这表明对 id 的更改location 表的 location_id 已级联以更新 projects 表中行的 location_id 字段。
关于MySQL 外键引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6000457/