---
title: "What is SQL Constraints?"  
description: "What is SQL Constraints?"  
author: "Anubhav Sharma"  
published: 2026-04-21  
updated: 2026-04-21  
canonical: https://www.mindstick.com/interview/34490/what-is-sql-constraints  
category: "database"  
tags: ["database", "sql server"]  
reading_time: 4 minutes  

---

# What is SQL Constraints?

**SQL Constraints** are rules applied to table columns to **control what data can be stored** in a database. They help maintain **data accuracy, consistency, and integrity**.

## What are SQL Constraints?

- Constraints act like **validation rules at the database level**.
- They ensure that only valid data is inserted, updated, or deleted.

## Why Constraints are Important

- Prevent invalid data entry
- Maintain data integrity
- Enforce business rules
- Improve data consistency across tables

## Types of SQL Constraints

### 1. NOT NULL

Ensures that a column **cannot have NULL (empty) values**

```plaintext
CREATE TABLE Users (
    Id INT,
    Name VARCHAR(100) NOT NULL
);
```

### 2. UNIQUE

Ensures all values in a column are **different**

```plaintext
CREATE TABLE Users (
    Email VARCHAR(100) UNIQUE
);
```

### 3. PRIMARY KEY

- Uniquely identifies each row
- Cannot be NULL
- Combines **NOT NULL + UNIQUE**

```plaintext
CREATE TABLE Users (
    Id INT PRIMARY KEY
);
```

### 4. FOREIGN KEY

Creates a relationship between two tables

```plaintext
CREATE TABLE Orders (
    OrderId INT PRIMARY KEY,
    UserId INT,
    FOREIGN KEY (UserId) REFERENCES Users(Id)
);
```

### 5. CHECK

Ensures values meet a specific condition

```plaintext
CREATE TABLE Employees (
    Age INT CHECK (Age >= 18)
);
```

### 6. DEFAULT

Sets a default value if none is provided

```plaintext
CREATE TABLE Users (
    Status VARCHAR(20) DEFAULT 'Active'
);
```

## Example with Multiple Constraints

```plaintext
CREATE TABLE Students (
    Id INT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    Email VARCHAR(100) UNIQUE,
    Age INT CHECK (Age >= 18),
    Status VARCHAR(20) DEFAULT 'Active'
);
```

## Table-Level vs Column-Level Constraints

- **Column-Level** → Applied directly to a column
- **Table-Level** → Applied to the whole table (useful for composite keys)

```plaintext
CREATE TABLE Enrollment (
    StudentId INT,
    CourseId INT,
    PRIMARY KEY (StudentId, CourseId)
);
```

## Advantages

- Ensures data correctness
- Reduces bugs in applications
- Improves database reliability

## In One Line

**SQL Constraints are rules that enforce valid and consistent data in a database.**

## Answers

### Answer by Anubhav Sharma

**SQL Constraints** are rules applied to table columns to **control what data can be stored** in a database. They help maintain **data accuracy, consistency, and integrity**.

## What are SQL Constraints?

- Constraints act like **validation rules at the database level**.
- They ensure that only valid data is inserted, updated, or deleted.

## Why Constraints are Important

- Prevent invalid data entry
- Maintain data integrity
- Enforce business rules
- Improve data consistency across tables

## Types of SQL Constraints

### 1. NOT NULL

Ensures that a column **cannot have NULL (empty) values**

```plaintext
CREATE TABLE Users (
    Id INT,
    Name VARCHAR(100) NOT NULL
);
```

### 2. UNIQUE

Ensures all values in a column are **different**

```plaintext
CREATE TABLE Users (
    Email VARCHAR(100) UNIQUE
);
```

### 3. PRIMARY KEY

- Uniquely identifies each row
- Cannot be NULL
- Combines **NOT NULL + UNIQUE**

```plaintext
CREATE TABLE Users (
    Id INT PRIMARY KEY
);
```

### 4. FOREIGN KEY

Creates a relationship between two tables

```plaintext
CREATE TABLE Orders (
    OrderId INT PRIMARY KEY,
    UserId INT,
    FOREIGN KEY (UserId) REFERENCES Users(Id)
);
```

### 5. CHECK

Ensures values meet a specific condition

```plaintext
CREATE TABLE Employees (
    Age INT CHECK (Age >= 18)
);
```

### 6. DEFAULT

Sets a default value if none is provided

```plaintext
CREATE TABLE Users (
    Status VARCHAR(20) DEFAULT 'Active'
);
```

## Example with Multiple Constraints

```plaintext
CREATE TABLE Students (
    Id INT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    Email VARCHAR(100) UNIQUE,
    Age INT CHECK (Age >= 18),
    Status VARCHAR(20) DEFAULT 'Active'
);
```

## Table-Level vs Column-Level Constraints

- **Column-Level** → Applied directly to a column
- **Table-Level** → Applied to the whole table (useful for composite keys)

```plaintext
CREATE TABLE Enrollment (
    StudentId INT,
    CourseId INT,
    PRIMARY KEY (StudentId, CourseId)
);
```

## Advantages

- Ensures data correctness
- Reduces bugs in applications
- Improves database reliability

## In One Line

**SQL Constraints are rules that enforce valid and consistent data in a database.**


---

Original Source: https://www.mindstick.com/interview/34490/what-is-sql-constraints

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
