概述
初始表结构和内容
包含Student、Course和SC三个表(分别是学生信息表,课程信息表和选课成绩表)
1.1 Student 表结构
1.2 Course 表结构
1.3 SC表结构
2.1 Student内容
2.2 Course内容
SELECT * FROM Course ORDER BY cast(Cno as int);
2.3 SC内容
插入数据
INSERT INTO Student VALUES('201212126','WANGHUA','MALE',18,'CS');
删除数据
DELETE FROM Course WHERE Cno = '11';
更新数据
UPDATE Course SET Cno = '8' WHERE Cno = '11';
查找数据
1.单表查询
SELECT Sno, Sname FROM Student;
SELECT Sname, 2021-Sage FROM Student;
SELECT Sname NAME,
'Year of Birth:' BIRTH,
2021-Sage BIRTHDAY,
LOWER(Sdept) DEPARTMENT
FROM Student;
SELECT Sno FROM SC;
SELECT DISTINCT Sno FROM SC;
SELECT Sname FROM Student WHERE Sdept = 'CS';
SELECT Sname, Sage FROM Student WHERE Sage<20;
SELECT Sname, Sdept, Sage FROM Student WHERE Sage (NOT) BETWEEN 19 AND 20;
SELECT Sname, Sdept, Sage FROM Student WHERE Sage BETWEEN 19 AND 20;
(NOT)
SELECT Sname, Ssex FROM Student WHERE Sdept IN('CS','IS');
模糊查询
通配符: _ %
SELECT * FROM Student WHERE Sno LIKE '201212121';
SELECT * FROM Course WHERE Cno = '2';
SELECT * FROM Course WHERE Cno LIKE '2';
SELECT * FROM Course WHERE Cno LIKE '2%';
SELECT Sname FROM Student WHERE Sname LIKE 'LIU%';
SELECT Sname FROM Student WHERE Sname LIKE '___CHEN%';
INSERT INTO Course VALUES('9','DBADESIGN','',4);
SELECT * FROM Course WHERE Cname LIKE 'DB_DESIGN%';
SELECT * FROM Course WHERE Cname LIKE 'DB_DESIGN%' ESCAPE'';
聚集函数
学生总人数
SELECT COUNT(*) FROM Student;
选课1的平均成绩
SELECT AVG(Grade) FROM SC WHERE Cno = '1';
学生201212121选修课程的总学分数
SELECT SUM(Ccredit) FROM SC,Course
WHERE Sno = '201212121' AND SC.Cno = Course.Cno;
GROUP BY
各课号及相应的选课人数
SELECT Cno, COUNT(Sno) FROM SC GROUP BY Cno;
链接查询
每个学生及其选修课程的情况
SELECT Student.*, SC.* FROM Student,SC WHERE Student.Sno = SC.Sno;
自然连接
SELECT Student.Sno, Sname, Ssex, Sage, Sdept, Cno, Grade
FROM Student, SC WHERE Student.Sno = SC.Sno;
选修2号课程且成绩在90分以下的学生学号和姓名
SELECT Student.Sno, Sname FROM Student, SC
WHERE Student.Sno = SC.Sno AND SC.Cno = '2' AND SC.Grade<=90;
自身连接
查询每一门课程的先修课(对比)
SELECT FIRST.Cno,SECOND.Cpno FROM Course FIRST, Course SECOND
WHERE FIRST.Cpno = SECOND.Cno;
SELECT Cno,Cpno FROM Course WHERE Cpno = Cno;
SELECT FIRST.Cno,FIRST.Cpno FROM Course FIRST,Course
WHERE FIRST.Cpno = FIRST.Cno;
外连接
SELECT Student.Sno, Sname, Sname, Ssex, Sage, Sdept,Cno,Grade
FROM Student LEFT OUTER JOIN SC ON (Student.Sno = SC.Sno);
多表连接
查询每个学生的学号、姓名、选修的课及成绩
SELECT Student.Sno, Sname, Cname, Grade FROM Student,SC,Course
WHERE Student.Sno = SC.Sno AND SC.Cno = Course.Cno;
嵌套查询
选取了2号课程的学生姓名
SELECT Sname FROM Student WHERE Sno IN
(SELECT Sno FROM SC WHERE Cno = '2');
每个学生超过他自己选修课程平均成绩的课程号
SELECT Sno, Cno FROM SC x
WHERE Grade >=(SELECT AVG(Grade) FROM SC y WHERE y.Sno = x.Sno);
集合查询
计算机科学系的学生以及年龄不大于19岁的学生
SELECT * FROM Student WHERE Sdept = 'CS'
UNION SELECT * FROM Student WHERE Sage<=19;
计算机科学系的学生与年龄不大于19岁的交集
SELECT * FROM Student WHERE Sdept = 'CS'
INTERSECT SELECT * FROM Student WHERE Sage<=19;
计算机科学与技术系的学生与年龄不大于19岁的学生的差集
SELECT * FROM Student WHERE Sdept = 'CS'
EXCEPT SELECT * FROM Student WHERE Sage<=19;
基于派生表的查询
找出每个学生超过他自己选修课程平均成绩的课程号
SELECT Sno, Cno
FROM SC, (SELECT Sno, Avg(Grade) FROM SC GROUP BY Sno)
AS Avg_sc(avg_sno,avg_grade)
WHERE SC.Sno = Avg_sc.avg_sno AND SC.Grade >= Avg_sc.avg_grade;
最后
以上就是活力西装为你收集整理的SQL 增删改查(学生、课程、选课表为例)的全部内容,希望文章能够帮你解决SQL 增删改查(学生、课程、选课表为例)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复