Adding a New SQL Column with a Default Value
In MySQL, adding a new column to a table with a default value requires the use of the ALTER TABLE statement. The syntax for this statement allows you to specify a default value during column creation.
ALTER TABLE Syntax
The general syntax for adding a new column with a default value is as follows:
ALTER TABLE table_name ADD COLUMN column_name data_type DEFAULT default_value;
In this syntax, table_name represents the table you wish to modify, column_name is the name of the new column, data_type defines the type of data that the column will hold, and default_value specifies the default value for the column.
Example
To add a new column named foo to the table table1 with a default value of 0, you can use the following statement:
ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;
Explanation
Documentation Reference
The syntax provided here is based on the MySQL documentation, which states:
ALTER TABLE tbl_name ADD COLUMN (col_name column_definition,...)
Where column_definition can include a DEFAULT clause:
column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value] ...
The above is the detailed content of How Do I Add a New Column with a Default Value in MySQL?. For more information, please follow other related articles on the PHP Chinese website!