zoukankan      html  css  js  c++  java
  • mysql修改表结构

    自我总结,有什么需要纠正补充的地方,请指正,谢谢!

    目的:当项目中需要修改表结构,且需要公布给其他的同事,高效率的方法就是将修改表结构的语句发送给其他同事。

     对表字段的操作:add,drop,modify | change .

    add:
    
    功能1:新加字段(默认在所有字段后面增加新字段)
    
    语法1:alter table +表名+ add +新字段 +新字段类型 +新字段是否可以为空 +默认值
    
               alter table student add classname varchar(30) not null default '';
    
    功能2:将新加字段放在所有字段的最前面
    
    语法:alter table +表名+add +新字段 +新字段类型 +新字段是否为空 +默认值 +first
    
             alter table student add classname varchar(30) not null default '' first;
    
    功能3:将现价再断放在某个字段之后
    
    语法:alter table +表名 +add +新字段 +新字段类型 +新字段是否为空 + 默认值 + after +已存在的字段名称
    
       alter table student add classno int  not null  after age;
    
     
    
    drop:
    
    语法:alter table +表名 +drop +已存在的字段名称
    
         alter table student  drop classno;
    
     
    
    修改表字段属性:
    
    功能1(modify):修改字段的类型或默认值
    
    语法:alter table +表名 +modify +需要修改的字段 +修改字段的类型 +默认值
    
         alter table student modify  name varchar(30) not null default '';
    
    功能2(change):修改字段的名称
    
    语法:alter table +表名 +change +旧字段 +新字段 +新类型  +是否为空 +默认值
    
       alter table student change name stuname varchar(6) not null default '0';

     

     基本须知:

            1. 修改表结构命令用 [ alter ]语句标识,例如 [ alter table student ]
    
        2. 之后加上对字段的增加,修改,删除命令标识分别为 add ,modify , drop 。
    
        3. 查看表结构语句 例如[ desc student; ]
    
        4. 查看建表语句 例如[  show create table student; ]
    
        5. 设置default值不是必须的,若这么做,mysql默认值为null
    
        6. not null不是必须的,若这么做,mysql默认值为 yes
    
        7. 当字段类型为vharchar类型的时,deafult '0'和 default 0 目的是一样的    

    我也是参考了其他园友博客,

     原文出处:http://www.cnblogs.com/qintangtao/archive/2012/11/17/2775209.html

  • 相关阅读:
    Mysql之存储过程与存储函数
    mysql-bin日志自动清理及手动删除
    mysql下面的binlog
    mysql下的数据备份与恢复
    查询mysql数据库中各个表所占空间大小以及索引大小
    mysql执行sql语句报错this is incompatible with sql_mode=only_full_group_by
    docker WARNING: IPv4 forwarding is disabled. 解决方法
    Linux平台修改环境变量的方式
    PuTsangTo
    (一) 从Angular1到Angular2的杂谈
  • 原文地址:https://www.cnblogs.com/xxyfhjl/p/4119143.html
Copyright © 2011-2022 走看看