SQL Add Constraint


Example

ALTER TABLE Employees
ADD CONSTRAINT DefaultSalary DEFAULT ((100)) FOR [Salary]

This adds a constraint called DefaultSalary which specifies a default of 100 for the Salary column.

A constraint can be added at the table level.

Types of constraints

  • Primary Key - prevents a duplicate record in the table
  • Foreign Key - points to a primary key from another table
  • Not Null - prevents null values from being entered into a column
  • Unique - uniquely identifies each record in the table
  • Default - specifies a default value
  • Check - limits the ranges of values that can be placed in a column

To learn more about constraints, see the Oracle documentation.