This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13877|Reply: 1

[Source] Oracle homework code

[Copy link]
Posted on 3/9/2015 4:01:19 PM | | |
  1. -新建学生信息表
  2. create table stuInfo
  3. (
  4.        stuid number primary key,
  5.        stuname varchar(10) not null,
  6.        stusex varchar(2) check (stusex='男' or stusex='女'),
  7.        stubir date
  8. );

  9. --新建课程信息表
  10. create table subInfo
  11. (
  12.        subid number primary key,
  13.        subname varchar(20) not null
  14. );
  15. --新建分类信息表
  16. create table scoreInfo
  17. (
  18.        scoreid number primary key,
  19.        scorexh number references stuinfo(stuid),
  20.        scoresubid number references subInfo(subid),
  21.        score number check (score>=0 or score <=100)
  22. );


  23. --向学生表插入数据
  24. insert into stuInfo values(1,'张山','男',to_date('2005-1-2','yyyy-mm-dd'));
  25. insert into stuInfo values(2,'历史','男',to_date('2001-3-6','yyyy-mm-dd'));
  26. insert into stuInfo values(3,'技术','女',to_date('1995-8-2','yyyy-mm-dd'));
  27. insert into stuInfo values(4,'张山','男',to_date('2006-1-22','yyyy-mm-dd'));
  28. insert into stuInfo values(5,'说道','女',to_date('2013-10-2','yyyy-mm-dd'));
  29. insert into stuInfo values(6,'虽然','男',to_date('1996-10-20','yyyy-mm-dd'));
  30. insert into stuInfo values(7,'谈一谈','女',to_date('2005-1-2','yyyy-mm-dd'));
  31. insert into stuInfo values(8,'一样','女',to_date('2005-1-2','yyyy-mm-dd'));
  32. insert into stuInfo values(9,'方法','男',to_date('1981-6-6','yyyy-mm-dd'));
  33. insert into stuInfo values(10,'尺寸','男',to_date('1980-8-8','yyyy-mm-dd'));
  34. --向课程信息表插入数据
  35. insert into subinfo values(1,'数据库');
  36. insert into subinfo values(2,'java');
  37. insert into subinfo values(3,'web开发');
  38. insert into subinfo values(4,'linux维护');
  39. insert into subinfo values(5,'安全维护');
  40. --向分类信息表插入数据
  41. insert into scoreInfo values(1,1,2,50);
  42. insert into scoreInfo values(2,1,3,70);
  43. insert into scoreInfo values(3,2,1,100);
  44. insert into scoreInfo values(4,3,1,80);
  45. insert into scoreInfo values(5,1,1,60);
  46. insert into scoreInfo values(6,1,1,70);


  47. --创建新表stu1并将stuinfo中的性别为女的学生的学号,姓名和性别的信息插入到该表中
  48. create table stu1 as select * from stuinfo where stusex='女';
  49. --删除stu1表
  50. --drop table stu1;

  51. select * from stu1


  52. 1.查询所有姓张的学员信息(模糊查询)
  53. select * from stuinfo where stuname like '%张%';

  54. 2.查询所有20岁以上的学员信息(用日期函数实现)
  55. select * from stuinfo where MONTHS_BETWEEN(sysdate,stubir)/12>20;

  56. 3.查询同一天出生的学员信息(表连接)
  57. select * from stuinfo where stubir in (select stubir from stuinfo group by stubir having count(*)>1)

  58. 4.查询参加考试的所有学员的名称,科目和成绩(按成绩排序)
  59. select stuinfo.stuname,scoreInfo.Score,scoreInfo.Scoresubid,subinfo.subname from stuinfo inner join scoreInfo on stuinfo.stuid=scoreInfo.Scorexh inner join subinfo on scoreInfo.Scoresubid=subinfo.subid order by scoreInfo.Score;

  60. 5.查询所有没有参加考试的学员学号,姓名,只要有1门课没考都要查询出来(子查询)
  61. --方法一(错误):
  62. select stuinfo.stuid,stuinfo.stuname from stuinfo inner join (select stuid from stuinfo MINUS select Scorexh from scoreInfo) tmp on stuinfo.stuid=tmp.stuid;
  63. --方法二(错误):
  64. select stuinfo.stuid,stuinfo.stuname from stuinfo where stuinfo.stuid not in (select Scorexh from scoreInfo);
  65. --正确答案:



  66. 6.查询出没有学生考试的课程名称
  67. --方法一:
  68. select subInfo.Subname from subInfo inner join (select subInfo.Subid from subInfo MINUS select Scoresubid from scoreInfo) tmp on subinfo.subid=tmp.subid;
  69. --方法二:
  70. select subinfo.subname from subinfo where subinfo.subid not in (select scoresubid from scoreInfo);

  71. 7.参加考试的学员的姓名和总分按总分从大到小排序
  72. select stuinfo.stuname,tmp.fen from stuinfo inner join (select sum(score) fen,scorexh from scoreInfo group by scorexh) tmp on stuinfo.stuid=tmp.scorexh order by fen DESC;

  73. 8.将所有的分数按升序排列并产生并列不跳空的序号
  74. select score,row_number() over(order by score Asc) from scoreInfo order by score Asc
Copy code






Previous:Learn the 54 SQL query statements that must be mastered by databases
Next:What are the types of 2D barcodes? What type do we currently use?
Posted on 11/25/2018 9:50:36 PM |
Is there no correct answer to question 5?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com