oracle 常用語句彙總

Franson發表於2016-07-15
一.複製表資料至另一張表
直接建相同的表並將資料複製過去:
create table students_backup as select * from students;
只建相同的表結構:
create table students_backup as select * from students where 1=2;
表已建好,直接複製資料:
insert into students_backup select * from students
二.兩表(多表)關聯update

  1.僅在where子句中的連線


SQL 程式碼
--這次提取的資料都是VIP,且包括新增的,所以順便更新客戶類別
update customers a -- 使用別名
set customer_type='01' --01 為vip,00為普通
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)

 

  2. 被修改值由另一個表運算而來


SQL 程式碼
update customers a -- 使用別名
set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
-- update 超過2個值
update customers a -- 使用別名
set (city_name,customer_type)=(select b.city_name,b.customer_type
from tmp_cust_city b
where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)

 

 

相關文章