{"base":{"id":"cs10-25","subject":"Databases","title":"Designing a database table","scene":"mix","steps":[{"use":"data","mode":"table","eyebrow":"Tables, fields and records","title":"Rows and columns","body":"A database table stores data about one kind of thing. Each column is a field; each row is a record. Tap each.","actions":[{"id":"field","label":"Field"},{"id":"record","label":"Record"}]},{"use":"data","mode":"key","eyebrow":"Primary keys","title":"One of a kind","body":"A primary key is a field whose value is different for every record, like a student ID. Add a key.","actions":[{"id":"addKey","label":"Add a key"}]},{"use":"py9","mode":"choose","eyebrow":"Data types","title":"Fit the type to the data","body":"Choose a data type for each field: text, integer, real, Boolean or date/time.","actions":[{"id":"c0","label":"Field 1"},{"id":"c1","label":"Field 2"},{"id":"c2","label":"Field 3"},{"id":"c3","label":"Field 4"}]},{"use":"data9","mode":"flat","eyebrow":"Why design matters","title":"What goes wrong?","body":"If the same data is typed over and over, mistakes creep in and updates are missed. Good design stores each fact once."},{"use":"data9","mode":"keys","eyebrow":"Linking tables","title":"Primary and foreign keys","body":"A foreign key is a field that refers to the primary key of another table. It links records without repeating data."},{"use":"data","mode":"quiz","look":"key","eyebrow":"Check yourself","title":"Primary key","body":"A table of library members has fields Name, DateOfBirth, MemberID and Town.","question":"Which is the best primary key?","options":["Name","MemberID","Town"],"answer":1,"right":"Correct. Each member gets a unique ID; names and towns can repeat.","wrong":"Not quite. A primary key must be unique for every record."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Step by step","items":[{"tag":"Design","title":"A Films table","body":"FilmID       TEXT (PK)   e.g. F0042\nTitle        TEXT\nYear         INTEGER\nRunTime      INTEGER (minutes)\nRating       REAL\nSubtitles    BOOLEAN","code":true},{"tag":"Count","title":"Fields and records","body":"a table with 6 columns and 250 rows\nhas 6 fields and 250 records","code":true},{"tag":"Choose","title":"Phone numbers","body":"store as TEXT, not INTEGER:\nleading zeros matter and no maths is done","code":true},{"tag":"Validation","title":"In the table","body":"Year: range 1888–2030\nRating: range 0.0–10.0\nFilmID: format F + 4 digits","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Using a name as the primary key","body":"Names are not unique."},{"tag":"Mistake","title":"Storing phone numbers as integers","body":"A leading 0 would be lost. Use text."},{"tag":"Mistake","title":"Storing age instead of date of birth","body":"Age changes every year; date of birth doesn't."},{"tag":"Mistake","title":"One field holding two facts","body":"Split \"Full address\" into street, town, postcode so you can search by town."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Real world","title":"Databases run the world","body":"Bank accounts, flight bookings and online shops all use databases. A large airline stores millions of booking records and must never give two people the same seat."},{"tag":"Going further","title":"Relational databases","body":"In 1970 Edgar F. Codd described storing data in linked tables. Nearly every business database since uses his relational model."},{"tag":"Privacy","title":"Collect only what you need","body":"Data protection laws in many countries say organisations must store only the data they need and keep it secure."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one on paper before opening the card.","items":[{"tag":"Challenge","title":"Design","body":"Design a table for a school sports day: list fields, data types and the primary key.\n\nAnswer: e.g. EventID (TEXT, PK), EventName (TEXT), AgeGroup (INTEGER), Date (DATE), Indoor (BOOLEAN)."},{"tag":"Challenge","title":"Spot","body":"Why is \"Age\" a poor field compared with \"DateOfBirth\"?\n\nAnswer: age goes out of date; date of birth stays correct and age can be calculated."},{"tag":"Challenge","title":"Link","body":"A Loans table records which member borrowed which book. Which foreign keys does it need?\n\nAnswer: MemberID and BookID."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Try it yourself","items":[{"title":"Design your bookshelf table","time":"20 minutes","need":["Paper or a spreadsheet","Some books or games at home"],"steps":["Choose 5 fields to describe each item.","Pick a data type for each field and choose a primary key.","Enter at least 6 records."],"why":"You designed a real table with fields, records, types and a key.","safety":"None."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"A row in a table is a…","options":["field","key","record"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"A column in a table is a…","options":["field","record","query"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"A primary key must be…","options":["a number","unique for each record","the first field"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"Best type for \"Has paid?\"","options":["Text","Real","Boolean"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"Best type for a price like 4.99","options":["Real","Integer","Boolean"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"A foreign key…","options":["is a password","links to a primary key in another table","encrypts data"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"Storing the same data many times leads to…","options":["faster searches","smaller files","inconsistencies"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"Phone numbers are best stored as…","options":["text","integer","real"],"answer":0,"right":"Correct.","wrong":"Not quite."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["explain table, field and record","choose a primary key","choose suitable data types","design a single table for a scenario","explain foreign keys","explain why repeated data causes problems"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"Database","meaning":"An organised, searchable collection of data."},{"word":"Table","meaning":"Data about one kind of thing, in rows and columns."},{"word":"Field","meaning":"One piece of information (a column)."},{"word":"Record","meaning":"All the data about one item (a row)."},{"word":"Primary key","meaning":"A field with a unique value for each record."},{"word":"Foreign key","meaning":"A field that links to another table's primary key."}]},{"type":"sources","id":"sources","title":"Sources","items":["E. F. Codd (1970), \"A Relational Model of Data for Large Shared Data Banks\", Communications of the ACM","Raspberry Pi Foundation, Isaac Computer Science: databases","BBC Bitesize, GCSE Computer Science: databases","Information Commissioner's Office: data minimisation"]}]},"overlay":null}