These days, Relational Database Management Systems are very powerful. They do so much for you that really knowing a database like Sql Server or Oracle is quite an accomplishment. Anyway, as a programmer, the more you know the better off you are.
Let's say that you have an application and you don't want to allow negative values in your database. If even one gets in, it could cause problems. You have three options, client side validation, server side validation or both. Let's examine what happens in all three.
Client Side Validation:
You write bullet proof code that doesn't allow negative values in any textbox that maps to the column in question. A few things could go wrong. The first is that you or another programmer could make a mistake in the future and allow negatives to enter your database. The second is that you or another programmer could add a new form and forget to include the validation code. The third is that someone could write a query outside of your app and run it, which enters negative numbers....or someone makes a typo in Enterprise manager or similar tool.
Server Side Validation:
Provided you write your constraint correctly, something that's easily tested, non-conforming data can never enter your database, not from an Application, Query Analyzer, or Enterprise Manager.
Mixed Validation:
This approach takes the most work, but it is probably the preferred method for most instances. But the Server Side method can guarantee that I never get bad data in the database, so why would I want to write extra code? User Friendliness and efficiency. Users don't want to enter data and hit a button hoping they did it right, only to have the database come back and yell at them. Moreover, why not try to filter as much junk as you can and then use the Server as final watchdog?
Enough already, let me show you how to do it. Let's say we have a table named Employees and one of the business rules is the following: "Every employee can no less than 0 dependants, and no more than 15. While they may physically have more than 15, our company will not recognize any over the 15 ceiling"
| CREATE TABLE Employees( EmployeeId INT ,AGE INT ,FirstName VARCHAR(75) ,LastName VARCHAR(75) ,Dependents INT ) |
| Private Sub SomeTextBox_OnValidating(ByVal sender As System.Object, ByVal e As CancelEventArgs) Handles SomeTextBox.Validating If CType(SomeTextBox.Text, Integer) < 0 Or CType(SomeTextBox.Text, Integer) > 15 Then e.Cancel = False End If End Sub |
| CREATE TABLE Employees( EmployeeId INT ,AGE INT ,FirstName VARCHAR(75) ,LastName VARCHAR(75) ,Dependents INT CHECK( Dependents > 0 AND Dependents <= 15)) |