找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
show tables;  查看对应库下面所有的表
新建一个tb_user
create table tb_user
(id int(5)primary key auto_increment,
name varchar(20)not null,
class int(5),
phone bigint(20)default13511111111,
time date);
数据类型:
int:整型,最大存储值为2147483647
bigint:长整型,存储手机号
varchar:存字符串(存的数据需要加单引号或者双引号)
date:时间,如“2022-01-17“
float:浮点型,小数
常见约束:
primary key:主键约束,字段值必须是唯一的,不能重复
auto_increment:自增长约束
not null:非空约束
default:默认值约束
foregin key:外键约束

对表结构的修改:
查看表结构:desc tb_user;
将tb_table表名修改为test:alter table tb_user rename test ;
将id字段修改为sid并去掉自增长约束:alter table test change id sid int(10);
将sid字段修改为id并加上自增长约束:alter table test change id sid int(5) auto_increment;
添加id2这个字段:alter table test add id2 int(10);
同时添加id3,id4这2个字段:alter table test add (id3 int(10),id4 int(5));
把id5这个字段放在表的最前面:alter table test add id5 int(5) frist;
把id6这个字段放到phone字段的后面:alter table test add id6 int(5) after phone;
删除表字段:alter table test drop id5;
删除3个表字段:alter table test drop id6,id4,id3;
删除表:drop table test;

对表数据的操作:
增加:insert into
insert into xiaoli(id,name,class,phone,time) value(1,"xiaozhao",1101,13521111111,"2022-01-17");
insert into xiaoli(name) value("xiaowang"),("xiaozhang");
0不等于null,null指的是空的属性,0是代表一个指
查:
查询所有数据:select * from 表名
查询满足某个条件的所有数据:select * from 表名 where 字段=值
查询单个字段的数据:select 字段 from +表名 where 字段=值
查询多个字段的数据:select 字段1,字段2 from +表名 where 字段=值
查询电话号码不等于135111111111的名字:select  name from test where phone!=135111111111;
“!=” 代表不等于 ,也可以用符号 ”<>“ 表示
查询同时满足多个条件数据:select * from +表名 where 条件1and 条件2
查询满足至少1个条件数据:select * from +表名 where 条件1 or 条件2
查询一个条件范围内的数据:select * from +表名 where 字段 between a and b;
查询字段满足在指定的集合中的数据:select * from +表名 where 字段 in(值1,值2);
查询字段不满足在指定的集合中的数据:select * from +表名 where 字段 not in(值1,值2);
查询字段为空的数据:select * from +表名 where 字段 is null;(查询字段为空的数据不能用字段=null)
查询字段不为空的数据:select * from +表名 where 字段 is not null;
查询某个字段所有包含li的数据:select * from +表名 where 字段 like "%li%";
查询某个字段所有以li开头的数据:select * from +表名 where 字段 like "li%";
查询某个字段所有以li结尾的数据:select * from +表名 where 字段 like "%li";
查询2到5行的数据:select * from test name limit 1,4;
limit m,n(m为下标,n为行数)
从小到大:升序
select * from test order by class asc;
小大到小:降序
select * from test order by class desc;
查询按照class字段分组的数据:select * from test group by class;
通过class字段分组然后求出每组对应的人数:select class count(*) from test group by class;
通过class字段分组然后加条件class不为null:select * from test group by class having class is not null;
注意:
1.group by 分组之后,只能使用having进行条件筛选
2.使用group by 分组之后,仅有分组字段和函数放置到from前面
常用的聚合函数:
count():统计
统计test表中数据条数:select count(*) from test;
sum():求和
求class为1003的数据之和:select sum(id) from test where class=1003;
avg():求平均值
求id的平均值:select avg(id) from test where class=1003;
max():最大值
求id 的最大值:select max(id) from test;
min():最小值
求id 的最小值:select min(id) from test;
distinct():去重
对phone字段去掉重复数据:select distinct(phone) from test;
改:
修改id=2的数据:update test set phone=135111111111 where id=2;
修改id大于等于5的数据:update test set phone=135111111111 where id>=5;
删:
从表中删除id=6的数据:delete from test where id=6;
删除表:delete from test;
删除表数据和表结构:drop table test;
删除表数据:truncate test;








分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册