数据库SQL:一张学生成绩表score,部分内容如下:name course grade 张三操作系统67 张三数据结构86 李四软件工程89 用一条SQL语句查询出每]课都大于80分的学生姓名。
时间: 2024-06-12 14:06:51 浏览: 147
SELECT name FROM score WHERE course IN (SELECT DISTINCT course FROM score WHERE grade > 80) GROUP BY name HAVING COUNT(*) = (SELECT COUNT(DISTINCT course) FROM score WHERE grade > 80)
相关问题
现有教学管理数据库:teachingdb,在数据库中存在四张数据表:student、course、teach、score,使用SQL语言实现数据库和数据表的建立,并对数据表中的数据实现增、删、改、查。四张表之间的结构和数据如下:
首先,让我们来创建这四个表格:
1. Student 表(学生信息)
```sql
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender CHAR(1)
);
```
2. Course 表(课程信息)
```sql
CREATE TABLE course (
id INT PRIMARY KEY,
name VARCHAR(50),
teacher_id INT,
FOREIGN KEY (teacher_id) REFERENCES teach(id)
);
```
3. Teach 表(教师信息)
```sql
CREATE TABLE teach (
id INT PRIMARY KEY,
name VARCHAR(50)
);
```
4. Score 表(学生成绩)
```sql
CREATE TABLE score (
student_id INT,
course_id INT,
grade FLOAT,
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (course_id) REFERENCES course(id)
);
```
接下来是对数据的操作:
- 插入数据(INSERT):
```sql
-- 学生
INSERT INTO student VALUES (1, '张三', 20, 'M');
-- 教师
INSERT INTO teach VALUES (1, '李四');
-- 课程
INSERT INTO course VALUES (1, '数学课', 1);
-- 成绩
INSERT INTO score VALUES (1, 1, 90.0);
-- 更具体的插入示例...
```
- 更新数据(UPDATE):
```sql
UPDATE student SET age = 21 WHERE id = 1;
```
- 删除数据(DELETE):
```sql
DELETE FROM score WHERE student_id = 1 AND course_id = 1;
```
- 查询数据(SELECT):
```sql
-- 获取学生信息
SELECT * FROM student;
-- 获取学生张三的所有课程成绩
SELECT * FROM score WHERE student_id IN (SELECT id FROM student WHERE name = '张三');
```
-- 相关问题--
1. 如何在SQL中删除一条特定的学生记录?
2. 如果需要查询某个课程的所有教师信息,应该如何编写SQL语句?
3. 如果想修改某位教师教授的所有课程成绩,应该怎么做?
在一个成绩管理系统数据库GradeDB中,定义如下关系表: STUDENT(SID,SName,Age,Sex) GRADE(SID,CID,Score, Note) COURSE(CID,CName,Teacher) (1)编写SQL程序创建数据库及其关系表,并插入20个学生的2门课程(“数据库原理及应用”、“数据结构与算法”)成绩数据; (2)GradeLOG表的信息包括user操作用户、SID学号、CID课程号、changetime修改时间、oldscore修改前成绩、newscore修改后成绩。 创建GradeLOG表。 编写触发器程序实现GRADE表数据的插入、删除、修改在日志表(GradeLOG)记录相关操作数据,如操作用户、学号、课程号、修改时间、修改前成绩、修改后成绩。 (3)编写存储过程程序实现统计各课程不及格学生人数,并在屏幕输出;
(1)创建数据库及其关系表,并插入数据:
```sql
CREATE DATABASE GradeDB;
USE GradeDB;
CREATE TABLE STUDENT (
SID CHAR(10) PRIMARY KEY,
SName VARCHAR(20) NOT NULL,
Age INT NOT NULL,
Sex CHAR(2) NOT NULL
);
CREATE TABLE COURSE (
CID CHAR(10) PRIMARY KEY,
CName VARCHAR(20) NOT NULL,
Teacher VARCHAR(20) NOT NULL
);
CREATE TABLE GRADE (
SID CHAR(10) REFERENCES STUDENT(SID),
CID CHAR(10) REFERENCES COURSE(CID),
Score INT NOT NULL,
Note VARCHAR(50),
PRIMARY KEY (SID, CID)
);
INSERT INTO STUDENT VALUES ('200001', '张三', 19, '男');
INSERT INTO STUDENT VALUES ('200002', '李四', 20, '女');
INSERT INTO STUDENT VALUES ('200003', '王五', 18, '男');
INSERT INTO STUDENT VALUES ('200004', '赵六', 21, '女');
INSERT INTO STUDENT VALUES ('200005', '刘七', 20, '男');
INSERT INTO STUDENT VALUES ('200006', '陈八', 19, '女');
INSERT INTO STUDENT VALUES ('200007', '张九', 18, '男');
INSERT INTO STUDENT VALUES ('200008', '李十', 21, '女');
INSERT INTO STUDENT VALUES ('200009', '王麻子', 20, '男');
INSERT INTO STUDENT VALUES ('200010', '赵大', 19, '女');
INSERT INTO STUDENT VALUES ('200011', '刘二', 18, '男');
INSERT INTO STUDENT VALUES ('200012', '陈三', 21, '女');
INSERT INTO STUDENT VALUES ('200013', '张四', 20, '男');
INSERT INTO STUDENT VALUES ('200014', '李五', 19, '女');
INSERT INTO STUDENT VALUES ('200015', '王六', 18, '男');
INSERT INTO STUDENT VALUES ('200016', '赵七', 21, '女');
INSERT INTO STUDENT VALUES ('200017', '刘八', 20, '男');
INSERT INTO STUDENT VALUES ('200018', '陈九', 19, '女');
INSERT INTO STUDENT VALUES ('200019', '张十', 18, '男');
INSERT INTO STUDENT VALUES ('200020', '李一', 21, '女');
INSERT INTO COURSE VALUES ('C001', '数据库原理及应用', '张三');
INSERT INTO COURSE VALUES ('C002', '数据结构与算法', '李四');
INSERT INTO GRADE VALUES ('200001', 'C001', 80, '优秀');
INSERT INTO GRADE VALUES ('200001', 'C002', 75, '良好');
INSERT INTO GRADE VALUES ('200002', 'C001', 65, '及格');
INSERT INTO GRADE VALUES ('200002', 'C002', 70, '良好');
INSERT INTO GRADE VALUES ('200003', 'C001', 55, '不及格');
INSERT INTO GRADE VALUES ('200003', 'C002', 60, '及格');
INSERT INTO GRADE VALUES ('200004', 'C001', 90, '优秀');
INSERT INTO GRADE VALUES ('200004', 'C002', 85, '优秀');
INSERT INTO GRADE VALUES ('200005', 'C001', 70, '良好');
INSERT INTO GRADE VALUES ('200005', 'C002', 75, '良好');
INSERT INTO GRADE VALUES ('200006', 'C001', 80, '优秀');
INSERT INTO GRADE VALUES ('200006', 'C002', 85, '优秀');
INSERT INTO GRADE VALUES ('200007', 'C001', 60, '及格');
INSERT INTO GRADE VALUES ('200007', 'C002', 65, '及格');
INSERT INTO GRADE VALUES ('200008', 'C001', 75, '良好');
INSERT INTO GRADE VALUES ('200008', 'C002', 80, '优秀');
INSERT INTO GRADE VALUES ('200009', 'C001', 55, '不及格');
INSERT INTO GRADE VALUES ('200009', 'C002', 60, '及格');
INSERT INTO GRADE VALUES ('200010', 'C001', 70, '良好');
INSERT INTO GRADE VALUES ('200010', 'C002', 75, '良好');
```
(2)创建GradeLOG表,并编写触发器程序实现GRADE表数据的插入、删除、修改在日志表记录相关操作数据:
```sql
CREATE TABLE GradeLOG (
user VARCHAR(20),
SID CHAR(10),
CID CHAR(10),
changetime DATETIME,
oldscore INT,
newscore INT
);
DELIMITER $$
CREATE TRIGGER log_insert
AFTER INSERT ON GRADE
FOR EACH ROW
BEGIN
INSERT INTO GradeLOG VALUES (USER(), NEW.SID, NEW.CID, NOW(), NULL, NEW.Score);
END$$
CREATE TRIGGER log_delete
AFTER DELETE ON GRADE
FOR EACH ROW
BEGIN
INSERT INTO GradeLOG VALUES (USER(), OLD.SID, OLD.CID, NOW(), OLD.Score, NULL);
END$$
CREATE TRIGGER log_update
AFTER UPDATE ON GRADE
FOR EACH ROW
BEGIN
INSERT INTO GradeLOG VALUES (USER(), NEW.SID, NEW.CID, NOW(), OLD.Score, NEW.Score);
END$$
DELIMITER ;
-- 测试触发器
DELETE FROM GRADE WHERE SID = '200001' AND CID = 'C001';
UPDATE GRADE SET Score = 90 WHERE SID = '200002' AND CID = 'C002';
```
(3)编写存储过程程序实现统计各课程不及格学生人数,并在屏幕输出:
```sql
DELIMITER $$
CREATE PROCEDURE count_failed_students()
BEGIN
DECLARE c_name VARCHAR(20);
DECLARE c_id CHAR(10);
DECLARE n INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT CName, CID FROM COURSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO c_name, c_id;
IF done THEN
LEAVE read_loop;
END IF;
SELECT COUNT(*) INTO n FROM GRADE WHERE CID = c_id AND Score < 60;
SELECT CONCAT(c_name, '不及格人数为', n) AS Result;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;
-- 执行存储过程
CALL count_failed_students();
```
以上代码是一个完整的MySQL数据库程序,可以创建数据库、关系表、插入数据、创建触发器和存储过程,实现对学生成绩的管理。
阅读全文