Oracle-02-基本select查詢
下面開始執行基本select語句
conn oracle
select * from tab;
檢視oracle下面所有的物件
TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------- BONUS TABLE DEPT TABLE EMP TABLE SALGRADE TABLE 11 rows selected. Elapsed: 00:00:00.01
檢視任意的一張表
select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Elapsed: 00:00:00.01
一個sql語句一定要有select子句和from子句
描述一個表的資訊可以用desc(注意,這裡沒有用分號
desc emp
Name Null? Type ----------------------------------------------------------------- -------- -------------------------------------------- EMPNO NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2)
select ename,sal from emp;
ENAME SAL
---------- ----------
SMITH 800
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
14 rows selected.
Elapsed: 00:00:00.00
SQL語句的表明和關鍵字是不區分大小寫的,SQL語句可以寫成一行,也可以寫成多行,但是關鍵字不能縮寫,也不能換行,一般不同的行我們都放在獨立的行,SQL語句要執行必須以 ;(分號)結束,或者在新的一行中以 / 結束(/的含義是執行最近一次的SQL語句,實際上就是執行在快取裡的SQL語句)
--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!劃重點!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SQL語句分類
資料操縱語言:DML: select; insert; delete; update; merge.
資料定義語言:DDL: create; alter; drop; truncate; rename; comment.
事務控制語言:TCL: commit; rollback; savepoint.
資料控制語言:DCL: grant; revoke.
SQL語句的特點
1)是SQL92/99的ANSI官方標準,只要按照該標準來寫,在任何的關係型資料庫中都可以直接執行
2)SQL語句的關健字不能簡寫,例如:select,where,from
3)大小寫不敏感,提倡大寫
4)能夠對錶資料進行增刪改查操作
5)必須以分號結束
6)通常稱做語句
SQLPLUS命令
COLUMN
DESCRIPTION
SET
SHOW (SHOW RECYCLEBIN,SHOW ALL,SHOW USER,SHOW ERROR)
HOST($)
CONNECT
PASSWORD
DISCONNECT
EXIT
LIST
APPEND
CHANGE
INPUT
N
EDIT
RUN(/)
DEL
START(@)
CLEAR SCREEN
SAVE
GET
SPOOL
&
&&
DEFINE
SQLPLUS中的環境變數
ARRAYSIZE
AUTOCOMMIT
COLSEP
FEEDBACK
HEADING
LINESIZE
LONG
PAGESIZE
SERVEROUTPUT
TERMOUT
TIME
TIMING
SQLPROMPT 'xxx'
PAUSE
SQLPLUS命令的特點
1)是oracle自帶的一款工具,在該工具中執行的命令叫SQLPLUS命令
2)SQLPLUS工具的命令中的關健字可以簡寫,也可以不簡寫,例如:col ename for a10;
3)大小寫不敏感,提倡大寫
4)不能夠對錶資料進行增刪改查操作,只能完成顯示格式控制,例如:設定顯示列寬,清屏,記錄執行結果
5)可以不用分號結束,也可以用分號結束,個人提倡不管SQL或SQLPLUS,都以分號結束
6)通常稱做命令,是SQLPLUS工具中的命令
注意:SQLPLUS命令是SQLPLUS工具中特有的語句
----------------------------------------------------分割線----------------------------------------------------
在select語句後可以直接用四則運算子
select ename,sal,sal+100 from emp;
ENAME SAL SAL+100
---------- ---------- ----------
SMITH 800 900
ALLEN 1600 1700
WARD 1250 1350
JONES 2975 3075
MARTIN 1250 1350
BLAKE 2850 2950
CLARK 2450 2550
SCOTT 3000 3100
KING 5000 5100
TURNER 1500 1600
ADAMS 1100 1200
JAMES 950 1050
FORD 3000 3100
MILLER 1300 1400
14 rows selected.
Elapsed: 00:00:00.00
salary+100這一列在表中並不存在,因此我們稱作計算表示式的偽列
算數表示式先乘除後加減,從左到右,()強制優先順序
例如每個僱員年工資增加100
select ename,sal,sal*12+100 from emp;
ENAME SAL SAL*12+100
---------- ---------- ----------
SMITH 800 9700
ALLEN 1600 19300
WARD 1250 15100
JONES 2975 35800
MARTIN 1250 15100
BLAKE 2850 34300
CLARK 2450 29500
SCOTT 3000 36100
KING 5000 60100
TURNER 1500 18100
ADAMS 1100 13300
JAMES 950 11500
FORD 3000 36100
MILLER 1300 15700
14 rows selected.
Elapsed: 00:00:00.00
每個僱員每月加100後年工資
select ename,sal,(sal+100)*12 from emp;
ENAME SAL (SAL+100)*12
---------- ---------- ------------
SMITH 800 10800
ALLEN 1600 20400
WARD 1250 16200
JONES 2975 36900
MARTIN 1250 16200
BLAKE 2850 35400
CLARK 2450 30600
SCOTT 3000 37200
KING 5000 61200
TURNER 1500 19200
ADAMS 1100 14400
JAMES 950 12600
FORD 3000 37200
MILLER 1300 16800
14 rows selected.
Elapsed: 00:00:00.00
關係型資料庫中存在空值 null ,是一個不確定的值,在做任何四則運算時得到的結果也是空值
select ename,sal,comm from emp;
ENAME SAL COMM
---------- ---------- ----------
SMITH 800
ALLEN 1600 300
WARD 1250 500
JONES 2975
MARTIN 1250 1400
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500 0
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
14 rows selected.
Elapsed: 00:00:00.01
根據獎金,算出工資總和
select ename,sal,sal+comm from emp;
ENAME SAL SAL+COMM
---------- ---------- ----------
SMITH 800
ALLEN 1600 1900
WARD 1250 1750
JONES 2975
MARTIN 1250 2650
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
14 rows selected.
Elapsed: 00:00:00.00
這裡我們發現沒有工資總和的是獎金為空的僱員,這裡要說明的是,空值不是0,也不是空格,是一個未定義的值,如果四則運算裡面需要用到空值,後續我們會介紹函式來專門處理空值的問題
----------------------------------------------------分割線--------------------------------------------------
select語句中的列別名
可以在列名後面跟as或者用空格再跟別名,雖然列別名在寫的時候是小寫,但是在結果集的列頭中預設都是大寫,如果強制要求小寫或者列別名中間有空格,關鍵字,特殊字元時,可以用“”(雙引號)把列別名表示出來
讓列名以小寫出現
select ename "name" from emp;
name
----------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
14 rows selected.
Elapsed: 00:00:00.01
通常別名用在有計算表示式的情況下,以簡要說明表示式的意義
select ename,sal*12 annaul_sal from emp;
ENAME ANNAUL_SAL
---------- ----------
SMITH 9600
ALLEN 19200
WARD 15000
JONES 35700
MARTIN 15000
BLAKE 34200
CLARK 29400
SCOTT 36000
KING 60000
TURNER 18000
ADAMS 13200
JAMES 11400
FORD 36000
MILLER 15600
14 rows selected.
Elapsed: 00:00:00.00
下面是別名有空格的情況
select ename,sal*12 "annaul sal" from emp;
ENAME annaul sal
---------- ----------
SMITH 9600
ALLEN 19200
WARD 15000
JONES 35700
MARTIN 15000
BLAKE 34200
CLARK 29400
SCOTT 36000
KING 60000
TURNER 18000
ADAMS 13200
JAMES 11400
FORD 36000
MILLER 15600
14 rows selected.
Elapsed: 00:00:00.00
----------------------------------------------------分割線------------------------------------------------
select語句中的列連線,可以用 || 做列的連線符
select ename||job from emp;
ENAME||JOB
-------------------
SMITHCLERK
ALLENSALESMAN
WARDSALESMAN
JONESMANAGER
MARTINSALESMAN
BLAKEMANAGER
CLARKMANAGER
SCOTTANALYST
KINGPRESIDENT
TURNERSALESMAN
ADAMSCLERK
JAMESCLERK
FORDANALYST
MILLERCLERK
14 rows selected.
Elapsed: 00:00:00.00
或者在列中間可以連線字串,當然這裡字串要用單引號引起來''
select ename||' '||job from emp;
ENAME||''||JOB
--------------------
SMITH CLERK
ALLEN SALESMAN
WARD SALESMAN
JONES MANAGER
MARTIN SALESMAN
BLAKE MANAGER
CLARK MANAGER
SCOTT ANALYST
KING PRESIDENT
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
FORD ANALYST
MILLER CLERK
14 rows selected.
Elapsed: 00:00:00.01
select ename||' is '||job from emp;
ENAME||'IS'||JOB
-----------------------
SMITH is CLERK
ALLEN is SALESMAN
WARD is SALESMAN
JONES is MANAGER
MARTIN is SALESMAN
BLAKE is MANAGER
CLARK is MANAGER
SCOTT is ANALYST
KING is PRESIDENT
TURNER is SALESMAN
ADAMS is CLERK
JAMES is CLERK
FORD is ANALYST
MILLER is CLERK
14 rows selected.
Elapsed: 00:00:00.00
----------------------------------------------------分割線----------------------------------------------
select語句去除重複值
select ename,deptno from emp;
ENAME DEPTNO
---------- ----------
SMITH 20
ALLEN 30
WARD 30
JONES 20
MARTIN 30
BLAKE 30
CLARK 10
SCOTT 20
KING 10
TURNER 30
ADAMS 20
JAMES 30
FORD 20
MILLER 10
14 rows selected.
Elapsed: 00:00:00.01
發現有僱員的部門是重複的,如果要去除重複值可以用 DISTINCT
select distinct deptno from emp;
DEPTNO
----------
30
20
10
Elapsed: 00:00:00.01