Mysql学习笔记(2)-管理表数据
2017-04-23 本文已影响120人
Mr_欢先生
5.修改表字段类型:
将name 的类型改为varchar(50);
alter table eurasis modify column name varchar(50);
![](https://img.haomeiwen.com/i1616232/eb8908b9c6ca6464.png)
6.修改字段名称
将name 字段名称修改为sex
alter table eurasis change column name sex varchar(20);
![](https://img.haomeiwen.com/i1616232/c5946fff36888a29.png)
7.修改表名称
alter table eurasis rename to student;
![](https://img.haomeiwen.com/i1616232/fb439d52a6c46dd7.png)
四.管理表数据
1.增加数据
插入所有字段
insert into student values(1,"mahuan",20);
插入部分字段
insert into student (id,age) values(2,22);
![](https://img.haomeiwen.com/i1616232/49f374869f955981.png)
2.查询数据
select * from student;
![](https://img.haomeiwen.com/i1616232/040f2f03e2db1612.png)
3.修改数据
全部修改
update student set age=44;
选择性修改
![](https://img.haomeiwen.com/i1616232/9f4d9a2cf334082f.png)
update student set age=21,sex="xiaoming" where id=1;
![](https://img.haomeiwen.com/i1616232/486e63aa18cd3746.png)
4.删除数据
delete from 和truncate table 的区别
delete from:
1.可以带条件
2.只能删除表的数据,不能删除表的约束
3.删除事物可以回滚(事物)
例如:有自增字段时,删除后序列号会继续接着上次增加
truncate table
1.不可带条件
2.即能删除表的数据,也能删除表的约束
3.删除事物不可以回滚(事物)
例如:有自增字段时,删除后序列号会从1开始
不带条件的删除
delete from huan;
带条件的删除
delete from huan where name=huan;
![](https://img.haomeiwen.com/i1616232/4ddc7939a68f6204.png)
小练习:
![](https://img.haomeiwen.com/i1616232/28e7d4c4584f4286.png)
代码实现:
create table employee( id int, name varchar(20), gender varchar(10),
birthday data, email varchar(10), remark varchar(50) );
alter table employee add column age int;
alter table employee modify column email varchar(50);
alter table employee drop column remark;
alter table employee change column name username varchar(20);
上接文章:Mysql学习笔记(1)-管理数据库和管理表