当前位置:   article > 正文

Java面试题-----数据库部分_java 数据库面试题

java 数据库面试题

1、有 3 个表(15 分钟):【基础

Student 学生表 (学号,姓名,性别,年龄,组织部门)

Course 课程表 (编号,课程名称)

Sc 选课表 (学号,课程编号,成绩)

建表语句

  1. -- ----------------------------
  2. -- Table structure for t_student
  3. -- ----------------------------
  4. DROP TABLE IF EXISTS `t_student`;
  5. CREATE TABLE `t_student` (
  6. `sno` bigint NOT NULL COMMENT '学号',
  7. `sname` varchar(50) DEFAULT NULL COMMENT '姓名',
  8. `ssex` char(10) DEFAULT NULL COMMENT '性别',
  9. `sage` bigint DEFAULT NULL COMMENT '年龄',
  10. `sdept` varchar(50) DEFAULT NULL COMMENT '组织部门',
  11. PRIMARY KEY (`sno`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
  13. -- ----------------------------
  14. -- Records of t_student
  15. -- ----------------------------
  16. INSERT INTO `t_student` VALUES ('2010211112', '貂蝉', '男', '22', '研发部');
  17. INSERT INTO `t_student` VALUES ('2010211113', '西施', '女', '23', '财务部');
  18. INSERT INTO `t_student` VALUES ('2010211114', '周星驰', '男', '22', '研发部');
  19. INSERT INTO `t_student` VALUES ('2010211115', '王昭君', '男', '22', '研发部');
  20. -- ----------------------------
  21. -- Table structure for t_course
  22. -- ----------------------------
  23. DROP TABLE IF EXISTS `t_course`;
  24. CREATE TABLE `t_course` (
  25. `cno` bigint NOT NULL COMMENT '课程号',
  26. `cname` varchar(50) DEFAULT NULL COMMENT '课程名称',
  27. PRIMARY KEY (`cno`)
  28. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
  29. -- ----------------------------
  30. -- Records of t_course
  31. -- ----------------------------
  32. INSERT INTO `t_course` VALUES ('978001123', '计算机组成原理');
  33. INSERT INTO `t_course` VALUES ('978001124', 'Java基础');
  34. INSERT INTO `t_course` VALUES ('978001125', 'JavaWeb设计');
  35. INSERT INTO `t_course` VALUES ('978001126', '计算机网络');
  36. -- ----------------------------
  37. -- Table structure for t_sc
  38. -- ----------------------------
  39. DROP TABLE IF EXISTS `t_sc`;
  40. CREATE TABLE `t_sc` (
  41. `grade` decimal(20,2) DEFAULT NULL COMMENT '分数',
  42. `sno` bigint DEFAULT NULL COMMENT '学号',
  43. `cno` bigint DEFAULT NULL COMMENT '课程号',
  44. KEY `fk_sno` (`sno`),
  45. KEY `fk_cno` (`cno`),
  46. CONSTRAINT `fk_cno` FOREIGN KEY (`cno`) REFERENCES `t_course` (`cno`) ON DELETE CASCADE ON UPDATE CASCADE,
  47. CONSTRAINT `fk_sno` FOREIGN KEY (`sno`) REFERENCES `t_student` (`sno`) ON DELETE CASCADE ON UPDATE CASCADE
  48. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
  49. -- ----------------------------
  50. -- Records of t_sc
  51. -- ----------------------------
  52. INSERT INTO `t_sc` VALUES ('80.00', '2010211112', '978001123');
  53. INSERT INTO `t_sc` VALUES ('85.00', '2010211113', '978001123');
  54. INSERT INTO `t_sc` VALUES ('95.00', '2010211114', '978001123');
  55. INSERT INTO `t_sc` VALUES ('91.00', '2010211114', '978001124');
  56. INSERT INTO `t_sc` VALUES ('92.00', '2010211114', '978001125');
  57. INSERT INTO `t_sc` VALUES ('88.00', '2010211114', '978001126');
  1. -- 1.查询选修了'计算机原理'的学生学号和姓名
  2. select stu.sno, stu.sname
  3. from t_student stu
  4. where (select count(*)
  5. from t_sc
  6. where sno = stu.sno
  7. and cno =
  8. (select cno from t_course where cname = '计算机原理')) != 0;
  9. -- 2.查询’周星驰’同学选修了的课程名字
  10. select cname
  11. from t_course
  12. where cno in (select cno
  13. from t_sc
  14. where sno = (select sno
  15. from t_student
  16. where sname = '周星驰'));
  17. -- 3.查询选修了 5 门课程的学生学号和姓名
  18. select stu.sno, stu.sname
  19. from t_student stu
  20. where
  21. (select count(*) from t_sc where stu.sno=stu.sno)=5;
  1. -- 员工表
  2. create table t_employee
  3. (
  4. id bigint not null comment '主键',
  5. name varchar(50) null comment '姓名',
  6. age bigint null comment '年龄',
  7. manager_id bigint null comment '所属主管人',
  8. constraint t_employee_id_uindex
  9. unique (id)
  10. )
  11. comment '员工表';
  12. alter table t_employee
  13. add primary key (id);
  1. -- 4. 列出所有年龄比所属主管年龄大的人的 ID 和名字
  2. select emp.name
  3. from t_employee emp
  4. where emp.age > (select manager.age
  5. from t_employee manager
  6. where manager.id = emp.manager_id);

1)写出建表语句;(参考上题)略

2)写出 SQL 语句,查询选修了所有选修课程的学生;

3)写出 SQL 语句,查询选修了至少 5 门以上的课程的学生。

  1. -- 5. 查询选修了所有选修课程的学生;
  2. select stu.sno 学号, stu.sname 姓名, stu.sage 性别, stu.sdept 部门
  3. from t_student stu
  4. where (select count(*)
  5. from t_sc
  6. where sno = stu.sno) = (select count(*)
  7. from t_course);
  8. -- 6. 查询选修了至少 5 门以上的课程的学生;
  9. select stu.sno, stu.sname
  10. from t_student stu
  11. where (select count(*) from t_sc where stu.sno = stu.sno) >= 5;

SQL语句:有如下两张表:【中等难度】

表 city: 表 state:

 

  1. -- 7. 查询城市和省份信息;
  2. select city.city_no 城市编号,
  3. city.city_name 城市名称,
  4. state.state_no 省份编号,
  5. state.state_name 省份名称
  6. from t_city city,
  7. t_state state
  8. where city.state_no = state.state_no
  9. order by (city.city_no);

查询结果:

  1. -- 8. 根据部门号从高到低,工资从低到高列出每个员工的信息;
  2. select emp.name, emp.salary, emp.dept_id
  3. from t_employee emp
  4. order by dept_id desc, salary asc;

SQL执行顺序

  1. select distinct
  2. a.sex,
  3. b.city,
  4. count(1) as cnt,
  5. sum(salary) as sum1
  6. from table1 a
  7. join table2 b on a.id=b.id
  8. where a.name=b.name
  9. group by a.sex,b.city
  10. having cnt>=2
  11. order by a.sex,b.city
  12. limit 10

建表模型

-- 1.查询平均成绩大于60分的学生学号、姓名和平均成绩

select sc.s_id, s.s_name, avg(sc.score) avg_score
from tb_score sc
left join tb_student s on sc.s_id = s.s_id
group by sc.s_id
having avg(sc.score)>60;

-- 2.查询所有学生的学号、姓名、选课数、总成绩

select
s.s_id, s.s_name, count(sc.c_id) course_count, sum(sc.score) total_score
from tb_score sc
right join tb_student s on sc.s_id=s.s_id
group by s.s_id
order by s.s_id desc;

-- 3.查询姓"李"的老师个数

SELECT COUNT(t.t_id) teacher_count
FROM tb_teacher t
WHERE t.t_name LIKE '李%';

-- 4.查询选修 001 课程和002课程的学生的学号、姓名

select s.s_id, s.s_name
from tb_score sc
right join tb_student s on sc.s_id=s.s_id
right join tb_course c on sc.c_id=c.c_id
where c.c_id in ('KC001','KC002')
group by s.s_id, s.s_name;

-- 5.查询所有课程成绩小于60分的学生学号、姓名

select s.s_id, s.s_name
from tb_student s
where s.s_id in (select sc.s_id
                 from tb_score sc
                 where sc.score < 60
                 order by sc.s_id desc);

-- 6.删除"叶平"老师 的课 

delete
from tb_course c
where c.t_id in (select t.t_id from tb_teacher t where t.t_name like '叶平%');

-- 7.查询每门课被选到的学生 

select s.*, c.*
from tb_course c
inner join tb_score sc on c.c_id=sc.c_id
inner join tb_student s on sc.s_id=s.s_id
limit 0, 5;

-- 8.查询课程数据库、分数低于60分的学生姓名和分数

select s.s_name,sc.score,c.c_name
from tb_score sc
left join tb_course c on sc.c_id=c.c_id
left join tb_student s on sc.s_id = s.s_id
where sc.score < 60 and c.c_name like '数据库%';

-- 9.查询选修小于两门课程的学生学号

select s.s_id, s.*
from tb_score sc
right join tb_student s on sc.s_id=s.s_id
group by sc.s_id
having count(sc.c_id) <= 2;

-- 10.查询选修两门以上不及格的学生和平均成绩

select s.*, avg(sc.score) avg_score
from tb_score sc
right join tb_student s on sc.s_id=s.s_id
where sc.score < 60
group by sc.s_id
having count(sc.c_id) >=2;

在oracle中建表模型

  1. -- 创建表空间
  2. create tablespace Sys_SchoolDB
  3. datafile 'c:\Sys_SchoolDB.dbf'
  4. size 100m
  5. autoextend on
  6. next 10m
  7. -- 创建用户
  8. create user hfnu_112
  9. identified by hfnu_112
  10. default tablespace Sys_SchoolDB
  11. -- 给用户授权给用户授权 dba
  12. grant dba to hfnu_112
  13. -- 查询所有用户
  14. select * from dba_users;
  15. -- 查看表空间
  16. select tablespace_name from dba_tablespaces;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/392828
推荐阅读
相关标签
  

闽ICP备14008679号