Oracle update 多字段更新


一次性update多個字段

以student表為例:

-- 創建學生表
create table student
(
  id number,
  name varchar2(40),
  age number,
  birthday date,
  address varchar2(200)
)

-- 插入數據
insert into student(id, name, age, birthday, address)
values(1, '王小波', 50, to_date('19700520','yyyymmdd'), '廣州市天河區')

-- 查詢
select * from student
-- 1	王小波	50	1970/5/20	廣州市天河區

如果需要修改數據,一般語法:

update student
set name = '王大錘',
    age = 49,
    birthday = to_date('19710206','yyyymmdd'),
    address = '廣州市越秀區'
where id = 1;

如果字段非常多,這樣寫就稍微麻煩點,因為待修改字段和待修改的數據沒有分離。

還有另外一種寫法(字段多的時候寫的時候方便,書寫效率高些;字段少的時候感覺不出來):

update student
set (name, age, birthday, address) = 
(select '王大錘', 49, to_date('19710206','yyyymmdd'), '廣州市越秀區' from dual)
where id = 1

update 多表級聯更新

多表級聯更新語法為:

update tableA a set a.col1 = (select b.col1 from tableB b where b.col2 = a.col2),
                    a.col3 = (select c.col1 from tableC c where c.col2 = a.col2)
where condition

該語句作用就是當tableA中col2列的值等於tableB中col2列的值時,那么將tableA所在行的col1的值設置為tableB相應行的col1的值,當tableA中col2列的值等於tableC中col2列的值時,那么將tableA所在行的col1的值設置為tableC相應行的col1的值,換句話說就是將tablA中所有滿足a.col2=b.col2的行的col1列的值設置為b對應的行的col1列的值,將tableA中所有滿足a.col3=c.col1的行的col3列的值設置為c對應的行的col1列的值。

多字段update

Oracle 多字段 update語法為:

update table_a
set (a,b,c) = 
(select a,b,c from table_b)

參考鏈接1:oracle中update語句修改多個字段

參考鏈接2:oracle update 多表級聯更新


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM