补上昨天用idea 搭建MSQ数据库

本文通过IDEA创建并管理MySQL数据库,包括创建school数据库,建立classinfo和studentinfo两个表,插入数据,以及执行各种查询、更新、删除操作,如修改学生信息、删除学生记录、按条件筛选数据等。
摘要由CSDN通过智能技术生成

show databases;

create database school;#创建数据库

use school;

create table classinfo( #创建班级信息表
classid int primary key auto_increment,#班级ID 设置主键 自增
classname varchar(32) not null#班级名字
)

create table studentinfo(#创建学生信息表
studentid int primary key , #学生ID
studentname varchar(32) not null,#学生名字
studentsex varchar(8) not null,#学生性别
studentage varchar(32) not null ,#学生年龄
studenphone varchar(64) not null ,#学生电话
studentaddress varchar(128)not null ,#学生地址
studentclass varchar(32) not null references classinfo(classid)#学生所在班级 设置外键 与主键关联
)

#插入数据
insert into studentinfo values(‘0609180849’,‘张三’,‘男’,‘25’,‘18736688748’,‘河南省’,‘1’)
insert into studentinfo values(‘0609180850’,‘李四’,‘女’,‘23’,‘18736688744’,‘浙江省’,‘2’)
insert into studentinfo values(‘0609180851’,‘王二’,‘女’,‘25’,‘18736688743’,‘广东省’,‘1’)
insert into studentinfo values(‘0609180852’,‘小强’,‘男’,‘27’,‘18736688742’,‘广西省’,‘2’)
insert into studentinfo values(‘0609180853’,‘小刚’,‘男’,‘32’,‘18736688742’,‘东北省’,‘3’)
insert into studentinfo values(‘0609180854’,‘小李’,‘男’,‘35’,‘18736688742’,‘海南省’,‘2’)
insert into studentinfo values(‘0609180855’,‘小王八’,‘男’,‘36’,‘18736688742’,‘吉林省’,‘3’)
#查询班级信息
select * from studentinfo

#修改李四同学的年龄 改为40
update studentinfo set studentage=40 where studentid=‘609180850’;
#删除李四同学的信息
delete from studentinfo where studentid=‘609180850’;
#删除编号为3的班级
delete from studentinfo where studentclass=3;
#查询学生地址是河南广东的人
select * from studentinfo where studentaddress in (‘河南省’,‘广东省’);
#查询年龄大于25岁的男生
select * from studentinfo where studentage>25 and studentsex=‘男’;
#查询年龄在25-35岁之间的学员
select * from studentinfo where studentage between 25 and 35;
#查询学生名字为小开头的学员
select * from studentinfo where studentname like ‘小%’;
#查询姓名中带李的学员
select * from studentinfo where studentname like’%李%’;
#根据地址分组查询学员平均年龄
select AVG(studentage) from studentinfo group by studentaddress;
#查询平均年龄小于35并且是男生的学员信息,并对年龄做出降序排列
select from studentinfo where studentage<35 and studentsex=‘男’ order by studentage desc;
#根据地址分组查询学员平均年龄信息,并得出平均年龄大于32岁的相关学员信息
select avg(studentage) from studentinfo group by studentaddress having avg(studentage)>32;
#根据地址分组查询学员平均年龄和人数
select avg(studentage),count(
)from studentinfo group by studentaddress;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值