MySQL Constraint – NOT NULL




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/

Hello everyone in previous article we learn about MySQL Constraints and in this article we will learn NOT NULL constraint with example. Let’s go.

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

What is NOT NULL Constraint?
MySQL NOT NULL constraint a column in a table is not allowed to store NULL values and by default a column can hold NULL values.

Following are the CREATE TABLE statement with NOT NULL constraint syntax:
CREATE TABLE table_name (
                              column1_name data type () NOT NULL,
                              column2_name data type () NOT NULL
):

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 NOT NULL constraint:
So we will create here Employee table with CREATE TABLE statement
CREATE TABLE EMPLOYEE (
                              EID INT NOT NULL,
                              First_Name VARCHAR (255) NOT NULL,
                              Last_Name VARCHAR (255) NOT NULL,
                              Address VARCHAR (255),
                              PRIMARY KEY (EID)
);

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

You can create NOT NULL constraint when the table is already created using ALTER TABLE statement. Following is the syntax and example:

Syntax for create NOT NULL constraint with ALTER TABLE statement:
ALTER TABLE table_name
MODIFY column_name data type () NOT NULL;

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 create NOT NULL constraint with ALTER TABLE statement on Employee Table:
We have created employee table with NOT NULL constraint and now we want to add NOT NULL constraint on Address column.
ALTER TABLE EMPLOYEE
MODIFY Address VARCHAR (255) NOT NULL;
Now the NOT NULL constraint adds on Address column.



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


Comments

Popular posts from this blog

ALTER statement in MySQL

MySQL Constraint – PRIMARY KEY

Order By Clause in MySQL