Advertisement
Google Ad Slot: content-top
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:
nameandagecannot beNULL.emailcan acceptNULLvalues.
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
emailcolumn mandatory, ensuring it cannot storeNULLvalues.