混淆形成 MySQL 主键和外键约束

Confusion to form MySQL Primary & Foreign Key constraints

我正在建立一个网站,供教师上传他们的课程然后发布。我正在规划数据库,但我心中有几个问题。考虑以下 tables:

instructors(id(PK), fullname, email, password, created, updated)
categories(id(PK), title, description, created, updated)
courses(id(PK), cat_id(FK), instructor_id(FK), title, description, created, updated)
lessons(id(PK), course_id(FK), title, description, duration, created, updated)

我已经在上述 table 之间建立了基本关系。现在的问题是:

如果我把category_id作为外键放在lessonstable中可以吗?这样我就可以通过加入 table 来 lessons 在一个类别中。对于反向关系,我还可以通过 selecting course.

select category

请帮帮我。提前致谢。

如果一节课有 0 或 1 个 "categories",那么您可以将 category_id 放在 lessons 中。那是恰当和正确的。

如果一节课可以有多个类别,那么您需要一个联结点 table:

create table lesson_categories (
    lesson_id . . .,
    category_id . . .,
    constraint fk_lesson_categories_lesson foreign key (lesson_id) references lessons(id),
    constraint fk_lesson_categories_category foreign key (category_id) references categories(id)
);