gpt4 book ai didi

mysql - MySQL下如何在表中创建引用项?

转载 作者:行者123 更新时间:2023-11-29 20:54:06 27 4
gpt4 key购买 nike

我想在表中创建引用。例如如果我有一张这样的 table :

CREATE TABLE foo (
id int primary key,
name VARCHAR(64) NOT NULL,
*......*
);

如何创建另一个可以引用 foo 表的 id 和名称的表?

CREATE TABLE goo (
-- I wanna add reference item from foo table :-)
);

最佳答案

CREATE TABLE `goo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`foo_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_goo_foo` (`foo_id`),
CONSTRAINT `FK_goo_foo` FOREIGN KEY (`foo_id`) REFERENCES `foo` (`id`)
);

然后加入选择查询中的表:

SELECT g.*, f.* FROM goo g
INNER JOIN foo f ON f.id = g.foo_id;

关于mysql - MySQL下如何在表中创建引用项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com