Relational modelling
導入済み
6:35
データベース
A database is a set of true sentences about a domain. This course shows how to model, query, protect and govern data. The first three units model and shape it…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| relationship/rɪˈleɪʃənʃɪp/ | 関係 |
| database/ˈdeɪtəbeɪs/ | An organised collection of data that can be accessed and managed. |
| entity/ˈentɪti/ | エンティティ |
| cardinality/ˌkɑːdɪˈnælɪti/ | カルディナリティ |
| primary key/ˈpraɪməri kiː/ | 主キー |
| foreign key/ˈfɒrən kiː/ | 外鍵 |
| referential integrity/ˌrefəˈrenʃl ɪnˈteɡrɪti/ | the rule that every foreign key value must match an existing primary key |
| many-to-many/ˈmeni tə ˈmeni/ | A database relationship in which each record on either side may be associated with several records on the other side. |
| junction table/ˈdʒʌŋkʃn ˈteɪbl/ | a table that links two entities in a many-to-many relationship |
| one-to-many/wʌn tə ˈmeni/ | A relationship in which one instance of an entity can be associated with several instances of another entity. |
| weak entity/wiːk ˈentɪti/ | an entity that cannot be identified without the entity it depends on |
| participation/pɑːˌtɪsɪˈpeɪʃn/ | whether every entity must take part in a relationship (total) or may not (partial) |
Entities, relationships, keys
- A database 数据库 model is a set of true sentences about a domain. An entity 实体 is a thing we count (a Student); a relationship 联系 connects entities (enrols in). The cardinality 基数 at each end, one or many, turns into keys and tables.
- A primary key 主键 identifies each row uniquely; a foreign key 外键 refers to one. The engine enforces referential integrity 参照完整性: no enrolment can name a student who does not exist.

A many-to-many relationship becomes —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The junction table holds the foreign-key pairs; neither end can hold the other.
Many-to-many needs a table
- A student enrols in many courses, and a course has many students. So the relationship becomes its own junction table 连接表 of foreign-key pairs.
- Rules live in keys:
PRIMARY KEY (student_id, course_id, term)makes a second identical enrolment impossible. The engine rejects it, not the application.
PRIMARY KEY (student, course, term) enforces —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The composite key rejects duplicate triples at the engine level.
A many-to-many relationship can be stored as one foreign key column on either table.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
One column holds one value per row, so it can store only one partner. Many-to-many needs a junction table of key pairs, such as Enrollment(student_id, course_id).
The join path
- Which students take a department's courses? Join Student to Enrollment on
student_id, then Course oncourse_id, then Department ondept_id: three joins, each one following a foreign key 外键. - The sample data has 8 students, 6 courses and 14 enrolments, so the join path gives 14 rows before filtering. Never store a student's name inside Enrollment: the copy will drift out of date.
Use surrogate IDs, not emails, as keys: emails change, surrogate keys never do.
The join path Student-Enrollment-Course-Department yields — rows on the sample data
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
14 enrollments carry the path; each enrollment is one joined row.
Cardinality decides the tables
- Each entity becomes a table. A one-to-many 一对多 relationship becomes a foreign key on the many side:
Course.dept_idpoints to Department. - A one-to-one relationship becomes a foreign key with a
UNIQUEconstraint. A many-to-many 多对多 relationship becomes a junction table. - A weak entity 弱实体 has no key of its own: a Section is identified by
(course_id, section_no), borrowing its owner's key. So a model with $5$ entities, three one-to-many and two many-to-many relationships needs $5 + 2 = 7$ tables.
A model has 4 entities, 3 one-to-many relationships and 2 many-to-many relationships. How many tables does it need?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
One table per entity plus one junction table per many-to-many: 4 + 2 = 6.
Each department offers many courses. Where does the foreign key go?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The many side holds the key: each course names its one department.
Must it take part?
- Cardinality says how many; participation 参与 says whether. Every Course must belong to a Department (total participation), so
Course.dept_idisNOT NULL. A Student may have no advisor yet (partial participation), soStudent.advisor_idmay beNULL. - The diagram's double line, or "at least 1", becomes one keyword in the table. Getting it wrong either blocks valid data or lets orphan rows in.
Every course must belong to a department. The column Course.dept_id should be:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Total participation: the foreign key may not be missing.
Worked example: deleting a student with enrolments
- Student 7 has 3 enrolments. With
ON DELETE RESTRICT(the default), the engine refuses to delete the student while those rows exist. - With
ON DELETE CASCADEit deletes the student and all $\mathbf{3}$ enrolments.SET NULLis impossible here, becausestudent_idis part of the primary key 主键. Choose the rule from the domain: grades are records, so most registries use restrict.
Student 7 has 3 enrollments; ON DELETE CASCADE. Rows removed in total (student plus enrollments)?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
The student row plus 3 enrollment rows.
ON DELETE SET NULL works on Enrollment.student_id.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It is part of the primary key, which cannot be NULL.
Marks that slip away
- Copying a name into the junction table. The Student table owns that fact; copies drift apart.
- The foreign key on the wrong side. One-to-many puts it on the many side:
Course.dept_id, not a list of courses in Department. - Skipping cardinality. It decides the tables: a many-to-many relationship always needs a junction table 连接表.
Recap
- Entities are things you count; relationships connect them; cardinality becomes keys.
- A primary key identifies a row; a foreign key refers to one, and the engine enforces integrity.
- Store each fact once. Cardinality decides the tables:
| Relationship | Becomes |
|---|---|
| one-to-one | a foreign key with UNIQUE |
| one-to-many | a foreign key on the many side |
| many-to-many | a junction table 连接表 of key pairs |
何が問題でしたか?この内容はサイトの運営チームだけが確認します。