-->

Thursday, March 6, 2014

How to Apply Constraints on Columns in SQL Programming?

How to Apply Constraints on Columns in SQL Programming?

In SQL Programming, consider an example where a user entered a duplicate value in the EmployeeID column of the Employee table. This would mean that two employees have same employee ID. This would further results in erroneous results when anybody queries the table. As a database developer, you can prevent this by enforcing data integrity on the table by using constraints.

Constraints define rules that must be followed to maintain consistency and correctness of data. A constraint can either be created while creating a table or can be added later. When a constraint is added after the table is created, it checks the existing data. If there is any violation, then the constraint is rejected.

A constraint can be created by using by using either of the following statements:

CREATE TABLE statement
ALTER TABLE statement
A constraint can be defined on a column while creating a table. It can be created with the CREATE TABLE statement. The syntax of adding a constraint at the time of table creation is:
CREATE TABLE table_name
(
Column_name CONSTRAINT constraint_name constraint_type [, CONSTRAINT
)
Where
  • Column_name is the name of the column on which the constraint is to be defined.
  • Constraint_name is the name of the constraint to be created and must follow the rules for the identifier.
  • Constraint_type is the type of constraint to be added.
Constraints can be divided into the following types:

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved