Table of contents
1.
Introduction
2.
Domain Constraints
3.
Type of Domain Constraints
3.1.
1. Domain Constraints – Not Null
3.2.
2. Domain Constraints – Check
4.
Examples
4.1.
Example 1: Using "Not Null"
4.2.
Example 2: Using "Check"
5.
Frequently Asked Questions
5.1.
What if wrong data gets into the database?
5.2.
Can I add these rules to data already in the database?
5.3.
Do all databases use these rules the same way?
6.
Conclusion
Last Updated: Aug 13, 2025
Easy

Domain Constraints in DBMS

Author Pallavi singh
0 upvote

Introduction

In today’s world where data is everything, managing it efficiently is crucial for any system. Domain constraints in database management systems (DBMS) play a fundamental role in ensuring the integrity and accuracy of the data stored. These rules define the acceptable values that can be entered into a database column, helping prevent errors and maintaining data quality. 

Domain Constraints in DBMS

This article will explore what domain constraints are, look into their types, and provide practical examples to understand their application better. 

Domain Constraints

Domain constraints are special rules in a database. They help make sure the data is correct and follows specific guidelines. Imagine you have a form to fill out, and some boxes need certain types of information, like your name or age. Domain constraints work similarly but in a database. They check the data going into the database to make sure it fits what's expected. This is important because it keeps the database clean and free from mistakes. For example, if you have a box where you're supposed to enter your age, a domain constraint can make sure you don't accidentally type in your phone number instead. This helps everyone using the database to trust that the information in it is accurate and reliable.

Type of Domain Constraints

1. Domain Constraints – Not Null

The "Not Null" constraint is like a rule that says a certain spot in the database can't be left empty. It's like when you're filling out an important form, and some fields must be filled in before you can submit it. In databases, this is important because some information is critical and must always be provided. For example, in a list of students, the student ID field can't be empty because each student needs an ID to be uniquely identified.

2. Domain Constraints – Check

The "Check" constraint is a bit more specific. It's like a more detailed rule for what can go into a spot in the database. Instead of just saying something can't be empty, it says what the content needs to be like. For example, it can make sure that the age entered for a person is reasonable, like between 0 and 120. This helps prevent mistakes, like typing in the wrong number or putting in information that doesn't make sense.

Examples

To understand domain constraints better, let's look at some examples. These examples will show how "Not Null" and "Check" constraints are used in a real database.

Example 1: Using "Not Null"

Imagine we have a database for a library. In this database, every book needs to have a unique code so it can be easily found. Here's how we might set up a table for the books:

CREATE TABLE LibraryBooks (
    BookID INT NOT NULL,
    Title VARCHAR(100) NOT NULL,
    Author VARCHAR(100),
    PublicationYear INT,
    PRIMARY KEY (BookID)
);


In this example:

BookID and Title must always have a value because of the NOT NULL constraint. This means every book in the library needs to have an ID and a title.

Author and PublicationYear can be left empty. Maybe some books are too old, and we don't know who wrote them or their exact publication year.

Example 2: Using "Check"

Now, let's say we want to make sure that the publication year of a book is reasonable. We can use a "Check" constraint to ensure the year is between 1445 (when the printing press was invented) and the current year.

CREATE TABLE LibraryBooks (
    BookID INT NOT NULL,
    Title VARCHAR(100) NOT NULL,
    Author VARCHAR(100),
    PublicationYear INT CHECK (PublicationYear >= 1445 AND PublicationYear <= YEAR(CURRENT_DATE)),
    PRIMARY KEY (BookID)
);


In this updated example:

The PublicationYear must be between 1445 and the current year. This makes sure the year is realistic for a printed book.

Frequently Asked Questions

What if wrong data gets into the database?

If data that doesn't follow the domain constraints tries to enter the database, the system won't accept it. This keeps the data clean and correct.

Can I add these rules to data already in the database?

Yes, you can add these rules later, but you might need to fix or change existing data to make sure it all follows the new rules.

Do all databases use these rules the same way?

Most databases use these rules, but how you set them up might be a little different depending on the database system you're using.

Conclusion

In this article, we learned about domain constraints in databases. These are rules that help keep data correct and make sure it fits what's expected. We talked about two main types: "Not Null," which means a data spot can't be left empty, and "Check," which makes sure data fits specific rules, like being within a certain range. These constraints are like helpers that make sure the data in a database is reliable and makes sense. This is important for keeping information organized and useful for everyone who needs it.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass