【MYSQL】字段增删改

  • 增加字段

    • 在表的最后加新列
      alter table 表名 add 列名称 列类型 列参数
      例:增加新列 id
      alter table tbname add id int unsigned not null default 0;

    • 在指定列后面加新列
      alter table 表名 add 列名称 列类型 列参数 after 新列
      例:在列id后面增加新列gender
      alter table tbname add gender char(1) not null default after id;

    • 在表的最前加新列
      alter table 表名 add 列名称 列类型 列参数 first
      例:增加新列 pid
      alter table tbname add pid int not null default 0 first ;

  • 删除列
    alter table 表名 drop 列名称

  • 修改列

    • 修改列名
      alter table 表名 CHANGE 旧列名 新列名 新列类型 新列参数
      例:修改列username的名称
      alter table tbname change username newname char(20) not null default ”;

    • 修改列类型/参数
      alter table 表名 modify 列名 列新类型 列新参数
      例:修改列gender的类型char(1)
      alter table tbname modify gender char(4) not null default ”;

你可能感兴趣的:(MySQL)