【发布时间】:2018-05-15 18:46:31
【问题描述】:
我在 Postgres 中有以下表格:问题、响应和 question_response。问题和回答之间存在多对多的关系。
questions.sql
CREATE TABLE questions(
id BIGSERIAL PRIMARY KEY,
question VARCHAR(255)
);
respones.sql
CREATE TABLE responses(
id BIGSERIAL PRIMARY KEY,
response VARCHAR(255)
);
question_respone.sql #
CREATE TABLE question_response(
question_id bigint REFERENCES questions ON DELETE CASCADE,
response_id bigint REFERENCES responses ON DELETE CASCADE,
PRIMARY KEY ( question_id, response_id)
);
问题表中有以下数据
id | question
1 Rate the effectiveness of the program?
2 Rate the ease of learning?
3 Rate the duration ?
.....
// there are a list of 20 questions
在响应表中
id | response
100 Poor
200 Average
300 Good
400 Excellent
如何在 Postgres 中编写函数查询以在 question_response 表中插入以下数据。
question_id | response_id
1 100
1 200
1 300
1 400
2 100
2 200
2 300
2 400
3 100
3 200
3 300
3 400
4 100
4 200
4 300
4 400
......... etc for all 20 questions
【问题讨论】:
标签: sql postgresql foreign-keys sql-insert