總結下Oracle中的Insert用法

2021-06-27 06:10:19 字數 2468 閱讀 1550

總結下oracle中的insert用法

1.標準insert --單錶單行插入

語法:insert into table [(column1,column2,...)] value (value1,value2,...)

例子:insert into dep (dep_id,dep_name) values (1,'技術部');

備註:使用標準語法只能插入一條資料,且只能在一張表中插入資料

2, 無條件 insert all --多表多行插入

語法:insert [all] [condition_insert_clause]

[insert_into_clause values_clause] (subquery)

示例:insert all

into sal_history(emp_id,hire_date,salary) values (empid,hiredate,sal)

into mgr_history(emp_id,manager_id,salary) values (empid,mgr,sal)

select employee_id empid,hire_date hiredate,salary sal,manager_id mgr

from employees

where employee_id>200;

3,有條件的insert

語法:insert [all | first]

when condition then insert_into_clause values_clause

[when condition then] [insert_into_clause values_clause]

......

[else] [insert_into_clause values_clause]

subquery;

示例:insert all

when id>5 then into z_test1(id, name) values(id,name)

when id<>2 then into z_test2(id) values(id)

else into z_test3 values(name)

select id,name from z_test;

當使用all關鍵字時,oracle會從上至下判斷每乙個條件,當條件滿足時就執行後面的into語句

在上面的例子中,如果id=6 那麼將會在z_test1中插入一條記錄,同時也在z_test2中插入一條記錄

備註:當使用first關鍵字時,oracle會從上至下判斷每乙個條件,當遇到第乙個滿足時就執行後面的into語句,

同時中斷判斷的條件判斷,在上面的例子中,如果id=6,僅僅會在z_test1中插入一條資料

4, 旋轉insert (pivoting insert)

create table sales_source_data (

employee_id number(6),

week_id number(2),

sales_mon number(8,2),

sales_tue number(8,2),

sales_wed number(8,2),

sales_thur number(8,2),

sales_fri number(8,2) );

insert into sales_source_data values (176,6,2000,3000,4000,5000,6000);

create table sales_info (

employee_id number(6),

week number(2),

sales number(8,2) );

看上面的表結構,現在將要sales_source_data表中的資料轉換到sales_info表中,這種情況就需要使用旋轉insert

示例如下:

insert all

into sales_info values(employee_id,week_id,sales_mon)

into sales_info values(employee_id,week_id,sales_tue)

into sales_info values(employee_id,week_id,sales_wed)

into sales_info values(employee_id,week_id,sales_thur)

into sales_info values(employee_id,week_id,sales_fri)

select employee_id,week_id,sales_mon,sales_tue,sales_wed,sales_thur,sales_fri

from sales_source_data;

從該例子可以看出,所謂旋轉insert是無條件 insert all 的一種特殊應用,但這種應用被oracle官方,賦予了乙個pivoting insert的名稱,即旋轉insert

小小的總結下

tablename代表資料庫中的表 fieldname代表需要查詢的資料庫表中的列名 cond代表查詢的條件表示式,cond.getcondition 得到具體的表示式 查詢函式 condition類,構造條件表示式 expression類代表任意的乙個表示式 public class condit...

總結下cocopods的安裝

一 cocoapods是什麼?cocoapods應該是ios最常用最有名的類庫管理工具了,上述兩個煩人的問題,通過cocoapods,只需要一行命令就可以完全解決,當然前提是你必須正確設定它。重要的是,絕大部分有名的開源類庫,都支援cocoapods。所以,作為ios程式設計師的我們,掌握cocoa...

自己試著總結下C語言的指標 持續總結中

一.指標變數 1.什麼是位址 2.左值 什麼是左值 c語言中很多內容不是左值,看書的我目前只知兩點 左值的幾個原則 3.關於左值的第四個原則 首先,上圖,圖下進行說明,對後面指標的概念或許有點幫助 定義乙個 int a,眾所周知a佔四個位元組,所以就如圖 a 一樣,分別占用了2000 2001 20...