{"id":82075,"date":"2024-11-28T15:38:12","date_gmt":"2024-11-28T08:38:12","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=82075"},"modified":"2024-11-28T15:38:12","modified_gmt":"2024-11-28T08:38:12","slug":"trigger-trong-sql","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/","title":{"rendered":"Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Trigger_la_gi\" >Trigger l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Thanh_phan_cua_mot_trigger_trong_SQL\" >Th\u00e0nh ph\u1ea7n c\u1ee7a m\u1ed9t trigger trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Mot_so_thao_tac_voi_trigger\" >M\u1ed9t s\u1ed1 thao t\u00e1c v\u1edbi trigger<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Cac_loai_trigger_trong_SQL\" >C\u00e1c lo\u1ea1i trigger trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Mot_so_tips_su_dung_trigger_trong_SQL_hieu_qua\" >M\u1ed9t s\u1ed1 tips s\u1eed d\u1ee5ng trigger trong SQL hi\u1ec7u qu\u1ea3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Uu_diem_va_han_che_cua_trigger_trong_SQL\" >\u01afu \u0111i\u1ec3m v\u00e0 h\u1ea1n ch\u1ebf c\u1ee7a trigger trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Cau_hoi_thuong_gap_ve_trigger_trong_SQL\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 trigger trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#Tong_ket_Trigger_trong_SQL\" >T\u1ed5ng k\u1ebft Trigger trong SQL<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>Trigger trong SQL l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd trong qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u. T\u00ecm hi\u1ec3u v\u1ec1 c\u00e1c thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi trigger SQL v\u1edbi t\u1eebng lo\u1ea1i trigger kh\u00e1c nhau.<\/strong><\/em><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Trigger l\u00e0 g\u00ec<\/span><\/li>\n<li aria-level=\"1\">C\u00e1c th\u00e0nh ph\u1ea7n ch\u00ednh c\u1ee7a m\u1ed9t trigger<\/li>\n<li aria-level=\"1\">C\u00e1c thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi trigger trong SQL<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c lo\u1ea1i trigger<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u01afu nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a trigger<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Trigger_la_gi\"><\/span><b>Trigger l\u00e0 g\u00ec?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Trigger (hay tr\u00ecnh k\u00edch ho\u1ea1t) trong SQL l\u00e0 m\u1ed9t \u0111o\u1ea1n m\u00e3 t\u1ef1 \u0111\u1ed9ng th\u1ef1c thi khi x\u1ea3y ra m\u1ed9t s\u1ef1 ki\u1ec7n c\u1ee5 th\u1ec3 tr\u00ean b\u1ea3ng ho\u1eb7c ch\u1ebf \u0111\u1ed9 xem (view) trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, ch\u1eb3ng h\u1ea1n nh\u01b0 th\u00eam, s\u1eeda, x\u00f3a d\u1eef li\u1ec7u ho\u1eb7c thay \u0111\u1ed5i c\u1ea5u tr\u00fac b\u1ea3ng. Trigger ho\u1ea1t \u0111\u1ed9ng nh\u01b0 m\u1ed9t c\u01a1 ch\u1ebf gi\u00e1m s\u00e1t, \u0111\u1ea3m b\u1ea3o r\u1eb1ng m\u1ecdi thay \u0111\u1ed5i trong c\u01a1 s\u1edf d\u1eef li\u1ec7u tu\u00e2n theo c\u00e1c quy t\u1eafc v\u00e0 quy \u0111\u1ecbnh \u0111\u00e3 thi\u1ebft l\u1eadp. Trigger gi\u00fap duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u, t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 gi\u1ea3m thi\u1ec3u l\u1ed7i do thao t\u00e1c th\u1ee7 c\u00f4ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trigger c\u00f2n \u0111\u01b0\u1ee3c coi l\u00e0 m\u1ed9t d\u1ea1ng stored procedure \u0111\u1eb7c bi\u1ec7t nh\u01b0ng kh\u00f4ng c\u00f3 tham s\u1ed1 v\u00e0 ch\u00fang \u0111\u01b0\u1ee3c t\u1ef1 \u0111\u1ed9ng k\u00edch ho\u1ea1t m\u00e0 kh\u00f4ng c\u1ea7n \u0111\u01b0\u1ee3c g\u1ecdi tr\u1ef1c ti\u1ebfp b\u1edfi ng\u01b0\u1eddi d\u00f9ng hay \u1ee9ng d\u1ee5ng. V\u00ed d\u1ee5, trong tr\u01b0\u1eddng h\u1ee3p b\u1ea1n c\u1ea7n theo d\u00f5i t\u1ea7n su\u1ea5t v\u00e0 th\u1eddi \u0111i\u1ec3m thay \u0111\u1ed5i d\u1eef li\u1ec7u trong m\u1ed9t b\u1ea3ng th\u01b0\u1eddng xuy\u00ean c\u1eadp nh\u1eadt, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng trigger \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng ghi l\u1ea1i th\u00f4ng tin thay \u0111\u1ed5i v\u00e0o m\u1ed9t b\u1ea3ng kh\u00e1c, gi\u00fap d\u1ec5 d\u00e0ng ki\u1ec3m so\u00e1t v\u00e0 ph\u00e2n t\u00edch l\u1ecbch s\u1eed thay \u0111\u1ed5i m\u00e0 kh\u00f4ng c\u1ea7n thao t\u00e1c th\u1ee7 c\u00f4ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T\u00f9y v\u00e0o h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u, c\u00fa ph\u00e1p v\u00e0 ch\u1ee9c n\u0103ng c\u1ee5 th\u1ec3 c\u1ee7a triggers c\u00f3 th\u1ec3 kh\u00e1c nhau, nh\u01b0ng m\u1ee5c \u0111\u00edch chung v\u1eabn l\u00e0 duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u ho\u1eb7c t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 b\u1ea3o v\u1ec7 ho\u1ea1t \u0111\u1ed9ng c\u1ee7a c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Thanh_phan_cua_mot_trigger_trong_SQL\"><\/span>Th\u00e0nh ph\u1ea7n c\u1ee7a m\u1ed9t trigger trong SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Trigger l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh SQL t\u1ef1 \u0111\u1ed9ng \u0111\u01b0\u1ee3c th\u1ef1c thi khi x\u1ea3y ra m\u1ed9t s\u1ef1 ki\u1ec7n c\u1ee5 th\u1ec3 tr\u00ean c\u01a1 s\u1edf d\u1eef li\u1ec7u. C\u00e1c th\u00e0nh ph\u1ea7n ch\u00ednh c\u1ee7a m\u1ed9t trigger bao g\u1ed3m:<\/span><\/p>\n<ul>\n<li><b>T\u00ean Trigger<\/b><span style=\"font-weight: 400;\">: T\u00ean \u0111\u1ecbnh danh c\u1ee7a trigger trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n<li><b>\u0110\u1ed1i t\u01b0\u1ee3ng b\u1ecb \u1ea3nh h\u01b0\u1edfng<\/b><span style=\"font-weight: 400;\">: L\u00e0 b\u1ea3ng ho\u1eb7c view m\u00e0 trigger \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng. \u0110i\u1ec1u n\u00e0y \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh trong ph\u1ea7n <\/span><span style=\"font-weight: 400;\">ON<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><b>Lo\u1ea1i S\u1ef1 Ki\u1ec7n<\/b><span style=\"font-weight: 400;\">: X\u00e1c \u0111\u1ecbnh trigger s\u1ebd \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t khi n\u00e0o, bao g\u1ed3m c\u00e1c h\u00e0nh \u0111\u1ed9ng nh\u01b0 <\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\">, ho\u1eb7c c\u00e1c s\u1ef1 ki\u1ec7n DDL (Data Definition Language).<\/span><\/li>\n<li><b>Th\u1eddi \u0111i\u1ec3m K\u00edch Ho\u1ea1t<\/b><span style=\"font-weight: 400;\">:<\/span>\n<ul>\n<li><span style=\"font-weight: 400;\">AFTER<\/span><span style=\"font-weight: 400;\">: Trigger th\u1ef1c thi sau khi s\u1ef1 ki\u1ec7n ho\u00e0n th\u00e0nh.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">INSTEAD OF<\/span><span style=\"font-weight: 400;\">: Thay th\u1ebf h\u00e0nh \u0111\u1ed9ng m\u1eb7c \u0111\u1ecbnh c\u1ee7a s\u1ef1 ki\u1ec7n.<\/span><\/li>\n<\/ul>\n<\/li>\n<li><b>Th\u00e2n Trigger (Trigger Body)<\/b><span style=\"font-weight: 400;\">: N\u1eb1m gi\u1eefa <\/span><span style=\"font-weight: 400;\">BEGIN &#8230; END<\/span><span style=\"font-weight: 400;\">, ch\u1ee9a c\u00e1c c\u00e2u l\u1ec7nh SQL th\u1ef1c hi\u1ec7n h\u00e0nh \u0111\u1ed9ng c\u1ee5 th\u1ec3 khi trigger \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t. V\u00ed d\u1ee5: ghi log, ki\u1ec3m tra \u0111i\u1ec1u ki\u1ec7n ho\u1eb7c th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c tr\u00ean b\u1ea3ng kh\u00e1c.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u1ea5u tr\u00fac t\u1ed5ng qu\u00e1t c\u1ee7a trigger nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE|ALTER TRIGGER trigger_name<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON {table_name | view_name | DATABASE | ALL SERVER}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{FOR | AFTER | INSTEAD OF} {event_type}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0-- C\u00e1c c\u00e2u l\u1ec7nh c\u1ea7n th\u1ef1c hi\u1ec7n khi trigger \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Mot_so_thao_tac_voi_trigger\"><\/span><b>M\u1ed9t s\u1ed1 thao t\u00e1c v\u1edbi trigger<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p v\u1edbi trigger c\u00f3 th\u1ec3 kh\u00e1c nhau t\u00f9y theo h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (DBMS) m\u00e0 b\u1ea1n \u0111ang s\u1eed d\u1ee5ng. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t c\u1ee7a trigger trong SQL Server:<\/span><\/p>\n<h3><b>T\u1ea1o trigger<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi t\u1ea1o trigger b\u1ea1n s\u1ebd \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u ph\u1ea3i x\u00e1c \u0111\u1ecbnh th\u1eddi \u0111i\u1ec3m trigger s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi (khi n\u00e0o) v\u00e0 h\u00e0nh \u0111\u1ed9ng m\u00e0 n\u00f3 s\u1ebd th\u1ef1c hi\u1ec7n.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE TRIGGER [schema_name.]trigger_name\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON { table_name | view_name }\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{ BEFORE | AFTER | INSTEAD OF } { INSERT | UPDATE | DELETE }\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0-- Trigger statements\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t trigger ghi l\u1ea1i c\u00e1c th\u00f4ng tin c\u1ee7a b\u1ea3n ghi b\u1ecb x\u00f3a trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0o b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees_log<\/span><\/i><span style=\"font-weight: 400;\"> ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE TRIGGER after_employee_delete\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON employees\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AFTER DELETE\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0INSERT INTO employees_log (employee_id, name, action)\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0VALUES (OLD.employee_id, OLD.name, 'deleted');\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<h3><b>Xo\u00e1 trigger<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">DROP TRIGGER [IF EXISTS] schema_name.trigger_name;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ho\u1eb7c n\u1ebfu b\u1ea1n s\u1eed d\u1ee5ng SQL Server, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1m th\u1eddi v\u00f4 hi\u1ec7u h\u00f3a trigger m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i xo\u00e1 ho\u1eb7c s\u1eeda l\u1ea1i trigger v\u1edbi c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">DISABLE TRIGGER trigger_name ON table_name;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 k\u00edch ho\u1ea1t l\u1ea1i trigger, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">ENABLE TRIGGER trigger_name ON table_name;<\/span><\/pre>\n<h3><b>S\u1eeda \u0111\u1ed5i trigger<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 s\u1eeda \u0111\u1ed5i m\u1ed9t trigger, b\u1ea1n c\u1ea7n ph\u1ea3i x\u00f3a trigger c\u0169 v\u00e0 t\u1ea1o m\u1ed9t trigger m\u1edbi v\u1edbi c\u00e1c thay \u0111\u1ed5i mong mu\u1ed1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">DROP TRIGGER IF EXISTS trigger_name;\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">CREATE TRIGGER trigger_name\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON { table_name | view_name }\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{ BEFORE | AFTER | INSTEAD OF } { INSERT | UPDATE | DELETE }\u00a0\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0-- Trigger statements\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 x\u00f3a trigger <\/span><i><span style=\"font-weight: 400;\">log_changes<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0 t\u1ea1o l\u1ea1i trigger m\u1edbi \u0111\u1ec3 ghi l\u1ea1i th\u00f4ng tin khi b\u1ea3n ghi trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">DROP TRIGGER IF EXISTS log_changes;\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">CREATE TRIGGER log_changes\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON employees\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AFTER UPDATE\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0INSERT INTO employees_log (employee_id, name, action)\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0VALUES (OLD.employee_id, OLD.name, 'updated');\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<h3><b>Ki\u1ec3m tra trigger<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 xem c\u00e1c trigger \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea1o trong c\u01a1 s\u1edf d\u1eef li\u1ec7u b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c c\u00e2u l\u1ec7nh truy v\u1ea5n \u0111\u1eb7c bi\u1ec7t t\u00f9y theo DBMS m\u00e0 b\u1ea1n s\u1eed d\u1ee5ng. Trong SQL Server, b\u1ea1n c\u00f3 th\u1ec3 xem c\u00e1c trigger th\u00f4ng qua truy v\u1ea5n h\u1ec7 th\u1ed1ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT * FROM sys.triggers<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean s\u1ebd tr\u1ea3 v\u1ec1 b\u1ea3ng k\u1ebft qu\u1ea3 g\u1ed3m:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>C\u1ed9t<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">name<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">T\u00ean trigger.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">object_id<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">ID duy nh\u1ea5t c\u1ee7a trigger trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">parent_id<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">ID c\u1ee7a b\u1ea3ng ho\u1eb7c \u0111\u1ed1i t\u01b0\u1ee3ng m\u00e0 trigger \u0111\u01b0\u1ee3c g\u1eafn v\u00e0o.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">type<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Lo\u1ea1i \u0111\u1ed1i t\u01b0\u1ee3ng, th\u01b0\u1eddng l\u00e0 &#8216;TR&#8217; cho trigger.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">type_desc<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">M\u00f4 t\u1ea3 lo\u1ea1i \u0111\u1ed1i t\u01b0\u1ee3ng, v\u00ed d\u1ee5: &#8216;SQL_TRIGGER&#8217; cho trigger th\u00f4ng th\u01b0\u1eddng.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">create_date<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Th\u1eddi gian t\u1ea1o trigger.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">modify_date<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Th\u1eddi gian ch\u1ec9nh s\u1eeda g\u1ea7n nh\u1ea5t c\u1ee7a trigger.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">is_ms_shipped<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb boolean (0 ho\u1eb7c 1), x\u00e1c \u0111\u1ecbnh trigger do h\u1ec7 th\u1ed1ng cung c\u1ea5p hay kh\u00f4ng.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">is_disabled<\/span><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb boolean, x\u00e1c \u0111\u1ecbnh trigger c\u00f3 \u0111ang b\u1ecb v\u00f4 hi\u1ec7u h\u00f3a hay kh\u00f4ng.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 ta c\u00f3 k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>name<\/b><\/td>\n<td><b>object_id<\/b><\/td>\n<td><b>parent_id<\/b><\/td>\n<td><b>type<\/b><\/td>\n<td><b>type_desc<\/b><\/td>\n<td><b>create_date<\/b><\/td>\n<td><b>modify_date<\/b><\/td>\n<td><b>is_ms_shipped<\/b><\/td>\n<td><b>is_disabled<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">log_changes<\/span><\/td>\n<td><span style=\"font-weight: 400;\">123456<\/span><\/td>\n<td><span style=\"font-weight: 400;\">654321<\/span><\/td>\n<td><span style=\"font-weight: 400;\">TR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">SQL_TRIGGER<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2024-01-01 10:00:00<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2024-01-02 12:00:00<\/span><\/td>\n<td><span style=\"font-weight: 400;\">0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Cac_loai_trigger_trong_SQL\"><\/span><b>C\u00e1c lo\u1ea1i trigger trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>DML trigger\u00a0<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">DML Trigger (Data Manipulation Language Trigger) \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t b\u1edfi c\u00e1c thao t\u00e1c d\u1eef li\u1ec7u nh\u01b0 <\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\"> tr\u00ean b\u1ea3ng ho\u1eb7c view. \u0110\u00e2y l\u00e0 lo\u1ea1i trigger gi\u00fap gi\u00e1m s\u00e1t v\u00e0 t\u1ef1 \u0111\u1ed9ng x\u1eed l\u00fd c\u00e1c thay \u0111\u1ed5i d\u1eef li\u1ec7u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">DML trigger bao g\u1ed3m:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Before Trigger<\/b><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c k\u00edch ho\u1ea1t tr\u01b0\u1edbc khi h\u00e0nh \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n th\u00e0nh c\u00f4ng v\u00e0 bao g\u1ed3m <\/span><span style=\"font-weight: 400;\">BEFORE<\/span><span style=\"font-weight: 400;\"> + INSERT\/UPDATE\/DELETE<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>After Trigger<\/b><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c k\u00edch ho\u1ea1t sau khi h\u00e0nh \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n th\u00e0nh c\u00f4ng v\u00e0 bao g\u1ed3m <\/span><span style=\"font-weight: 400;\">AFTER<\/span><span style=\"font-weight: 400;\"> + INSERT\/UPDATE\/DELETE<\/span><span style=\"font-weight: 400;\">. L\u01b0u \u00fd l\u00e0 After trigger kh\u00f4ng \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 tr\u00ean view.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Instead of Trigger<\/b><span style=\"font-weight: 400;\">: Thay th\u1ebf h\u00e0nh \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\"> m\u1eb7c \u0111\u1ecbnh b\u1eb1ng h\u00e0nh \u0111\u1ed9ng \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh trong trigger v\u00e0 bao g\u1ed3m <\/span><span style=\"font-weight: 400;\">INSTEAD OF<\/span><span style=\"font-weight: 400;\"> + INSERT\/UPDATE\/DELETE<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<blockquote><p><b><i>L\u01b0u \u00fd l\u00e0 SQL Server kh\u00f4ng h\u1ed7 tr\u1ee3 <\/i><\/b><i><span style=\"font-weight: 400;\">BEFORE Trigger<\/span><\/i><b><i>.<\/i><\/b><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">DML trigger th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ki\u1ec3m tra t\u00ednh h\u1ee3p l\u1ec7 ho\u1eb7c gi\u1edbi h\u1ea1n thao t\u00e1c d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng c\u1eadp nh\u1eadt b\u1ea3ng kh\u00e1c d\u1ef1a tr\u00ean thay \u0111\u1ed5i trong b\u1ea3ng ch\u00ednh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ghi l\u1ea1i l\u1ecbch s\u1eed thay \u0111\u1ed5i d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t trong SQL server:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE|ALTER TRIGGER [schema_name.]trigger_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON {table_name | view_name}\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{FOR | BEFORE | AFTER | INSTEAD OF } { INSERT | UPDATE | DELETE }<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">-- trigger statements\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<blockquote><p><i>L\u01b0u \u00fd l\u00e0 After trigger kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng tr\u00ean view trong SQL server.<\/i><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">schema_name<\/span><span style=\"font-weight: 400;\">: T\u00f9y ch\u1ecdn, l\u00e0 schema ch\u1ee9a b\u1ea3ng ho\u1eb7c view.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">trigger_name<\/span><span style=\"font-weight: 400;\">: T\u00ean trigger.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">AFTER<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">INSTEAD OF<\/span><span style=\"font-weight: 400;\">: Th\u1eddi \u0111i\u1ec3m k\u00edch ho\u1ea1t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">INSERT | UPDATE | DELETE<\/span><span style=\"font-weight: 400;\">: Lo\u1ea1i s\u1ef1 ki\u1ec7n k\u00edch ho\u1ea1t trigger.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">BEGIN &#8230; END<\/span><span style=\"font-weight: 400;\">: Ch\u1ee9a c\u00e1c l\u1ec7nh SQL c\u1ea7n th\u1ef1c hi\u1ec7n.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t <\/span><strong>AFTER INSERT<\/strong><span style=\"font-weight: 400;\"><strong> trigger<\/strong> tr\u00ean b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Employees<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u1ec3 ghi log khi c\u00f3 b\u1ea3n ghi m\u1edbi \u0111\u01b0\u1ee3c th\u00eam v\u00e0o, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE TRIGGER roAfterInsert\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON Employees\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AFTER INSERT\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0INSERT INTO EmployeeLog(EmployeeID, Action)\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0SELECT EmployeeID, 'Inserted'\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0FROM inserted;\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1ea3ng <\/span><span style=\"font-weight: 400;\">inserted<\/span><span style=\"font-weight: 400;\">: L\u00e0 b\u1ea3ng t\u1ea1m do SQL Server t\u1ea1o, ch\u1ee9a c\u00e1c b\u1ea3n ghi m\u1edbi \u0111\u01b0\u1ee3c th\u00eam v\u00e0o.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1ea3ng <\/span><span style=\"font-weight: 400;\">deleted<\/span><span style=\"font-weight: 400;\">: S\u1eed d\u1ee5ng trong c\u00e1c trigger <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ee9a c\u00e1c b\u1ea3n ghi b\u1ecb x\u00f3a ho\u1eb7c b\u1ecb thay \u0111\u1ed5i tr\u01b0\u1edbc khi c\u1eadp nh\u1eadt.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">=&gt; B\u1ea3ng <\/span><span style=\"font-weight: 400;\">inserted<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">deleted<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 so s\u00e1nh d\u1eef li\u1ec7u tr\u01b0\u1edbc v\u00e0 sau khi thay \u0111\u1ed5i trong trigger <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><b>DDL Trigger<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">DDL Trigger (Data Definition Language Trigger) c\u00f3 ch\u1ee9c n\u0103ng ph\u1ea3n h\u1ed3i c\u00e1c s\u1ef1 ki\u1ec7n li\u00ean quan \u0111\u1ebfn thay \u0111\u1ed5i c\u1ea5u tr\u00fac c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 <\/span><span style=\"font-weight: 400;\">CREATE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">ALTER<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">DROP<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">DDL trigger th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ng\u0103n ch\u1eb7n c\u00e1c thay \u0111\u1ed5i kh\u00f4ng mong mu\u1ed1n trong c\u1ea5u tr\u00fac c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Theo d\u00f5i v\u00e0 ghi l\u1ea1i c\u00e1c thay \u0111\u1ed5i schema cho m\u1ee5c \u0111\u00edch audit.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c thao t\u00e1c b\u1ea3o m\u1eadt \u0111\u1ed1i v\u1edbi thay \u0111\u1ed5i c\u1ea5u tr\u00fac d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t trong SQL Server:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE|ALTER TRIGGER trigger_name\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON { ALL SERVER | DATABASE }\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{ FOR | AFTER } { event_type }\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0-- Trigger statements\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">ALL SERVER<\/span><span style=\"font-weight: 400;\">: \u00c1p d\u1ee5ng tr\u00ean to\u00e0n b\u1ed9 server.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">DATABASE<\/span><span style=\"font-weight: 400;\">: \u00c1p d\u1ee5ng tr\u00ean c\u01a1 s\u1edf d\u1eef li\u1ec7u c\u1ee5 th\u1ec3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">event_type<\/span><span style=\"font-weight: 400;\">: C\u00e1c s\u1ef1 ki\u1ec7n nh\u01b0 <\/span><span style=\"font-weight: 400;\">CREATE_TABLE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">DROP_TABLE<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t DDL trigger \u0111\u1ec3 ng\u0103n ch\u1eb7n vi\u1ec7c x\u00f3a b\u1ea3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE TRIGGER PreventDropTable\u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">ON DATABASE\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FOR DROP_TABLE\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0PRINT 'Dropping tables is not allowed';\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0ROLLBACK;\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3i th\u00edch v\u00ed d\u1ee5: \u200b<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi h\u00e0nh \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">DROP TABLE<\/span><span style=\"font-weight: 400;\"> x\u1ea3y ra, trigger s\u1ebd hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o v\u00e0 h\u1ee7y b\u1ecf h\u00e0nh \u0111\u1ed9ng.<\/span><\/li>\n<\/ul>\n<h3><b>Session Trigger<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">L\u00e0 lo\u1ea1i trigger \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t khi c\u00f3 s\u1ef1 ki\u1ec7n <\/span><span style=\"font-weight: 400;\">LOGON<\/span><span style=\"font-weight: 400;\"> l\u00e0 khi m\u1ed9t ng\u01b0\u1eddi d\u00f9ng \u0111\u0103ng nh\u1eadp th\u00e0nh c\u00f4ng nh\u01b0ng tr\u01b0\u1edbc khi phi\u00ean l\u00e0m vi\u1ec7c b\u1eaft \u0111\u1ea7u ho\u1eb7c khi s\u1ef1 ki\u1ec7n <\/span><span style=\"font-weight: 400;\">LOGOFF<\/span><span style=\"font-weight: 400;\"> khi ng\u01b0\u1eddi d\u00f9ng k\u1ebft th\u00fac phi\u00ean l\u00e0m vi\u1ec7c. C\u1ea3 hai trigger n\u00e0y cho ph\u00e9p ki\u1ec3m so\u00e1t phi\u00ean l\u00e0m vi\u1ec7c c\u1ee7a ng\u01b0\u1eddi d\u00f9ng, ghi log th\u00f4ng tin \u0111\u0103ng nh\u1eadp ho\u1eb7c \u0111\u0103ng xu\u1ea5t v\u00e0 \u00e1p \u0111\u1eb7t c\u00e1c ch\u00ednh s\u00e1ch b\u1ea3o m\u1eadt.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Session Trigger th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ki\u1ec3m so\u00e1t s\u1ed1 l\u01b0\u1ee3ng phi\u00ean l\u00e0m vi\u1ec7c, ghi nh\u1eadn th\u1eddi gian \u0111\u0103ng nh\u1eadp, ho\u1eb7c gi\u1edbi h\u1ea1n truy c\u1eadp d\u1ef1a tr\u00ean \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 d\u1ecdn d\u1eb9p ho\u1eb7c ghi l\u1ea1i th\u00f4ng tin cu\u1ed1i c\u00f9ng c\u1ee7a phi\u00ean l\u00e0m vi\u1ec7c.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t tr\u00ean SQL server:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE | ALTER\u00a0 TRIGGER trigger_name\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON ALL SERVER<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{ FOR | AFTER } {LOGON | LOGOFF}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0-- Trigger statements\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">ALL SERVER<\/span><span style=\"font-weight: 400;\">: Ch\u1ec9 \u0111\u1ecbnh trigger \u00e1p d\u1ee5ng \u1edf c\u1ea5p m\u00e1y ch\u1ee7.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">LOGON | LOGOFF<\/span><span style=\"font-weight: 400;\">: Ch\u1ec9 \u0111\u1ecbnh s\u1ef1 ki\u1ec7n k\u00edch ho\u1ea1t khi ng\u01b0\u1eddi d\u00f9ng \u0111\u0103ng nh\u1eadp ho\u1eb7c \u0111\u0103ng xu\u1ea5t.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t logon trigger \u0111\u1ec3 gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng phi\u00ean \u0111\u0103ng nh\u1eadp cho m\u1ed9t t\u00e0i kho\u1ea3n ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE TRIGGER LimitLoginSessions\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON ALL SERVER\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0FOR LOGON\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0IF (SELECT COUNT(*)\u00a0 FROM sys.dm_exec_sessions WHERE login_name = ORIGINAL_LOGIN()) &gt; 3\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0BEGIN\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PRINT 'Login limit exceeded';\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ROLLBACK;\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0END\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3i th\u00edch v\u00ed d\u1ee5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">sys.dm_exec_sessions<\/span><span style=\"font-weight: 400;\">:\u00a0 l\u00e0 b\u1ea3ng h\u1ec7 th\u1ed1ng ch\u1ee9a th\u00f4ng tin v\u1ec1 c\u00e1c phi\u00ean l\u00e0m vi\u1ec7c hi\u1ec7n t\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Trigger s\u1ebd h\u1ee7y \u0111\u0103ng nh\u1eadp n\u1ebfu s\u1ed1 phi\u00ean v\u01b0\u1ee3t qu\u00e1 3.<\/span><\/li>\n<\/ul>\n<h3><b>Server Error Trigger<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">L\u00e0 lo\u1ea1i trigger \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t khi x\u1ea3y ra l\u1ed7i m\u00e1y ch\u1ee7 trong qu\u00e1 tr\u00ecnh ho\u1ea1t \u0111\u1ed9ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Server Error Trigger \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ghi nh\u1eadn l\u1ed7i h\u1ec7 th\u1ed1ng \u0111\u1ec3 ph\u00e2n t\u00edch v\u00e0 x\u1eed l\u00fd sau.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n c\u00e1c h\u00e0nh \u0111\u1ed9ng kh\u1eafc ph\u1ee5c khi x\u1ea3y ra l\u1ed7i nghi\u00eam tr\u1ecdng.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t tr\u00ean SQL Server:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE | ALTER\u00a0 TRIGGER trigger_name\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON ALL SERVER<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{ FOR | AFTER } {LOGON | LOGOFF}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0-- Trigger logic here<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">ERROR_LOG<\/span><span style=\"font-weight: 400;\">: S\u1ef1 ki\u1ec7n k\u00edch ho\u1ea1t trigger khi m\u1ed9t l\u1ed7i h\u1ec7 th\u1ed1ng \u0111\u01b0\u1ee3c ghi l\u1ea1i.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t trigger c\u00f3 ch\u1ee9c n\u0103ng gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng phi\u00ean \u0111\u0103ng nh\u1eadp \u0111\u1ed3ng th\u1eddi ta s\u1eed d\u1ee5 c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE TRIGGER LimitLoginSessions\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON ALL SERVER\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FOR LOGON\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">AS\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0IF (SELECT COUNT(*)\u00a0 FROM sys.dm_exec_sessions WHERE login_name = ORIGINAL_LOGIN()) &gt; 3\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0BEGIN<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PRINT 'Login limit exceeded';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ROLLBACK;\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0END\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3i th\u00edch v\u00ed d\u1ee5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">ERROR_MESSAGE()<\/span><span style=\"font-weight: 400;\">: H\u00e0m tr\u1ea3 v\u1ec1 th\u00f4ng b\u00e1o l\u1ed7i g\u1ea7n nh\u1ea5t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">GETDATE()<\/span><span style=\"font-weight: 400;\">: H\u00e0m l\u1ea5y th\u1eddi gian hi\u1ec7n t\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Trigger ghi l\u1ea1i l\u1ed7i v\u00e0o b\u1ea3ng <\/span><span style=\"font-weight: 400;\">ServerErrorLog<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ph\u00e2n t\u00edch sau.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Mot_so_tips_su_dung_trigger_trong_SQL_hieu_qua\"><\/span><b>M\u1ed9t s\u1ed1 tips s\u1eed d\u1ee5ng trigger trong SQL hi\u1ec7u qu\u1ea3<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Gi\u1eef trigger \u0111\u01a1n gi\u1ea3n v\u00e0 hi\u1ec7u qu\u1ea3<\/b><span style=\"font-weight: 400;\">: Tr\u00e1nh c\u00e1c logic ph\u1ee9c t\u1ea1p ho\u1eb7c thao t\u00e1c d\u1eef li\u1ec7u l\u1edbn trong trigger \u0111\u1ec3 kh\u00f4ng l\u00e0m ch\u1eadm hi\u1ec7u su\u1ea5t h\u1ec7 th\u1ed1ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng trigger \u0111\u1ec3 ghi log v\u00e0 audit<\/b><span style=\"font-weight: 400;\">: Trigger r\u1ea5t h\u1eefu \u00edch trong vi\u1ec7c t\u1ef1 \u0111\u1ed9ng ghi l\u1ea1i c\u00e1c thay \u0111\u1ed5i d\u1eef li\u1ec7u, t\u1ea1o ra c\u00e1c b\u1ea3n ghi audit \u0111\u1ec3 theo d\u00f5i v\u00e0 \u0111\u1ea3m b\u1ea3o t\u00ednh minh b\u1ea1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Tr\u00e1nh th\u1ef1c hi\u1ec7n c\u00e1c h\u00e0nh \u0111\u1ed9ng x\u1eed l\u00fd ph\u1ee9c t\u1ea1p trong trigger<\/b><span style=\"font-weight: 400;\">: Nh\u1eefng h\u00e0nh \u0111\u1ed9ng ph\u1ee9c t\u1ea1p n\u00ean \u0111\u01b0\u1ee3c qu\u1ea3n l\u00fd trong \u1ee9ng d\u1ee5ng ho\u1eb7c stored procedure, trigger ch\u1ec9 n\u00ean d\u00f9ng cho c\u00e1c t\u00e1c v\u1ee5 t\u1ef1 \u0111\u1ed9ng \u0111\u01a1n gi\u1ea3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ghi ch\u00fa r\u00f5 r\u00e0ng cho trigger<\/b><span style=\"font-weight: 400;\">: Ghi ch\u00e9p v\u1ec1 m\u1ee5c \u0111\u00edch v\u00e0 c\u00e1ch th\u1ee9c ho\u1ea1t \u0111\u1ed9ng c\u1ee7a trigger gi\u00fap vi\u1ec7c b\u1ea3o tr\u00ec v\u00e0 hi\u1ec3u r\u00f5 h\u1ec7 th\u1ed1ng d\u1ec5 d\u00e0ng h\u01a1n, \u0111\u1eb7c bi\u1ec7t khi c\u00f3 nhi\u1ec1u trigger trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Uu_diem_va_han_che_cua_trigger_trong_SQL\"><\/span><b>\u01afu \u0111i\u1ec3m v\u00e0 h\u1ea1n ch\u1ebf c\u1ee7a trigger trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<table>\n<tbody>\n<tr>\n<td><b>\u01afu \u0111i\u1ec3m<\/b><\/td>\n<td><b>H\u1ea1n ch\u1ebf<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>T\u1ef1 \u0111\u1ed9ng th\u1ef1c thi c\u00e1c t\u00e1c v\u1ee5<\/b><span style=\"font-weight: 400;\">: Trigger \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t t\u1ef1 \u0111\u1ed9ng, gi\u1ea3m thi\u1ec3u s\u1ef1 can thi\u1ec7p th\u1ee7 c\u00f4ng.<\/span><\/td>\n<td><b>G\u00e2y qu\u00e1 t\u1ea3i hi\u1ec7u su\u1ea5t<\/b><span style=\"font-weight: 400;\">: Trigger c\u00f3 th\u1ec3 l\u00e0m t\u0103ng t\u1ea3i cho m\u00e1y ch\u1ee7 c\u01a1 s\u1edf d\u1eef li\u1ec7u n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>T\u0103ng c\u01b0\u1eddng t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u<\/b><span style=\"font-weight: 400;\">: Trigger \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c ki\u1ec3m tra v\u00e0 tu\u00e2n th\u1ee7 c\u00e1c quy t\u1eafc tr\u01b0\u1edbc khi ch\u00e8n ho\u1eb7c c\u1eadp nh\u1eadt.<\/span><\/td>\n<td><b>Kh\u00f3 kh\u0103n trong vi\u1ec7c g\u1ee1 l\u1ed7i<\/b><span style=\"font-weight: 400;\">: Trigger ho\u1ea1t \u0111\u1ed9ng \u1edf t\u1ea7ng c\u01a1 s\u1edf d\u1eef li\u1ec7u, l\u00e0m vi\u1ec7c x\u00e1c \u0111\u1ecbnh v\u00e0 s\u1eeda l\u1ed7i ph\u1ee9c t\u1ea1p h\u01a1n.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>H\u1ed7 tr\u1ee3 ghi log v\u00e0 x\u1eed l\u00fd l\u1ed7i<\/b><span style=\"font-weight: 400;\">: T\u1ef1 \u0111\u1ed9ng ghi l\u1ea1i c\u00e1c thay \u0111\u1ed5i ho\u1eb7c l\u1ed7i ph\u00e1t sinh.<\/span><\/td>\n<td><b>R\u1ee7i ro t\u1ea1o v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n<\/b><span style=\"font-weight: 400;\">: Trigger c\u00f3 th\u1ec3 t\u1ea1o ra v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c c\u1ea5u h\u00ecnh \u0111\u00fang, d\u1eabn \u0111\u1ebfn vi\u1ec7c l\u1eb7p l\u1ea1i c\u00e1c t\u00e1c v\u1ee5 kh\u00f4ng mong mu\u1ed1n.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Gi\u1ea3m t\u1ea3i c\u00f4ng vi\u1ec7c \u1edf ph\u00eda client<\/b><span style=\"font-weight: 400;\">: Nhi\u1ec1u thao t\u00e1c \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n tr\u1ef1c ti\u1ebfp trong c\u01a1 s\u1edf d\u1eef li\u1ec7u thay v\u00ec \u1ee9ng d\u1ee5ng.<\/span><\/td>\n<td><b>H\u1ea1n ch\u1ebf trong qu\u1ea3n l\u00fd<\/b><span style=\"font-weight: 400;\">: Trigger ch\u1ec9 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c t\u1ea1o trong c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7n t\u1ea1i v\u00e0 c\u00f3 th\u1ec3 tham chi\u1ebfu \u0111\u1ebfn c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng ngo\u00e0i c\u01a1 s\u1edf d\u1eef li\u1ec7u, g\u00e2y kh\u00f3 kh\u0103n trong qu\u1ea3n l\u00fd.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">D\u1ec5 d\u00e0ng ch\u1ec9nh s\u1eeda, th\u00eam m\u1edbi ho\u1eb7c x\u00f3a trigger.<\/span><\/td>\n<td><b>Kh\u00f3 ki\u1ec3m so\u00e1t<\/b><span style=\"font-weight: 400;\">: Trigger th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 m\u00e0 ng\u01b0\u1eddi d\u00f9ng kh\u00f4ng th\u1ea5y \u0111\u01b0\u1ee3c, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn vi\u1ec7c kh\u00f3 theo d\u00f5i khi x\u1ea3y ra s\u1ef1 c\u1ed1.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_ve_trigger_trong_SQL\"><\/span><b>C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 trigger trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>\u0110i\u1ec3m kh\u00e1c bi\u1ec7t gi\u1eefa trigger v\u00e0 transaction l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trigger l\u00e0 m\u1ed9t c\u01a1 ch\u1ebf t\u1ef1 \u0111\u1ed9ng th\u1ef1c thi khi c\u00f3 s\u1ef1 thay \u0111\u1ed5i trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, nh\u01b0ng kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi th\u1ee7 c\u00f4ng v\u00e0 kh\u00f4ng nh\u1eadn tham s\u1ed1. Trong khi \u0111\u00f3, Transaction l\u00e0 m\u1ed9t kh\u1ed1i c\u00e1c c\u00e2u l\u1ec7nh SQL c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c \u0111i\u1ec1u khi\u1ec3n th\u1ee7 c\u00f4ng, cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng th\u1ef1c hi\u1ec7n commit ho\u1eb7c rollback \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u.<\/span><\/p>\n<h3><b>C\u00f3 th\u1ec3 t\u1ea1o nhi\u1ec1u Trigger cho c\u00f9ng m\u1ed9t s\u1ef1 ki\u1ec7n kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3, h\u1ea7u h\u1ebft c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u cho ph\u00e9p t\u1ea1o nhi\u1ec1u trigger cho c\u00f9ng m\u1ed9t s\u1ef1 ki\u1ec7n. Tuy nhi\u00ean, vi\u1ec7c n\u00e0y c\u00f3 th\u1ec3 l\u00e0m ph\u1ee9c t\u1ea1p qu\u1ea3n l\u00fd v\u00e0 gi\u1ea3m hi\u1ec7u su\u1ea5t, n\u00ean c\u1ea7n s\u1eed d\u1ee5ng c\u1ea9n th\u1eadn.<\/span><\/p>\n<h3><b>Trigger c\u00f3 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn hi\u1ec7u su\u1ea5t c\u1ee7a c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00f3, s\u1eed d\u1ee5ng nhi\u1ec1u triggers ph\u1ee9c t\u1ea1p c\u00f3 th\u1ec3 l\u00e0m ch\u1eadm hi\u1ec7u su\u1ea5t, \u0111\u1eb7c bi\u1ec7t khi \u00e1p d\u1ee5ng tr\u00ean c\u00e1c b\u1ea3ng l\u1edbn. \u0110\u1ec3 tr\u00e1nh t\u00e1c \u0111\u1ed9ng ti\u00eau c\u1ef1c, n\u00ean s\u1eed d\u1ee5ng trigger m\u1ed9t c\u00e1ch c\u1ea9n tr\u1ecdng v\u00e0 t\u1ed1i \u01b0u h\u00f3a c\u00e1c thao t\u00e1c b\u00ean trong trigger v\u1edbi m\u1ed9t s\u1ed1 ph\u01b0\u01a1ng ph\u00e1p nh\u01b0:<\/span><\/p>\n<ul>\n<li><b>Gi\u1eef logic trigger \u0111\u01a1n gi\u1ea3n v\u00e0 ng\u1eafn g\u1ecdn: <\/b><span style=\"font-weight: 400;\">Ch\u1ec9 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 th\u1eadt s\u1ef1 c\u1ea7n thi\u1ebft b\u00ean trong trigger v\u00e0 tr\u00e1nh vi\u1ebft c\u00e1c \u0111o\u1ea1n m\u00e3 ph\u1ee9c t\u1ea1p ho\u1eb7c c\u00e1c truy v\u1ea5n n\u1eb7ng l\u00e0m t\u0103ng th\u1eddi gian th\u1ef1c thi.<\/span><\/li>\n<li><b>Ch\u1ec9 k\u00edch ho\u1ea1t trigger tr\u00ean c\u00e1c s\u1ef1 ki\u1ec7n c\u1ee5 th\u1ec3 v\u00e0 \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ea7n thi\u1ebft: <\/b><span style=\"font-weight: 400;\">Gi\u1edbi h\u1ea1n trigger \u0111\u1ec3 ch\u1ec9 k\u00edch ho\u1ea1t khi c\u1ea7n thi\u1ebft v\u00e0 ch\u1ec9 \u00e1p d\u1ee5ng trigger tr\u00ean c\u00e1c b\u1ea3ng th\u1ef1c s\u1ef1 c\u1ea7n theo d\u00f5i thay \u0111\u1ed5i.<\/span><\/li>\n<li><b>V\u00f4 hi\u1ec7u h\u00f3a nh\u1eefng trigger d\u01b0 th\u1eeba, kh\u00f4ng c\u00f2n \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng.<\/b><\/li>\n<li><b>S\u1eed d\u1ee5ng b\u1ea3ng t\u1ea1m ho\u1eb7c x\u1eed l\u00fd ngo\u00e0i trigger khi c\u1ea7n: <\/b><span style=\"font-weight: 400;\">Thay v\u00ec x\u1eed l\u00fd to\u00e0n b\u1ed9 logic b\u00ean trong trigger, c\u00f3 th\u1ec3 ghi d\u1eef li\u1ec7u v\u00e0o m\u1ed9t b\u1ea3ng t\u1ea1m v\u00e0 x\u1eed l\u00fd th\u00f4ng qua t\u00e1c v\u1ee5 b\u00ean ngo\u00e0i \u0111\u1ec3 gi\u1ea3m t\u1ea3i c\u00f4ng vi\u1ec7c tr\u1ef1c ti\u1ebfp tr\u00ean trigger.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_Trigger_trong_SQL\"><\/span><b>T\u1ed5ng k\u1ebft Trigger trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Trigger trong SQL l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 h\u1eefu \u00edch \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. Tuy nhi\u00ean, c\u1ea7n l\u01b0u \u00fd v\u1ec1 c\u00e1c h\u1ea1n ch\u1ebf v\u00e0 t\u00e1c \u0111\u1ed9ng \u0111\u1ebfn hi\u1ec7u su\u1ea5t khi s\u1eed d\u1ee5ng. Khi s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch, trigger s\u1ebd gi\u00fap b\u1ea1n qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 h\u01a1n v\u00e0 gi\u1ea3m thi\u1ec3u l\u1ed7i trong qu\u00e1 tr\u00ecnh thao t\u00e1c v\u1edbi d\u1eef li\u1ec7u.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00f9ng t\u00ecm hi\u1ec3u th\u00eam v\u1ec1 c\u00e1c ch\u1ee7 \u0111\u1ec1 kh\u00e1c thu\u1ed9c SQL:<\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/\" target=\"_blank\" rel=\"noopener\">C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/function-trong-sql\/\" target=\"_blank\" rel=\"noopener\">T\u1ed5ng h\u1ee3p 90+ function trong SQL c\u1ea7n bi\u1ebft<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/\" target=\"_blank\" rel=\"noopener\">Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/sql-injection-la-gi\/\" target=\"_blank\" rel=\"noopener\">SQL Injection: C\u00e1ch th\u1ee9c ho\u1ea1t \u0111\u1ed9ng v\u00e0 ph\u00f2ng ch\u1ed1ng SQLi<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/\" target=\"_blank\" rel=\"noopener\">SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/join-trong-sql\/\" target=\"_blank\" rel=\"noopener\">JOIN trong SQL: C\u00fa ph\u00e1p v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c ph\u00e9p JOIN<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Trigger trong SQL l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd trong qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u. T\u00ecm hi\u1ec3u v\u1ec1 c\u00e1c thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi trigger SQL v\u1edbi t\u1eebng lo\u1ea1i trigger kh\u00e1c nhau. \u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u r\u00f5 [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":82228,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-82075","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n - ITviec Blog<\/title>\n<meta name=\"description\" content=\"Kh\u00e1m ph\u00e1 c\u00e1c thao t\u00e1c s\u1eed d\u1ee5ng trigger trong SQL hi\u1ec7u qu\u1ea3 c\u00f9ng c\u00e1c lo\u1ea1i trigger v\u00e0 m\u1eb9o gi\u00fap duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n v\u00e0 hi\u1ec7u su\u1ea5t c\u01a1 s\u1edf d\u1eef li\u1ec7u.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n\" \/>\n<meta property=\"og:description\" content=\"Trigger trong SQL l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd trong qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u. T\u00ecm hi\u1ec3u v\u1ec1 c\u00e1c thao\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/trigger-trong-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-28T08:38:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Trigger-trong-SQL-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"M\u1ef9 Duy\u00ean\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"M\u1ef9 Duy\u00ean\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n - ITviec Blog","description":"Kh\u00e1m ph\u00e1 c\u00e1c thao t\u00e1c s\u1eed d\u1ee5ng trigger trong SQL hi\u1ec7u qu\u1ea3 c\u00f9ng c\u00e1c lo\u1ea1i trigger v\u00e0 m\u1eb9o gi\u00fap duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n v\u00e0 hi\u1ec7u su\u1ea5t c\u01a1 s\u1edf d\u1eef li\u1ec7u.","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":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/","og_locale":"vi_VN","og_type":"article","og_title":"Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n","og_description":"Trigger trong SQL l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd trong qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 v\u00e0 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u. T\u00ecm hi\u1ec3u v\u1ec1 c\u00e1c thao","og_url":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-11-28T08:38:12+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Trigger-trong-SQL-vippro.jpg","type":"image\/jpeg"}],"author":"M\u1ef9 Duy\u00ean","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"M\u1ef9 Duy\u00ean","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"15 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n","datePublished":"2024-11-28T08:38:12+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/"},"wordCount":3950,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Trigger-trong-SQL-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/","url":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/","name":"Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Trigger-trong-SQL-vippro.jpg","datePublished":"2024-11-28T08:38:12+00:00","description":"Kh\u00e1m ph\u00e1 c\u00e1c thao t\u00e1c s\u1eed d\u1ee5ng trigger trong SQL hi\u1ec7u qu\u1ea3 c\u00f9ng c\u00e1c lo\u1ea1i trigger v\u00e0 m\u1eb9o gi\u00fap duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n v\u00e0 hi\u1ec7u su\u1ea5t c\u01a1 s\u1edf d\u1eef li\u1ec7u.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/trigger-trong-sql\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Trigger-trong-SQL-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Trigger-trong-SQL-vippro.jpg","width":1500,"height":790,"caption":"Trigger trong SQL - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/trigger-trong-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"Trigger trong SQL: Chi ti\u1ebft c\u00e1c thao t\u00e1c trigger SQL c\u01a1 b\u1ea3n"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7","name":"M\u1ef9 Duy\u00ean","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","caption":"M\u1ef9 Duy\u00ean"},"url":"https:\/\/itviec.com\/blog\/author\/my-duyen\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/82075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/209"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=82075"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/82075\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/82228"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=82075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=82075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=82075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}