sql

SQL Constraints Example

Welcome readers, in this tutorial, we will understand the different SQL Constraints that maintain the data integrity inside a database table.

1. Introduction

SQL Constraints are the restrictions that help maintain the data integrity inside a database table. Constraints can be divided into column level (i.e. limiting only column data) or table level (i.e. limiting whole data in a database table). The commonly used constraints are,

  • NOT NULL: This constraint restricts a column from the null value
  • UNIQUE: This constraint restricts a column to have different values i.e. a column cannot have a duplicate value
  • PRIMARY KEY: This constraint uniquely identifies each record in a database table. This also ensures that column values must be unique and not null
  • FOREIGN KEY: This constraint unique identifies each record in a different table
  • CHECK: This constraint restricts a column value if it does not satisfy the given condition
  • DEFAULT: This constraint sets a default value for the column. This value is set when no value is specified by the user

To start with this tutorial, we are hoping that users at present have their preferred database installed on their machines. For easy usage, I am using MySQL on a Windows operating system. If someone needs to go through the MySQL installation, please watch this video.

2. SQL Constraints Example

Here is a systematic guide and we are using MySQL Database and Workbench.

2.1 NOT NULL Constraint

The following command will declare the id and fullname fields of table1 as not null i.e. these columns cannot have a null value.

1
2
3
4
5
6
/* Query #1 :: Adding NOT NULL constraint on the table */
CREATE TABLE IF NOT EXISTS table1 (
    id INT(6) NOT NULL,
    fullname VARCHAR(10) NOT NULL,
    address VARCHAR(20)
);

If everything goes well, the table with not null constraint will be created.

SQL Constraints - Not Null Constraint
Fig. 1: Not Null Constraint

2.2 UNIQUE Constraint

The following command will declare the id field of table2 as not null and unique i.e. this column will have only unique and not null value.

/* Query #2 :: Adding UNIQUE constraint on the table */
CREATE TABLE IF NOT EXISTS table2 (
    id INT(6) NOT NULL UNIQUE,
    fullname VARCHAR(10),
    address VARCHAR(20)
);

If everything goes well, the table with unique constraint will be created.

SQL Constraints - Unique Constraint
Fig. 2: Unique Constraint

2.3 PRIMARY KEY Constraint

The following command will declare the id column as a primary key. This column must contain a unique & not null value and works like an index in a database table.

/* Query #3 :: Adding PPRIMARY KEY constraint on the table */
CREATE TABLE IF NOT EXISTS table3 (
    id INT(6) NOT NULL,
    fullname VARCHAR(10),
    address VARCHAR(20),
    PRIMARY KEY (id)
);

If everything goes well, the table with a primary key constraint will be created.

SQL Constraints - Primary Key Constraint
Fig. 3: Primary Key Constraint

2.4 FOREIGN KEY Constraint

The following command will declare the id field as a foreign key and establishes a relationship with table3.

/* Query #4 :: Adding FOREIGN KEY constraint on the table */
CREATE TABLE IF NOT EXISTS table4 (
    refid INT(10) NOT NULL,
    id INT(10),
    PRIMARY KEY (refid),
    FOREIGN KEY (id)
        REFERENCES table3 (id)
);

If everything goes well, the table with foreign key constraint will be created.

SQL Constraints - Foreign Key
Fig. 4: Foreign Key Constraint

2.5 CHECK Constraint

The following command will apply a constraint on the age field to have a value greater than 21.

/* Query #5 :: Adding CHECK constraint on the table */
CREATE TABLE IF NOT EXISTS table5 (
    id INT(6) NOT NULL,
    fullname VARCHAR(10),
    address VARCHAR(20),
    age INT(5),
    CHECK (age >= 21)
);

If everything goes well, the table with check constraint will be created.

SQL Constraints - Check
Fig. 5: Check Constraint

2.6 DEFAULT Constraint

The following command will set a default value for the product_manufacture_city column. This default value will be added to all new records if no value is specified by the user.

/* Query #6 :: Adding DEFAULT constraint on the table */
CREATE TABLE IF NOT EXISTS table6 (
    id INT(6) NOT NULL,
    product_name VARCHAR(50) NOT NULL,
    product_manufacture_city VARCHAR(30) DEFAULT 'India'
);

If everything goes well, the table with default constraint will be created.

SQL Constraints - Default
Fig. 6: Default Constraint

2.7 Dropping a Constraint

Let’s say we want to drop a constraint on a column. Developers can use the following command syntax to drop an existing constraint from a column.

1
2
/* Query #6 :: Adding DEFAULT constraint on the table */
ALTER TABLE 'table_name' DROP CONSTRAINT 'constraint_name';

For e.g., if want to remove the foreign key constraint from table4, we can run the following command in terminal.

1
ALTER TABLE 'table4' DROP FOREIGN KEY 'id';

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

3. Conclusion

In this section, developers learned how to create different SQL Constraints in a database table. Always remember, users can specify these constraints even after creating a table. For that, they will have to use the ALTER statement like below:

ALTER TABLE 'table_name' ADD CONSTRAINT 'constraint_name' 'constraint_type'

Developers can download the sample scripts in the Downloads section.

4. Download the SQL Script

This was an example of creating SQL Constraints in a database table.

Download
You can download the full source code of this example here: SQL Constraints Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button