因为最近好像没什么要紧的事干,复习一下数据库吧。
数据库系统可如上图所示。
CREATE DATABASE Studentdb;
CREATE TABLE Student(
Sno CHAR(4) NOT NULL,
Sname VARCHAR(8) NOT NULL,
Gender CHAR(2),
Birthday DATE,
Haddress VARCHAR(50),
Height DECIMAL(3,2),
Remark text,
PRIMARY KEY(Sno)
);
CREATE TABLE Course(
Cno CHAR(4) NOT NULL,
Cname VARCHAR(20) NOT NULL,
Credit int,
PRIMARY KEY(Cno)
);
CREATE TABLE Score(
Sno char(4) NOT NULL,
Cno char(4),
Grade DECIMAL(3,1),
PRIMARY KEY(Sno, Cno)
);
INSERT INTO student(Sno,Sname,Gender,Birthday,Haddress,Height)
VALUES
('0001','刘一平','男','1990-10-1','温州市环城西路201号',1.78),
('0002','张得民','男','1990-12-2','杭州市下沙路22号',1.65),
('0003','马东','男','1990-7-4','宁波市中山北道20号',1.71),
('0004','肖海燕','女','1990-3-15','温州市越秀北路43号',1.65),
('0005','张民华','女','1991-5-13','宁波市艮山路7号',1.63);
INSERT INTO course(Cno,Cname,Credit)
VALUES
('0001','计算机基础',2),
('0002','管理学原理',3),
('0003','数据库原理及应用',3),
('0004','项目管理',2),
('0005','毕业论文',10);
INSERT INTO score(Sno,Cno,Grade)
VALUES
('0001','0001',80.0),
('0001','0002',90.0),
('0001','0003',70.0),
('0001','0004',85.0),
('0001','0005',92.0),
('0002','0001',78.0),
('0002','0002',NULL),
('0002','0003',77.0),
('0002','0004',67.0),
('0003','0001',66.0),
('0003','0002',76.0),
('0003','0003',NULL),
('0003','0004',73.0)
;
SELECT * FROM student;
SELECT Sname,Haddress FROM student;
SELECT * FROM student
WHERE Sname LIKE "刘%";
SELECT * FROM student
WHERE Sname LIKE "%民%";
SELECT Sno,Sname FROM student
WHERE Gender='男' AND Height>1.75;
SELECT Sname,Gender,TIMESTAMPDIFF(YEAR, Birthday, CURDATE()) AS Age
FROM student
WHERE Haddress LIKE "宁波%";
SELECT DISTINCT Sno,Cno FROM Score
WHERE Grade <=> NULL;
SELECT DISTINCT Sno FROM score
WHERE Grade IS NOT NULL;
SELECT Cname FROM course
WHERE Credit>=3;
SELECT Cno,Cname
FROM course
WHERE Credit>=1 AND Credit<=5;
SELECT * FROM course
WHERE Cname="数据库原理及应用";
SELECT score.Cno,student.*
FROM student INNER JOIN score
ON score.Sno = student.Sno ORDER BY Cno;
SELECT MAX(Grade)
FROM score
WHERE Cno='0002';
SELECT COUNT(DISTINCT Sno)
FROM score
WHERE Cno='0001';
SELECT AVG(DISTINCT Grade)
FROM score
WHERE Cno='0003';
SELECT student.Sname
FROM student INNER JOIN score
ON score.Sno = student.Sno
GROUP BY score.Sno
HAVING COUNT(*)>=3;
SELECT Cno,COUNT(DISTINCT Sno)
FROM score
GROUP BY Cno;
SELECT student.Sno,student.Sname
FROM student,score
WHERE student.Sno = score.Sno AND
score.Grade IS NULL;
SELECT student.Sno,student.Sname
FROM student,score
WHERE student.Sno = score.Sno
HAVING COUNT(*)=(SELECT COUNT(Cno)FROM score);
SELECT score.Cno,course.Cname
FROM score,course
WHERE score.Cno = course.Cno
GROUP BY score.Cno
HAVING COUNT(*)>=3;
SELECT student.Sno,student.Sname,course.Cname,score.Grade
FROM student,course,score
WHERE student.Sno = score.Sno AND score.Cno = course.Cno;
SELECT student.Sname, student.Haddress
FROM student, score
WHERE student.Sno = score.Sno AND
score.Cno='0003';
SELECT student.Sno,student.Sname
FROM student,score
WHERE student.Sno = score.Sno AND
Grade >= 90
GROUP BY student.Sno
HAVING COUNT(*)>=2;
SELECT DISTINCT course.*
FROM course, score
WHERE course.Cno = score.Cno
GROUP BY score.Cno
HAVING AVG(score.Grade)>
(SELECT AVG(Grade) FROM score WHERE Cno='0003');
SELECT student.*,course.*
FROM student,course,score
WHERE student.Sno = score.Sno AND
course.Cno = score.Cno;
UPDATE score
SET Grade = Grade-2
WHERE Cno='0002';
DELETE FROM score
WHERE Cno='0002';
DELETE FROM score;
alter table student drop Remark;
alter table student modify column Sname VARCHAR(20) not null ;
UPDATE student set 系名 = '信息系'
WHERE Sno="0001";
UPDATE student set 系名 = '数学系'
WHERE Sno="0002";
UPDATE student set 系名 = '计算机系'
WHERE Sno="0003";
UPDATE student set 系名 = '信息系'
WHERE Sno="0004";
UPDATE student set 系名 = '计算机系'
WHERE Sno="0005";
UPDATE student
SET Height = Height + 0.05
WHERE 系名="信息系";
UPDATE student,score
SET score.Grade = 0
WHERE student.Sno = score.Sno AND
student.系名="计算机系";
Delete From Score
Where Sno in( Select b.* FROM
(SELECT score.Sno FROM student,score
WHERE student.Sno = score.Sno AND student.系名 = '信息系') b
);
SELECT Sname
FROM student
WHERE student.系名 =
(SELECT 系名
FROM student
WHERE Sname="刘一平");
SELECT student.Sno, student.Sname
FROM student,score
WHERE score.Sno=student.Sno AND score.Cno="0002" AND
student.系名<>"信息系" AND
score.Grade >(SELECT MAX(score.Grade)
FROM score,student
WHERE score.Sno = student.Sno AND student.系名 = "信息系");
SELECT Sname, Gender
FROM student
WHERE 系名<>"信息系" AND Birthday<(
SELECT MAX(Birthday)
FROM student
WHERE 系名="信息系");
SELECT student.Sno
FROM student, score
WHERE student.Sno = score.Sno AND 系名="信息系"
GROUP BY student.Sno
HAVING COUNT(score.Cno) =
MAX( (SELECT COUNT(*)
FROM student,score
WHERE student.Sno = score.Sno and 系名="信息系"
GROUP BY student.Sno) );
SELECT DISTINCT student.Sno, student.Sname
FROM student,score s1
WHERE student.Sno = s1.Sno AND s1.Grade<
(SELECT AVG(s2.Grade)
FROM score s2
WHERE s1.Cno=s2.Cno);
CREATE TABLE stu_age(
Sdept VARCHAR(20),
Sage INT
);
INSERT INTO stu_age (Sdept, Sage) SELECT
`系名`,
AVG(TIMESTAMPDIFF(YEAR, Birthday, CURDATE()))
FROM
student
GROUP BY
`系名`;
CREATE VIEW V_IS
as
SELECT *
FROM student
WHERE 系名="信息系";
CREATE VIEW V_S_C_G
AS
SELECT student.Sno, student.Sname,
course.Cno, course.Cname, score.Grade
FROM student,score,course
WHERE student.Sno = score.Sno
AND course.Cno = score.Cno;
CREATE VIEW V_NUM_AVG
AS
SELECT COUNT(系名), AVG(Height)
FROM student
GROUP BY 系名;
CREATE VIEW V_YEAR
AS
SELECT COUNT(Birthday)
FROM student
GROUP BY YEAR(Birthday);
CREATE VIEW V_AVG_S_G
AS
SELECT COUNT(score.Cno), AVG(score.Grade)
FROM student,score
WHERE student.Sno = score.Sno
GROUP BY student.Sno;
CREATE VIEW V_AVG_C_G
AS
SELECT COUNT(score.Sno),AVG(score.Grade)
FROM score
GROUP BY Cno;
现已知成绩绩点的计算方式如下:
create view V_S_GPA
as
(select student.sno,sname,Course.Cno, Cname, credit,(case
when(Grade>=90) then 4
when(Grade >=85 and Grade <90) then 3.7
when(Grade >=82 and Grade <85) then 3.3
when(Grade >=78 and Grade <82) then 3
when(Grade >=75 and Grade <78) then 2.7
when(Grade >=71 and Grade <75) then 2.0
when(Grade >=66 and Grade <71) then 1.7
when(Grade >=62 and Grade <65) then 1.3
when(Grade >=60 and Grade <61) then 1.0
else 0
end) as gpa from Score,Student,Course
where Score.Cno=Course.Cno and Student.Sno= Score.Sno);
SELECT *
FROM v_avg_c_g, v_avg_s_g,v_is,v_num_avg,v_s_c_g,v_s_gpa,v_year;
SELECT student.Sno,student.Sname,v_avg_s_g.`AVG(score.Grade)`
FROM student, v_avg_s_g
WHERE student.Sno = v_avg_s_g.Sno AND
v_avg_s_g.`AVG(score.Grade)`>90 ;
SELECT student.Sno, student.Sname, course.Cname,score.Grade
FROM student,score,course,v_avg_c_g
WHERE student.Sno = score.Sno AND score.Cno=course.Cno
AND course.Cno = v_avg_c_g.Cno AND
score.Grade>v_avg_c_g.`AVG(score.Grade)`
GROUP BY student.Sno
HAVING COUNT(score.Sno)=(SELECT COUNT(Cno) FROM course);
SELECT 系名,COUNT(student.Sno)
FROM student,v_avg_s_g
WHERE student.Sno = v_avg_s_g.Sno
AND student.Sno = v_avg_s_g.Sno AND v_avg_s_g.`AVG(score.Grade)`>80
GROUP BY student.系名
ORDER BY COUNT(*) DESC;
注:平均绩点GPA是国际通用的学生学习质量评定标准,其计算公式如下:课程学分1绩点+课程学分2绩点+…+课程学分n*绩点)/(课程学分1+课程学分2+…+课程学分n)
SELECT student.*
FROM student,v_s_gpa
WHERE student.Sno = v_s_gpa.sno
GROUP BY student.Sno
HAVING AVG(v_s_gpa.gpa)>2.5
ORDER BY `系名`;
UPDATE v_is
set Sname="刘二平"
WHERE Sno="0001";
UPDATE v_is
SET Sname="马西"
WHERE Sno="0004";
SELECT *
FROM v_is;
INSERT
INTO v_is
VALUES('1001','韩磊',"男","1992-01-23", 'IS', 1.75, "信息系");
SELECT *
FROM v_is;
DELETE
FROM v_is
WHERE Sno="0003";
SELECT *
FROM v_is;
UPDATE v_s_c_g
set Sno="0005_2"
WHERE Sno="0001";
在这里不可以,因为原表中,Sno的长度只有4,将姓名修改为“S12_MMM“其长度已经超过此数据类型的范围,因此运行程序也会报错,因此不可以。
不行,无法进行更新因为系统无法修改各科成绩以使平均成绩为90分。
数据库系统概论(第5版)——王珊 萨师煊
大二上数据库自己做的作业(SQL自己做的难免可能有错,欢迎批评指正,希望直接代码砸脸上!)