This article explains the 3 ways possible to do a validation to
database level to prevent Are there values other than telephones
in your SQL Server database.
Managing validations with clauses check allows you to reduce errors in the
information stored in your table in addition to for the purposes of development
of applications allows the developer From the backend only worry about capturing
the error message without having to develop a whole algorithm around
Of the information provided in user forms.
The ALTER TABLE check syntax TABLE_NAME ADD CONSTRAINT
constraint_name CHECK (condition); Where table_name is the name of
The table to which you are going to add the restriction, restriction_name is the
the name you assign to the restriction, And condition is the expression that should be
comply for the value to be valid.
In particular, I have my reservations. on the use of Triggers in tables
, but here I show you how to create a trigger in SQL Server to validate that
A telephone value has exactly 10 numeric digits before inserting
or update a record in a table.
This trigger will be executed before it is insert or update a record in the table
my_table, so any attempt to insertion or update that does not comply with
The validation condition of the phone will be canceled and an error message will be generated.
Function in SQL Server to validate.
If the value is valid, we set the return value of the function in 1;
otherwise, we set it to 0.
To use this function in a statement CHECK to validate the telephone field
from a table, you can use the following syntax: ALTER TABLE my_ADD table
CONSTRAINT phone_check CHECK (dbo.
validate_telephone (phone) = 1); In this CHECK, we are using the dbo function.
validate_phone to verify that The value of the telephone column has
exactly 10 numerical digits.
If the value does not meet the condition, the Table restriction will not allow the
inserting or updating the registry.
Until Next Thanks:D!