找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
在数据库下建表

show tables; ==>查看对应库下面的所有表

新建一个ztt表
create tableztt
(id int(5)primary key auto_increment,
name varchar(20)not null,
class int(5),
phone bigint(20)default"13511111111",
time date);

数据类型:
int  ==>整型,最大存储为2147483647
bigint ==>长整型,存储手机号
varchar ==>存字符串(存的数据需要加单引号或双引号)
date ==>时间 如“2022-01-17”
float== 浮点型,小数

常见的约束有哪些?
primary key ==》主键约束,字段值必须是唯一的不能重复
auto_increment ==》自增长约束
not null ==>非空约束
default ==>默认值约束
foregin key ==》外键约束

【对表结构的修改】
descztt; ==》查看表结构
alter table ztt rename test;==>将tb_user表名修改为test;
alter table test change id sid int(10);==>将id字段修改为sid并且去掉自增长约束;
alter table test change sid id int(5)auto_increment;
==》将sid字段修改为id并且增加自增长约束;
alter table test add ztt1 int(10); ==》添加id2这个字段
alter table test add (ztt1 int(5),id4 int(5));==》同时添加2个字段
alter table test add id5 int(5)first;==》把id5这个字段放到最前面
alter table test add id6 int(6)after phone;==》把id6这个字段放到phone字段后;
alter table test drop id5;==>删除id5字段
alter table test drop id6,drop id2,drop id3,drop id4;==》同时删除多个字段
drop table test;==》删除test表

【对表数据的操作】
增加==》insert into
insert into test(id,name,class,phone,time)values(1,"xiaoliu",1003,1353333333,"2022-01-17");
insert into test(name)values("xiaoli");
insert into test(name)values("xiaozhang"),("xiaowang"),("xiaozhao");
0不等于null,null指的是空的属性  0是代表一个值


select * from 表名;
select * from test; ==》查询所有数据
select * from test where name='ztt'; ==》查询name为ztt的所有数据
select id  from test where name='ztt'; ==》查询单个字段
select id,phone  from test where name='xiaoli';==》查询多个字段用and链接



update test set phone=13433333333 where id=2;修改id等于2的数据
update test set phone=13433333333 where id>=5;修改id大于等于5的数据


delete from test where id=6; 删除id等于6的数据
delete from test;删除全表数据
drop table test;  删除表数据和表结构
truncate test;删除表数据

单表查询
select * from test; ==》查询全表数据
select name from test; ==》查询单个字段值
select id,name from test; ==》查询多个字段值
select * from test where name="xiaoli"; ==》查询name等于xiaoli的所有数据
select * from test where phone != '13433333333'; ==》查询phone字段不等于13433333333的数据
select * from test where phone <> '13433333333'; ==》查询phone字段不等于13433333333的数据
select * from test where class =1004 and phone=13511111111;==》
查询class为1004并且phone为1351111111的所有数据
select * from test where class =1004 or phone=13511111111; ==》
查询class为1004 或者phone为1351111111的所有数据
select * from test where id>=2 and id<=5; ==>查询id大于等于2和小于等于5的数据
select * from test where id between 2 and 5;  ==>查询id大于等于2和小于等于5的数据
select * from test where class in(1005,1004);==> 查询class为1004或1005       
select * from test where class not in(1005,1004);==》class不在1004或1005里面的数据
select * from test where time is null; ==》查询time为空的数据,不能直接用字段=null
select * from test where time is not null; ==>查询time不为空的数据
select * from test where name like "%li%"; 查询name字段所有包含li的数据
select * from test where name like "lao%";查询name字段以lao开头的所有数据
select * from test where name like "%ang";查询name字段以ang结尾的所有数据
*select * from test name limit 1,4; ==》查询2到5行的数据
select * from test name limit 0,5;==>查询前5行数据
limit m,n(m为下标,n为查询行数)

排序
从小到大--升序 asc
select * from test order by class asc;

从大到小--降序 desc
select * from test order by class desc;

分组
select * from test group by class;==》通过class字段进行分组
select class,count(*) from test group by class; ==》通过class字段
进行分组然后求出每组对应人数
select class,count(*) from test group by class having class is not null;
通过class字段进行分组然后加条件calss不为null
注:
1.group by分组之后,只能使用having进行条件筛选
2.使用group by分组后,仅有分组字段和函数放置到from 前面

常用的聚合函数
():里面加类型

count()---统计
sum()--求和(
avg()--求平均值
max()--最大值
min()--最小值       
distinct()--去重

select count(*) from test;==》统计test表中数据条数
select sum(id) from test where class=1003; ==》求class为1003的数据id之和
select avg(id) from test where class=1003; ==》求id的平均值
select max(id) from test;==》求id最大值
select min(id) from test;==》求id最小值
select distinct(phone) from test; ==》对phone字段去掉重复数据





分享至 : QQ空间
收藏

0 个回复

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