Data Validation

Ensuring that changes are saved is only half the data safety problem. A program should also ensure that the saved data values make as much sense as possible. For instance, if the user accidentally types 80201 in a phone number field, the program should not save the data to the database.

At the lowest level, you can use the database itself to validate data. For example, you can use the NOT NULL statement to require a value in a field. You can use CHECK clauses to provide more elaborate validation. For instance, the following SQL statement creates a ZipCodes table. Each record’s Zip field must have the format 12345 or 12345-6789.

 CREATE TABLE Zips ( Zip VARCHAR(10) UNIQUE NOT NULL CHECK ((Zip LIKE '[0-9][0-9][0-9][0-9][0-9]') ...

Get Visual Basic® .NET Database Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.