面试积累 数据库篇(一)

创建表:

create table emp (id int primary key auto_increment,
name varchar(50),
salary bigint,
deptid int
)

插入实验数据:

insert into emp values(null,'zs',1000,1),(null,'ls',1100,1),(null
,'ww',1100,1),(null,'zl',900,1) ,(null,'zl',1000,2), (null,'zl',900,2) ,(null,'z
l',1000,2) , (null,'zl',1100,2);

1、根据部门号从高到低,工资从低到高列出每个员工的信息。

select * from emp order by deptid desc,sal;

2、列出各个部门中工资高于本部门的平均工资的员工数和部门号,并按部门号排序

 select t1.deptid,count(t1.id) from emp t1 where salary>
(select svg(salary) from emp e2 where e2.deptid=e1.deptid)
存储过程
//定义
create procedure insert_emp(name varchar(50),sallary bigint,deptid int,out all int)
begin  
insert into emp values(null,name ,bigint,deptid);
select count(*) into all from emp;
end;
//调用
call insert_emp("菜老板",8000,1,@all);
select @all;
数据库的三大范式:

第一范式:字段具有原子性 即 字段名不能再分
第二范式:每个列有主键,实体的属性完全依赖于主键
第三范式:要求一个数据库表中不包含已在其它表中已包含的非主关键字信息。例如,帖子表中只能出现发帖人的id而不能出现发帖人的id,还同时出现发帖人姓名,否则,只要出现同一发帖人id的所有记录,它们中的姓名部分都必须严格保持一致,这就是数据冗余。

说出一些数据库优化方面的经验?

1、如果是重复性的sql语句,采用PreparedStatement 会比Statement性能高,而且PreparedStatement 能够防SQL注入

2、下面的子查询语句要比第二条关联查询的效率高:

 select e.name,e.salary where e.managerid=(select id from employee where name='zxx');

  select e.name,e.salary,m.name,m.salary from employees e,employees m where
 e.managerid = m.id and m.name='zxx';

3、姓名和密码单独从用户表中独立出来。
面向对象面考虑, 用户信息就是用户这个人,用户名密码就是进门的钥匙
性能方面考虑,数据检索的时候列少了要快些,而且密码这东西,登录进去就没用了
安全性考虑,如果有一个模块查询用户信息,你直接把密码也带过去了,会容易出现恶意操作,

union和union all有什么不同?
假设我们有一个表Student,包括以下字段与数据:
drop table student;
create table student
(
id int primary key,
name nvarchar2(50) not null,
score number not null
);
insert into student values(1,'Aaron',78);
insert into student values(2,'Bill',76);
insert into student values(3,'Cindy',89);
insert into student values(4,'Damon',90);
insert into student values(5,'Ella',73);
insert into student values(6,'Frado',61);
insert into student values(7,'Gill',99);
insert into student values(8,'Hellen',56);
insert into student values(9,'Ivan',93);
insert into student values(10,'Jay',90);
commit;
Union和Union All的区别。 
select *
from student
where id < 4
union
select *
from student
where id > 2 and id < 6
结果将是
1    Aaron    78
2    Bill    76
3    Cindy    89
4    Damon    90
5    Ella    73

如果换成Union All连接两个结果集,则返回结果是:
1    Aaron    78
2    Bill    76
3    Cindy    89
3    Cindy    89
4    Damon    90
5    Ella    73
可以看到,Union和Union All的区别之一在于对重复结果的处理。
UNION在进行表链接后会筛选掉重复的记录,
所以在表链接后会对所产生的结果集进行排序运算,
删除重复的记录再返回结果。实际大部分应用中是不会产生重复的记录,最常见的是过程表与历史表UNION。如:
select * from gc_dfys
union
select * from ls_jg_dfys
这个SQL在运行时先取出两个表的结果,
再用排序空间进行排序删除重复的记录,
最后返回结果集,如果表数据量大的话可能会导致用磁盘进行排序。

 而UNION ALL只是简单的将两个结果合并后就返回。这样,如果返回的两个结果集中有重复的数据,
那么返回的结果集就会包含重复的数据了。
 从效率上说,UNION ALL 要比UNION快很多,所以,
如果可以确认合并的两个结果集中不包含重复的数据的话,那么就使用UNION ALL


分页语句

取出sql表中第31到40的记录(以自动增长ID为主键)

mysql方案:select * from t order by id limit 30,10
oracle方案:select *from(select rownum r,*from emp where r<=40)where r>30;

用一条SQL语句 查询出每门课都大于80分的学生姓名
name   kecheng   fenshu 
张三     语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90

创建表
create table score(id int primary key auto_increment,name varchar(20),subject varchar(20),score int);
insert into score values 
(null,'张三','语文',81),
(null,'张三','数学',75),
(null,'李四','语文',76),
(null,'李四','数学',90),
(null,'王五','语文',81),
(null,'王五','数学',100),
(null,'王五 ','英语',90);

A: select distinct name from score  
where  name not in (select distinct name from score where score<=80)

B:select distince name t1 from score 
where 80< all (select score from score where name=t1);

你可能感兴趣的:(面试积累 数据库篇(一))