MySQL:有什么方法可以让列默认为自动增量(或从自动增量字段复制)但仍然允许插入其他值?
我正在建立一个问答网站。我想将问题和答案存储在同一个表中:
CREATE TABLE Q_n_A (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
question_id INT NOT NULL,
# type indicates whether the record is a question (1) or an answer (0)
type BOOL,
title VARCHAR(80),
body VARCHAR(5000)
);
当我插入新问题时,我没有该新问题的 question_id
值。因此,我想使用 id
的 auto_incremented 值作为 question_id
的值。因此,在这种情况下,我希望 question_id
默认为 AUTO_INCRMENT
或从 id
复制。
当我插入现有问题的答案时,我已经知道该答案的 question_id
是什么。因此,我想在插入时指定 question_id
的值。
有没有办法在这里做我想做的事:默认 question_id
为 AUTO_INCRMENT
(或从 AUTO_INCRMENT
字段 id
复制>) 但仍然允许指定 question_id
的非默认值?
谢谢。
I'm building a Q&A site. I want to store both questions and answers in the same table:
CREATE TABLE Q_n_A (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
question_id INT NOT NULL,
# type indicates whether the record is a question (1) or an answer (0)
type BOOL,
title VARCHAR(80),
body VARCHAR(5000)
);
When I insert a new question, I don't have a value of question_id
for this new question. As such, I want to use the auto_incremented value of id
as the value for question_id
. So in this case I want question_id
to either default to AUTO_INCREMENT
or to copy from id
.
When I insert an answer to an existing question, I already know what is the question_id
for that answer. As such I want to specify a value for question_id
at the insert.
Is there a way to do what I want here: default question_id
to AUTO_INCREMENT
(or copy from the AUTO_INCREMENT
field id
) but still allow non-default value of question_id
to be specified?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,当有人询问时,只需不插入parent_id,就会导致parent_id = 0
当有人回答时,通过 $_POST 数据或您的表单设置方式将 ovverall_id 插入到 Parent_id 中。
轻松查询给定问题的正确答案
Now when someone ask simply insert no parent_id, resulting it to have parent_id = 0
When someone answers insert into parent_id the ovverall_id, via $_POST data or however your form is setup.
Making it easy to query the correct answers to the given question