Databases Constraints: The Check Constraint
When performing data entry, in some columns, even after indicating the types of values you expect the user to provide for a certain column, you may want…
Introduction#
When entering data into certain columns, you might want to limit the permitted range of values even after defining the expected types. Similarly, you can establish a rule that must be satisfied across a combination of columns before a record is accepted. For instance, you could require the database engine to verify that at least one of two fields has been populated. In a customer information table, for example, you might mandate that either a phone number or an email address be provided for every record. This functionality for ensuring that one or more rules are followed on a table is known as a check constraint. A check constraint represents a Boolean operation carried out by the SQL interpreter, which validates a newly provided value for a column. If the value is appropriate:
- The constraint produces TRUE
- The value gets accepted
- The value is assigned to the column
If the value is not appropriate:
- The constraint produces FALSE
- The value gets rejected
- The value is not assigned to the column
You create a check constraint at the time you are creating a table.
Visually Creating a Check Constraint#
To create a check constraint, when creating a table, right-click anywhere in (even outside) the table and click Check Constraints...
This action launches the Check Constraints dialog box. Within this window, selecting Add allows you to create a new constraint. Since each constraint is treated as an object, a unique name must be assigned to it. ...
After creating the expression, you can click OK. If the expression is invalid, you would receive an error and given the opportunity to correct it. You can create as many check constraints as you judge necessary for your table:
After creating the check constraints, you can click OK.
Programmatically Creating a Check Constraint#
To create a check constraint in SQL, first create the column on which the constraint will apply. Before the closing parenthesis of the table definition, use the following formula:
CONSTRAINT name CHECK (expression)The keywords CONSTRAINT and CHECK are mandatory. Because it is an object, ensure that you specify a name for it. The expression to be enforced should be placed inside the parentheses that follow the CHECK operator. The following example demonstrates how to ensure that an employee's hourly salary exceeds 12.50:
using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
Button btnCreateTable;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnCreateTable = new Button();
btnCreateTable.AutoSize = true;
btnCreateTable.Text = "Select Records";
btnCreateTable.Location = new Point(12, 12);
btnCreateTable.Click += new EventHandler(CreateTable);
Text = "Kolo Bank";
Controls.Add(btnCreateTable);
}
void CreateTable(object sender, EventArgs e)
{
using (SqlConnection connection =
new SqlConnection("Data Source=(local);" +
"Database='Exercise2';" +
"Integrated Security=yes;"))
{
SqlCommand command =
new SqlCommand("CREATE TABLE Employees(" +
"[Employee Number] nchar(7)," +
"[Full Name] varchar(80)," +
"[Hourly Salary] smallmoney," +
"CONSTRAINT CK_HourlySalary CHECK ([Hourly Salary] > 12.50));",
connection);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("A table named Employees has been deleted.",
"Exercise",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}It is important to understand that a check constraint it neither an expression nor a function. A check constraint contains an expression and may contain a function as part of its definition. After creating the constraint(s) for a table, inside the table's node, there is a node named Constraints and, if you expand it, you would see the name of the constraint. With the constraint(s) in place, during data entry, if the user (or your code) provides an invalid value, an error would display. Instead of an expression that uses only the regular operators, you can use a function to assist in the checking process. You can create and use your own function or you can use one of the built-in Transact-SQL functions. Home Copyright © 2010-2011 C# Key

