Java学习day19

-- 数据库高级操作

use j240101;

drop table if exists day02;

create table if not exists day02(
    id int primary key auto_increment,
    name varchar(20) not null,
    age int not null,
    sex varchar(10) not null,
    birth date not null,
    address varchar(200) not null
);


-- 数据插入
-- 同时插入多条数据
insert into day02(name,age,sex,birth,address) values('张三',18,'男','1999-01-01','北京'),('李四',19,'女','1999-01-01','北京'),('王五',20,'男','1999-01-01','北京');
select * from day02;

-- 解决主键冲突
-- 1.使用duplicate key解决 格式: insert into table_name (column1,column2,...) values (value1,value2,...) on duplicate key update column1=value1;
insert into day02(id,name,age,sex,birth,address) values(2,'赵六',21,'女','1999-01-01','北京') on duplicate key update name='赵六';
-- 该条数据未插入,只是主键冲突的行的name字段被更新为'赵六'
-- 2.使用replace into解决 格式: replace into table_name (column1,column2,...) values (value1,value2,...);
replace into day02(id,name,age,sex,birth,address) values(3,'赵六',21,'女','1999-01-01','北京');
-- 该条数据直接替换掉主键为3的行
select * from day02;

-- 蠕虫复制
-- 一般用于快速复制表中的数据,用以测试
insert into day02(name,age,sex,birth,address) select name,age,sex,birth,address from day02;
select * from day02;


-- 数据修改
-- 使用update对表进行修改;用set来指定修改的字段和修改的值;用where来指定修改的行。若没有指定where条件,则所有行都会被修改。
update day02 set age=22 where id=1;
select * from day02;


-- 数据删除
-- 使用delete删除表中的数据;用where来指定删除的行。若没有指定where条件,则所有行都会被删除。
delete from day02 where id=2;
select * from day02;


-- 数据查询
-- 使用select查询表中的数据;用from来指定查询的表;用where来指定查询的行;用group by来指定分组的字段;用order by来指定排序的字段;用limit来指定返回的行数。
-- 查询表中的所有数据
select * from day02;

-- 查询表中的指定字段
select name,age,sex,birth from day02;

-- 查询表中的指定行
select * from day02 where id=1;

-- 按照指定字段分组查询表中的数据
select sex from day02 group by sex;

-- 多分组查询
select sex,birth from day02 group by sex,birth;

-- 聚合函数配合分组查询
select max(age),sex,count(*) from day02 group by sex;

-- having配合分组查询
select sex,count(*) from day02 group by sex having count(*)>1;

-- 按照指定字段排序查询表中的数据 asc升序,desc降序
select name,age,sex,birth from day02 order by age;

-- 分页查询 格式:limit offset_num,limit_num;就是从第offset_num行开始,返回limit_num行。
select name,age,sex,birth from day02 limit 0,2;
select name,age,sex,birth from day02 limit 2,2;

use j240101;
drop table if exists student;

create table if not exists student(
    id int primary key auto_increment,
    name varchar(20) not null,
    age int not null,
    height int not null,
    classid int not null
);
insert into student(name,age,height,classid) values('张三',18,170,1),('李四',19,180,1),('王五',20,190,1),('赵六',21,180,2),('jack',21,190,2),('rose',21,185,2),('lilei',21,157,3),('小明',21,180,3);
select classid,max(height) from student group by classid;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值