---
title: "what is database constraints?"  
description: "what is database constraints?"  
author: "Ravi Vishwakarma"  
published: 2026-01-29  
updated: 2026-02-01  
canonical: https://answers.mindstick.com/qa/116302/what-is-database-constraints  
category: "database"  
tags: ["database"]  
reading_time: 2 minutes  

---

# what is database constraints?

## Answers

### Answer by Anubhav Sharma

Database constraints are **rules applied to database tables** to make sure the [data stored](https://www.mindstick.com/interview/23089/how-to-create-grid-view-in-c-sharp-data-stored-in-sql-server) is **accurate, consistent, and reliable**.

Think of them as **guardrails** for your data — they prevent invalid or unwanted data from getting into the database.

## Why database constraints are needed

They help to:

- Prevent duplicate or invalid data
- Maintain [data integrity](https://answers.mindstick.com/qa/113211/what-are-the-uses-of-blockchain-for-ensuring-data-integrity-in-ar-healthcare-applications)
- Enforce relationships between tables
- Reduce bugs caused by bad data

## Common types of database constraints

### 1. PRIMARY KEY

- Uniquely identifies each row in a table
- Cannot be `NULL`
- No [duplicate values](https://www.mindstick.com/forum/23111/swap-keys-and-duplicate-values-in-hashmap)

## Example:

```plaintext
UserId INT PRIMARY KEY
```

### 2. FOREIGN KEY

- Creates a relationship between [two tables](https://www.mindstick.com/forum/34330/how-i-can-show-two-tables-record-in-gridview)
- Ensures values exist in the referenced table

## Example:

```plaintext
UserId INT,
FOREIGN KEY (UserId) REFERENCES Users(UserId)
```

### 3. UNIQUE

- Ensures all values in a column are unique
- Allows only one `NULL` (depends on DB)

## Example:

```plaintext
Email VARCHAR(100) UNIQUE
```

### 4. NOT NULL

- Prevents `NULL` values in a column

## Example:

```plaintext
Name VARCHAR(50) NOT NULL
```

### 5. CHECK

- Validates data using a condition

## Example:

```plaintext
Age INT CHECK (Age >= 18)
```

### 6. DEFAULT

- Assigns a [default value](https://www.mindstick.com/forum/23023/default-value-when-using-singleordefault) if none is provided

## Example:

```plaintext
CreatedDate DATETIME DEFAULT GETDATE()
```

## Simple real-world example

```plaintext
CREATE TABLE Users (
    UserId INT PRIMARY KEY,
    Email VARCHAR(100) UNIQUE NOT NULL,
    Age INT CHECK (Age >= 18),
    CreatedDate DATETIME DEFAULT GETDATE()
);
```

Here:

- `UserId` must be unique
- `Email` cannot be duplicated or empty
- `Age` must be 18 or above
- `CreatedDate` auto-fills if not provided

### In one line:

> **Database constraints enforce rules to keep your data correct and trustworthy.**


---

Original Source: https://answers.mindstick.com/qa/116302/what-is-database-constraints

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
