Loading

MySQL Constraint

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 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 effect 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

 

Share with:


Leave a Reply

Connect with:





z35W7z4v9z8w