mysql-sql语句修改表结构

注意:[]内的为可选内容

  1. 修改表中字段长度,字符集,是否允许为空和添加注释(某些字段无需设置长度 如datetime等)(字符集一定要放在注释前面,并且注释内容不能单独设定,否则可能会影响字符集的设定,同样,再次设置字符集也会影响注释)

    alter table 表名 modify column 字段名称 字段类型 [(字段长度)] [character set 字段字符集 collate 排序规则] [not nullnull] [comment '注释内容'];
  2. 删除字段默认值
    alter table 表名 alter column 字段名 drop default;

  3. 添加字段默认值(注意默认值 char或者varchar需要加''包裹

    1. 给日期(datetime)类型添加默认值
      alter table 表名 modify column 字段名 datetime 日期默认值
    2. 给一般字段添加默认值
      alter table 表名 alter column 字段名 set default 字段默认值
  4. 取消自动增长
    alter table 表名 modify column 字段名 字段类型[(字段长度)]

  5. 设置自动增长
    alter table 表名 modify column 字段名 字段类型 auto_increment unique;

  6. 创建表语句(按顺序,字段名称,字段类型,字段长度,设置字段字符集,是否允许为空,字段默认值,注释,是否自动增长)

    create table 表名 (
    字段名 字段类型[(字段长度)]
    [character set 字段字符集 collate 排序规则]
    [null 或者 not null]
    [default 默认值]
    [comment 注释内容]
    [auto_increment unique],
    字段二,
    字段三
    );
  7. 删除表
    DROP TABLE IF EXISTS 表名

  8. 删除表中字段
    alter table 表名 drop column 字段名

  9. 删除索引
    drop index 索引名 on 表名

  10. 增加字段 同时添加字段属性,包括(按顺序)字符集,是否为空,注释内容,是否自增。

    alter table 表名 add column 字段名 字段类型 [(字段长度)] [unsigned] [character set 字符集名称 collate 字符集排序规则] [not null 或者 null] [comment 注释内容] [auto_increment unique];
  11. 创建普通或unique索引 若不指定索引名称,MySQL会自动添加默认为字段名称,同时可以为一个或多个字段添加索引

    alter table 表名 add [unique] index [索引名称] [字段一、字段二、字段三...]
  12. 添加主键索引
    alter table 表名 add primary key 字段名;

  13. 改变主键索引
    alter table 表名 drop primary key, add primary key 字段名

  14. 为字段设置字符集
    alter table 表名 modify 字段名 字段类型 [(字段长度)] character set 字符集名称 collate 字符集排序规则

posted @   生活的样子就该是那样  阅读(346)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示