January 13, 2025
sql-database

What is Database Constraint?

This article describes about the database constraint in SQL. Summary of the article:

  • What is Database Constraint?
  • Types of Database constraints
  • How to make a Great Primary Key?
  • Primary Key Constraint
  • Foreign Key Constraint
  • Unique Key Constraint
  • Not null Constraint
  • Check Constraint

What is Database Constraint?
Database constraint is user-defined structures that express the behaviors of column. It is used to protect wrong entry.

Types of Database constraints
There are five types of database constraints. Such:

  1. Primary Key Constraint
  2. Foreign Key Constraint
  3. Unique Key Constraint
  4. Not null Constraint
  5. Check Constraint

Primary Key Constraint
A column value will be unique among all the rows in a table. It does not allow any null value. Therefore, a primary key constraint has the behaviors of both Not Null and Unique Key Constraints.

Foreign Key Constraint
We can’t insert a foreign key value which is not present in the primary key column of that foreign key.

Unique Key Constraint
A column value will be unique among all the rows in a table. It allows only a single Null value in that column. Depending on the design, a table may have more than one unique key constraint.

Not null Constraint
It makes a column mandatory. This means you can’t insert a row in the table without providing a valid data.

Check Constraint
Check Constraints are table-level constraint. It doesn’t make the column mandatory.

That’s all about the constraint of a database.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

One thought on “What is Database Constraint?

Leave a Reply

Your email address will not be published. Required fields are marked *