Oracle常見SQL

–檢索需要的信息
select rowid,job,ename from emp;
–select 中可以使用運算符 ±/
select sal
(1+0.1),sal from emp;
–as 別名 但是as可以省略
select empno as “員工編號” ,ename as “員工姓名” ,job as “職務” ,sal as “工資” from emp;
select empno “員工編號”, ename “員工姓名” ,job “職務” ,sal “工資” from emp;
–去重複
select distinct job from emp;
–比較篩選
select empno,ename,sal from emp where sal >1500 order by sal;
select enmon,ename,sal from emp where sal <> all (3000,950,1600);
–特殊關鍵字篩選 like “_” 表示任意一個字符 "%"表示任意長度的字符
select empno,ename,job from emp where ename like ‘S%’;
**–In 測試一個數據值是否匹配一組目標值中的一個 員工信息匹配職務 president,**manager,analyst (區分大小寫)
select empno,ename,job from emp where job in (‘PRESIDENT’,‘MANAGER’,‘ANALYST’);
–(not)between … and 位於兩個給定的值的中間值
select empno ,ename ,sal from emp where sal between 2000 and 3000;
–is(not) null 空值 不確定的值 不等於 空字符串
select empno, ename,job,comm from emp where comm is not null;
–邏輯運算符可以使用 and
select empno ,ename ,job,sal from emp where sal >2000 and sal <= 3000;

–分組查詢 group by 按照部門編號和職務 進行分組
select deptno ,job from emp group by deptno ,job order by deptno;
–having 子句對 group by 進行再次篩選 having中可以包含聚合函數(avg,count,sum)
select deptno “部門編號” ,avg(sal) “平均工資” from emp group by deptno having avg(sal) >2000
–order by 可以對一列多列進行排序,第一個是主要排序,其次是次要排序
select deptno, empno, ename from emp order by deptno, empno;

–多表關聯查詢
select e.empno, e.ename,e.job, d.dname
from emp e, dept d
where e.deptno = d.deptno
and e.job = ‘MANAGER’;
–內連接 (INNER) JOIN … on (實現內連接的表達式)
select empno,ename,deptno from emp;
select deptno ,dname from dept;
select e.empno,e.ename,d.dname from emp e inner join dept d on e.deptno=d.deptno;

insert into emp(empno,ename,job) values (9527,‘EAST’,‘SALESMAN’)

–左外連接 查詢結果會包含左表中不滿足”連接條件“
select e.empno,e.ename,e.job,d.deptno,d.dname from emp e left join dept d on e.deptno=d.deptno;
–右外連接 查詢結果會包含右表中不滿足”連接條件“
select e.empno,e.ename,e.job,d.deptno,d.dname from emp e right join dept d on e.deptno=d.deptno;
–完全外連接 執行一個完整的左外連接和右外連接,然後合併查詢結果,並消除重複行
select e.empno,e.ename,e.job,d.deptno,d.dname from emp e full join dept d on e.deptno=d.deptno;
–自然連接 實際環境用的比較少
select empno,ename,job,sal,dname from emp natural join dept where sal > 2000;
–自連接
select e2.ename as 管理者,e1.ename as 員工 from emp e1 left join emp e2 on e1.mgr=e2.empno order by e1.mgr;
–交叉連接 不需要任何連接條件的連接 cross join 查詢結果非常冗餘,可以用where提取有用信息
select count(*) from dept cross join emp;

select sysdate as 系統日期 from dual
–當前日期再加6個月
select ADD_MONTHS(sysdate , 6) from dual;

select * from emp;
select * from dept;
–子查詢
select empno,ename,job from emp where deptno = (select deptno from dept where dname=‘RESEARCH’);
–多表查詢
select empno,ename,job from emp join dept on emp.deptno =dept.deptno where dept.dname=‘RESEARCH’;
–單行子查詢
select empno ,ename, job, sal from emp where sal >(select min(sal) from emp) and sal<(select max(sal) from emp);
–多行子查詢 A!B 或 A<>B
–in
select empno,ename,job from emp where deptno in (select deptno from dept where dname<>‘SALES’);
–any
select empno,ename,job from emp where sal >any (select sal from emp where deptno=10) and deptno<> 10;
–all
select empno,ename,job from emp where sal >all(select sal from emp where deptno=30);
–關聯子查詢 內查詢需要藉助於外查詢 外查詢的執行離不開內查詢的執行
select empno,ename,job,sal from emp e where sal >(select avg(sal) from emp where job =e.job) order by sal;
insert into emp (empno,ename,job) values (7901,‘Qian’,‘Boss’);
insert into emp (empno,ename,job) values (7979,‘Q’,‘Clerk’);
select * from emp;
–效率
–低

select empno,ename,job from emp where empno not in (select deptno from dept where loc=‘BEIJING’);
–select empno,ename,job from emp where empno exists (select deptno from dept where loc !=‘BEIJING’);

–查看錶結構
describe emp;
–創建一個副本
create table emp_2 as select * from emp;
select * from emp ;
select * from emp_2;
–刪除副本
delete from emp_2;

–創建表
create table student(
stuno number(10) not null,
stuname varchar2(8),
sex char(2),
age int
);
select * from student;
insert into student values (01,‘張三’,‘男’,18);
–增加字段
alter table student add (b varchar2(20));
–刪除字段 僅需要刪除一個字段 關鍵字 column
alter table student drop column class;
–刪除多個字段
alter table student drop (b,c);
–修改字段 alter table … modify
alter table student modify stuname varchar2(60);
–重命名錶名
alter table student rename to stu;
–修改表空間
–'非分區’表移動的一個新的表空間 alter table …move tablespace tbsp_1 表空間名
alter table stu move tablespace tbsp_1;
–查詢表空間以及修改表空間
select TABLESPACE_NAME from all_tables where table_name=‘EMP’;
select TABLESPACE_NAME from all_tables where table_name=‘stu’;
alter table stu move tablespace USERS;
–刪除表 drop刪除表結構 如果存在約束 還需要cascade constraints
drop table emp_2 cascade constraints;
select * from emp_2;
–閃回技術 FLASHBACK TABLE
select * from emp_2;
–select object_name,original_name from recyclebin where original_name=‘emp_2’;
flashback table emp_2 to before drop;
–show recyclebin;
select * from stu for update;
drop table stu cascade constraints;
select object_name,original_name from recyclebin where original_name=‘stu’;
flashback table stu to before drop;
–將表設置爲read only 狀態 read write
alter table stu read only;
alter table stu read write;
–數據完整性 和 約束性
–非空約束 not null

create table books(
bookno number(4) not null,
bookname varchar2(32),
authname varchar2(32),
publish date,
isbn varchar2(32) not null
);

select * from books for update;
–設置bookname 爲非空
alter table books modify bookname not null;

–主鍵約束
create table books_1 (
bookno number(4) not null,
bookname varchar2(32),
authname varchar2(32),
publish date,
isbn varchar2(32) not null,
constraint BOOKS_PK primary key (bookno)
);
–如果創建表的時候沒有創建主鍵
alter table boos_1 add constraint BOOKS_PK primary key (bookno);
–系統自動分配主鍵約束 省略constraint
create table books_2(
bookno number(4) primary key
);
–唯一性約束 UNIQUE 強調所在列不允許有重複值
–UNIQUE約束列有值,不允許重複,但是可以插入多個null

create table Members(
memno number(4) not null,
menname varchar2(32),
phone varchar2(32),
qq varchar2(20) constraint QQ_UK unique,
provcode varchar2(32),
indate date default sysdate,
constraint Mem_PK primary key(mumno)
);
–後期UNIQUE約束
alter table Member add constraint Email_UK unique (phone);
–外鍵約束 保證數據的唯一性

– custemer 和 orders
create table custemer(
cusid number(4) primary key,
cname varchar2(32),
caddress varchar2(32),
cphone varchar2(32)
);
select * from custemer;
create table orders(
oid number(4) primary key,
proname varchar2(32),
procount varchar2(32),
cid number(4)
);
select * from orders;
– alter table 表名 add constraint 約束名 foreign key(外鍵列名) references 主表(主表主鍵列)
alter table orders add constraint fk_order foreign key(cid) references custemer(cusid);
–insert into orders values (7,‘牛仔褲’,‘2000’,7) 外鍵約束

–視圖
–創建視圖

select * from emp order by deptno;
grant create view to scott
create or replace view emp_view as select empno,ename,job from emp where deptno =20;
select * from emp_view;
insert into emp_view values (8889,‘Queny’,‘Aoss’);
insert into emp ( empno,ename,job) values (0909,‘J’,‘D’);
delete from emp_view where JOB=‘Aoss’;
–只讀視圖 + with read only; 不可以進行dml操作
create or replace view emp_view as select empno,ename,job from emp where deptno =20 with only read;
–刪除
drop view emp_view cascade constraints;
select * from dual;
select * from scott.emp;

–bigfile
–alter tablespace 表空間名 resize 大小(1G)
–alter database datafile ‘D:\app\administrator\oradata\atsdb\TS_ECP.ORA’
–表空間temp_1 設置爲臨時表空間
alter database default temprory tablespace temp_1;
–tbs_example 設置爲永久表空間
alter database default tablespace tbs_example;
–修改tbs_test3表空間爲只讀狀態
alter tablespace tb_test3 read only;
–修改tbs_test3表空間爲讀寫狀態
alter tablespace tb_test3 read write;
–重命名錶空間
alter tablespace tb_test3 rename to tb_test4;
–刪除表空間
–including contents-刪除數據
–cascade constraint-刪除相關完整性,主鍵,索引等
drop tablespace tb_test including contents cascade constraint;
–向user表空間添加一個新的數據文件
alter tablespace user add datafile ‘e:\app\Administrator\oardata\orcl\user.dbf’ size 10M autoextend on next 5M unlimited;
–從表空間中刪除數據文件 users 表空間
alter tablespace users drop datafile ‘e:\app\Administrator\oradata\orcl\user.dbf’;

–導入導出
–創建dump

create directory dump_dir as ‘d:dump’;
–授權
grant read,write on directory dump_dir to scott;
–導出表
–expdp jats001/jats001 directory=dump_dir schemas=jats001 dumpfile=JATS002.dmp logfile=impats001.log
expdp scott/scott directory=dump_dir dumpfile=tab.dmp tables=emp,dept;
–導出模式 導出全部對象 schemas
expdp soctt.scott directory=dump_dir dumfile=tab.dmp schemas =scott,hr;
–導出表空間
expdp scott/scott directory=dump_dir dumpfile=tablespce.dmp tablespace=tbsp_1;
–導出全數據庫
expdp scott/scott directory=dump_dir dumpfile=fulldatabase.dmp full=y;

–導入
–導入:impdp jats001/jats001 directory=dump_dir schemas=jats001 dumpfile=JATS001.DMP logfile=impats001.log
–導入表 remap_schema 如果表導入其他模式中,則必須指定remap_schema參數
impdp scott/scott directory=dump_dir dumpfile=tab.dmp tables=scott.dept,scott.emp remap_schema=scott:system
–導入模式
impdp scott/scott directory=dump_dir dumpfile=tablespace.dmp schemas=scott remap_schema=scott:system;
–導入表空間
impdp scott/scott directory=dump_dir dumpfile=tablespace.dmp tablespace=tbsp_1;
–導入全數據庫
impdp scott/scott directory=dump_dir dumpfile=fulldatabase.dmp full=y;
–remap_schema 將源模式中的所有對象裝載到目標模式
–remap_schema = source_schema:target_schema
–source_schema:源模式
–target_schema :目標模式
–remap_tablespace

加粗樣式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章