标签:
1.查找全部学生的信息
[SQL]select * from student
受影响的行: 0
时间: 0.000s
2.查出成绩及格的所有人
[SQL]select * from student where StudentNo in (select StudentNo from result where StudentResult > 60)
受影响的行: 0
时间: 0.001s
3.查出所有指定年级的同学
[SQL]select * from student where GradeId in (1,2)
受影响的行: 0
时间: 0.001s
4.查出“赵”姓同学
[SQL]select * from student where StudentName like ‘赵%‘
受影响的行: 0
时间: 0.000s
5.学生按学号降序查询出所有学生
[SQL]select * from student ORDER BY StudentNo desc
受影响的行: 0
时间: 0.001s
6.学生按住址升/降序排序查询
[SQL]select * from student ORDER BY Address asc
受影响的行: 0
时间: 0.001s
[SQL]select * from student ORDER BY Address desc
受影响的行: 0
时间: 0.001s
7.查询输出4条学生信息
[SQL]select * from student limit 4
受影响的行: 0
时间: 0.001s
8.查询输出第2条至第7条学生信息
[SQL]select * from student limit 1,6
受影响的行: 0
时间: 0.001s
9.求出全班平均分数
[SQL]select avg(StudentResult) from result
受影响的行: 0
时间: 0.001s
10.全校有多少学生
[SQL]select Count(*) from student
受影响的行: 0
时间: 0.000s
11.通过分组查询统计地址不同的学生人数
[SQL]select Address,Count(*) as `人数` from student GROUP BY Address
受影响的行: 0
时间: 0.011s
12.通过分组查询统计每学科的总分
[SQL]select s.SubjectName,SubjectNo,sum(StudentResult) as `总分` from result r LEFT join `subject` s on r.SubjectNo = s.SubjectId GROUP BY SubjectNo,s.SubjectName
受影响的行: 0
时间: 0.001s
13.通过分组查询得到总分在90分以上的学科
select s.SubjectName,SubjectNo,sum(StudentResult) as `zs` from result r LEFT join `subject` s on r.SubjectNo = s.SubjectId GROUP BY SubjectNo,s.SubjectName
having zs > 90
14.内连接查询学生表和年级表
[SQL]select * from student s join grand g on s.GradeId = g.GrandId
受影响的行: 0
时间: 0.001s
15.左/右连接查询成绩表和科目表
[SQL]select * from result r left join `subject` s on r.SubjectNo = s.SubjectId
受影响的行: 0
时间: 0.002s
16.为学生表和班级表连接查询的结果创建视图
[SQL]create or replace view view1
as
select *
from student s
left join grand g on s.GradeId = g.GrandId
受影响的行: 0
时间: 0.014s
17.为“上海”的学生创建视图
[SQL]create or replace view view2
as
select *
from student
where Address = ‘上海‘
受影响的行: 0
时间: 0.008s
18.建立订单表及商品表,建立主外键关系
[SQL]create table g(
`id` int PRIMARY key not null AUTO_INCREMENT,
`Name` VARCHAR(50) not null,
`Num` int not null DEFAULT 0
)
受影响的行: 0
时间: 0.017s
[SQL]create table o(
`oid` int PRIMARY key not null AUTO_INCREMENT,
`gid` int not null,
`much` int not null,
KEY `fk3` (`gid`),
CONSTRAINT `fk3` FOREIGN KEY (`gid`) REFERENCES g (`id`)
)
受影响的行: 0
时间: 0.017s
19新增数据
20.新建insert触发器、delete触发器、update触发器
[SQL]create TRIGGER trg_1
AFTER INSERT on o
for EACH ROW
BEGIN
UPDATE g SET num = num - new.much where id = new.gid;
END;
受影响的行: 0
时间: 0.039s
[SQL]INSERT into o values(1,1,2)
受影响的行: 1
时间: 0.001s
[SQL]create TRIGGER trg_2
AFTER DELETE on o
for EACH ROW
BEGIN
UPDATE g SET num = num + old.much where id = old.gid;
END;
受影响的行: 0
时间: 0.032s
[SQL]delete from o where oid = 1
受影响的行: 1
时间: 0.000s
[SQL]create TRIGGER trg_3
AFTER update on o
for EACH ROW
BEGIN
UPDATE g SET num = num + old.much-new.much where id = new.gid;
END;
受影响的行: 0
时间: 0.029s
[SQL]update o set much =3 where oid =1
受影响的行: 1
时间: 0.001s
标签:
原文地址:http://www.cnblogs.com/nele/p/5094345.html