Oracle 常用语句档案(二)

本文详细介绍了SQL中的各种联接类型,包括内联接、外联接等,并提供了具体的语法示例。此外,还讲解了如何使用子查询增强查询功能,以及数据操作语言(DML)的基本用法。

Displaying Data from Multiple Tables

Types of Joins

SQL:1999  Compliant Joins:

--Cross joins

--Natural joins

--Using clause

--Full or two sided outer joins

--Arbitrary join conditions for outer joins

Syntax:

Select tableA.column,tableB.column from tableA

[CROSS JOIN tableB] |

[NATURAL JOIN tableB] |

[JOIN tableB USING (column_name)] |

[JOIN tableB ON (tableA.column_name = tableB.column_name)] |

[LEFT | RIGHT | FULL OUTER JOIN tableB ON (tableA.column_name = tableB.column_name)];

 

Oracle Proprietary Joins

--Equijoin

select a.columnA, b.columnB,c.columnC from tableA a,tableB b,tableC c where a.id=b.id and b.id=c.id;

--Non-euqijoins

select a.columnA,b.columnB from tableA a,tableB b where a.salary between b.lowestsalary and b.highestsalary;

--Outer join

select a.column,b.column from tableA a,tableB b where a.column(+) = b.column;

select a.column,b.column from tableA a,tableB b where a.column = b.column(+);

--Self join

select a.columnA, a.columnB from tableA a,tableA b where a.columnC=b.columnC;

 

Cartesian Products   

select ColumnA,ColumnB,ColumnC from tableA tableB;

select columnA,columnB from tableA cross jion tableB;

 

Aggregating Data Using Group Functions

Types of group functions: avg,count,max,min,sum,stddev,variance

select count(distinct columnA) from dual;

select avg(nvl(columnA,0) from tale;

 

using the group by clause on multiple columns

select avg(salary) from tableA group by columnA,columnB;

Syntax:

SELECT column,group_function  FROM table  [WHERE condition]

[group by group_by_expression]  [having   group_condition]   [order by  column];

 

Note:you can’t use where clause to restrict groups but you can use the having clause to restrict groups.

Error: select department_id,avg(salary) from employee where avg(salary)>8000 group by department_id;

Right:select department_id,avg(salary) from employee group by department_id having avg(salary)>8000;

 

nesting group functions :

select max(avg(salary)) from employees group by department_id;

 

Subqueries

SELECT select_list FROM table WHERE expr operator 

              (SELECT select _list FROM table);

Operator : IN , ANY  , ALL

IN: Equal to any member in the list

ANY:compare value to each value returned by the subquery

ALL:compare value to every value returned by the subquery

<ANY  means less than the maximum

>ANY  means more than the minimum

=ANY  is equivalent  to IN

<ALL  means less than the minimum

>ALL  means more than the maximum

The NOT operator can be used with IN,ANY and ALL operators.

If one of the values returned by the inner query is a null value, and hence the entire query returns no rows. The reason is that all conditions that compare a null value result is a null.So whenever null values are likely to be part of the results set of a subquery,do not use the NOT IN operator.The NOT IN operator is equivalent to  <>ALL.

 

Manipulating Data

Data Manipulation Language

A DML statement is executed when you:

-Add new rows to a table

-Modify exsitng rows in a table

-Remove exsting rows from a table

A transaction consists of a collection of DML statements that form a logical unit of work.

 

INSERT INTO table [(column [,column …])] VALUES (value [,value…]);

INSERT INTO table( login_time) values ( TO_DATE(DATE(’FEB 3, 1999’, ’MON DD, YYYY’));

INSERT ITNO (SELECT columnA,columnB from tableA where id=50) VALUES(avariableA,avariableB);

INSERT INTO (columnA) values (default);

UPDATE table SET column=value[,column =value,…] [WHERE condition];

DELETE [FROM] table [WHERE condition];

WITH CHECK OPTION keyword

The with check option keyword prohibits you from changing rows that are not in the subquery

The MERGE statement

Provides the ability to conditionally update or insert data into a database table

performs an update if the row exists,and an insert if it is a new row;

--avoids separate updates

--Increases performance and ease of use

--Is useful in data warehousing applications

Syntax:

MERGE INTO  table_name table_alias

USING  (table|view|sub_query) alias

ON (join condition)

WHEN MATCHED THEN

UPDATE SET

col1 = col_vall,

col2 = col_val2

WHEN NOT MATCHED THEN

INSERT (column_list)

VALUES (column_values);

转载于:https://www.cnblogs.com/spirit/archive/2009/10/30/1593221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值