1. 程式人生 > >oracle-常用sql

oracle-常用sql

wishyouhappy

https://www.cnblogs.com/wishyouhappy/p/3700683.html#char


1)基本

2)數學函式

3)rownum

4)分頁

5)時間處理

6)字元函式

7)to_number

8)聚合函式

9)學生選課

10)圖書館借閱

 

基本

複製程式碼
--新建表:
create table table1( id varchar(300) primary key, name varchar(200) not null);

--插入資料   
insert into table1 (id,name) values ('aa','bb');
 
--更新資料   
update
table1 set id = 'bb' where id='cc'; --刪除資料 delete from table1 where id ='cc'; --刪除表 drop table table1; --修改表名: alter table table1 rename to table2; --表資料複製: insert into table1 (select * from table2); --複製表結構: create table table1 select * from table2 where 1>1; --複製表結構和資料: create
table table1 select * from table2; --複製指定欄位: create table table1 as select id, name from table2 where 1>1; --條件查詢: select id,name (case gender when 0 then '' when 1 then ‘女’ end ) gender from table1
複製程式碼

 

數學函式

複製程式碼
--絕對值:abs()
   select abs(-2) value from dual;          --(2)

--取整函式(大):ceil()
   select ceil(-2.001) value from dual;       --(-2)

--取整函式(小):floor()
   select floor(-2.001) value from dual;       --(-3)

--取整函式(擷取):trunc()
   select trunc(-2.001) value from dual;       -- (-2)

--四捨五入:round()
   select round(1.234564,4) value from dual;       --(1.2346)

--取平方:Power(m,n)
   select power(4,2) value from dual;       --(16)

--取平方根:SQRT()
   select sqrt(16) value from dual;       --(4)

--取隨機數:dbms_random(minvalue,maxvalue)
   select dbms_random.value() from dual;  (預設是0到1之間)
 select dbms_random.value(2,4) value from dual;  (2-4之間隨機數)

--取符號:Sign()
  select sign(-3) value from dual; --(-1)
  select sign(3) value from dual; --(1)


--取集合的最大值:greatest(value)
   select greatest(-1,3,5,7,9) value from dual;       --(9)

--取集合的最小值:least(value)
   select least(-1,3,5,7,9) value from dual;       --(-1)

--處理Null值:nvl(空值,代替值)
   select  nvl(null,10) value from dual;       --(10)
  
   select nvl(score,10) score from student;
複製程式碼

 

 

 rownum相關

複製程式碼
--rownum小於某個數時可以直接作為查詢條件(注意oracle不支援select top)
select * from student where rownum <3;

--查詢rownum大於某個數值,需要使用子查詢,並且rownum需要有別名
select * from(select rownum rn ,id,name from student) where rn>2;
select * from (select rownum rn, student.* from student) where rn >3;

--區間查詢
select * from (select rownum rn, student.* from student) where rn >3 and rn<6;

--排序+前n條
select * from (select rownum rn, t.* from ( select d.* from DJDRUVER d order  by drivernumber)t )p where p.rn<10;

--排序+區間查詢1
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;

--排序+區間查詢2
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;--效率遠高於方式一
複製程式碼

 

 分頁查詢

(假設每頁顯示10條)

不包含排序:

複製程式碼
--效率低

select * from (select rownum rn, d.* from DJDRIVER d  )p where p.rn<=20 and p.rn>=10;
select * from (select rownum rn, d.* from DJDRIVER d )p where p.rn between 10 and 20; --效率高 select * from (select rownum rn, d.* from DJDRIVER d where rownum<=20 )p where p.rn>=10;
複製程式碼

包含排序:

複製程式碼
--排序+區間查詢1(效率低)
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;
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; --排序+區間查詢2(效率高)
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基本使用

複製程式碼
--日期
--年 yyyy yyy yy year
--月 month mm mon month
--日+星期  dd ddd(一年中第幾天) dy day 
--小時  hh hh24 
--分 mi
--秒 ss
複製程式碼

eg1:

複製程式碼
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')currenttime, 
       to_char(sysdate,'yyyy') year,
       to_char(sysdate,'mm') month,
       to_char(sysdate,'dd') day,
       to_char(sysdate,'day') week,
       to_char(sysdate,'hh24')hour,
       to_char(sysdate,'mi') minute,
       to_char(sysdate,'ss') second
from dual;
複製程式碼

eg2:

複製程式碼
select to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss')currenttime,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'yyyy')year,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mm')month,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'dd') day,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day') week,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day','NLS_DATE_LANGUAGE=American') week, --設定語言
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'hh24')hour,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mi') minute,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'ss') second
from dual;
複製程式碼

 

2)months_between

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

 

3)next_day

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

 

4)時間區間

eg:

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

 

5)interval

複製程式碼
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') currenttime,
       to_char(sysdate - interval '7' year,'yyyy-mm-dd hh24:mi:ss') intervalyear,   
       to_char(sysdate - interval '7' month,'yyyy-mm-dd hh24:mi:ss') intervalMonth,   
       to_char(sysdate - interval '7' day,'yyyy-mm-dd hh24:mi:ss') intervalday,   
       to_char(sysdate - interval '7' hour,'yyyy-mm-dd hh24:mi:ss') intervalHour,   
       to_char(sysdate - interval '7' minute,'yyyy-mm-dd hh24:mi:ss') intervalMinute,   
       to_char(sysdate - interval '7' second,'yyyy-mm-dd hh24:mi:ss') intervalSecond  
  from dual; 
複製程式碼

 

 

6)add_months

select add_months(sysdate,12) newtime from dual;

 

7)extract

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

 

 

字元函式

複製程式碼
--字元函式
select substr('abcdefg',1,5)substr,                     --字串擷取
       instr('abcdefg','bc') instr,                     --查詢子串
       
       'Hello'||'World' concat,                         --連線
       
       trim('  wish  ') trim,                           --去前後空格
       rtrim('wish  ') rtrim,                           --去後面空格
       ltrim('  wish') ltrim,                           --去前面空格
       
       trim(leading 'w' from 'wish') deleteprefix,      --去字首
       trim(trailing 'h' from 'wish') deletetrailing,   --去後綴
       trim('w' from 'wish') trim1,
       
       ascii('A') A1, 
       ascii('a') A2,                                   --ascii(轉換為對應的十進位制數)
       chr(65) C1, 
       chr(97) C2,                                      --chr(十進位制轉對應字元)
       
       length('abcdefg') len,                           --length 
       
       lower('WISH')lower, 
       upper('wish')upper, 
       initcap('wish')initcap,                            --大小寫變換
       
       replace('wish1','1','youhappy') replace,           --替換
       
       translate('wish1','1','y')translate,               --轉換,對應一位(前面的位數大於等於後面的位數)
       translate('wish1','sh1','hy')translate1,
       
       concat('11','22') concat                     --連線

from dual;
複製程式碼

 

 

 

to_number

複製程式碼
--to_number(expr)
--to_number(expr,format)
--to_number(expr,format,'nls-param')

select to_number('0123')number1,            --converts a string to number
       trunc(to_number('0123.123'),2) number2,
       to_number('120.11','999.99') number3,
     to_number('0a','xx') number4,        --converts a hex number to decimal
       to_number(100000,'xxxxxx') number5
  
from dual;
複製程式碼

 

 

聚合函式

student表如下:

count:

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

語句1結果:11

 

avg

--avg (distinct|all)
select avg(score) score from student;
select avg(distinct score) from student;
select classno,avg(score) score from student group by classno;

 

語句3輸出結果:

 

max

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

 

min

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

 

stddev(standard deviation)標準差

--stddev
select stddev(score) from student;
select classno, stddev(score) score from student group by classno;

 

sum

--sum
select sum(score) from student;
select classno, sum(score) score from student group by classno;

 

median--中位數

--median
select median(score) from student;
select classno, median(score) score from student group by classno;

 

 案例1--學生選課

1. 建立表 stu(學生表),course(課程表),選課表(s_c)

複製程式碼
--建立表

create table STU  
(  
  id   NUMBER not null,  
  name VARCHAR2(255)  
) ;
   
create table COURSE  
(  
  id  NUMBER not null,  
  coursename VARCHAR2(255)  
) ; 
   
create table S_C  
(  
  sid    NUMBER,  
  cid    NUMBER,  
  score NUMBER  
);
複製程式碼

 

2.插入資料

相關推薦

oracle 常用sql語句

onu format del 滿足 blank ica end var har 目錄 1)基本 2)數學函數 3)rownum 4)分頁 5)時間處理 6)字符函數 7)to_number 8)聚合函數 9)學生選課 10)圖書館借閱 基本 --新建表: cre

ORACLE 常用SQL(自用、用到更新)

true 執行 存儲 gin session pad any mov sched /**********************************************************************************************

oracle常用SQL命令

兩張 alt server 更新數據 gn3 屬性 rac happy fray 將A表中的字段a1的值全部賦給B表中的字段B1,根據兩張表的ID關聯。 方式1: UPDATE HBSZ071001.tac_bank aSET a.bankno= (SELE

ORACLE 常用sql用法和及註意事項

com 從右到左 指定 turn 但是 數據 lba etime its 1.exits和in用法 1)說明:   1. exists對外表做循環,每次循環對內表查詢;in將內表和外表做hash連接   2. 使用exists oracle會先檢查主查詢; 使用in,首先執

oracle-常用sql

wishyouhappy https://www.cnblogs.com/wishyouhappy/p/3700683.html#char

Oracle常用sql語句

整理一些個人常用到的sql語句: 1.更新一條記錄 update 表名稱 set 列名稱1 = null,列名稱2=to_date(‘15:27:10’,’hh24:mi:ss’) where 列名稱3 = ‘123’ 2.刪除一條記錄 detel

Oracle 常用SQL總結之一

1、distinct :去重複 ,可以跟上一列或多列來排除行重複  2、<> :不等於 --->select * from emp where 

oracle 常用sql命令

進行資料庫備份遷移之前,如果是oracle11g,一定要做匯出空表設定(ArcSDE10.1版本中的SDE庫比較奇怪,導不出所有的空表,必須先進行設定)。下列cmd命令或sql執行語句,請實施人員一定要看懂,如有必要,請進行相應的替換。 一、資料庫備份步驟: 0、匯出sde

ORACLE常用SQL語句大全

一、基礎 1、說明:建立資料庫 CREATE DATABASE database-name 2、說明:刪除資料庫 drop database dbname 3、說明:備份sql server — 建立 備份資料的 device USE master

Oracle常用sql語句(三)之子查詢

子查詢 子查詢要解決的問題,不能一步求解 分為: - 單行子查詢 - 多行子查詢 語法: SELECT select_list FROM table WHERE expr operator (SELECT select_l

ORACLE常用SQL

insert acl column spa sql 常用sql oracle word bsp -- 添加“email”列到表格的末尾列。 ALTER TABLE TABLE_NAME ADD email VARCHAR(60); --

常用sql 分頁語句(Oracle)

part strong spa 數據 rac syntax tween 另類 排序 常用的Oracle查詢語句 1.無ORDER BY排序的寫法。(效率最高) 經過測試,此方法成本最低,只嵌套一層,速度最快!即使查詢的數據量再大,也幾乎不受影響,速度依然! sql語句如下:

sql優化(oracle)- 第二部分 常用sql用法和註意事項

個數 its 用法 記錄 減少 合並 .com 語句 一個 第二部分 常用sql用法和註意事項 1. exists 和 in 2. union 和 union all

Oracle數據庫常用SQL

ssd lec isl ams gtk kkk kavm zhang constrain Oracle數據庫創建實例的過程類似於Sql server創建數據庫,Oracle一個實例可以對應多個表空間,一個表空間對應一個用戶,根據不同的用戶名、密碼登錄不同的表空間。 因此,創

Oracle數據庫,常用SQL語句匯總

sql oracl -s 使用率 style pac alter _id 查詢 --查看表空間名稱、數據文件的路徑、大小、及使用率select b.tablespace_name "表空間名稱", b.file_name "數據文件路徑",

oracle dataguard常用sql語句

dataguardselect log_mode,open_mode ,database_role from v$database; --查看dataguard主備庫狀態 select status from v$instance; --查看數據庫狀態 alter database recover manag

常用SQL語句(oracle)(1)

查詢表結構 - select COLUMN_NAME,DATA_TYPE,DATA_LENGTH from user_tab_cols where table_name='TEST'; - desc 表名; 新增列 ALTER TABLE tab

Sql Server和Oracle常用SQL指令碼

插入欄位 //新增欄位並附預設值 alter table GSPAuditEvent add ISPRECUT char(1) NOT NULL DEFAULT ('1') alter table GSPAuditEvent add(ISPRECUT char(1) DEFAUL

OracleSQL Plus 設定與常用命令

顯示設定   -- 設定每行顯示的最長字元數 set linesize 120   -- 設定一頁顯示的行數 set pagesize 20   -- 設定是否顯示一頁的記錄數 set feedback o

Oracle系統表整理+常用SQL語句收集(轉載)

原文:https://www.cnblogs.com/jiangxinnju/p/5840420.html-- DBA/ALL/USER/V_$/GV_$/SESSION/INDEX開頭的絕大部分都是檢視 -- DBA_TABLES意為DBA擁有的或可以訪問的所有的關係表。 -- ALL_TABLES意