How to use DEFAULT Constraint on CREATE TABLE In MySQL
We can add a DEFAULT constraint while creating a table in MySQL.
Syntax
CREATE TABLE table_name ( column1 data_type DEFAULT default_value, column2 data_type DEFAULT default_value, ... );
Example
CREATE TABLE customers ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) DEFAULT 'example@domain.com', city VARCHAR(50) DEFAULT 'Unknown' );
MySQL DEFAULT Constraint
The MySQL DEFAULT constraint returns the default value for a table column. The DEFAULT value of a column is a value used in the case, when there is no value specified by the user.
To use this function there should be a DEFAULT value assigned to the column. Otherwise, it will generate an error.