开发者

MySQL: any way to have a column defaults to auto-increment (or copy from an auto-increment field) but still allow other values to be inserted?

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 th开发者_如何学Pythone 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.


CREATE TABLE Q_n_A (
overall_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
parent_id INT default '0',
title VARCHAR(80),
body VARCHAR(5000)
);

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜