zoukankan      html  css  js  c++  java
  • mysql-8 alter命令

    当我们需要修改数据表名或者修改数据表字段时,就需要用到Mysql alter命令。
    查看表结构:

    -- 以下2个命令是通用的
    show columns from test_alter_tbl;
    desc test_alter_tbl;
    

    1.删除、添加、修改表字段

    create table `test_alter_tbl` (`id` int(3),`name` varchar(20));
    desc test_alter_tbl;
    -- 删除某列
    alter table test_alter_tbl drop name;
    desc test_alter_tbl;
    -- 增加某列,默认追加为最后一列
    alter table test_alter_tbl add name2 varchar(20);
    desc test_alter_tbl;
    -- 在指定位置增加某列
    alter table test_alter_tbl add personDesc varchar(20) after id;
    desc test_alter_tbl;
    -- 将插入的列设置为第一列
    alter table test_alter_tbl add grade int(2) first;
    desc test_alter_tbl;
    

    注意:

    • 1.数据表中只剩1个字段时,不能使用drop来删除字段
    • 2.添加字段时,没有before关键字。只有after、first
    • 3.如果想重置某字段,就要先删除再将列插入到原位置

    2.修改字段类型及名称

    修改字段类型,使用modify 列名 类型;
    修改字段名称,使用change 列名 新列名 类型;

    alter table test_alter_tbl change name2 varchar(10);
    alter table test_alter_tbl change name2 name varchar(20);
    

    3.alter table对null值和默认值的影响

    • 当修改字段时,可以指定是否接收null值,也可以指定默认值。alter table 表 modify 列名 类型 not null default value;
    • 修改字段默认值,列的默认值默认为null: alter table 表 alter 列名 set default value;
    • 删除字段的默认值: alter table 表 alter 列名 drop default;
    • 也可用于删除其他信息,如外键别名: alter table 表 drop foreign key 外键别名;
    • 修改数据表类型: alter table 表 engine = myisam;
    -- 修改字段的类型并设置默认值
    alter table test_alter_tbl modify name varchar(10) not null default "tom";
    desc test_alter_tbl;
    -- 设置字段的默认值
    alter table test_alter_tbl alter name set default "jerry";
    desc test_alter_tbl;
    -- 删除默认值
    alter table test_alter_tbl alter name drop default;
    desc test_alter_tbl; 
    -- 修改数据表类型
    alter table test_alter_tbl engine = myisam;
    -- 使用客户端查看
    show table status like 'test_alter_tbl';
    -- 使用mysql查看
    show table status like 'test_alter_tbl'G
    

    4.修改表名

    修改数据表的名称,可以使用rename语句: alter table 表 rename to 新表名;

    show tables;
    alter table test_alter_tbl rename to test_alter_csj;
    show tables;
    
  • 相关阅读:
    Vue学习笔记-基本语法
    Vue学习笔记-使用ElementUI
    Vue学习笔记-目录结构
    Arcgis api for javascript学习笔记(3.2版本)
    _countof
    自启动在UAC开启状态下解决方案
    windows常见启动项启动顺序
    ssh登录的一个小问题
    centos5.5 环境变量设置
    avalon2 第一个demo
  • 原文地址:https://www.cnblogs.com/csj2018/p/9955405.html
Copyright © 2011-2022 走看看