A Database Is Records and Fields in a GridA record is everything stored about one thing, such as one student or one library book.A field is one piece of that record, such as surname, year group or date borrowed.A table holds all the records of one kind, drawn as a grid with rows for records and columns for fields.Every field is given a data type, so date_borrowed holds a date and copies_held holds an integer.Deciding your fields before you type in any data saves a rebuild, because adding a field later means going back through every record.A Primary Key Names One Record and Only OneDefinitionPrimary keyA field in a database table whose value is different for every record, so it identifies one record and only one.A primary key is the field whose value is different in every single record.Names make bad keys, because two students called Sofia Martinez will eventually turn up in the same year group.Schools use an admission number and shops use a product code, both invented for no other reason than being unique.A primary key can never be left blank, so a record without one cannot be stored at all.Keys are also what let two tables point at each other, which is the whole basis of a relational database.HintPick a key that will never need changing, since email addresses and phone numbers change and drag every link with them.When nothing natural is unique, add an automatically numbered ID column and use that.Flat File or Relational: Where the Duplication GoesA flat file database is a single table, which is exactly what one spreadsheet is.It works well while the data stays simple, such as 30 rows of name, form and locker number.It falls apart as soon as something repeats, since storing a teacher's room number against each of their 120 students stores that room 120 times.Move that teacher to a different room and you have 120 edits, and missing one leaves the database contradicting itself.A relational database splits the data into linked tables, so the room number is stored once in a Teachers table.You trade a little more setup at the start for far less duplication and far fewer contradictions later.Two Tables, One RelationshipTake a school clubs system, which needs one table for Members and one for Clubs.Members holds member_id as its primary key, plus name and year group.Clubs holds club_id as its primary key, plus the club name and the day it meets.Adding a club_id field to Members creates a foreign key, which is a field pointing at the primary key of another table.That is a one to many relationship, since one club has many members while each member here sits in one club.Changing chess club from Tuesday to Thursday is then a single edit in Clubs, and every member sees the new day.TipDraw your tables as two boxes joined by a line before you build anything.Write the key field at the top of each box, and remember the line always joins a primary key to a foreign key.Queries and Sorting Pull Out What You NeedA query asks the database for the records matching a condition and hands back a smaller table.Every query answers three things: which fields you want, which table they are in, and what the condition is.Sorting puts the answer in order, so ORDER BY year DESC lists the oldest year groups first.Conditions join with AND and OR in exactly the way the conditions in your code do.A query never changes the stored data, so you can run one as often as you like while you are testing.Saving your useful queries is worth doing, since a club register is the same query run every week.ExampleSQL: SELECT name, year FROM MembersWHERE club_id = "C3"ORDER BY year DESCValidation Stops Rubbish Getting InValidation is a rule the database checks before it will accept a value.A range check rejects anything outside sensible limits, such as a year group below 1 or above 13.A type check refuses letters in a field that is meant to hold a number.A presence check insists a field is not left empty, which every primary key needs.A format check tests the shape of the entry, such as an email address containing an @ or a postcode matching a pattern.Validation cannot tell whether data is true, so a year group of 9 passes every check even when the student is in year 10.Common MistakeValidation checks the shape of the data, while verification checks it was copied in correctly, usually by asking for it twice.A perfectly valid date of birth can still belong to the wrong person.Storing People's Data ResponsiblyCollect only the fields your design genuinely uses.Say what you are collecting and why on the form itself, before anyone types anything.Ask permission for anything that identifies a person, and treat photographs of students as identifying data.Keep the data only as long as the project needs it, then delete it and record in your folder that you did.Use made up test data for screenshots, so your ePortfolio shows member 1042 rather than a classmate's real name.These decisions belong in Evaluating, where you judge your solution against a specification point you wrote about privacy.Active recallWhat is the difference between a record and a field?Why is a person's name a poor choice of primary key?Give one problem a flat file causes that two linked tables solve.Name the three things every query has to state.Why can data pass every validation rule and still be wrong?