JAVA企业面试题精选 数据库21-30

1.21.表名:高考信息表

准考证号     科目      成绩
2006001     语文      119
2006001     数学      108
2006002     物理      142
2006001     化学      136
2006002     数学      149
2006002     英语      110
2006002     语文      105
2006001     英语       98
2006002     化学      129
......

给出高考总分在600分以上的学生准考证号.

参考答案:

  在MySQL数据库中,创建测试表和测试数据,代码如下所示:

create table score(
    num int,
    name varchar(20),
    score numeric
);
insert into score(num,name,score) values(2006001,'语文',119);
insert into score(num,name,score) values(2006001,'数学',108);
insert into score(num,name,score) values(2006002,'物理',142);
insert into score(num,name,score) values(2006001,'化学',136);
insert into score(num,name,score) values(2006001,'物理',127);
insert into score(num,name,score) values(2006002,'数学',149);
insert into score(num,name,score) values(2006002,'英语',110);
insert into score(num,name,score) values(2006002,'语文',105);
insert into score(num,name,score) values(2006001,'英语',98);
insert into score(num,name,score) values(2006002,'化学',129);

  查询高考总分在600以上的学生准考证号的SQL语句如下所示:

select num from score group by num having sum(score)>600;

1.22.数据库有两张表一个学生表(id,name,sex),一个学生成绩表(id,chineses,English,math),要求查询学生基本信息以及各科成绩和总成绩,总成绩要求在200到300之间,学生姓名降序

参考答案:

  在MySQL数据库中,创建测试表和测试数据,代码如下所示:

create table student(
    id int primary key,
    name varchar(20),
    sex varchar(2)
);

create table grade(
    id int REFERENCES student(id),
    chineses numeric,
    english numeric,
    math numeric
);

insert into student(name,sex) values('张三','男');
insert into student(name,sex) values('李四','男');
insert into student(name,sex) values('王五','男');

insert into grade(id,chinese,english,math) values(1,80,80,60);
insert into grade(id,chinese,english,math) values(2,50,50,50);
insert into grade(id,chinese,english,math) values(3,80,80,70);

  查询学生基本信息以及各科成绩和总成绩,总成绩要求在200到300之间,学生姓名降旭的SQL语句如下所示:

select name,chinese,english,math,(chineses+english+math) sumscore from student join grade
where student.id=grade.id
and (chinese+english+math) between 200 and 300
order by name desc;

1.23.现有关系数据库表如下:

学生表(学号 char(6),姓名,性别,身份证号);

课程表(课号 char(6),名称);

成绩表(id,学号,课号,分数).

用SQL实现如下两道题:

1.检索姓马的女同学情况(姓名,身份证号);

2.检索有一门或一门以上课程成绩大于等于90的所有学生信息(学号,姓名).

参考答案:

  在MySQL数据库中,创建测试表和测试数据,代码如下所示:

create table stu(
    stuNo char(6),
    name varchar(20),
    gender varchar(4),
    idcard varchar(18)
);

insert into stu(stuno,name,gender,idcard) values('100001','马青青','女','232301198006013421');
insert into stu(stuno,name,gender,idcard) values('100002','马立军','男','232301198006013422');
insert into stu(stuno,name,gender,idcard) values('100003','王媛媛','女','232301198006013423');

create table course(
    cno char(6);
    cname varchar(20);
);

insert into course(cno,cname) values('1','语文');
insert into course(cno,cname) values('2','数学');
insert into course(cno,cname) values('3','英语');

create table score(
    id int primary key AUTO_INCREMENT,
    stuno char(6) REFERENCES stu(stuno),
    cno char(6) REFERENCES course(cno),
    score numeric
);

insert into score(stuno,cno,score) values('100001','1',80);
insert into score(stuno,cno,score) values('100001','2',90);
insert into score(stuno,cno,score) values('100001','3',95);

insert into score(stuno,cno,score) values('100002','1',70);
insert into score(stuno,cno,score) values('100002','2',80);
insert into score(stuno,cno,score) values('100002','3',85);

insert into score(stuno,cno,score) values('100003','1',60);
insert into score(stuno,cno,score) values('100003','2',70);
insert into score(stuno,cno,score) values('100003','3',80);

  1.检索姓马的女同学的姓名,身份证号的SQL语句如下所示:

select name,idcard from stu where name like '马%' and gender='女';

  2.检索有一门或一门以上课程成绩大于等于90的所有学生的学号,姓名,SQL语句如下所示:

select distinct stu.stuno,name from stu
join score
on stu.stuno=score.stuno
join course
on course.cno=score.cno
where score>=90;

1.24.有三张表,学生表Student,课程Course,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联,详细要求如下:

  1.写出建表语句;

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

  3.写出SQL语言,查询选修了至少2门以上的课程的学生;

参考答案:

  1.在Oracle数据库中,创建表和测试数据的SQL语句如下所示:

create table student(
id number(10) primary key,
name varchar2(20));

create table course(
id number(10) primary key,
name varchar2(20));

create table sc(
sid number(10) references student(id),
cid number(10) references course(id),
grade number(4,2));

insert into student values(1,'wanwan');
insert into student values(2,'tutu');
insert into student values(3,'suisui');
insert into student values(4,'fofo');
insert into student values(5,'faiwu');

insert into courese values(1,'java');
insert into courese values(2,'c');
insert into courese values(3,'python');
insert into courese values(4,'c++');

insert into sc values(1,1,98);
insert into sc values(2,1,97);
insert into sc values(3,1,94);
insert into sc values(4,1,92);
insert into sc values(5,1,93);
insert into sc values(1,2,94);
insert into sc values(2,2,92);
insert into sc values(3,2,95);
insert into sc values(5,2,97);
insert into sc values(1,3,92);
insert into sc values(2,3,92);
insert into sc values(4,3,91);
insert into sc values(1,4,99);
insert into sc values(3,4,89);

  2.查询选修了所有选修课程的学生的SQL语句如下所示:

select name from student where id in(select sid sc group by sid having count(*)=(select count(*) from course));

  3.查询选修了至少2门以上的课程的学生的SQL语句如下所示:

select name from student where id in(select sid sc group by sid having count(*)>=2);

1.25.表class和student结构如下,请完成后续SQL语句

表class

属性 类型(长度) 默认值 约束 含义
CLASSNO 数值(2) 主键 班级编号
CNAME 变长字符(10) 非空 班级名称

表student

属性 类型(长度) 默认值 约束 含义
STUNO 数值(8) 主键 学号
SNAME 变长字符(12) 非空 姓名
SEX 字符(2) 性别
BIRTHDAY 字符(8) 生日
EMAIL 变长字符(20) 唯一 电子邮件
SCORE 数值(5,2) 检查 成绩
CLASSNO 数值(2) 外键,关联到表class的CLASSNO 编辑编号

测试数据

STUNO SNAME SEX BIRTHDAY EMAIL SCORE CLASSNO
21 tom 19790203 [email protected] 89.50 1
56 jerry 默认值 2

1.修改表student的数据,将所有一班的学生成绩加10分.

2.删除表student的数据,将所有3班出生日期晚于1981年5月12日的记录删除.

3.按班级升序排序,成绩降序排序,查询student表的所有记录.

4.查询student表中所有三班成绩为空的学生记录.

5.表student与class联合查询,要求查询所有学生的学号,姓名,成绩,班级名称.

6.按班级编号分组统计每个班的人数,最高分,最低分,平均分,按平均分降序排序.

7.查询一班学生记录中所有成绩高于本班学生平均分的记录.

8.查询所有学生记录中成绩前十名的学生的学号,姓名,成绩,班级编号.

参考答案:

  在Oracle数据库中,创建测试表和测试数据,代码如下所示:

create table class(
    classno number(2) primary key,
    cname varchar2(10)
);

create table student(
    stuno number(18) primary key,
    sname varchar2(12),
    sex char(2) default '男',
    birthday char(8),
    email varchar2(20) unique,
    score number(5,2) check(score between 0 and 100),
    classno number(2) references class(classno)
);

insert student(stuno,sname,sex,birthday,email,score,classno) values(21,'tom','男','19790203','[email protected]',89.50,1);

insert student(stuno,sname,birthday,email,score,classno) values(56,'jerry',null,null,null,2);

  1.修改student的数据,将所有一班的学生成绩加10分,SQL语句如下所示:

update student set score=score+10
where classno=(select classno from class where cname='一班');

  2.删除表student的数据,将所有3班出生日期晚于1981年5月12日的记录删除,SQL语句如下所示:

delete student
where to date(birthday,'yyyymmdd')<to date('19810512','yyyymmdd')
and classno=(select classno from class where cname='三班');

  3.按班级升序排序,成绩降序排序,查询student表的所有记录,SQL语句如下所示:

select * from student order by classno,score desc;

  4.查询student表中所有三班成绩为空的学生记录,SQL语句如下所示:

select * from student where classno=(select classno from class where cname='三班') and score is null;

  5.表student与class联合查询,要求查询所有学生的学号,姓名,成绩,班级名称,SQL语句如下所示:

select s.stuno,s.name,s.score,c.name from student s join class c on s.classno=c.classno;

  6.按班级编号分组统计每个班的人数,最高分,最低分,平均分,按平均分降序排序,代码如下所示:

select count(*) "人数",max(score) "最高分",min(score) "最低分",avg(nvl(score,0)) "平均分" from student group by classno order by avg(nvl(score,0));

  7.查询一班学生记录中所有成绩高于本班学生平均分的记录,代码如下所示:

select * from student
where score>(select avg(nvl(score,0)) from student where classno=(select classno from class where cname='一班'))
and classno=(select classno from class where cname='一班');

  8.查询所有学生记录中成绩前十名的学生的学号,姓名,成绩,班级编号,代码如下所示:

select stuno,sname,score,classno from
(select stuno,sname,score,classno from student order by score desc)
where rownum<=10;

1.26.有两张表student和score:

Student:学号,姓名,性别,年龄;

Score:学号,语文,数学,英语;

1.查询张三的学号,姓名,性别,语文,数学,英语;

2.查询语文比数学好的同学;

3.查出姓名相同的学生学号.

参考答案:

  在oracle数据库中,创建测试表和测试数据,代码如下所示:

create table student(
    stuno number(8) primary key,
    sname varchar2(12),
    sex char(2) default '男',
    age int
);

create table score(
    stuno number(8),
    chinese number(3),
    math number(3),
    english number(3)
);

insert into student(stuno,sname,sex,age) values(1,'张三','男',22);
insert into student(stuno,sname,sex,age) values(2,'李四','男',21);
insert into student(stuno,sname,sex,age) values(3,'王五','男',23);
insert into student(stuno,sname,sex,age) values(4,'王五','女',23);

insert into score(stuno,chinese,english,math) values(1,70,80,90);
insert into score(stuno,chinese,english,math) values(2,60,80,70);
insert into score(stuno,chinese,english,math) values(3,70,85,95);
insert into score(stuno,chinese,english,math) values(4,98,85,95);

  1.查询张三的学号,姓名,性别,语文,数学,英语,SQL语句如下所示:

select s.stuno,sname,sex,chinese,math,english from student s join score c on s.stuno=c.stuno and sname='张三';

  2.查询语文比数学好的同学,SQL语句如下所示:

select s.stuno,sname,sex,chinese,math from student s join score c on s.stuno=c.stuno and chinese>math;

  3.查出姓名相同的学生学号,SQL语句如下所示:

select stuno from student where sname in(select sname from student group by sname having count(sname)>1);

1.27.对一个用户登录模块,要求每个用户只允许3次登录错误,超过则将锁定此账户

参考答案:

  基于数据库,在数据库中增加两个字段,分别记录次数和第三次登录的时间,当大于3的那次登录时,判定当前时间和记录的第三次的时间,如果在10分钟内,则依然不能登录.如果超了10分钟,则使得次数和时间重置.
  锁定后可以通过以下方式解锁,可以给每个账户都设一把密钥,当其中任何一个账户被锁定时,系统会自动以短信+电子邮件的形式生成一个随机的密钥发送到对应的用户,该用户想解锁可以等待时间过时,也可以重新输入密钥解锁.也就是说,只要被锁定了以后,除非用户名+密码+密钥全部通过才能解锁.

1.28.数据脚本

create table test1(
    pici VARCHAR2(30),
    busicode VARCHAR2(50),
    amt NUMBER,
    flag VARCHAR2(1)
);
--Add comments to the columns comment on column test1.flag is '1表示成功 2表示失败';

insert into test1 values('20130201','020111',10,1);
insert into test1 values('20130201','020112',5,2);
insert into test1 values('20130201','020113',10,2);
insert into test1 values('20130201','020114',5,1);

insert into test1 values('20130202','020211',10,1);
insert into test1 values('20130202','020212',20,1);
insert into test1 values('20130202','020213',20,1);
insert into test1 values('20130202','020214',20,1);

insert into test1 values('20130203','020311',10,2);
insert into test1 values('20130203','020311',10,2);
insert into test1 values('20130203','020311',10,2);

实现要求:标识位flag,1表示扣款成功,2表示扣款失败,使用一个SQL查询出每天扣款成功笔数,成功金额,失败笔数,失败金额.

参考答案:

  查询每天扣款成功笔数,成功金额,失败笔数,失败金额的SQL语句如下所示:

select to_date(pici,'yyyymmdd') as "日期",
    sum(case when flag=1 then 1 else 0 end) as "成功笔数",
    sum(case when flag=1 then amt else 0 end) as "成功金额",
    sum(case when flag=2 then 1 else 0 end) as "失败笔数",
    sum(case when flag=2 then amt else 0 end) as "失败金额"
    from test1
    GROUP by to_date(pici,'yyyymmdd');

1.29.数据库题

1.创建sms表的语句;

2.写出users与sms左关联的查询语句.

参考答案:

  1.在oracle数据库中,创建sms表和users表的SQL语句如下:

create table sms(
    id number primary key,
    sname varchar2(20)
);

create talbe users(
    id number primary key,
    username varchar2(20),
    smsid number references sms(id)
);

  2.users与sms左关联的查询语句的SQL语句如下:

select sname,username from users u left join sms s on u.smsid=s.id;

1.30.书表(books)

books表字段及测试数据

book_id book_name creatdate Lastmodifydate decription
001 三个人的世界 2005-02-02 2005-07-07 NULL

作者表(authors)字段及测试数据

a_id a_name
01 王纷
02 李尚
03 泰和

部门表(depts)字段及测试数据

d_id d_name
001 编辑一部
002 编辑二部
003 编辑三部

书和作者关联表(bookmap)字段及测试数据

d_id a_id
001 01
001 02
001 03

  查询每个部门的所写的总书量,比如:一本书有3个人写,如果三个人在不同的部门,则每个部门的总书量则为1,最后的结果如下:

   部门  书量
  编辑一部  1
  编辑二部  1
  编辑三部  1

参考答案:

  在oracle数据库中,创建测试表和测试数据,代码如下所示:

create table books(
    book_id number(20) primary key,
    book_name varchar2(200),
    creatdate varchar2(200),
    Lastmodifydate varchar2(200),
    decription varchar2(200)
);

insert into books(book_id,book_name,creatdate,Lastmodifydate,decription) values('001','三个人的世界','2005-02-02','2005-07-07','NULL');

create table authors(
A_id number(20) primary key,
A_name varchar2(200)
);

insert into authors(a_id,a_name) values('01','王纷');
insert into authors(a_id,a_name) values('02','李尚');
insert into authors(a_id,a_name) values('03','泰和');

create table depts(
    d_id number(20) primary key,
    d_name varchar2(200)
);

insert into depts(d_id,d_name) values('001','编辑一部');
insert into depts(d_id,d_name) values('001','编辑二部');
insert into depts(d_id,d_name) values('001','编辑三部');

create table bookmap(
    book_id number(20) references books(book_id),
    a_id number(20) references authors(a_id)
);

insert into bookmap(book_id,a_id) values(001,01);
insert into bookmap(book_id,a_id) values(001,02);
insert into bookmap(book_id,a_id) values(001,03);

create table depmap(
    d_id number(20) references depts(d_id),
    a_id number(20) references authors(a_id)
);

insert into depmap(d_id,a_id) values(001,01);
insert into depmap(d_id,a_id) values(002,02);
insert into depmap(d_id,a_id) values(003,03);

  查询出每个部门的所写的总书量的SQL语句如下所示:

select a.d_name "部门",count(c.book_id) "书量"
from depts a,auhors b,books c,depmap d,bookmap e
where a.d id=d.d id
and d.a id=b.a id
and c.book id=e.book id
and e.a id=b.a id
GROUP BY a.d_name;

你可能感兴趣的:(java面试题)