日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

ORACLE数据库 常用命令和Sql常用语句

發(fā)布時(shí)間:2023/12/19 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ORACLE数据库 常用命令和Sql常用语句 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ORACLE

賬號(hào)相關(guān)

如何獲取表及權(quán)限

1.COPY表空間
backup scott
exp
登錄管理員賬號(hào)system
2.創(chuàng)建用戶(hù)
create user han identified(認(rèn)證) by mima default tablespace users(默認(rèn)的表空間) quota(配額)10M on users;創(chuàng)建賬號(hào)
分配權(quán)限
grant creatr session,create table,create view to 賬號(hào)
grant connect to han; 賦予賬號(hào)han登錄權(quán)限
3.import the date引入數(shù)據(jù)

過(guò)程全選YES

imp

alter user scott account unlock; 解鎖賬號(hào)



?

對(duì)表的操作?

建表

--新增2個(gè)字段
alter table stu
add(
stu_tel varchar2(11),--電話(huà)
stu_addr varchar2(50)--地址
);
alter table stu
modify (stuName varchar2(20),?
stu_tel varchar2(8)?
);?
刪1段alter table stu
drop column stuId;?
刪除多段alter table stu
drop (stu_tel,stu_addr);

?


insert into table_name values (值1, 值2,....)
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)


update table_name set stuAge = 27 where stuno='1001';

update table_name set stuAge = 27,stuname='abc' where stuno='1002';


delete from emp3;
delete from stu where stuno='1003';

*查
select * from stu where stuno='1001';
select * from stu where stuno='1002';
查詢(xún)所有列
select * from stu t
重復(fù)的只顯示一條
select distinct stuno from stu;

select * from stu where stuno='1006';
select stuno,stuage from stu;

select distinct stuno from stu;
select * from stu where stuno ='1006' and stuage=29;
select * from stu order by stuage desc;
select * from stu order by stuage desc,stuno asc;

備份表
create table stuBAK as
select * from stu;

?

desc emp;描述EMP表有什么字段

?

一單條select語(yǔ)句(必須熟練!)

查詢(xún)語(yǔ)句組成
1.
select * from emp; 查詢(xún)emp表的所有的內(nèi)容
select ename,sal*12 from emp;
select ename,sal*12 anuual_sal(anuual_sal表名可以隨便寫(xiě)2個(gè)單詞用下劃線(xiàn)隔開(kāi) 不然顯示from不存在 不要占from的位置 簡(jiǎn)而言之 名字只能占一個(gè) 如果非得占2個(gè) 必須帶上雙一號(hào)“anuual sal”包住 默認(rèn)引號(hào)內(nèi)的是名字 而且顯示是小寫(xiě) 可以認(rèn)為 “”可以保持你輸入時(shí)的狀態(tài) 當(dāng)然 中文也行) from emp;
select enamel||'哈哈(隨便寫(xiě)字符串)' from emp; ||+'字符' 表示字符串拼接
select enamel||' 哈'哈(隨便寫(xiě)字符串)' from emp; 是錯(cuò)誤的 如果字符串里面有單引號(hào) 需要轉(zhuǎn)義
select distinct deptno from emp; 重復(fù)的只顯示一次
select distinect deptno,job from emp; 這個(gè)有重復(fù)的 這個(gè)不是表里自己和自己比 而是 2個(gè)表 deptno和job 2個(gè)表如果有重復(fù)的 只顯示一次

2.from 那張表?

3.where 出現(xiàn)在單行語(yǔ)句 作為篩選條件優(yōu)先執(zhí)行
select ename,sal from emp where sal=800;
select ename,sal from emp where sal!=800;
select ename,sal from emp where sal>=800 and sal<=1500; 包含800和1500
select ename,sal from emp where sal between and 1500; 包含800和1500
select ename,sal,deptno from emp where sal not in (800,1500); 薪水不是800和1500的
select ename,sal,comm from emp where comm is null; 查出ename,sal,comm里面有null的表 之所以不用=因?yàn)?null是空值就是沒(méi)值的意思
select ename,sal,comm from emp where comm is not null;
select ename,sal from emp where sal in (800,1500,2000); 選出sal=800或者1500或者2000的表
select ename,sal from emp where ename in ('SMITH','KING','JONES'); 選出'SMITH','KING','JONES'表
select ename,sal from emp where deptno=10 or sal>1500;
select ename,sal,deptno from emp where deptno=10 or sal>1500;


4.<--group by>(必須掌握)(需求 每個(gè)部門(mén)的平均薪資)
select avg(sal)from emp group by deptno;

select avg(sal)from emp group by deptno , job;

select deptno ,job,max(sal)from emp group by deptno , job;

select ename,max(sal)from emp;錯(cuò)誤 組函數(shù)(一個(gè)都不能忘!)(多行輸入 只有一行輸出)
select ename from emp where sal=(select max(sal)from emp);

select ename ,max(sal) from emp group by deptno;錯(cuò)誤 ename必須出現(xiàn)在 max()和group by 2個(gè)中一個(gè),只有這樣才唯一
select ename ,max(sal)from emp group by ename; 正確
select deptno,max(sal)from emp group by deptno;正確

5.having(需求每個(gè)部門(mén)平均薪資大于2000的有哪些 不能用where 因?yàn)閣here只能由于一條語(yǔ)句或者說(shuō)一個(gè)表的篩選)
select avg(sal),deptno form emp group by deptno having avg(sal)>2000;


6.order by

select * from dept order by deptno desc; 降序排列
select empno,ename from emp; 升序
select empno,ename from emp order by asc; 升序
select empno,ename from emp where deptno!=10 order by empno asc; 部門(mén)標(biāo)號(hào)不是10的 按升序排列的表
select ename,sal,deptno from emp order by deptno asc; 按部門(mén)編號(hào)升序
select ename,sal,deptno from emp order by sal asc;按薪水升序
select ename,sal,deptno from emp order by sal asc,ename desc; 薪水升序的情況下 名字倒序
select ename,sal*12 anuual_sal from emp where ename like '_A%' and sal>800 order by sal desc;

總結(jié) 1 select avg(sal),字段名 2 from 表 3 where 4 group by 5 having 6 order by

select avg(sal)from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc;


二,子查詢(xún) (把他當(dāng)成一張表!)

select ename ,max(sal) form emp;錯(cuò)誤 因?yàn)檫@個(gè)max(sal)可以對(duì)應(yīng)很多ename 不是一一對(duì)應(yīng)關(guān)系
select ename,sal from emp where sal=(select max(sal)from emp);

需求 有哪些人的工資在平均工資之上

select ename,sal from emp where sal > (select avg(sal)from emp);

表連接

<--內(nèi)連接>
<--寫(xiě)法1 常用>
select d1.dname , d1.loc, e.ename,e.mgr?
from dept d1, emp e?
where d1.deptno = e.deptno;
<--寫(xiě)法2 >
select d1.dname , d1.loc, e.ename,e.mgr?
from dept d1
inner join emp e?
on d1.deptno = e.deptno;

<--左外鏈接>
select d1.dname , d1.loc, e.ename,e.mgr?
from dept d1
left join emp e?
on d1.deptno = e.deptno;
<--右外鏈接>
select d1.dname , d1.loc, e.ename,e.mgr?
from dept d1
right join emp e?
on d1.deptno = e.deptno;

<--聯(lián)合查詢(xún)>
<--聯(lián)合查詢(xún)>
<--相同和合并>
select*from emp
union select * from emp;
<--直接相加>
select*from emp;
union all select *from emp2;


需求:按照部門(mén)分組后 每個(gè)部門(mén)掙錢(qián)最多的那個(gè)人
select ename,sal,deptno from emp where sal = (select max(sal)from emp group by deptno); 錯(cuò)誤(單行 子查詢(xún) 返回 多個(gè)值)
select ename,sal,deptno from emp where sal in (select max(sal)from emp) group by deptno;錯(cuò)誤 (返回的是 符合3個(gè)值(3個(gè)部門(mén)最大工資)之一的所有可能 那么很有可能別的部門(mén)不是最大值的是別的部門(mén)的最大值也會(huì)選出來(lái) 結(jié)果就不對(duì)了!)

<--錯(cuò)誤示范2個(gè)>
select ename,sal,deptno from emp where sal=(select max(sal)from emp) group by deptno;
select ename,sal,deptno from emp where sal in (select max(sal)from emp) group by deptno;

<--正確>
<--正確 每個(gè)部門(mén)掙錢(qián)最多的那個(gè)人>
1.(select max(sal)max_sal,deptno from emp group by deptno)t 命名這倆表是t join是連接
2.on (emp.sal=t.max_sal and emp.deptno = t.deptno); on是限制條件

select ename,sal from emp

join(select max(sal)max_sal,deptno from emp group by deptno)t

on (emp.sal=t.max_sal and emp.deptno = t.deptno);

<-- 需求每個(gè)部門(mén)平均薪水的等級(jí)(表連接 1先寫(xiě)出每個(gè)部門(mén)的平均薪水等級(jí) 2 和薪水等級(jí)那個(gè)表做連接)>

select deptno,avg_sal,grade from
(select deptno,avg(sal)avg_sal from emp group by deptno)t
join salgrade s
on (t.avg_sal between s.losal and s.hisal);

<--員工名字和經(jīng)理人名字>
select empno,ename,mgr from emp;
select empno,ename,mgr from emp;
<-- 自連接 自身和自身鏈接 一個(gè)表看成2個(gè)一樣表起2個(gè)別名做鏈接>
select e1.ename ,e2.ename from emp e1 ,emp e2 where e1.mgr=e2.empno;
<--92年老版本過(guò)濾篩選條件where里面寫(xiě)鏈接表的條件和過(guò)濾條件的寫(xiě)法,99年將連接條件和過(guò)濾條件分開(kāi)寫(xiě)>
select ename ,dname,grade from emp e,dept d,salgrade s
where e.deptno=d.deptno and e.sal between s.losal and s.hisal and
job<>'clerk';
<--過(guò)濾和鏈接條件 99年的新寫(xiě)法 推薦這個(gè)寫(xiě)!>
<--等值鏈接>
<--cross join 交叉連接>
select ename ,dname from emp cross join dept;

<--92語(yǔ)法>
select ename,dname from emp,dept where emp.deptno =dept.deptno;
<--99年語(yǔ)法>
select ename,dname from emp join dept on (emp.deptno=dept.deptno);

<--非等值鏈接>
select ename,grade from emp e join salgrade s on (e.sal between s.losal and s.hisal);

<--3張表做鏈接>
select ename ,dname,grade from emp e
join dept d on (e.deptno=d.deptno)
join salgrade s on(e.sal between s.losal and s.hisal)
where ename not like '_A%';

<--自連接 用99新語(yǔ)法>
select e1.ename,e2.ename from emp e1 join emp e2 on(e1.mgr=e2.empno);
<--外鏈接 左外鏈接和右外鏈接 全外鏈接>
select e1.ename,e2.ename from emp e1 left join emp e2 on (e1.mgr=e2.empno);
select e1.ename,e2.ename from emp e1 right join emp e2 on (e1.mgr=e2.empno);

select ename , dname from emp e
join dept d
on(e.deptno = d.deptno);

select ename , dname from emp e
right outer join dept d
on(e.deptno = d.deptno);
<--全外鏈接>
select ename , dname from emp e
full join dept d
on(e.deptno=d.deptno);

<--每個(gè)部門(mén)平均薪水等級(jí)>
select deptno,avg_sal,grade from
(select deptno,avg(sal)avg_sal from emp group by deptno)t
join salgrade s1
on (t.avg_sal between s1.losal and s1.hisal);

<--每個(gè)人的薪水等級(jí)>
select deptno,ename,grade from emp
join salgrade s
on (emp.sal between s.losal and s.hisal);
<--每個(gè)部門(mén)(每個(gè)員工平均薪水等級(jí)的)平均薪水等級(jí)>
select deptno, avg(grade)from
(select deptno,ename,grade from emp
join salgrade s
on (emp.sal between s.losal and s.hisal))t
group by deptno;
<-- 那些雇員是經(jīng)理?>
select ename from emp where empno in(select mgr from emp);
select ename from emp where empno in(select distinct mgr from emp);

select stuno num,stuage age from stu;

?

子查詢(xún)
select deptno from emp where ename ='SCOTT';

in慎用 尤其結(jié)果大的
select * from emp where deptno in('10','20');
select * from emp where deptno not in('10','20');

select * from emp where( deptno='10' or deptno= '20') and sal>2000;

e1是emp的取名
select * from emp e1 where sal> (select avg(sal)from emp e2 where e1.deptno=e2.deptno);


select dname from dept where deptno where =(deptno from emp where sal>3000);

select dname from dept d where exists (select * from emp e where d.deptno = e.deptno and sal>3000);

<--不相關(guān)子查詢(xún)>
select dname from dept where deptno =(select deptno from emp where sal>3000);
select dname from dept where deptno = (select deptno from emp where ename ='SMITH');


?


三,騷操作
select stuno "學(xué)號(hào)“stuage 年齡 from stu;
拼接字符
select 'test'||to_char(sysdate,'yyyy-mm-dd')from dual;
select 's'|| stuno "學(xué) 號(hào)“stuage 年齡 from stu;

select '測(cè)試','s'|| stuno "學(xué) 號(hào)“stuage 年齡 from stu;

select to_char(sysdate,'yyyy-mm-dd')from dual;
select to_char(sysdate,'yyyy"年"mm"月"dd"日 "hh24:mi:ss')from dual;

?

四,單行函數(shù)
select to_number('234')from dual;
select to_date('2017-09-09','yyyy-mm-dd')from dual;

select distinct stuno from stu;

?

五,聚合函數(shù)

5個(gè)聚合函數(shù)
所以數(shù)字類(lèi)型的數(shù)字相加用和這個(gè)
(1)計(jì)數(shù)
select count(*)from emp;
(2)求和
select sum(empno) from emp;

(3)最小
select min(sal)from emp;
(4)最大
select max(sal)from emp;
(5)平均
select avg(sal)from emp;

?

分析函數(shù)(略)

截取字符函數(shù)
select substr('sdsfds',2) from dual;

select substr('sdsfds',2,4) from dual;
select substr('20170909120910',1,8) from dual;

NVL函數(shù)
如果是空 就打印后邊的4
select nvl(null,4)from dual;
打印5
select nvl(5,4)from dual;

select nvl(comm,0)*1.1 from emp; 工作經(jīng)驗(yàn)
上面為啥這么寫(xiě)呢?因?yàn)槿绻鹀omm是null的話(huà) *1.1不合適 所以 我們這么寫(xiě)
如果是null就改成0

判斷字符是否一樣 1一樣 0不一樣
select decode('男','女',1,0)from dual; 0
select decode('男','男',1,0)from dual; 1
select decode('男','女',1,'男',0,2)from dual;
select decode('男','女',1,'男',0,2)from dual;
select decode('男','女',1,'',0,2)from dual;



?

<分頁(yè)查詢(xún)>
偽列
<--分頁(yè)>
<--偽列>
<rowid rownum>
;
<--先抓取5個(gè) 然后排序>
select sal from emp where rownum<6
order by sal desc;
<--收入前五>
select *from(select * from emp order by sal desc) where rownum<6;
select *from(select * from emp order by sal desc) where rownum<16;

<--先找到需要排序的表排序 2然后 對(duì)這個(gè)表進(jìn)行rownum固化 3.對(duì)固化后的表加限定條件>
select * from (
select e.*,rownum rn from
(select * from emp order by sal desc)
e
)
where rn=5;

數(shù)據(jù)遷移
1、找到數(shù)據(jù)文件 執(zhí)行select*from 表; 導(dǎo)出sql文件:點(diǎn) 綠色的按鈕 導(dǎo)出查詢(xún)結(jié)果 得到sql文件
2.建表 可以COPY→ 查看 sql(v) 里面有多余的部分 不要 只要建表操作
3.導(dǎo)入sql 新建命令窗口 @ 找到要導(dǎo)入的sql 文件

?

oracle 數(shù)據(jù)庫(kù)編程語(yǔ)言 pl/sql 過(guò)程化語(yǔ)言和結(jié)構(gòu)化語(yǔ)言的語(yǔ)言
存儲(chǔ)過(guò)程

declare聲明 變量 常量賦值 c_rate_iner number(7,2) comstant :=1.10; 變量 select ename,sal*c_rate_iner into v_name,v_rate from emp where empno='7788';
begin sql語(yǔ)句
exception 異常
end;

loop循環(huán)
DBMS_OUTPUT.PUT_LINE(''+變量)


if 條件 then
goto ming

else null;

end if


end loop;
<<ming>>

?

--變量類(lèi)型 %type屬性 他的意思可以根據(jù)別的表的數(shù)據(jù)類(lèi)型定義自己的數(shù)據(jù)類(lèi)型,
--表的某個(gè)字段數(shù)據(jù)類(lèi)型 綁定 自己的數(shù)據(jù)類(lèi)型,表的某個(gè)字段類(lèi)型變成什么,他就跟著變成什么 >
例子
declare
--v_num 的數(shù)據(jù)類(lèi)型是emp表下面的empno這個(gè)字段的數(shù)據(jù)類(lèi)型
v_num emp.empno%type :=5566;
begin
dbms_output.put_line(v_num);
end;

--table數(shù)據(jù)類(lèi)型 相當(dāng)于java里面的數(shù)組 自定義類(lèi)型 先聲明一種類(lèi)型 再用這個(gè)類(lèi)型 聲明一個(gè)變量
declare
--標(biāo)準(zhǔn)寫(xiě)法 type表明 我要自定義一個(gè)類(lèi)型 type_table_emp_empno是類(lèi)型名字 is table of 是一張表
--emp.empno%type表的類(lèi)型是emp這個(gè)表的empno這個(gè)字段的類(lèi)型(綁定) index by索引下標(biāo) binary_integer索引下標(biāo)的類(lèi)型
type type_table_emp_empno is table of emp.empno%type index by binary_integer;
--v_empnos 類(lèi)型名 和定義好的數(shù)組類(lèi)型
v_empnos type_table_emp_empno;
begin
v_empnos(0):=7369;
v_empnos(1):=7370;
v_empnos(2):=7860;
v_empnos(-1):=9999;
dbms_output.put_line(v_empnos(-1));
end;

--record類(lèi)型 相當(dāng)于java里面的類(lèi)
declare
type type_record_dept is record
(
deptno dept.deptno%type,
dname dept.dname%type,
loc dept.loc%type
);

v_temp type_record_dept;

begin
v_temp.deptno:=50;
v_temp.dname:='aaaa';
v_temp.loc:='beijing';
dbms_output.put_line(v_temp.deptno||' '||v_temp.dname);
end;

--

使用 %rowtype屬性 聲明 record變量 (解決了原來(lái)的表dept改變了 record會(huì)跟著編號(hào) 解決了維護(hù)麻煩的問(wèn)題)
declare
v_temp dept%rowtype;
begin
v_temp.deptno:=50;
v_temp.dname:='aaaa';
v_temp.loc:='beijing';
dbms_output.put_line(v_temp.deptno||' '||v_temp.dname);
end;

轉(zhuǎn)載于:https://www.cnblogs.com/PoeticalJustice/p/7608575.html

總結(jié)

以上是生活随笔為你收集整理的ORACLE数据库 常用命令和Sql常用语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。