首页 > IT职业 > IT图书 > [SQL]常见数据库上机考试题
student表:
| 字段名 | 描述 | 数据类型 | 约束 |
|---|---|---|---|
| sno | 学号 | 12个字符宽 | 主键 |
| sname | 姓名 | 10个字符宽 | |
| sex | 性别 | 2个字符宽 | |
| age | 年龄 | 整型 | |
| address | 住址 | 50个字符宽 | |
| classno | 班级号 | 5个字符宽 |
course表:
| 字段名 | 描述 | 数据类型 | 约束 |
|---|---|---|---|
| cno | 课程号 | 10个字符宽 | 主键 |
| cname | 课程名 | 20个字符宽 | |
| lecture | 学时 | 整型 | |
| credit | 学分 | 整型 |
sc表:
| 字段名 | 描述 | 数据类型 | 约束 |
|---|---|---|---|
| sno | 学号 | 10个字符宽 | 主键 |
| cno | 课程号 | 20个字符宽 | 主键 |
| score | 成绩 | 整型 |
class表:
| 字段名 | 描述 | 数据类型 | 约束 |
|---|---|---|---|
| classno | 班级号 | 5个字符宽 | 主键 |
| classname | 班级名称 | 20个字符宽 | |
| monitor | 班长姓名 | 10个字符宽 |
创建以上表结构:
/*数据表的创建*/USE HNIST GO --①创建student表 CREATE TABLE student ( --sno 学号 12个字符宽 主键 sno char(12) primary key, --sname 姓名 10个字符宽 sname char(10), --sex 性别 2个字符宽 sex char(2), --age 年龄 整型 age int, --address 住址 50个字符宽 address char(50), --classno 班级号 5个字符宽 classno char(5) ) GO --②创建course表 CREATE TABLE course ( --cno 课程号 10个字符宽 主键 cno char(10) primary key, --cname 课程名 20个字符宽 cname char(20), --lecture 学时 整型 lecture int, --credit 学分 整型 credit int, ) GO --③创建sc表 CREATE TABLE sc ( --sno 学号 10个字符宽 主键 sno char(10) not null, --cno 课程号 20个字符宽 主键 cno char(20) not null, --score 成绩 整型 score int, primary key(sno,cno) ) GO --④创建class表 CREATE TABLE class( --classno 班级号 5个字符宽 主键 classno char(5) primary key, --classname 班级名称 20个字符宽 classname char(20), --monitor 班长姓名 10个字符宽 monitor char(10) --foreign key(dno) REFERENCES Dept_info(dno) ) GO
注意题目答案都是我自己整理和编写的SQL语句若有瑕疵请私信指出,感激不尽
USE HNISTGOALTER TABLE scADD CONSTRAINT con CHECK(sc.score>0 and sc.score<150)
--思路:先成绩->再学号->然后姓名
USE HNIST GO SELECT sno,score FROM sc WHERE cno='002' and score > ( select score from sc where cno='2' and sno=( select sno from student where sname = 'L.Toni' ) ); go
--思路:嵌套的思维,按题目顺序从前到后(正常是从后到前),先通过名字和课程号找到成绩再套一层select找相同成绩,由于相同成绩不止一个所以给到的是in
USE HNIST SELECT sc.sno,sname FROM sc,student WHERE sc.sno = student.sno and cno = '006' and score in ( select score from sc,student where sname = 'B' and cno = '006')
思路:在课程号009内没有这位学生的
SELECT sno,sname FROM student WHERE sno not in ( SELECT sno FROM SC WHERE cno = '009')
USE HNIST GO SELECT sname,age FROM student WHERE sex = '男' and age > ( SELECT avg(age) FROM student WHERE sex = '女')
SELECT classno,classname,AVG(score) FROM class,sc GROUP BY classno,classname
--思路:sc表用于连接student和class表,使值不重复
USE HNIST GO SELECT student.sno,scorefrom student,sc,classwhere student.sno = sc.sno and class.classno = SC.cno and class.classname='云计算' and score>90go
USE HNIST GO SELECT student.sno,sname,score*0.8 as 'score'FROM SC,student WHERE cno = '001' and score between 80 and 90;
USE HNIST GO CREATE PROCEDURE pr4 @number char(12) as select sname,classname,credit,lecture,score from student join course on student.classno = course.cno and sno = @number ,class,scGO
--执行:exec pr4 '传入一个值'
USE HNIST GO CREATE VIEW v as select student.sno,sname,address,class.classno,cname,credit,lecturefrom student,sc,class,course
USE HNIST GO SELECT cno,score FROM SC WHERE score = ( SELECT MAX(score) FROM SC WHERE sno = ( SELECT sno FROM student WHERE sname = 'B' ) )
--思路:依靠属性连接表,再进行筛选
USE HNIST GO SELECT sc.sno,score FROM sc,student,classwhere sc.sno = student.sno and sc.cno = class.classno and classname = '大数据分析' and score>( select avg(score) from sc where classname = '大数据分析')
USE HNIST GO SELECT sno,score FROM sc,classWHERE sc.cno = class.classno and classname = '大数据分析'order by score asc,cno desc
USE HNIST GO SELECT sno,score FROM scwhere score = ( select max(score) from sc where cno = '006')
--思路:值对应
USE HNIST GO select student.sno,sname,classname,scorefrom course,student,class,scwhere course.cno = sc.cno and sc.cno = class.classno and sc.sno = student.sno and credit = '3'
--注意不要把存储过程命名为proc!
USE HNIST go create procedure pro @name char(10) as select sname,age,score from sc,student where sc.sno = student.sno and classno = ( select classno from class where classname = @name)
--执行:exec pro '传入一个值'
作者:Topus
链接:https://www.jianshu.com/p/427168fc0e65
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2349/viewspace-2821084/ ,如需转载,请注明出处,否则将追究法律责任。