That’s where SQL constraints help. These data rules define conditions that tables must follow.

What Are SQL Constraints? Definitions, Uses, and Practical Examples

SQL serves as a database management tool. Database tables require the implementation of rules to maintain correct and proper data entries. That’s where SQL constraints help. These data rules define conditions that tables must follow.

This article explains SQL constraints, their definitions, and practical uses while providing relevant examples. The discussion will demonstrate the ways constraints enhance database quality.

Don’t Miss This: SQL Commands | DDL, DQL, DML, DCL and TCL Commands

What Are SQL Constraints?

A database table column accepts SQL constraints as predefined rules to enforce. The established rules assist database management by validating the stored data. An SQL constraint enables field correction when users forget to enter values and when they enter duplicate information in the same field.

Database constraints establish rules that maintain accurate and clean data storage. Such rules execute directly on the database rather than running from inside your application code.

Why Use SQL Constraints?

Using constraints is very important. Here’s why:

  • They stop the wrong or invalid data.
  • They make sure all rows are unique when needed.
  • They protect relationships between tables.
  • They save time by preventing mistakes early.

Let’s look at each type of SQL constraint and how it works.

Next Up: SQL Data Types Explained with Examples

1. NOT NULL Constraint

This function makes sure a column cannot be left empty.

Example:

CREATE TABLE Students (
  ID INT NOT NULL,
  Name VARCHAR(50) NOT NULL
);

In this case, every student must have an ID and a Name.

2. UNIQUE Constraint

This ensures all values in a column are different. It is used for things like emails or usernames.

Example:

CREATE TABLE Users (
  Email VARCHAR(255) UNIQUE
);

Now, no two users can have the same email.

You Might Also Like: How to Delete a Listing on Airbnb

3. PRIMARY KEY Constraint

This identifies each row in a table uniquely. It also means the value cannot be empty or repeated.

Example:

CREATE TABLE Orders (
  OrderID INT PRIMARY KEY,
  OrderDate DATE
);

Every order will have a unique ID.

4. FOREIGN KEY Constraint

This connects one table to another. It ensures the value exists in the other table.

Example:

CREATE TABLE OrderDetails (
  OrderID INT,
  FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);

Only valid orders can be added to the details table.

5. CHECK Constraint

This sets a condition for the value. It won’t be saved if the value doesn’t match the condition.

Example:

CREATE TABLE Products (
  Price DECIMAL(10,2) CHECK (Price > 0)
);

Prices must be more than 0.

See Related Article: Watts to kWh Calculator Using HTML, CSS and JavaScript

6. DEFAULT Constraint

This sets a value when none is given.

Example:

CREATE TABLE Customers (
  Status VARCHAR(10) DEFAULT 'Active'
);

If status is not given, it will be ‘active’ by default.

How to Add Constraints Later

You can also add constraints after a table is created using ALTER TABLE.

Example:

ALTER TABLE Students
ADD CONSTRAINT chk_age CHECK (Age >= 18);

This ensures that the age is 18 or older.

Also read: SQL Interview Questions

Real Examples in Daily Use

  • In online stores: UNIQUE for product codes.
  • In banks: FOREIGN KEY for accounts and users.
  • In school systems: CHECK to ensure valid marks or grades.

These help keep your data safe and correct.

1. Online Shopping Website

Use of UNIQUE and CHECK Constraints

CREATE TABLE Products (
  ProductID INT PRIMARY KEY,
  ProductCode VARCHAR(100) UNIQUE,
  Price DECIMAL(10, 2) CHECK (Price > 0)
);

Explanation:

  • Each product has a unique code (like SKU).
  • The price must be greater than 0.
  • Ensures no two products have the same code and no free products by mistake.

2. Student Management System

Use of NOT NULL and FOREIGN KEY Constraints

Also check: SQL Cheat Sheets

CREATE TABLE Students (
  StudentID INT PRIMARY KEY,
  Name VARCHAR(100) NOT NULL
);

CREATE TABLE Enrollments (
  EnrollmentID INT PRIMARY KEY,
  StudentID INT,
  FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);

Explanation:

  • Every student must have a name.
  • Students can only enroll if they are on the main Students table.
  • Keeps student records linked correctly.

3. Banking System

Use of DEFAULT and CHECK Constraints

Also read: SQL Roadmap: Ultimate Guide for Beginners

CREATE TABLE Accounts (
  AccountID INT PRIMARY KEY,
  Balance DECIMAL(12, 2) DEFAULT 0 CHECK (Balance >= 0),
  Status VARCHAR(20) DEFAULT 'Active'
);

Explanation:

  • All new accounts start with a 0 balance.
  • Balance can never go below 0.
  • Status is ‘Active’ unless otherwise specified.

Final Thoughts

Database rules called SQL constraints function as basic regulations that strengthen database security. Databases stay organized with constraints that both maintain clean and usable data while simplifying their management.

The information about SQL constraints definitions with their practical examples and uses should help you apply them in your current projects. Minor elements in databases deliver essential functions that support database success.

Recommended for You

Similar Posts

Leave a Reply

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