Home >Database >Mysql Tutorial >How to Add a New SQL Column with a Default Value of 0?

How to Add a New SQL Column with a Default Value of 0?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-17 01:15:25866browse

How to Add a New SQL Column with a Default Value of 0?

Adding a New SQL Column with a Default Value of 0

Adding a new column to a MySQL database with a default value of 0 is a simple task. Here's the syntax:

ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;

This example adds a column named "foo" of type INT to the table "table1" and sets its default value to 0.

Reference Breakdown:

The syntax for adding a new column is:

ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
   alter_specification [, alter_specification] ...

Where "alter_specification" can be:

ADD [COLUMN] (col_name column_definition,...)

The column_definition syntax is similar to that of CREATE TABLE:

column_definition:  
   data_type [NOT NULL | NULL] [DEFAULT default_value]
   [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]  
   [COMMENT 'string']  
   [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]  
   [STORAGE {DISK|MEMORY|DEFAULT}]  
   [reference_definition]  

In this case, we use "INT" as the data_type and "DEFAULT 0" to specify the default value.

The above is the detailed content of How to Add a New SQL Column with a Default Value of 0?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn