Skip to content

Computer Science · Ch 9 — Structured Query Language (SQL)

Constraints

9.3.2

Constraints

Constraints are rules that restrict what data values can be stored in a column. They exist to protect the correctness and reliability of your data — for example, preventing a blank entry where a value is required, or stopping two students from having the same roll number. The textbook makes clear that defining constraints is optional; you choose them per attribute based on what your data needs.

The five commonly used SQL constraints are listed in Table 9.2 of the NCERT text. Here is each one explained:

  • NOT NULL – This constraint ensures that a column cannot have a NULL value. NULL in SQL means missing, unknown, or not applicable data. If you mark a column as NOT NULL, every row must have a real value in that column — no blanks allowed.

  • UNIQUE – This guarantees that all values in a column are distinct from one another. No two rows can share the same value in that column. It is useful for fields like email addresses or employee IDs where duplicates would be invalid.

  • DEFAULT – This assigns a fallback value to a column when no value is provided during insertion. For example, if you set a default of 'Active' for a status column, any new row without an explicit status will automatically get 'Active'.

  • PRIMARY KEY – This is the column (or combination of columns) that uniquely identifies each row in a table. A primary key automatically enforces both NOT NULL and UNIQUE — no two rows can have the same key value, and the key can never be NULL. Every table should ideally have one.

  • FOREIGN KEY – This constraint links a column in one table to the primary key of another table. It ensures that the value in the foreign key column must already exist as a primary key value in the referenced table. This maintains referential integrity — you cannot, for instance, assign a student to a class that does not exist. …

Table 9.2Commonly used SQL Constraints
ConstraintDescription
NOT NULLEnsures that a column cannot have NULL values where NULL means missing/unknown/not applicable value.
UNIQUEEnsures that all the values in a column are distinct/unique
DEFAULTA default value specified for the column if no value is provided
PRIMARY KEYThe column which can uniquely identify each row/record in a table.