MySQL Not Null

MySQL NOT NULL Constraint

The NOT NULL constraint in MySQL ensures that a column cannot store NULL values. It is used when you want to ensure that a column always contains a value and cannot be left empty.



NOT NULL on CREATE TABLE


Syntax:

CREATE TABLE table_name (
  column1 data_type NOT NULL,
  column2 data_type,
  column3 data_type NOT NULL
);


Example

Example
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100),
age INT NOT NULL
);

Explanation:

  • name and age cannot be NULL.
  • email can accept NULL values.


NOT NULL on ALTER TABLE



Syntax:


ALTER TABLE table_name
MODIFY column_name data_type NOT NULL;


Example:

Example
ALTER TABLE students
MODIFY email VARCHAR(100) NOT NULL;

Explanation:

  • This makes the email column mandatory, ensuring it cannot store NULL values.



Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.