Oracle數(shù)據(jù)庫面試題

(一)Oracle數(shù)據(jù)庫

1.oraclerow_id理解

ORACLE的row_id是一個偽列,其個是為18個字節(jié)可將這18個字節(jié)用6363來劃分,分別表示段編號,數(shù)據(jù)文件編號,數(shù)據(jù)塊

2.嵌入式數(shù)據(jù)庫和傳統(tǒng)數(shù)據(jù)庫的區(qū)別【大唐動力面試題】

嵌入式數(shù)據(jù)庫主要像:SQLite、

傳統(tǒng)數(shù)據(jù)庫服務(wù)器:SQL Server、Oracle、MySQL

嵌入式數(shù)據(jù)庫:SQLite的主要特點:

1.支持事件,不需要配置,不需要安裝,也不需要管理員;

2.支持大部分SQL92;

3.一個完整的數(shù)據(jù)庫保存在磁盤上面一個文件,同一個數(shù)據(jù)庫文件可以在不同機器上面使用,最大支持?jǐn)?shù)據(jù)庫到2T,字符和BLOB的支持僅限制于可用內(nèi)存;

4.整個系統(tǒng)少于3萬行代碼,少于250KB的內(nèi)存占用(gcc),大部分應(yīng)用比目前常見的客戶端/服務(wù)端的數(shù)據(jù)庫快,沒有其它依賴

5.源代碼開放,代碼95%有較好的注釋,簡單易用的API。官方帶有TCL的編譯版本。

關(guān)系數(shù)據(jù)庫特點:

1、更好的安全性、多用戶管理

2、強大的數(shù)據(jù)管理能力,如索引、視圖等關(guān)系對象

3、強大的數(shù)據(jù)庫編程式的設(shè)計,像T-SQL、存儲過程、游標(biāo)

4、豐富的數(shù)據(jù)類型

其它略。。。。

3.Inserteddeleted的含義?

inserted表反映插入或更新操作時插入的記錄

deleted表反映刪除或更新操作時刪除的記錄

4.函數(shù)和過程的區(qū)別?

?存儲過程:

1)一般用于在數(shù)據(jù)庫中完成特定的業(yè)務(wù)或任務(wù)

2)可以定義返回類型,也可以不定義返回類型

3)SQL語句中不可以調(diào)用

?函數(shù):

1)一般用于特定的數(shù)據(jù)查詢或數(shù)據(jù)轉(zhuǎn)轉(zhuǎn)換處理

2)申請時必須要定義返回類型,且程序體中必須定義return語句。

3)不能獨立執(zhí)行,必須作為表達(dá)式的一部分調(diào)用

4)SQL語句中可以調(diào)用。

5.數(shù)據(jù)庫優(yōu)化的方案

建立主鍵,為數(shù)據(jù)庫創(chuàng)建索引,建立存儲過程,觸發(fā)器,可提高查詢速度。

6.Oracle中有哪幾種索引

1.單列索引與復(fù)合索引

一個索引可以由一個或多個列組成,用來創(chuàng)建索引的列被稱為“索引列”。

單列索引是基于單列所創(chuàng)建的索引,復(fù)合索引是基于兩列或者多列所創(chuàng)建的索引。

2.唯一索引與非唯一索引

唯一索引是索引列值不能重復(fù)的索引,非唯一索引是索引列可以重復(fù)的索引。

無論是唯一索引還是非唯一索引,索引列都允許取NULL值。默認(rèn)情況下,Oracle創(chuàng)建的索引是不唯一索引。

3.B樹索引

B樹索引是按B樹算法組織并存放索引數(shù)據(jù)的,所以B樹索引主要依賴其組織并存放索引數(shù)據(jù)的算法來實現(xiàn)快速檢索功能。

4.位圖索引

位圖索引在多列查詢時,可以對兩個列上的位圖進行AND和OR操作,達(dá)到更好的查詢效果。

5.函數(shù)索引

Oracle中不僅能夠直接對表中的列創(chuàng)建索引,還可以對包含列的函數(shù)或表達(dá)式創(chuàng)建索引,這種索引稱為“位圖索引

7.數(shù)據(jù)庫索引的優(yōu)點和缺點【首航財務(wù)】

優(yōu)點:

1、通過創(chuàng)建唯一性索引,可以保證數(shù)據(jù)庫表中每一行數(shù)據(jù)的唯一性。

2、可以大大加快數(shù)據(jù)的檢索速度,這也是創(chuàng)建索引的最主要的原因。

3、可以加速表和表之間的連接,特別是在實現(xiàn)數(shù)據(jù)的參考完整性方面特別有意義。

4、在使用分組和排序子句進行數(shù)據(jù)檢索時,同樣可以顯著減少查詢中分組和排序的時間。

5、通過使用索引,可以在查詢的過程中,使用優(yōu)化隱藏器,提高系統(tǒng)的性能。

缺點:

1、創(chuàng)建索引和維護索引要耗費時間,這種時間隨著數(shù)據(jù)量的增加而增加。

2、索引需要占物理空間,除了數(shù)據(jù)表占數(shù)據(jù)空間之外,每一個索引還要占一定的物理空間,如果要建立聚簇索引,那么需要的空間就會更大。

3、當(dāng)對表中的數(shù)據(jù)進行增加、刪除和修改的時候,索引也要動態(tài)的維護,這樣就降低了數(shù)據(jù)的維護速度。

8.觸發(fā)器有幾種?

共2種,一種DML觸發(fā),就是遇到DML事件時觸發(fā)執(zhí)行,像insert\update\delete。一種DDL觸發(fā),遇到DDL事件時觸發(fā),像Login Datatabase、更改數(shù)據(jù)庫狀態(tài)、create語句等。

9.oracle中除了數(shù)據(jù)庫備份,還有什么方法備份?

Oracle數(shù)據(jù)庫有三種標(biāo)準(zhǔn)的備份方法,它們分別是導(dǎo)出/導(dǎo)入(EXP/IMP)、熱備份和冷備份。導(dǎo)出備份是一種邏輯備份,冷備份和熱備份是物理備份。

10G有幾種新功能進行備份,像數(shù)據(jù)磅

10.寫出刪除表中重復(fù)記錄的語句oracle

delete from people

where peopleId in (select ??peopleId from people group by ??peopleId ??having count(peopleId) > 1) ?and rowid not in (select min(rowid) from ??people group by

11.數(shù)據(jù)庫里面游標(biāo),索引是怎么用的?

declare cur cursor keyset for

get返回null,load classnotfoundException

12..Oracle中數(shù)據(jù)庫中的一個表中,這個表沒有主鍵id也沒有特定標(biāo)示來查數(shù)據(jù),怎么查?

利用偽列標(biāo)識進行查詢。

13.用兩種方式根據(jù)部門號從高到低,工資從低到高列出每個員工的信息。

employee:

eid,ename,salary,deptid;

select * from employee order by deptid desc,salary

14.列出各個部門中工資高于本部門的平均工資的員工數(shù)和部門號,并按部門號排序?!旧虾e\星信息面試題】

select a.ename,a.salary,a.deptid

from emp a,

(select deptd,avg(salary) avgsal from emp group by deptid ) b

where a.deptid=b.deptid and a.salary>b.avgsal ;

15.unionunion all有什么不同?

假設(shè)我們有一個表Student,包括以下字段與數(shù)據(jù):

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;

UnionUnion All的區(qū)別。

select *

from student

where id < 4

union

select *

from student

where id > 2 and id < 6

結(jié)果將是

1??? Aaron??? 78

2??? Bill??? 76

3??? Cindy??? 89

4??? Damon??? 90

5??? Ella??? 73

如果換成Union All連接兩個結(jié)果集,則返回結(jié)果是:

1??? Aaron??? 78

2??? Bill??? 76

3??? Cindy??? 89

3??? Cindy??? 89

4??? Damon??? 90

5??? Ella??? 73

可以看到,Union和Union All的區(qū)別之一在于對重復(fù)結(jié)果的處理。

UNION在進行表鏈接后會篩選掉重復(fù)的記錄,所以在表鏈接后會對所產(chǎn)生的結(jié)果集進行排序運算,刪除重復(fù)的記錄再返回結(jié)果。實際大部分應(yīng)用中是不會產(chǎn)生重復(fù)的記錄,最常見的是過程表與歷史表UNION。如:

select * from gc_dfys

union

select * from ls_jg_dfys

這個SQL在運行時先取出兩個表的結(jié)果,再用排序空間進行排序刪除重復(fù)的記錄,最后返回結(jié)果集,如果表數(shù)據(jù)量大的話可能會導(dǎo)致用磁盤進行排序。

而UNION ALL只是簡單的將兩個結(jié)果合并后就返回。這樣,如果返回的兩個結(jié)果集中有重復(fù)的數(shù)據(jù),那么返回的結(jié)果集就會包含重復(fù)的數(shù)據(jù)了。

從效率上說,UNION ALL要比UNION快很多,所以,如果可以確認(rèn)合并的兩個結(jié)果集中不包含重復(fù)的數(shù)據(jù)的話,那么就使用UNION ALL,

16.用一條SQL語句查詢出每門課都大于80分的學(xué)生姓名

name?? kecheng?? fenshu

張三語文81

張三數(shù)學(xué)75

李四語文76

李四數(shù)學(xué)90

王五語文81

王五數(shù)學(xué)100

王五英語90

準(zhǔn)備數(shù)據(jù)的sql代碼:

create table score(id int primary key auto_increment,name varchar(20),subject varchar(20),score int);

insert into score values

(null,'張三','語文',81),

(null,'張三','數(shù)學(xué)',75),

(null,'李四','語文',76),

(null,'李四','數(shù)學(xué)',90),

(null,'王五','語文',81),

(null,'王五','數(shù)學(xué)',100),

(null,'王五','英語',90);

提示:當(dāng)百思不得其解時,請理想思維,把小變成大做,把大變成小做,

答案:

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);

17.所有部門之間的比賽組合

一個叫department的表,里面只有一個字段name,一共有4條紀(jì)錄,分別是a,b,c,d,對應(yīng)四個球?qū)?,現(xiàn)在四個球?qū)M行比賽,用一條sql語句顯示所有可能的比賽組合.

答:select a.name, b.name

from team a, team b

where a.name < b.name

18.每個月份的發(fā)生額都比101科目多的科目

請用SQL語句實現(xiàn):從TestDB數(shù)據(jù)表中查詢出所有月份的發(fā)生額都比101科目相應(yīng)月份的發(fā)生額高的科目。請注意:TestDB中有很多科目,都有1-12月份的發(fā)生額。

AccID:科目代碼,Occmonth:發(fā)生額月份,DebitOccur:發(fā)生額。

數(shù)據(jù)庫名:JcyAudit,數(shù)據(jù)集:Select * from TestDB

準(zhǔn)備數(shù)據(jù)的sql代碼:

drop table if exists TestDB;

create table TestDB(id int primary key auto_increment,AccID varchar(20), Occmonth date, DebitOccur bigint);

insert into TestDB values

(null,'101','1988-1-1',100),

(null,'101','1988-2-1',110),

(null,'101','1988-3-1',120),

(null,'101','1988-4-1',100),

(null,'101','1988-5-1',100),

(null,'101','1988-6-1',100),

(null,'101','1988-7-1',100),

(null,'101','1988-8-1',100);

--復(fù)制上面的數(shù)據(jù),故意把第一個月份的發(fā)生額數(shù)字改小一點

insert into TestDB values

(null,'102','1988-1-1',90),

(null,'102','1988-2-1',110),

(null,'102','1988-3-1',120),

(null,'102','1988-4-1',100),

(null,'102','1988-5-1',100),

(null,'102','1988-6-1',100),

(null,'102','1988-7-1',100),

(null,'102','1988-8-1',100);

--復(fù)制最上面的數(shù)據(jù),故意把所有發(fā)生額數(shù)字改大一點

insert into TestDB values

(null,'103','1988-1-1',150),

(null,'103','1988-2-1',160),

(null,'103','1988-3-1',180),

(null,'103','1988-4-1',120),

(null,'103','1988-5-1',120),

(null,'103','1988-6-1',120),

(null,'103','1988-7-1',120),

(null,'103','1988-8-1',120);

--復(fù)制最上面的數(shù)據(jù),故意把所有發(fā)生額數(shù)字改大一點

insert into TestDB values

(null,'104','1988-1-1',130),

(null,'104','1988-2-1',130),

(null,'104','1988-3-1',140),

(null,'104','1988-4-1',150),

(null,'104','1988-5-1',160),

(null,'104','1988-6-1',170),

(null,'104','1988-7-1',180),

(null,'104','1988-8-1',140);

--復(fù)制最上面的數(shù)據(jù),故意把第二個月份的發(fā)生額數(shù)字改小一點

insert into TestDB values

(null,'105','1988-1-1',100),

(null,'105','1988-2-1',80),

(null,'105','1988-3-1',120),

(null,'105','1988-4-1',100),

(null,'105','1988-5-1',100),

(null,'105','1988-6-1',100),

(null,'105','1988-7-1',100),

(null,'105','1988-8-1',100);

答案:

select distinct AccID from TestDB

where AccID not in

(select TestDB.AccIDfrom TestDB,

(select * from TestDB where AccID='101') as db101

where TestDB.Occmonth=db101.Occmonth and TestDB.DebitOccur<=db101.DebitOccur

);

19.統(tǒng)計每年每月的信息

year??month amount

1991?? 1???? 1.1

1991?? 2???? 1.2

1991?? 3???? 1.3

1991?? 4???? 1.4

1992?? 1???? 2.1

1992?? 2???? 2.2

1992?? 3???? 2.3

1992?? 4???? 2.4

查成這樣一個結(jié)果

year m1??m2??m3??m4

1991 1.1 1.2 1.3 1.4

1992 2.1 2.2 2.3 2.4

提示:這個與工資條非常類似,與學(xué)生的科目成績也很相似。

準(zhǔn)備sql語句:

drop table if exists sales;

create table sales(id int auto_increment primary key,year varchar(10), month varchar(10), amount float(2,1));

insert into sales values

(null,'1991','1',1.1),

(null,'1991','2',1.2),

(null,'1991','3',1.3),

(null,'1991','4',1.4),

(null,'1992','1',2.1),

(null,'1992','2',2.2),

(null,'1992','3',2.3),

(null,'1992','4',2.4);

答案一、

select sales.year ,

(select t.amount from sales t where t.month='1' and t.year= sales.year) '1',

(select t.amount from sales t where t.month='1' and t.year= sales.year) '2',

(select t.amount from sales t where t.month='1' and t.year= sales.year) '3',

(select t.amount from sales t where t.month='1' and t.year= sales.year) as '4'

from sales ?group by year;

20.顯示文章標(biāo)題,發(fā)帖人、最后回復(fù)時間

表:id,title,postuser,postdate,parentid

準(zhǔn)備sql語句:

drop table if exists articles;

create table articles(id int auto_increment primary key,title varchar(50), postuser varchar(10), postdate datetime,parentid int references articles(id));

insert into articles values

(null,'第一條','張三','1998-10-10 12:32:32',null),

(null,'第二條','張三','1998-10-10 12:34:32',null),

(null,'第一條回復(fù)1','李四','1998-10-10 12:35:32',1),

(null,'第二條回復(fù)1','李四','1998-10-10 12:36:32',2),

(null,'第一條回復(fù)2','王五','1998-10-10 12:37:32',1),

(null,'第一條回復(fù)3','李四','1998-10-10 12:38:32',1),

(null,'第二條回復(fù)2','李四','1998-10-10 12:39:32',2),

(null,'第一條回復(fù)4','王五','1998-10-10 12:39:40',1);

答案:

select a.title,a.postuser,

(select max(postdate) from articles where parentid=a.id) reply

from articles a where a.parentid is null;

21.刪除除了id號不同,其他都相同的學(xué)生冗余信息

2.學(xué)生表如下:

id號學(xué)號姓名課程編號課程名稱分?jǐn)?shù)

1??????? 2005001張三0001數(shù)學(xué)69

2??????? 2005002李四0001數(shù)學(xué)89

3??????? 2005001張三0001數(shù)學(xué)69

A: delete from tablename where id號not in(select min(id號) from tablename group by學(xué)號,姓名,課程編號,課程名稱,分?jǐn)?shù))

實驗:

create table student2(id int auto_increment primary key,code varchar(20),name varchar(20));

insert into student2 values(null,'2005001','張三'),(null,'2005002','李四'),(null,'2005001','張三');

//如下語句,mysql報告錯誤,可能刪除依賴后面統(tǒng)計語句,而刪除又導(dǎo)致統(tǒng)計語句結(jié)果不一致。

delete from student2 where id not in(select min(id) from student2 group by name);

//但是,如下語句沒有問題:

select * ?from student2 where id not in(select min(id) from student2 group by name);

//于是,我想先把分組的結(jié)果做成虛表,然后從虛表中選出結(jié)果,最后再將結(jié)果作為刪除的條件數(shù)據(jù)。

delete from student2 where id not in(select mid from (select min(id) mid

from student2 group by name) as t);

或者:

delete from student2 where id not in(select min(id) from (select * from s

tudent2) as t group by t.name);

22.航空網(wǎng)的幾個航班查詢題:

表結(jié)構(gòu)如下:

flight{flightID,StartCityID ,endCityID,StartTime}

city{cityID, CityName)

實驗環(huán)境:

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);

//航班本來應(yīng)該沒有日期部分才好,但是下面的題目當(dāng)中涉及到了日期

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、查詢起飛城市是北京的所有航班,按到達(dá)城市的名字排序

參與運算的列是我起碼能夠顯示出來的那些列,但最終我不一定把它們顯示出來。各個表組合出來的中間結(jié)果字段中必須包含所有運算的字段。

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 wh

ere flight.endcityid=e.cityid and flight.startcityid=(select cityid from city wh

ere cityname='北京');

mysql> select flight.flightid,s.cityname,e.cityname from flight,city s,city e wh

ere flight.startcityid=s.cityid and s.cityname='北京' and flight.endCityId=e.cit

yID order by e.cityName desc;

2、查詢北京到上海的所有航班紀(jì)錄(起飛城市,到達(dá)城市,起飛時間,航班號)

select c1.CityName,c2.CityName,f.StartTime,f.flightID

from city c1,city c2,flight f

where f.StartCityID=c1.cityID

and f.endCityID=c2.cityID

and c1.cityName='北京'

and c2.cityName='上海'

3、查詢具體某一天(2005-5-8)的北京到上海的的航班次數(shù)

select count(*) from

(select c1.CityName,c2.CityName,f.StartTime,f.flightID

from city c1,city c2,flight f

where f.StartCityID=c1.cityID

and f.endCityID=c2.cityID

and c1.cityName='北京'

and c2.cityName='上海'

and查幫助獲得的某個日期處理函數(shù)(startTime) like '2005-5-8%'

mysql中提取日期部分進行比較的示例代碼如下:

select * from flight where date_format(starttime,'%Y-%m-%d')='1998-01-02'

23.查出比經(jīng)理薪水還高的員工信息:

select e.* from employees e,employees m where e.managerid=m.id and e.sala

ry>m.salary;

24.求出小于45歲的各個老師所帶的大于12歲的學(xué)生人數(shù)

數(shù)據(jù)庫中有3個表teacher表,student表,tea_stu關(guān)系表。

teacher表teaID name age

student表stuID name age

teacher_student表teaID stuID

要求用一條sql查詢出這樣的結(jié)果

1.顯示的字段要有老師name, age每個老師所帶的學(xué)生人數(shù)

2只列出老師age為40以下,學(xué)生age為12以上的記錄

預(yù)備知識:

1.sql語句是對每一條記錄依次處理,條件為真則執(zhí)行動作(select,insert,delete,update)

2.只要是迪卡爾積,就會產(chǎn)生“垃圾”信息,所以,只要迪卡爾積了,我們首先就要想到清除“垃圾”信息

實驗準(zhǔn)備:

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);

結(jié)果:2à3,3à2,4à3

解題思路:

1要會統(tǒng)計分組信息,統(tǒng)計信息放在中間表中:

select teaid,count(*) from tea_stu group by teaid;

2接著其實應(yīng)該是篩除掉小于12歲的學(xué)生,然后再進行統(tǒng)計,中間表必須與student關(guān)聯(lián)才能得到12歲以下學(xué)生和把該學(xué)生記錄從中間表中剔除,代碼是:

select tea_stu.teaid,count(*) total from student,tea_stu

where student.stuid=tea_stu.stuid and student.age>12 group by tea_stu.teaid

3.接著把上面的結(jié)果做成虛表與teacher進行關(guān)聯(lián),并篩除大于45的老師

select teacher.teaid,teacher.name,total from teacher ,(select tea_stu.tea

id,count(*) total from student,tea_stu where student.stuid=tea_stu.stuid and stu

dent.age>12 group by tea_stu.teaid) as tea_stu2where teacher.teaid=tea_stu2.tea

id and teacher.age<45;

25.求出發(fā)帖最多的人:

select authorid,count(*) total from articles

group by authorid

having total=

(select max(total2) from (select count(*) total2 from articles group by authorid) as t);

select t.authorid,max(t.total) from

(select authorid,count(*) total from articles)as t

這條語句不行,因為max只有一列,不能與其他列混淆。

select authorid,count(*) total from articles

group by authorid having total=max(total)也不行。

26.一個用戶表中有一個積分字段,假如數(shù)據(jù)庫中有100多萬個用戶,若要在每年第一天凌晨將積分清零,你將考慮什么,你將想什么辦法解決?

alter table drop column score;

alter table add colunm score int;

可能會很快,但是需要試驗,試驗不能拿真實的環(huán)境來操刀,并且要注意,

這樣的操作時無法回滾的,在我的印象中,只有inert update delete等DML語句才能回滾,

對于create table,drop table ,alter table等DDL語句是不能回滾。

解決方案一,update user set score=0;

解決方案二,假設(shè)上面的代碼要執(zhí)行好長時間,超出我們的容忍范圍,那我就alter table user drop column score;alter table user add column score int。

下面代碼實現(xiàn)每年的那個凌晨時刻進行清零。

Runnable runnable =

new Runnable(){

public void run(){

clearDb();

schedule(this,new Date(new Date().getYear()+1,0,0));

}

};

schedule(runnable,

new Date(new Date().getYear()+1,0,1));

27.一個用戶具有多個角色,請查詢出該表中具有該用戶的所有角色的其他用戶。

select count(*) as num,tb.id

from

tb,

(select role from tb where id=xxx) as t1

where

tb.role = t1.role and tb.id != t1.id

group by tb.id

having

num = select count(role) from tb where id=xxx;

28.xxx公司的sql面試

Table EMPLOYEES Structure:

EMPLOYEE_ID ?????NUMBER ???????Primary Key,

FIRST_NAME ??????VARCHAR2(25),

LAST_NAME ??????VARCHAR2(25),

Salary number(8,2),

HiredDate DATE,

Departmentid number(2)

Table Departments Structure:

Departmentid number(2) ???????Primary Key,

DepartmentName ?VARCHAR2(25).

(2)基于上述EMPLOYEES表寫出查詢:寫出雇用日期在今年的,或者工資在[1000,2000]之間的,或者員工姓名(last_name)以’Obama’打頭的所有員工,列出這些員工的全部個人信息。(4分)

select * from employees

where Year(hiredDate) = Year(date())

or (salary between 1000 and 200)

or left(last_name,3)='abc';

(3)基于上述EMPLOYEES表寫出查詢:查出部門平均工資大于1800元的部門的所有員工,列出這些員工的全部個人信息。(4分)

mysql> 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 t

where employee1.deptid = t.deptid and employee1.salary>t.avgSalary;

29.大數(shù)據(jù)量下的分頁解決方法?!君R谷網(wǎng)絡(luò)】

答:最好的辦法是利用sql語句進行分頁,這樣每次查詢出的結(jié)果集中就只包含某頁的數(shù)據(jù)內(nèi)容。再sql語句無法實現(xiàn)分頁的情況下,可以考慮對大的結(jié)果集通過游標(biāo)定位方式來獲取某頁的數(shù)據(jù)。

sql語句分頁,不同的數(shù)據(jù)庫下的分頁方案各不一樣,下面是主流的三種數(shù)據(jù)庫的分頁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 =

"select * from " +

(select *,rownum rid from (select * from students order by postime desc) where rid<=" + pagesize*pagenumber + ") as t" +

"where t>" + pageSize*(pageNumber-1);

30.寫一個用jdbc連接并訪問oracle數(shù)據(jù)的程序代碼

private final ?static String driverString = "oracle.jdbc.driver.OracleDriver";

private final static ?String url="jdbc:oracle:thin:@localhost:1521:orcl";

public static Connection getOracleDB()

{

Connection conn=null;

try {

Class.forName(driverString);

conn= DriverManager.getConnection(url,"scott","123");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

下載完整面試題文件及答案:

https://www.duyunwl.cn/2019/03/08/java面試題大全/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,625評論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,993評論 0 11
  • 50個常用的sql語句Student(S#,Sname,Sage,Ssex) 學(xué)生表Course(C#,Cname...
    哈哈海閱讀 1,326評論 0 7
  • 一、數(shù)據(jù)庫操作: 1.1 創(chuàng)建數(shù)據(jù)庫: create database student; 1.2 刪除數(shù)據(jù)庫: ...
    __71db閱讀 845評論 0 0
  • 最近,聽了同事麗紅講述她姐姐姐夫的婚姻,瞬間感覺自己如此幸運,遇到一個懂得心疼媳婦的老公。 婚姻若非天堂,...
    牽念閱讀 705評論 4 1

友情鏈接更多精彩內(nèi)容