Why databases and the relational model
แนะนำใหม่
17:15
ฐานข้อมูล & โมเดลเชิงสัมพันธ์
ก่อนมีฐานข้อมูล แต่ละโปรแกรมจะเก็บไฟล์แบนของตัวเอง — ไฟล์ละหนึ่งต่อโปรแกรม สมมติว่าเป็นร้านขายของ โปรแกรมขายของ โปรแกรมออกบิล และโปรแกรมจัดส่ง…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| disk/dɪsk/ | A storage device that holds programs and data even when power is off. |
| database/ˈdeɪtəbeɪs/ | An organised collection of data that can be accessed and managed. |
| trees | Structures that organise data in levels, with items linked like branches. |
| file layout | The arrangement of data within a file, including the order of its fields. |
| table/ˈteɪbl/ | ตาราง |
| file-based approach | A way of storing data in separate files managed by individual programs. |
| relational database/rɪˈleɪʃənl ˈdeɪtəbeɪs/ | ฐานข้อมูลเชิงสัมพันธ์ |
| flat files/flæt faɪlz/ | ไฟล์แบน |
| Data redundancy/ˈdeɪtə rɪˈdʌndənsi/ | The same data, a customer's address, is held in several files, wasting storage and effort. |
| Data inconsistency/ˈdeɪtə ˌɪnkənˈsɪstənsi/ | The copies are updated separately, so they drift apart and nobody knows which is right. |
| Data dependence | Programs are tied to the file format , so a change to the layout means rewriting every program that uses the file. |
| file format | A way of organising and storing data in a file. |
| Integrity/ɪnˈteɡrɪti/ | The accuracy, completeness and protection of data against unauthorised or unintended alteration. |
| field/fiːld/ | ฟิลด์ |
| DBMS/ˌdiː biː em ˈes/ | Database management system: software for defining, storing, querying, updating and controlling access to a database. |
| data independence | The ability to change data storage without changing the programs that use it. |
| access rights/ˈækses raɪts/ | The actions a user or process is permitted to perform on a resource. |
| backups | Extra copies of data kept so it can be restored if lost or damaged. |
| joined | Combined using matching values in related fields from two tables. |
| rows | Horizontal lines in a table, each containing one record. |
| columns | Vertical lines in a table, each containing one field for every record. |
| relation/rɪˈleɪʃn/ | A table that stores data in rows and columns. |
| entity/ˈentɪti/ | เอนทิตี |
| record/ˈrekɔːd/ | บันทึก |
| tuple/ˈtuːpl/ | ทูเปิล |
| attribute/ˈætrɪbjuːt/ | แอททริบิวต์ |
| primary key/ˈpraɪməri kiː/ | คีย์หลัก |
| null/nʌl/ | A marker showing that a field has no value. |
| composite key/ˈkɒmpəzɪt kiː/ | คีย์รวม |
| candidate key/ˈkændɪdeɪt kiː/ | คีย์ผู้สมัคร |
| secondary key/ˈsekəndəri kiː/ | A non-primary field that is indexed for fast searching; indexing builds an index on a field so look-ups and joins run faster. |
| index/ˈɪndeks/ | A position marker or lookup structure used to locate data within a collection or database. |
| indexing/ˈɪndeksɪŋ/ | การจัดดัชนี |
| foreign key/ˈfɒrən kiː/ | คีย์ภายนอก |
| Relationships | Links between entities that show how their records are connected. |
| Referential integrity/ˌrefəˈrenʃl ɪnˈteɡrɪti/ | A rule requiring every foreign-key value to match an existing primary key. |
| One-to-one/wʌn tuː wʌn/ | A relationship where each record in either table links to at most one in the other. |
| One-to-many/wʌn tə ˈmeni/ | A relationship where one record links to many others, each linked back to at most one. |
| Many-to-many/ˈmeni tə ˈmeni/ | A relationship where many records in each table can link to many in the other. |
| orphan records | Records that refer to another record that no longer exists. |
| insert/ˈɪnsɜːt/ | To add a new item to a data structure, file or table. |
| delete/dɪˈliːt/ | To remove a record from a table. |
| cascade/kəˈskeɪd/ | Connecting stages one after another, so the output of one feeds the input of the next. |
เอกสารที่ IBM ไม่อยากเห็น
- ในช่วง 1970 นักวิจัย IBM ชื่อ Edgar Codd ได้เผยแพร่บทความยาว 12 หน้าเสนอให้จัดเก็บข้อมูลในรูปแบบตารางอย่างง่าย เชื่อมโยงกันด้วยค่าร่วมกัน และขอข้อมูลผ่านภาษาที่บอก สิ่งที่คุณต้องการ แทนที่จะบอก ตำแหน่งบนดิสก์
- IBM ขายฐานข้อมูลที่จัดเก็บข้อมูลแบบต้นไม้ที่ผูกกับโครงสร้างไฟล์อยู่แล้ว จึงเพิกเฉยต่อเขาเป็นเวลานาน โปรแกรมที่ถูกเขียนขึ้นสำหรับไฟล์เหล่านั้นต้องถูกเขียนใหม่ทุกครั้งเมื่อโครงสร้างเปลี่ยนแปลง
- ในช่วง 1980 ธนาคารสายการบิน และหน่วยงานรัฐบาลทุกแห่งกำลังย้ายมาใช้ตารางของ Codd หลังผ่านไปครึ่งศตวรรษ ระบบเหล่านี้ยังคงใช้งานอยู่บนพื้นฐานนี้
- บทเรียนนี้คือปัญหาของ วิธีการใช้ไฟล์ (file-based approach), วิธีที่ ฐานข้อมูลเชิงสัมพันธ์ แก้ไขปัญหาเหล่านั้น และคำศัพท์ที่คุณต้องใช้ให้ถูกต้อง
วิธีการใช้ไฟล์ (The file-based approach)
- ก่อนมีฐานข้อมูล Each program kept its own flat files: โปรแกรมขายมีไฟล์ลูกค้า โปรแกรมบัญชีมีอีกไฟล์โปรแกรมจัดส่งมีอีกไฟล์
- แต่ละไฟล์มีโครงสร้างตายตัวที่ code ของโปรแกรมขึ้นอยู่กับ และไม่มีอะไรนอกเหนือจากโปรแกรมนี้รู้ว่าข้างในมีอะไร
- It works for one small program. The trouble starts when the second program needs the same data.

สามโปรแกรม สามสำเนาของลูกค้า

ข้อจำกัด
- ความซ้ำซ้อนของข้อมูล: ข้อมูลเดียวกัน เช่น ที่อยู่ลูกค้า ถูกเก็บไว้ในหลายไฟล์ สิ้นเปลืองพื้นที่จัดเก็บและความพยายาม
- ความไม่สอดคล้องของข้อมูล: สำเนาถูกอัปเดตแยกกัน ทำให้ข้อมูลเบี่ยงเบนออกจากกันและไม่มีใครรู้ว่าอันไหนถูกต้อง
- การพึ่งพาข้อมูล: โปรแกรมผูกติดกับรูปแบบไฟล์ ดังนั้นการเปลี่ยนแปลงโครงสร้างหมายถึงการเขียนโปรแกรมใหม่ทั้งหมดที่ใช้ไฟล์นั้น
- ความสมบูรณ์ของข้อมูล ควบคุมยาก แชร์ข้อมูลได้อย่างปลอดภัยยาก การค้นหาข้ามไฟล์ช้า และความปลอดภัยไม่สามารถตั้งค่าได้ต่อ field

Storing a customer's address in several separate files leads to:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The same data held in many places (redundancy) can be updated separately and become inconsistent.
In a flat-file system the same data is often duplicated across files, which can become inconsistent when only one copy is updated.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
That redundancy and the resulting inconsistency is the core problem the relational model solves.
Which are limitations of the file-based approach? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Redundancy, inconsistency and data dependence are the three named limitations. Tables and joins belong to the relational approach that replaces it.
ฐานข้อมูลเชิงสัมพันธ์ตอบคำถามเหล่านี้อย่างไร
- ฐานข้อมูลเชิงสัมพันธ์ เก็บข้อมูลใน ตาราง ที่จัดการโดยซอฟต์แวร์เดียว DBMS ซึ่งทุกโปรแกรมใช้
- ทุกข้อเท็จจริงถูกเก็บ ครั้งเดียว ดังนั้นความซ้ำซ้อนและความไม่สอดคล้องหายไป: ที่อยู่ถูกเปลี่ยนในที่เดียวและทุกโปรแกรมเห็นการเปลี่ยนแปลง
- โปรแกรมขอข้อมูลจาก DBMS ตามชื่อ ดังนั้นการจัดเก็บสามารถเปลี่ยนแปลงได้โดยไม่ทำให้โปรแกรมเปลี่ยนไป: ความเป็นอิสระของข้อมูล
- กฎความถูกต้อง (integrity rules) สิทธิ์การเข้าถึง และการสำรองข้อมูลถูกบังคับใช้แบบรวมศูนย์ และตารางใดก็สามารถค้นหาหรือเชื่อมโยงกับตารางอื่นได้

ข้อมูลชุดเดียว ผู้ดูแลประตูเดียว

How does a relational database remove data inconsistency?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
One copy, one update, no drift. The DBMS gives every program the same current value.
คำศัพท์: ตาราง แถว และคอลัมน์
- ตาราง (relation) คือตารางข้อมูลที่มีแถวและคอลัมน์ มีตารางหนึ่งสำหรับแต่ละประเภทของ เอนทิตี (entity) หรือสิ่งที่เป็นที่เก็บข้อมูล:
CUSTOMER,ORDER,PRODUCT. - บันทึก (record) คือแถวหนึ่ง ซึ่งเป็นตัวอย่างหนึ่งของเอนทิตี; คำศัพท์ทางการคือ ทิวเปิล (tuple).
- ฟิลด์ (field) คือกอลัมน์หนึ่ง เป็นข้อมูลชิ้นหนึ่งเกี่ยวกับแต่ละบันทึก; คำศัพท์ทางการคือ แอททริบิวต์ (attribute).

แถวเป็นบันทึก คอลัมน์เป็นฟิลด์ และคอลัมน์หนึ่งชี้ไปยังตารางอื่น

Read a relational table with SELECT
A relational table is just rows (records) and columns (fields). WHERE keeps the rows that match a condition; SELECT then keeps only the columns you asked for.
คำศัพท์: คีย์
- คีย์หลัก (primary key) คือฟิลด์หรือกลุ่มฟิลด์ที่ระบุตัวบันทึกได้อย่างไม่ซ้ำซ้อน ไม่สามารถเป็นค่าว่าง (null) และไม่มีการซ้ำซ้อน คีย์ผสม (composite key) คือคีย์หลักที่ประกอบด้วยฟิลด์สองขึ้นไป
- คีย์ทางเลือก (candidate key) คือฟิลด์หรือกลุ่มใด ๆ ที่สามารถใช้เป็นคีย์หลักได้ คีย์รอง (secondary key) คือฟิลด์ที่ไม่ใช่คีย์หลักซึ่งมีการสร้างดัชนีเพื่อการค้นหาที่รวดเร็ว การสร้างดัชนี (indexing) สร้างดัชนีบนฟิลด์เพื่อให้การค้นหาและการเชื่อมโยงเร็วขึ้น
- คีย์ภายนอก (foreign key) คือฟิลด์whose value ตรงกับคีย์หลักของตารางอื่น เพื่อเชื่อมโยงทั้งสองตาราง ในรูปย่อ คีย์หลักจะขีดเส้นใต้และระบุคีย์ภายนอก:
CUSTOMER(CustomerID, Name, Phone)
ORDER(OrderID, CustomerID, OrderDate) -- CustomerID is a foreign key → CUSTOMER

A primary key:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The primary key uniquely identifies each row. A foreign key is the one that links to another table.
Match each kind of key to what it is.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The primary key identifies a row; a foreign key links to another table; composite = several fields together; candidate = a possible primary key.
A field whose value matches the primary key of another table is a ______ key.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The foreign key is what creates the relationship between two tables.
ตัวอย่างวิธีทำ: ชื่อกองส่วนต่างๆ ของฐานข้อมูลห้องสมุด
- ห้องสมุดบันทึกหนังสือ สมาชิก และยืมหนังสือแต่ละครั้ง ระบุเอนทิตี คีย์หลักของแต่ละรายการ และคีย์ภายนอก
- เอนทิตี:
BOOK,MEMBER,LOAN. คีย์หลัก:BookID,MemberID,LoanIDซึ่งเลือกเพราะมีความไม่ซ้ำซ้อนสำหรับทุกบันทึกและไม่เคยว่าง; ชื่อเรื่องหรือชื่อจะไม่สามารถใช้ได้ เนื่องจากหนังสือสองเล่มอาจมีชื่อเรื่องเดียวกัน LOAN(LoanID, BookID, MemberID, DateOut, DateDue):BookIDและMemberIDเป็นคีย์ภายนอก แต่ละตัวตรงกับคีย์หลักของตารางของตนเอง เบอร์โทรศัพท์ของสมาชิกควรอยู่ในMEMBERไม่ใช่ในรายการยืมทุกครั้ง

In LOAN(LoanID, BookID, MemberID, DateOut), BookID and MemberID are ____ keys.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Each matches the primary key of another table, BOOK and MEMBER, and links the loan to them.
ความสัมพันธ์และความถูกต้องของการอ้างอิง
- ความสัมพันธ์ เชื่อมโยงเอนทิตีสองชนิด หนึ่งต่อหนึ่ง: สมาชิกหนึ่งคนมีบัตรห้องสมุดหนึ่งใบ หนึ่งต่อหลาย: สมาชิกหนึ่งคนมีการยืมหลายครั้ง; การยืมแต่ละครั้งเป็นของสมาชิกหนึ่งคน หลายต่อหลาย: หนังสือหนึ่งเล่มมีผู้เขียนหลายคน และผู้เขียนคนหนึ่งเขียนหนังสือหลายเล่ม
- Referential integrity means every foreign-key value must match an existing primary key in the table it refers to: no loan for a member who does not exist, no orphan records.
- The DBMS enforces it: it refuses an insert with an unknown foreign key, and refuses to delete a record that other records still refer to.
Referential integrity ensures that:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
It prevents orphan records — you cannot reference a primary key that does not exist.
Match each relationship to its type.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Count how many of each entity can be linked to one of the other. Many-to-many needs a link table to store.
ตัวอย่างวิธีทำ: ความถูกต้องของการอ้างอิงป้องกันอะไรได้บ้าง
- ลูกค้าที่มีคำสั่งซื้อค้างชำระสามรายการถูกลบออกจากตาราง
CUSTOMERอธิบายว่าความถูกต้องของการอ้างอิงทำงานอย่างไร - Each order's
CustomerIDis a foreign key that must match an existing customer. Deleting the customer would leave three orders pointing at a record that no longer exists: orphan records. - ดังนั้น DBMS จึงปฏิเสธการลบจนกว่าคำสั่งซื้อจะถูกลบหรือโอนไปใหม่ หรือหากตั้งค่าให้ cascade จะลบคำสั่งซื้อไปด้วย ในทุกกรณีคำสั่งซื้อจะไม่เคยอ้างถึงลูกค้าที่ไม่มีอยู่จริง
- บอกกฎว่าอะไรคือกฎ อะไรจะทำลายกฎนั้น และ DBMS จะทำอะไร

Referential integrity allows a customer to be deleted while orders in another table still refer to that customer.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
That would create orphan records. The DBMS refuses the deletion, or cascades it to the orders, so every foreign key keeps pointing at an existing record.
นิยามที่กรรมการสอบยอมรับ
| พจน์ | นิยาม |
|---|---|
| เอนทิตี | สิ่งที่เป็นที่เก็บข้อมูล แทนด้วยตารางหนึ่ง |
| แอททริบิวต์ | ข้อมูลชิ้นหนึ่งเกี่ยวกับเอนทิตี คอลัมน์หนึ่ง |
| ทิวเปิล | แถวหนึ่งของตาราง บันทึกหนึ่ง |
| คีย์หลัก | แอททริบิวต์หรือกลุ่มแอททริบิวต์ที่ระบุตัวทิวเปิลแต่ละตัวได้อย่างไม่ซ้ำซ้อน |
| คีย์ทางเลือก | แอททริบิวต์หรือกลุ่มที่สามารถเลือกเป็นคีย์หลักได้ |
| คีย์รอง | แอททริบิวต์ที่มีดัชนีเพื่อค้นหาตารางอย่างรวดเร็ว |
| คีย์ภายนอก | แอททริบิวต์ในตารางหนึ่งที่เป็นคีย์หลักของอีกตารางหนึ่ง เพื่อสร้างการเชื่อมโยง |
| ความถูกต้องของการอ้างอิง | คีย์ภายนอกทุกค่าอ้างถึงคีย์หลักที่มีอยู่จริง |
คะแนนที่หลุดหายไป
- ทิวเปิลคือแถว, แอททริบิวต์คือคอลัมน์. สลับกันจะได้คะแนนเต็มไม่ได้
- คำว่า "ไม่ซ้ำซ้อน" (unique) เพียงอย่างเดียวไม่ได้นิยามคีย์หลัก; มัน ระบุตัวบันทึก และเป็นค่าว่างไม่ได้
- คีย์ภายนอก สามารถซ้ำได้: ลูกค้าหนึ่งคน มีคำสั่งซื้อหลายรายการ ส่วนคีย์หลักนั้นห้ามซ้ำ
- คีย์รอง ใช้สำหรับการค้นหาที่เร็ว ไม่ใช่สำหรับการระบุตัวตน อย่าเรียกว่า "คีย์หลักลำดับที่สอง"
คุณเข้าใจแล้ว
- วิธีการใช้ไฟล์ suffers ความซ้ำซ้อน, ความไม่สอดคล้องกัน และ ความขึ้นอยู่กับข้อมูล พร้อมกับความถูกต้อง การแชร์ การค้นหา และความปลอดภัยที่ต่ำ
- ฐานข้อมูลเชิงสัมพันธ์ เก็บข้อเท็จจริงแต่ละอย่างไว้ครั้งเดียวในตารางที่ถูกจัดการโดย DBMS เดียวกัน ทำให้โปรแกรมอิสระจากการจัดเก็บและกฎถูกบังคับใช้แบบรวมศูนย์
- เอนทิตี → ตาราง · ทิวเปิล → บันทึก, แถว · แอททริบิวต์ → ฟิลด์, คอลัมน์ · คีย์หลัก ระบุ, คีย์ภายนอก เชื่อมโยง, คีย์ทางเลือก อาจระบุ, คีย์รอง มีดัชนีสำหรับการค้นหา
- ความสมบูรณ์ของการอ้างอิง: คีย์ภายนอกทั้งหมดตรงกับคีย์หลักที่มีอยู่จริง ดังนั้นจึงไม่มีบันทึกที่ไร้เจ้าของ
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้