Oracle中的SQL語言

SQL分類:sql

①DML:select、insert、update、delete主要針對表中的數據orm

②DDL:create、alter、drop、rename、truncate主要針對表結構排序

③DCL:grant、revoke主要針對權限索引

PL/SQL的關鍵地方:字符串

①Help -> Registerio

②Session -> Log  ontable

③File -> New -> SQL Windowform

SQL語法:效率

1.爲表添加註釋:comment  table  emp  is  ‘僱員表’;date

2.爲列添加註釋:comment  on  column  emp.Empno  is  '僱員工號';

3.查詢語句:select  想要查找的字段  from  表名  where  條件;

4.去重重複數據:select  distinct  想要查找的字段  from  表名;

  注意:去重也能夠針對多個字段,多個字段值只要有一個不匹配就算是不統計錄值

5.給列添加別名:select  e.empno  想起的別名  from  emp  e;

  注意:若是給列起的別名中包含空格,那麼,要用」「將別名包圍起來

6.等於:select  *  from  emp  where  deptno  =  20;

7.不等於:select * from emp where deptno != 20;

            select * from emp where deptno <> 20;

8.小於:select sal from emp where sal < 1500;

9.大於:select sal from emp where sal > 1500;

10.小於等於:select sal from emp where sal <= 1500;

11.大於等於:select sal from emp where sal >= 1500;

12.任意一個:大於any集合中的任意一個值都會成立

        select sal from emp where sal > any(1000,1500,3000);

13.某些:some和any是同一個效果,只要大於some集合中的某一個值都會成立

      select sal from emp where sal > some(1000,1500,3000);

14.全部:大於全部的值纔會成立

      select sal from emp where sal > any(1000,1500,3000);

15.判斷是否爲空:在sql語法中,null表示一個特殊的含義,null != null,不能用=,!=進行判斷,要用is null和is not null進行判斷

                         select * from emp where comm is null;

         select * from emp where comm is not null;

16.範圍判斷:select * from emp where sal between 1500 and 3000;

       select * from emp where sal >= 1500 and sal<=3000;

17.等值判斷:須要進行某些值的等值判斷時可使用in和not in

       select * from emp where deptno in(10,20);

       select * from emp where deptno not in(10,20);

18.and和or關鍵字:select * form emp where deptno = 10 or deptno = 20;

           select * form emp where deptno != 10 and deptno != 20;

     注意:and至關於與操做,or至關於或操做,在and和or出如今同一sql語句中時,and優先級大於or優先級

     因此,在使用or關鍵字時要使用()提升優先級

19.exists(sub-query),當exists中的子查詢語句能查詢到對應結果的時候,意味着條件知足

 如今要查詢部門編號爲10 和20的員工,要求使用exists實現:

 select * from emp e where exists(select * from dept d where (d.deptno = 10 or deptno = 20) and e.deptno = d.deptno);

20.模糊查詢:在like語句中,須要使用佔位符或者通配符

       _,某個字符或者數字僅出現一次

       %,任意字符出現任意次數

 查詢名字以S開頭且倒數第二個字符爲T的用戶

 select * from emp where ename like('S%T_');

注意:在使用模糊查詢時,效率較低,能夠參考使用索引

   但在使用索引時,like中不能以%開頭,這樣會使得索引失效

21.轉意字符:escape,在模糊查詢中%表明任意字符出現任意次數,使用escape後%的含義就是%自己字符

 查詢名字中帶有%的名字:select * from emp where ename like('%\%%') escape ('\');

22.排序:order by進行排序操做,asc爲升序,desc爲降序,若排序時未標註升序仍是降序則默認升序

  select * from emp order by sal;

  select * from emp order by sal desc;

23.字符串鏈接符:select 'my name is '|| ename from emp;

         select concat('my name is ',ename) from emp;

24.nvl(aa,bb):若aa值爲null,返回bb值;null作任何運算都仍是null,所以要將空進行轉換

 計算全部員工的年薪:select ename,(e.sal+e.comm)*12 from emp e;comm中存在值null的狀況,此寫法有瑕疵

 select ename,(e.sal+nvl(e.comm,0))*12 from emp e;

  A:select * from emp where deptno = 30;

  B:select * from emp where sal > 1000;

  C:集合A與集合B重複的部分

25.並集:集合A(不包含重複部分)+集合B(不包含重複部分)+一份集合C

  select * from emp where deptno = 30 union select * from emp where sal > 1000;

26.全集:集合A+集合B  或  {集合A(不包含重複部分)+集合B(不包含重複部分)+一份集合C}

  select * from emp where deptno = 30 union all select * from emp where sal > 1000;

27.交集:集合C

  select * from emp where deptno = 30 intersect select * from emp where sal > 1000;

28.差集:包含在A集合而不包含在B集合,與集合的順序相關

  select * from emp where deptno = 30 minus select * from emp where sal > 1000;

相關文章
相關標籤/搜索