EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

SQL Constraints

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » SQL Tutorial » SQL Constraints

SQL Constraints

Introduction to SQL Constraints

SQL Constraints are the guidelines used to create restrictions to be implemented on the contents of the table or database, so that the records should obey these rules in order to be placed in that table. The guidelines are designed by the business personnel or the database architect, in an attempt to organize the data in the database, that can give assurance to flexible future upgrades. These constraints are designed carefully and prudently, as it results in overall smart design of the database.

In SQL, we have many different kinds of constraints. Let us look at the following few constraints in this article.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • NOT NULL
  • CHECK
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY
  • DEFAULT

Different Kinds of SQL Constraints

Below given are different kinds:

1. NOT NULL Constraint

This constraint is used when you do not want any value in that particular column to be a Null value. This means that we cannot insert a Null value for that column while inserting a new row in the table. Every field in this column always has a non-Null value. A null value means that a particular field has been left empty, and values such as zero or blank space do not come under Null values.

Let us look at an example to create a table called Employee, having 5 columns, where empid, name and mobile columns do not accept NULL values.

CREATE TABLE Employee (
empid INT NOT NULL,
name VARCHAR(20) NOT NULL,
dob DATE,
mobile VARCHAR(10) NOT NULL,
address VARCHAR(20)
);

2. CHECK Constraint

This constraint limits the values that can be entered in that particular column of the table. To understand this better, let us take the example of passing marks in an exam. The range of values for these marks can only be from 35 to 100. To ensure that only values in this range are entered, we can create a CHECK constraint.

Let us look at an example of creating a CHECK constraint on the salary of employees.

CREATE TABLE Employee (
empid INT NOT NULL,
name VARCHAR(20) NOT NULL,
dob DATE,
mobile VARCHAR(10) NOT NULL,
salary INT CHECK (salary >= 15000 AND salary <= 30000),
address VARCHAR(20)
);

3. UNIQUE Constraint

This constraint is applied to ensure that the particular column accepts only unique values, and repetitive values are not allowed with such a constraint on the column. We can create multiple UNIQUE constraints on various columns in a table. A UNIQUE constraint allows NULL values to be entered.

Let us look at an example of enforcing the UNIQUE constraint. In this example, we are creating a column called mobile, in the table Employee, which is to be unique and cannot accept the same mobile number twice.

CREATE TABLE Employee (
empid INT NOT NULL,
name VARCHAR(20) NOT NULL,
dob DATE,
mobile VARCHAR(10) NOT NULL UNIQUE,
address VARCHAR(20)
);

4. PRIMARY KEY Constraint

This constraint is used to identify a particular column or a group of columns that can uniquely identify a row in the table. With the PRIMARY KEY constraint in place, any row cannot have a duplicate value. We cannot have NULL as the value for such a column. Even though both a PRIMARY KEY constraint and a UNIQUE constraint impose that the values are to be unique, we use a UNIQUE constraint for when we do not want to declare the column as Primary Key, but still want the values in that column to be unique. We can have only a single PRIMARY KEY column or group of columns in a table, but we can declare many individual columns to be UNIQUE.

Let us look at an example of the Employee table, creating unique employee IDs, therefore we can declare the empid column to be the PRIMARY KEY.

CREATE TABLE Employee (
empid INT NOT NULL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
dob DATE,
mobile VARCHAR(10) NOT NULL UNIQUE,
address VARCHAR(20)
);

5. FOREIGN KEY Constraint

This constraint helps the data in one table to establish a relationship with the data in another table in the database. Foreign Key can be a single column or a set of columns. Let us consider two tables, Employee and Departments. Suppose we a column called depicted in Employee and a departed in Departments. We can reference the departed of Employee to the departed of Departments if the columns match. In this case, the column in Employee becomes a foreign key reference to the column in the Department table, which is a primary key.

CREATE TABLE Employee (
empid INT NOT NULL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
dob DATE,
mobile VARCHAR(10) NOT NULL UNIQUE,
address VARCHAR(20),
depicted INT FOREIGN KEY REFERENCES Department(depicted)
);

6. DEFAULT Constraint

This constraint is used to specify the default value for a particular column in the table. This way if there is no value inserted for that column explicitly, the database engine can always refer to the default value specified and insert that in the column. Suppose we have inserted a column with the constraint NOT NULL and also with the constraint DEFAULT, then we do not need to explicitly define a default value. Even without giving a default value, the particular row will be inserted in the table.

Let us look at an example where we have entered the place in the address as default to have the value ‘India’.

CREATE TABLE Employee (
empid INT NOT NULL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
dob DATE,
mobile VARCHAR(10) NOT NULL UNIQUE,
address VARCHAR(20) DEFAULT ‘India’,
depicted INT FOREIGN KEY REFERENCES Department(depicted),
);

Conclusion

SQL constraints help the developer by specifying restrictions and rules for the data that is to be inserted in the table. Constraints can be applied at the column level, just to the particular column or at the table level, where the constraints are applied to the complete table. These constraints restrict the kind of information that can be inserted in the table. This guarantees the correctness and consistency of the data in the table. In case of any violations of the rules specified by the constraints, the action is terminated.

Recommended Articles

This is a guide to SQL Constraints. Here we discuss the introduction to SQL Constraints along with different kinds of SQL Constraints which include Not Null, Check, Unique, Primary, Foreign and Default Constraints. You can also go through our other suggested articles to learn more –

  1. Database in SQL​ 
  2. Uses of SQL
  3. Foreign Key in SQL
  4. MySQL Constraints

SQL Training Program (7 Courses, 8+ Projects)

7 Online Courses

8 Hands-on Projects

73+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
    • SQL Super Key
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • SQL Window Functions
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • ANY in SQL
    • LIKE Query in SQL
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL MOD()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Server Permission
    • T-SQL INSERT
    • SQL Ranking Function
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • SQL Formatter
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL INSTR()
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - SQL Training Program (7 Courses, 8+ Projects) Learn More