Adding a New Column with a Default Value in SQL
When expanding a database schema, it's often necessary to add new columns. In MySQL, this can be achieved using the ALTER TABLE statement. One essential consideration is assigning an appropriate default value to the new column to maintain data integrity.
Syntax:
To add a column with a default value of 0 to a MySQL table named 'table1', use the following syntax:
ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;
Explanation:
Reference:
The MySQL documentation provides extensive information on modifying tables using ALTER TABLE, including the addition of new columns with default values. Refer to the following sections:
By understanding the syntax and referencing the official documentation, you can effortlessly add new columns with default values to your MySQL databases, ensuring data integrity and simplifying data manipulation processes.
The above is the detailed content of How to Add a New Column with a Default Value in MySQL?. For more information, please follow other related articles on the PHP Chinese website!