MySQL添加字段和删除字段

数据库 MySQL
在MySQL数据库中,对字段进行添加或者删除等操作是很常见的操作,下文对添加字段和删除字段的语句写法作了详尽的阐述,供您参考。

MySQL添加字段应该如何实现呢?这是很多刚刚接触MySQL数据库的新人都提到过的问题,下面就为您介绍MySQL添加字段和删除字段的方法,希望对您能有所启迪。

MySQL添加字段:

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

删除字段:

  1. alter table `user_movement_log` drop column Gatewayid  
  2.  

调整字段顺序:

  1. ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID  
  2.  
  3. //主键  
  4.  
  5. alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);//增加一个新列  
  6.  
  7. alter table t2 add d timestamp;  
  8. alter table infos add ex tinyint not null default '0';//删除列  
  9.  
  10. alter table t2 drop column c;//重命名列  
  11.  
  12. alter table t1 change a b integer;  
  13. //改变列的类型  
  14.  
  15. alter table t1 change b b bigint not null;  
  16. alter table infos change list list tinyint not null default '0';  
  17. //重命名表  
  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.  
  25. mysql> alter table tablename add primary key(id);加***限制条件的索引  
  26.  
  27. mysql> alter table tablename add unique emp_name2(cardnumber);删除某个索引  
  28.  
  29. mysql>alter table tablename drop index emp_name;修改表:  
  30.  

增加字段:

  1. mysql> ALTER TABLE table_name ADD field_name field_type;修改原字段名称及类型:  
  2.  
  3. mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;删除字段:  
  4.  
  5. mysql> ALTER TABLE table_name DROP field_name;  

 

 

 

 

【编辑推荐】

MySQL定义外键的方法

详解MySQL中SlowLog的实际配置

带您深入了解MYSQL字符串连接

MySQL字段类型简介

Mysql外键用法分析

责任编辑:段燃 来源: 互联网
相关推荐

2010-11-23 15:27:00

MySQL添加字段

2010-10-13 10:03:08

MySQL修改表结构

2010-09-01 15:59:44

SQL删除字段

2010-09-03 11:16:54

SQL删除

2010-10-08 10:37:00

MYSQL添加新字段

2010-09-02 09:29:02

SQL删除字段

2010-09-01 17:13:07

SQL删除约束

2010-11-22 10:57:22

MySQL字段类型

2010-11-22 11:55:23

MySQL字段

2011-08-25 11:22:39

SQL Server数据库字段说明

2010-09-28 15:07:14

SQL语句

2009-07-22 08:34:47

Scala方法和字段

2010-11-22 12:59:30

MySQL字段

2021-06-06 13:03:53

MySQL普通索引

2010-11-22 12:14:55

MySQL字段

2009-07-21 12:24:01

Scala先决条件添加字段

2010-11-22 10:29:42

MySQL字段自增长

2010-10-08 14:59:00

MySql字段

2010-10-08 09:17:06

mysql修改字段

2010-09-16 15:56:15

SQL Server表
点赞
收藏

51CTO技术栈公众号