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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.