当前位置:Gxlcms > 数据库问题 > oracle 常用sql语句

oracle 常用sql语句

时间:2021-07-01 10:21:17 帮助过:13人阅读

  1. --新建表:
  2. create table table1( id varchar(300) primary key, name varchar(200) not null);
  3. --插入数据
  4. insert into table1 (id,name) values (‘aa‘,‘bb‘);
  5. --更新数据
  6. update table1 set id = ‘bb‘ where id=‘cc‘;
  7. --删除数据
  8. delete from table1 where id =‘cc‘;
  9. --删除表
  10. drop table table1;
  11. --修改表名:
  12. alter table table1 rename to table2;
  13. --表数据复制:
  14. insert into table1 (select * from table2);
  15. --复制表结构:
  16. create table table1 select * from table2 where 1>1;
  17. --复制表结构和数据:
  18. create table table1 select * from table2;
  19. --复制指定字段:
  20. create table table1 as select id, name from table2 where 1>1;
  21. --条件查询:
  22. select id,name (case gender when 0 then ‘男‘ when 1 then ‘女’ end ) gender from table1
技术分享

 

数学函数

技术分享
  1. --绝对值:abs()
  2. select abs(-2) value from dual; --(2)
  3. --取整函数(大):ceil()
  4. select ceil(-2.001) value from dual; --(-2)
  5. --取整函数(小):floor()
  6. select floor(-2.001) value from dual; --(-3)
  7. --取整函数(截取):trunc()
  8. select trunc(-2.001) value from dual; -- (-2)
  9. --四舍五入:round()
  10. select round(1.234564,4) value from dual; --(1.2346)
  11. --取平方:Power(m,n)
  12. select power(4,2) value from dual; --(16)
  13. --取平方根:SQRT()
  14. select sqrt(16) value from dual; --(4)
  15. --取随机数:dbms_random(minvalue,maxvalue)
  16. select dbms_random.value() from dual; (默认是0到1之间)
  17.  select dbms_random.value(2,4) value from dual; (2-4之间随机数)
  18. --取符号:Sign()
  19.   select sign(-3) value from dual; --(-1)
  20.   select sign(3) value from dual; --(1)
  21. --取集合的最大值:greatest(value)
  22. select greatest(-1,3,5,7,9) value from dual; --(9)
  23. --取集合的最小值:least(value)
  24. select least(-1,3,5,7,9) value from dual; --(-1)
  25. --处理Null值:nvl(空值,代替值)
  26. select nvl(null,10) value from dual; --(10)
  27.   
  28. select nvl(score,10) score from student;
技术分享

 

 

 rownum相关

技术分享
  1. --rownum小于某个数时可以直接作为查询条件(注意oracle不支持select top)
  2. select * from student where rownum <3;
  3. --查询rownum大于某个数值,需要使用子查询,并且rownum需要有别名
  4. select * from(select rownum rn ,id,name from student) where rn>2;
  5. select * from (select rownum rn, student.* from student) where rn >3;
  6. --区间查询
  7. select * from (select rownum rn, student.* from student) where rn >3 and rn<6;
  8. --排序+前n条
  9. select * from (select rownum rn, t.* from ( select d.* from DJDRUVER d order by drivernumber)t )p where p.rn<10;
  10. --排序+区间查询1
  11. select * from (select rownum rn, t.* from ( select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES)t )p where p.rn<9 and p.rn>6;
  12. --排序+区间查询2
  13. select * from (select rownum rn, t.* from ( select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES)t where rownum<9 )p where p.rn>6;<em>--效率远高于方式一</em>
技术分享

 

 分页查询

(假设每页显示10条)

不包含排序:

技术分享
  1. --效率低
  2. select * from (select rownum rn, d.* from DJDRIVER d )p where p.rn<=20 and p.rn>=10;<br>
  3. select * from (select rownum rn, d.* from DJDRIVER d )p where p.rn between 10 and 20;
  4. --效率高
  5. select * from (select rownum rn, d.* from DJDRIVER d where rownum<=20 )p where p.rn>=10;
技术分享

包含排序:

技术分享
  1. --排序+区间查询1(效率低)<br>
  2. select * from (select rownum rn, t.* from ( select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES)t )p where p.rn<=20 and p.rn>=10;<br>
  3. select * from (select rownum rn, t.* from ( select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES)t )p where p.rn between 10 and 20;
  4. --排序+区间查询2(效率高) <br>
  5. select * from (select rownum rn, t.* from ( select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES)t where rownum<=20 )p where p.rn>=10;
技术分享

 

 时间处理

1. to_char和to_date基本使用

技术分享
  1. --日期
  2. --年 yyyy yyy yy year
  3. --月 month mm mon month
  4. --日+星期 dd ddd(一年中第几天) dy day
  5. --小时 hh hh24
  6. --分 mi
  7. --秒 ss
技术分享

eg1:

技术分享
  1. select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘)currenttime,
  2. to_char(sysdate,‘yyyy‘) year,
  3. to_char(sysdate,‘mm‘) month,
  4. to_char(sysdate,‘dd‘) day,
  5. to_char(sysdate,‘day‘) week,
  6. to_char(sysdate,‘hh24‘)hour,
  7. to_char(sysdate,‘mi‘) minute,
  8. to_char(sysdate,‘ss‘) second
  9. from dual;
技术分享

技术分享

eg2:

技术分享
  1. select to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘)currenttime,
  2. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘yyyy‘)year,
  3. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘mm‘)month,
  4. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘dd‘) day,
  5. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘day‘) week,
  6. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘day‘,<strong><em>‘NLS_DATE_LANGUAGE=American‘</em></strong>) week, <em>--设置语言</em>
  7. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘hh24‘)hour,
  8. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘mi‘) minute,
  9. to_char(to_date(‘2009-07-04 05:02:01‘,‘yyyy-mm-dd hh24:mi:ss‘),‘ss‘) second
  10. from dual;
技术分享

技术分享

 

2)months_between

  1. select months_between(to_date(‘03-31-2014‘,‘MM-DD-YYYY‘),to_date(‘12-31-2013‘,‘MM-DD-YYYY‘)) "MONTHS"
  2. FROM DUAL;

技术分享

 

3)next_day

  1. select sysdate today, next_day(sysdate,6) nextweek from dual;

技术分享

 

4)时间区间

eg:

  1. select cardid, borrowdate from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘) between
  2. to_date(‘2014-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and
  3. to_date(‘2014-05-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘);

 

5)interval

技术分享
  1. select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) currenttime,
  2. to_char(sysdate - interval ‘7‘ year,‘yyyy-mm-dd hh24:mi:ss‘) intervalyear,
  3. to_char(sysdate - interval ‘7‘ month,‘yyyy-mm-dd hh24:mi:ss‘) intervalMonth,
  4. to_char(sysdate - interval ‘7‘ day,‘yyyy-mm-dd hh24:mi:ss‘) intervalday,
  5. to_char(sysdate - interval ‘7‘ hour,‘yyyy-mm-dd hh24:mi:ss‘) intervalHour,
  6. to_char(sysdate - interval ‘7‘ minute,‘yyyy-mm-dd hh24:mi:ss‘) intervalMinute,
  7. to_char(sysdate - interval ‘7‘ second,‘yyyy-mm-dd hh24:mi:ss‘) intervalSecond
  8. from dual;
技术分享

 

技术分享

 

6)add_months

  1. select add_months(sysdate,12) newtime from dual;

 

7)extract

  1. select extract(month from sysdate) "This Month",
  2. extract(year from add_months(sysdate,36)) " Years" from dual;

技术分享

 

 

字符函数

技术分享
  1. --字符函数
  2. select substr(‘abcdefg‘,1,5)substr, --字符串截取
  3. instr(‘abcdefg‘,‘bc‘) instr, --查找子串
  4. ‘Hello‘||‘World‘ concat, --连接
  5. trim(‘ wish ‘) trim, --去前后空格
  6. rtrim(‘wish ‘) rtrim, --去后面空格
  7. ltrim(‘ wish‘) ltrim, --去前面空格
  8. trim(leading ‘w‘ from ‘wish‘) deleteprefix, --去前缀
  9. trim(trailing ‘h‘ from ‘wish‘) deletetrailing, --去后缀
  10. trim(‘w‘ from ‘wish‘) trim1,
  11. ascii(‘A‘) A1,
  12. ascii(‘a‘) A2, --ascii(转换为对应的十进制数)
  13. chr(65) C1,
  14. chr(97) C2, --chr(十进制转对应字符)
  15. length(‘abcdefg‘) len, --length
  16. lower(‘WISH‘)lower,
  17. upper(‘wish‘)upper,
  18. initcap(‘wish‘)initcap, --大小写变换
  19. replace(‘wish1‘,‘1‘,‘youhappy‘) replace, --替换
  20. translate(‘wish1‘,‘1‘,‘y‘)translate, --转换,对应一位(前面的位数大于等于后面的位数)
  21. translate(‘wish1‘,‘sh1‘,‘hy‘)translate1,
  22. concat(‘11‘,‘22‘) concat          --连接<br><br>
  23. from dual;
技术分享

 

技术分享

 

 

to_number

技术分享
  1. --to_number(expr)
  2. --to_number(expr,format)
  3. --to_number(expr,format,‘nls-param‘)
  4. select to_number(‘0123‘)number1, --converts a string to number
  5. trunc(to_number(‘0123.123‘),2) number2,
  6. to_number(‘120.11‘,‘999.99‘) number3,
  1.     to_number(‘0a‘,‘xx‘) number4, --converts a hex number to decimal
  2. to_number(100000,‘xxxxxx‘) number5
  3. from dual;
技术分享

技术分享

 

 

聚合函数

student表如下:

技术分享

count:

  1. --count (distinct|all)
  2. select count(1) as count from student;--效率最高
  3. select count(*) as count from student;
  4. select count(distinct score) from student;

语句1结果:11

 

avg

  1. --avg (distinct|all)
  2. select avg(score) score from student;
  3. select avg(distinct score) from student;
  4. select classno,avg(score) score from student group by classno;

 

语句3输出结果:

技术分享

 

max

  1. --max (distinct|all)
  2. select max(score) from student;
  3. select classno, max(score) score from student group by classno;

 

min

  1. --min (distinct|all)
  2. select min(score) from student;
  3. select classno, min(score) score from student group by classno;

 

stddev(standard deviation)标准差

  1. --stddev
  2. select stddev(score) from student;
  3. select classno, stddev(score) score from student group by classno;

 

sum

  1. --sum
  2. select sum(score) from student;
  3. select classno, sum(score) score from student group by classno;

 

median--中位数

  1. --median
  2. select median(score) from student;
  3. select classno, median(score) score from student group by classno;

 

 案例1--学生选课

1. 创建表 stu(学生表),course(课程表),选课表(s_c)

技术分享
  1. --创建表
  2. create table STU
  3. (
  4. id NUMBER not null,
  5. name VARCHAR2(255)
  6. ) ;
  7. create table COURSE
  8. (
  9. id NUMBER not null,
  10. coursename VARCHAR2(255)
  11. ) ;
  12. create table S_C
  13. (
  14. sid NUMBER,
  15. cid NUMBER,
  16. score NUMBER
  17. );
技术分享

 

2.插入数据

技术分享
  1. --插入数据
  2. Insert into STU (ID,NAME) values (1,‘wish‘);
  3. Insert into STU (ID,NAME) values (2,‘rain‘);
  4. Insert into STU (ID,NAME) values (3,‘july‘);
  5. Insert into STU (ID,NAME) values (4,‘joey‘);
  6. Insert into COURSE (ID,COURSENAME) values (1,‘math‘);
  7. Insert into COURSE (ID,COURSENAME) values (2,‘english‘);
  8. Insert into COURSE (ID,COURSENAME) values (3,‘Japanese‘);
  9. Insert into COURSE (ID,COURSENAME) values (4,‘chinese‘);
  10. Insert into S_C (SID,CID,SCORE) values (1,1,80);
  11. Insert into S_C (SID,CID,SCORE) values (1,2,90);
  12. Insert into S_C (SID,CID,SCORE) values (2,4,100);
  13. Insert into S_C (SID,CID,SCORE) values (4,4,90);
  14. Insert into S_C (SID,CID,SCORE) values (4,1,100);
  15. Insert into S_C (SID,CID,SCORE) values (4,3,80);
  16. Insert into S_C (SID,CID,SCORE) values (4,2,80);
  17. Insert into S_C (SID,CID,SCORE) values (2,1,90);
  18. Insert into S_C (SID,CID,SCORE) values (2,4,100);
  19. Insert into S_C (SID,CID,SCORE) values (3,1,60);
技术分享

 

3.查询学生选课情况

  1. with vt as
  2. (select s.id,s.name,c.coursename,sc.score from stu s, course c, s_c sc where s.id=sc.sid and c.id=sc.cid)
  3. select * from vt order by id;

结果:

技术分享

 

 

案例2--图书馆借阅

1.创建表: 图书(book),读者(reader),借阅(borrow)

技术分享
  1. --创建表 book
  2. create table book(
  3. bookId varchar2(30), --图书总编号
  4. sortid varchar2(30), --分类号
  5. bookname varchar2(100), --书名
  6. author varchar2(30), --作者
  7. publisher varchar2(100),--出版单位
  8. price number(6,2) --价格
  9. );
  10. --创建表 reader
  11. create table reader (
  12. cardId varchar2(30), --借书证号
  13. org varchar2(100), --单位
  14. name varchar2(100), --姓名
  15. gender varchar2(2), --性别
  16. title varchar2(30), --职称
  17. address varchar2(100) --地址
  18. );
  19. --创建表 borrow
  20. create table borrow(
  21. cardId varchar2(30), --借书证号
  22. bookId varchar2(30), --图书总编号
  23. borrowDate varchar2(30) --借阅时间
  24. );
技术分享

 

2.插入数据

技术分享
  1. --插入数据-book
  2. insert into book (bookId,sortid,bookname,author,publisher,price)
  3. values (‘aaa‘,‘a1‘,‘gone with the wind‘,‘CA‘,‘renmin‘,‘103‘);
  4. insert into book (bookId,sortid,bookname,author,publisher,price)
  5. values (‘bbb‘,‘a2‘,‘the little prince‘,‘CB‘,‘jixie‘,‘30‘);
  6. insert into book (bookId,sortid,bookname,author,publisher,price)
  7. values (‘ccc‘,‘a3‘,‘the ordinary world‘,‘CC‘,‘renmin‘,‘130‘);
  8. insert into book (bookId,sortid,bookname,author,publisher,price)
  9. values (‘ddd‘,‘a4‘,‘the little women‘,‘CA‘,‘dianzi‘,‘110‘);
  10. --插入数据-reader
  11. insert into reader(cardid, org, name,gender, title, address)
  12. values (‘xxx‘,‘A‘,‘wish‘,‘1‘,‘student‘,‘bupt‘);
  13. insert into reader(cardid, org, name,gender, title, address)
  14. values (‘uuu‘,‘A‘,‘luna‘,‘1‘,‘student‘,‘bupt‘);
  15. insert into reader(cardid, org, name,gender, title, address)
  16. values (‘vvv‘,‘B‘,‘harry‘,‘1‘,‘student‘,‘bupt‘);
  17. insert into reader(cardid, org, name,gender, title, address)
  18. values (‘www‘,‘C‘,‘chander‘,‘2‘,‘professor‘,‘bupt‘);
  19. insert into reader(cardid, org, name,gender, title, address)
  20. values (‘yyy‘,‘A‘,‘joey‘,‘2‘,‘student‘,‘bupt‘);
  21. insert into reader(cardid, org, name,gender, title, address)
  22. values (‘zzz‘,‘B‘,‘richard‘,‘2‘,‘student‘,‘bupt‘);
  23. insert into reader(cardid, org, name,gender, title, address)
  24. values (‘OOO‘,‘A‘,‘micheal‘,‘2‘,‘student‘,‘bupt‘);
  25. insert into reader(cardid, org, name,gender, title, address)
  26. values (‘ppp‘,‘A‘,‘richal‘,‘2‘,‘student‘,‘bupt‘);
  27. insert into reader(cardid, org, name,gender, title, address)
  28. values (‘abp‘,‘A‘,‘michal‘,‘2‘,‘student‘,‘bupt‘);
  29. insert into reader(cardid, org, name,gender, title, address)
  30. values (‘ccp‘,‘A‘,‘mike‘,‘2‘,‘student‘,‘bupt‘);
  31. --插入数据-borrow
  32. insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘aaa‘,‘2014-4-29‘);
  33. insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘bbb‘,‘2014-4-29‘);
  34. insert into borrow(cardid,bookid,borrowdate) values(‘xxx‘,‘ccc‘,‘2014-4-28‘);
  35. insert into borrow(cardid,bookid,borrowdate) values(‘yyy‘,‘ccc‘,‘2014-4-28‘);
  36. insert into borrow(cardid,bookid,borrowdate) values(‘yyy‘,‘ddd‘,‘2014-4-27‘);
  37. insert into borrow(cardid,bookid,borrowdate) values(‘yyy‘,‘aaa‘,‘2014-4-27‘);
  38. insert into borrow(cardid,bookid,borrowdate) values(‘zzz‘,‘bbb‘,‘2014-4-28‘);
  39. insert into borrow(cardid,bookid,borrowdate) values(‘zzz‘,‘ddd‘,‘2014-4-27‘);
  40. insert into borrow(cardid,bookid,borrowdate) values(‘zzz‘,‘aaa‘,‘2014-4-27‘);
  41. insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘bbb‘,‘2014-4-28‘);
  42. insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘ddd‘,‘2014-4-27‘);
  43. insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘aaa‘,‘2014-4-27‘);
  44. insert into borrow(cardid,bookid,borrowdate) values(‘uuu‘,‘ccc‘,‘2014-4-26‘);
  45. insert into borrow(cardid,bookid,borrowdate) values(‘vvv‘,‘bbb‘,‘2014-4-28‘);
  46. insert into borrow(cardid,bookid,borrowdate) values(‘vvv‘,‘ddd‘,‘2014-4-27‘);
  47. insert into borrow(cardid,bookid,borrowdate) values(‘www‘,‘aaa‘,‘2014-4-27‘);
  48. insert into borrow(cardid,bookid,borrowdate) values(‘www‘,‘ccc‘,‘2014-4-26‘);
技术分享

表信息如下:

book------> reader-------> borrow

技术分享

技术分享

技术分享

 

 

3. 查询A单位借阅图书的读者人数和人员详细信息

人数:

  1. with vt1 as
  2. (select cardid from reader where reader.org=‘A‘)
  3. select count(1) from vt1 where exists (select cardid from borrow where borrow.cardid=vt1.cardid);

技术分享

详细信息:

  1. with vt1 as
  2. (select cardid,name,org from reader where reader.org=‘A‘)
  3. select cardid,name,org from vt1 where exists (select cardid from borrow where borrow.cardid=vt1.cardid);

技术分享

 

4.查询借书证号尾字符为‘p‘的读者

  1. select cardid, name, org from reader where cardid like ‘%p‘;

技术分享

 

5. 查询名字以m开头的女性读者,‘1’显示为女,‘2’显示为男

  1. select cardid, name, org,
  2. case when gender=‘1‘ then ‘女‘ when gender=‘2‘ then ‘男‘ else ‘其他‘ end gender
  3. from reader where name like ‘m%‘;

技术分享

 

6. 2014年2-4月借过书的读者

1)查询满足条件的读者(仅包含cardid)--未去重

  方式一:

  1. select cardid, borrowdate from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘
  2. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘mm‘)>=‘02‘
  3. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘mm‘)<=‘04‘;

  方式二:

  1. select cardid, borrowdate from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘ --查询
  2. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)>=‘2014-02‘
  3. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)<=‘2014-04‘;

  方式三:

  1. select cardid, borrowdate from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘) between
  2. to_date(‘2014-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and
  3. to_date(‘2014-05-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘);

 

技术分享

2) 查询+去重

  1. select distinct cardid from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘ --查询+去重
  2. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)>=‘2014-02‘
  3. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)<=‘2014-04‘;
  1. select distinct cardid from borrow where to_date(borrowdate,‘yyyy-mm-dd hh24:mi:ss‘) between
  2. to_date(‘2014-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and
  3. to_date(‘2014-05-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘);

技术分享

 

3)查询+去重+读者姓名等信息

技术分享
  1. with vt1 as
  2. (select distinct cardid from borrow where to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy‘)=‘2014‘
  3. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)>=‘2014-02‘
  4. and to_char(to_date(borrowdate,‘yyyy-mm-dd‘),‘yyyy-mm‘)<=‘2014-04‘)
  5. select cardid, name,org from reader where exists (select cardid from vt1 where vt1.cardid=reader.cardid);
技术分享

技术分享

转自:http://www.cnblogs.com/wishyouhappy/p/3700683.html

oracle 常用sql语句

标签:images   top   bsp   xxx   数据   bms   xxxx   1.2   sign   

人气教程排行