<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        PostgreSQL實現MySQL&amp;amp;quot;insertignore&amp;amp;quot;_MySQL

        來源:懂視網 責編:小采 時間:2020-11-09 20:12:18
        文檔

        PostgreSQL實現MySQL&amp;quot;insertignore&amp;quot;_MySQL

        PostgreSQL實現MySQL&quot;insertignore&quot;_MySQL:bitsCN.com 對MySQL熟悉的人可能都知道,MySQL 有一個“insert ignore 語法來忽略已經存在的記錄。 PostgreSQL暫時不提供這樣的語法,但是可以用其他方法來代替。t_girl=# /d insert_ignore Table ytt.insert_igno
        推薦度:
        導讀PostgreSQL實現MySQL&quot;insertignore&quot;_MySQL:bitsCN.com 對MySQL熟悉的人可能都知道,MySQL 有一個“insert ignore 語法來忽略已經存在的記錄。 PostgreSQL暫時不提供這樣的語法,但是可以用其他方法來代替。t_girl=# /d insert_ignore Table ytt.insert_igno

        bitsCN.com 對MySQL熟悉的人可能都知道,MySQL 有一個“insert ignore" 語法來忽略已經存在的記錄。 PostgreSQL暫時不提供這樣的語法,但是可以用其他方法來代替。
        t_girl=# /d insert_ignore Table "ytt.insert_ignore" Column | Type | Modifiers ----------+------------------------+----------- id | integer | not null log_time | time without time zone | Indexes: "insert_ignore_pkey" PRIMARY KEY, btree (id) t_girl=# select * from insert_ignore; id | log_time ----+---------------- 1 | 14:44:12.37185 (1 row)
        我來演示下幾種代替方法。第一種方法, 用自帶的規則(RULE)來實現。 

        t_girl=# create rule r_insert_ignore as on insert to insert_ignore where exists (select 1 from insert_ignore where id = new.id) do instead nothing;
        CREATE RULE

        這時,我們插入兩條記錄,其中一條的主鍵值已經存在,直接忽略掉。 實際插入的記錄數為1.
        t_girl=# insert into insert_ignore values(1,current_time),(2,current_time);
        INSERT 0 1
        t_girl=# select * from insert_ignore;
        id | log_time
        ----+-----------------
        1 | 14:44:12.37185
        2 | 14:48:22.222848
        (2 rows)


        第二種方法, 建立一個返回NULL的觸發器函數。 那么函數體如下:
        t_girl=# create or replace function sp_insert_ignore() returns trigger as $ytt$ begin perform 1 from insert_ignore where id = new.id; if found then return null; end if; return new;end;$ytt$ language 'plpgsql';CREATE FUNCTION對應的觸發器如下:t_girl=# create trigger tr_ib_insert_ignore before insert on insert_ignore for each row execute procedure sp_insert_ignore();CREATE TRIGGER繼續插入兩條記錄。t_girl=# insert into insert_ignore values (3,current_time),(2,current_time);INSERT 0 1t_girl=# select * from insert_ignore; id | log_time ----+----------------- 1 | 14:44:12.37185 2 | 14:48:22.222848 3 | 15:05:33.198847(3 rows)OK。目的達到了。 
        t_girl=# insert into insert_ignore 	with ytt_test(f1,f2) as (	values(6,current_time),(3,current_time)	) 	select a.* from ytt_test as a where a.f1 not in (select id from insert_ignore as b); INSERT 0 1查看記錄,插入了一條ID為6的記錄,忽略了ID為3的記錄。t_girl=# select * from insert_ignore; id | log_time ----+----------------- 1 | 14:44:12.37185 2 | 14:48:22.222848 3 | 15:05:33.198847 6 | 15:15:52.297802(4 rows)第四種,用存儲過程來代替INSERT處理。t_girl=# create or replace function sp_insert_ignore ( IN f_id int, IN f_log_time time without time zone ) returns void as $ytt$ begin insert into insert_ignore values (f_id,f_log_time); exception when unique_violation then raise notice 'Duplicated Key Error on ID:%',f_id; return; end; $ytt$ language plpgsql; 第一次調用,拋出了錯誤。 t_girl=# select sp_insert_ignore(1,'14:22:35'::time); NOTICE: Duplicated Key Error on ID:1 sp_insert_ignore ------------------ (1 row) 第二次正常插入。 t_girl=# select sp_insert_ignore(8,'14:22:35'::time); sp_insert_ignore ------------------ (1 row) t_girl=# select * from insert_ignore; id | log_time ----+----------------- 1 | 14:44:12.37185 2 | 14:48:22.222848 3 | 15:05:33.198847 6 | 15:15:52.297802 8 | 14:22:35 (5 rows) t_girl=# OK,目的也達到了。
        bitsCN.com

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        PostgreSQL實現MySQL&amp;quot;insertignore&amp;quot;_MySQL

        PostgreSQL實現MySQL&quot;insertignore&quot;_MySQL:bitsCN.com 對MySQL熟悉的人可能都知道,MySQL 有一個“insert ignore 語法來忽略已經存在的記錄。 PostgreSQL暫時不提供這樣的語法,但是可以用其他方法來代替。t_girl=# /d insert_ignore Table ytt.insert_igno
        推薦度:
        標簽: mysql insert postgreSql
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲高清中文字幕免费| 免费无码婬片aaa直播表情| 免费看搞黄视频网站| 国产AV无码专区亚洲AWWW| 中国性猛交xxxxx免费看| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲第一页在线观看| 四虎国产精品永久免费网址| 亚洲AV区无码字幕中文色| 99精品视频免费在线观看| 亚洲视屏在线观看| 免费看韩国黄a片在线观看| 亚洲精品一二三区| 日日夜夜精品免费视频| 七次郎成人免费线路视频| 国产亚洲AV手机在线观看| 免费观看一区二区三区| 91情国产l精品国产亚洲区 | 亚洲色偷精品一区二区三区 | 亚洲爽爽一区二区三区| 成人自慰女黄网站免费大全| 五月天网站亚洲小说| 成人无码区免费A片视频WWW| 国产成人综合亚洲一区| 国产亚洲精品无码拍拍拍色欲| 成人性生交大片免费看好| 亚洲狠狠ady亚洲精品大秀| 成人国产mv免费视频| 在线视频网址免费播放| 亚洲最大视频网站| 国产三级免费电影| 久久伊人免费视频| 伊人久久五月丁香综合中文亚洲| 免费欧洲毛片A级视频无风险| 麻豆精品不卡国产免费看| 久久久久久亚洲精品影院| 精品国产亚洲男女在线线电影 | 亚洲丰满熟女一区二区v| 亚洲VA综合VA国产产VA中| 免费无码中文字幕A级毛片| 亚洲AV无码一区二区大桥未久|