SQL 更新實(shí)例

插入

  • 插入默認(rèn)值
-- 數(shù)據(jù)準(zhǔn)備
create table table4(
  id int4,
  name text default 'no one'
);
-- 使用 default
insert into table4 values (1001, default);
insert into table4(id) values (1002);
  • 多表插入
-- 數(shù)據(jù)準(zhǔn)備
create table raw_data(
  id int,
  name varchar2(32),
  dept varchar2(32),
  salary double precision
);
insert into raw_data values (1001, 'aladdin', 'bigdata', 13000);
insert into raw_data values (1002, 'bilib', 'java', 10000);
-- 創(chuàng)建相似表結(jié)構(gòu),無法使用 like
create table med_emp_info as select * from raw_data where 1 = 0;
create table hig_emp_info as select * from raw_data where 1 = 0;

-- Oracle 支持,MySQL 和 Postgres 不支持
-- 多表插入
insert all
  when
    salary <= 10000
  then
    into med_emp_info(id, name, dept, salary)
    values (id, name, dept, salary)
  when
    salary > 10000 and salary <= 30000
  then
    into hig_emp_info(id, name, dept, salary)
    values (id, name, dept, salary)
select id, name, dept, salary
from raw_data;

復(fù)制數(shù)據(jù)

  • 復(fù)制數(shù)據(jù)到另一個表
insert into table6 select * from table5;
  • 復(fù)制表結(jié)構(gòu)
-- MySQL/Postgres/Oracle
create table table6 as select * from table5 where 1 = 0;
-- DB2
create table table6 like table5;

更新

  • 更新記錄
update table5 set name = 'chrome' where id = 1002;
update table5 set name = 'ccc' where emp in (select emp from emp_bonus);
  • 使用另一表的數(shù)據(jù)更新
-- Oracle
update
  table1 t1
set 
    (id, salary) = (select id, salary from table2 t2 where t1.id = t2.id)
where exists (select null from table2 t2 where t1.id = t2.id);

-- Postgres
update
  table1 t1
set
    salary = t2.salary
from
     table2 t2
where
      t1.id = t2.id;

刪除

  • 刪除記錄
-- 刪除全部數(shù)據(jù)
delete from table2;
-- 刪除指定數(shù)據(jù)
delete from table1 where id = 1001;
  • 刪除違反參照物的數(shù)據(jù)
-- 使用 exists
delete from
            table1 t1
where not exists(select * from table2 t2 where t1.id = t2.id);
-- 使用 not in
delete from table1 where id not in (select id from table2);
  • 刪除重復(fù)數(shù)據(jù)
-- 數(shù)據(jù)準(zhǔn)備
create table table3(
  id int,
  name varchar2(32)
);
insert into table3 (id, name) values (1001, 'a');
insert into table3 (id, name) values (1002, 'b');
insert into table3 (id, name) values (1003, 'c');
insert into table3 (id, name) values (1004, 'c');
insert into table3 (id, name) values (1005, 'c');
-- 刪除數(shù)據(jù)
delete from table3 where id not in (select min(id) from table3 group by name);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ORA-00001: 違反唯一約束條件 (.) 錯誤說明:當(dāng)在唯一索引所對應(yīng)的列上鍵入重復(fù)值時,會觸發(fā)此異常。 O...
    我想起個好名字閱讀 5,945評論 0 9
  • MYSQL 基礎(chǔ)知識 1 MySQL數(shù)據(jù)庫概要 2 簡單MySQL環(huán)境 3 數(shù)據(jù)的存儲和獲取 4 MySQL基本操...
    Kingtester閱讀 8,057評論 5 115
  • 爆竹聲中一歲除,春風(fēng)送暖入屠蘇。千門萬戶曈曈日,總把新桃換舊符。”這是宋朝大詩人王安石描寫的春節(jié)景象,和諧美好,現(xiàn)...
    梁一丹閱讀 196評論 0 0
  • 四升級換代閱讀 105評論 1 0
  • 2018年5月21日 星期一 多云 521,我愛你,兒子,媽媽真的很愛你,但這種愛卻只能放在心底。不想...
    棟鈺媽閱讀 44評論 0 0

友情鏈接更多精彩內(nèi)容