本文主要介紹的是關(guān)于Oracle中的簡(jiǎn)單查詢(xún)和限定查詢(xún),下面話不多說(shuō),來(lái)一起看看吧。
SQL:
1,DML(數(shù)據(jù)操作語(yǔ)言):主要指的是數(shù)據(jù)庫(kù)的查詢(xún)與更新的操作,查詢(xún)操作是整個(gè)sql語(yǔ)法 中最麻煩也是筆試中最常用的部分。
2,DDL(數(shù)據(jù)定義語(yǔ)言):主要指的是數(shù)據(jù)對(duì)象的創(chuàng)建(表、用戶、)例如:creat.需要相關(guān)的設(shè)計(jì)范式。
3,DCL(數(shù)據(jù)控制語(yǔ)言):主要進(jìn)行權(quán)限的操作(需要結(jié)合用戶來(lái)觀察),此部分由DBA負(fù)責(zé)。
簡(jiǎn)單查詢(xún):
1,利用select
子句控制要顯示的數(shù)據(jù)列:
select empno,ename,ename,job,sal from emp;
2,可以使用distinct
來(lái)消除重復(fù)的數(shù)據(jù)行顯示:
select distinct job from emp;
3,select
子句可以進(jìn)行四則運(yùn)算,可以直接輸出常量?jī)?nèi)容,但是對(duì)于字符串使用單引號(hào)數(shù)字直接編寫(xiě),日期格式按照字符格式:
select empno,ename,(sal*15+(200+100)) income from emp;
4,||負(fù)責(zé)輸出內(nèi)容連接此類(lèi)的操作很少直接在查詢(xún)中出現(xiàn):
select empno||ename from emp;
5,where
子句一般都寫(xiě)在from
子句之后,但是是緊跟著from
子句之后執(zhí)行的,where
子句控制顯示數(shù)據(jù)行的操作,而select
控制數(shù)據(jù)列,select
子句要落后于where
子句執(zhí)行,所以在select
子句之中定義的別名無(wú)法在where
中使用。
限定查詢(xún):
1,關(guān)系運(yùn)算符:
select * from emp where sal>1500; select * from emp where ename ='SMITH' select empno,ename,job from emp where job>'SALESMAN';
2,邏輯運(yùn)算符:
select * from emp where sal>1500 and sal3000; select * from emp where sal>2000 or job='CLERK'; select * from emp where not sal >=2000;
3,范圍查詢(xún):
select * from emp where sal between 1500 and 2000; select * from emp where hiredate between '01-1月-1981'and'31-12月-1981';
4,空判斷(空在數(shù)據(jù)庫(kù)上表示不確定,如果在數(shù)據(jù)列使用null不表示0)
select * from emp where comm is not null;
5,IN操作符(類(lèi)似于between and
而in給出的是指定的范圍):
select * from emp where empno in (7369,7566,7788,9999);
關(guān)于not in
與null
的問(wèn)題:
在使用not in
進(jìn)行范圍判斷的時(shí)候,如果范圍有null
,那么不會(huì)有任何結(jié)果返回。
6,模糊查詢(xún):
“-”:匹配任意一位字符;
“%”:匹配任意的0,1,,或者多位字符;
查詢(xún)姓名是以字母A開(kāi)頭的雇員信息:
select * from emp where ename like 'A%'
查詢(xún)姓名第二個(gè)字母是A的雇員信息:
select * from emp where ename like '_A%';
查詢(xún)姓名任意位置是A的雇員信息:
select * from emp where ename like '%A%';
查詢(xún)排序:
ASC(默認(rèn)):按照升序排列;
DESC: 按照降序排列;
查詢(xún)所有的雇員信息,要求按照工資的由高到低:
select * from emp order by sal desc;
查詢(xún)每個(gè)雇員的編號(hào),姓名,年薪,按照年薪由低到高排序:
select empno ,ename,sal*12 income from emp order by income;
語(yǔ)句的執(zhí)行順序:from
- where
-select
- order by
基礎(chǔ)練習(xí):
1,選擇部門(mén)30中的所有員工:
select * from emp where deptno=30;
2,列出所有辦事員(clerk)的姓名,編號(hào),和部門(mén)編號(hào):
select ename,empno,deptno from emp where job='CLERK';
3,找出傭金高于薪金的60%的員工:
select * from emp where comm>sal*0.6 ;
4,找出部門(mén)10中所有的經(jīng)理(manager)和部門(mén)20中所有的辦事員(clerk):
select * from emp where (deptno=10 and job='MANAGER' )or(deptno=20 and job='CLERK' );
5,找出部門(mén)10中所有的經(jīng)理(manager),部門(mén)20中的所有辦事員(clerk),以及既不是經(jīng)理又不是辦事員但是工資高于等于2000的所有員工資料:
select * from emp where (deptno=10 and job='MANAGER')or(deptno=20 and job='CLERK')or(job! ='MANAGER'and job!='CLERK' and sal>=2000);
select * from emp where (deptno=10 and job='MANAGER')or(deptno=20 and job='CLERK')or(job not in ('CLERK','MANAGER') and sal>=2000);
6,找出收取傭金的員工的不同工作:
select distinct job from emp where comm is not null;
7,找出收取傭金或者收取的傭金低于100的員工:
select distinct job from emp where comm is null or comm100;
8,顯示不帶有“R”的員工姓名:
select * from emp where ename not like '%R%';
9,顯示姓名字段含有A的所有員工姓名,顯示的結(jié)果按照基本的工資由高到低,如果工資相同,則按照雇傭年限由早到晚,如果雇傭日期相同,則按職位排序:
select * from emp where ename like '%A%' order by sal desc,hiredate asc,job;
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
標(biāo)簽:和田 承德 固原 武漢 青島 周口 開(kāi)封 甘肅
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle基礎(chǔ)學(xué)習(xí)之簡(jiǎn)單查詢(xún)和限定查詢(xún)》,本文關(guān)鍵詞 Oracle,基礎(chǔ),學(xué),習(xí)之,簡(jiǎn)單,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。