百家互聯網QA面試題--數據庫

1、在MS SQL_Server 數據庫中通過什麼約束保證數據庫的實體完整性

5大約束:
NOT NULL
CHECK約束
UNIQUE 約束
PRIMARY KEY 約束
FOREIGN KEY 約束
如果一個外鍵值百沒有候選鍵,則不能插入帶該值(NULL 除外)的行。如果嘗試刪除度現有外鍵指向的行,ON DELETE 子句將控制版所採權取的操作。ON DELETE 子句有兩個選項:
·NO ACTION 指定刪除因錯誤而失敗。
·CASCADE 指定還將刪除包含指向已刪除行的外鍵的所有行。
如果嘗試更新現有外鍵指向的候選鍵值,ON UPDATE 子句將定義所採取的操作。它也支持 NO ACTION 和 CASCADE 選項。
有時用默認值和觸發器
約束優先權最高 默認值次之

2、學生表(學生id,姓名,性別,分數) )student(s_id, name, sex, score)

班級表(班級id,班級名稱) class(c_id, c_name)
學生班級表(班級id,學生id) student_class(s_id,c_id)
1)查詢一班得分在80分以上或者等於60,61,62的學生
2)査詢所有班級的名稱,和所有版中女生人數和女生的平均分。
(1)SELECT s.s_id,s.name,s.score,sc.c_id,c.c_name from student s LEFT JOIN student_class sc on s.s_id = sc.s_id LEFT JOIN class c on sc.c_id=c.c_id where (s.score>80 or s.score in(60,61,62)) and c.c_name=‘一班’;
(2)SELECT sc.s_id,c.c_name,COUNT(s.sex),AVG(s.score) from student_class sc LEFT JOIN class c on sc.c_id=c.c_id LEFT JOIN student s on sc.s_id = s.s_id where s.sex=‘女’ group BY c.c_name ORDER BY c.c_id asc;

3、sql語句應該考慮哪些安全性?

(1)防止sql注入,對特殊字符進行轉義,過濾或者使用預編譯的sql語句綁定變量。
(2)最小權限原則,特別是不要用root賬戶,爲不同的類型的動作或者組建使用不同的賬戶。
(3)當sql運行出錯時,不要把數據庫返回的錯誤信息全部顯示給用戶,以防止泄漏服務器和數據庫相關信息。

4、簡述sql優化思路

1)sql儘量使用索引,而且查詢要走索引
2)子查詢變成left join
3)limit 分佈優化,先利用ID定位,再分頁
4)or條件優化,多個or條件可以用union all對結果進行合併(union all結果可能重複)
5)不必要的排序
6)where代替having,having 檢索完所有記錄,才進行過濾
7)避免嵌套查詢
8)對多個字段進行等值查詢時,聯合索引

5、info 表

Date result

2005-05-09 win
2005-05-09 lose
2005-05-09 lose
2005-05-09 lose
2005-05-10 win
2005-05-10 lose
2005-05-10 lose
如果要生成下列結果, 該如何寫sql語句?
win lose
2005-05-09 2 2
2005-05-10 1 2

(1) select date , sum( case when result = “win” then 1 else 0 end ) as “win”,
sum(case when result = “lose” then 1 else 0 end) as “lose” from info group by date;
(2) select a.date, a.result as win, b.result as lose from
 (select date, count (result) as result from info where result = “win” group by date) as a
 join
 (select date, count (result) as result from info where result = “lose” group by date) as b
on a.date = b.date

6、表中有A B C三列,用SQL語句實現:當A列大於B列時選擇A列否則選擇B列,當B列大於C列時選擇B列否則選擇C列

select ( case when a > b then a else b end ), (case when b > c then b else c end ) from table

7、姓名:name 課程:subject 分數:score 學號:stuid

張三 數學 89 1
張三 語文 80 1
張三 英語 70 1
李四 數學 90 2
李四 語文 70 2
李四 英語 80 2
1)計算每個人的總成績並排名(要求顯示字段:姓名,總成績)
select name,sum(score) as allscore from stuscore group by name order by allscore
2)計算每個人的總成績並排名(要求顯示字段: 學號,姓名,總成績)
答案:select distinct t1.name,t1.stuid,t2.allscore from stuscore t1,( select stuid,sum(score) as allscore from stuscore group by stuid)t2where t1.stuid=t2.stuidorder by t2.allscore desc

3)計算每個人單科的最高成績(要求顯示字段: 學號,姓名,課程,最高成績)
答案:select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(select stuid,max(score) as maxscore from stuscore group by stuid) t2where t1.stuid=t2.stuid and t1.score=t2.maxscore

4)計算每個人的平均成績(要求顯示字段: 學號,姓名,平均成績)
答案:select distinct t1.stuid,t1.name,t2.avgscore from stuscore t1,(select stuid,avg(score) as avgscore from stuscore group by stuid) t2where t1.stuid=t2.stuid

5)列出各門課程成績最好的學生(要求顯示字段: 學號,姓名,科目,成績)
答案:select t1.stuid,t1.name,t1.subject,t2.maxscore from stuscore t1,(select subject,max(score) as maxscore from stuscore group by subject) t2where t1.subject=t2.subject and t1.score=t2.maxscore

6)列出各門課程成績最好的兩位學生(要求顯示字段: 學號,姓名,科目,成績)
答案:select distinct t1.* from stuscore t1 where t1.id in (select top 2 stuscore.id from stuscore where subject = t1.subject order by score desc) order by t1.subject

7)統計如下:學號 姓名 語文 數學 英語 總分 平均分
答案:select stuid as 學號,name as 姓名,sum(case when subject=’語文’ then score else 0 end) as 語文,sum(case when subject=’數學’ then score else 0 end) as 數學,sum(case when subject=’英語’ then score else 0 end) as 英語,sum(score) as 總分,(sum(score)/count(*)) as 平均分from stuscoregroup by stuid,name order by 總分desc

8)列出各門課程的平均成績(要求顯示字段:課程,平均成績)
答案:select subject,avg(score) as avgscore from stuscoregroup by subject

9)列出數學成績在2-3名的學生(要求顯示字段:學號,姓名,科目,成績)
答案:select t3.* from(select top 2 t2.* from (select top 3 name,subject,score,stuid from stuscore where subject=’數學’order by score desc) t2 order by t2.score) t3 order by t3.score desc
10)求出李四的數學成績的排名
答案:
declare @tmp table(pm int,name varchar(50),score int,stuid int)insert into @tmp select null,name,score,stuid from stuscore where subject=’數學’ order by score descdeclare @id intset @id=0;update @tmp set @id=@id+1,pm=@idselect * from @tmp where name=’李四’

11)統計如下:課程 不及格(0-59)個 良(60-80)個 優(81-100)個
答案:select subject, (select count() from stuscore where score<60 and subject=t1.subject) as 不及格,(select count() from stuscore where score between 60 and 80 and subject=t1.subject) as 良,(select count(*) from stuscore where score >80 and subject=t1.subject) as 優from stuscore t1 group by subject

12)統計如下:數學:張三(50分),李四(90分),王五(90分),趙六(76分)
答案:
declare @s varchar(1000)set @s=”select @s =@s+’,’+name+’(‘+convert(varchar(10),score)+’分)’ from stuscore where subject=’數學’ set @s=stuff(@s,1,1,”)print ‘數學:’+@s

用一條SQL 語句 查詢出每門課都大於80 分的學生姓名

name kecheng fenshu
張三 語文 81
張三 數學 75
李四 語文 76
李四 數學 90
王五 語文 81
王五 數學 100
王五 英語 90

A: select distinct name from table where name not in (select distinct name from table where fenshu<=80)
select name from table group by name having min(fenshu)>80

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章