Course 9.1Lesson 3 of 3
A database is only useful if its data is sensible. Validation is a check that the computer makes on data before it goes into a table. You choose suitable checks for each field: range, length, type, presence, format and check digit, and you write validation in Python before a record is added.
Try the code and the simulations beside the lesson.
Imagine a club list where one member is years old and another has no surname. A search for members under would give wrong answers. Nobody typed the mistakes on purpose, but the table is now wrong.
Validation is an automatic check, made by the computer, that data is sensible before it is accepted. If the data fails the check, it is rejected and the person is asked to type it again.
Validation cannot prove that data is correct. If a member is and someone types , a range check from to accepts it. The value is sensible, but wrong.
Each check suits a different kind of field.
M07.When you define a table, you say which checks each field needs. Here is a plan for the Members table.
MemberID: presence check, length check (exactly characters) and format check (M and two digits).Surname: presence check.Age: type check (an integer) and range check ( to ).Paid: the field is Boolean, so only TRUE or FALSE can be stored.A field can have more than one check. Ask: what would a wrong value look like, and which check would catch it?
Validation and verification. Validation checks that data is sensible. Verification checks that data was copied correctly: a visual check (reading it again) or a double entry check (typing it twice and comparing). Chapter 7 teaches both in algorithms.
Test a range check at its ends. For an age from to , try and (both accepted), then and (both rejected). A check written with < where it needed <= fails on the end values.
The program checks each new member before it goes into the table. It makes a presence check, a format check and a range check. Only the records that pass all the checks are added with executemany.
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Email has not been left empty?Floor in a hotel table must be a whole number from to . Name two checks it needs.TB4410. Which check makes sure the code follows that pattern?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.