grademanger
和四张表VarCHAR(20)
若有错,请多多包涵b( ̄▽ ̄)d
CREATE DATABASE grademanager
CREATE TABLE Student(
Sno CHAR(7) NOT NULL PRIMARY KEY,
Sname VarCHAR(20) NOT NULL,
Ssex CHAR(2) NOT NULL,
Sage SMALLINT ,
Clno CHAR(5) NOT NULL);
CREATE TABLE Course(
Cno CHAR(1) NOT NULL PRIMARY KEY,
Cname VarCHAR(20) NOT NULL,
Credit Smallint);
CREATE TABLE Class(
Clno CHAR(5) NOT NULL PRIMARY KEY,
Speciality VarCHAR(20) NOT NULL,
Inyear CHAR(4) NOT NULL,
Number Integer,
Monitor CHAR(7));
CREATE TABLE Grade(
Sno CHAR(7) NOT NULL,
Cno CHAR(1) NOT NULL,
Gmark Numeric(4,1));
INSERT INTO Student
VALUES('2000101','李勇','男',20,'00311');
INSERT INTO Student
VALUES('2000102','刘诗晨','女',19,'00311');
INSERT INTO Student
VALUES('2000103','王一鸣','男',20,'00312');
INSERT INTO Student
VALUES('2000104','张婷婷','女',21,'00312');
INSERT INTO Student
VALUES('2001101','李勇敏','女',19,'01311');
INSERT INTO Student
VALUES('2001102','贾向东','男',22,'01311');
INSERT INTO Student
VALUES('2001103','陈宝玉','男',20,'01311');
INSERT INTO Student
VALUES('2001104','张逸凡','男',21,'01311');
INSERT INTO Course
VALUES('1','数据库',4);
INSERT INTO Course
VALUES('2','离散数学',3);
INSERT INTO Course
VALUES('3','管理信息系统',2);
INSERT INTO Course
VALUES('4','操作系统',4);
INSERT INTO Course
VALUES('5','数据结构',4);
INSERT INTO Course
VALUES('6','数据处理',2);
INSERT INTO Course
VALUES('7','C语言',4);
INSERT INTO Class
VALUES('00311','计算机软件','2000',120,'2000101');
INSERT INTO Class
VALUES('00312','计算机应用','2000',140,'2000103');
INSERT INTO Class
VALUES('01311','计算机软件','2001',220,'2000103');
INSERT INTO Grade
VALUES('2000101','1',92);
INSERT INTO Grade
VALUES('2000101','3',null);
INSERT INTO Grade
VALUES('2000101','5',86);
INSERT INTO Grade
VALUES('2000102','1',78);
INSERT INTO Grade
VALUES('2000102','6',55);
INSERT INTO Grade
VALUES('2000103','3',65);
INSERT INTO Grade
VALUES('2000103','6',78);
INSERT INTO Grade
VALUES('2000103','5',66);
INSERT INTO Grade
VALUES('2000104','1',54);
INSERT INTO Grade
VALUES('2001101','6',83);
INSERT INTO Grade
VALUES('2001101','2',70);
INSERT INTO Grade
VALUES('2001102','4',65);
INSERT INTO Grade
VALUES('2001102','2',80);
INSERT INTO Grade
VALUES('2001102','4',null);
INSERT INTO Grade
VALUES('2001102','6',83);
INSERT INTO Grade
VALUES('2001103','4',76);
INSERT INTO Grade
VALUES('2001103','6',56);
--1.给学生表增加属性Nation(民族),数据类型为VarCHAR(20)
ALTER TABLE Student
ADD Nation VarCHAR(20);
--2.删除学生表中新增的属性Nation
ALTER TABLE Student
DROP COLUMN Nation;
--3.向成绩表中插入记录('2001110','3',80)
INSERT INTO Grade
VALUES('2001110','3',80);
--4.将学号为200110的学生的成绩修改为70分
UPDATE Grade
SET Gmark =70
WHERE Sno = '2001110';
--删除学号为200110的学生的成绩
DELETE FROM Grade
WHERE Sno = '2001110';
--6.在学生表的Clno属性上创建一个名为IX_Class的索引,以班级号的升序排序
CREATE INDEX IX_Class
ON Student(Clno);
--7.删除IX_Class 索引
DROP INDEX IX_Class ON Student;
--1.找出所有被学生选修了的课程号
SELECT DISTINCT Cno FROM Grade
--2.找出01312班女生的个人信息
SELECT * FROM Student
WHERE Clno = '01311' and Ssex = '女';
--3.找出01311班和01312班的学生姓名 性别 出生年份
SELECT Sname,Ssex,Sage FROM Student
WHERE Clno = '01311' or Clno = '01312'
--4.找出所有姓李的学生的个人信息
SELECT * FROM Student
WHERE Sname LIKE '李%';
--5.找出李勇所在班级的学生人数
SELECT COUNT(*) FROM Student
WHERE Clno IN (SELECT Clno FROM Student WHERE Sname = '李勇');
--6.找出课程名为操作系统的平均成绩 最高分 最低分
SELECT AVG(Gmark) AS '平均成绩',MAX(Gmark) AS '最高分',MIN(Gmark) AS '最低分' FROM Grade
WHERE Cno IN (SELECT Cno FROM Course WHERE Cname = '操作系统');
--7.选修了课程的学生人数
SELECT COUNT(DISTINCT Sno) AS '课程学生人数' FROM Grade
--8.选修了操作系统的学生人数
SELECT COUNT(DISTINCT Sno) AS '学生人数' FROM Grade
WHERE Cno IN (SELECT Cno FROM Course WHERE Cname = '操作系统');
--9.找出2000级计算机软件班的成绩为空的学生姓名
SELECT Sname FROM Student
WHERE (Clno IN (SELECT Clno FROM Class WHERE Speciality = '计算机软件' AND Inyear = '2000'))
AND (Sno IN (SELECT Sno FROM Grade WHERE Gmark IS NULL));
SELECT * FROM Grade
WHERE Gmark IS NULL;