oracle學習歷程 2017/8/22

多表連接查詢

 介紹一下表別名。如果在多表查詢的時候出現了多個相同名字的列。於是我們就採用了表別名的方式,是給各個表一個簡短的名稱。
     例如:select em.employee_id,em.last_name,dep.department_name
                  from employees em,department dep
                   where em.department_id = dep.department_id
                     and dep.department_name = 'Shipping';
                    如果表指定了別名,那麼語句中所有的子句都必須使用別名。不允許使用實際的表名。

簡單連接

  簡單連接僅僅是通過select和from來連接多個表實現的。通過笛卡兒積所生成的表。(兩個表的行數的積)。

join 連接

   分爲內連接,外連接,自連接,自然連接。
大體語法  from 表名  連接類型 表名 [連接條件]

內連接:

inner join此關鍵詞中的inner可以省略,但是必須有on子句作爲連接條件,進行多表查詢的時候,查詢的結果集中僅包含符合查詢條件和連接條件的行。例如:
              select   a.*,b.*   from   a   inner   join   b     on   a.id=b.parent_id       就是簡單的把兩個表連接起來。

自然連接:

oracle將第一個表中的列與第二個表中相同的列進行自然連接。不需要用on 來指定。有好有壞。現實中表名相同但實際含義不一定相同。
             select em.employeeid,em.firstname,lastname,dep.departmentname from
                         from employees em natural join deparment dep
                           where dep.departmentname='sales';

外連接:

與內連接不同的是,外連接除了會返回所需要的結果集,還會返回一部分或全部不匹配的行。
             分爲:左外連接(列出左表中所有符合搜索條件的數據行),右外連接(列出右表中所有符合搜索條件的數據行),和全連接三種(列出兩個表表中所有符合搜索條件的數據行)
例如:
 a表     id   name     b表     id   job   parent_id   
              1   張3                   1     23     1   
              2   李四                 2     34     2   
              3   王武                 3     34     4       
  a.id同parent_id   存在關係   
左外連接:  
select   a.*,b.*   from   a   left   join   b     on   a.id=b.parent_id       
  結果是     
  1   張3                   1     23     1   
  2   李四                  2     34     2   
  3   王武                  null   
右外連接:
  select   a.*,b.*   from   a   right   join   b     on   a.id=b.parent_id       
  結果是     
  1   張3                   1     23     1   
  2   李四                  2     34     2   
  null                       3     34     4   
全連接:
  select   a.*,b.*   from   a   full   join   b     on   a.id=b.parent_id   
  結果是     
  1   張3                  1     23     1   
  2   李四                 2     34     2   
  null                   3     34     4   
  3   王武                 null

自連接

 用戶可能擁有自引用式外鍵。一個表中的某一列可能是該表主鍵的一個外鍵。比如employees表的manager_id列可以是另一行的employee_id。

集合操作:

 集合操作就是兩個或多個sql查詢結果合併構成符合查詢。常見的有union(並運算)union all,intersect(交運算),minus(差運算)
union(並運算)  將查詢結果相加,結果等同於集合運算中的並運算。相當於or
例如:
select employee_id,last_name
from HR.EMPLOYEES
where last_name like 'C%' or last_name like 'S%'          (這個列出c,s開頭的僱員)
union
select employee_id,last_name
from HR.EMPLOYEES
where LAST_NAME like 'S%' or last_name like 'T%';           (列出S,T開頭的僱員)
結果就是C或者S或者T開頭的僱員的信息均被列出。


union all
與union不同之處是操作符形成的結果集中包含有兩個子結果集中重複的行。

intersect(交運算)
相當於and,是交集運算。列出c,s開頭的僱員,列出S,T開頭的僱員,結果是返回以S開頭的僱員。
,minus(差運算)
在兩個給定的集合之間的差,該集合操作符會返回所有從第一個查詢中返回的,但是沒有在第二個查詢中返回的記錄。
按上面的例子來說:列出c,s開頭的僱員,列出S,T開頭的僱員,結果是返回以C開頭的僱員。

子查詢

子查詢和連接查詢一樣,都提供了使用單個查詢訪問多個表中數據的方法。子查詢是一個select語句,它可以在select,insert,update,delete語句中使用。也可以在select和having子句中使用子查詢。
in 關鍵字
 /*查詢所有部門在1700地區的僱員信息*/
 select employee_id,last_name,department_id
 from HR.EMPLOYEES
 where department_id in (            (首先執行括號內的子查詢,然後再執行外層查詢)
 select department_id
 from departments
 where location_id = 1700);


exists 關鍵字  
  只注重子查詢是否返回行,如果返回有值,則爲true,否則爲false。
 select employee_id,last_name from employee em
 where exists(
 select * from department dep
 where em.department_id=dep.department_id
 and location_id =1700);                  這個和in相同。

比較運算符=,<>,<,>,<=,>=

數據操縱

包括插入、刪除、和修改三種操作。對呀insert,delete,update。oracle中還有truncate,call,explain plan、lock table、merge。
 
insert 是插入語句
insert into 要插入的表名【數據庫鏈接名】【列名】 values (要插入的值);
例如:insert into jobs(job_id,job_title,min_salary,max_salary) values (' * ','*',300,800);
批量insert 
使用select語句替換values,由select語句提供添加的數據。
語法:insert into 要插入的表名【數據庫鏈接名】【列名】 select查詢語句
相當於從其他表裏讀出來,放到要添加的這個表裏。

方法2:

insert into persons 

(id_p, lastname , firstName, city )

values

(200,'haha' , 'deng' , 'shenzhen'),

(201,'haha2' , 'deng' , 'GD'),

(202,'haha3' , 'deng' , 'Beijing');


update語句:修改表中的一列或多列的值。

語法: update  表名  set  用於設置要更新的列以及各列的新值

            where  限定的條件;(限定指定的表。)

update employees

           set salary = salary * 1.15

            where job_id ='it_prog';


delete 語句
   用來刪除表或者表中的哪一條記錄。
 delete  from 表名  where   限定條件
比如:delete from employees where employee_id =107;

truncate 語句
     用戶用於刪除表中所有的記錄,則建議使用truncate語句。但是執行的truncate操作也不能被撤銷。
      truncate  table it_employees;

數據控制,主要介紹sql的安全性控制。關係到授權什麼的。
                 grant 和 revoke 語句來決定授權
                  授權的結果存入數據字典。
                  當用戶提出操作請求時,根據授權情況進行檢查,決定是否執行操作請求。
grant   用於向用戶操作權限。一般格式爲grant 權限。
語法: grant 權限 on 要把那張表的權限賦值給用戶 to 用戶
其中,grant  權限名稱 on TABLE 表名 to 用戶 WITH GRANT OPTION。後面這句話的意思是允許該用戶再將權限給其他用戶,如果不加的話則不行。
相關權限。
對象 對象類型 操作權限
屬性列 table column select,insert,update,delete,all privileges
視圖 table  view select,insert,update,delete,all privileges
基表 table select,insert,update,delete,all privileges,index
數據庫 database createtable

比如:grant select on table employees to user1;
          grant all privileges  on  table employees,jobs to user2,user3;
REVOKE語句
將授予的權限收回。一般格式:
          revoke  權限 on 表名  from 用戶
revoke update on table employees from user4;


         
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章