gpt4 book ai didi

mysql - SQL中与外键的关系?

转载 作者:搜寻专家 更新时间:2023-10-30 20:30:55 24 4
gpt4 key购买 nike

我正在将模式中的递归关系转换为 sql,并且我正在尝试将表中的两列引用到另一个表中的一列架构如下:

Table: Request
(
mem_id1,
mem id2,
approved
)

所需的外键:

request.mem_id1 will reference member.mem_id  
request.mem_id2 will reference member.mem_id

我尝试了以下方法:

CREATE TABLE members (
mem_id INTEGER,
f_name VARCHAR(10),
l_name VARCHAR(10),
address VARCHAR(40),
name VARCHAR(20),
domain VARCHAR(15),
PRIMARY KEY(mem_id)
)

CREATE TABLE member_phone (
mem_id INTEGER,
phone_no INTEGER,
PRIMARY KEY (mem_id,phone_no),
FOREIGN KEY (mem_id) REFERENCES members (mem_id)
)

CREATE TABLE request (
mem_id1 INTEGER,
mem_id2 INTEGER,
approved BIT(1),
PRIMARY KEY (mem_id1,mem_id2),
FOREIGN KEY (mem_id1,mem_id2) REFERENCES members (mem_id)
)

最佳答案

我相信您需要两个单独的外键:

CREATE TABLE request (
mem_id1 INTEGER,
mem_id2 INTEGER,
approved BIT(1),
PRIMARY KEY (mem_id1, mem_id2),
FOREIGN KEY (mem_id1) REFERENCES members (mem_id),
FOREIGN KEY (mem_id2) REFERENCES members (mem_id)
)

关于mysql - SQL中与外键的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106402/

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