对表结构的修改:
desc xiaoxu :查看表的结构
alter table xiaoxu +新表名:修改表名
alter table +表名 change +原字段名 +新字段名 数据类型,约束
alter table test change sid id int(5)auto_increment; 将sid字段修改为id 并增加自增长约束
alter table test add id2 int(10);添加ID2这个字段
alter table xuxuxu add id1 int(10)first; 把ID1放在最前面
alter table xuxuxu add id1 int(10)after class; 把id1放在class字段后面
alter table xuxuxu drop id1; 删除, 删除多个用逗号隔开
drop table xuxuxu; 删除xuxuxu表
对表数据的操作
增加:insert into :insert into xuxuxu (name,id,age,phone)values("xp",101,20,13995664813); 对表格进行赋值
查:select * fom 表名 查表里所以数据
select * from xuxuxu where name='xp'; 查询所以(name)名字为xp的数据
查询xuxuxu表中名字为xp的时间(time):select time from xuxuxu where name='xp';
update xuxuxu set age=30 where xupeng=7; 修改某一条
update xuxuxu set age=30 where xupeng>3; 修改ID大于3的年龄
删:
dlete from xuxuxu; 全部删除 dlete from xuxuxu where name='xp' 删除某一项
drop table xuxuxu; 删除表数据和结构 truncate xuxuxu; 删除 xuxuxu的表数据
“!=”:不等于 <> ;不等于
select * from xuxuxu where name !="xp"; 查询xuxuxu表中名字不是小刘的
select * from xuxuxu where name <>"xp";查询xuxuxu表中名字不是小刘的
select * from xuxuxu where name="wangke" and id=105; 查找名字叫wangke 并且id 为105 的
select * from xuxuxu where name="wangke" or id=105; 查找名字叫wangke 或者 id 为105 的
select * from xuxuxu where xupeng between 1 and 7; 查询xuxuxu表中 xupeng 1到7之间 ;between and
select * from xuxuxu where age in(18,20,30); where in 查新年龄为18 20 30 的
select * from xuxuxu where class is null; c查询class为null : is null
select * from xuxuxu where class is not null; 查询class不为null
select * from xuxuxu where name like "%ke"; 查询name字段以ke结尾的
select * from xuxuxu name limit 1,4; 查询2到5行的数据
select * from xuxuxu name limit 0,5;查询前5行
select * from xuxuxu order by phone asc; 从小到大排序升序 :asc
select * from xuxuxu order by phone desc; 从大到小desc :降序
select * from xuxuxu group by phone; 将phone进行分组
select age,count(*) from xuxuxu group by class; 将 class进行分组 计算有多少人
注意:
1:group by 分组之后 只能使用having进行条件筛选
2:使用group by 分组之后 仅能分组字段和函数可以放置到from前面
常用的聚合函数
count():统计
sum() :求和
avg() :求平均值
max() :求最大值
min();求最小值
distinct():去重
|
|