Relational databases: a key and a relationship
แนะนำใหม่
5:36
ฐานข้อมูล
ห้องสมุดโรงเรียนจัดเก็บข้อมูลการยืมหนังสือในสเปรดชีตเดียว แต่ละแถวมีชื่อผู้เรียน ชั้นเรียน หนังสือ และวันที่ เมื่อผู้เรียนเปลี่ยนชั้นเรียน จำนวนหลายสิบแถวต้อง…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| relational database/rɪˈleɪʃənl ˈdeɪtəbeɪs/ | A database that organises data into tables connected through matching key values. |
| primary key/ˈpraɪməri kiː/ | คีย์หลัก |
| foreign key/ˈfɒrən kiː/ | คีย์ภายนอก |
| record/ˈrekɔːd/ | A stored collection of information about an item, person or event. |
| field/fiːld/ | A named item of data within a database record. |
| normalisation/ˌnɔːməlaɪˈzeɪʃn/ | Organising relational database tables to reduce unnecessary duplication and avoid update, insertion and deletion anomalies. |
อะไรจะอธิบายการสังเกตนี้ได้?
- การซ้ำที่อยู่ผู้ใช้ในคำสั่งซื้อทุกฉบับอาจทำให้เกิดบันทึกที่ขัดแย้งกันเมื่อที่อยู่มีการเปลี่ยนแปลง
What did Codd propose in 1970?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The relational model.
สร้างโมเดล
- ตารางแบบสัมพันธ์ประกอบด้วยแถวและคุณสมบัติ
- คีย์หลัก ใช้ระบุแถว; คีย์ภายนอก เชื่อมโยงไปยังคีย์ที่ถูกอ้างอิงในตารางอื่น
- การทำให้เป็นมาตรฐานสามารถลดการซ้ำซ้อนที่ไม่จำเป็นและความผิดปกติในการอัปเดตwhile preserving meaningful relationships
- คีย์หลัก: ชุดคุณสมบัติที่ใช้ระบุแถวของตารางได้อย่างชัดเจน
- คีย์ภายนอก: ชุดคุณสมบัติที่อ้างอิงถึงคีย์ในตารางอื่น
Which field is the best primary key for Students?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
It is unique and does not change.
เลือกหลักฐานมาทดสอบสมมติฐาน
- การ JOIN รวมแถวตามเงื่อนไขที่กำหนด
- จำนวนผลลัพธ์ขึ้นอยู่กับชนิดความสัมพันธ์และการกรองข้อมูล
- ข้อจำกัดคีย์ภายนอกใช้บังคับกฎความสัมพันธ์เท่านั้น ไม่ได้เข้ารหัสข้อมูลส่วนบุคคลโดยอัตโนมัติ
ในห้องปฏิบัติการ: ออกแบบชุดข้อมูลผู้ใช้-คำสั่งซื้อสังเคราะห์ขนาดเล็กที่ไม่มีข้อมูลส่วนบุคคลจริง กำหนดคีย์ ทดสอบการแทรกข้อมูลที่ซ้ำหรือขาดการอ้างอิง แล้วตรวจสอบผลลัพธ์การ query กับตารางเป้าหมายที่คำนวณด้วยมือ
A foreign key holds the primary key of another table.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
It links the tables.
ทำงานจากปริมาณที่ทราบค่า
- ที่ทราบ: ลูกค้าสี่ราย มีคำสั่งซื้อตามลำดับ 2, 0, 3 และ 1 รายการ
- Inner Join ระหว่างลูกค้ากับคำสั่งจะคืนผล 2+0+3+1 = 6 แถว
- Left join ยังคงข้อมูลลูกค้าที่ไม่มีคำสั่งซื้อเป็นแถวที่มีค่า null-matched ซึ่งให้ 7 ในที่นี้
Your turn: Three customers have 1, 3 and 2 orders. How many rows does the customer-order inner join return?
One source row, several matches
Each source row contributes one result per matching record. A left join retains an unmatched source row as one NULL-matched row.
What problem does normalisation prevent?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Store each fact once.
ตรวจสอบข้อสรุปและขอบเขต
- คีย์หลักไม่จำเป็นต้องเป็นชื่อคน
- การ JOIN ไม่ใช่การต่อกันของตารางอย่างง่าย
- ใช้คำถามที่มีพารามิเตอร์สำหรับข้อมูลที่ไม่เชื่อถือได้ แทนการสร้าง SQL จากข้อความธรรมดา
ไม่ถูกต้อง: คีย์ภายนอกไม่ได้เข้ารหัสข้อมูลทุกแถวโดยอัตโนมัติ คีย์ภายนอกคือลิงก์อ้างอิงระหว่างตาราง ซึ่งตรวจสอบว่าแถวที่ถูกอ้างอิงมีอยู่จริงเท่านั้น ไม่มีการเข้ารหัส การปกป้องเนื้อหาข้อมูลเป็นกลไกความปลอดภัยแยกต่างหาก
- ตารางแบบสัมพันธ์ประกอบด้วยแถวและคุณสมบัติ
- การ JOIN รวมแถวตามเงื่อนไขที่กำหนด
- ระวัง: คีย์หลักอาจไม่ใช่ชื่อคน
Four students have 0, 2, 2 and 3 enrolments. How many rows does an inner join of Students and Enrolments return?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
0 + 2 + 2 + 3 = 7; the student with none is omitted.
Why is Ben missing from the join result?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
An inner join returns only matches.
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
Integrity and transactions
- Referential integrity: an enrolment cannot refer to a StudentID that does not exist.
- A transaction is all-or-nothing (atomic): moving a student between classes must not leave them in both or neither.
An atomic transaction either completes fully or not at all.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
No half-moved records.
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
A name is not a key. Two students can share a name, and names change. Use a unique identifier that never changes as the primary key.
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
A relational database stores entities in tables of records and fields. A primary key identifies each record; a foreign key links tables; normalisation removes repetition; an inner join returns only matching rows; integrity rules and atomic transactions keep data consistent.
Design tables for a school library (books, members, loans) with primary and foreign keys, and write a query listing the titles borrowed by member 42.
Tables, keys, query.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้