Constraints in SQL

SQL constraints are rules that are applied to the data in a table. We can specify a constraint with the CREATE TABLE statement at the time of table creation or with the ALTER TABLE statement after the table has been created.

Syntax:

CREATE TABLE <em>tableName</em>
(
<em>columnName1 datatype</em>(<em>size</em>) <em>constraintName</em>,
<em>columnName2 data_type</em>(<em>size</em>) <em>constraintName</em>,
<em>columnName2 data_type</em>(<em>size</em>) <em>constraintName</em>,
....
);

Commonly used constraints in SQL:

1. SQL PRIMARY KEY Constraint.

2. SQL FOREIGN KEY.

3. SQL NOT NULL Constraint.

4. SQL UNIQUE Constraint.

5. SQL CHECK Constraint

6. SQL DEFAULT Constraint

7. SQL INDEX Constraint.