面试题: 数据库未看12

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;

猜你喜欢

转载自www.cnblogs.com/shan1393/p/9117771.html
今日推荐