MySQL Constraint – CHECK





MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Hello everyone in the previous article we learn about MySQL Constraint –FOREIGN KEY and in this article, we will learn CHECK constraint with example. Let’s go.

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/

What is CHECK constraint?

MySQL CHECK constraint controls the values in a column satisfies a specific condition. CHECK constraint defines on a table it can limit the values on specific columns based on values in other columns in the row.

Following is the CREATE TABLE statement with CHECK constraint syntax:
                             CREATE TABLE table_name(
                                            Column1_name data type CHECK (condition)
                                            Column2_name data type CHECK (condition)
                             );




MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/


For define multiple columns CHECK constraint then you can use following syntax:
CREATE TABLE table_name(
                              column1_name data type ()
                              column1_name data type ()
                              CONSRTAINT constraint_name CHECK (condition AND condition)
);

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/



Example of CREATE TABLE statement with a CHECK constraint:
                              CREATE TABLE EMPLOYEE (
                                            EID INT NOT NULL,
                                            First_Name VARCHAR (255),
                                            Last_Name VARCHAR (255),
                                            Age INT CHECK (Age > =25)
                              );
Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/

Example for define multiple column a CHECK constraint:
                              CREATE TABLE EMPLOYEE (
                                            EID INT NOT NULL,
                                            First_Name VARCHAR (255),
                                            Last_Name VARCHAR (255),
                                            Address VARCHAR (255),
                                            CONSTRAINT CHK_EMPLOYEE CHECK (Age > = 25 AND Address = ‘USA’)
                              );
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

You can add CHECK constraint when the table is already created using the ALTER TABLE statement. Following is the syntax and example:
Syntax
ALTER TABLE table_name
                              ADD CONSTRAINT constraint_name CHECK (column_name);
               Example
                              ALTER TABLE EMPLOYEE
                              ADD CONSTRAINT CHK_EMPLOYEE CHECK (Age > = 25 AND Address = ‘USA’);
If you want to DROP created a CHECK constraint, then follow the steps:
ALTER TABLE EMPLOYEE
DROP CHECK CHK_EmployeeAge;


MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/


Comments

Popular posts from this blog

ALTER statement in MySQL

Order By Clause in MySQL

MySQL Constraint – UNIQUE