How can you add a field in mysql and set a default value?

To add a new column with a default value in MySQL, you can use the ALTER TABLE statement. Here is the syntax for adding a column with a default value:

ALTER TABLE table_name
ADD COLUMN column_name data_type DEFAULT default_value;

Please replace “table_name” with the name of the table to add the field, replace “column_name” with the name of the field to add, replace “data_type” with the data type of the field, and replace “default_value” with the default value of the field.

For instance, to add a field called “email” with a default value of “example@example.com” to a table named “customers”, you can use the following statement:

ALTER TABLE customers
ADD COLUMN email VARCHAR(100) DEFAULT 'example@example.com';

Before executing the ALTER TABLE statement, make sure there are no duplicate column names in the table to avoid conflicts.

bannerAds