航空网站数据库面试题.docx

上传人:s****u 文档编号:12788133 上传时间:2020-05-24 格式:DOCX 页数:8 大小:23.54KB
返回 下载 相关 举报
航空网站数据库面试题.docx_第1页
第1页 / 共8页
航空网站数据库面试题.docx_第2页
第2页 / 共8页
航空网站数据库面试题.docx_第3页
第3页 / 共8页
点击查看更多>>
资源描述
1.航空网的几个航班查询题:表结构如下:flightflightID,StartCityID ,endCityID,StartTimecitycityID, CityName)实验环境:create table city(cityID int auto_increment primary key,cityName varchar(20);create table flight (flightID int auto_increment primary key, StartCityID int references city(cityID), endCityID int references city(cityID), StartTime timestamp); /航班本来应该没有日期部分才好,但是下面的题目当中涉及到了日期insert into city values(null,北京),(null,上海),(null,广州);insert into flight values (null,1,2,9:37:23),(null,1,3,9:37:23),(null,1,2,10:37:23),(null,2,3,10:37:23);1、查询起飞城市是北京的所有航班,按到达城市的名字排序参与运算的列是我起码能够显示出来的那些列,但最终我不一定把它们显示出来。各个表组合出来的中间结果字段中必须包含所有运算的字段。 select * from flight f,city c where f.endcityid = c.cityid and startcityid = (select c1.cityid from city c1 where c1.cityname = 北京) order by c.cityname asc;mysql select flight.flightid,北京 startcity, e.cityname from flight,city e where flight.endcityid=e.cityid and flight.startcityid=(select cityid from city where cityname=北京);mysql select flight.flightid,s.cityname,e.cityname from flight,city s,city e where flight.startcityid=s.cityid and s.cityname=北京 and flight.endCityId=e.cityID order by e.cityName desc;2、查询北京到上海的所有航班纪录(起飞城市,到达城市,起飞时间,航班号)select c1.CityName,c2.CityName,f.StartTime,f.flightIDfrom city c1,city c2,flight fwhere f.StartCityID=c1.cityID and f.endCityID=c2.cityIDand c1.cityName=北京and c2.cityName=上海3、查询具体某一天(2005-5-8)的北京到上海的的航班次数select count(*) from (select c1.CityName,c2.CityName,f.StartTime,f.flightIDfrom city c1,city c2,flight fwhere f.StartCityID=c1.cityID and f.endCityID=c2.cityIDand c1.cityName=北京and c2.cityName=上海and 查帮助获得的某个日期处理函数(startTime) like 2005-5-8%mysql中提取日期部分进行比较的示例代码如下:select * from flight where date_format(starttime,%Y-%m-%d)=1998-01-022.查出比经理薪水还高的员工信息:Drop table if not exists employees;create table employees(id int primary key auto_increment,name varchar(50),salary int,managerid int references employees(id);insert into employees values (null, lhm,10000,null), (null, zxx,15000,1),(null,flx,9000,1),(null,tg,10000,2),(null,wzg,10000,3);Wzg大于flx,lhm大于zxx解题思路: 根据sql语句的查询特点,是逐行进行运算,不可能两行同时参与运算。涉及了员工薪水和经理薪水,所有,一行记录要同时包含两个薪水,所有想到要把这个表自关联组合一下。 首先要组合出一个包含有各个员工及该员工的经理信息的长记录,譬如,左半部分是员工,右半部分是经理。而迪卡尔积会组合出很多垃圾信息,先去除这些垃圾信息。select e.* from employees e,employees m where e.managerid=m.id and e.salarym.salary;3、求出小于45岁的各个老师所带的大于12岁的学生人数数据库中有3个表 teacher 表,student表,tea_stu关系表。 teacher 表 teaID name age student 表 stuID name age teacher_student表 teaID stuID 要求用一条sql查询出这样的结果 1.显示的字段要有老师name, age 每个老师所带的学生人数 2 只列出老师age为40以下,学生age为12以上的记录预备知识: 1.sql语句是对每一条记录依次处理,条件为真则执行动作(select,insert,delete,update) 2.只要是迪卡尔积,就会产生“垃圾”信息,所以,只要迪卡尔积了,我们首先就要想到清除“垃圾”信息实验准备: drop table if exists tea_stu; drop table if exists teacher; drop table if exists student; create table teacher(teaID int primary key,name varchar(50),age int); create table student(stuID int primary key,name varchar(50),age int); create table tea_stu(teaID int references teacher(teaID),stuID int references student(stuID);insert into teacher values(1,zxx,45), (2,lhm,25) , (3,wzg,26) , (4,tg,27);insert into student values(1,wy,11), (2,dh,25) , (3,ysq,26) , (4,mxc,27);insert into tea_stu values(1,1), (1,2), (1,3);insert into tea_stu values(2,2), (2,3), (2,4);insert into tea_stu values(3,3), (3,4), (3,1);insert into tea_stu values(4,4), (4,1), (4,2) , (4,3);结果:23,32,43解题思路:(真实面试答题时,也要写出每个分析步骤,如果纸张不够,就找别人要)1要会统计分组信息,统计信息放在中间表中:select teaid,count(*) from tea_stu group by teaid;2接着其实应该是筛除掉小于12岁的学生,然后再进行统计,中间表必须与student关联才能得到12岁以下学生和把该学生记录从中间表中剔除,代码是:select tea_stu.teaid,count(*) total from student,tea_stu where student.stuid=tea_stu.stuid and student.age12 group by tea_stu.teaid3.接着把上面的结果做成虚表与teacher进行关联,并筛除大于45的老师select teacher.teaid,teacher.name,total from teacher ,(select tea_stu.teaid,count(*) total from student,tea_stu where student.stuid=tea_stu.stuid and student.age12 group by tea_stu.teaid) as tea_stu2 where teacher.teaid=tea_stu2.teaid and teacher.age select id,name,salary,deptid did from employee1 where (select avg(salary)from employee1 where deptid = did) 1800;(4) 基于上述EMPLOYEES表写出查询:查出个人工资高于其所在部门平均工资的员工,列出这些员工的全部个人信息及该员工工资高出部门平均工资百分比。(5分)select employee1.*,(employee1.salary-t.avgSalary)*100/employee1.salary from employee1, (select deptid,avg(salary) avgSalary from employee1 group by deptid) as twhere employee1.deptid = t.deptid and employee1.salaryt.avgSalary;8、注册Jdbc驱动程序的三种方式9、用JDBC如何调用存储过程代码如下:package com.huawei.interview.lym;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Types;public class JdbcTest /* * param args */ public static void main(String args) / TODO Auto-generated method stub Connection cn = null; CallableStatement cstmt = null; try /这里最好不要这么干,因为驱动名写死在程序中了 Class.forName(com.mysql.jdbc.Driver); /实际项目中,这里应用DataSource数据,如果用框架, /这个数据源不需要我们编码创建,我们只需Datasource ds = context.lookup() /cn = ds.getConnection(); cn = DriverManager.getConnection(jdbc:mysql:/test,root,root); cstmt = cn.prepareCall(call insert_Student(?,?,?); cstmt.registerOutParameter(3,Types.INTEGER); cstmt.setString(1, wangwu); cstmt.setInt(2, 25); cstmt.execute(); /get第几个,不同的数据库不一样,建议不写 System.out.println(cstmt.getString(3); catch (Exception e) / TODO Auto-generated catch block e.printStackTrace(); finally /*trycstmt.close();catch(Exception e) trycn.close();catch(Exception e)*/ try if(cstmt != null) cstmt.close(); if(cn != null) cn.close(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); 10、JDBC中的PreparedStatement相比Statement的好处答:一个sql命令发给服务器去执行的步骤为:语法检查,语义分析,编译成内部指令,缓存指令,执行指令等过程。select * from student where id =3-缓存-xxxxx二进制命令select * from student where id =3-直接取-xxxxx二进制命令select * from student where id =4- -会怎么干?如果当初是select * from student where id =?- -又会怎么干?上面说的是性能提高可以防止sql注入。11. 写一个用jdbc连接并访问oracle数据的程序代码12、Class.forName的作用?为什么要用? 答:按参数中指定的字符串形式的类名去搜索并加载相应的类,如果该类字节码已经被加载过,则返回代表该字节码的Class实例对象,否则,按类加载器的委托机制去搜索和加载该类,如果所有的类加载器都无法加载到该类,则抛出ClassNotFoundException。加载完这个Class字节码后,接着就可以使用Class字节码的newInstance方法去创建该类的实例对象了。有时候,我们程序中所有使用的具体类名在设计时(即开发时)无法确定,只有程序运行时才能确定,这时候就需要使用Class.forName去动态加载该类,这个类名通常是在配置文件中配置的,例如,spring的ioc中每次依赖注入的具体类就是这样配置的,jdbc的驱动类名通常也是通过配置文件来配置的,以便在产品交付使用后不用修改源程序就可以更换驱动类名。13、大数据量下的分页解决方法。答:最好的办法是利用sql语句进行分页,这样每次查询出的结果集中就只包含某页的数据内容。再sql语句无法实现分页的情况下,可以考虑对大的结果集通过游标定位方式来获取某页的数据。sql语句分页,不同的数据库下的分页方案各不一样,下面是主流的三种数据库的分页sql:sql server: String sql = select top + pageSize + * from students where id not in +(select top + pageSize * (pageNumber-1) + id from students order by id) + order by id;mysql: String sql = select * from students order by id limit + pageSize*(pageNumber-1) + , + pageSize; oracle: String sql = 14、说出数据连接池的工作机制是什么? J2EE服务器启动时会建立一定数量的池连接,并一直维持不少于此数目的池连接。客户端程序需要连接时,池驱动程序会返回一个未使用的池连接并将其表记为忙。如果当前没有空闲连接,池驱动程序就新建一定数量的连接,新建连接的数量有配置参数决定。当使用的池连接调用完成后,池驱动程序将此连接表记为空闲,其他调用就可以使用这个连接。 实现方式,返回的Connection是原始Connection的代理,代理Connection的close方法不是真正关连接,而是把它代理的Connection对象还回到连接池中。15、为什么要用 ORM? 和 JDBC 有何不一样? orm是一种思想,就是把object转变成数据库中的记录,或者把数据库中的记录转变成objecdt,我们可以用jdbc来实现这种思想,其实,如果我们的项目是严格按照oop方式编写的话,我们的jdbc程序不管是有意还是无意,就已经在实现orm的工作了。现在有许多orm工具,它们底层调用jdbc来实现了orm工作,我们直接使用这些工具,就省去了直接使用jdbc的繁琐细节,提高了开发效率,现在用的较多的orm工具是hibernate。也听说一些其他orm工具,如toplink,ojb等。
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!