{"id":84297,"date":"2025-01-31T20:13:00","date_gmt":"2025-01-31T13:13:00","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=84297"},"modified":"2025-02-01T22:48:57","modified_gmt":"2025-02-01T15:48:57","slug":"cau-lenh-update-trong-sql","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/","title":{"rendered":"C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng"},"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\/cau-lenh-update-trong-sql\/#Tong_quan_ve_cau_lenh_UPDATE_trong_SQL\" >T\u1ed5ng quan v\u1ec1 c\u00e2u l\u1ec7nh UPDATE trong SQL<\/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\/cau-lenh-update-trong-sql\/#Cac_thanh_phan_cua_cau_lenh_UPDATE_trong_SQL\" >C\u00e1c th\u00e0nh ph\u1ea7n c\u1ee7a c\u00e2u l\u1ec7nh UPDATE 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\/cau-lenh-update-trong-sql\/#Cau_lenh_UPDATE_trong_SQL_hoat_dong_nhu_the_nao\" >C\u00e2u l\u1ec7nh UPDATE trong SQL ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/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\/cau-lenh-update-trong-sql\/#Cac_truong_hop_su_dung_cau_lenh_UPDATE_trong_SQL\" >C\u00e1c tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE 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\/cau-lenh-update-trong-sql\/#Mot_so_luu_y_khi_su_dung_cau_lenh_UPDATE_trong_SQL\" >M\u1ed9t s\u1ed1 l\u01b0u \u00fd khi s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE trong SQL<\/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\/cau-lenh-update-trong-sql\/#Cau_hoi_thuong_gap_ve_cau_lenh_UPDATE_trong_SQL\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 c\u00e2u l\u1ec7nh UPDATE 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\/cau-lenh-update-trong-sql\/#Tong_ket_cau_lenh_UPDATE_trong_SQL\" >T\u1ed5ng k\u1ebft c\u00e2u l\u1ec7nh UPDATE trong SQL<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>C\u00e2u l\u1ec7nh UPDATE trong SQL l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd v\u00e0 c\u1ea7n thi\u1ebft khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap b\u1ea1n c\u1eadp nh\u1eadt d\u1eef li\u1ec7u nhanh ch\u00f3ng v\u00e0 ch\u00ednh x\u00e1c. T\u1eeb vi\u1ec7c thay \u0111\u1ed5i th\u00f4ng tin c\u00e1 nh\u00e2n, c\u1eadp nh\u1eadt gi\u00e1 s\u1ea3n ph\u1ea9m, \u0111\u1ebfn x\u1eed l\u00fd tr\u1ea1ng th\u00e1i \u0111\u01a1n h\u00e0ng, l\u1ec7nh UPDATE mang l\u1ea1i s\u1ef1 linh ho\u1ea1t v\u00e0 hi\u1ec7u qu\u1ea3 trong vi\u1ec7c qu\u1ea3n l\u00fd d\u1eef li\u1ec7u.<\/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;\">C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a c\u00e2u l\u1ec7nh UPDATE trong SQL<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p v\u00e0 v\u00ed d\u1ee5 c\u1ee5 th\u1ec3<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Tong_quan_ve_cau_lenh_UPDATE_trong_SQL\"><\/span><b>T\u1ed5ng quan v\u1ec1 c\u00e2u l\u1ec7nh UPDATE trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh UPDATE trong SQL l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd \u0111\u1ec3 duy tr\u00ec v\u00e0 qu\u1ea3n l\u00fd t\u00ednh ch\u00ednh x\u00e1c, nh\u1ea5t qu\u00e1n c\u1ee7a d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. V\u1edbi l\u1ec7nh n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 c\u1eadp nh\u1eadt m\u1ed9t ho\u1eb7c nhi\u1ec1u b\u1ea3n ghi trong b\u1ea3ng m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng, b\u1eb1ng c\u00e1ch ch\u1ec9 \u0111\u1ecbnh c\u00e1c \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3 (th\u00f4ng qua m\u1ec7nh \u0111\u1ec1 WHERE).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh UPDATE th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c t\u00ecnh hu\u1ed1ng c\u1ea7n ch\u1ec9nh s\u1eeda d\u1eef li\u1ec7u c\u1ee7a m\u1ed9t b\u1ea3n ghi trong b\u1ea3ng, v\u00ed d\u1ee5 nh\u01b0 c\u1eadp nh\u1eadt tr\u1ea1ng th\u00e1i \u0111\u01a1n h\u00e0ng, s\u1eeda \u0111\u1ed5i th\u00f4ng tin c\u00e1 nh\u00e2n ho\u1eb7c \u0111i\u1ec1u ch\u1ec9nh s\u1ed1 l\u01b0\u1ee3ng t\u1ed3n kho.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cac_thanh_phan_cua_cau_lenh_UPDATE_trong_SQL\"><\/span><b>C\u00e1c th\u00e0nh ph\u1ea7n c\u1ee7a c\u00e2u l\u1ec7nh UPDATE trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE table_name<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET column1 = value1, column2 = value2, ...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE condition;<\/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;\">UPDATE table_name<\/span><span style=\"font-weight: 400;\">: X\u00e1c \u0111\u1ecbnh t\u00ean b\u1ea3ng m\u00e0 b\u1ea1n mu\u1ed1n c\u1eadp nh\u1eadt d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">SET column1 = value1, column2 = value2, &#8230;<\/span><span style=\"font-weight: 400;\">: Ch\u1ec9 \u0111\u1ecbnh c\u1ed9t c\u1ea7n c\u1eadp nh\u1eadt v\u00e0 gi\u00e1 tr\u1ecb m\u1edbi t\u01b0\u01a1ng \u1ee9ng. B\u1ea1n c\u00f3 th\u1ec3 c\u1eadp nh\u1eadt m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u1ed9t c\u00f9ng l\u00fac.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> condition: \u0110i\u1ec1u ki\u1ec7n x\u00e1c \u0111\u1ecbnh b\u1ea3n ghi n\u00e0o s\u1ebd \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt. N\u1ebfu kh\u00f4ng c\u00f3 m\u1ec7nh \u0111\u1ec1 WHERE, t\u1ea5t c\u1ea3 c\u00e1c b\u1ea3n ghi trong b\u1ea3ng s\u1ebd b\u1ecb c\u1eadp nh\u1eadt (c\u1ea9n th\u1eadn khi s\u1eed d\u1ee5ng).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb (value): C\u00f3 th\u1ec3 l\u00e0 h\u1eb1ng s\u1ed1, gi\u00e1 tr\u1ecb t\u1eeb m\u1ed9t c\u1ed9t kh\u00e1c, ho\u1eb7c k\u1ebft qu\u1ea3 c\u1ee7a bi\u1ec3u th\u1ee9c hay truy v\u1ea5n con.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cau_lenh_UPDATE_trong_SQL_hoat_dong_nhu_the_nao\"><\/span><b>C\u00e2u l\u1ec7nh UPDATE trong SQL ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Khi th\u1ef1c thi m\u1ed9t c\u00e2u l\u1ec7nh UPDATE, SQL Server ho\u1eb7c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00e1c th\u1ef1c hi\u1ec7n c\u00e1c b\u01b0\u1edbc sau:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u00e1c \u0111\u1ecbnh b\u1ea3ng m\u1ee5c ti\u00eau<\/b><span style=\"font-weight: 400;\">: H\u1ec7 th\u1ed1ng x\u00e1c \u0111\u1ecbnh b\u1ea3ng \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh trong l\u1ec7nh <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u00c1p d\u1ee5ng \u0111i\u1ec1u ki\u1ec7n <\/b><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">: H\u1ec7 th\u1ed1ng l\u1ecdc c\u00e1c b\u1ea3n ghi th\u1ecfa m\u00e3n \u0111i\u1ec1u ki\u1ec7n \u0111\u01b0\u1ee3c cung c\u1ea5p trong m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">. N\u1ebfu kh\u00f4ng c\u00f3 m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, t\u1ea5t c\u1ea3 c\u00e1c b\u1ea3n ghi trong b\u1ea3ng s\u1ebd \u0111\u01b0\u1ee3c ch\u1ecdn \u0111\u1ec3 c\u1eadp nh\u1eadt.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u1eadp nh\u1eadt c\u1ed9t \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh<\/b><span style=\"font-weight: 400;\">: D\u1eef li\u1ec7u trong c\u00e1c c\u1ed9t ch\u1ec9 \u0111\u1ecbnh \u1edf m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">SET<\/span><span style=\"font-weight: 400;\"> s\u1ebd \u0111\u01b0\u1ee3c thay th\u1ebf b\u1eb1ng gi\u00e1 tr\u1ecb m\u1edbi.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ghi d\u1eef li\u1ec7u m\u1edbi<\/b><span style=\"font-weight: 400;\">: H\u1ec7 th\u1ed1ng ghi l\u1ea1i c\u00e1c thay \u0111\u1ed5i v\u00e0o b\u1ea3ng, \u0111\u1ed3ng th\u1eddi duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ki\u1ec3m tra r\u00e0ng bu\u1ed9c v\u00e0 ch\u1ec9 s\u1ed1<\/b><span style=\"font-weight: 400;\">: H\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u \u0111\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c r\u00e0ng bu\u1ed9c (constraints) nh\u01b0 kh\u00f3a ch\u00ednh, kh\u00f3a ngo\u1ea1i v\u00e0 ch\u1ec9 m\u1ee5c \u0111\u01b0\u1ee3c tu\u00e2n th\u1ee7 trong qu\u00e1 tr\u00ecnh c\u1eadp nh\u1eadt.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u00e1c nh\u1eadn thay \u0111\u1ed5i<\/b><span style=\"font-weight: 400;\">: N\u1ebfu kh\u00f4ng c\u00f3 l\u1ed7i, c\u00e1c thay \u0111\u1ed5i \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng v\u00e0 SQL s\u1ebd tr\u1ea3 v\u1ec1 s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi \u0111\u00e3 \u0111\u01b0\u1ee3c thay \u0111\u1ed5i; n\u1ebfu x\u1ea3y ra l\u1ed7i (v\u00ed d\u1ee5: vi ph\u1ea1m r\u00e0ng bu\u1ed9c), giao d\u1ecbch s\u1ebd b\u1ecb h\u1ee7y.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed ta c\u00f3 b\u1ea3ng employees ch\u1ee9a th\u00f4ng tin nh\u00e2n vi\u00ean nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>employee_id<\/b><\/td>\n<td><b>name<\/b><\/td>\n<td><b>department<\/b><\/td>\n<td><b>salary<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">HR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">50000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Jane Smith<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Finance<\/span><\/td>\n<td><span style=\"font-weight: 400;\">60000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">8<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Alice Johnson<\/span><\/td>\n<td><span style=\"font-weight: 400;\">IT<\/span><\/td>\n<td><span style=\"font-weight: 400;\">55000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">10<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bob Brown<\/span><\/td>\n<td><span style=\"font-weight: 400;\">IT<\/span><\/td>\n<td><span style=\"font-weight: 400;\">48000<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 khi th\u1ef1c thi m\u1ed9t c\u00e2u l\u1ec7nh UPDATE, SQL Server ho\u1eb7c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00e1c s\u1ebd th\u1ef1c hi\u1ec7n c\u00e1c b\u01b0\u1edbc sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE employees<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET salary = 70000<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE employee_id = 10;<\/span><\/pre>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u00e1c \u0111\u1ecbnh b\u1ea3ng m\u1ee5c ti\u00eau<\/b><span style=\"font-weight: 400;\">: Trong v\u00ed d\u1ee5 tr\u00ean, b\u1ea3ng m\u1ee5c ti\u00eau l\u00e0 b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\">, \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh t\u1ea1i d\u00f2ng <\/span><span style=\"font-weight: 400;\">UPDATE employees<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u00c1p d\u1ee5ng \u0111i\u1ec1u ki\u1ec7n WHERE<\/b><span style=\"font-weight: 400;\">: Trong v\u00ed d\u1ee5 tr\u00ean, ch\u1ec9 b\u1ea3n ghi c\u1ee7a nh\u00e2n vi\u00ean c\u00f3 <\/span><span style=\"font-weight: 400;\">employee_id = 10<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c ch\u1ecdn \u0111\u1ec3 c\u1eadp nh\u1eadt.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u1eadp nh\u1eadt c\u1ed9t \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh<\/b><span style=\"font-weight: 400;\">: c\u1ed9t salary c\u1ee7a nh\u00e2n vi\u00ean c\u00f3 <\/span><span style=\"font-weight: 400;\">employee_id = 10<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt th\u00e0nh gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">70000<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ghi d\u1eef li\u1ec7u m\u1edbi<\/b><span style=\"font-weight: 400;\">: B\u1ea3n ghi c\u1ee7a nh\u00e2n vi\u00ean c\u00f3 <\/span><span style=\"font-weight: 400;\">employee_id = 10<\/span><span style=\"font-weight: 400;\"> trong b\u1ea3ng employees \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt v\u1edbi m\u1ee9c l\u01b0\u01a1ng m\u1edbi l\u00e0 <\/span><span style=\"font-weight: 400;\">70000<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ki\u1ec3m tra r\u00e0ng bu\u1ed9c v\u00e0 ch\u1ec9 s\u1ed1<\/b><span style=\"font-weight: 400;\">: N\u1ebfu gi\u00e1 tr\u1ecb m\u1edbi g\u00e2y xung \u0111\u1ed9t v\u1edbi r\u00e0ng bu\u1ed9c kh\u00f3a ch\u00ednh ho\u1eb7c kh\u00f3a ngo\u1ea1i, c\u00e2u l\u1ec7nh s\u1ebd b\u1ecb h\u1ee7y v\u00e0 h\u1ec7 th\u1ed1ng tr\u1ea3 v\u1ec1 th\u00f4ng b\u00e1o l\u1ed7i.<\/span><\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Cac_truong_hop_su_dung_cau_lenh_UPDATE_trong_SQL\"><\/span><b>C\u00e1c tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed ta c\u00f3 m\u1ed9t h\u1ec7 th\u1ed1ng qu\u1ea3n l\u00fd b\u00e1n h\u00e0ng g\u1ed3m c\u00e1c b\u1ea3ng:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Products: Qu\u1ea3n l\u00fd th\u00f4ng tin s\u1ea3n ph\u1ea9m (product_id, product_name, price, stock, category).<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>product_id<\/b><\/td>\n<td><b>product_name<\/b><\/td>\n<td><b>price<\/b><\/td>\n<td><b>stock<\/b><\/td>\n<td><b>category<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">101<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product A<\/span><\/td>\n<td><span style=\"font-weight: 400;\">100000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">50<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Electronics<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">102<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product B<\/span><\/td>\n<td><span style=\"font-weight: 400;\">120000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">30<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Electronics<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">103<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product C<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">20<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Household<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Orders: Qu\u1ea3n l\u00fd \u0111\u01a1n h\u00e0ng (order_id, customer_id, status, total_price).<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>order_id<\/b><\/td>\n<td><b>customer_id<\/b><\/td>\n<td><b>status<\/b><\/td>\n<td><b>total_price<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">201<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pending<\/span><\/td>\n<td><span style=\"font-weight: 400;\">8000000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">202<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pending<\/span><\/td>\n<td><span style=\"font-weight: 400;\">15000000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">203<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pending<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5000000<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Payments: Th\u00f4ng tin v\u1ec1 c\u00e1c giao d\u1ecbch thanh to\u00e1n (payment_id, order_id, payment_status).<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>payment_id<\/b><\/td>\n<td><b>order_id<\/b><\/td>\n<td><b>payment_status<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Paid<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pending<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Failed<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u1eadp nh\u1eadt m\u1ed9t c\u1ed9t (Update Single Column)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 c\u1eadp nh\u1eadt gi\u00e1 c\u1ee7a s\u1ea3n ph\u1ea9m c\u00f3 <\/span><span style=\"font-weight: 400;\">product_id = 101<\/span><span style=\"font-weight: 400;\"> th\u00e0nh 150,000 ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Products<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET price = 150000<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE product_id = 101;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean, b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Products<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd tr\u1edf th\u00e0nh (Gi\u00e1 c\u1ee7a Product A thay \u0111\u1ed5i t\u1eeb 100000 th\u00e0nh 150000):<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>product_id<\/b><\/td>\n<td><b>product_name<\/b><\/td>\n<td><b>price<\/b><\/td>\n<td><b>stock<\/b><\/td>\n<td><b>category<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">101<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product A<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">50<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Electronics<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">102<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product B<\/span><\/td>\n<td><span style=\"font-weight: 400;\">120000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">30<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Electronics<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">103<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product C<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">20<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Household<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u1eadp nh\u1eadt nhi\u1ec1u c\u1ed9t (Updating Multiple Columns)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 c\u1eadp nh\u1eadt gi\u00e1 s\u1ea3n ph\u1ea9m <\/span><span style=\"font-weight: 400;\">product_id = 102<\/span><span style=\"font-weight: 400;\"> l\u00ean 200,000 v\u00e0 gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng trong kho \u0111i 5 \u0111\u01a1n v\u1ecb, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Products<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET price = 200000, stock = stock - 5<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE product_id = 102;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean, b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Products<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd tr\u1edf th\u00e0nh (Gi\u00e1 c\u1ee7a Product B thay \u0111\u1ed5i t\u1eeb 120000 th\u00e0nh 200000 v\u00e0 s\u1ed1 l\u01b0\u1ee3ng trong kho thay \u0111\u1ed5i t\u1eeb 30 th\u00e0nh 25):<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>product_id<\/b><\/td>\n<td><b>product_name<\/b><\/td>\n<td><b>price<\/b><\/td>\n<td><b>stock<\/b><\/td>\n<td><b>category<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">101<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product A<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">50<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Electronics<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">102<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product B<\/span><\/td>\n<td><span style=\"font-weight: 400;\">200000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">25<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Electronics<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">103<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product C<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">20<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Household<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>B\u1ecf qua m\u1ec7nh \u0111\u1ec1 WHERE<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Tr\u01b0\u1eddng h\u1ee3p mu\u1ed1n \u00e1p d\u1ee5ng thay \u0111\u1ed5i cho t\u1ea5t c\u1ea3 c\u00e1c b\u1ea3n ghi trong b\u1ea3ng l\u00e0 thay \u0111\u1ed5i t\u1ea5t c\u1ea3 c\u00e1c s\u1ea3n ph\u1ea9m trong b\u1ea3ng Products sang danh m\u1ee5c &#8220;Outdated&#8221; ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Products<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET category = 'Outdated';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean, b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Products<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd tr\u1edf th\u00e0nh (To\u00e0n b\u1ed9 c\u00e1c gi\u00e1 tr\u1ecb trong c\u1ed9t category thay \u0111\u1ed5i th\u00e0nh &#8220;Outdated&#8221;):<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>product_id<\/b><\/td>\n<td><b>product_name<\/b><\/td>\n<td><b>price<\/b><\/td>\n<td><b>stock<\/b><\/td>\n<td><b>category<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">101<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product A<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">50<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Outdated<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">102<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product B<\/span><\/td>\n<td><span style=\"font-weight: 400;\">200000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">25<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Outdated<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">103<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product C<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">20<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Outdated<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u1eadp nh\u1eadt d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb t\u1eeb m\u1ed9t b\u1ea3ng kh\u00e1c<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi c\u1ea7n \u0111\u1ed3ng b\u1ed9 d\u1eef li\u1ec7u gi\u1eefa c\u00e1c b\u1ea3ng, ch\u1eb3ng h\u1ea1n c\u1eadp nh\u1eadt tr\u1ea1ng th\u00e1i \u0111\u01a1n h\u00e0ng d\u1ef1a tr\u00ean b\u1ea3ng kh\u00e1c l\u00e0 c\u1eadp nh\u1eadt tr\u1ea1ng th\u00e1i \u0111\u01a1n h\u00e0ng th\u00e0nh &#8220;<\/span><i><span style=\"font-weight: 400;\">Completed<\/span><\/i><span style=\"font-weight: 400;\">&#8221; cho nh\u1eefng \u0111\u01a1n h\u00e0ng \u0111\u00e3 \u0111\u01b0\u1ee3c thanh to\u00e1n <\/span><span style=\"font-weight: 400;\">v\u1edbi b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Payments<\/span><\/i><span style=\"font-weight: 400;\"> ch\u1ee9a th\u00f4ng tin v\u1ec1 c\u00e1c giao d\u1ecbch thanh to\u00e1n v\u00e0 c\u00e1c tr\u1ea1ng th\u00e1i c\u1ee7a giao d\u1ecbch thanh to\u00e1n nh\u01b0 <\/span><i><span style=\"font-weight: 400;\">&#8216;Paid&#8217;<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">&#8216;Pending&#8217;<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">&#8216;Failed&#8217;<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Orders<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET status = 'Completed'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE order_id IN (<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0SELECT order_id FROM Payments WHERE payment_status = 'Paid'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean, b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd tr\u1edf th\u00e0nh (Tr\u1ea1ng th\u00e1i c\u1ee7a \u0111\u01a1n h\u00e0ng c\u00f3 <\/span><span style=\"font-weight: 400;\">order_id = 1<\/span><span style=\"font-weight: 400;\"> thay \u0111\u1ed5i t\u1eeb &#8220;Pending&#8221; th\u00e0nh &#8220;Completed&#8221;):<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>order_id<\/b><\/td>\n<td><b>customer_id<\/b><\/td>\n<td><b>status<\/b><\/td>\n<td><b>total_price<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">201<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Completed<\/span><\/td>\n<td><span style=\"font-weight: 400;\">8000000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">202<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pending<\/span><\/td>\n<td><span style=\"font-weight: 400;\">15000000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">203<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pending<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5000000<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u1eadp nh\u1eadt d\u1ef1a tr\u00ean bi\u1ec3u th\u1ee9c<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ta c\u00f3 th\u1ec3 d\u00f9ng UPDATE thay \u0111\u1ed5i gi\u00e1 tr\u1ecb d\u1ef1a tr\u00ean t\u00ednh to\u00e1n ho\u1eb7c \u0111i\u1ec1u ki\u1ec7n nh\u01b0 \u0111\u1ec3 t\u0103ng gi\u00e1 c\u1ee7a t\u1ea5t c\u1ea3 c\u00e1c s\u1ea3n ph\u1ea9m thu\u1ed9c danh m\u1ee5c &#8220;Electronics&#8221; l\u00ean 10%, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Products<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET price = price * 1.1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE category = 'Electronics';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean, b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Products<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd tr\u1edf th\u00e0nh (Gi\u00e1 c\u1ee7a Product A v\u00e0 Product B thay \u0111\u1ed5i l\u1ea7n l\u01b0\u1ee3t t\u1eeb 150000 th\u00e0nh 165000 v\u00e0 t\u1eeb 200000 th\u00e0nh 220000):<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>product_id<\/b><\/td>\n<td><b>product_name<\/b><\/td>\n<td><b>price<\/b><\/td>\n<td><b>stock<\/b><\/td>\n<td><b>category<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">101<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product A<\/span><\/td>\n<td><span style=\"font-weight: 400;\">165000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">50<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Outdated<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">102<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product B<\/span><\/td>\n<td><span style=\"font-weight: 400;\">220000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">25<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Outdated<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">103<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Product C<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150000<\/span><\/td>\n<td><span style=\"font-weight: 400;\">20<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Outdated<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u1eadp nh\u1eadt d\u1eef li\u1ec7u s\u1eed d\u1ee5ng CASE<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi c\u1ea7n c\u1eadp nh\u1eadt d\u1eef li\u1ec7u theo nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n kh\u00e1c nhau, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">CASE<\/span><span style=\"font-weight: 400;\">. V\u00ed d\u1ee5 \u0111\u1ec3 thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i \u0111\u01a1n h\u00e0ng d\u1ef1a tr\u00ean t\u1ed5ng gi\u00e1 tr\u1ecb (<\/span><i><span style=\"font-weight: 400;\">total_price<\/span><\/i><span style=\"font-weight: 400;\">), n\u1ebfu gi\u00e1 tr\u1ecb tr\u00ean 10 tri\u1ec7u th\u00ec tr\u1ea1ng th\u00e1i l\u00e0 &#8220;High Value&#8221;, d\u01b0\u1edbi 10 tri\u1ec7u l\u00e0 &#8220;Regular&#8221;, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Orders<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET status = CASE<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WHEN total_price &gt; 10000000 THEN 'High Value'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ELSE 'Regular'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0END;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean, b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd thay \u0111\u1ed5i nh\u01b0 sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u01a1n h\u00e0ng <\/span><span style=\"font-weight: 400;\">order_id = 1<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">order_id = 3<\/span><span style=\"font-weight: 400;\"> th\u00e0nh &#8220;Regular&#8221;.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u01a1n h\u00e0ng <\/span><span style=\"font-weight: 400;\">order_id = 2<\/span><span style=\"font-weight: 400;\"> th\u00e0nh &#8220;High Value&#8221;.<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><b>order_id<\/b><\/td>\n<td><b>customer_id<\/b><\/td>\n<td><b>status<\/b><\/td>\n<td><b>total_price<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">201<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Regular<\/span><\/td>\n<td><span style=\"font-weight: 400;\">8000000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">202<\/span><\/td>\n<td><span style=\"font-weight: 400;\">High Value<\/span><\/td>\n<td><span style=\"font-weight: 400;\">15000000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">203<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Regular<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5000000<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Mot_so_luu_y_khi_su_dung_cau_lenh_UPDATE_trong_SQL\"><\/span><b>M\u1ed9t s\u1ed1 l\u01b0u \u00fd khi s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ki\u1ec3m tra ph\u1ea1m vi \u1ea3nh h\u01b0\u1edfng b\u1eb1ng l\u1ec7nh SELECT tr\u01b0\u1edbc khi UPDATE<\/b><span style=\"font-weight: 400;\">: Tr\u01b0\u1edbc khi th\u1ef1c thi l\u1ec7nh <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">, b\u1ea1n n\u00ean s\u1eed d\u1ee5ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> v\u1edbi \u0111i\u1ec1u ki\u1ec7n <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> t\u01b0\u01a1ng t\u1ef1 \u0111\u1ec3 ki\u1ec3m tra c\u00e1c b\u1ea3n ghi s\u1ebd b\u1ecb \u1ea3nh h\u01b0\u1edfng. \u0110i\u1ec1u n\u00e0y gi\u00fap \u0111\u1ea3m b\u1ea3o r\u1eb1ng ch\u1ec9 nh\u1eefng d\u1eef li\u1ec7u c\u1ea7n thay \u0111\u1ed5i m\u1edbi \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Lu\u00f4n s\u1eed d\u1ee5ng m\u1ec7nh \u0111\u1ec1 WHERE khi c\u1ea7n gi\u1edbi h\u1ea1n b\u1ea3n ghi<\/b><span style=\"font-weight: 400;\">: N\u1ebfu b\u1ea1n b\u1ecf qua m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, to\u00e0n b\u1ed9 c\u00e1c b\u1ea3n ghi trong b\u1ea3ng s\u1ebd b\u1ecb c\u1eadp nh\u1eadt, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 g\u00e2y ra h\u1eadu qu\u1ea3 kh\u00f4ng mong mu\u1ed1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Sao l\u01b0u d\u1eef li\u1ec7u tr\u01b0\u1edbc khi th\u1ef1c thi l\u1ec7nh UPDATE quan tr\u1ecdng<\/b><span style=\"font-weight: 400;\">: \u0110\u1ed1i v\u1edbi c\u00e1c h\u1ec7 th\u1ed1ng l\u1edbn, d\u1eef li\u1ec7u r\u1ea5t quan tr\u1ecdng v\u00e0 kh\u00f3 kh\u00f4i ph\u1ee5c n\u1ebfu x\u1ea3y ra l\u1ed7i. Tr\u01b0\u1edbc khi th\u1ef1c hi\u1ec7n l\u1ec7nh <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\"> c\u00f3 \u1ea3nh h\u01b0\u1edfng r\u1ed9ng, h\u00e3y sao l\u01b0u c\u01a1 s\u1edf d\u1eef li\u1ec7u ho\u1eb7c b\u1ea3ng d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u1ea9n th\u1eadn v\u1edbi c\u00e1c c\u1ed9t c\u00f3 r\u00e0ng bu\u1ed9c (Constraints)<\/b><span style=\"font-weight: 400;\">: N\u1ebfu b\u1ea3ng c\u00f3 c\u00e1c r\u00e0ng bu\u1ed9c nh\u01b0 <\/span><span style=\"font-weight: 400;\">NOT<\/span> <span style=\"font-weight: 400;\">NULL<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UNIQUE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">FOREIGN<\/span> <span style=\"font-weight: 400;\">KEY<\/span><span style=\"font-weight: 400;\">, th\u00ec vi\u1ec7c c\u1eadp nh\u1eadt d\u1eef li\u1ec7u kh\u00f4ng h\u1ee3p l\u1ec7 c\u00f3 th\u1ec3 g\u00e2y l\u1ed7i. B\u1ea1n n\u00ean ki\u1ec3m tra k\u1ef9 c\u00e1c r\u00e0ng bu\u1ed9c n\u00e0y tr\u01b0\u1edbc khi th\u1ef1c thi l\u1ec7nh.<\/span><\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_ve_cau_lenh_UPDATE_trong_SQL\"><\/span><b>C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 c\u00e2u l\u1ec7nh UPDATE trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 c\u1eadp nh\u1eadt nhi\u1ec1u b\u1ea3n ghi c\u00f9ng l\u00fac?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ta c\u00f3 th\u1ec3 c\u1eadp nh\u1eadt nhi\u1ec1u b\u1ea3n ghi b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ec9 \u0111\u1ecbnh c\u00e1c b\u1ea3n ghi c\u1ea7n thay \u0111\u1ed5i.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed ta c\u00f3 b\u1ea3ng employees:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>employee_id<\/b><\/td>\n<td><b>name<\/b><\/td>\n<td><b>department<\/b><\/td>\n<td><b>salary<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sales<\/span><\/td>\n<td><span style=\"font-weight: 400;\">50000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Jane Smith<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sales<\/span><\/td>\n<td><span style=\"font-weight: 400;\">60000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Alice Johnson<\/span><\/td>\n<td><span style=\"font-weight: 400;\">IT<\/span><\/td>\n<td><span style=\"font-weight: 400;\">70000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bob Brown<\/span><\/td>\n<td><span style=\"font-weight: 400;\">HR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">55000<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u0103ng 10% l\u01b0\u01a1ng cho t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean thu\u1ed9c ph\u00f2ng ban &#8220;Sales&#8221; ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Employees<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET salary = salary * 1.1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE department = 'Sales';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea3ng employees s\u1ebd tr\u1edf th\u00e0nh:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>employee_id<\/b><\/td>\n<td><b>name<\/b><\/td>\n<td><b>department<\/b><\/td>\n<td><b>salary<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sales<\/span><\/td>\n<td><span style=\"font-weight: 400;\">55000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Jane Smith<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sales<\/span><\/td>\n<td><span style=\"font-weight: 400;\">66000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Alice Johnson<\/span><\/td>\n<td><span style=\"font-weight: 400;\">IT<\/span><\/td>\n<td><span style=\"font-weight: 400;\">70000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bob Brown<\/span><\/td>\n<td><span style=\"font-weight: 400;\">HR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">55000<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">N\u1ebfu b\u1ecf m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, t\u1ea5t c\u1ea3 b\u1ea3n ghi trong b\u1ea3ng <\/span><span style=\"font-weight: 400;\">Employees<\/span><span style=\"font-weight: 400;\"> s\u1ebd b\u1ecb c\u1eadp nh\u1eadt:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Employees<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET salary = salary * 1.1;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea3ng employees s\u1ebd tr\u1edf th\u00e0nh:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>employee_id<\/b><\/td>\n<td><b>name<\/b><\/td>\n<td><b>department<\/b><\/td>\n<td><b>salary<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sales<\/span><\/td>\n<td><span style=\"font-weight: 400;\">60500<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Jane Smith<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sales<\/span><\/td>\n<td><span style=\"font-weight: 400;\">72600<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Alice Johnson<\/span><\/td>\n<td><span style=\"font-weight: 400;\">IT<\/span><\/td>\n<td><span style=\"font-weight: 400;\">77000<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bob Brown<\/span><\/td>\n<td><span style=\"font-weight: 400;\">HR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">60500<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, n\u1ebfu b\u1ea1n mu\u1ed1n \u00e1p d\u1ee5ng l\u1ec7nh UPDATE l\u00ean t\u1ea5t c\u1ea3 c\u00e1c b\u1ea3n ghi trong b\u1ea3ng, b\u1ea1n kh\u00f4ng c\u1ea7n s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 c\u1eadp nh\u1eadt d\u1eef li\u1ec7u t\u1eeb m\u1ed9t b\u1ea3ng kh\u00e1c?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE k\u1ebft h\u1ee3p v\u1edbi truy v\u1ea5n con (subquery) ho\u1eb7c s\u1eed d\u1ee5ng JOIN \u0111\u1ec3 c\u1eadp nh\u1eadt d\u1eef li\u1ec7u t\u1eeb m\u1ed9t b\u1ea3ng kh\u00e1c. <\/span><span style=\"font-weight: 400;\">T\u00f9y v\u00e0o h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (DBMS), c\u00fa ph\u00e1p c\u00f3 th\u1ec3 kh\u00e1c nhau nh\u01b0 PostgreSQL v\u00e0 SQL Server c\u00f3 h\u1ed7 tr\u1ee3 FROM trong l\u1ec7nh Update, tuy nhi\u00ean MySQL l\u1ea1i kh\u00f4ng h\u1ed7 tr\u1ee3, m\u00e0 s\u1ebd s\u1eed d\u1ee5ng JOIN tr\u1ef1c ti\u1ebfp..<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 s\u1eed d\u1ee5ng subquery: \u0111\u1ec3 c\u1eadp nh\u1eadt gi\u00e1 c\u1ee7a c\u00e1c s\u1ea3n ph\u1ea9m thu\u1ed9c danh m\u1ee5c &#8220;Electronics&#8221; d\u1ef1a tr\u00ean gi\u00e1 trung b\u00ecnh trong b\u1ea3ng <\/span><span style=\"font-weight: 400;\">Sales<\/span><span style=\"font-weight: 400;\"> ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Products<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET price = (SELECT AVG(price) FROM Sales WHERE Sales.product_id = Products.product_id)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE category = 'Electronics';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 s\u1eed d\u1ee5ng JOIN: \u0111\u1ec3 c\u1eadp nh\u1eadt gi\u00e1 s\u1ea3n ph\u1ea9m trong b\u1ea3ng <\/span><span style=\"font-weight: 400;\">Products<\/span><span style=\"font-weight: 400;\"> d\u1ef1a tr\u00ean gi\u00e1 m\u1edbi t\u1eeb b\u1ea3ng <\/span><span style=\"font-weight: 400;\">Sales<\/span><span style=\"font-weight: 400;\"> ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE Products<\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET Products.price = Sales.new_price<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM Products<\/span>\r\n\r\n<span style=\"font-weight: 400;\">JOIN Sales ON Products.product_id = Sales.product_id<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE Sales.new_price IS NOT NULL;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE Sales.new_price IS NOT NULL<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o ch\u1ec9 c\u1eadp nh\u1eadt gi\u00e1 (price) cho c\u00e1c s\u1ea3n ph\u1ea9m m\u00e0 gi\u00e1 m\u1edbi (new_price) trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Sales<\/span><\/i><span style=\"font-weight: 400;\"> kh\u00f4ng ph\u1ea3i l\u00e0 <\/span><span style=\"font-weight: 400;\">NULL<\/span><span style=\"font-weight: 400;\">, gi\u00fap tr\u00e1nh vi\u1ec7c c\u1eadp nh\u1eadt gi\u00e1 th\u00e0nh gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">NULL<\/span><span style=\"font-weight: 400;\"> trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Products<\/span><\/i><span style=\"font-weight: 400;\">, duy tr\u00ec t\u00ednh ch\u00ednh x\u00e1c v\u00e0 h\u1ee3p l\u1ec7 c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00f3 th\u1ec3 undo c\u00e2u l\u1ec7nh UPDATE kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">SQL kh\u00f4ng c\u00f3 ch\u1ee9c n\u0103ng &#8220;undo&#8221; t\u1ef1 \u0111\u1ed9ng cho c\u00e2u l\u1ec7nh UPDATE. \u0110\u1ec3 kh\u00f4i ph\u1ee5c d\u1eef li\u1ec7u, b\u1ea1n c\u00f3 th\u1ec3:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng Transaction: N\u1ebfu b\u1ea1n b\u1eaft \u0111\u1ea7u m\u1ed9t giao d\u1ecbch (<\/span><span style=\"font-weight: 400;\">BEGIN TRANSACTION<\/span><span style=\"font-weight: 400;\">), b\u1ea1n c\u00f3 th\u1ec3 h\u1ee7y thay \u0111\u1ed5i b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">ROLLBACK<\/span><span style=\"font-weight: 400;\"> tr\u01b0\u1edbc khi giao d\u1ecbch \u0111\u01b0\u1ee3c x\u00e1c nh\u1eadn (<\/span><span style=\"font-weight: 400;\">COMMIT<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u00f4i ph\u1ee5c t\u1eeb b\u1ea3n sao l\u01b0u<\/b><span style=\"font-weight: 400;\">: N\u1ebfu d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt v\u00e0 giao d\u1ecbch \u0111\u00e3 ho\u00e0n th\u00e0nh, b\u1ea1n ch\u1ec9 c\u00f3 th\u1ec3 kh\u00f4i ph\u1ee5c d\u1eef li\u1ec7u t\u1eeb b\u1ea3n sao l\u01b0u n\u1ebfu c\u00f3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng b\u1ea3ng l\u01b0u tr\u1eef (Audit Table)<\/b><span style=\"font-weight: 400;\">: N\u1ebfu h\u1ec7 th\u1ed1ng c\u1ee7a b\u1ea1n c\u00f3 b\u1ea3ng l\u01b0u tr\u1eef ho\u1eb7c l\u1ecbch s\u1eed, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng d\u1eef li\u1ec7u t\u1eeb b\u1ea3ng \u0111\u00f3 \u0111\u1ec3 kh\u00f4i ph\u1ee5c d\u1eef li\u1ec7u v\u1ec1 tr\u1ea1ng th\u00e1i c\u0169.<\/span><\/li>\n<\/ol>\n<blockquote><p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u th\u00eam v\u1ec1 <a href=\"https:\/\/itviec.com\/blog\/transaction-trong-database-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Transaction trong database: C\u00e1c thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi transaction<\/strong><\/a><\/span><span style=\"font-weight: 400;\">.<\/span><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_cau_lenh_UPDATE_trong_SQL\"><\/span><b>T\u1ed5ng k\u1ebft c\u00e2u l\u1ec7nh UPDATE trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">C\u00e2u l\u1ec7nh UPDATE trong SQL l\u00e0 m\u1ed9t ph\u1ea7n kh\u00f4ng th\u1ec3 thi\u1ebfu trong qu\u1ea3n l\u00fd d\u1eef li\u1ec7u, cung c\u1ea5p kh\u1ea3 n\u0103ng ch\u1ec9nh s\u1eeda th\u00f4ng tin m\u1ed9t c\u00e1ch linh ho\u1ea1t v\u00e0 m\u1ea1nh m\u1ebd. D\u00f9 b\u1ea1n c\u1ea7n c\u1eadp nh\u1eadt m\u1ed9t c\u1ed9t, nhi\u1ec1u c\u1ed9t, hay thay \u0111\u1ed5i d\u1ef1a tr\u00ean \u0111i\u1ec1u ki\u1ec7n ph\u1ee9c t\u1ea1p, hi\u1ec3u r\u00f5 c\u00e1ch s\u1eed d\u1ee5ng l\u1ec7nh UPDATE s\u1ebd gi\u00fap b\u1ea1n l\u00e0m vi\u1ec7c hi\u1ec7u qu\u1ea3 h\u01a1n v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ITviec hy v\u1ecdng b\u00e0i vi\u1ebft tr\u00ean \u0111\u00e3 cung c\u1ea5p cho b\u1ea1n c\u00e1i nh\u00ecn t\u1ed5ng quan v\u00e0 gi\u00fap b\u1ea1n t\u1eadn d\u1ee5ng \u0111\u01b0\u1ee3c l\u1ec7nh UPDATE.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C\u00e2u l\u1ec7nh UPDATE trong SQL l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd v\u00e0 c\u1ea7n thi\u1ebft khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap b\u1ea1n c\u1eadp nh\u1eadt d\u1eef li\u1ec7u nhanh ch\u00f3ng v\u00e0 ch\u00ednh x\u00e1c. T\u1eeb vi\u1ec7c thay \u0111\u1ed5i th\u00f4ng tin c\u00e1 nh\u00e2n, c\u1eadp nh\u1eadt gi\u00e1 s\u1ea3n ph\u1ea9m, \u0111\u1ebfn x\u1eed l\u00fd tr\u1ea1ng th\u00e1i \u0111\u01a1n h\u00e0ng, l\u1ec7nh UPDATE [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":84346,"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-84297","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>C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng - ITviec Blog<\/title>\n<meta name=\"description\" content=\"T\u00ecm hi\u1ec3u c\u00e1ch s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE trong SQL \u0111\u1ec3 c\u1eadp nh\u1eadt d\u1eef li\u1ec7u. H\u01b0\u1edbng d\u1eabn chi ti\u1ebft t\u1eeb c\u00fa ph\u00e1p, c\u00e1ch ho\u1ea1t \u0111\u1ed9ng \u0111\u1ebfn c\u00e1c v\u00ed d\u1ee5 th\u1ef1c t\u1ebf.\" \/>\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\/cau-lenh-update-trong-sql\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng\" \/>\n<meta property=\"og:description\" content=\"C\u00e2u l\u1ec7nh UPDATE trong SQL l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd v\u00e0 c\u1ea7n thi\u1ebft khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap b\u1ea1n c\u1eadp nh\u1eadt d\u1eef li\u1ec7u nhanh ch\u00f3ng v\u00e0 ch\u00ednh x\u00e1c. T\u1eeb vi\u1ec7c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-lenh-update-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=\"2025-01-31T13:13:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-01T15:48:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-lenh-Update-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=\"13 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng - ITviec Blog","description":"T\u00ecm hi\u1ec3u c\u00e1ch s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE trong SQL \u0111\u1ec3 c\u1eadp nh\u1eadt d\u1eef li\u1ec7u. H\u01b0\u1edbng d\u1eabn chi ti\u1ebft t\u1eeb c\u00fa ph\u00e1p, c\u00e1ch ho\u1ea1t \u0111\u1ed9ng \u0111\u1ebfn c\u00e1c v\u00ed d\u1ee5 th\u1ef1c t\u1ebf.","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\/cau-lenh-update-trong-sql\/","og_locale":"vi_VN","og_type":"article","og_title":"C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng","og_description":"C\u00e2u l\u1ec7nh UPDATE trong SQL l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd v\u00e0 c\u1ea7n thi\u1ebft khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap b\u1ea1n c\u1eadp nh\u1eadt d\u1eef li\u1ec7u nhanh ch\u00f3ng v\u00e0 ch\u00ednh x\u00e1c. T\u1eeb vi\u1ec7c","og_url":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-01-31T13:13:00+00:00","article_modified_time":"2025-02-01T15:48:57+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-lenh-Update-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":"13 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng","datePublished":"2025-01-31T13:13:00+00:00","dateModified":"2025-02-01T15:48:57+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/"},"wordCount":3119,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-lenh-Update-trong-SQL-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/","url":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/","name":"C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-lenh-Update-trong-SQL-vippro.jpg","datePublished":"2025-01-31T13:13:00+00:00","dateModified":"2025-02-01T15:48:57+00:00","description":"T\u00ecm hi\u1ec3u c\u00e1ch s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh UPDATE trong SQL \u0111\u1ec3 c\u1eadp nh\u1eadt d\u1eef li\u1ec7u. H\u01b0\u1edbng d\u1eabn chi ti\u1ebft t\u1eeb c\u00fa ph\u00e1p, c\u00e1ch ho\u1ea1t \u0111\u1ed9ng \u0111\u1ebfn c\u00e1c v\u00ed d\u1ee5 th\u1ef1c t\u1ebf.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-lenh-Update-trong-SQL-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-lenh-Update-trong-SQL-vippro.jpg","width":1500,"height":790,"caption":"ca\u0302u le\u0323\u0302nh Update trong SQL - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-lenh-update-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":"C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng"}]},{"@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\/84297","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=84297"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/84297\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/84346"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=84297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=84297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=84297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}