{"id":1413,"date":"2019-01-04T18:35:44","date_gmt":"2019-01-04T09:35:44","guid":{"rendered":"https:\/\/www.oqiita.com\/?p=1413"},"modified":"2019-01-04T18:35:44","modified_gmt":"2019-01-04T09:35:44","slug":"%e3%80%90pl-sql%e3%80%91%e3%83%88%e3%83%aa%e3%82%ac%e3%83%bc","status":"publish","type":"post","link":"https:\/\/www.oqiita.com\/?p=1413","title":{"rendered":"\u3010PL\/SQL\u3011\u30c8\u30ea\u30ac\u30fc"},"content":{"rendered":"<p>\u66f8\u3044\u3066\u304a\u3044\u3066\u5fd8\u308c\u3066\u305f\u8a18\u4e8b\u304c\u3042\u3063\u305f\u305e\u30fcw<br \/>\nPL\/SQL \u306e \u30c8\u30ea\u30ac\u30fc\u306b\u3064\u3044\u3066\u3060\u305d\u3046\u3067\u3059\uff08\u4ed6\u4eba\u4e8b\uff09<\/p>\n<p><!--more--><\/p>\n<h3 id=\"i-0\">\u30c6\u30fc\u30d6\u30eb\u69cb\u6210<\/h3>\n<p>\u4eca\u56de\u306f\u3053\u306e\u30c6\u30fc\u30d6\u30eb\u3002\u3088\u304f\u3042\u308b\u5546\u54c1\u30de\u30b9\u30bf\u3002<br \/>\n<a href=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER001.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER001.png\" alt=\"\" width=\"950\" height=\"346\" class=\"alignnone size-full wp-image-1415\" srcset=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER001.png 950w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER001-300x109.png 300w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER001-768x280.png 768w\" sizes=\"auto, (max-width: 950px) 100vw, 950px\"><\/a><\/p>\n<h3 id=\"i-1\">\u30c8\u30ea\u30ac\u30fc\u767b\u9332<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plsql\">CREATE OR REPLACE TRIGGER M_PRODUCT_TRIGGER\n  BEFORE INSERT OR UPDATE OR DELETE\n  ON M_PRODUCT\n  FOR EACH ROW\n  \nBEGIN\n\n  DBMS_OUTPUT.PUT_LINE('PRODUCT_ID\uff1a' || :NEW.PRODUCT_ID);\n\n  IF (INSERTING) THEN\n    DBMS_OUTPUT.PUT_LINE('\u767b\u9332');\n    -- \u767b\u9332\u30e6\u30fc\u30b6\u30fc\u3068\u6642\u9593\u3092\u8a2d\u5b9a\n    :NEW.PRODUCT_INSERT_USER := :NEW.PRODUCT_LAST_EDIT_USER;\n    :NEW.PRODUCT_INSERT_DATE := SYSDATE;\n    -- \u66f4\u65b0\u30e6\u30fc\u30b6\u30fc\u3068\u6642\u9593\u3092\u8a2d\u5b9a\n    :NEW.PRODUCT_UPDATE_USER := :NEW.PRODUCT_LAST_EDIT_USER;\n    :NEW.PRODUCT_UPDATE_DATE := SYSDATE;\n  END IF;\n  \n  IF (UPDATING) THEN\n    DBMS_OUTPUT.PUT_LINE('\u66f4\u65b0');\n    -- \u66f4\u65b0\u30e6\u30fc\u30b6\u30fc\u3068\u6642\u9593\u3092\u8a2d\u5b9a\n    :NEW.PRODUCT_UPDATE_USER := :NEW.PRODUCT_LAST_EDIT_USER;\n    :NEW.PRODUCT_UPDATE_DATE := SYSDATE;\n  END IF;\n  \n  IF(:NEW.PRODUCT_DELETE_FLG = 1) THEN\n    DBMS_OUTPUT.PUT_LINE('\u8ad6\u7406\u524a\u9664');\n    -- \u524a\u9664\u30e6\u30fc\u30b6\u30fc\u3068\u6642\u9593\u3092\u8a2d\u5b9a\n    :NEW.PRODUCT_DELETE_USER := :NEW.PRODUCT_LAST_EDIT_USER;\n    :NEW.PRODUCT_DELETE_DATE := SYSDATE;\n  END IF;\n  \n  IF (DELETING) THEN\n    DBMS_OUTPUT.PUT_LINE('\u524a\u9664');\n    -- \u306a\u3093\u304b\u51e6\u7406\u304c\u3042\u3063\u305f\u3089\u3088\u304b\u3063\u305f\uff01\n  END IF;\n\nEND M_PRODUCT_TRIGGER;\n\/<\/code><\/pre>\n<h3 id=\"i-2\">INSERT<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-sql\">-- \u65b0\u898f\u767b\u9332\nINSERT INTO M_PRODUCT(\n  PRODUCT_ID,\n  PRODUCT_NAME,\n  PRODUCT_PRICE,\n  PRODUCT_LAST_EDIT_USER,\n  PRODUCT_DELETE_FLG)\nVALUES(\n  1,\n  'TEST001',\n  100,\n  'oqiita',\n  0)\n;<\/code><\/pre>\n<p>\u767b\u9332\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb<br \/>\n<a href=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER002.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER002-1024x79.png\" alt=\"\" width=\"680\" height=\"52\" class=\"alignnone size-large wp-image-1416\" srcset=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER002-1024x79.png 1024w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER002-300x23.png 300w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER002-768x60.png 768w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER002.png 1649w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\"><\/a><\/p>\n<h3 id=\"i-3\">UPDATE<\/h3>\n<p>\u66f4\u65b0\u524d\u306e\u30c6\u30fc\u30d6\u30eb<br \/>\n<a href=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER003.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER003-1024x111.png\" alt=\"\" width=\"680\" height=\"74\" class=\"alignnone size-large wp-image-1417\" srcset=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER003-1024x111.png 1024w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER003-300x32.png 300w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER003-768x83.png 768w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER003.png 1630w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\"><\/a><\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">-- \u66f4\u65b0\u51e6\u7406\nUPDATE M_PRODUCT SET\n  PRODUCT_PRICE = PRODUCT_PRICE * 1.5,\n  PRODUCT_LAST_EDIT_USER = 'oqiita2'\nWHERE\n  PRODUCT_PRICE &gt;= 200\n;<\/code><\/pre>\n<p>\u66f4\u65b0\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb<br \/>\n<a href=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER004.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER004-1024x117.png\" alt=\"\" width=\"680\" height=\"78\" class=\"alignnone size-large wp-image-1418\" srcset=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER004-1024x117.png 1024w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER004-300x34.png 300w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER004-768x88.png 768w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER004.png 1641w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\"><\/a><\/p>\n<h3 id=\"i-4\">\u8ad6\u7406\u524a\u9664<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-sql\">-- \u8ad6\u7406\u524a\u9664\nUPDATE M_PRODUCT SET\n  PRODUCT_DELETE_FLG = 1,\n  PRODUCT_LAST_EDIT_USER = 'oqiita3'\nWHERE\n  PRODUCT_ID = 3\n;<\/code><\/pre>\n<p>\u8ad6\u7406\u524a\u9664\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb<br \/>\n<a href=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER005.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER005-1024x109.png\" alt=\"\" width=\"680\" height=\"72\" class=\"alignnone size-large wp-image-1419\" srcset=\"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER005-1024x109.png 1024w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER005-300x32.png 300w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER005-768x82.png 768w, https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/09\/TRIGGER005.png 1628w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\"><\/a><\/p>\n<h3 id=\"i-5\">DELETE<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-sql\">-- \u7269\u7406\u524a\u9664\nDELETE FROM M_PRODUCT WHERE PRODUCT_ID = 4;\n<\/code><\/pre>\n<p>\u3068\u304f\u306b\u51e6\u7406\u304c\u601d\u3044\u6d6e\u304b\u3070\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u30ed\u30b0\u304c\u3067\u308b\u3060\u3051\u3067\u3059\uff01<\/p>\n<h3 id=\"i-6\">\u307e\u3068\u3081<\/h3>\n<p>\u3042\u308c\u3070\u4fbf\u5229\u306a\u30c8\u30ea\u30ac\u30fc\u3002MYSQL\u306b\u306a\u308b\u3068\u307e\u305f\u4f5c\u308a\u65b9\u304c\u9055\u3046\u304b\u3089\u3084\u3063\u304b\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u66f8\u3044\u3066\u304a\u3044\u3066\u5fd8\u308c\u3066\u305f\u8a18\u4e8b\u304c\u3042\u3063\u305f\u305e\u30fcw PL\/SQL \u306e \u30c8\u30ea\u30ac\u30fc\u306b\u3064\u3044\u3066\u3060\u305d\u3046\u3067\u3059\uff08\u4ed6\u4eba\u4e8b\uff09<\/p>\n","protected":false},"author":1,"featured_media":733,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[177],"tags":[92,180],"class_list":{"0":"post-1413","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-pl-sql","8":"tag-oracle","9":"tag-180","10":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.oqiita.com\/wp-content\/uploads\/2018\/05\/sango_db_icatch.png","jetpack_shortlink":"https:\/\/wp.me\/p8QluP-mN","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts\/1413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1413"}],"version-history":[{"count":4,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions\/1480"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=\/wp\/v2\/media\/733"}],"wp:attachment":[{"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oqiita.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}