[root@localhost ~]# su - oracle
[oracle@localhost ~]$ sqlplus / as sysdba;
SQL> show user;
USER is "SYS"
SQL> startup open;
SQL> alter user scott identified by tiger account unlock;
SQL> alter user hr identified by hr account unlock;
SQL> conn scott/tiger;
SQL> set linesize 140;(default: 80)
SQL> set pagesize 140;(default: 14)
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
BONUS TABLE
DEPT TABLE
EMP TABLE
SALGRADE TABLE
SQL> desc emp;
SQL> select * from emp;
SQL> desc dept;
SQL> select deptno, dname from dept;
DEPTNO DNAME
---------- --------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
SQL> /
SQL> l
SQL> select empno, ename, sal comm from emp;
SQL> c/sal/sal,
1* select empno, ename, sal, comm from emp
SQL> /
SQL> select empno eno, ename "name", sal, sal*12+30 as "Total Sal" from emp;
ENO name SAL Total Sal
---------- -------------------- ---------- ----------
7369 SMITH 800 9630
7499 ALLEN 1600 19230
SQL> conn hr/hr;
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
COUNTRIES TABLE
DEPARTMENTS TABLE
EMPLOYEES TABLE
EMP_DETAILS_VIEW VIEW
JOBS TABLE
JOB_HISTORY TABLE
LOCATIONS TABLE
REGIONS TABLE
8 rows selected.
SQL> desc employees
SQL> clear screen
SQL> select first_name||' '||last_name as full_name, salary from employees;
SQL> select first_name ||q'['s family name is ]'||last_name as "Result" from employees;
SQL> conn scott/tiger;
SQL> select * from emp where ename like '_A%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
SQL> select * from dept where loc like 'NEW\_%' escape '\';
SQL> select empno, &n, sal from emp order by &n;
Enter value for n: ename
Enter value for n: ename
old 1: select empno, &n, sal from emp order by &n
new 1: select empno, ename, sal from emp order by ename
EMPNO ENAME SAL
---------- ---------- ----------
7876 ADAMS 1100
7499 ALLEN 1600
SQL> select empno, &&n, sal from emp order by &n
Enter value for n: ename
old 1: select empno, &&n, sal from emp order by &n
new 1: select empno, ename, sal from emp order by ename
EMPNO ENAME SAL
---------- ---------- ----------
7876 ADAMS 1100
7499 ALLEN 1600
SQL> define n=ename;
SQL> select empno, &n, sal from emp order by &n;
old 1: select empno, &n, sal from emp order by &n
new 1: select empno, ename, sal from emp order by ename
EMPNO ENAME SAL
---------- ---------- ----------
7876 ADAMS 1100
7499 ALLEN 1600
SQL> show verify
verify ON
SQL> set verify off
SQL> show verify
verify OFF
SQL> select empno, &n, sal from emp order by &n;
EMPNO ENAME SAL
---------- ---------- ----------
7876 ADAMS 1100
7499 ALLEN 1600
SQL> undefine n;
SQL> select sysdate from dual;
SQL> select user from dual;
SQL> desc dual;
SQL> select * from dual;
SQL> select lower('Hello, World') from dual;
SQL> select upper('Hello, World') from dual;
SQL> select initcap('hello, world') from dual;
SQL> select ename, substr(ename, -3) from emp;
ENAME SUB
---------- ---
SMITH ITH
ALLEN LEN
SQL> select lpad(sal, 8, '-') from emp;
LPAD(SAL
--------
-----800
----1600
SQL> select rpad(sal, 8, '-') from emp;
SQL> select length('Helloworld') from dual;
LENGTH('HELLOWORLD')
--------------------
10
SQL> select lengthb('中国') from dual;
LENGTHB('中国')
-----------------
4
SQL> select trim('H' from 'HHHssdHHdgH') from dual;
TRIM('H
-------
ssdHHdg
SQL> select instr('HelloWorld', 'W') from dual;
INSTR('HELLOWORLD','W')
-----------------------
6
SQL> select round(45.926, 2) from dual;
SQL> select trunc(45.926, 2) from dual;
SQL> select mod(1600, 300) from dual;
SQL> select to_char(hiredate, 'yyyy-mm-dd hh24:mi:ss') from emp;
TO_CHAR(HIREDATE,'Y
-------------------
1980-12-17 00:12:00
1981-02-20 00:02:00
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
SQL> select hiredate from emp;
HIREDATE
-------------------
1980-12-17 00:00:00
1981-02-20 00:00:00
SQL> col empno for 9999;
SQL> col sal for 9999.00;
SQL> select empno, ename, sal, to_char(hiredate, 'yyyy/mm/dd') from emp;
EMPNO ENAME SAL TO_CHAR(HI
---------- ---------- -------- ----------
7369 SMITH 800.00 1980/12/17
7499 ALLEN 1600.00 1981/02/20
SQL> select to_char(5000, 'L999,999.00') from dual;
TO_CHAR(5000,'L999,99
---------------------
$5,000.00
SQL> col sal for $999,999.00;
SQL> select empno, ename, sal, to_char(hiredate, 'yyyy/mm/dd') from emp;
EMPNO ENAME SAL TO_CHAR(HI
---------- ---------- ------------ ----------
7369 SMITH $800.00 1980/12/17
7499 ALLEN $1,600.00 1981/02/20
SQL> select empno, ename, nvl(comm, -1) from emp;
EMPNO ENAME NVL(COMM,-1)
---------- ---------- ------------
7369 SMITH -1
7499 ALLEN 300
7521 WARD 500
SQL> select empno, ename, sal*12+comm as total_sal from emp;
EMPNO ENAME TOTAL_SAL
---------- ---------- ----------
7369 SMITH
7499 ALLEN 19500
SQL> select empno, ename, nvl2(comm, sal*12+comm, sal*12) as total_sal from emp;
EMPNO ENAME TOTAL_SAL
---------- ---------- ----------
7369 SMITH 9600
7499 ALLEN 19500
SQL> select empno, ename, nullif(2,2) from emp;
EMPNO ENAME NULLIF(2,2)
---------- ---------- -----------
7369 SMITH
7499 ALLEN
SQL> select empno, ename, nullif(2,3) from emp;
EMPNO ENAME NULLIF(2,3)
---------- ---------- -----------
7369 SMITH 2
7499 ALLEN 2
如果两表达式相等,返回null; 如果不等,返回第一个表达式的值。
SQL> select coalesce(null, null, 3) from dual;
COALESCE(NULL,NULL,3)
---------------------
3
返回第一个不是null的表达式的值。
SQL> select
2 case deptno
3 when 10 then 'Ten'
4 when 20 then 'Twenty'
5 else 'Other'
6 end
7 from emp;
SQL> select * from emp order by comm desc nulls last;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ------------ ---------- ----------
7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 $1,250.00 1400 30
7521 WARD SALESMAN 7698 1981-02-22 00:00:00 $1,250.00 500 30
7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 $1,600.00 300 30
7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 $1,500.00 0 30
7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 $3,000.00 20
7839 KING PRESIDENT 1981-11-17 00:00:00 $5,000.00 10
7876 ADAMS CLERK 7788 1987-05-23 00:00:00 $1,100.00 20