SQL> conn scott/scottConnected.SQL> select * from cat;TABLE_NAME TABLE_TYPE------------------------------ -----------BONUS TABLEDEPT TABLEEMP TABLESALGRADE TABLEselect ENAME,JOB,to_char(HIREDATE,'YYYY-mm-dd') as HIREDATE from emp where DEPTNO = (select DEPTNO from emp where ENAME='BLAKE')and ENAME<>'BLAKE';ENAME JOB HIREDATE---------- --------- ----------ALLEN SALESMAN 1981-02-20WARD SALESMAN 1981-02-22MARTIN SALESMAN 1981-09-28TURNER SALESMAN 1981-09-08JAMES CLERK 1981-12-03
显示位置在Dallas的部门内的雇员姓名、变化以及工作
select e.EMPNO,e.ENAME,e.JOB from EMP e, DEPT d where e.DEPTNO=d.DEPTNO and LOC='DALLAS';# 或者select EMPNO,ENAME,JOB from emp where DEPTNO=(select deptno from dept where LOC='DALLAS'); EMPNO ENAME JOB---------- ---------- ---------7369 SMITH CLERK7566 JONES MANAGER7788 SCOTT ANALYST7876 ADAMS CLERK7902 FORD ANALYST
显示被 KING 直接管理的雇员的姓名以及工资
select ENAME,SAL from emp where MGR=(select EMPNO from emp where ENAME='KING');# 或者SELECT E.ENAME,E.SAL FROM EMP E,EMP EP WHERE E.MGR=EP.EMPNO AND EP.ENAME='KING';ENAME SAL---------- ----------JONES 2975BLAKE 2850CLARK 2450
创建一查询,显示能获得与 Scott 一样工资和奖金的其他雇员的姓名、受雇日期以及工资。
select ENAME,to_char(HIREDATE,'YYYY-MM-DD') HIREDATE,SALfrom empwhere SAL=(select SAL from emp where ENAME='SCOTT')and nvl(COMM,0)=(select nvl(COMM,0) from emp where ENAME='SCOTT')and ENAME<>'SCOTT';ENAME HIREDATE SAL---------- ---------- ----------FORD 1981-12-033000
SQL> conn / as sysdbaConnected.SQL> show parameter spfileNAME TYPE VALUE------------------------------------ ----------- ------------------------------spfile string /u01/app/oracle/11.2.0.4/db_1/ dbs/spfileorcl.oraSQL> create pfile from spfile;File created.SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> ! ls -l /u01/app/oracle/11.2.0.4/db_1/dbs/total 39768-rw-r-----. 1 oracle oinstall 10158080 Aug 292016 c-1445641855-20160829-01-rw-r-----. 1 oracle oinstall 10158080 Aug 292016 c-1445641855-20160829-03-rw-r-----. 1 oracle oinstall 10158080 Aug 292016 c-1445641855-20160829-04-rw-r-----. 1 oracle oinstall 10223616 Aug 292016 c-1445641855-20160829-06-rw-rw----. 1 oracle oinstall 1544 Jul 1810:30 hc_orcl.dat-rw-r--r--. 1 oracle oinstall 2851 May 152009 init.ora-rw-r--r-- 1 oracle oinstall 827 Jul 1810:30 initorcl.ora-rw-r----- 1 oracle oinstall 24 Jul 18 08:43 lkORCL-rw-r----- 1 oracle oinstall 1536 Jul 18 08:46 orapworcl-rw-r----- 1 oracle oinstall 2560 Jul 18 08:47 spfileorcl.ora# 使用pfile启动数据库SQL> startup pfile='/u01/app/oracle/11.2.0.4/db_1/dbs/initorcl.ora';ORACLE instance started.Total System Global Area 4375998464 bytesFixed Size 2260328 bytesVariable Size 905970328 bytesDatabase Buffers 3456106496 bytesRedo Buffers 11661312 bytesDatabase mounted.Database opened.SQL> show parameter spfileNAME TYPE VALUE------------------------------------ ----------- ------------------------------spfile string# 使用SPFILE启动数据库(默认)SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> create spfile from pfile;File created.SQL> startupORACLE instance started.Total System Global Area 4375998464 bytesFixed Size 2260328 bytesVariable Size 905970328 bytesDatabase Buffers 3456106496 bytesRedo Buffers 11661312 bytesDatabase mounted.Database opened.SQL> show parameter pfileNAME TYPE VALUE------------------------------------ ----------- ------------------------------spfile string /u01/app/oracle/11.2.0.4/db_1/ dbs/spfileorcl.ora
给表空间增加一个数据文件
SQL> col FILE_NAME for a50SQL> col TABLESPACE_NAME for a15SQL> select FILE_NAME,TABLESPACE_NAME,BYTES/1024/1024 size_m from dba_data_files;FILE_NAME TABLESPACE_NAME SIZE_M-------------------------------------------------- --------------- ----------/u01/app/oracle/oradata/orcl/users01.dbf USERS 5/u01/app/oracle/oradata/orcl/undotbs01.dbf UNDOTBS1 90/u01/app/oracle/oradata/orcl/sysaux01.dbf SYSAUX 520/u01/app/oracle/oradata/orcl/system01.dbf SYSTEM 750/u01/app/oracle/oradata/orcl/example01.dbf EXAMPLE 313.125SQL> alter tablespace USERS add datafile '/u01/app/oracle/oradata/orcl/users02.dbf' size 5M;Tablespace altered.
创建一个新的UNDO表空间,并使用它。
SQL> create undo tablespace UNDOTBS2 datafile '/u01/app/oracle/oradata/orcl/undotbs02.dbf' size 200M;Tablespace created.SQL> alter system set undo_tablespace='UNDOTBS2';System altered.SQL> show parameter undoNAME TYPE VALUE------------------------------------ ----------- ------------------------------undo_management string AUTOundo_retention integer 900undo_tablespace string UNDOTBS2
给当前redo增加一组新的redo group
SQL> select GROUP#,MEMBERS,BYTES/1024/1024 size_m from v$log; GROUP# MEMBERS SIZE_M---------- ---------- ----------115021503150SQL> col MEMBER for a50SQL> select GROUP#,MEMBER from v$logfile; GROUP# MEMBER---------- --------------------------------------------------3 /u01/app/oracle/oradata/orcl/redo03.log2 /u01/app/oracle/oradata/orcl/redo02.log1 /u01/app/oracle/oradata/orcl/redo01.logSQL> alter database add logfile group 4 ('/u01/app/oracle/oradata/orcl/redo04.log') size 50M;Database altered.SQL> select GROUP#,MEMBERS,BYTES/1024/1024 size_m from v$log; GROUP# MEMBERS SIZE_M---------- ---------- ----------1150215031504150
计算当前数据库中所有数据文件的总计大小
SQL> select sum(BYTES)/1024/1024 size_m from v$datafile; SIZE_M---------- 1883.125
杂记
dual是什么?
是Oracle下的一个字典表
属于sys用户
用于构造一个标准的SQL
对优化器有一定影响
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';select sysdate from dual;select user from dual;
SQL语句的种类
DML: Data Manipulation language
SELECT
INSERT
DELETE
UPDATE
DDL: Data Definition Language
CREATE
DROP
TRUNCATE
ALTER
DCL: Data Control Language
GRANT
REVOKE
create user lyj identified by lyj default tablespace users;grant connect, resource to lyj;conn lyj/lyjcreate table t1 (id int);truncate table t1;alter table t1 add name varchar2(10);insert into t1 values(1,'lyj');delete from t1 where id=1;drop table t1 purge;conn / as sysdbadrop user lyj cascade;conn scott/scottselect job,max(sal),min(sal),avg(sal),sum(sal) from emp group by job;alter database backup controlfile to trace;