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
Release: 2024-12-17 01:15:25
Original
780 people have browsed it

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;
Copy after login

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] ...
Copy after login

Where "alter_specification" can be:

ADD [COLUMN] (col_name column_definition,...)
Copy after login

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]  
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template