Adding a new SQL column with a default value

前端 未结 10 653
自闭症患者
自闭症患者 2020-12-02 05:15

I am looking for the syntax to add a column to a MySQL database with a default value of 0

Reference

相关标签:
10条回答
  • 2020-12-02 05:40

    Try this:

    ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;
    

    From the documentation that you linked to:

    ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
       alter_specification [, alter_specification] ...
    
    alter_specification:
        ...
        ADD [COLUMN] (col_name column_definition,...)
        ...
    

    To find the syntax for column_definition search a bit further down the page:

    column_definition clauses use the same syntax for ADD and CHANGE as for CREATE TABLE. See Section 12.1.17, “CREATE TABLE Syntax”.

    And from the linked page:

    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]  
    

    Notice the word DEFAULT there.

    0 讨论(0)
  • 2020-12-02 05:40

    Try This :)

    ALTER TABLE TABLE_NAME ADD COLUMN_NAME INT NOT NULL DEFAULT 0;
    
    0 讨论(0)
  • 2020-12-02 05:48

    You can try this,

    ALTER TABLE table_name ADD column_name INT DEFAULT 0;
    
    0 讨论(0)
  • 2020-12-02 05:48
    ALTER TABLE my_table ADD COLUMN new_field TinyInt(1) DEFAULT 0;
    
    0 讨论(0)
提交回复
热议问题