What are Integrity Constraints in SQL?

What are Integrity Constraints in SQL?
What are Integrity Constraints in SQL?


Think about you’re the gatekeeper of a society the place each resident and customer should comply with sure guidelines to keep up peace and order. On the planet of databases, these guidelines are referred to as integrity constraints. Simply as a society thrives when everybody abides by its legal guidelines, a database stays correct and constant when its information adheres to those essential constraints. Intriguing, proper? Additional on this article, we’ll focus on integrity constraints in SQL.


  • Integrity constraints in SQL guarantee information accuracy and consistency like guidelines keep order in a metropolis.
  • Integrity constraints forestall information anomalies, making certain legitimate and constant information references.
  • The 4 fundamental sorts are entity, area, referential, and test constraints.
  • Sensible examples present how these constraints enhance information accuracy and consistency and cut back errors.
  • Integrity constraints are important for a strong and dependable database system, safeguarding information integrity.

Why are Integrity Constraints Necessary?

Think about a desk storing details about clients and their orders. With out constraints, you would possibly find yourself with an order referencing a non-existent buyer! Integrity constraints assist forestall such information anomalies by implementing particular guidelines.

Varieties of Integrity Constraints in SQL

There are 4 fundamental sorts of integrity constraints in SQL, every serving a particular goal:

Sort 1:Entity Integrity Constraints

These guarantee every desk row has a novel identifier, usually enforced by way of a major key constraint. That is very useful in stopping duplicate entries and ensures a strategy to determine every document uniquely. We are able to additionally use that attribute to fetch information and set up information.

Instance of Entity Integrity Constraints

E-commerce Order Desk

  • Desk Title: Orders
  • Columns:
    • OrderID (integer) Main Key
    • CustomerID (integer)
    • OrderDate (date)
    • TotalAmount (decimal)

On this instance, OrderID is the first key. This ensures every order has a novel identifier, stopping duplicate order entries.

Sort 2: Area Constraints

These outline the legitimate values for a selected column. For instance, an age column might need a site constraint proscribing entries to NU. This ensures information conforms to the anticipated format and in addition undesirable entries within the information which can result in extra issues within the database.

Product Desk

  • Desk Title: Merchandise
  • Columns:
    • ProductID (integer) Main Key
    • ProductName (textual content)
    • Worth (decimal) NOT NULL
    • StockLevel (integer) NOT NULL

Right here, area constraints are enforced on each Worth and StockLevel columns. The NOT NULL constraints make sure the Worth will not be null.

Sort 3: Referential Integrity Constraints

These keep relationships between tables. A international key constraint creates a hyperlink between a column in a single desk (the international key) and the first key of one other desk (the referenced desk). This ensures information references in your database are legitimate and constant.

Library Database

  • We are able to prolong the library database to incorporate a Bookshelves desk:
    • Desk Title: Bookshelves
    • Columns:
      • BookshelfID (integer) Main Key
      • Location (textual content)
      • Capability (integer)
    • We are able to add a international key constraint to the Books desk:
      • Desk Title: Books 
      • Columns:
        • Creator (textual content)
        • Title (textual content)
        • ISBN (textual content) Main Key
        • BookshelfID` (integer) FOREIGN KEY REFERENCES Bookshelves(BookshelfID)

On this situation, the BookshelfID column within the Books desk turns into a international key referencing the BookshelfID major key within the Bookshelves desk. This ensures a e book document solely references a sound bookshelf location.

Sort 4: Verify Constraints

These enable for extra advanced validation guidelines on a column or group of columns. You possibly can outline a customized expression that the info should adhere to. This provides better flexibility for implementing particular enterprise logic inside your database.

Product Desk

  • Desk Title: Merchandise
  • Columns:
    • ProductID (integer) Main Key
    • ProductName (textual content)
    • Worth (decimal) CHECK (Worth > 0)
    • StockLevel (integer) CHECK (StockLevel >= 0)

Right here, area constraints are enforced on each Worth and StockLevel columns. The CHECK constraints make sure the Worth is all the time optimistic and the StockLevel isn’t destructive.

Additionally Learn: SQL: A Full Fledged Guide from Basics to Advance Level

Advantages of Utilizing Integrity Constraints

Listed here are the advantages of utilizing integrity constraints:

  • Improved Information Accuracy: Stop invalid or misguided information from coming into the database.
  • Enhanced Information Consistency: Guarantee information adheres to outlined guidelines, sustaining consistency throughout tables.
  • Diminished Errors: Implement information validation guidelines, serving to forestall errors throughout information manipulation.
  • Stronger Information Relationships: Referential constraints implement relationships between tables, making certain information references are legitimate.
  • Dependable Information Basis: Safeguard the integrity of your information, resulting in extra reliable data.

Additionally learn: Different Keys in SQL (Primary Key, Candidate Key, Foreign Key)

Understanding All Constraints With Examples

Now, we’ll use yet one more instance to grasp these constraints in higher element – Right here, we’ve taken two tables, Departments and Staff, and we’ve used constraints.

-- Create tables

CREATE TABLE Departments (






    FirstName VARCHAR(50) NOT NULL,

    LastName VARCHAR(50) NOT NULL,

    E mail VARCHAR(100) UNIQUE,

    Wage DECIMAL(10, 2) CHECK (Wage > 0),

    DeptID INT,

    FOREIGN KEY (DeptID) REFERENCES Departments(DeptID)


-- Insert pattern information

INSERT INTO Departments (DeptID, DeptName) VALUES

(1, 'HR'),

(2, 'IT'),

(3, 'Finance');

INSERT INTO Staff (EmpID, FirstName, LastName, E mail, Wage, DeptID) VALUES

(101, 'John', 'Doe', '[email protected]', 50000, 1),

(102, 'Jane', 'Smith', '[email protected]', 60000, 2),

(103, 'Mike', 'Johnson', '[email protected]', 55000, 3);
Integrity Constraints in SQL

Examples of Every Constraint

Now, let’s look at every constraint:

  1. Main Key Constraint: Ensures a novel identifier for every document.

    This can fail as a result of duplicate major key

INSERT INTO Departments (DeptID, DeptName) VALUES (1, 'Advertising and marketing');
Primary Key Constraint
  1. Overseas Key Constraint: Maintains referential integrity between tables.

This can fail as a result of non-existent DeptID

INSERT INTO Staff (EmpID, FirstName, LastName, E mail, Wage, DeptID)
VALUES (104, 'Alice', 'Brown', '[email protected]', 52000, 4);
Primary Key Constraint
  1. Distinctive Constraint: Ensures no duplicate values in a column.

    This can fail as a result of duplicate electronic mail

INSERT INTO Staff (EmpID, FirstName, LastName, E mail, Wage, DeptID)
VALUES (105, 'Bob', 'Wilson', '[email protected]', 54000, 2);
Primary Key Constraint
  1. Verify Constraint: Enforces area integrity by limiting the values in a column.

This can fail as a result of destructive wage

INSERT INTO Staff (EmpID, FirstName, LastName, E mail, Wage, DeptID)
VALUES (106, 'Carol', 'Davis', '[email protected]', -1000, 3);
Primary Key Constraint
  1. Not Null Constraint: Ensures a column can’t have NULL values.

    This can fail as a result of NULL FirstName

INSERT INTO Staff (EmpID, FirstName, LastName, E mail, Wage, DeptID)
VALUES (107, NULL, 'Taylor', '[email protected]', 58000, 1);
Primary Key Constraint

These integrity constraints work collectively to keep up information consistency and reliability in your database.

Additionally learn: SQL For Data Science: A Beginner Guide!


So, we noticed how integrity constraints assist construct a strong and dependable database system. These constraints act as a security internet, safeguarding the integrity of your information and making certain its accuracy for future use. By implementing guidelines that information should adhere to, they forestall errors and inconsistencies that might in any other case result in important points. Whether or not it’s sustaining distinctive identifiers with major keys, making certain relationships with international keys, or implementing particular information ranges with test constraints, these mechanisms are important for the well being and reliability of your database. As you design and handle your

Ceaselessly Requested Questions

Q1. What are integrity constraints in SQL? 

Ans. Integrity constraints in SQL are guidelines that guarantee information accuracy and consistency, they usually:
A. Implement information validation
B. Preserve relationships between tables
C. Stop invalid information entry
D. Embrace Main Key, Overseas Key, Distinctive, Verify, and Not Null constraints

Q2. What are the 6 constraints in SQL? 

Ans. A: The six fundamental constraints in SQL are:
A. Main Key Constraint
B. Overseas Key Constraint
C. Distinctive Constraint
D. Verify Constraint
E. Not Null Constraint
F. Default Constraint

Q3. What’s the integrity of knowledge in SQL? 

Ans. Information integrity in SQL means:
A. Information is correct and constant
B. Data stays dependable over time
C. Information is protected against unauthorized adjustments
D. Saved information matches its supposed illustration
E. Relationships between information components are preserved

This autumn. What’s information integrity vs integrity constraints? 

Ans. Information integrity is the total idea of sustaining correct and constant information, whereas integrity constraints are the precise guidelines applied in SQL to implement information integrity. In different phrases:
A. Information integrity is the aim of making certain information is correct, constant, and dependable.
B. Integrity constraints are the means: particular guidelines and mechanisms in SQL that assist obtain and keep information integrity.
C. Integrity constraints are instruments used to implement and implement information integrity inside a database administration system.

Leave a Reply

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