structured query language(非過程性的結構查詢語言)
主要內容:
數據庫怎麼限制裏面的數據;
數據庫怎麼排列裏面的數據。
WHERE子句:
--WHERE字句起到的是對數據庫查詢結果集的限制做用
----[WHERE condition(s)]
------[]表明可選,意思是WHERE字句是能夠沒有的
------(s)也就是說條件是能夠多個的
----WHERE條件的三個要素
------列名
------比較條件
--------不等於的三種形式!=、<>、^=
SQL
>
select
count(
*)
from emp;
COUNT(
*)
----------
14
SQL
>
select
count(
*)
from emp
where empno^
=
7788;
COUNT(
*)
----------
13
------between and 是有邊界的,意思是包括邊界值的
------常數、值列表
--------常數的特徵一、字符串和日期必須用引號括起來;二、字符串是大小寫敏感
------like走索引的問題:like '%XX'是不走索引的,基於索引的原理
SQL
>
select
*
from emp
where empno
=
'7788';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST
7566
09
-
2月
-
82
3000
20
執行計劃
----------------------------------------------------------
Plan hash
value:
4024650034
--------------------------------------------------------------------------------------
| Id |
Operation |
Name |
Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------------------------
|
0 |
SELECT
STATEMENT | |
1 |
38 |
1 (
0)| 00:00:
01 |
|
1 |
TABLE
ACCESS
BY
INDEX ROWID| EMP |
1 |
38 |
1 (
0)| 00:00:
01 |
|
*
2 |
INDEX
UNIQUE SCAN | EMP_PK |
1 | |
0 (
0)| 00:00:
01 |
--------------------------------------------------------------------------------------
Predicate Information (identified
by
operation id):
---------------------------------------------------
2
-
access(
"EMPNO"
=
7788)
統計信息
----------------------------------------------------------
0
recursive calls
0 db block gets
2 consistent gets
0 physical
reads
0 redo
size
772 bytes sent via
SQL
*Net
to client
405 bytes received via
SQL
*Net
from client
1
SQL
*Net roundtrips
to
/
from client
0 sorts (memory)
0 sorts (disk)
1
rows processed
有索引,且走索引
SQL
>
select
*
from emp
where empno
like
'%88';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST
7566
09
-
2月
-
82
3000
20
執行計劃
----------------------------------------------------------
Plan hash
value:
3956160932
--------------------------------------------------------------------------
| Id |
Operation |
Name |
Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------------
|
0 |
SELECT
STATEMENT | |
1 |
38 |
3 (
0)| 00:00:
01 |
|
*
1 |
TABLE
ACCESS
FULL| EMP |
1 |
38 |
3 (
0)| 00:00:
01 |
--------------------------------------------------------------------------
Predicate Information (identified
by
operation id):
---------------------------------------------------
1
- filter(TO_CHAR(
"EMPNO")
LIKE
'%88')
統計信息
----------------------------------------------------------
0
recursive calls
0 db block gets
8 consistent gets
0 physical
reads
0 redo
size
864 bytes sent via
SQL
*Net
to client
416 bytes received via
SQL
*Net
from client
2
SQL
*Net roundtrips
to
/
from client
0 sorts (memory)
0 sorts (disk)
1
rows processed
不能走索引,走全表掃描
--------except 逃避符 通常用來處理 字符串裏面的特點字符 例如 '\'
------null的特徵之一就是不能用比較等於或者不等於
SQL
>
select
*
from emp
where comm !
=
null;
未選定行
SQL
>
select
*
from emp
where comm
=
null;
未選定行
--邏輯運算
----AND
AND |
TRUE |
FALSE |
NULL |
TRUE |
TRUE |
FALSE |
NULL |
FALSE |
FALSE |
FALSE |
FALSE |
NULL |
NULL |
FALSE |
NULL |
----OR
OR |
TRUE |
FALSE |
NULL |
TRUE |
TRUE |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
NULL |
NULL |
TRUE |
NULL |
NULL
|
----NLS_UPPER:轉換成大寫,不一樣語言都有用
----UPPER:不能處理特殊語言
--IN的查詢轉換問題: