1、SQL有哪些主要特点,SQL的用途有哪些? 特点:简单易懂,风格统一。 用途:数据查询语言DQL:查询数据。 数据定义语言DDL:建立、删除和修改数据对象。 数据操纵语言DML:完成数据操作的命令,包括查询。 数据控制语言DCL:控制对数据库的访问,服务器的关闭、启动等。 2、在Oracle9i中,SQL如何访问数据表? 访问数据表是通过“用户名.数据表”的形式来进行的。例:select * from scott.emp。 3、SQL查询语句 select * from scott.emp,其中*表示数据表中所有字段。 select empno,ename,job from scott.emp select distinct job from scott.emp,distinct表示去除相同记录,与之对应是all,默认为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函数查询: select mgr,mgr/100,ceil(mgr/100) from scott.emp; //ceil(n)取大于等于n的最小整数 select mgr,mgr/100,floor(mgr/100) from scott.emp; //floor(n)取小于等于n的最大整数 select mgr,mod(mgr,1000),mod(mgr,100) from scott.emp; //mod(m,n)取m整除n后的余数 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)取字段平均值,要求字段为数值型 select count(distinct job)工作类别总数from scott.emp; //count()统计数值型字段的总数 select min(sal) from scott.emp; //min()取数值型字段中最小值 select max(sal) from scott.emp; //max()取数值型字段中最大值 select sum(sal) from scott.emp; //sum()取数值型字段中总数 4、SQL录入语句 数值型字段:可以直接写值; 字符型字段:其值要加上单引号; 日期型字段:其值要加上单引号,注意年、月、日的排列次序。 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的表可以不一样,但对应字段的属性必须一致。 create table scott.test as (select distinct empno,ename,hiredate from scott.emp where empno>=7000); //表间数据复制 5、SQL删除语句 delect删除数据,并将数据保存在系统回滚段中,需要的时候数据可以回滚恢复。 truncate删除整表数据但保留结构,但数据是不可恢复的。 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; |
|