Month End Offer : Get 30% OFF + $999 Study Material FREE - SCHEDULE CALL

sddsfsf

How to Check Constraint in SQL & SQL Default Constraint

 

While working with a database, you might sometimes face two scenarios. In one scenario, you have a field that cannot accept null values. At the same time, you want to make an arrangement such that if you miss out on entering a value in that field the system will automatically take care of it. Or otherwise, it will put a value you set previously on your behalf. The other scenario is that you want to find out or validate the data that is being entered into the system. SQL Server allows both of these to happen using the Default and the Check constraint. Over the following few paragraphs, we will learn about check and default constraints in SQL.

What are SQL Constraints

Rules for a table's data can be specified with the help of SQL constraints.The type of data that can be entered into a table can be restricted with constraints. This guarantees the reliability and accuracy of the data in the table. The action is aborted if there is a violation between the constraint and the data action. Column-level or table-level constraints are possible. Constraints at the table level apply to the entire table, whereas those at the column level apply to a single column.

In SQL, the following constraints are frequently used:

  • NOT NULL:- Ensures that a column cannot contain a NULL value;
  • UNIQUE:- Ensures that each column's values are distinct;
  • PRIMARY KEY:- Enables a combination of UNIQUE and NOT NULL. Identifies each row in a table in a unique way
  • FOREIGN KEY:- Prevents actions that would break links between tables
  • CHECK:- Ensures that the values in a column meet a particular condition
  • DEFAULT:- Sets a default value for a column if none is specified
  • CREATE INDEX:- Used to create and retrieve data from the database quickly

Check Constraint in SQL

Using the MS SQL CHECK constraint, a column's value range can be restricted.A column with a SQL Server CHECK constraint on it will only allow specific values.A CHECK constraint can limit the value specificrtain columns based on values in other columns in the row if it is applied to a table.We will then learn how to use a DBMS check constraint and learn to check constraints in SQL.

Advantages of MS SQL Check Constraint

The following are some of the benefits of SQL Server Check Constraints:

  • The capacity to enforce rules in the database without necessarily requiring additional application logic.
  • The application cannot circumvent the check.
  • Check constraints to provide data integrity. The rule cannot be circumvented during dynamic SQL or ad hoc processing.

Disadvantages of Check Constraint in DBMS

The disadvantages of MS SQL Check Constraints are as follows

CHECK constraints reject FALSE-valued values. A constraint may be overridden by the presence of null values in expressions because they evaluate to UNKNOWN.

SQL CHECK Constraint on CREATE TABLE

The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table is created. The CHECK constraint ensures that the age of a person must be 18 or older: The SQL Query is as follows

CREATE TABLE Persons_check (

ID int NOT NULL,

LastName varchar(255) NOT NULL,

FirstName varchar(255),

Age int CHECK (Age>=18)

);

The output looks like below

To allow the naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:

CREATE TABLE Persons_Check1 (

ID int NOT NULL,

LastName varchar(255) NOT NULL,

FirstName varchar(255),

Age int,

City varchar(255),

CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')

);

The output is as below

SQL Server Check Constraint on Alter Table

To create a CHECK constraint on the "Age" column when the table is already created, use the following SQL:

ALTER TABLE Persons
ADD CHECK (Age>=18);

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:

ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');
Drop a SQL Server Check Constraint
To drop a CHECK constraint, use the following SQL:
ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge;

The DEFAULT constraint is used to set a default value for a column.The default value will be added to all new records if no other value is specified.

Uses of SQL Default Constraint

The uses of SQL Default Constraint are

  • Prevent bad data from being entered into tables of interest are some of the uses of SQL Default Constraint.
  • Require database-level enforcement of business logic.
  • Detailed documentation of crucial database rules
  • Ensure the integrity of relationships between any number of tables.
  • Make the database work better.
  • Reinforce individuality.

Advantages of SQL Default Constraint

A column's default value can be set with the SQL DEFAULT constraint. If no other value is specified, all new records will have the default value added to them.

Disadvantages of SQL Default Constraint

Disadvantages of SQL Default Constraint are as follows

  • Even if we wanted to, we would not be able to insert a null value into a field in a table if a default constraint was applied to it.

Create Default Constraint in SQL Server

The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created: The SQL query is as follows

CREATE TABLE Persons_default (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);

The output looks like below

The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE(: The SQL query looks like this below

CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE()
);

The output looks like below

SQL Default Constraint on Alter Table

To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL:

ALTER TABLE [dbo].[Persons_default]
ADD CONSTRAINT df_City
DEFAULT 'Sandnes' FOR City;
DROP a SQL DEFAULT Constraint
To drop a DEFAULT constraint, use the following SQL:
ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT;

Conclusion

Over the last few paragraphs, we learned about Check and Default Constraints in SQL. We learned their uses, advantages, and disadvantages. Hope this becomes an introductory text for people who want to learn more about these two constraints and if you wander what MySQL check constraint is can do that too.

cta14 icon

SQL Testing Training

  • Personalized Free Consultation
  • Access to Our Learning Management System
  • Access to Our Course Curriculum
  • Be a Part of Our Free Demo Class

Trending Courses

Gen AI icon

Gen AI

  • Introduction to Generative Models
  • Generative Adversarial Networks (GANs)
  • The Art and Science of Prompt Engineering
  • MLOps: Deploying Generative AI Models
Gen AI icon1

Upcoming Class

11 days 11 Aug 2026

Agentic AI icon

Agentic AI

  • Introduction to Agentic AI
  • Multi-Agent Setup with LangGraph Context Handling in Graphs
  • Performance Benchmarking Advanced Prompt Engineering for Agents
  • Agent Behavior Tuning Project and Mock Session
Agentic AI icon1

Upcoming Class

7 days 07 Aug 2026

AI in Automation Testing icon

AI in Automation Testing

  • Intro to AI & ML in Automation
  • Playwright + JS (JavaScript) + API Tesng
  • Automaon with Using ChatGPT & Playwright MCP server
  • GitHub Copilot, AI Tools & Interview preparation
AI in Automation Testing icon1

Upcoming Class

-0 day 31 Jul 2026

Cyber Security icon

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security icon1

Upcoming Class

7 days 07 Aug 2026

Data Science icon

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science icon1

Upcoming Class

-0 day 31 Jul 2026

QA icon

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA icon1

Upcoming Class

-0 day 31 Jul 2026

Salesforce Service Cloud icon

Salesforce Service Cloud

  • Industry Knowledge Introduction
  • Adoption and Maintenance
  • Interaction Channels Introduction
  • Integration and Data Management
Salesforce Service Cloud icon1

Upcoming Class

14 days 14 Aug 2026

AWS icon

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53
AWS icon1

Upcoming Class

-0 day 31 Jul 2026