{"base":{"id":"cs9-20","subject":"Data","title":"Related tables and keys","scene":"data9","steps":[{"mode":"flat","eyebrow":"One big table","title":"What goes wrong?","body":"Putting students and their results in one table repeats names and classes in many rows. Look at the problems this causes."},{"mode":"tables","eyebrow":"Two related tables","title":"Link with a key","body":"Split the data into Students and Results. StudentID links them. Choose a student to see which results belong to them.","controls":[{"id":"id","label":"Student","min":1,"max":3,"step":1,"value":1}]},{"mode":"keys","eyebrow":"Keys","title":"Primary and foreign","body":"A primary key identifies each record. A foreign key holds another table's primary key, creating the link."},{"mode":"quiz","eyebrow":"Check yourself","title":"Foreign key","body":"A Loans table records which library member borrowed each book. It has LoanID, MemberID and BookID.","question":"Which fields are foreign keys?","options":["LoanID only","MemberID and BookID","All three"],"answer":1,"right":"Correct. They hold the primary keys of the Members and Books tables.","wrong":"Not quite. Which fields point to other tables?","look":"keys"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"A name is a good primary key.\"","body":"Two people can share a name. Use a unique ID."},{"tag":"Popular idea","title":"\"Related tables store the same data twice.\"","body":"They store each fact once and link records with keys."},{"tag":"Popular idea","title":"\"A foreign key must be unique in its table.\"","body":"One student can appear many times in Results."}]},{"type":"cards","id":"code","eyebrow":"Tables","title":"Students and Results","items":[{"tag":"Students","title":"Primary key StudentID","body":"StudentID | Name      | Class\nS1        | Ava Chen  | 9A\nS2        | Leo Silva | 9B","code":true},{"tag":"Results","title":"Foreign key StudentID","body":"ResultID | StudentID | Subject | Score\nR1       | S1        | Maths   | 72\nR2       | S2        | Maths   | 58","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Library cards","items":[{"title":"Design a library database","time":"20 minutes","need":["Paper"],"steps":["Design three tables: Members, Books and Loans.","Choose a primary key for each.","Add foreign keys to Loans.","Write 3 sample records in each table."],"why":"Most real databases are made of related tables linked by keys.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"What is a primary key?","options":["A field unique for every record","The first field","A password"],"answer":0,"right":"Correct. It identifies each record.","wrong":"Not quite. Unique."},{"question":"Why split data into related tables?","options":["To make it slower","To avoid repeating data","To hide it"],"answer":1,"right":"Correct. Store each fact once.","wrong":"Not quite. Think of updates."},{"question":"Which is the best primary key for students?","options":["Surname","Class","StudentID"],"answer":2,"right":"Correct. Unique.","wrong":"Not quite. Can two students share it?"},{"question":"A foreign key…","options":["links to another table's primary key","is always unique","is a password"],"answer":0,"right":"Correct. It makes the link.","wrong":"Not quite. It points elsewhere."},{"question":"Leo changes class. In a related database, how many rows change?","options":["Every row with his results","None","One, in Students"],"answer":2,"right":"Correct. His class is stored once.","wrong":"Not quite. Where is the class stored?"},{"question":"One student, many results. This is a…","options":["one-to-one relationship","one-to-many relationship","no relationship"],"answer":1,"right":"Correct. One record links to many.","wrong":"Not quite. Count both sides."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["why one big table causes repeated data","how to split data into related tables","what primary and foreign keys are","how keys link records"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"relational database","meaning":"A database of tables linked by keys."},{"word":"record","meaning":"One row: all the data about one item."},{"word":"field","meaning":"One column: one kind of data."},{"word":"primary key","meaning":"A field that is unique for each record."},{"word":"foreign key","meaning":"A field that links to another table's primary key."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize KS3 Computer Science","Raspberry Pi Foundation, Computing curriculum resources"]}]},"overlay":null}