1. 程式人生 > >面試題: 數據庫未看12

面試題: 數據庫未看12

外連接 dex 原子 order tex get ring 連接查詢 VG

1、Sql 約束

http://www.cnblogs.com/henw/archive/2012/08/15/2639510.html

2、修改列類型

MySQL:ALTER TABLE tableName modify column columnName 類型;
Oracle:ALTER TABLE tableName modify(columnName 類型);
  • 1
  • 2

3、聯合索引最左前綴原則

例如在表(name,age,birth)中建立聯合索引name_age_birth,在查詢中,where中的條件:

name and age and birth,
name and age,
name
  • 1
  • 2
  • 3

都會使用索引,而其它情況都不會使用索引
所以:當建立聯合索引時,要分析哪個字段在where中是最常用到的,應該放到聯合索引的最左邊。
http://www.cnblogs.com/jamesbd/p/4333901.html

4、ACID

A:原子性,事務操作必須是一個原子性操作,要麽全部完成,要麽全部不完成
C:一致性,事務開始前和事務結束後,數據庫的完整性不受到影響
I:隔離性,事務與事務之間不受到影響
D:持久性,事務完成後,將會持久性得保存到數據庫中

5、索引的類型

主鍵索此 ( PRIMARY )
唯一索引 ( UNIQUE )
普通索引 ( INDEX )
全文索引(FULLTEXT , MYISAM 及 mysql 5.6 以上的 Innodb )

6、內連接,左外連接,右外鏈接,全連接

假如A表和B表做連接
內連接:返回A和B的公共部分C
左外連接:返回C + A中不匹配的部分,B中以Null表示
右外連接:返回C + B中不匹配的部分,A中以Null表示
全連接(FULL JOIN):返回A和B中所有的部分

詳細:http://www.jb51.net/article/39432.htm

7、帶有in的子查詢(cvte面試時問過類似的)

eg:查詢與“劉晨”在同一個系學習的學生
使用子查詢:

select Sno,Sname,Sdept from Student where Sdept in(
    select Sdept from Student where Sname = ‘劉晨‘);
  • 1
  • 2

使用連接查詢:

select s1.Sno, s1.Sname, s1.Sdept from Student s1, Student s2 where s1.Sdept = s2.Sdept and s2.Sname = ‘劉晨‘;
  • 1

8、group by 和 having

groupby,然後對結果進行篩選的方式:
1. group by用於對查詢結果分組,having可以對分許結果進行篩選
2. 先where 再 group by
eg:查詢平均成績>=90分的學生學號和平均成績
正確示例:

select Sno, avg(Grage) from SC group by Sno 
having avg(Grage) >= 90;
  • 1
  • 2

錯誤示例:因為聚集函數不能用於where

select Sno, avg(Grage) from SC where avg(Grade) >= 90
group by Sno;
  • 1
  • 2

當出現聚集函數,不能group by 然後where

9、Group by 、Order By連用

今天去面試,遇到下面的題:
USER(u_id, name, nick_name)用戶信息表
USER_SCORE(s_id, u_id, score, create_time)用戶積分消耗表

q1:查找積分消耗前10的用戶信息和積分消耗總額

select u.u_id, u.name, u.nick_name , sum(score) scores 
from user u, user_score us where u.u_id = us.u_id 
group by u.u_id order by scores desc;
  • 1
  • 2
  • 3

q2:查找u_id為1的用戶信息和積分消耗總額

select u.u_id, u.name, u.nick_name , sum(score) scores 
from user u, user_score us 
where u.u_id = 1;

面試題: 數據庫未看12