MySQL

How to Add NOT NULL Constraint in MySQL using ALTER Command

In this tutorial, we are going to see how to add Not Null constraint in MySQL using ALTER command. The constraint Not Null is a constraint that guarantees that the values stored in a column are not NULL.

To apply the constraint Not Null to a column in MySQL, use the statement ALTER TABLE …. MODIFY and reformulate the definition of the column by adding the Not Null attribute.
 
 

Syntax:

Here is the syntax to add the constraint NOT NULL to a column in MySQL

ALTER TABLE tableName MODIFY columnName VARCHAR(255) NOT NULL;

 

Example:

Here is an example to add the constraint NOT NULL to the column “Name” of the “Employee” table:

ALTER TABLE Employee MODIFY Name VARCHAR(20) NOT NULL;

To make sure you don’t miss anything, you can use the statement SHOW CREATE TABLE to display the full definition of the column:

SHOW CREATE TABLE Employee;

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *