mysql语句----详细内容

***************************
1、写出创建上面这两张表的sql
use  Student_information;  
create table Students (              #创建Students表
Id int(10) not null unique PRIMARY key auto_increment ,
Name VARCHAR(20) not null ,
Sex VARCHAR(4),
age int(10) not null,
phone int(20) UNIQUE,
class VARCHAR(20) not null,
Addr VARCHAR(50),
cn VARCHAR(50) DEFAULT 'china'
);


use Student_information;
create table Score(             #创建Score表
Id int(10) not null PRIMARY KEY auto_increment,
Stu_id int(10) not null,
C_name varchar(20),
Grade int(10)
);


***************************         ???????????????
2、使用while循环和repeat循环写各两个存储过程,传入一个行数,
控制插入多少条数据,往第一题创建的两个表中各插入500条数据,
参考ppt上的存储过程


***************************
3、写insert语句插入右边两个表的数据
#插入Students表数据
insert into Students(Id,Name,Sex,age,class,Addr) values
(801,'刘海洋','男','21','乔巴','北京市海淀区'),
(802,'周飞','男','18','乔巴','北京市昌平区'),
(803,'味全','男','26','路飞','湖南省永州市'),
(804,'孙杨','女','21','乔巴','辽宁省阜新市'),
(805,'李佳','女','22','超人','福建省厦门市'),
(806,'保总','女','30','乔巴','湖南省衡阳市'),
(1001,'徐振永','男','21','索隆','辽宁省阜新市'),
(1002,'李卫强','男','18','索隆','福建省厦门市'),
(1003,'狄枫','男','26','蜘蛛侠','湖南省衡阳市'),
(1004,'女屌丝','女','21','蜘蛛侠','北京市海淀区'),
(1005,'郁燕','女','22','索隆','北京市昌平区'),
(1006,'裴颖菲','女','30','索隆','辽宁省阜新市'),
(1007,'戴小龙','男','50','索隆','辽宁省阜新市');


#插入Score表数据
INSERT INTO Score 
VALUES
(1, 801, '计算机', 98),
(2, 801, '英语', 80),
(3, 802, '计算机', 65),
(4, 802, '中文', 88),
(5, 803, '中文', 95),
(6, 804, '计算机', 70),
(7, 804, '英语', 92),
(8, 805, '英语', 94),
(9, 806, '计算机', 57),
(10, 806, '英语', 45),
(11, 1001, '计算机', 98),
(12, 1007, '英语', 80),
(13, 1002, '计算机', 65),
(14, 1002, '中文', 88),
(15, 1003, '中文', 95),
(16, 1004, '计算机', 70),
(17, 1004, '英语', 92),
(18, 1005, '英语', 94),
(19, 1006, '计算机', 57),
(20, 1006, '英语', 45);


***************************
4、查询students表的所有记录
SELECT * FROM Students;
SELECT * FROM Score;


***************************
5、查询students表的第2条到4条记录
SELECT * FROM Students limit 1,3;


***************************
6、从students表查询所有学生的学号(id)、姓名(name)和班级(class)的信息
select Id ,Name,class from Students;


***************************
7、从students表中查询乔巴和索隆的学生的信息
use Student_information;
select * from Students where class='乔巴' or class='索隆';
select * from Students where class in('乔巴', '索隆');


***************************
8、从students表中查询年龄18~25岁的学生信息
select * from Students where age BETWEEN 18 and 25;


***************************
9、从students表中查询每个班有多少人
select class,count(*) as 人数 from Students group BY class;


***************************
10、从score表中查询每个科目的最高分
select C_name, max(Grade) as 最高分 from Score group BY C_name;


***************************
11、查询女屌丝的考试科目(c_name)和考试成绩(grade)
select Name, C_name, Grade from Score , Students where Students.Id=Score.Stu_id and Name='女屌丝';




***************************
12、用4种多表连接的方式查询所有学生的信息和考试信息(左连接、右连接、内连接、=号连接)
#=号连接
select * from Score , Students where Students.Id=Score.Stu_id ;
#内连接
select * from Score a INNER JOIN Students b ON b.Id=a.Stu_id ;
#左连接
select * from Score a left JOIN Students b ON b.Id=a.Stu_id ;
#右连接
select * from Score a RIGHT JOIN Students b ON b.Id=a.Stu_id ;




***************************
13、计算每个学生的总成绩
select b.Name,sum(Grade) as 总成绩 from Score a, Students b where b.Id=a.Stu_id GROUP BY b.Name;




***************************
14、计算每个考试科目的平均成绩
select C_name, avg(Grade) as 平均成绩 from Score group BY C_name;




***************************
15、查询计算机成绩低于95的学生信息
select *  from Score a, Students b where a.Grade<95 and a.C_name='计算机' and b.Id=a.Stu_id ;




***************************
16、查询同时参加计算机和英语考试的学生的信息    @@@@@@@@@@@@@@
select * from Score a , Students c where  a.C_name='计算机'  and c.Id=a.Stu_id and Stu_id in(select Stu_id from Score b where  b.C_name='英语' ) ;


SELECT * from Students a, (select Stu_id from Score  where C_name='计算机' ) b,(select Stu_id from Score where  C_name='英语' ) c WHERE a.Id=b.Stu_id and b.Stu_id=c.Stu_id




***************************
17、将计算机考试成绩按从高到低进行排序
select * from Score a WHERE a.C_name='计算机'  ORDER BY Grade DESC;


***************************
18、从student表和score表中查询出学生的学号,然后合并查询结果   @@@@@@@@@@@@@@@@      
select Stu_id from Score union select Id from Students


***************************
19、查询索隆班姓李的男同学的成绩和学生信息
select * from Score , Students where Students.Id=Score.Stu_id and class='索隆' and Name LIKE '李%'and Sex='男';


***************************
20、查询都是湖南的学生的姓名、年龄、班级和考试科目及成绩
select Name,age,class,C_name,Grade from Score , Students where Students.Id=Score.Stu_id and Addr LIKE '湖南%';


***************************
21、把总成绩小于100的学生名称修改为天才
SELECT  Stu_id from Score  GROUP BY Stu_id HAVING Sum(Grade)<100
SELECT * from Students;
update Students set Name='天才' WHERE Id in(SELECT  Stu_id from Score  GROUP BY Stu_id HAVING Sum(Grade)<100)


***************************
22、查询只学过一门课的学生信息
SELECT * from Students where Id in(SELECT Stu_id from Score GROUP BY Stu_id HAVING COUNT(*)=1)


***************************
23、查出有多少个年龄一样的学生     ??????????????
create VIEW aa as  SELECT  age,COUNT(*) as gs  from Students GROUP BY age HAVING gs>1
select sum(gs) as 个数 from aa
SELECT * from aa
drop view aa


***************************
24、查询出每门课程低于平均成绩的学生姓名、课程名称、分数   ??????????????????????
create view cc as SELECT C_name,AVG(Grade) as 平均分 from Score GROUP BY C_Name
create view dd as (SELECT Stu_id,a.C_name,Grade, 平均分 from Score a,cc b where a.C_name=b.C_name and a.Grade-b.平均分<0)
SELECT * from c
SELECT Name,C_name,Grade from Students c,dd d WHERE c.Id=d.Stu_id 


***************************
25、查询出每个人成绩最高的课程名称及分数
SELECT Stu_id ,C_name,Max(Grade) from Score GROUP BY Stu_id 




***************************
26、索引是什么,如何创建索引,为什么要使用索引?写自己的理解
    




***************************
26、创建一个视图,要求显示总成绩大于160的学生的班级、课程名称、分数、学号、学生姓名、学生性别
SELECT class,C_name,Grade,a.Id,Name,Sex ,SUM(Grade)from Students a,Score b where a.Id=b.Stu_id group by Stu_id HAVING SUM(Grade)>160
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值