精心整理sql語(yǔ)句,含練習(xí)題和思維導(dǎo)圖,還有什么學(xué)不會(huì)

先來(lái)一張思維導(dǎo)圖

圖太大了,就不打開給大家展示了

圖中主要是以下幾個(gè)部分的內(nèi)容

而今天講解沒那么高大上,就是我總結(jié)的一些常用的sql語(yǔ)句,即時(shí)分享,也是自己的知識(shí)點(diǎn)的總結(jié)和整理

第一節(jié)課:sql語(yǔ)言介紹及基本查詢sql學(xué)習(xí)

1、數(shù)據(jù)庫(kù)表的介紹

emp表:?jiǎn)T工表

dept表:部門表

salgrady:薪資水平表

Balance:

2、基本的查詢語(yǔ)句:

? ? 知識(shí)點(diǎn):

select * from 表名? 查詢表中所有的數(shù)據(jù)

select 字段名1,字段名2,..from 表名 查詢指定字段的所有值,不同字段之間使用逗號(hào)隔開

使用算術(shù)表達(dá)式:select 字段名(運(yùn)算符),字段名(運(yùn)算符)...from 表名

案例:

select * from emp---查詢所有員工信息

select ename,job,sal from emp--查詢員工姓名,工作,薪資

查詢所有員工一年的工資,以及每位員工年終3000后的所有薪資(不包括津貼)

select ename,job,sal*12,sal*12+3000 from emp? 使用運(yùn)算符

第二節(jié)課:

1、使用別名

as可以省略不寫,如果有特殊字符獲取空格使用雙引號(hào)添加別名

select ename 姓名,job 工作,sal*12 年資,sal*12+3000 年薪 from emp? 直接在字段后面添加別名

select ename "姓名",job 工作,sal*12 ’年資’,sal*12+3000 年薪 from emp? 使用雙引號(hào)添加別名,不能使用單引號(hào)

select ename as 姓名,job "工作",sal*12 年資,sal*12+3000 年薪 from emp? 使用as關(guān)鍵字添加別名

select ename "姓? 名",job 工作,sal*12 年資,sal*12+3000 年薪 from emp? 如果別名中有空格或者其他特殊字符,使用雙引號(hào)

2、使用鏈接符:

select ename,job,sal from emp;

select ename||job,sal from emp;? 使用||符號(hào)進(jìn)行字符鏈接

select ename||'? 的工作是? '||job||'? 并且月工資是? '||sal from emp;? 字符鏈接,普通字符使用單引號(hào)

3、去除重復(fù) distinct

select * from emp;

select job from emp--發(fā)現(xiàn)工作是重復(fù)的,而工作種類是需要去除重復(fù)的

問題:查看員工工作種類

select distinct job from emp? 使用distinct關(guān)鍵字去除重復(fù)值

問題;怎么多字段去除重復(fù)

select distinct job,sal from emp;? 多字段去除重復(fù),去除的是重復(fù)的一整條信息

4、排序

知識(shí)點(diǎn):

select 字段名,字段名...from 表名 order by 字段名

select *from 表名 order by 字段名

講解:

--查詢員工姓名,工作,薪資

select ename,job,sal from emp

--查詢按照薪資排序的員工姓名,工作,薪資

select ename,job,sal from emp order by sal;? 使用order by排序

select ename,job,sal from emp order by sal desc;? 使用desc關(guān)鍵從大到小排序

select ename,job,sal from emp order by sal asc;? 使用asc關(guān)鍵從小到大排序,默認(rèn)是從小到大排序

select * from emp order by sal,ename? 多字段作為排序條件的時(shí)候,優(yōu)先按照第一個(gè)字段排序,然后依次按照其他字段排序。

select ename,job,sal 工資 from emp order by 工資? 使用別名

select ename,job,sal from emp order by sal*12? 使用算術(shù)表達(dá)式

第三節(jié)課:簡(jiǎn)單的where子句

使用where條件進(jìn)行結(jié)果篩選

語(yǔ)法:select * from 表名 where 條件;注意:條件中字段值區(qū)分大小寫,字段名不區(qū)分大小寫,字段值使用單引號(hào)括起來(lái)

1、在where中使用算術(shù)表達(dá)式 = ,< , > ,>= ,<= ,<>

2、使用order by 對(duì)篩選結(jié)果進(jìn)行排序,order by 出現(xiàn)在where后面

查看工資等于1250的員工信息

select * from emp where sal='1250'--篩選條件是個(gè)數(shù)字也可以使用單引號(hào)

查看工作等于CLERK的員工信息

select * from emp where job='CLERK'--在篩選條件中字段值如果是字符需要加上單引號(hào)

select * from emp where job='clerk'--在sql語(yǔ)句中字段的值是區(qū)分大小寫的

select * from emp where JOB='CLERK'--在sql中字段是不區(qū)分大小寫的

查看工資大于1250的員工姓名和工作

select ename,job ,sal from emp? where sal>'1250' order by sal--使用order by 對(duì)篩選結(jié)果進(jìn)行排序,order by 出現(xiàn)在where后面

查看工資大于等于2000的員工信息

select * from emp where sal>=2000;

查看工資小于等于2000的員工信息;

select * from emp where sal<=2000

查看工資不等于1500的員工信息

select * from emp where sal<>1500 order by sal

查看入職日期在81年后的員工信息

select * from emp where hiredate>'1981年12月31號(hào)'

select * from emp where hiredate>'1981/12/31'

select * from emp where hiredate>'31-12月-1981'--使用日期的默認(rèn)格式查詢符合要求的數(shù)據(jù),日-月-年

第四節(jié)課:where子句使用關(guān)鍵字

使用where子句進(jìn)行結(jié)果的篩選

知識(shí)點(diǎn):where查詢條件中使用關(guān)鍵字

1、and 用于多條件的與篩選:select * from 表名 where 條件 and 條件 and 條件....

2、or 用于多條件的或篩選: select * from 表名 where 條件 or 條件 or 條件....

3、in 用于多條件的或篩選: select * from 表名 where 字段名 in(值,值,值....)

4、like用于模糊查詢:? ? ? select * from 表名 where 字段名 like '%值%' 包含

5、is null 和is not null 用來(lái)判斷字段是否為空? select * from 表名 where 字段名 is null


? 講解: 在where子句中使用關(guān)鍵字(and, or ,like, is null,is not null,between and,)

--查詢工資在2000-3000之間的員工信息

select * from emp where sal>=2000 and sal<=3000--使用and關(guān)鍵字進(jìn)行"與"的多條件篩選;

select * from emp where sal between 2000 and 3000;--使用between and 關(guān)鍵字進(jìn)行篩選;

--查詢工作為SALESMAN,ANALYST,MANAGER的員工信息

select? * from emp where job='SALESMAN' or job='ANALYST' or job='MANAGER'--使用or關(guān)鍵字進(jìn)行"或"的多條件篩選

select * from emp where job in('SALESMAN','ANALYST','MANAGER');--使用in關(guān)鍵字進(jìn)行"或"的多條件篩選

select? * from emp where job='ANALYST'

--查詢姓名中包含s的,以s開頭的,以s結(jié)尾的,第二個(gè)字符為A的。

select * from emp where ename like '%S%';--使用like關(guān)鍵字,姓名中包含S的,%代表任意多個(gè)字符

select * from emp where ename like 'S%';--使用like關(guān)鍵字,以S開頭的

select * from emp where ename like '%S';--以S結(jié)尾的

select * from emp where ename like '_A%'--使用"_"指定位置包含指定字符的信息,"_"代表任意一個(gè)字符

--------------查詢名字中包含下劃線的用戶信息

select * from emp where ename like '%A_%'escape 'A';--使用escape關(guān)鍵字將普通字符設(shè)置成為轉(zhuǎn)譯字符。

--查詢有津貼的員工信息

select * from emp where comm is not null;

select * from emp where comm is null;--查詢沒有津貼的員工信息

第五節(jié)課:函數(shù)學(xué)習(xí)

--查詢工作為SALESMAN,MANAGER并且工資大于2500的員工信息

--1、使用小括號(hào)提升where篩選條件的執(zhí)行優(yōu)先級(jí)別

--2、and的優(yōu)先級(jí)別高于or

select * from emp

select * from emp where job='SALESMAN' or job='MANAGER' and sal>2500

select * from emp where (job='SALESMAN' or job='MANAGER') and sal>2500

-----------------------------------------------------------------------------------------------------------

使用函數(shù) 單行函數(shù) 多行函數(shù) 轉(zhuǎn)換函數(shù) 其他函數(shù)

--單行函數(shù)學(xué)習(xí):不改變?cè)紨?shù)據(jù),只改變結(jié)果

---1、字符函數(shù)

select * from emp;

select INITCAP(ename) from emp;--initcap函數(shù)將首字母大寫

select lower(ename)from emp;--lower 字母小寫

select replace(ename,'S','M')? from emp;--replace 替換

--2、數(shù)值函數(shù)--Math

----偽表 dual

select * from dual

select abs(-3) 絕對(duì)值,ceil(3.1415926)向上取整,floor(3.1415926)向下取整,power(2,3)冪,round(3.4)四舍五入 from dual

--3、日期函數(shù)

select months_between('13-12月-2016','13-10月-2016') from dual--months_between兩個(gè)日期之間的月份數(shù)

第六節(jié)課:多行函數(shù)學(xué)習(xí)

多行函數(shù) :

? max: max(字段名) 返回此字段的最大值

? min:min(字段名) 返回此字段的最小值

? avg:avg(字段名) 返回平均值

? sum:sum(字段名) 返回字段的和

? count:count

--count(*),用來(lái)查詢表中有多少條記錄

--count(字段名),用來(lái)查詢某個(gè)字段有值的個(gè)數(shù)

--count(distinct 字段名),可以先去除重復(fù)再計(jì)數(shù)。

? 注意:

? --多行函數(shù)不能和普通字段直接出現(xiàn)在查詢語(yǔ)句中,除非group by

? --多行函數(shù)和單行函數(shù)不能直接出現(xiàn)在查詢語(yǔ)句中,除非group by? ? ? ?

查看員工的最高工資

select max(sal),ename from emp--多行函數(shù)不能和普通字段直接出現(xiàn)在查詢語(yǔ)句中,除非group by

select max(sal),lower(ename) from emp--多行函數(shù)和單行函數(shù)不能直接出現(xiàn)在查詢語(yǔ)句中,除非group by

select ename from emp

查看員工的最低工資

select min(sal) from emp

查看員工的平均工資

select avg(sal) from emp

查看所有的員工工資之和

select sum(sal) from emp

查詢公司有多少員工

select * from emp

select count(*) from emp--使用count(*)來(lái)查看一張表中有多少條記錄

查詢有津貼的員工人數(shù)

select count(comm) from emp--使用count(字段名),查詢?cè)撟侄斡兄档挠涗洈?shù)

select count(ename) from emp

查詢公司有多少工作種類

select count(job) from emp

select? count(distinct job) from emp?

========================================================================================================================================

第一節(jié)課:轉(zhuǎn)換函數(shù)學(xué)習(xí)

轉(zhuǎn)換函數(shù):在轉(zhuǎn)換的時(shí)候改變的是數(shù)據(jù)的類型,數(shù)據(jù)內(nèi)容不會(huì)改變,可以指定格式。

1、to_number:將數(shù)字字符轉(zhuǎn)換為數(shù)字類型的數(shù)值,to_number(數(shù)字字符)

2、to_char:將數(shù)字轉(zhuǎn)換為字符類型,將日期轉(zhuǎn)換為字符類型 to_char(數(shù)字/日期)

3、to_date:將字符類型的日期轉(zhuǎn)換為日期類型: to_date(char)

--------------------------------------------------------------------------------------

number-->char 轉(zhuǎn)換的時(shí)候使用的是默認(rèn)格式,

select? to_char(123) from dual--將數(shù)字轉(zhuǎn)換為字符類型,使用to_char(number)

select sal, to_char(sal) from emp

number-->char 使用指定的格式

to_char(number,'格式'),格式:

$代表美元符,9代碼數(shù)字占位。例如,L999,999,999表示三位一組的顯示方式L代表人民幣符號(hào),0代表保留數(shù)字個(gè)數(shù),不足使用0補(bǔ)充。

select to_char(sal),to_char(sal,'L999,999,999') from emp

select to_char(sal),to_char(sal,'L0000.00') from emp

char--->number? to_number(數(shù)字字符)

select to_number('123') from dual

char--->date,轉(zhuǎn)換的字符必須是日期格式的字符串,默認(rèn)格式 dd-mm-yyyy

注意:因?yàn)樽址泻芏?,所以在字符轉(zhuǎn)換為日期的時(shí)候,需要指定格式,因?yàn)槿掌谑蔷邆湟欢ǜ袷降淖址M合。

字符轉(zhuǎn)換為日期的時(shí)候,指定的格式為字符的日期順序,無(wú)需指定間隔符。

yyyy:表示年? mm:表示月? dd表示日

select to_date('05-12月-2016') from dual;

select to_date('2016-05-12','yyyy-mm-dd') from dual--使用指定的格式將指定的日期字符串轉(zhuǎn)換為日期

select to_date('12-05-2016','mm/dd/yyyy') from dual

date--->char

注意:因?yàn)槿掌诒旧砭途邆湟欢ǖ母袷皆诓皇侵付ǜ袷降那闆r下會(huì)默認(rèn)使用dd-mm-yyyy格式顯示數(shù)據(jù)

指定的格式會(huì)作為日期轉(zhuǎn)換為字符串類型的顯示格式存在。例如:

yyyy-mm-dd'? ‘2016-12-05’

yyyy/mm/dd'? ? '2016/12/05'

yyyy"年"mm"月"dd"日"'? ? 2016年12月05日

select hiredate,to_char(hiredate) from emp--日期轉(zhuǎn)換為字符的時(shí)候,不指定格式使用默認(rèn)格式:dd-mm-yyyy

select hiredate,to_char(hiredate,'yyyy-mm-dd') from emp;--使用指定格式將日期轉(zhuǎn)換為字符串類型

select hiredate,to_char(hiredate,'yyyy/mm/dd') from emp;--使用指定格式將日期轉(zhuǎn)換為字符串類型

select hiredate,to_char(hiredate,'yyyy"年"mm"月"dd"日"') from emp;--使用指定格式將日期轉(zhuǎn)換為字符串類型

查詢?nèi)肼毴掌谠?1年10月20日后的員工信息

第一種:自動(dòng)轉(zhuǎn)型

select * from emp where hiredate>'20-10月-1981'

第二種:將日期轉(zhuǎn)換為字符串

select * from emp where to_char(hiredate,'yyyy-mm-dd')>'1981-10-20'

第三種:

select * from emp where hiredate>to_date('1981-10-20','yyyy/mm/dd')

第二節(jié)課:其他函數(shù):

單行函數(shù):lower

多行函數(shù):min max sum avg count

轉(zhuǎn)換函數(shù):to_number to_char to_date

-----------------------------------------------------------------------------------------

其他函數(shù):

1、nvl():nvl(字段名,執(zhí)行)--相當(dāng)于java中的if條件判斷

2、nvl2():nvl2(字段名,值,值)--相當(dāng)于java中的If(){}else{}判斷

3、decode():decode(字段名,條件1,執(zhí)行內(nèi)容1,條件2,執(zhí)行內(nèi)容2,條件3,執(zhí)行內(nèi)容3,默認(rèn)執(zhí)行內(nèi)容)

相當(dāng)于java中if(){}else if(){}else if(){}...else{}

查詢所有員工的月薪及姓名和工作

select * from emp

select ename,sal 基本工資,comm 績(jī)效,sal+comm 月薪 from emp

select ename,sal 基本工資,comm 績(jī)效,sal+nvl(comm,0)月薪 from emp

查詢所有員工的月薪及姓名和工作

select ename,sal 基本工資,comm 績(jī)效,nvl2(comm,sal+comm,sal)月薪 from emp

顯示員工的職稱

select ename,job,decode(job,'MANAGER','經(jīng)理','SALESMAN','銷售人員','普通員工') from emp

第三節(jié)課:使用group by分組

在多行函數(shù)中不能直接使用普通字段,除非group by

在多行函數(shù)中不能直接使用單行函數(shù),除非group by

group by學(xué)習(xí):

---1、使用group by進(jìn)行數(shù)據(jù)分組 select 多行函數(shù),分組字段 from 表名 group by 分組字段

---2、多字段進(jìn)行分組的時(shí)候,按照字段順序進(jìn)行分組,第一條件分組完成后,繼續(xù)使用其他條件依次分組。

---3、group by依然可以和order by 聯(lián)合使用

---4、可以和單行函數(shù)聯(lián)合進(jìn)行分組,注意使用了單行函數(shù)那么在查詢語(yǔ)句中必須也要使用

查詢最高工資和員工數(shù)

select max(sal),count(*) from emp

查詢不同部門的最高工資

select * from emp order by deptno

select? deptno,max(sal) from emp group by deptno--使用group進(jìn)行分組查詢,分組的字段可以出現(xiàn)在查詢中,其他字段依然不可以

查詢不同工作崗位的員工數(shù)

select * from emp for update

select lower(job),count(*) from emp group by lower(job)--使用單行函數(shù)進(jìn)行分組

查詢不同部門的不同工作崗位的人數(shù)

select deptno,job ,count(*) from emp group by deptno,job--使用多字段組合進(jìn)行分組

select deptno,job ,count(*) from emp group by deptno,job order by deptno

查詢不同部門的不同工作崗位的并且人數(shù)大于1的信息t count(*) from emp where count(*)>3 group by deptno

selec

select deptno,job ,count(*) from emp where count(*)>1 group by deptno,job order by deptno

查詢部門號(hào)大于10的不同部門的不同工作崗位的人數(shù)

select deptno,job ,count(*) from? emp where deptno>10 group by deptno,job order by deptno

使用having進(jìn)行分組后篩選

having學(xué)習(xí):

? --1、使用group by分組后在進(jìn)行數(shù)據(jù)篩選的時(shí)候,where中不能出現(xiàn)多行函數(shù),所以使用新的關(guān)鍵字having進(jìn)行條件篩選

? --2、where條件篩選的執(zhí)行順序:from-->where--->group -->select

? --3、having條件篩選的執(zhí)行順序:from-->group by -->having-->select

? --4、where的執(zhí)行效率比having要高,能使用where的情況下盡量不要使用having

查詢不同部門的不同工作崗位的并且人數(shù)大于1的信息

使用where語(yǔ)句進(jìn)行篩選

where條件語(yǔ)句sql執(zhí)行順序:from-->where--->group -->select

select count(*) from emp where count(*)>1 group by deptno,job

使用having語(yǔ)句進(jìn)行篩選

having條件語(yǔ)句的執(zhí)行順序:from-->group by -->having-->select

select deptno, count(*) from emp? group by deptno having count(*)>5

select deptno,job ,count(*) from? emp? group by deptno,job? having deptno>10 order by deptno

第四節(jié)課:插入數(shù)據(jù)學(xué)習(xí)及數(shù)據(jù)的備份

單表查詢語(yǔ)句(select)

1、插入數(shù)據(jù)(insert)

1、語(yǔ)法規(guī)范? insert into 表名(字段1,字段2,字段3,....)values('值1','值2','值3'.....)

2、主鍵:用來(lái)唯一標(biāo)識(shí)一條數(shù)據(jù)的字段通常設(shè)置主鍵,主鍵是唯一不可以重復(fù)的

3、如果插入的數(shù)據(jù)是全字段數(shù)據(jù),字段可以省略不寫。部分字段,必須加上字段說(shuō)明和字段值,但是主鍵不能為空

4、事務(wù)的提交:如果一個(gè)事件是由多個(gè)動(dòng)作組成,只要有一個(gè)動(dòng)作沒有執(zhí)行成功則自動(dòng)將數(shù)據(jù)回滾到原始狀態(tài),此們技術(shù)稱之為事務(wù)

? 保證數(shù)據(jù)的安全和完整

? 事物的提交:

? 使用第三放插件的提交按鈕

? 使用commit語(yǔ)句

5、增加刪除修改的數(shù)據(jù)sql語(yǔ)句執(zhí)行完畢后,不會(huì)立馬進(jìn)入數(shù)據(jù)的寫入

需要手動(dòng)的對(duì)數(shù)據(jù)進(jìn)行提交,如果數(shù)據(jù)有問題還可以回滾

select * from dept for update

在北京新建了一個(gè)名為L(zhǎng)OL學(xué)院的新部門,請(qǐng)插入

insert into dept(deptno,dname,loc)values('50','lol學(xué)院','北京');

主鍵是唯一不可以重復(fù)的

insert into dept(deptno,dname,loc)values('50','教學(xué)部','北京');

如果插入的數(shù)據(jù)是全字段數(shù)據(jù),字段可以省略不寫。部分字段,必須加上字段說(shuō)明和字段值,但是主鍵不能為空

insert into dept values('80','教學(xué)部','北京');

insert into dept values('90','教學(xué)部','北京');

insert into dept values(100','教學(xué)部','北京');

insert into dept values('110','教學(xué)部','北京');

2、創(chuàng)建數(shù)據(jù)的備份

(1)create table 表名 as 查詢語(yǔ)句,創(chuàng)建的是和查詢結(jié)果一樣的表,查詢結(jié)果是什么就會(huì)備份一個(gè)相同的表

(2)insert into 表名 查詢語(yǔ)句,注意:查詢出來(lái)的結(jié)果在結(jié)構(gòu)上必須和插入數(shù)據(jù)的表相同,字段個(gè)數(shù)必須相同

(3)注意:備份表只有字段和數(shù)據(jù)相同,并不會(huì)備份約束。

1、備份完整的數(shù)據(jù)和表

select * from dept;

create table tdept as select * from dept;--備份dept表和數(shù)據(jù),只能備份數(shù)據(jù)和字段

select * from tdept

2、備份完整表

create table tdept1 as select * from dept where 1>2--備份表,不備份數(shù)據(jù)。

3、備份部分?jǐn)?shù)據(jù)和表.create table 表名 as 查詢語(yǔ)句,創(chuàng)建的是和查詢結(jié)果一樣的表,查詢結(jié)果是什么就會(huì)備份一個(gè)相同的表

create table tdept2 as select dname,loc from dept

select *from tdept2

4、給備份表添加數(shù)據(jù) insert into 表名 查詢語(yǔ)句,注意:查詢出來(lái)的結(jié)果在結(jié)構(gòu)上必須和插入數(shù)據(jù)的表相同,字段個(gè)數(shù)必須相同

select? * from tdept1

insert into tdept1 select dname,loc from dept where deptno>40

select *from tdept2

insert into tdept2 select dname,loc from dept where deptno>40

第五節(jié)課:數(shù)據(jù)的更新和刪除:

更新數(shù)據(jù):update

語(yǔ)法結(jié)構(gòu):update 表名 set 字段名1='字段值1',字段名2='字段值2',...where 條件

將部門70的名字改為教學(xué)部2

update dept set dname='教學(xué)部2' where deptno='70'

update dept set dname='教學(xué)部2',loc='上海' where deptno='70'

select * from dept

刪除數(shù)據(jù):delete

1)刪除語(yǔ)句:delete 表名 where 條件

--刪除部門標(biāo)號(hào)為70的數(shù)據(jù)

delete dept where deptno='70'---刪除指定數(shù)據(jù)

select * from dept

delete tdept---清空表數(shù)據(jù)

truncate table tdept---清空表數(shù)據(jù)建議使用truncate關(guān)鍵字,但是此關(guān)鍵字不能回滾數(shù)據(jù)

===================================================================================================================================

第一節(jié)課:sql92學(xué)習(xí)

-查詢員工姓名,工作,薪資,部門名稱

sql的聯(lián)合查詢(多表查詢)

--1、sql92標(biāo)準(zhǔn)

----笛卡爾積:一件事情的完成需要很多步驟,而不同的步驟有很多種方式,完成這件事情的所有方式稱之為笛卡爾積

select * from emp--14

select * from dept--7

select * from emp,dept order by ename--7*14=98

等值鏈接,鏈接條件。等值鏈接的時(shí)候字段的名字可以不相同,但是字段的值要相同。

--查詢員工姓名,工作,薪資,部門名稱

select * from emp,dept where emp.deptno=dept.deptno--使用等值鏈接進(jìn)行結(jié)果篩選

select ename,job,sal,dname from emp,dept where emp.deptno=dept.deptno;--使用等值鏈接查詢指定字段數(shù)據(jù)

select ename,job,sal,emp.deptno,dname from emp,dept where emp.deptno=dept.deptno;--多表查詢的時(shí)候,查看相同字段的值,必須聲明所在表

select emp.ename,emp.job,emp.sal,emp.deptno,dept.dname from emp,dept where emp.deptno=dept.deptno;--在查詢指定字段值的時(shí)候,加上表名提高查詢效率

select e.ename,e.job,e.sal,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno;--多表查詢中使用別名進(jìn)行表的區(qū)分

select e.ename,e.job,e.sal,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno order by deptno;--還可以使用order by 排序

非等值鏈接

---查詢員工姓名,工作,工資,工資等級(jí)

select * from salgrade

select * from emp,salgrade order by ename

select * from emp,salgrade where sal>losal and sal<hisal

select e.ename,e.job,e.sal,s.grade from emp e,salgrade s where e.sal>s.losal and e.sal<s.hisal;

自連接:使用頻率不是很高,因?yàn)樽赃B接的條件要求不同信息共存在一張里,其實(shí)就兩張相同的表的等值鏈接。

--查詢員工姓名,工作,薪資,及上級(jí)領(lǐng)導(dǎo)姓名

select * from emp for update

create table temp as select * from emp

select * from emp e,temp t where e.mgr=t.empno

外連接

select * from emp,dept

--查詢員工姓名,工作,薪資以及部門名稱并且查詢沒有部門的員工信息

select * from emp e,dept d where e.deptno=d.deptno(+)

--查詢員工姓名,工作,薪資以及部門名稱并且查詢沒有員工的部門信息

select * from emp e,dept d where e.deptno(+)=d.deptno

第二節(jié)課:sql99

sql99學(xué)習(xí):

交叉鏈接(沒啥用)

select * from emp cross join dept

自然鏈接:natural join

(1)、 自然鏈接會(huì)自動(dòng)使用多表中所有相同字段(不但值相同,名字也要相同)進(jìn)行篩選

前提:多表一定要有同名同值的字段

注意:自然連接會(huì)自動(dòng)使用所有的相同字段進(jìn)行結(jié)果篩選

(2)、使用using關(guān)鍵字可以指定字段進(jìn)行鏈接查詢,但是必須是同名字段? inner join

(3)、使用on關(guān)鍵字可以直接在其后書寫鏈接條件,沒有限制? ? ? ? ? ? inner join

查詢員工及員工所在的部門信息

select * from dept natural join emp

select * from dept d,emp e where d.deptno=e.deptno

問題1:假如在鏈接查詢的時(shí)候不想使用所有相同字段進(jìn)行篩選怎么辦?

----使用using關(guān)鍵字

---查詢員工及員工所在的部門信息

select * from dept? join emp using(deptno)

問題2:假如在鏈接查詢中沒有同名字段,但是有同值字段怎么篩選?

? 使用on關(guān)鍵字

查詢員工及員工所在的部門信息

select * from dept inner join emp on dept.deptno=emp.deptno

查詢部門及部門的城市信息

select * from dept inner join city on dept.loc=city.cid

外鏈接

? (1)、左外鏈接 left outer join

(2)、右外鏈接? right outer join

(3)、全外鏈接? full outer join?

左外鏈接

查詢沒有部門的員工信息及員工和部門信息

select * from emp e,dept d where d.deptno(+)=e.deptno--sql92

select *? ? ---sql99

from emp e

left join dept

using(deptno)

--右外鏈接

select * from emp e,dept d where d.deptno=e.deptno(+)--sql92

select *? ---sql99

from emp e

right outer join dept d

on e.deptno=d.deptno

--全外鏈接

select *

from emp e

full outer join dept d

on e.deptno=d.deptno

第三節(jié)課:自連接及三表聯(lián)合查詢

查詢員工姓名,職位,薪資,上級(jí)領(lǐng)導(dǎo)姓名

? select e.ename,e.job,e.sal,m.ename from emp e,emp m where e.mgr=m.empno--sql92

sql99實(shí)現(xiàn):

select e.ename,e.job,e.sal,m.ename

from emp e

inner join emp m

on e.mgr=m.empno

-----------------------------------------------------------------------

sql99三表聯(lián)合查詢

查詢員工姓名,工作,薪資,部門名稱,城市名稱

sql92實(shí)現(xiàn):

select e.ename,e.job,e.sal,d.dname,c.cname

from emp e,dept d,city c

where e.deptno=d.deptno and d.loc=c.cid

sql99實(shí)現(xiàn):

select e.ename,e.job,e.sal,d.dname,c.cname

from emp e

inner join dept d

on e.deptno=d.deptno

inner join city c

on d.loc=c.cid

查詢工資高于2000的員工姓名,工作,薪資,部門名稱,城市名稱

sql92實(shí)現(xiàn):

select e.ename,e.job,e.sal,d.dname,c.cname

from emp e,dept d,city c

where e.deptno=d.deptno and d.loc=c.cid and sal>2000

sql99實(shí)現(xiàn):

select e.ename,e.job,e.sal,d.dname,c.cname --查詢內(nèi)容

from emp e? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? --查詢表

inner join dept d? ? ? ? ? ? ? ? ? ? ? ? ? --鏈接表

on e.deptno=d.deptno? ? ? ? ? ? ? ? ? ? ? --鏈接條件

inner join city c? ? ? ? ? ? ? ? ? ? ? ? ? --鏈接表

on d.loc=c.cid? ? ? ? ? ? ? ? ? ? ? ? ? ? --鏈接條件

where e.sal>2000? ? ? ? ? ? ? ? ? ? ? ? ? --篩選條件

第四節(jié)課:?jiǎn)涡凶硬樵?/p>

子查詢(單行子查詢、多行子查詢)

單行子查詢

select 內(nèi)容 from 表名 where 子查詢語(yǔ)句

1、什么時(shí)候使用單行子查詢?

--在不能直接獲取有效信息的時(shí)候,考慮使用子查詢

2、單行子查詢的注意事項(xiàng)?

---子查詢的結(jié)果必須只有一個(gè)值

---可以直接使用算術(shù)鏈接符

---子查詢出現(xiàn)在where中,一般出現(xiàn)在條件語(yǔ)句的右邊

問題:查詢所有比雇員“CLARK”工資高的員工信息

解讀1:查詢雇員CLARK的工資

select sal from emp where ename='CLARK'

解讀2:查詢工資大于2450的員工信息

select * from emp where sal>'2450'

使用子查詢

select * from emp where sal>(select sal from emp where ename='CLARK')

select * from emp where (select sal from emp where ename='CLARK')<sal

查詢工資高于平均工資的員工的名字和工資

select * from emp where sal>(select avg(sal) from emp) order by sal

查詢和soctt屬于同一部門且工資比他低的員工資料

select * from emp where deptno=(select deptno from emp where ename='SCOTT') and sal<(select sal from emp where ename='SCOTT')

查詢工資最高的員工資料

select * from emp where sal=(select max(sal) from emp)

查詢職務(wù)和scott相同,雇傭時(shí)間早的員工信息

select * from emp where job=(select job from emp where ename='SCOTT') and hiredate<(select hiredate from emp where ename='SCOTT')

查詢工資比scott高或者雇傭時(shí)間早的員工編號(hào)和名字

select empno,ename from emp where sal>(select sal from emp where ename='SCOTT') or hiredate<(select hiredate from emp where ename='SCOTT')

查詢工資高于任意一個(gè)CLERK的所有員工信息

select * from emp where sal>(select min(sal) from emp where job='CLERK') and job<>'CLERK'

----------------------------------------------------------------------------------------------

第五節(jié)課: 多行子查詢

多行子查詢學(xué)習(xí):其實(shí)就使用使用關(guān)鍵字:any all in

注意:子查詢返回多個(gè)值建議使用多行子查詢,返回的單個(gè)值使用單行子查詢

select * from emp where sal> any (select sal from emp where job='CLERK') and job<>'CLERK'

查詢工資高于所有SALESMAN的員工信息

select * from emp where sal>(select max(sal) from emp where job='SALESMAN')--單行子查詢的寫法

select * from emp where sal>all (select sal from emp where job='SALESMAN';

查詢部門20中同部門10的雇員工作一樣的雇員信息

select * from emp where deptno='20' and (job='MANAGER' or job='PRESIDENT' or job='CLERK')

select * from emp where deptno='20' and job in ('MANAGER', 'PRESIDENT' , 'CLERK')

select * from emp where deptno='20' and job in (select job from emp where deptno='10')

select * from emp where deptno='20' and job= any (select job from emp where deptno='10')

select job from emp where deptno='10'

第六節(jié)課:用戶創(chuàng)建

創(chuàng)建用戶

當(dāng)前用戶介紹:權(quán)限級(jí)別的分配

-------system:系統(tǒng)賬戶

-------sys:超級(jí)管理員

-------scott:普通用戶

1、 創(chuàng)建自定義用戶: create user 用戶名 identified by 密碼;

普通用戶不具備創(chuàng)建用戶的權(quán)限,需要使用system賬戶進(jìn)行創(chuàng)建

直接創(chuàng)建好的用戶不能登錄,還需要使用system進(jìn)行權(quán)限的分配(角色:一系列權(quán)限的集合)

create user zyp identified by 123456;

給創(chuàng)建的用戶賦予角色。? ? grant 角色名,角色名...to 用戶名;

grant connect to zyp;--賦予鏈接庫(kù)權(quán)限

grant resource to zyp;--賦予操作資源權(quán)限

grant dba to zyp;--賦予DBA角色

grant dba to zyp

刪除用戶權(quán)限

revoke dba from zyp

revoke connect from zyp

2、 刪除用戶

drop user zyp

========================================================================================================================================

第五天:

第一節(jié)課:創(chuàng)建表和字段講解

--創(chuàng)建表學(xué)習(xí)

1、 創(chuàng)建表的基本語(yǔ)句:create table 表名(字段名 類型,字段名 類型,字段名 類型....)

創(chuàng)建學(xué)生表:學(xué)號(hào),姓名,性別,年齡,qq號(hào),郵箱。

create table student(

? snum number(10),---指定學(xué)號(hào)的數(shù)字長(zhǎng)度為10

? sname varchar2(100),---指定姓名的存儲(chǔ)上限為100個(gè)字節(jié)

? sex char(4),

? age number,

? qq number,

? sal number(6,2),--指定工資的整數(shù)位為6和小數(shù)位為2

? mail varchar2(50)

)

2、字段類型

number類型:數(shù)字類型,用來(lái)聲明存儲(chǔ)數(shù)字的字段。 number(指定數(shù)字的整數(shù)長(zhǎng)度,指定數(shù)字的小數(shù)位長(zhǎng)度)

varchar2類型:字符類型,用來(lái)聲明存儲(chǔ)字符的字段。會(huì)根據(jù)存儲(chǔ)的數(shù)據(jù)自動(dòng)增長(zhǎng)大小,varchar2(存儲(chǔ)上限)

char類型:字符類型,用來(lái)聲明存儲(chǔ)字符的字符。會(huì)開辟指定大小的內(nèi)存來(lái)存儲(chǔ)數(shù)據(jù)。

區(qū)別1:char的存儲(chǔ)效率高于varchar2

區(qū)別2:char是開辟指定大小的內(nèi)存空間,varchar2是根據(jù)數(shù)據(jù)大小來(lái)開辟空間的大小

date類型:存儲(chǔ)日期類型

第二節(jié)課:修改表學(xué)習(xí):

修改表

在命令窗口查看表結(jié)構(gòu) desc 表名

添加新字段? alter table 表名 add 字段名 類型

alter table student add phone number(11)

修改字段類型? ? alter table 表名 modify 字段名 類型

alter table? student modify sname varchar2(200)

刪除字段? ? alter table 表名 drop column 字段名

alter table student drop column phone

修改表名 rename 表名 to 新的表名

rename student to stu

rename stu to student

刪除表? drop table 表名

drop table student

第三節(jié)課:約束學(xué)習(xí)(根據(jù)情況切分成兩節(jié)課)

select * from student for update

添加數(shù)據(jù)

問題1:學(xué)號(hào)重復(fù)了,數(shù)據(jù)還可以插入成功

insert into student(snum,sna,sex,age,qq,sal,mail)values('123','張三','男',18,12345678,88.88,'12345678@qq.com')

insert into student values(123,'李四','男',18,7890122,88.99,'7890122@qq.com')

問題2:姓名可以為空。

insert into student values(456,'柳巖','女',18,666999,99.66,'666999@qq.com')

insert into student (snum,sex,age,qq,sal,mail) values(789,'女',18,888444,99.66,'888444@qq.com')

問題3:性別不但可以為空,還可以為其他不知道的字符

insert into student (snum,sna,age,qq,sal,mail) values(108,'景甜',18,000999,99.66,'000999@qq.com')

insert into student (snum,sna,sex,age,qq,sal,mail) values(102,'景甜','a',18,000999,99.66,'000999@qq.com')

insert into student (snum,sna,sex,age,qq,sal,mail) values(102,'景甜','女',18,000999,99.66,'000999@qq.com')

問題4:年齡可以超過(guò)200

insert into student (snum,sna,sex,age,qq,sal,mail) values(103,'唐嫣','女',23,78900,99.66,'78900@qq.com')

insert into student (snum,sna,sex,age,qq,sal,mail) values(103,'唐嫣','女',230,78900,99.66,'78900@qq.com')

問題5:qq號(hào)一致

insert into student (snum,sna,sex,age,qq,sal,mail) values(104,'關(guān)曉彤','女',19,111000,99.66,'111000@qq.com')

insert into student (snum,sna,sex,age,qq,sal,mail) values(105,'袁華','男',22,111000,99.66,'111000@qq.com')

--------------------------------------------------------------------------------------------------------

約束學(xué)習(xí):

create table student(

? snum number(10),--primary key,--使用主鍵約束

? sna varchar2(100), --not null,

? sex char(4), --default '男' check(sex='男' or sex='女') not null,

? age number check(age>0 and age<120),

? qq number, --unique,

? sal number(6,2),

? mail varchar2(50)

-- constraints pk_student_snum primary key(snum)

? -- constraints ck_student_sna check(sna is not null)

-- constraints ck_student_sex? check(sex ='男' or sex='女')

--constraints uk_student_qq unique(qq)

)

alter table student add constraints pk_student_snum primary key(snum);--在創(chuàng)建表后添加主鍵

alter table student drop constraints pk_student_snum--刪除主鍵

alter table student modify sna varchar2(100) not null;--添加非空約束

alter table student modify sna varchar2(100) null;--修改字段為空

alter table student add constraints ck_student_sex check(sex='男' or sex='女')--添加檢查約束

alter table student drop constraints ck_student_sex --刪除檢查約束

alter table student add constraints uk_student_qq unique(qq)

alter? table student drop constraints uk_student_qq

drop table student

問題1:學(xué)號(hào)重復(fù)了,數(shù)據(jù)還可以插入成功

使用主鍵約束:學(xué)號(hào)是唯一標(biāo)識(shí)一條數(shù)據(jù)的,所以必須唯一且不能為空

? ---(1)、在確定為主鍵的字段后添加 primary key關(guān)鍵字

? ---(2)、在創(chuàng)建表的后面使用:constraints pk_表名_字段名 primary key(字段名)

? --(3)、在創(chuàng)建表后使用 alter table 表名 add constraints pk_表名_字段名 primary key(字段名);

? --刪除主鍵:alter table 表名 drop constraints pk_表名_字段名

問題2:姓名可以為空。

使用非空約束

? ---(1)、創(chuàng)建表的時(shí)候在字段后面添加not null

? ---(2)、在創(chuàng)建表字段后使用 constraints ck_表名_字段名 check(字段名 is not null)? 了解

? --(3)、在創(chuàng)建表后使用alter table 表名 modify 字段名 類型 not null;

? ---(4)、修改字段可以存儲(chǔ)空值:alter table 表名 modify 字段名 類型 null;

問題3:性別不但可以為空,還可以為其他不知道的字符

使用檢查約束

? ---(1)、創(chuàng)建表的時(shí)候在字段后使用 default 值 check(條件),

? ---------但是會(huì)允許空值的出現(xiàn),并且默認(rèn)值只有在字段不聲明的情況下生效

? ---(2)、在創(chuàng)建表所有字段后使用:constraints ck_表名_字段名? check(條件)

? ---(3)、在創(chuàng)建表后使用:alter table 表名 add constraints ck_表名_字段名 check(條件)

問題4:年齡可以超過(guò)200

--使用檢查約束條件

問題5:qq號(hào)一致

使用唯一約束

--(1)、在字段后直接使用unique關(guān)鍵字

--(2)、在所有字段后使用:constraints uk_表名_字段名 unique(字段名)

--(3)、 alter table 表名 add constraints uk_表名_字段名 unique(字段名)

--刪除唯一約束:alter table 表名 drop constraints uk_表名_字段名

第四節(jié)課:外鍵約束學(xué)習(xí):

創(chuàng)建學(xué)生表

create table stu(

? snum number(10) primary key,

? sname varchar2(100) not null,

? sex? char(4) check(sex='男' or sex='女'),

? cinfo number(10)-- references clazz(cid)--外鍵

? -- constraints fk_stu_cinfo foreign key(cinfo) references clazz(cid)

)

alter table stu add constraints fk_stu_cinfo foreign key(cinfo) references clazz(cid) on delete cascade--添加外鍵

alter table stu add constraints fk_stu_cinfo foreign key(cinfo) references clazz(cid) on delete set null--添加外鍵

alter table stu drop constraints fk_stu_cinfo --刪除外鍵

drop table stu

創(chuàng)建班級(jí)表

create table clazz(

? cid number(10) primary key,

? cname varchar2(100) not null,

? cdesc varchar2(500)

)

創(chuàng)建班級(jí)表數(shù)據(jù)

insert into clazz values(1,'軟件01班','最厲害的班級(jí)');

insert into clazz values(2,'軟件02班','掛科最多的班級(jí)');

insert into clazz values(3,'508班','大牛和最帥的班級(jí)');

select * from clazz

delete from clazz where cid=1

創(chuàng)建學(xué)生數(shù)據(jù)

insert into stu values(1002,'張三','男',1);

insert into stu values(1003,'李四','女',2);

insert into stu values(1004,'王五','男',3);

insert into stu values(1005,'趙四','男',4);

select * from stu

問題1:在插入學(xué)生信息的時(shí)候,出現(xiàn)了不存在的班級(jí)信息?

? 使用外鍵約束

? --(1)、在字段后使用 references 參照表表名(參照字段)

? --(2)、在所有字段后使用 constraints fk_表名_字段名 foreign key(字段名) references 參照表名(參照字段名)

? --(3)、在創(chuàng)建表后使用alter table 表名 add constraints fk_表名_字段名 foreign key(字段名) references 參照表名(參照字段名)

? --刪除外鍵 alter table 表名 drop constraints fk_表名_字段名

select table_name from all_all_tables where owner='SCOTT'

問題2:在刪除父表數(shù)據(jù)的時(shí)候需要先刪除子表數(shù)據(jù)?

解決方式1:先解除主外鍵關(guān)聯(lián),然后刪除數(shù)據(jù),再然后添加主外鍵關(guān)聯(lián)

解決方式2:在創(chuàng)建外鍵的時(shí)候使用級(jí)聯(lián)操作。

? --在創(chuàng)建外鍵時(shí) on delete cascade

? --在創(chuàng)建外鍵時(shí) on delete set null

怎么選取外鍵?

一般將主表的主鍵作為子表的外鍵

外鍵的值能為 not null? 不建議在外鍵后使用非空約束

第五節(jié)課: 序列

--序列

select cid from clazz order by cid

insert into clazz values(4,'402班級(jí)','老師最污的班');

insert into clazz values(10,'402班級(jí)','老師最污的班');

--問題1:如果數(shù)據(jù)量比較大,插入新的數(shù)據(jù)的時(shí)候id怎么選取才能避免重復(fù)?

使用序列

1、創(chuàng)建序列 create sequence 序列名

特點(diǎn):默認(rèn)沒有初始值的,nextval的值默認(rèn)從1開始,默認(rèn)每次自增+1

特點(diǎn):可以使用 序列名.nextval坐為主鍵使用

create sequence cnum;

select cnum.currval from dual---獲取當(dāng)前序列值

select cnum.nextval from dual---序列自增后返回當(dāng)前值

truncate table clazz

select * from clazz

insert into clazz values(cnum.nextval,'402班級(jí)','老師最污的班');--將序列作為主鍵

問題2:如果當(dāng)前表中已有很多數(shù)據(jù),但是接下來(lái)需要使用序列進(jìn)行主鍵自增?

創(chuàng)建序列時(shí)指定序列初始值及步長(zhǎng)

create sequence cc

start with 90---設(shè)定序列初始值

increment by 5---設(shè)定步長(zhǎng)

insert into clazz values(cc.nextval,'402班級(jí)','老師最污的班');

select max(cid) from clazz

--

select cc.nextval from dual

select cc.currval from dual

select * from clazz

刪除序列

drop sequence cc

----------------完整的序列格式-----------------------------------

create sequence aa--序列名

start with 20? --設(shè)置序列初始值

increment by 10 --設(shè)置步長(zhǎng)

maxvalue 10000---設(shè)置最大值

cache? 10? -----設(shè)置緩存

第六節(jié)課:索引

索引學(xué)習(xí)

select * from emp order by sal

select * from emp where sal=3000

問題:當(dāng)表中數(shù)據(jù)量比較大的時(shí)候,使用條件查詢就會(huì)出現(xiàn)效率的問題

使用索引進(jìn)行解決

1、原理:類似于java中數(shù)組的折半查找,在數(shù)據(jù)庫(kù)中數(shù)據(jù)使用B樹形結(jié)構(gòu)進(jìn)行數(shù)據(jù)的存儲(chǔ),

? --這樣可以對(duì)外提供快速的數(shù)據(jù)查找方式。

2、創(chuàng)建索引 create index? 表名_index_字段名 on 表名(字段名)

特點(diǎn):顯示的創(chuàng)建,隱式的執(zhí)行.在數(shù)據(jù)中會(huì)給主鍵默認(rèn)創(chuàng)建索引。

create index emp_index_sal on emp(sal)--創(chuàng)建查詢索引

select * from emp where sal=3000

select * from emp order by sal

create index emp_index_sal_desc on emp(sal desc);--創(chuàng)建單排序索引

select * from emp order by sal,job

create index emp_index_sal_desc2 on emp(sal desc,job asc);--創(chuàng)建多條件排序索引

3、刪除索引 drop index 索引名

drop index emp_index_sal_desc

=====================================================================================================================================

文章首發(fā)公眾號(hào):java架構(gòu)師聯(lián)盟,歡迎關(guān)注,謝謝

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容