<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
        主站蜘蛛池模板: 亚洲欧洲日本在线| 日本一区二区三区日本免费| 国产自偷亚洲精品页65页| 看Aⅴ免费毛片手机播放| 午夜一区二区免费视频| 亚洲熟妇成人精品一区| 日韩免费视频播放| 精品视频免费在线| 亚洲AⅤ永久无码精品AA| 永久免费观看黄网站| 亚洲人成伊人成综合网久久久| 成人精品视频99在线观看免费| 国产亚洲精品拍拍拍拍拍| 丝袜足液精子免费视频| 久久精品亚洲综合| 国产成人精品免费视频网页大全| 亚洲福利秒拍一区二区| 天天拍拍天天爽免费视频 | 亚洲无砖砖区免费| 国内精品乱码卡1卡2卡3免费| 久久乐国产综合亚洲精品| 国产国产人免费视频成69大陆 | 亚洲另类激情综合偷自拍图| 99精品一区二区免费视频| 亚洲国产成人无码av在线播放| 国产精品成人免费一区二区 | 免费99精品国产自在现线| 亚洲精品无码av中文字幕| 亚洲av日韩片在线观看| 免费网站看av片| 77777亚洲午夜久久多喷| 亚洲国产香蕉人人爽成AV片久久| 在线观看片免费人成视频无码| 亚洲无人区视频大全| 亚洲国产精品自产在线播放 | 免费一级毛片在线播放视频| 亚洲一区二区三区在线| 亚洲日韩涩涩成人午夜私人影院| 日本免费一区二区久久人人澡| 中文字幕乱码亚洲无线三区 | 黄色免费在线网站|