gpt4 book ai didi

mysql - 外键说 "NULL"什么时候应该是一个 int?

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:18 24 4
gpt4 key购买 nike

我正在构建一个问答网站,目前我有两个表(问题和答案)我需要将答案表的 id 字段作为问题表中的外键,这样我就可以链接多个答案一个问题。当我输入 not null 时,php 代码无法正常工作?这是我的表格:

create table answers (
a_id int not null auto_increment,
answer varchar(100) not null,
primary key (a_id)
);

create table questions (
q_id int not null auto_increment,
question varchar(100) not null,
a_id int,
primary key (q_id),
foreign key (a_id) references answers (a_id)
);

我需要在外键部分有一个数字,以便用户可以看到他或她的问题的所有答案。我在网上搜索了几个小时,所以我来到了 stackoverflow。我希望有人能提供帮助!

最佳答案

难道“answers”表中没有引用问题 a_id 的外键吗?像下面这样的东西:

create table questions (
q_id int not null auto_increment
, question varchar(100) not null
, primary key (q_id)
);

create table answers (
a_id int not null auto_increment
, answer varchar(100) not null
, q_id int not null
, primary key (a_id)
, foreign key (q_id) references questions(q_id)
);

也许我遗漏了什么,但那是我的主意。

关于mysql - 外键说 "NULL"什么时候应该是一个 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44831965/

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