TUTORIAL INDEX

MySQL Constraints

|

What is MySQL Constraint and Why is it used?


Constraints are rules that you apply to table columns to make sure the data is correct. They help stop wrong or unwanted data from getting into the table, so the data stays valid and safe.

Constraints are used to prevent duplicate, invalid, or unwanted data from being entered and stored in a table by mistake. They prevent this type of data from being stored in the table and help maintain data integrity, accuracy, and consistency.


The list of Constraint types is:


  1. Primary Key
  2. Foreign Key
  3. Unique Constraint
  4. NOT NULL
  5. CHECK Constraint
  6. Default Constraint

MySQL Constraints and Their Uses:


MySQL has different types of constraints which control the data added to a table. Each constraint has a different use.

Some constraints stop duplicate data, some stop empty values, and some check if the data follows a rule.

The table below shows the main types of MySQL constraints and what they do.


Constraint type Work Example PRIMARY KEY It identifies each row with a unique value. Student ID


FOREIGN KEY It connects two tables. Student ID


UNIQUE It does not allow duplicate values. Email


NOT NULL It does not allow an empty value. Student Name


CHECK It checks if the value follows a rule. Age ≥ 18


DEFAULT It adds a value when no value is given. Country = India

ONLINE WEB TOOLS

WhatIsMyIpAddress
Shorterner