sql
  1. sql-overview-of-constraints

SQL Constraints Overview

Introduction

Constraints are rules we define on a table that restricts the kind of data that can be inserted into the table. This ensures data accuracy, consistency, and integrity.

There are several types of constraints in SQL like primary key, foreign key, unique key, check, and not null constraints.

Syntax

The syntax for creating constraints varies depending on the type of constraint.

Primary Key Constraint

CREATE TABLE table_name (
    column1 datatype, 
    column2 datatype,
    ...,
    PRIMARY KEY (column1, column2, ...));

Foreign Key Constraint

CREATE TABLE table1 (
    column1 datatype primary key, 
    column2 datatype,
    ...);

CREATE TABLE table2 (
    column1 datatype primary key,
    column2 datatype,
    ...,
    FOREIGN KEY (column1) REFERENCES table1(column1));

Unique Key Constraint

CREATE TABLE table_name (
    column1 datatype, 
    column2 datatype,
    ...,
    UNIQUE (column1));

Check Constraint

CREATE TABLE table_name (
    column1 datatype CHECK (condition1), 
    column2 datatype CHECK (condition2),
    ...);

Not Null Constraint

CREATE TABLE table_name (
    column1 datatype NOT NULL, 
    column2 datatype NOT NULL,
    ...);

Example

Primary Key Constraint Example

CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    age INT);

Foreign Key Constraint Example

CREATE TABLE orders (
    id INT PRIMARY KEY,
    product_name VARCHAR(255),
    customer_id INT,
    FOREIGN KEY (customer_id) REFERENCES customers(id));

Unique Key Constraint Example

CREATE TABLE users (
    id INT,
    email VARCHAR(255),
    UNIQUE (id, email));

Check Constraint Example

CREATE TABLE books (
    id INT,
    name VARCHAR(255),
    author VARCHAR(255),
    quantity INT CHECK (quantity >= 0));

Not Null Constraint Example

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL);

Explanation

Primary Key Constraint

A primary key constraint is used to define a column or group of columns that uniquely identify each row in the table. In our example, we use the id column as a primary key to uniquely identify each employee.

Foreign Key Constraint

A foreign key constraint is used to link two tables together. In our example, we use the customer_id column in the orders table as a foreign key referencing the id column in the customers table.

Unique Key Constraint

A unique key constraint ensures that the values in a column or group of columns are unique. In our example, we create a unique key constraint on the id and email columns to ensure that each user has a unique combination of id and email.

Check Constraint

A check constraint is used to define a condition that must be satisfied for the data to be inserted or updated in the table. In our example, we use the check constraint to ensure that the value of the quantity column is always greater than or equal to zero.

Not Null Constraint

A not null constraint is used to ensure that a column cannot have null values. In our example, we use the not null constraint on the name and email columns in the customers table to ensure that those columns are always populated with data.

Use

Constraints are an important feature of SQL that helps ensure data accuracy, consistency, and integrity. They help to prevent data entry mistakes and improve the quality of the data stored in the database.

Constraints can be used to define relationships between tables, ensure data uniqueness, enforce data integrity, and much more. They are an essential tool for working with databases and should be used whenever appropriate.

Important Points

  • Constraints are rules that restrict the kind of data that can be inserted into a table.
  • There are several types of constraints in SQL like primary key, foreign key, unique key, check, and not null constraints.
  • Constraints ensure data accuracy, consistency, and integrity.
  • Constraints can be used to define relationships between tables, ensure data uniqueness, enforce data integrity, and much more.
  • Constraints are an essential tool for working with databases and should be used whenever appropriate.

Summary

In this overview, we learned about the different types of SQL constraints and their syntax. We also looked at some examples of constraints and explained how they can be used to ensure data accuracy, consistency, and integrity. Constraints are an essential tool for working with databases and help to improve the quality of the data stored in the database.

Published on: