oracle資料庫常用語句整理

2021-10-12 01:43:40 字數 2355 閱讀 1876

1. nvl(表示式1,表示式2)

是乙個空值轉換函式

如果表示式1為空值,nvl返回值為表示式2的值,否則返回表示式1的值。 該函式的目的是把乙個空值(null)轉換成乙個實際的值。其表示式的值可以是數字型、字元型和日期型。但是表示式1和表示式2的資料型別必須為同乙個型別。

對數字型: nvl( comm,0);

對字元型 nvl( to_char(comm), 'no commission')

對日期型 nvl(hiredate,' 31-dec-99')

2.欄位新增:

alter table 表名 add(字段 varchar2(52));

注釋:comment on column t_qc_inspection_rule.complete_machine_check is '整機檢驗';

3.新增資料庫

create table 表名 (

id varchar2(40 byte) not null ,

plan_number number(10),

)tablespace mes_data;

alter table 表名 add constraint pk_pp_designnum_id primary key (id) using index tablespace mes_index;

comment on table 表名 is '明細表';

comment on column 表名.id is '主鍵';

comment on column 表名.plan_number is '計畫數量';

commit;

4. 建立檢視

create or replace view 檢視名 as

select * from 表名

5. 模糊查詢

字段 like '%' ||  # || '%'
6. 字元型別按一定格式轉化為日期型別

to_date(字串字段,』『yyyy-mm-dd』』) #
7. 將日期轉按一定格式換成字元型別

select to_char(sysdate,』『yyyy-mm-dd hh24:mi:ss』』) time from dual;
8. trunc函式的用法

用法有兩種:trunc(number)表示截斷數字,trunc(date)表示截斷日期。

(1)截斷數字:

格式:trunc(n1,n2),n1表示被截斷的數字,n2表示要截斷到那一位。n2可以是負數,表示截斷小數點前。注意,trunc截斷不是四捨五入。

sql> select trunc(15.79) from dual;-------15

sql> select trunc(15.79,1) from dual;------- 15.7

sql> select trunc(15.79,-1) from dual; -------10

(2)截斷日期:

select trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'yyyy') from dual ;--返回當年第一天

select trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'mm') from dual ; --返回當月第一天

select trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'dd') from dual ;--返回當前年月

select trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'d') from dual ; --返回當前星期的第一天(星期日)

select trunc(to_date('2018-02-01 1:12:12','yyyy-mm-dd hh:mi:ss'),'hh') from dual ;--返回當前日期擷取到小時,分秒補0

select trunc(to_date('2018-02-01 1:12:12','yyyy-mm-dd hh:mi:ss'),'mi') from dual ;--返回當前日期擷取到分,秒補0

oracle 資料庫常用語句

1 oracle分頁 1 select from select a.rownum rn from select from tabel order by xx a where rownum 5 where rn 2 注 在oracle中用rownum來記錄資料的順序,這個語句的意思是,按某個字段排序並...

Oracle資料庫常用語句

1 建立表空間 注釋 creat tablespace 表名 datafile 虛擬機器所在目錄 字尾一定是.bdf size 100m 初始大小100兆 autoextend on 自動擴充套件大小 next 10 每次擴充套件10m 2 刪除表空間 drop tablespace 表名 3 建立...

Oracle資料庫常用語句

建表語句 create table test1 id varchar2 20 not null primary key 增加乙個表字段 alter table test1 add name varchar2 20 default null not null 增加多個表字段 alter table t...