数据库(mydb)

drop database if exists mydb;
create database mydb;#创建数据库
use mydb;#使用数据库
#创建班级表
create table clazz
(
cid int primary key auto_increment,#主键,自增,班级id
cname varchar(20) not null#班级名称
);
#创建学生表
create table student 
(
sid int primary key auto_increment,#学生id,主键,自增
sname varchar(20) not null,#学生名称
ssex varchar(20) not null,#学生性别,非空
sage int not null,#学生年龄,非空
stuid int references clazz(cid)#外键
);
#添加数据
insert into clazz values 
(null,"j211001"),
(null,"j211002");
select * from clazz;

insert into student values 
(null,"张三","男",18,1),
(null,"李四","男",16,1),
(null,"张津龙","女",17,2),
(null,"王五","男",20,1);
select * from student;
#左外连接  内连接
select * from clazz c left join student s on c.cid=s.stuid; 
select * from clazz c inner join student s on c.cid=s.stuid; 
#查询所有年龄大于平均年龄的学生信息
select * from student where sage>(select AVG(sage) from student);
#分页
select * from student where sage>(select AVG(sage) from student) limit 1,1;
#修改
update student set sname="张三1" where sname="张三";
#删除
delete from student where sname="张三1";
#模糊
select * from student where sname like "张%";
select * from student where sname like "%龙";
#最大值 最小值  平均值
select sname,max(sage) from student;
select sname,min(sage) from student;
select avg(sage) from student;

select * from student where sage between 15 and 20;

select count(*) from student where ssex="男";

select * from student  order by sage desc;

select stuid,count(*) from student group by stuid;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值