设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1269|回复: 1
打印 上一主题 下一主题

MySQL添加字段和删除字段 [复制链接]

Rank: 8Rank: 8

跳转到指定楼层
1#
那个谁 发表于 2011-10-22 16:18:08 |只看该作者 |倒序浏览
本帖最后由 那个谁 于 2011-10-22 16:19 编辑
  1. MySQL添加字段应该如何实现呢?这是很多刚刚接触MySQL数据库的新人都提到过的问题,下面就为您介绍MySQL添加字段和删除字段的方法,希望对您能有所启迪。

  2. MySQL添加字段:

  3. alter table `user_movement_log`   Add column GatewayId int not null default 0 AFTER `Regionid` (在哪个字段后面添加)   删除字段:

  4. alter table `user_movement_log` drop column Gatewayid   调整字段顺序:

  5. ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID  
  6. //主键  
  7. alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
  8. //增加一个新列  
  9. alter table t2 add d timestamp;
  10. alter table infos add ex tinyint not null default '0';
  11. //删除列   
  12. alter table t2 drop column c;
  13. //重命名列  
  14. alter table t1 change a b integer;
  15.   //改变列的类型   
  16. alter table t1 change b b bigint not null;  
  17. alter table infos change list list tinyint not null default '0';
  18. //重命名表   
  19. alter table t1 rename t2;
  20. 加索引  
  21. mysql> alter table tablename change depno depno int(5) not null;
  22. mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);
  23.   mysql> alter table tablename add index emp_name (name);加主关键字的索引   
  24. mysql> alter table tablename add primary key(id);
  25. 加唯一限制条件的索引   
  26. mysql> alter table tablename add unique emp_name2(cardnumber);
  27. 删除某个索引   
  28. mysql>alter table tablename drop index emp_name;
  29. 修改表:   
  30. 增加字段:
  31. mysql> ALTER TABLE table_name ADD field_name field_type;
  32. 修改原字段名称及类型:   mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
  33. 删除字段:   mysql> ALTER TABLE table_name DROP field_name;
复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

2#
混混@普宁.中国 实名认证  发表于 2011-10-24 08:18:41 |只看该作者
杰管 v5

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-4-29 06:38 , Processed in 1.111707 second(s), 11 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部