zoukankan      html  css  js  c++  java
  • Mysql修改表

    基本介绍:修改表,就是指,当我们的表创建好了,根据业务逻辑的需要, 我们要对表进行修改(比如 修改表名, 增加字段,删除字段,修改字段长度,修改字段类型), 这时我们就要使用修改表技术

    查看表结构:desc 表名;

    案例演示:

    --创建表
    create table staff(
    id int not null default 1 comment "序列号",
    name varchar(5) not null default '保密' comment '姓名',
    sex enum('','','保密') not null default '保密' comment '性别',
    birthday date comment '生日',
    entry_date date comment '入职日期',
    station varchar(20) not null default '保密' comment '职位',
    salary decimal(10,2) unsigned not null default 0 comment '薪资',
    resume text comment '文章'
    )charset=utf8 engine=myisam;
    --插入数据
    insert into `staff` value(6,'老王',1,'1976-9-8','2014-5-18','保洁',3900.00,' ');
    insert into `staff` value(10,'小明',1,'1992-7-16','2016-5-18','保洁',13000.00,' ');
    --查看表
    select * from `staff`;
    --查看表结构
    desc `staff`;
    --在员工表上增加一个户籍地址的列
    alter table `staff` add `address` varchar(20) not null default '' comment '户籍地址';
    --修改岗位(station)列其长度为60默认值为空
    alter table `staff` modify `station` varchar(60) not null default '' comment '职位';
    --删除文章(resume)列
    alter table `staff` drop `resume`;
    --修改表名为worker
    rename table `staff` to `worker`;
    --修改表的字符集为utf8
    alter table `worker` character set utf8;
    --修改字段名name为staff_name长度改为64默认值改为空;
    alter table `worker` change name staff_name varchar(64) not null default '' comment '姓名';

     案例说明:

       (1) 添加列可以在后面给列设定位置(比如说:alter table `staff` add `address` varchar(20) not null default '' comment '户籍地址' after `sex`;就是在sex后面插入列)

      (2) 修改字段名的同时也可以修改字段信息

    注意事项: 

       (1)   如果我们删除了某个字段,那么这个表的该字段内容就删除,所以要小心

      (2)   如果我们把一个字段的长度减小 varchar(64) ==> varchar(32) , 如果你当前这个字段没有任何数据,是可以ok,但是如果已经有数据则看实际情况,就是如果有数据超过 32 了,则会提示错误.

      (3)   对字段类型的修改, 比如 varchar ==> int 那么要看你的varchar 的内容是否可以转成int, 'hello'=>int 就不能成功。

  • 相关阅读:
    redis的间隔性速度慢的问题
    centos增加静态路由
    排查问题的一些基本命令
    laradock
    python之sqlalchemy基本
    ssl证书之certbot
    mysql 多实例
    模块和包
    异步
    WSGI
  • 原文地址:https://www.cnblogs.com/rickyctbur/p/11187547.html
Copyright © 2011-2022 走看看