面试题-----数据库

1. 数据库连接池原理:

  数据库连接池的基本思想就是为数据库连接 建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个(busy状态),使用完毕之后再放回去(nobusy状态)。我们可以通过设定 连接池最大连接数来防止系统无尽的与数据库连接。

 

2. mysql--group by用法:

  一张表,一旦分组 完成后,查询后只能得到组相关的信息。

     组相关的信息:(统计信息) count(计数),sum(求和),max(最大),min(最小),avg(求平均值),nul(存在null的字段,0)  分组的标准)

    SQLServer中分组时:不能以text,ntext,image类型的字段作为分组依据

    select统计函数中的字段,不能和普通的字段放在一起;
 
  例子:
    
    求出发帖最多的人? 
 
    正确的写法如下: 
 
    select teaid,count(*) total from articles group by teaid having count(*) = 
 
    (select max(total2) from (select count(*) total2 from articles group by teaid) t); 
 
    记得不能写成如下这样哦: 
 
    select teaid,count(*) total from articles group by teaid 
 
    having total =    //total 一定要写成 count(*) 
 
    (select max(total2) from (select count(*) total2 from articles group by teaid) t); 
 
    下面这样写直接报错 
    select teaid,count(*) total from articles group by teaid having 
 
    count(*)=max(total) 
 
    也不应该写成: 
 
    select teaid,count(*) total from articles group by teaid 
 
    having total =    //total 一定要写成 count(*) 
 
    (select max(total2) from (select count(*) total2 from articles group by teaid) 
 
    as t);//子查询不能加as as只能用作表的别名 
 
    下面的写法也会报错 
 
    select teaid , count(*) c from articles group by teaid 
 
    having count(*) = select max(t.total) from (select teaid,count(*) total from articles group by teaid) t 
 
应该写成下面这样 
 
    select teaid , count(*) c from articles group by teaid 
 
    having count(*) = (select max(t.total) from (select teaid,count(*) total from 
 
    articles group by teaid) t)//应该加上括号  
 

3. 创建函数

create or replace function add_workday(p_date date, p_days number)
  return date is
  v_date  date;
  v_sign  number;
  v_sign1 number;
begin
  if p_days < -1000 or p_days > 1000 then
    v_date := to_date('1900-01-01', 'yyyy-mm-dd');
  else
    v_date := p_date + p_days;
    loop
      -- 循环 如果日期不符合要求就加一天,直到日期是工作日为止。
      select count(*) into v_sign from t_holiday where holiday = v_date;
      v_sign1 := to_number(to_char(v_date, 'd'));
      if v_sign = 0 and v_sign1 <> 6 and v_sign1 <> 7 then
        --判断是否为工作日
        exit;
      end if;
      v_date := v_date + 1;
    end loop;
  end if;
  return v_date;
 
end add_workday;

 

posted @ 2013-11-18 19:19  zl_说不得  阅读(182)  评论(0编辑  收藏  举报