随笔-159  评论-114  文章-7  trackbacks-0

上回,Select DISTINCT dept_id,title from s_emp,注意DISTINCT是作用于两列的组合。

Group Functions

分组函数

SELECT column,group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];

AVG(DISTINCT|ALL|n)
COUNT(DISTINCT|ALL|expr|*)
MAX(DISTINCT|ALL|expr)
MIN(DISTINCT|ALL|expr)
SUM(DISTINCT|ALL|n)


SQL> select AVG(salary),MAX(salary),
  2  MIN(salary),SUM(salary)
  3  from s_emp
  4  where UPPER(title) LIKE 'SALES%';

AVG(SALARY) MAX(SALARY) MIN(SALARY) SUM(SALARY)
----------- ----------- ----------- -----------
    1463.75        1515        1400        5855


SQL> select count(*)
  2  from s_emp
  3  where dept_id=31
  4  ;

  COUNT(*)
----------
         1

SQL> select count(commission_pct)
  2  from s_emp
  3  where dept_id = 31;

COUNT(COMMISSION_PCT)
---------------------
                    1

注意,count具体列,只统计非空数目。

  1  select dept_id,Count(*) "Number"
  2  from s_emp
  3* group by dept_id
SQL> /

   DEPT_ID     Number
---------- ----------
        31          1
        32          1
        33          1
        34          1
        35          1
        41          2
        42          2
        43          2
        44          2
        45          3
                      8

不写Group by,只是一组,

加了Group by,多组。

出现在Select中,非组函数的值,必须出现加在Group by中。可以不Select某列,但是group by该列,但结果当然也没有这一列,只是按照它来分组显示其他组函数数据。


组函数的判断不能在Where子句中,必须是在Having中。

SELECT title,12*AVG(salary) "ANNUAL SALARY",COUNT(*) "NUMBER OF EMPLOYEES"
FROM s_emp
GROUP BY title
HAVING COUNT(*)>2;

第一步,分组
第二步,应用组函数
第三步,匹配Having子句的组函数

select title,sum(salary) PALALL
FROM s_emp
WHERE title NOT LIKE "VP%"
GROUP BY title
HAVING SUM(salary) > 5000
ORDER BY SUM(salary);

Subqueries 子查询

select last_name,title
from s_emp
where title=
(select title
from s_emp
where last_name='Smith');

select last_name,title,salary
from s_emp
where salary<
(select avg(salary)
from s_emp);

自查询有多值返回时,使用IN关键字

select last_name,first_name,title
from s_emp
where dept_id IN
(select id
from s_dept
where name = 'Finance' or region_id = 2);

子查询也可以出现在Having中

Select dept_id,AVG(salary)
from s_emp
group by dept_id
having avg(salary)>
(select avg(salary)
from s_emp
where dept_id=32);

子查询有效率问题,既能够用连接又能用子查询时,请使用连接,一次查询快!

设置变量

SQL> select id,last_name,salary
  2  from s_emp
  3  where dept_id=&department_number;
Enter value for department_number:

where title = '&job_title';

一般用于插入数据时,进行定义。

可以保存一个.sql文件,在文件的开头设定变量,在执行insert,但sqlplus中,只保存sql命令,不保存sqlplus命令,DEFINE和ACCEPT是sqlplus命令。

DEFINE did=105(CHAR) 都是字符型。

insert into lujl11
values (&did,'&address',&salary);

使用ACCEPT命令来进行变量定义,可以区分类型,和添加提示符

ACCEPT p_dname PROMPT 'Provide the department name'
ACCEPT p_salary NUMBER PROMPT 'Salary amoount:'
ACCEPT pswd CHAR PROMPT 'Password:' HIDE

UNDEFINE或者退出sqlplus,会清楚变量。



SET VERIFY ON
察看新旧值开关。


数据库建模

立项
----〉需求分析
----------------〉设计,设计细化
----------------------------------〉建表,测试(逻辑测试,需求满足否,物理测试,数据量承受力),优化
--------------------------------------------〉真实数据,产品。

实线---必然的联系

虚线---可能的联系

范式

第一范式 解决单值问题。
第二范式 解决依赖性。
第三范式 解决非传递性问题。

例子:一想就明白了

经过需求分析,整理出下列属性。

product#,order#,name,date,price,quantity,customer#,customer_name,customer_address(Province,city,address))

INF分析后

(product#,order#,name,date,price,quantity,customer#,customer_name,Province,City,Address)

2NF分析后

(product#,name,price)
(order#,date,customer#,customer_name,Province,City,Address)
(product#,order#,quantity)

3NF分析后

(product#,name,price)
(order#,date,customer#)
(product#,order#,quantitiy)
(customer#,customer_name,Province,City,Address)





今天安装FC3,费了些时间,为能够在自己机器上也跑上Redhat,安装了Fedora Core 3,http://fedora.linuxsir.org/main/
不错的网站,我就是照着装上的,以后就可以研究一下了。哈哈











 



posted on 2005-11-22 00:05 北国狼人的BloG 阅读(390) 评论(0)  编辑  收藏 所属分类: 达内学习总结

只有注册用户登录后才能发表评论。


网站导航: