Code has been added to clipboard!

SQL Constraint List and Syntax

Reading time 1 min
Published Aug 9, 2017
Updated Oct 11, 2019

SQL Constraint: Main Tips

  • An SQL constraint defines a rule to the data records in tables.
  • When an infraction against the data record action and the constraint occurs that action is stopped by it.
  • They can be defined after or when creating the table.

Constraint Syntax and Quick Reference

Example
CREATE TABLE table_name (
    column1 datatype constraint_one,
    column2 datatype constraint_two,
    column3 datatype constraint_three,
    ....
);

The main constraints in SQL are:

  • NOT NULL - there is no NULL value contained in a column.
  • UNIQUE - every row in a specific column needs a unique value.
  • PRIMARY KEY - a combination of UNIQUE and NOT NULL: columns must have an identity which is unique. This helps when you need to locate a specific data record faster.
  • FOREIGN KEY - one table's SQL referential integrity matches another table's.
  • CHECK - a defined condition is met within the value.
  • DEFAULT - stock column value.

SQL Constraint: Summary

  • You can define a constraint when you create a table, or afterrwards.
  • Constraints stop data record actions with data that breaks the constraint rule.
Learn SQL
Introduction
Syntax
Data Types
Server Data Types
Commands
Commands List
Wildcards
Constraints
Aggregate Functions
Date Functions
Date Format
Injection
SQL Server Hosting
Views
Auto-incrementation
SQL Operators
AS
AND & OR
IN
BETWEEN
WHERE
GROUP BY
HAVING
ORDER BY
LIKE
NOT
NOT EQUAL
UNION
NULL
NOT NULL
DEFAULT
UNIQUE
FOREIGN KEY
PRIMARY KEY
CHECK
Indexes
ALTER TABLE
CREATE DATABASE
CREATE TABLE
DELETE
DROP
INSERT INTO SELECT
INSERT INTO
SELECT
SELECT DISTINCT
SELECT INTO
SELECT TOP
UPDATE
FULL OUTER JOIN
INNER JOIN
JOIN
LEFT JOIN
RIGHT JOIN
AVG()
COUNT
FIRST
LAST
MAX
MIN()
SUM()
LEN
UCASE
MID
NOW
ROUND
FORMAT
LOWER
CONVERT
ISNULL