1、建立数据库,以自己的名字+最后两位学号命名。库中有如下关系,关系模式如下: 教师(教师号,教师名,部门,奖金) 学生(学号,姓名,性别,年龄,系,奖学金) 课程(课程号,课程名,学分,教师号) 选课(学号,课程号,成绩) 创建上述表结构,并插入元组 2、请在“课程”表中插入('c8', 'java', 3 ,'0005') 请在“选课”中中插入('95003', 'c3') 3、查询刘老师所授课程的课程号、课程名及学生选课的名单(包括学号、姓名) 4、查询各学生的选课门数,要求查询学号、姓名和选课门数。 5、查询每个学生所修课程的总学分(成绩大于等于60才有学分) 6、查询选修了课程名为“数据库原理”的学生的学号,姓名和成绩 7、按性别统计男女生人数,显示性别和人数。查询获得奖学金的学生的学号和姓名,系。 8、查询平均成绩在85以上的学生的学号和姓名。
时间: 2023-11-16 22:11:34 浏览: 169
抱歉,我作为一个AI语言模型,并没有数据库查询的功能。不过,我可以告诉你一个基本的SQL查询语句,你可以在相关的数据库管理系统中使用它来查询:
1. 建立数据库并创建表:
```
CREATE DATABASE your_name+last_two_digits_of_student_number;
CREATE TABLE teacher (
teacher_id INT PRIMARY KEY,
teacher_name VARCHAR(50),
department VARCHAR(50),
bonus INT
);
CREATE TABLE student (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
gender VARCHAR(10),
age INT,
department VARCHAR(50),
scholarship INT
);
CREATE TABLE course (
course_id INT PRIMARY KEY,
course_name VARCHAR(50),
credit INT,
teacher_id INT,
FOREIGN KEY (teacher_id) REFERENCES teacher(teacher_id)
);
CREATE TABLE choose_course (
student_id INT,
course_id INT,
score INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (course_id) REFERENCES course(course_id)
);
```
2. 插入元组:
```
INSERT INTO course (course_id, course_name, credit, teacher_id)
VALUES ('c8', 'java', 3 ,'0005');
INSERT INTO choose_course (student_id, course_id)
VALUES ('95003', 'c3');
```
3. 查询刘老师所授课程的课程号、课程名及学生选课的名单(包括学号、姓名):
```
SELECT course.course_id, course.course_name, student.student_id, student.student_name
FROM course
INNER JOIN teacher ON course.teacher_id = teacher.teacher_id
INNER JOIN choose_course ON course.course_id = choose_course.course_id
INNER JOIN student ON choose_course.student_id = student.student_id
WHERE teacher.teacher_name = '刘老师';
```
4. 查询各学生的选课门数,要求查询学号、姓名和选课门数:
```
SELECT student.student_id, student.student_name, COUNT(choose_course.course_id) AS courses_num
FROM student
LEFT JOIN choose_course ON student.student_id = choose_course.student_id
GROUP BY student.student_id;
```
5. 查询每个学生所修课程的总学分(成绩大于等于60才有学分):
```
SELECT student.student_id, student.student_name, SUM(course.credit) AS total_credits
FROM student
INNER JOIN choose_course ON student.student_id = choose_course.student_id
INNER JOIN course ON choose_course.course_id = course.course_id
WHERE choose_course.score >= 60
GROUP BY student.student_id;
```
6. 查询选修了课程名为“数据库原理”的学生的学号,姓名和成绩:
```
SELECT student.student_id, student.student_name, choose_course.score
FROM student
INNER JOIN choose_course ON student.student_id = choose_course.student_id
INNER JOIN course ON choose_course.course_id = course.course_id
WHERE course.course_name = '数据库原理';
```
7. 按性别统计男女生人数,显示性别和人数。查询获得奖学金的学生的学号和姓名,系:
```
-- 统计男女生人数
SELECT gender, COUNT(*) AS num
FROM student
GROUP BY gender;
-- 查询获得奖学金的学生的学号和姓名,系
SELECT student_id, student_name, department
FROM student
WHERE scholarship > 0;
```
8. 查询平均成绩在85以上的学生的学号和姓名:
```
SELECT student.student_id, student.student_name
FROM student
INNER JOIN choose_course ON student.student_id = choose_course.student_id
GROUP BY student.student_id
HAVING AVG(choose_course.score) >= 85;
```
阅读全文