MySql Constraint Syntax:- Constraints have specified the rule of data that will store in a database table. That means constraints will allow or restrict what value of data will store in the table. So, constraints will ensure accuracy, integrity, and type of data. So, Constraint will define rules to restrict or allow what value will store in the database table column. Basically, constraints are associated with a specific column or with a table. Constraints can be defined when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.
Syntax of constraint:-
CREATE TABLE table_name (
col1_name datatype constraint,
col2_name datatype constraint,
col3_name datatype constraint,
);
On the basis of the declaration, we can categorize constraints into two types.
Table-based constraints:- This constraint affects the entire column. It limits the type of data for the whole table.
Example:- CREATE TABLE employee(
salary INT,
CHECK(salary >500)
);
Column-based constraints:- This constraint affect specific column only. It limits the type of particular column data.
Example:- CREATE TABLE employee(
salary INT CHECK(salary >500)
);
some common constraints which we can use in MySql:-
PRIMARY KEY CONSTRAINT
FOREIGN KEY CONSTRAINT
UNIQUE CONSTRAINT
CHECK CONSTRAINT
NOT NULL CONSTRAINT
DEFAULT CONSTRAINT
CREATE INDEX CONSTRAINT
AUTO_INCREMENT CONSTRAINT
INDEX CONSTRAINT
ENUM CONSTRAINT
For more detail about MySQL constraints please follow here
Get the basic idea about constraint. thanx.