SQL:
1轧简,DML(數(shù)據(jù)操作語言):主要指的是數(shù)據(jù)庫的查詢與更新的操作梗顺,查詢操作是整個sql語法 中最麻煩也是筆試中最常用的部分。
2灶搜,DDL(數(shù)據(jù)定義語言):主要指的是數(shù)據(jù)對象的創(chuàng)建(表肮雨、用戶岳守、)例如:creat.需要相關(guān)的設(shè)計范式胳挎。
3客情,DCL(數(shù)據(jù)控制語言):主要進行權(quán)限的操作(需要結(jié)合用戶來觀察)帜讲,此部分由DBA負責衅谷。
簡單查詢:
- 1,利用select 子句控制要顯示的數(shù)據(jù)列:
select empno,ename,ename,job,sal from emp;
- 2似将,可以使用distinct來消除重復的數(shù)據(jù)行顯示:
select distinct job from emp;
- 3获黔,select子句可以進行四則運算,可以直接輸出常量內(nèi)容在验,但是對于字符串使用單引號數(shù)字直接編寫玷氏,日期格式按照字符格式:
select empno,ename,(sal*15+(200+100)) income from emp;
- 4,||負責輸出內(nèi)容連接此類的操作很少直接在查詢中出現(xiàn):
select empno||ename from emp;
- 5,where子句一般都寫在from子句之后译红,但是是緊跟著from子句之后執(zhí)行的预茄,where子句控制顯示數(shù)據(jù)行的操作,而select控制數(shù)據(jù)列侦厚,select子句要落后于where子句執(zhí)行耻陕,所以在select子句之中定義的別名無法在where中使用。
限定查詢:
1刨沦,關(guān)系運算符:
select * from emp where sal>1500;
select * from emp where ename ='SMITH'
select empno,ename,job from emp wherejob<>'SALESMAN';
2诗宣,邏輯運算符:
select * from emp where sal>1500 and sal<3000;
select * from emp where sal>2000 or job='CLERK';
select * from emp where not sal >=2000;
3,范圍查詢:
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ù)庫上表示不確定召庞,如果在數(shù)據(jù)列使用null不表示0)
select * from emp where comm is not null;
5,IN操作符(類似于between and 而in給出的是指定的范圍):
select * from emp where empno in (7369,7566,7788,9999);
關(guān)于not in與null的問題:
在使用not in 進行范圍判斷的時候来破,如果范圍有null篮灼,那么不會有任何結(jié)果返回。
6徘禁,模糊查詢:
“-”:匹配任意一位字符诅诱;
“%”:匹配任意的0,1送朱,娘荡,或者多位字符干旁;
- 查詢姓名是以字母A開頭的雇員信息:
select * from emp where ename like 'A%'
- 查詢姓名第二個字母是A的雇員信息:
select * from emp where ename like '_A%';
- 查詢姓名任意位置是A的雇員信息:
select * from emp where ename like '%A%';
查詢排序:
ASC(默認):按照升序排列;
DESC: 按照降序排列炮沐;
- 查詢所有的雇員信息争群,要求按照工資的由高到低:
select * from emp order by sal desc;
- 查詢每個雇員的編號,姓名大年,年薪换薄,按照年薪由低到高排序:
select empno ,ename,sal*12 income from emp order by income;
語句的執(zhí)行順序:from - where -select - order by
.
基礎(chǔ)練習:
1,選擇部門30中的所有員工:
select * from emp where deptno=30;
2鲜戒,列出所有辦事員(clerk)的姓名,編號专控,和部門編號:
select ename,empno,deptno from emp
where job='CLERK';
3抹凳,找出傭金高于薪金的60%的員工:
select * from emp where comm>sal*0.6 ;
4,找出部門10中所有的經(jīng)理(manager)和部門20中所有的辦事員(clerk):
select *
from emp
where (deptno=10 and job='MANAGER' )or(deptno=20 and job='CLERK' );
5,找出部門10中所有的經(jīng)理(manager)遏餐,部門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 comm<100;
8赢底,顯示不帶有“R”的員工姓名:
select *
from emp
whereename not like '%R%';
9失都,顯示姓名字段含有A的所有員工姓名,顯示的結(jié)果按照基本的工資由高到低幸冻,如果工資相同粹庞,則按照雇傭年限由早到晚,如果雇傭日期相同洽损,則按職位排序:
select * from emp where ename like '%A%' order by sal desc,hiredate
asc,job;