{"id":651,"date":"2019-03-06T23:12:43","date_gmt":"2019-03-06T15:12:43","guid":{"rendered":"http:\/\/suherman.asia\/w2\/?p=651"},"modified":"2019-03-06T23:12:43","modified_gmt":"2019-03-06T15:12:43","slug":"prevent-insert-using-trigger","status":"publish","type":"post","link":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html","title":{"rendered":"Prevent Insert Using Trigger"},"content":{"rendered":"<p>ada suatu kondisi dimana saya harus membuat sebuah tabel dalam database postgreSQL akan dikenakan proses insert yg banyak dari banyak mesin yang mengirimkan data yang kodisi nya bisa saja terjadi data yang sama, sedangkan di tabel target ada syarat dimana data tidak boleh redundan ketika di timpa banyak kondisi insert yang tidak terkontrol, lagi bagaiamana mensiasatinya ?<!--more--><\/p>\n<p>untuk mencapai goal tersebut, saya akan simulasikan langkah2 penyelesaiannya dengan cara berikut :<br \/>\nmisal table target itu adalah tabel yang saya beri nama\u00a0<strong>testtrigger<\/strong>.<br \/>\n<code>create table public.testtrigger(id serial, kode varchar(3), kode2 integer, nama varchar(100),<br \/>\nCONSTRAINT testtrigger_pkey PRIMARY KEY (id), CONSTRAINT nomords_pkeyunique UNIQUE (kode,kode2));<\/code><br \/>\njadi yang menjadi unique key nya adalah field <strong>kode<\/strong> dan field <strong>kode2<\/strong>.<\/p>\n<p>setelah itu saya buat function untuk kebutuhan trigger sebagai berikut :<\/p>\n<p><code>CREATE or replace FUNCTION check_if_data_exist()<br \/>\nRETURNS trigger AS<br \/>\n$func$<br \/>\nBEGIN<br \/>\nIF EXISTS (SELECT kode FROM testtrigger c WHERE c.kode = NEW.kode and c.kode2= NEW.kode2)<br \/>\nTHEN RETURN NULL;<br \/>\nEND IF;<br \/>\nRETURN NEW;<br \/>\nEND<br \/>\n$func$ LANGUAGE plpgsql;<\/code><br \/>\nlihat di function diatas, saya buatkan kondisi bhw jika data yg mau di insert sudah ada di table, maka yang terjadi adalah return NULL, dengan begitu tidak ada data yang terinsert.<\/p>\n<p>kemudian saya buat juga trigger nya sebagai berikut :<\/p>\n<p><code>CREATE TRIGGER tr_check_if_data_exist<br \/>\nBEFORE INSERT ON testtrigger<br \/>\nFOR EACH ROW EXECUTE PROCEDURE check_if_data_exist();<\/code><\/p>\n<p>kemudian saya coba jalankan query sebagai berikut :<\/p>\n<p><code>insert into testtrigger(kode,kode2, nama) values('001',3, 'tes 1');<\/code><\/p>\n<p>saat query ini dijalankan, data berhasil masuk 1 buah record, namun setelah saya eksekusi ulang perintah insert diatas, data tidak bertambah. berarti dalam hal ini teknik ini sudah berhasil.<\/p>\n<p>demikian semoga bermanfaat.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ada suatu kondisi dimana saya harus membuat sebuah tabel dalam database postgreSQL akan dikenakan proses insert yg banyak dari banyak mesin yang mengirimkan data yang kodisi nya bisa saja terjadi data yang sama, sedangkan di tabel target ada syarat dimana data tidak boleh redundan ketika di timpa banyak kondisi insert \u2026<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":652,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[92,113],"class_list":["post-651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-community","tag-postgresql","tag-trigger"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Prevent Insert Using Trigger - Suherman Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prevent Insert Using Trigger - Suherman Blog\" \/>\n<meta property=\"og:description\" content=\"ada suatu kondisi dimana saya harus membuat sebuah tabel dalam database postgreSQL akan dikenakan proses insert yg banyak dari banyak mesin yang mengirimkan data yang kodisi nya bisa saja terjadi data yang sama, sedangkan di tabel target ada syarat dimana data tidak boleh redundan ketika di timpa banyak kondisi insert \u2026 Continue reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\" \/>\n<meta property=\"og:site_name\" content=\"Suherman Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/emantin34\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/emantin34\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-06T15:12:43+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"639\" \/>\n\t<meta property=\"og:image:height\" content=\"471\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#article\",\"isPartOf\":{\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\"},\"author\":{\"name\":\"admin\",\"@id\":\"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc\"},\"headline\":\"Prevent Insert Using Trigger\",\"datePublished\":\"2019-03-06T15:12:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\"},\"wordCount\":182,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc\"},\"image\":{\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage\"},\"thumbnailUrl\":\"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg\",\"keywords\":[\"PostgreSQL\",\"trigger\"],\"articleSection\":[\"Community\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\",\"url\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\",\"name\":\"Prevent Insert Using Trigger - Suherman Blog\",\"isPartOf\":{\"@id\":\"http:\/\/suherman.asia\/w2\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage\"},\"thumbnailUrl\":\"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg\",\"datePublished\":\"2019-03-06T15:12:43+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage\",\"url\":\"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg\",\"contentUrl\":\"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg\",\"width\":639,\"height\":471,\"caption\":\"prevent insert using trigger in postgreSQL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/suherman.asia\/w2\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Prevent Insert Using Trigger\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/suherman.asia\/w2\/#website\",\"url\":\"http:\/\/suherman.asia\/w2\/\",\"name\":\"Suherman Blog\",\"description\":\"Just Another Geek Site\",\"publisher\":{\"@id\":\"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/suherman.asia\/w2\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/suherman.asia\/w2\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eed6f889b4c2af2a8c18cb3bf63de6a4?s=96&d=retro&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eed6f889b4c2af2a8c18cb3bf63de6a4?s=96&d=retro&r=g\",\"caption\":\"admin\"},\"logo\":{\"@id\":\"http:\/\/suherman.asia\/w2\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/suherman.asia\",\"https:\/\/www.facebook.com\/emantin34\",\"https:\/\/x.com\/emantin34\"],\"url\":\"http:\/\/suherman.asia\/w2\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Prevent Insert Using Trigger - Suherman Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html","og_locale":"en_US","og_type":"article","og_title":"Prevent Insert Using Trigger - Suherman Blog","og_description":"ada suatu kondisi dimana saya harus membuat sebuah tabel dalam database postgreSQL akan dikenakan proses insert yg banyak dari banyak mesin yang mengirimkan data yang kodisi nya bisa saja terjadi data yang sama, sedangkan di tabel target ada syarat dimana data tidak boleh redundan ketika di timpa banyak kondisi insert \u2026 Continue reading","og_url":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html","og_site_name":"Suherman Blog","article_publisher":"https:\/\/www.facebook.com\/emantin34","article_author":"https:\/\/www.facebook.com\/emantin34","article_published_time":"2019-03-06T15:12:43+00:00","og_image":[{"width":639,"height":471,"url":"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg","type":"image\/jpeg"}],"author":"admin","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#article","isPartOf":{"@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html"},"author":{"name":"admin","@id":"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc"},"headline":"Prevent Insert Using Trigger","datePublished":"2019-03-06T15:12:43+00:00","mainEntityOfPage":{"@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html"},"wordCount":182,"commentCount":0,"publisher":{"@id":"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc"},"image":{"@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage"},"thumbnailUrl":"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg","keywords":["PostgreSQL","trigger"],"articleSection":["Community"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#respond"]}]},{"@type":"WebPage","@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html","url":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html","name":"Prevent Insert Using Trigger - Suherman Blog","isPartOf":{"@id":"http:\/\/suherman.asia\/w2\/#website"},"primaryImageOfPage":{"@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage"},"image":{"@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage"},"thumbnailUrl":"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg","datePublished":"2019-03-06T15:12:43+00:00","breadcrumb":{"@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#primaryimage","url":"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg","contentUrl":"http:\/\/suherman.asia\/w2\/wp-content\/uploads\/2019\/03\/trgger.jpg","width":639,"height":471,"caption":"prevent insert using trigger in postgreSQL"},{"@type":"BreadcrumbList","@id":"http:\/\/suherman.asia\/w2\/prevent-insert-using-trigger.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/suherman.asia\/w2"},{"@type":"ListItem","position":2,"name":"Prevent Insert Using Trigger"}]},{"@type":"WebSite","@id":"http:\/\/suherman.asia\/w2\/#website","url":"http:\/\/suherman.asia\/w2\/","name":"Suherman Blog","description":"Just Another Geek Site","publisher":{"@id":"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/suherman.asia\/w2\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"http:\/\/suherman.asia\/w2\/#\/schema\/person\/63654a129ee88012961c1a00415967dc","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/suherman.asia\/w2\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eed6f889b4c2af2a8c18cb3bf63de6a4?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eed6f889b4c2af2a8c18cb3bf63de6a4?s=96&d=retro&r=g","caption":"admin"},"logo":{"@id":"http:\/\/suherman.asia\/w2\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/suherman.asia","https:\/\/www.facebook.com\/emantin34","https:\/\/x.com\/emantin34"],"url":"http:\/\/suherman.asia\/w2\/author\/admin"}]}},"_links":{"self":[{"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/comments?post=651"}],"version-history":[{"count":1,"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":653,"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/posts\/651\/revisions\/653"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/media\/652"}],"wp:attachment":[{"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/suherman.asia\/w2\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}