daodan

导航

 

1.oracle 中rowid和rownum的区别

oracle 数据库中,每一行都有一个唯一行的标识符,ROWID,数据库内部用来存储行的物理位置。ROWID是一个18位数字,采用base-64编码。用desc table的时候,rowid不显示,所以rowid通常称为伪列。

rownum 通常返回每一行在结果集中的行号。

2.理解空值

数据库中空值,标识该列的值是未知的。

通常用IS NULL 来检查空值,区分空值和空的字符串,通常用函数NVL().

3.SQL中的like操作符

通常要匹配字符串中10% 中的'%',则用 ESCAPE

例如: select name from promotions where name like '%\%%' ESCAPE '\';

4.BETWEEN 用来检索列值包含在指定区间内的行。包含区间的两个端点

例如: select  * from customers where customers_id between 1 and 3. (其中包含1和3.)

5.笛卡尔积

如果在多表中查询不指定表之前的连接关系,就会导致将一个表中的所有行都连接到另外一个表中的所有行上,这种情况就成为笛卡尔积。

例如:第一个表有50行数据,第一张表有100行数据,如果这两个表查询的时候,不使用关联条件,则就会查出来5000行。

6.表之间的链接条件

1)等连接  (=)

2)不等连接(<> ,  between,<,  >,  <=,   >=,  like , in)

3)内连接 只有当连接中的列包含满足连接条件的值时才会返回一行,如果某一行的连接条件中的一列是空值,那么这行就不会返回。

例如: select p.name,pt.name

frm products p  inner  join  product_types pt

on p.product_type_id = pt.product_type_id; 

多表的内连接:

sql/86 的查询

 select c.first_name ,c.last_name,p.name as product,pt.name as type

from customers c, purchases pr ,products p ,product_types pt

where c.customer_id = pr.customer_id

and p.product_id = pr.product_id

and p.product_type_id = pt.product_type_id

order by p.name;

或者也可以sql/92这样写:

select c.first_name ,c.last_name,p.name as product,pt.name as type

from customers c  inner join purchases pr

using (customer_id)

inner join products p

using (product_id )

inner join product_types pt

using (product_type_id)

order by p.name;

如果关联条件连接了2张表的多列,则可以这样处理:

select *

from table1 inner join table2

on table1.col1 = table2.col1

and table1.col2 = table2.col2;

或者:

select  * from table1 inner join table2

using (col1,col2);

4)外连接

左外连接:连接符号(+) 实际上在等号的右边,表示等号左边表中有空值的列都得检索出来

(+在等号右边,表示检索表products  p中product_type_id有空值的数据都检索出来。)

例如: select p.name,  pt.name 

       from products p, product_types pt

       where p.product_type_id = pt.product_type_id(+)

右外连接: 连接符号(+)在等号的左边,表示等号右边的表中有空值的数据都检索出来。

外链接的限制:

              不能等号两端都使用连接符号+

       例如 : select p.name,  pt.name 

       from products p, product_types pt

       where p.product_type_id(+) = pt.product_type_id(+) 错误使用

       order by p.name;

               不能同时使用外连接和IN 操作符

      例如 : select p.name,  pt.name 

       from products p, product_types pt

       where p.product_type_id(+) in (1,2,3,4)  错误使用

           不能同时使用一个外连接条件和另外一个使用OR操作符的连接条件

 

       例如 : select p.name,  pt.name 

 

       from products p, product_types pt

 

       where p.product_type_id(+) = pt.product_type_id(+)

 

 

       or p.product_type_id = 1;    错误使用     

5)自连接

自连接是对同一张表的连接,要执行一个自连接,必须要使用不同的表的别名来标识在查询中对表的引用。自连接可以和外链接同时使用

例如:select w.last_name || 'work as' || nvl(m.last_name ,'the shareholders')

      from employees w, employees m

      where w.manager_id = m.manager_id(+)

     order by w.last_name;

 

posted on 2016-12-04 14:15  daodan  阅读(171)  评论(0编辑  收藏  举报