© 2026 schoolict.net (Ruwan Mangala Suraweera)
9.4 Relational Database Design: Primary Key, Foreign Key and Relationshipsසම්බන්ධක දත්ත සමුදාය සැලසුම: Primary Key, Foreign Key සහ සම්බන්ධතා
G.C.E. O/L Grade 10 ICT · Lesson 9G.C.E. O/L ICT · 10 ශ්රේණිය · පාඩම 9
In this videoමේ වීඩියෝවේ
- A primary key uniquely identifies each record.Primary key සෑම record එකක්ම අනන්යව හඳුනා ගනී.
- A foreign key is a PK of another table placed in this table.Foreign key යනු වෙනත් table එකක PK එක මෙම table එකේ තැබීමයි.
- One-to-many: one class has many students.One-to-many: එක් පන්තියකට බොහෝ සිසුන්.
- One-to-one and many-to-many (needs a link table).One-to-one සහ many-to-many (link table අවශ්යයි).
- Referential integrity: every FK matches a PK.Referential integrity: සෑම FK එකක්ම PK එකකට ගැළපේ.
Notesසටහන්
If we typed the class teacher’s name and room into every student record, the same data would be repeated many times. A relational design stores each kind of data in its own table and links the tables with keys. In this note you will learn the primary key, the foreign key, and the three kinds of relationship – mainly one-to-many – using our school’s Class–Student and Book–Borrow–Student tables.සෑම ශිෂ්ය record එකකම පන්ති භාරාචාර්යවරයාගේ නම සහ කාමරය ඇතුළත් කළහොත් එකම දත්ත බොහෝ වාරයක් නැවත යෙදේ. සම්බන්ධක සැලසුමක් එක් එක් දත්ත වර්ගය තමන්ගේම table එකක ගබඩා කර keys මගින් tables සම්බන්ධ කරයි. මෙම සටහනේ primary key, foreign key සහ සම්බන්ධතා වර්ග තුන – ප්රධාන වශයෙන් one-to-many – අපගේ පාසලේ Class–Student සහ Book–Borrow–Student tables භාවිතයෙන් ඉගෙන ගනිමු.
1. Why split data into tables?1. දත්ත tables වලට බෙදන්නේ ඇයි?
In one big table, ‘Mrs. N. Perera, Room 12’ would be typed for all 4 students of 10A. If the teacher changes, 4 records must be edited – a missed one makes the data inconsistent. In a relational design, the teacher is stored once in tblClass, and each student only stores ClassID.එක් විශාල table එකක 10A හි සිසුන් 4 දෙනාටම ‘Mrs. N. Perera, Room 12’ ඇතුළත් කළ යුතුය. ගුරුවරයා වෙනස් වුවහොත් records 4 ක් සංස්කරණය කළ යුතු අතර එකක් මග හැරුණොත් දත්ත අසංගත වේ. සම්බන්ධක සැලසුමක ගුරුවරයා tblClass හි එක් වරක් ගබඩා කර සෑම සිසුවෙකුම ClassID පමණක් ගබඩා කරයි.
2. Primary key and foreign key2. Primary key සහ foreign key
3. Types of relationships3. සම්බන්ධතා වර්ග
| Typeවර්ගය | Meaningතේරුම | Exampleඋදාහරණය |
|---|---|---|
| One-to-many (1–∞)One-to-many (1–∞) | One record in table A matches many in table B; each B matches only one ATable A හි එක් record එකක් B හි බොහොමයකට ගැළපේ; සෑම B එකක්ම එක් A එකකට පමණි | One class – many students; one student – many loansඑක් පන්තියක් – සිසුන් බොහෝ; එක් සිසුවෙක් – ණය බොහෝ |
| One-to-one (1–1)One-to-one (1–1) | One record matches exactly one recordඑක් record එකක් හරියටම එක් record එකකට | One student – one school ID card; one school – one principalඑක් සිසුවෙක් – එක් පාසල් හැඳුනුම්පතක්; එක් පාසලක් – එක් විදුහල්පතිවරයෙක් |
| Many-to-many (∞–∞)Many-to-many (∞–∞) | Many A match many B; built with a link (junction) table and two 1–∞ relationshipsA බොහෝ B බොහෝ වලට; link (junction) table එකක් සහ 1–∞ සම්බන්ධතා දෙකකින් සාදයි | Students and books: tblBorrow links themසිසුන් සහ පොත්: tblBorrow ඒවා සම්බන්ධ කරයි |
4. One-to-many in our data4. අපගේ දත්තවල one-to-many
| ClassID (PK in tblClass)ClassID (tblClass හි PK) | Class teacherපන්ති භාරාචාර්ය | Number of students (FK matches in tblStudent)සිසුන් ගණන (tblStudent හි FK ගැළපීම්) |
|---|---|---|
| 10A10A | Mrs. N. PereraMrs. N. Perera | 44 |
| 10B10B | Mr. S. KumaraMr. S. Kumara | 33 |
| 10C10C | Mrs. F. RizwanMrs. F. Rizwan | 33 |
| 11A11A | Mr. R. SilvaMr. R. Silva | 00 |
5. Many-to-many: Book – Borrow – Student5. Many-to-many: Book – Borrow – Student
A student can borrow many books and a book can be borrowed by many students, so Student–Book is many-to-many. A DBMS cannot link them directly; we add tblBorrow with two foreign keys (BookID, AdmNo). Now Book 1–∞ Borrow and Student 1–∞ Borrow. Student 24101 has 2 loans and 24103 has 2.සිසුවෙකුට පොත් බොහොමයක් ණයට ගත හැකි අතර පොතක් සිසුන් බොහෝ දෙනෙකුට ණයට ගත හැකි නිසා Student–Book many-to-many වේ. DBMS එකකට ඒවා සෘජුව සම්බන්ධ කළ නොහැක; foreign keys දෙකක් (BookID, AdmNo) සහිත tblBorrow එකතු කරමු. දැන් Book 1–∞ Borrow සහ Student 1–∞ Borrow. සිසු 24101 ට ණය 2 ක් සහ 24103 ට 2 ක් ඇත.
Key Wordsප්රධාන පද
Sample Q&Aආදර්ශ ප්රශ්න සහ පිළිතුරු
1Define a foreign key.Foreign key අර්ථ දක්වන්න.
2Name the PK and FK that link tblClass and tblStudent.tblClass සහ tblStudent සම්බන්ධ කරන PK සහ FK නම් කරන්න.
3What type of relationship is Class–Student?Class–Student සම්බන්ධතා වර්ගය කුමක්ද?
4How is a many-to-many relationship built?Many-to-many සම්බන්ධතාවක් සාදන්නේ කෙසේද?
5Give an example of a one-to-one relationship.One-to-one සම්බන්ධතාවක උදාහරණයක් දෙන්න.
6Why not store the class teacher in tblStudent?tblStudent හි පන්ති භාරාචාර්ය ගබඩා නොකරන්නේ ඇයි?
MCQබහුවරණ ප්රශ්න
Choose the correct answer for each question.සෑම ප්රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.
1A field that links to the primary key of another table is a…වෙනත් table එකක primary key එකට සම්බන්ධ වන field එක…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
FK.FK.
2In tblStudent, ClassID is a…tblStudent හි ClassID…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
Links to tblClass.tblClass ට සම්බන්ධයි.
3The primary key of tblBorrow is…tblBorrow හි primary key එක…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
AutoNumber PK.AutoNumber PK.
4tblBorrow has how many foreign keys?tblBorrow හි foreign keys කීයද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
BookID and AdmNo.BookID සහ AdmNo.
5One class has many students. This is…එක් පන්තියකට සිසුන් බොහෝ ඇත. මෙය…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
1–∞.1–∞.
6How many students are in class 10A in the sample data?නියැදි දත්තවල 10A පන්තියේ සිසුන් කී දෙනෙක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Count FK = 10A.FK = 10A ගණන.
7Class 11A has 0 students in tblStudent. This is…tblStudent හි 11A පන්තියට සිසුන් 0 කි. මෙය…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
Zero is fine.බිංදුව හරි.
8Adding a student with ClassID ’12Z’ (not in tblClass) is refused because of…ClassID ’12Z’ (tblClass හි නැත) ඇති සිසුවෙකු එකතු කිරීම ප්රතික්ෂේප වන්නේ…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
FK must match a PK.FK, PK එකකට ගැළපිය යුතුය.
9Students and books (a student borrows many books, a book is borrowed by many students) is…සිසුන් සහ පොත් (සිසුවෙක් පොත් බොහෝ, පොතක් සිසුන් බොහෝ)…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
∞–∞.∞–∞.
10A many-to-many relationship is created using…Many-to-many සම්බන්ධතාවක් සාදන්නේ…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
Junction table.Junction table.
11One school has one principal. This is…එක් පාසලකට එක් විදුහල්පතිවරයෙක්. මෙය…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
1–1.1–1.
12In Figure 9.4, the ‘∞’ symbol is at the side of the…රූපය 9.4 හි ‘∞’ සංකේතය ඇත්තේ… පැත්තේ
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Many = FK side.Many = FK පැත්ත.
13Book B001 appears in tblBorrow…B001 පොත tblBorrow හි දිස්වන්නේ…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
Count.ගණන.
14A primary key made of AdmNo + SubjectID is a…AdmNo + SubjectID වලින් සෑදූ primary key එක…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
Two fields.Fields දෙකක්.
15In an e-channelling system, one doctor has many appointment sessions. The FK DoctorID is in the…E-channelling පද්ධතියක එක් වෛද්යවරයෙකුට සැසි බොහෝ ඇත. FK DoctorID ඇත්තේ…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
Many side holds the FK.Many පැත්තේ FK ඇත.
MCQ answer keyMCQ පිළිතුරු
- A – FK.FK.
- B – Links to tblClass.tblClass ට සම්බන්ධයි.
- C – AutoNumber PK.AutoNumber PK.
- C – BookID and AdmNo.BookID සහ AdmNo.
- D – 1–∞.1–∞.
- C – Count FK = 10A.FK = 10A ගණන.
- D – Zero is fine.බිංදුව හරි.
- A – FK must match a PK.FK, PK එකකට ගැළපිය යුතුය.
- B – ∞–∞.∞–∞.
- A – Junction table.Junction table.
- D – 1–1.1–1.
- C – Many = FK side.Many = FK පැත්ත.
- A – Count.ගණන.
- B – Two fields.Fields දෙකක්.
- B – Many side holds the FK.Many පැත්තේ FK ඇත.
Structured Questionsව්යුහගත ප්රශ්න
1Using Figure 9.4, name the primary key of each table and the foreign keys. (5 marks)රූපය 9.4 භාවිතයෙන් සෑම table එකකම primary key සහ foreign keys නම් කරන්න. (ලකුණු 5) (5 marksලකුණු 5)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
2(i) Define a one-to-many relationship. (1 mark)
(ii) Give two examples from a school. (2 marks)(i) One-to-many සම්බන්ධතාවක් අර්ථ දක්වන්න. (ලකුණු 1)
(ii) පාසලකින් උදාහරණ දෙකක් දෙන්න. (ලකුණු 2) (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
3Explain why Student–Book is many-to-many and how tblBorrow solves it. (3 marks)Student–Book many-to-many වන්නේ ඇයි සහ tblBorrow එය විසඳන්නේ කෙසේද? (ලකුණු 3) (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
4Using the sample data, how many students does each class have? Which class shows that the ‘many’ side can be zero? (3 marks)නියැදි දත්ත භාවිතයෙන් සෑම පන්තියකම සිසුන් කී දෙනෙක්ද? ‘many’ පැත්ත බිංදුව විය හැකි බව පෙන්වන පන්තිය කුමක්ද? (ලකුණු 3) (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
5State referential integrity and give one example of data it rejects. (2 marks)Referential integrity සඳහන් කර එය ප්රතික්ෂේප කරන දත්ත සඳහා උදාහරණයක් දෙන්න. (ලකුණු 2) (2 marksලකුණු 2)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
🎮 Game🎮 ක්රීඩා
Summary & Downloadsසාරාංශය සහ බාගත කිරීම්
Key pointsප්රධාන කරුණු
- Relational design: one table per entity, linked by keys.සම්බන්ධක සැලසුම: entity එකකට table එකක්, keys මගින් සම්බන්ධ.
- PK = unique, not empty (ClassID, AdmNo, BookID).PK = අනන්ය, හිස් නොවන (ClassID, AdmNo, BookID).
- FK = another table’s PK (ClassID in tblStudent).FK = වෙනත් table එකක PK (tblStudent හි ClassID).
- 1–∞: one class, many students (most common).1–∞: එක් පන්තියක්, සිසුන් බොහෝ (වඩාත් සුලභ).
- 1–1: school – principal; ∞–∞ needs a link table.1–1: පාසල – විදුහල්පති; ∞–∞ ට link table එකක්.
- tblBorrow links Student and Book (two FKs).tblBorrow, Student සහ Book සම්බන්ධ කරයි (FK දෙකක්).
- Referential integrity: FK must match a PK.Referential integrity: FK, PK එකකට ගැළපිය යුතුය.
- NIC, e-channelling, admissions use keys.NIC, e-channelling, ඇතුළත් කිරීම් keys භාවිත කරයි.
