Adding a new SQL column with a default value

前端 未结 10 652
自闭症患者
自闭症患者 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:27

    table users (user_id int unsigned PK, username varchar(32))

    alter table users add column verified tinyint unsigned default 0
    
    0 讨论(0)
  • 2020-12-02 05:30

    This will work for ENUM type as default value

    ALTER TABLE engagete_st.holidays add column `STATUS` ENUM('A', 'D') default 'A' AFTER `H_TYPE`;
    
    0 讨论(0)
  • 2020-12-02 05:31

    If you are learning it's helpful to use a GUI like SQLyog, make the changes using the program and then see the History tab for the DDL statements that made those changes.

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

    Another useful keyword is FIRST and AFTER if you want to add it in a specific spot in your table.

    ALTER TABLE `table1` ADD COLUMN `foo` AFTER `bar` INT DEFAULT 0;
    
    0 讨论(0)
  • 2020-12-02 05:35

    Simply add default 0 at the end of your ALTER TABLE <table> ADD COLUMN <column> <type> statement

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

    Like this?

    ALTER TABLE `tablename` ADD `new_col_name` INT NOT NULL DEFAULT 0;
    
    0 讨论(0)
提交回复
热议问题