1、SQL有哪些主要特點,SQL的用途有哪些? 特點:簡單易懂,風(fēng)格統(tǒng)一。 用途:數(shù)據(jù)查詢語言DQL:查詢數(shù)據(jù)。 數(shù)據(jù)定義語言DDL:建立、刪除和修改數(shù)據(jù)對象。 數(shù)據(jù)操縱語言DML:完成數(shù)據(jù)操作的命令,包括查詢。 數(shù)據(jù)控制語言DCL:控制對數(shù)據(jù)庫的訪問,服務(wù)器的關(guān)閉、啟動等。 2、在Oracle9i中,SQL如何訪問數(shù)據(jù)表? 訪問數(shù)據(jù)表是通過“用戶名.數(shù)據(jù)表”的形式來進(jìn)行的。例:select * from scott.emp。 3、SQL查詢語句 select * from scott.emp,其中*表示數(shù)據(jù)表中所有字段。 select empno,ename,job from scott.emp select distinct job from scott.emp,distinct表示去除相同記錄,與之對應(yīng)是all,默認(rèn)為all。 select empno,ename,job from scott.emp where job=’MANAGER’,條件查詢。 select empno,ename,sal from scott.emp where sal<=2500 symbols:=, != or ^= or <>, <, >, <=, >=, in(), not in(), between…and, not between…and, like’…’, not like’…’, is null, is not null。(%表示任意長度的字符串,_表示一個任意的字符) selectempno,ename,job,salfromscott.empwherejob>=’CLERK’ and sal<=2000,組合條件查詢。(and, or, not) select empno,ename,job from scott.emp where job<=’CLERK’order byjobasc,saldesc,排序查詢。order by放在where后面,asc表示上升,desc表示下降。 select empno,ename,job,sal from scott.emp group by job,empno,ename,sal having sal<=2000 select empno,ename,job,sal from scott.emp where sal<=2000 group by job,empno,ename,sal (where檢查每條記錄是否符合條件,having是檢查分組后的各組是否滿足條件,having只能配合group by使用) select empno,ename,sal,mgr,sal+mgr from scott.emp,字段運算查詢,+, -, *, /。 select empno編號,ename姓名,job工作,sal工資from scott.emp,變換查詢顯示。 SQL多表查詢: select emp.empno,emp.ename,emp.deptno,dept.dname,dept.loc from scott.dept,scott.emp; select emp.empno,emp.ename,emp.deptno,dept.dname,dept.loc from scott.dept,scott.emp where scott.emp.deptno=scott.dept.deptno;等值多表查詢。 SQL嵌套查詢: select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>=(select sal from scott.emp where ename=’WARD’); select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where salin(select sal from scott.emp where ename=’WARD’); //在其中 select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>any(select sal from scott.emp where ename=’WARD’); //大于其中一個 select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal=some(select sal from scott.emp where ename=’WARD’); //等于其中一個 select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>all(select sal from scott.emp where ename=’WARD’); //等于任何一個 select emp.empno,emp.ename,emp.job,emp.sal from scott.emp,scott.dept whereexits(select * from scott.emp where scott.emp.deptno=scott.dept.deptno); (select deptno from scott.emp) union (select deptno from scott.dept); //集合A與集合B的并集。 (select deptno from scott.emp) intersect (select deptno from scott.dept); //集合A與集合B的交集。 (select deptno from scott.emp) minus (select deptno from scott.dept); //差集,屬于集合A,但不屬于集合B。 SQL函數(shù)查詢: select mgr,mgr/100,ceil(mgr/100) from scott.emp; //ceil(n)取大于等于n的最小整數(shù) select mgr,mgr/100,floor(mgr/100) from scott.emp; //floor(n)取小于等于n的最大整數(shù) select mgr,mod(mgr,1000),mod(mgr,100) from scott.emp; //mod(m,n)取m整除n后的余數(shù) select mgr,power(mgr,2),power(mgr,3) from scott.emp; //power(m,n)取m的n次方 select mgr,round(mgr/1000,2) from scott.emp; //round(m,n)取m四舍五入,保留n位 select mgr,sign(mgr-7800) from scott.emp; //sign(m),m>0取1,=0取0,<0取-1 select avg(mgr)平均薪水from scott.emp; //avg(m)取字段平均值,要求字段為數(shù)值型 select count(distinct job)工作類別總數(shù)from scott.emp; //count()統(tǒng)計數(shù)值型字段的總數(shù) select min(sal) from scott.emp; //min()取數(shù)值型字段中最小值 select max(sal) from scott.emp; //max()取數(shù)值型字段中最大值 select sum(sal) from scott.emp; //sum()取數(shù)值型字段中總數(shù) 4、SQL錄入語句 數(shù)值型字段:可以直接寫值; 字符型字段:其值要加上單引號; 日期型字段:其值要加上單引號,注意年、月、日的排列次序。 insert into scott.emp(empno,ename,hiredate) values(7999,’JONE’,’25-11月-2002); //單行錄入 insert into scott.emp(empno,ename,hiredate) (select empno+100,ename,hiredate from scott.emp where empno>=6999); //多行錄入。Insert與select的表可以不一樣,但對應(yīng)字段的屬性必須一致。 create table scott.test as (select distinct empno,ename,hiredate from scott.emp where empno>=7000); //表間數(shù)據(jù)復(fù)制 5、SQL刪除語句 delect刪除數(shù)據(jù),并將數(shù)據(jù)保存在系統(tǒng)回滾段中,需要的時候數(shù)據(jù)可以回滾恢復(fù)。 truncate刪除整表數(shù)據(jù)但保留結(jié)構(gòu),但數(shù)據(jù)是不可恢復(fù)的。 delect from scott.test where empno>=7500 and empno<=8000; truncate table scott.test; 6、SQL更新語句 update scott.emp set empno=7888,ename=’TOM’,hiredate=’03-9月-2002’ where empno=7655; update scott.emp set sal=(select sal+300 from scott.emp where empno=7897) where empno=7897; |
|