如何为三个恋爱关系创建一个交界表?
我有三个名为student
的tabel,<代码>问题和考试
。
学生:
create table if not exists user (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(30) NOT NULL UNIQUE,
username VARCHAR(20) NOT NULL UNIQUE,
firstname VARCHAR(20) NOT NULL UNIQUE,
lastname VARCHAR(20) NOT NULL UNIQUE,
birthday DATE ,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
考试:
create table if not exists exam(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
exam_type ENUM('daily','weekly','monthly','special'),
exam_name varchar(50) NOT NULL,
total_questions int not null,
total_marks int not null
)
问题:
CREATE TABLE IF NOT EXISTS question(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
subject_id INT NOT NULL ,
question_text VARCHAR(1000) ,
option_1 varchar(255) NOT NULL ,
option_2 varchar(255) NOT NULL ,
option_3 varchar(255) NOT NULL ,
option_4 varchar(255) NOT NULL ,
correct_option ENUM('option_1','option_2','option_3','option_4'),
subject ENUM('physics','chemistry','math','english'),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- 在多次考试中可以问一个问题。
- 学生可以参加多次考试。
- 多个学生可以参加考试。
我需要为每个Question> Question
为每个考试>考试
提供students
给出的答案。 我已经定义了我的交界表如下:
CREATE TABLE IF NOT EXISTS user_answer(
user_id INT NOT NULL,
exam_id INT NOT NULL,
question_id INT NOT NULL,
FOREIGN KEY (user_id) references user(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (exam_id) references exam(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (question_id) references question(id) ON UPDATE CASCADE ON DELETE CASCADE
);
有这样的关系可以吗?还是还有另一种更好的方法?
I have three tabels named student
, question
and exam
.
student:
create table if not exists user (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(30) NOT NULL UNIQUE,
username VARCHAR(20) NOT NULL UNIQUE,
firstname VARCHAR(20) NOT NULL UNIQUE,
lastname VARCHAR(20) NOT NULL UNIQUE,
birthday DATE ,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
exam:
create table if not exists exam(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
exam_type ENUM('daily','weekly','monthly','special'),
exam_name varchar(50) NOT NULL,
total_questions int not null,
total_marks int not null
)
question:
CREATE TABLE IF NOT EXISTS question(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
subject_id INT NOT NULL ,
question_text VARCHAR(1000) ,
option_1 varchar(255) NOT NULL ,
option_2 varchar(255) NOT NULL ,
option_3 varchar(255) NOT NULL ,
option_4 varchar(255) NOT NULL ,
correct_option ENUM('option_1','option_2','option_3','option_4'),
subject ENUM('physics','chemistry','math','english'),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- A question can be asked in multiple exam.
- A student can appear in multiple exam.
- A exam can be attended by multiple students.
And I need to store the answer given by the students
for each question
for every exam
.
And I have defined my junction table as below:
CREATE TABLE IF NOT EXISTS user_answer(
user_id INT NOT NULL,
exam_id INT NOT NULL,
question_id INT NOT NULL,
FOREIGN KEY (user_id) references user(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (exam_id) references exam(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (question_id) references question(id) ON UPDATE CASCADE ON DELETE CASCADE
);
Is it ok to have such relationship or is there another better way for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论