码迷,mamicode.com
首页 > 数据库 > 详细

mysql修改表结构

时间:2014-11-25 10:27:38      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   ar   color   sp   strong   on   

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

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

 

 对表字段的操作: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 room_id varchar(6) not null default ‘0‘;

 

 

 基本须知:

        1. 修改表结构命令用 [ alter ]语句标识,例如 [ alter table student ]

    2. 之后加上对字段的增加,修改,删除命令标识分别为 add ,modify , drop 。

    3. 查看表结构语句 例如[ desc student; ]

    4. 查看建表语句 例如[  show create table student; ]

    5. 设置默认值不是必须的,若这么做,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修改表结构

标签:des   style   blog   http   ar   color   sp   strong   on   

原文地址:http://www.cnblogs.com/xxyfhjl/p/4119143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!