在mysql中删除表中字段_MySQL中的表中增加删除字段

1.增加一个字段

alter table user表 add COLUMN new1字段 VARCHAR(20)   NOT NULL DEFAULT 0; //增加一个字段,VARCHERA 20 , 不为空,默认值是 0

//增加多个

alter table user表

add COLUMN new1字段 VARCHAR(20)   NOT NULL DEFAULT 0,

add COLUMN new2字段 VARCHAR(20)   NOT NULL DEFAULT 0;

alter table user add COLUMN new2 VARCHAR(20) NOT NULL;  //增加一个字段,默认不能为空  www.2cto.com

2.删除一个字段

alter table user DROP COLUMN new2;   //删除一个字段

3.修改一个字段

alter table user MODIFY new1 VARCHAR(10);  //修改一个字段的类型

alter table user CHANGE new1 new4 int;  //修改一个字段的名称,此时一定要重新指定该字段的类型

mysql> create table id_name(id int,name varchar(20));

Query OK, 0 rows affected (0.13 sec)

mysql> alter table id_name add age int,add address varchar(11);

Query OK, 0 rows affected (0.13 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc id_name;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| name | varchar(20) | YES | | NULL | |

| age | int(11) | YES | | NULL | |

| address | varchar(11) | YES | | NULL | |

+---------+-------------+------+-----+---------+-------+

rows in set (0.00 sec)

2.删除两个字段

mysql> alter table id_name drop column age,drop column address;

Query OK, 0 rows affected (0.14 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc id_name;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(11) | YES | | NULL | |

| name | varchar(20) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

rows in set (0.00 sec)

3.插入

mysql> insert into id_name values (1,'qustdjx');

Query OK, 1 row affected (0.00 sec)

4.查询看一下

mysql> alter table id_name add age int,add address varchar(11);

Query OK, 1 row affected (0.07 sec)

Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from id_name;

+------+---------+------+---------+

| id | name | age | address |

+------+---------+------+---------+

| 1 | qustdjx | NULL | NULL |

+------+---------+------+---------+

row in set (0.00 sec)

5.新增字段并插入

mysql> insert into id_name values(2,'qust',14,'山东');

Query OK, 1 row affected (0.00 sec)

mysql> select * from id_name;

+------+---------+------+---------+

| id | name | age | address |

+------+---------+------+---------+

| 1 | qustdjx | NULL | NULL |

| 2 | qust | 14 | 山东 |

+------+---------+------+---------+

rows in set (0.00 sec)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值