{"id":83983,"date":"2025-01-10T14:52:58","date_gmt":"2025-01-10T07:52:58","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=83983"},"modified":"2025-09-28T22:50:37","modified_gmt":"2025-09-28T15:50:37","slug":"cau-hoi-phong-van-sql","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/","title":{"rendered":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t"},"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-hoi-phong-van-sql\/#Tong_quan_ve_SQL\" >T\u1ed5ng quan v\u1ec1 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-hoi-phong-van-sql\/#Nhung_ai_can_biet_cau_hoi_phong_van_SQL\" >Nh\u1eefng ai c\u1ea7n bi\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n 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-hoi-phong-van-sql\/#Cac_cau_hoi_phong_van_SQL_danh_cho_Fresher\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL d\u00e0nh cho Fresher<\/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-hoi-phong-van-sql\/#Cac_cau_hoi_phong_van_SQL_danh_cho_Trung_cap\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL d\u00e0nh cho Trung c\u1ea5p<\/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-hoi-phong-van-sql\/#Cac_cau_hoi_phong_van_SQL_danh_cho_Senior\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL d\u00e0nh cho Senior<\/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-hoi-phong-van-sql\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><em><strong>SQL l\u00e0 m\u1ed9t k\u1ef9 n\u0103ng quan tr\u1ecdng trong ng\u00e0nh c\u00f4ng ngh\u1ec7 th\u00f4ng tin, \u0111\u1eb7c bi\u1ec7t khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u. Vi\u1ec7c chu\u1ea9n b\u1ecb c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL l\u00e0 y\u1ebfu t\u1ed1 then ch\u1ed1t gi\u00fap b\u1ea1n th\u00e0nh c\u00f4ng trong c\u00e1c bu\u1ed5i ph\u1ecfng v\u1ea5n. Trong b\u00e0i vi\u1ebft n\u00e0y, ITviec s\u1ebd gi\u1edbi thi\u1ec7u c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn, k\u00e8m theo l\u1eddi gi\u1ea3i th\u00edch chi ti\u1ebft v\u00e0 v\u00ed d\u1ee5 th\u1ef1c t\u1ebf \u0111\u1ec3 gi\u00fap b\u1ea1n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c v\u00e0 g\u00e2y \u1ea5n t\u01b0\u1ee3ng v\u1edbi nh\u00e0 tuy\u1ec3n d\u1ee5ng.<\/strong><\/em><\/p>\n\n\n\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\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Nh\u1eefng v\u1ecb tr\u00ed ngh\u1ec1 nghi\u1ec7p y\u00eau c\u1ea7u SQL<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL th\u01b0\u1eddng g\u1eb7p d\u00e0nh cho Fresher<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL th\u01b0\u1eddng g\u1eb7p d\u00e0nh cho Intermediate<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL th\u01b0\u1eddng g\u1eb7p d\u00e0nh cho Advanced<\/span><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tham kh\u1ea3o <strong><a href=\"https:\/\/itviec.com\/it-jobs\/sql?utm_medium=anchor_text_high&amp;utm_source=blog&amp;utm_campaign=vec_lam_sql&amp;utm_content=cau_hoi_phong_van_sql\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m SQL<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean to\u00e0n qu\u1ed1c<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-quan-v\u1ec1-sql\"><span class=\"ez-toc-section\" id=\"Tong_quan_ve_SQL\"><\/span><b>T\u1ed5ng quan v\u1ec1 SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">SQL (Structured Query Language) l\u00e0 ng\u00f4n ng\u1eef truy v\u1ea5n \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1eb7c bi\u1ec7t \u0111\u1ec3 l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 (Relational Database Management Systems &#8211; RDBMS). SQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u01b0u tr\u1eef, truy xu\u1ea5t, qu\u1ea3n l\u00fd v\u00e0 thao t\u00e1c d\u1eef li\u1ec7u trong c\u00e1c h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u00ean c\u1ea1nh \u0111\u00f3, SQL c\u00f2n h\u1ed7 tr\u1ee3 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 c\u1ea5p quy\u1ec1n, t\u1ea1o b\u1ea3ng v\u00e0 \u0111\u1ecbnh ngh\u0129a c\u1ea5u tr\u00fac d\u1eef li\u1ec7u d\u01b0\u1edbi d\u1ea1ng c\u00e1c b\u1ea3ng (table). M\u1ed7i b\u1ea3ng trong SQL bao g\u1ed3m c\u00e1c c\u1ed9t (column) bi\u1ec3u di\u1ec5n thu\u1ed9c t\u00ednh c\u1ee7a d\u1eef li\u1ec7u v\u00e0 c\u00e1c h\u00e0ng (row) ch\u1ee9a d\u1eef li\u1ec7u c\u1ee5 th\u1ec3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">SQL \u0111\u01b0\u1ee3c ph\u00e1t tri\u1ec3n l\u1ea7n \u0111\u1ea7u ti\u00ean v\u00e0o nh\u1eefng n\u0103m 1970 t\u1ea1i IBM v\u1edbi m\u1ee5c \u0111\u00edch t\u1ea1o ra m\u1ed9t ng\u00f4n ng\u1eef ti\u00eau chu\u1ea9n \u0111\u1ec3 t\u01b0\u01a1ng t\u00e1c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u. Sau \u0111\u00f3, n\u00f3 \u0111\u00e3 \u0111\u01b0\u1ee3c chu\u1ea9n h\u00f3a b\u1edfi ANSI (American National Standards Institute) v\u00e0 ISO (International Organization for Standardization), v\u00e0 tr\u1edf th\u00e0nh ng\u00f4n ng\u1eef chu\u1ea9n qu\u1ed1c t\u1ebf cho c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-nh\u1eefng-ai-c\u1ea7n-bi\u1ebft-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-sql\"><span class=\"ez-toc-section\" id=\"Nhung_ai_can_biet_cau_hoi_phong_van_SQL\"><\/span><b>Nh\u1eefng ai c\u1ea7n bi\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>L\u1eadp tr\u00ecnh vi\u00ean (Developers): <\/b><span style=\"font-weight: 400;\">C\u00e1c l\u1eadp tr\u00ecnh vi\u00ean, \u0111\u1eb7c bi\u1ec7t l\u00e0 backend developers v\u00e0 full-stack developers, c\u1ea7n c\u00f3 ki\u1ebfn th\u1ee9c v\u1ec1 SQL \u0111\u1ec3 l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u khi x\u00e2y d\u1ef1ng v\u00e0 ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng. H\u1ecd th\u01b0\u1eddng \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u vi\u1ebft c\u00e1c truy v\u1ea5n SQL \u0111\u1ec3 th\u00eam, x\u00f3a, ho\u1eb7c c\u1eadp nh\u1eadt d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>Chuy\u00ean vi\u00ean ph\u00e2n t\u00edch d\u1eef li\u1ec7u (Data Analysts): <\/b><span style=\"font-weight: 400;\">Nh\u1eefng ng\u01b0\u1eddi l\u00e0m c\u00f4ng vi\u1ec7c ph\u00e2n t\u00edch d\u1eef li\u1ec7u c\u1ea7n s\u1eed d\u1ee5ng SQL \u0111\u1ec3 truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb c\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u l\u1edbn v\u00e0 t\u1ea1o b\u00e1o c\u00e1o ph\u1ee5c v\u1ee5 cho vi\u1ec7c ra quy\u1ebft \u0111\u1ecbnh c\u1ee7a doanh nghi\u1ec7p.<\/span><\/li>\n\n\n\n<li><b>K\u1ef9 s\u01b0 d\u1eef li\u1ec7u (Data Engineers): <\/b><span style=\"font-weight: 400;\">C\u00e1c k\u1ef9 s\u01b0 d\u1eef li\u1ec7u ph\u1ea3i thao t\u00e1c v\u1edbi kh\u1ed1i l\u01b0\u1ee3ng l\u1edbn d\u1eef li\u1ec7u, thi\u1ebft k\u1ebf v\u00e0 t\u1ed1i \u01b0u h\u00f3a c\u1ea5u tr\u00fac c\u01a1 s\u1edf d\u1eef li\u1ec7u. H\u1ecd c\u1ea7n th\u00e0nh th\u1ea1o SQL \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c x\u1eed l\u00fd v\u00e0 l\u01b0u tr\u1eef hi\u1ec7u qu\u1ea3.<\/span><\/li>\n\n\n\n<li><b>Chuy\u00ean vi\u00ean khoa h\u1ecdc d\u1eef li\u1ec7u (Data Scientists): <\/b><span style=\"font-weight: 400;\">Trong qu\u00e1 tr\u00ecnh thu th\u1eadp v\u00e0 chu\u1ea9n b\u1ecb d\u1eef li\u1ec7u \u0111\u1ec3 x\u00e2y d\u1ef1ng m\u00f4 h\u00ecnh ph\u00e2n t\u00edch, c\u00e1c nh\u00e0 khoa h\u1ecdc d\u1eef li\u1ec7u c\u1ea7n vi\u1ebft truy v\u1ea5n SQL \u0111\u1ec3 l\u1ecdc, nh\u00f3m, ho\u1eb7c k\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng kh\u00e1c nhau.<\/span><\/li>\n\n\n\n<li><b>Qu\u1ea3n tr\u1ecb vi\u00ean c\u01a1 s\u1edf d\u1eef li\u1ec7u (Database Administrators &#8211; DBAs):<\/b><b><br><\/b><span style=\"font-weight: 400;\">C\u00e1c DBA c\u1ea7n n\u1eafm v\u1eefng SQL \u0111\u1ec3 qu\u1ea3n l\u00fd, b\u1ea3o tr\u00ec v\u00e0 t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t c\u1ee7a c\u01a1 s\u1edf d\u1eef li\u1ec7u, \u0111\u1ed3ng th\u1eddi \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n v\u00e0 b\u1ea3o m\u1eadt c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>Nh\u00e0 qu\u1ea3n l\u00fd d\u1ef1 \u00e1n v\u00e0 ki\u1ec3m th\u1eed vi\u00ean (Project Managers &amp; Testers): <\/b><span style=\"font-weight: 400;\">\u0110\u1ed1i v\u1edbi c\u00e1c nh\u00e0 qu\u1ea3n l\u00fd d\u1ef1 \u00e1n ph\u1ea7n m\u1ec1m v\u00e0 ki\u1ec3m th\u1eed vi\u00ean, hi\u1ec3u SQL gi\u00fap h\u1ecd ki\u1ec3m tra, x\u00e1c th\u1ef1c d\u1eef li\u1ec7u v\u00e0 \u0111\u1ea3m b\u1ea3o h\u1ec7 th\u1ed1ng ho\u1ea1t \u0111\u1ed9ng \u0111\u00fang y\u00eau c\u1ea7u.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, b\u1ea5t k\u1ef3 ai l\u00e0m vi\u1ec7c trong m\u00f4i tr\u01b0\u1eddng li\u00ean quan \u0111\u1ebfn d\u1eef li\u1ec7u v\u00e0 ph\u00e1t tri\u1ec3n ph\u1ea7n m\u1ec1m \u0111\u1ec1u c\u1ea7n c\u00f3 hi\u1ec3u bi\u1ebft v\u1ec1 SQL.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-sql-danh-cho-fresher\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_SQL_danh_cho_Fresher\"><\/span><b>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL d\u00e0nh cho Fresher<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-di\u1ec3m-khac-bi\u1ec7t-gi\u1eefa-sql-va-nosql\"><b>\u0110i\u1ec3m kh\u00e1c bi\u1ec7t gi\u1eefa SQL v\u00e0 NoSQL?<\/b><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>SQL<\/b><\/td><td><b>NoSQL<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Lo\u1ea1i c\u01a1 s\u1edf d\u1eef li\u1ec7u<\/span><\/td><td><span style=\"font-weight: 400;\">Quan h\u1ec7 (Relational Database).<\/span><\/td><td><span style=\"font-weight: 400;\">C\u01a1 s\u1edf d\u1eef li\u1ec7u linh ho\u1ea1t (Flexible Database), kh\u00f4ng b\u1ecb gi\u1edbi h\u1ea1n b\u1edfi m\u00f4 h\u00ecnh quan h\u1ec7.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">M\u00f4 h\u00ecnh d\u1eef li\u1ec7u<\/span><\/td><td><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef d\u01b0\u1edbi d\u1ea1ng b\u1ea3ng c\u00f3 h\u00e0ng v\u00e0 c\u1ed9t.<\/span><\/td><td><span style=\"font-weight: 400;\">L\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u01b0\u1edbi d\u1ea1ng Key-Value, Document, Column-Family, Graph.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">C\u1ea5u tr\u00fac d\u1eef li\u1ec7u<\/span><\/td><td><span style=\"font-weight: 400;\">Y\u00eau c\u1ea7u l\u01b0\u1ee3c \u0111\u1ed3 (schema) c\u1ed1 \u0111\u1ecbnh, ch\u1eb7t ch\u1ebd.<\/span><\/td><td><span style=\"font-weight: 400;\">Linh ho\u1ea1t, kh\u00f4ng y\u00eau c\u1ea7u l\u01b0\u1ee3c \u0111\u1ed3 r\u00f5 r\u00e0ng ho\u1eb7c c\u00f3 l\u01b0\u1ee3c \u0111\u1ed3 \u0111\u1ed9ng.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ng\u00f4n ng\u1eef truy v\u1ea5n<\/span><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng ng\u00f4n ng\u1eef truy v\u1ea5n chu\u1ea9n SQL.<\/span><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng API ho\u1eb7c ng\u00f4n ng\u1eef truy v\u1ea5n ri\u00eang bi\u1ec7t c\u1ee7a CSDL.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng<\/span><\/td><td><span style=\"font-weight: 400;\">M\u1edf r\u1ed9ng theo chi\u1ec1u d\u1ecdc (Vertical Scaling), nh\u01b0ng m\u1ed9t s\u1ed1 h\u1ec7 th\u1ed1ng c\u0169ng h\u1ed7 tr\u1ee3 m\u1edf r\u1ed9ng chi\u1ec1u ngang (v\u00ed d\u1ee5 sharding trong MySQL).<\/span><\/td><td><span style=\"font-weight: 400;\">M\u1edf r\u1ed9ng theo chi\u1ec1u ngang (Horizontal Scaling).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">T\u00ednh nh\u1ea5t qu\u00e1n<\/span><\/td><td><span style=\"font-weight: 400;\">Tu\u00e2n th\u1ee7 ACID (\u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u).<\/span><\/td><td><span style=\"font-weight: 400;\">Tu\u00e2n th\u1ee7 BASE (\u0111\u1ea3m b\u1ea3o t\u00ednh s\u1eb5n s\u00e0ng cao), m\u1ed9t s\u1ed1 lo\u1ea1i cung c\u1ea5p t\u00f9y ch\u1ecdn \u0111\u1ec3 h\u1ed7 tr\u1ee3 t\u00ednh nh\u1ea5t qu\u00e1n m\u1ea1nh m\u1ebd h\u01a1n.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">T\u00ednh linh ho\u1ea1t<\/span><\/td><td><span style=\"font-weight: 400;\">\u00cdt linh ho\u1ea1t h\u01a1n khi thay \u0111\u1ed5i c\u1ea5u tr\u00fac d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">Linh ho\u1ea1t h\u01a1n trong vi\u1ec7c thay \u0111\u1ed5i v\u00e0 m\u1edf r\u1ed9ng d\u1eef li\u1ec7u.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">V\u00ed d\u1ee5<\/span><\/td><td><span style=\"font-weight: 400;\">MySQL, PostgreSQL, SQL Server, Oracle.<\/span><\/td><td><span style=\"font-weight: 400;\">MongoDB, Cassandra, Redis, CouchDB.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cau-l\u1ec7nh-select-trong-sql-dung-d\u1ec3-lam-gi-cu-phap-c\u01a1-b\u1ea3n-c\u1ee7a-no\"><b>C\u00e2u l\u1ec7nh SELECT trong SQL d\u00f9ng \u0111\u1ec3 l\u00e0m g\u00ec? C\u00fa ph\u00e1p c\u01a1 b\u1ea3n c\u1ee7a n\u00f3?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> trong SQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 truy v\u1ea5n v\u00e0 l\u1ea5y d\u1eef li\u1ec7u t\u1eeb m\u1ed9t ho\u1eb7c nhi\u1ec1u b\u1ea3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. \u0110\u00e2y l\u00e0 c\u00e2u l\u1ec7nh ph\u1ed5 bi\u1ebfn v\u00e0 quan tr\u1ecdng nh\u1ea5t trong SQL, cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng truy xu\u1ea5t c\u00e1c c\u1ed9t c\u1ee5 th\u1ec3 trong b\u1ea3ng, t\u00ednh to\u00e1n gi\u00e1 tr\u1ecb, \u0111\u1ed3ng th\u1eddi c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p v\u1edbi c\u00e1c m\u1ec7nh \u0111\u1ec1 nh\u01b0 WHERE \u0111\u1ec3 l\u1ecdc d\u1eef li\u1ec7u theo \u0111i\u1ec1u ki\u1ec7n, ORDER BY \u0111\u1ec3 s\u1eafp x\u1ebfp d\u1eef li\u1ec7u, c\u00e1c ph\u00e9p JOIN \u0111\u1ec3 k\u1ebft h\u1ee3p nhi\u1ec1u b\u1ea3ng d\u1eef li\u1ec7u l\u1ea1i v\u1edbi nhau.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p c\u01a1 b\u1ea3n:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT column1, column2, ...<\/span>\n\n<span style=\"font-weight: 400;\">FROM table_name<\/span>\n\n<span style=\"font-weight: 400;\">&#91;WHERE condition]<\/span>\n\n<span style=\"font-weight: 400;\">&#91;ORDER BY column_name ASC|DESC];<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">: X\u00e1c \u0111\u1ecbnh c\u00e1c c\u1ed9t c\u1ea7n l\u1ea5y d\u1eef li\u1ec7u. C\u00f3 th\u1ec3 d\u00f9ng <\/span><span style=\"font-weight: 400;\">SELECT *<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ecdn t\u1ea5t c\u1ea3 c\u00e1c c\u1ed9t trong b\u1ea3ng ho\u1eb7c k\u1ebft h\u1ee3p c\u00e1c h\u00e0m t\u00ednh to\u00e1n d\u1eef li\u1ec7u<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">FROM<\/span><span style=\"font-weight: 400;\">: Ch\u1ec9 \u0111\u1ecbnh t\u00ean b\u1ea3ng ch\u1ee9a d\u1eef li\u1ec7u c\u1ea7n truy v\u1ea5n.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">WHERE<\/span> <i><span style=\"font-weight: 400;\">(t\u00f9y ch\u1ecdn)<\/span><\/i><span style=\"font-weight: 400;\">: \u0110i\u1ec1u ki\u1ec7n \u0111\u1ec3 l\u1ecdc c\u00e1c h\u00e0ng ph\u00f9 h\u1ee3p.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">ORDER BY<\/span> <i><span style=\"font-weight: 400;\">(t\u00f9y ch\u1ecdn)<\/span><\/i><span style=\"font-weight: 400;\">: S\u1eafp x\u1ebfp k\u1ebft qu\u1ea3 theo m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u1ed9t. C\u00f3 th\u1ec3 s\u1eafp x\u1ebfp t\u0103ng d\u1ea7n (<\/span><span style=\"font-weight: 400;\">ASC<\/span><span style=\"font-weight: 400;\">) ho\u1eb7c gi\u1ea3m d\u1ea7n (<\/span><span style=\"font-weight: 400;\">DESC<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: Gi\u1ea3 s\u1eed c\u00f3 m\u1ed9t b\u1ea3ng t\u00ean l\u00e0 <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> v\u1edbi c\u00e1c c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">ID<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">Name<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">Salary<\/span><\/i><span style=\"font-weight: 400;\">. \u0110\u1ec3 l\u1ea5y t\u1ea5t c\u1ea3 d\u1eef li\u1ec7u t\u1eeb b\u1ea3ng employees ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT * FROM Employees;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y t\u00ean v\u00e0 l\u01b0\u01a1ng c\u1ee7a t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary FROM Employees;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y d\u1eef li\u1ec7u nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng l\u1edbn h\u01a1n 5000, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">WHERE Salary &gt; 5000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 s\u1eafp x\u1ebfp k\u1ebft qu\u1ea3 theo l\u01b0\u01a1ng gi\u1ea3m d\u1ea7n, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">ORDER BY Salary DESC;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-subset-trong-sql-la-gi\"><b>Subset trong SQL l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Subset trong SQL \u0111\u1ec1 c\u1eadp \u0111\u1ebfn vi\u1ec7c l\u1ea5y m\u1ed9t ph\u1ea7n d\u1eef li\u1ec7u con t\u1eeb b\u1ea3ng d\u1eef li\u1ec7u l\u1edbn h\u01a1n b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c \u0111i\u1ec1u ki\u1ec7n l\u1ecdc ho\u1eb7c c\u00e1c c\u00f4ng c\u1ee5 truy v\u1ea5n kh\u00e1c nhau. Vi\u1ec7c t\u1ea1o subset gi\u00fap ng\u01b0\u1eddi d\u00f9ng l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c t\u1eadp d\u1eef li\u1ec7u nh\u1ecf h\u01a1n, ph\u00f9 h\u1ee3p v\u1edbi m\u1ee5c \u0111\u00edch ph\u00e2n t\u00edch, b\u00e1o c\u00e1o ho\u1eb7c t\u00ednh to\u00e1n c\u1ee5 th\u1ec3.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-m\u1ed9t-s\u1ed1-vi-d\u1ee5-t\u1ea1o-subset-trong-sql\"><b>M\u1ed9t s\u1ed1 v\u00ed d\u1ee5 t\u1ea1o subset trong SQL:<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y nh\u1eefng nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng l\u1edbn h\u01a1n 5000, ta l\u1ecdc d\u1eef li\u1ec7u b\u1eb1ng <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ecdn c\u00e1c h\u00e0ng th\u1ecfa m\u00e3n \u0111i\u1ec1u ki\u1ec7n nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">WHERE Salary &gt; 5000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 l\u1ea5y ra c\u00e1c ph\u00f2ng ban c\u00f3 h\u01a1n 3 nh\u00e2n vi\u00ean, ta l\u1ecdc d\u1eef li\u1ec7u theo nh\u00f3m v\u1edbi <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> d\u1ef1a tr\u00ean \u0111i\u1ec1u ki\u1ec7n nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Department, COUNT(*) AS SoLuongNhanVien<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">GROUP BY Department<\/span>\n\n<span style=\"font-weight: 400;\">HAVING COUNT(*) &gt; 3;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">GROUP BY Department<\/span><span style=\"font-weight: 400;\"> nh\u00f3m d\u1eef li\u1ec7u theo ph\u00f2ng ban.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">HAVING COUNT(*) > 3<\/span><span style=\"font-weight: 400;\"> l\u00e0 b\u1ed9 l\u1ecdc ch\u1ec9 \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng sau khi c\u00e1c nh\u00f3m \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea1o b\u1edfi GROUP BY, gi\u00fap ch\u1ec9 gi\u1eef l\u1ea1i c\u00e1c nh\u00f3m c\u00f3 s\u1ed1 l\u01b0\u1ee3ng nh\u00e2n vi\u00ean l\u1edbn h\u01a1n 3.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-primary-key-va-foreign-key\"><b>Ph\u00e2n bi\u1ec7t Primary Key v\u00e0 Foreign Key<\/b><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>Primary Key<\/b><\/td><td><b>Foreign Key<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">\u0110\u1ecbnh ngh\u0129a<\/span><\/td><td><span style=\"font-weight: 400;\">L\u00e0 kh\u00f3a ch\u00ednh d\u00f9ng \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh t\u00ednh duy nh\u1ea5t c\u1ee7a m\u1ed7i h\u00e0ng trong b\u1ea3ng.<\/span><\/td><td><span style=\"font-weight: 400;\">L\u00e0 kh\u00f3a ngo\u1ea1i d\u00f9ng \u0111\u1ec3 t\u1ea1o m\u1ed1i quan h\u1ec7 gi\u1eefa c\u00e1c b\u1ea3ng.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">T\u00ednh duy nh\u1ea5t<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00e1c gi\u00e1 tr\u1ecb trong c\u1ed9t Primary Key kh\u00f4ng \u0111\u01b0\u1ee3c tr\u00f9ng l\u1eb7p v\u00e0 kh\u00f4ng \u0111\u01b0\u1ee3c \u0111\u1ec3 tr\u1ed1ng (NOT NULL).<\/span><\/td><td><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb trong Foreign Key c\u00f3 th\u1ec3 tr\u00f9ng l\u1eb7p v\u00e0 c\u00f3 th\u1ec3 ch\u1ee9a gi\u00e1 tr\u1ecb NULL.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">B\u1ea3ng \u00e1p d\u1ee5ng<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c khai b\u00e1o trong b\u1ea3ng ch\u00ednh \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh danh t\u00ednh duy nh\u1ea5t c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c khai b\u00e1o trong b\u1ea3ng con \u0111\u1ec3 tham chi\u1ebfu \u0111\u1ebfn Primary Key c\u1ee7a b\u1ea3ng ch\u00ednh.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o t\u00ednh duy nh\u1ea5t v\u00e0 to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u trong b\u1ea3ng.<\/span><\/td><td><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed1i quan h\u1ec7 r\u00e0ng bu\u1ed9c gi\u1eefa hai b\u1ea3ng. \u0110\u1ea3m b\u1ea3o r\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n d\u1eef li\u1ec7u gi\u1eefa hai b\u1ea3ng.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">S\u1ed1 l\u01b0\u1ee3ng<\/span><\/td><td><span style=\"font-weight: 400;\">M\u1ed7i b\u1ea3ng ch\u1ec9 c\u00f3 m\u1ed9t Primary Key.<\/span><\/td><td><span style=\"font-weight: 400;\">M\u1ed9t b\u1ea3ng c\u00f3 th\u1ec3 c\u00f3 nhi\u1ec1u Foreign Key.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-s\u1ef1-khac-bi\u1ec7t-gi\u1eefa-where-va-having-la-gi\"><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa WHERE v\u00e0 HAVING l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec1u \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u1ecdc d\u1eef li\u1ec7u trong SQL, nh\u01b0ng ch\u00fang c\u00f3 s\u1ef1 kh\u00e1c bi\u1ec7t quan tr\u1ecdng v\u1ec1 th\u1eddi \u0111i\u1ec3m \u00e1p d\u1ee5ng v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng trong c\u00e2u l\u1ec7nh truy v\u1ea5n.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>WHERE<\/b><\/td><td><b>HAVING<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch<\/span><\/td><td><span style=\"font-weight: 400;\">L\u1ecdc d\u1eef li\u1ec7u tr\u01b0\u1edbc khi th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh ho\u1eb7c nh\u00f3m d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">L\u1ecdc d\u1eef li\u1ec7u sau khi \u0111\u00e3 th\u1ef1c hi\u1ec7n ph\u00e9p nh\u00f3m (<\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\">).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">\u00c1p d\u1ee5ng cho<\/span><\/td><td><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u d\u00f2ng (h\u00e0ng) trong b\u1ea3ng.<\/span><\/td><td><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 c\u1ee7a c\u00e1c nh\u00f3m \u0111\u01b0\u1ee3c t\u1ea1o b\u1edfi <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c s\u1eed d\u1ee5ng tr\u1ef1c ti\u1ebfp v\u1edbi c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">D\u00f9ng v\u1edbi h\u00e0m t\u1ed5ng h\u1ee3p<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 d\u00f9ng v\u1edbi c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p (SUM, AVG&#8230;).<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 d\u00f9ng v\u1edbi c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: Gi\u1ea3 s\u1eed c\u00f3 b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Employees<\/span><\/i><span style=\"font-weight: 400;\"> v\u1edbi c\u00e1c c\u1ed9t: <\/span><i><span style=\"font-weight: 400;\">Name<\/span><\/i><span style=\"font-weight: 400;\"> (t\u00ean), <\/span><i><span style=\"font-weight: 400;\">Department<\/span><\/i><span style=\"font-weight: 400;\"> (ph\u00f2ng ban), v\u00e0 <\/span><i><span style=\"font-weight: 400;\">Salary<\/span><\/i><span style=\"font-weight: 400;\"> (l\u01b0\u01a1ng), \u0111\u1ec3 l\u1ecdc ra c\u00e1c nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng \u00edt h\u01a1n 5000, ta s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">WHERE Salary &gt; 5000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y c\u00e1c ph\u00f2ng ban c\u00f3 t\u1ed5ng l\u01b0\u01a1ng l\u1edbn h\u01a1n 20000, ta s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Department, SUM(Salary) AS TotalSalary<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">GROUP BY Department<\/span>\n\n<span style=\"font-weight: 400;\">HAVING SUM(Salary) &gt; 20000;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-sao-d\u1ec3-s\u1eafp-x\u1ebfp-d\u1eef-li\u1ec7u-trong-sql\"><b>L\u00e0m sao \u0111\u1ec3 s\u1eafp x\u1ebfp d\u1eef li\u1ec7u trong SQL?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong SQL, \u0111\u1ec3 s\u1eafp x\u1ebfp d\u1eef li\u1ec7u theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n ho\u1eb7c gi\u1ea3m d\u1ea7n, ta s\u1eed d\u1ee5ng m\u1ec7nh \u0111\u1ec1 <code>ORDER BY<\/code>. M\u1ec7nh \u0111\u1ec1 n\u00e0y cho ph\u00e9p s\u1eafp x\u1ebfp k\u1ebft qu\u1ea3 c\u1ee7a truy v\u1ea5n d\u1ef1a tr\u00ean m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u1ed9t c\u1ee5 th\u1ec3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p c\u01a1 b\u1ea3n:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT column1, column2, ...<\/span>\n\n<span style=\"font-weight: 400;\">FROM table_name<\/span>\n\n<span style=\"font-weight: 400;\">ORDER BY column_name &#91;ASC | DESC];<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 xu\u1ea5t ra t\u00ean v\u00e0 l\u01b0\u01a1ng c\u1ee7a nh\u00e2n vi\u00ean theo th\u1ee9 t\u1ef1 l\u01b0\u01a1ng gi\u1ea3m d\u1ea7n, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">ORDER BY Salary DESC;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">N\u1ebfu c\u00f3 nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n, SQL s\u1ebd s\u1eafp x\u1ebfp theo c\u1ed9t \u0111\u1ea7u ti\u00ean, sau \u0111\u00f3 \u0111\u1ebfn c\u1ed9t th\u1ee9 hai n\u1ebfu c\u1ea7n. V\u00ed d\u1ee5 nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary, Department&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">ORDER BY Department ASC, Salary DESC;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">S\u1eafp x\u1ebfp <\/span><span style=\"font-weight: 400;\">Department<\/span><span style=\"font-weight: 400;\"> theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n (A \u2192 Z).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Trong c\u00f9ng m\u1ed9t <\/span><span style=\"font-weight: 400;\">Department<\/span><span style=\"font-weight: 400;\">, s\u1eafp x\u1ebfp <\/span><span style=\"font-weight: 400;\">Salary<\/span><span style=\"font-weight: 400;\"> theo th\u1ee9 t\u1ef1 gi\u1ea3m d\u1ea7n.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 xu\u1ea5t ra danh s\u00e1ch nh\u00e2n vi\u00ean c\u00f3 t\u1ed5ng l\u01b0\u01a1ng v\u00e0 th\u01b0\u1edfng theo th\u1ee9 t\u1ef1 gi\u1ea3m d\u1ea7n ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary, Bonus&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">ORDER BY (Salary + Bonus) DESC;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">N\u1ebfu c\u00f3 gi\u00e1 tr\u1ecb NULL th\u00ec s\u1ebd \u0111\u01b0\u1ee3c x\u1eed l\u00fd \u0111\u1eb7c bi\u1ec7t khi s\u1eafp x\u1ebfp nh\u01b0 sau:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Khi s\u1eed d\u1ee5ng <code>ORDER BY ASC<\/code> (th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n), gi\u00e1 tr\u1ecb NULL th\u01b0\u1eddng \u0111\u01b0\u1ee3c x\u1ebfp <\/span><b>\u1edf \u0111\u1ea7u<\/b><span style=\"font-weight: 400;\"> danh s\u00e1ch.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Khi s\u1eed d\u1ee5ng <code>ORDER BY DESC<\/code> (th\u1ee9 t\u1ef1 gi\u1ea3m d\u1ea7n), gi\u00e1 tr\u1ecb NULL th\u01b0\u1eddng \u0111\u01b0\u1ee3c x\u1ebfp <\/span><b>\u1edf cu\u1ed1i<\/b><span style=\"font-weight: 400;\"> danh s\u00e1ch.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, h\u00e0nh vi n\u00e0y c\u00f3 th\u1ec3 thay \u0111\u1ed5i t\u00f9y theo h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (DBMS). M\u1ed9t s\u1ed1 h\u1ec7 qu\u1ea3n tr\u1ecb nh\u01b0 PostgreSQL h\u1ed7 tr\u1ee3 c\u00fa ph\u00e1p t\u00f9y ch\u1ec9nh <code>NULLS FIRST<\/code> ho\u1eb7c <code>NULLS LAST<\/code> \u0111\u1ec3 ch\u1ec9 \u0111\u1ecbnh r\u00f5 c\u00e1ch s\u1eafp x\u1ebfp gi\u00e1 tr\u1ecb <code>NULL<\/code>.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 s\u1eafp x\u1ebfp danh s\u00e1ch nh\u00e2n vi\u00ean theo l\u01b0\u01a1ng gi\u1ea3m d\u1ea7n, \u0111\u1ed3ng th\u1eddi c\u00e1c gi\u00e1 tr\u1ecb <code>NULL<\/code> \u0111\u01b0\u1ee3c \u0111\u01b0a xu\u1ed1ng cu\u1ed1i, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p nh\u01b0 sau trong MySQL:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">ORDER BY Salary DESC NULLS LAST;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ebf-nao-la-null-trong-sql-null-khac-gi-v\u1edbi-0\"><b>Th\u1ebf n\u00e0o l\u00e0 NULL trong SQL? NULL kh\u00e1c g\u00ec v\u1edbi 0?<\/b><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>NULL<\/b><\/td><td><b>0<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">\u0110\u1ecbnh ngh\u0129a<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ea1i di\u1ec7n cho gi\u00e1 tr\u1ecb kh\u00f4ng x\u00e1c \u0111\u1ecbnh ho\u1eb7c kh\u00f4ng c\u00f3 d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">L\u00e0 m\u1ed9t gi\u00e1 tr\u1ecb s\u1ed1 c\u1ee5 th\u1ec3, ngh\u0129a l\u00e0 &#8220;kh\u00f4ng&#8221;.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ki\u1ec3u d\u1eef li\u1ec7u<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u00e1p d\u1ee5ng cho m\u1ecdi ki\u1ec3u d\u1eef li\u1ec7u (chu\u1ed7i, s\u1ed1, ng\u00e0y&#8230;).<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 \u00e1p d\u1ee5ng cho ki\u1ec3u s\u1ed1.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 ph\u00e9p to\u00e1n<\/span><\/td><td><span style=\"font-weight: 400;\">Ph\u00e9p to\u00e1n v\u1edbi NULL s\u1ebd tr\u1ea3 v\u1ec1 <\/span><span style=\"font-weight: 400;\">NULL<\/span><span style=\"font-weight: 400;\"> (v\u00ec kh\u00f4ng x\u00e1c \u0111\u1ecbnh).<\/span><\/td><td><span style=\"font-weight: 400;\">Ph\u00e9p to\u00e1n v\u1edbi 0 s\u1ebd th\u1ef1c hi\u1ec7n nh\u01b0 b\u00ecnh th\u01b0\u1eddng.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">So s\u00e1nh<\/span><\/td><td><span style=\"font-weight: 400;\">NULL kh\u00f4ng th\u1ec3 so s\u00e1nh tr\u1ef1c ti\u1ebfp v\u1edbi NULL kh\u00e1c (<\/span><span style=\"font-weight: 400;\">NULL = NULL<\/span><span style=\"font-weight: 400;\"> l\u00e0 FALSE). \u0110\u1ec3 ki\u1ec3m tra NULL, ph\u1ea3i d\u00f9ng IS NULL ho\u1eb7c IS NOT NULL.<\/span><\/td><td><span style=\"font-weight: 400;\">0 c\u00f3 th\u1ec3 so s\u00e1nh v\u1edbi 0 (<\/span><span style=\"font-weight: 400;\">0 = 0<\/span><span style=\"font-weight: 400;\"> l\u00e0 TRUE).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">V\u00ed d\u1ee5<\/span><\/td><td><span style=\"font-weight: 400;\">M\u1ed9t c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">Salary<\/span><\/i><span style=\"font-weight: 400;\"> kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb n\u00e0o \u0111\u01b0\u1ee3c nh\u1eadp v\u00e0o (NULL).<\/span><\/td><td><span style=\"font-weight: 400;\">M\u1ed9t c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">Salary<\/span><\/i><span style=\"font-weight: 400;\"> c\u00f3 gi\u00e1 tr\u1ecb nh\u1eadp v\u00e0o l\u00e0 0.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 ta c\u00f3 b\u1ea3ng sau:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Name<\/b><\/td><td><b>Salary<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">An<\/span><\/td><td><span style=\"font-weight: 400;\">NULL<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">B\u00ecnh<\/span><\/td><td><span style=\"font-weight: 400;\">0<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ch\u00e2u<\/span><\/td><td><span style=\"font-weight: 400;\">5000<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>NULL:<\/b><span style=\"font-weight: 400;\"> V\u1edbi nh\u00e2n vi\u00ean &#8220;An&#8221;, <\/span><i><span style=\"font-weight: 400;\">Salary<\/span><\/i><span style=\"font-weight: 400;\"> kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb, t\u1ee9c l\u00e0 ch\u01b0a \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh.<\/span><\/li>\n\n\n\n<li><b>0:<\/b><span style=\"font-weight: 400;\"> V\u1edbi nh\u00e2n vi\u00ean &#8220;B\u00ecnh&#8221;, <\/span><i><span style=\"font-weight: 400;\">Salary<\/span><\/i><span style=\"font-weight: 400;\"> c\u00f3 gi\u00e1 tr\u1ecb &#8220;0&#8221;, ngh\u0129a l\u00e0 l\u01b0\u01a1ng b\u1eb1ng 0.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cau-l\u1ec7nh-join-trong-sql-la-gi-k\u1ec3-ten-cac-lo\u1ea1i-join\"><b>C\u00e2u l\u1ec7nh JOIN trong SQL l\u00e0 g\u00ec? K\u1ec3 t\u00ean c\u00e1c lo\u1ea1i JOIN.<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong SQL, JOIN l\u00e0 c\u00e2u l\u1ec7nh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 k\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb hai ho\u1eb7c nhi\u1ec1u b\u1ea3ng d\u1ef1a tr\u00ean m\u1ed9t \u0111i\u1ec1u ki\u1ec7n chung, th\u01b0\u1eddng l\u00e0 m\u1ed1i quan h\u1ec7 gi\u1eefa c\u00e1c c\u1ed9t trong c\u00e1c b\u1ea3ng. C\u00e2u l\u1ec7nh JOIN gi\u00fap b\u1ea1n truy v\u1ea5n v\u00e0 l\u1ea5y d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng m\u1ed9t c\u00e1ch linh ho\u1ea1t v\u00e0 hi\u1ec7u qu\u1ea3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT column1, column2, ...<\/span>\n\n<span style=\"font-weight: 400;\">FROM table1<\/span>\n\n<span style=\"font-weight: 400;\">&#91;JOIN_TYPE] table2<\/span>\n\n<span style=\"font-weight: 400;\">ON table1.column_name = table2.column_name;<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cac-lo\u1ea1i-join\"><b>C\u00e1c lo\u1ea1i JOIN:<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>INNER JOIN<\/b><span style=\"font-weight: 400;\">: Ch\u1ec9 tr\u1ea3 v\u1ec1 c\u00e1c h\u00e0ng c\u00f3 gi\u00e1 tr\u1ecb kh\u1edbp nhau trong c\u1ea3 hai b\u1ea3ng.<\/span><\/li>\n\n\n\n<li><b>LEFT JOIN (LEFT OUTER JOIN)<\/b><span style=\"font-weight: 400;\">: Tr\u1ea3 v\u1ec1 t\u1ea5t c\u1ea3 c\u00e1c h\u00e0ng t\u1eeb b\u1ea3ng b\u00ean tr\u00e1i v\u00e0 c\u00e1c h\u00e0ng kh\u1edbp t\u1eeb b\u1ea3ng b\u00ean ph\u1ea3i. N\u1ebfu kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb kh\u1edbp, k\u1ebft qu\u1ea3 s\u1ebd hi\u1ec3n th\u1ecb NULL cho b\u1ea3ng b\u00ean ph\u1ea3i.<\/span><\/li>\n\n\n\n<li><b>RIGHT JOIN (RIGHT OUTER JOIN)<\/b><span style=\"font-weight: 400;\">: Tr\u1ea3 v\u1ec1 t\u1ea5t c\u1ea3 c\u00e1c h\u00e0ng t\u1eeb b\u1ea3ng b\u00ean ph\u1ea3i v\u00e0 c\u00e1c h\u00e0ng kh\u1edbp t\u1eeb b\u1ea3ng b\u00ean tr\u00e1i. N\u1ebfu kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb kh\u1edbp, k\u1ebft qu\u1ea3 s\u1ebd hi\u1ec3n th\u1ecb NULL cho b\u1ea3ng b\u00ean tr\u00e1i.<\/span><\/li>\n\n\n\n<li><b>FULL JOIN (FULL OUTER JOIN)<\/b><span style=\"font-weight: 400;\">: Tr\u1ea3 v\u1ec1 t\u1ea5t c\u1ea3 c\u00e1c h\u00e0ng t\u1eeb c\u1ea3 hai b\u1ea3ng. C\u00e1c h\u00e0ng kh\u00f4ng kh\u1edbp s\u1ebd hi\u1ec3n th\u1ecb NULL \u1edf b\u1ea3ng kh\u00f4ng c\u00f3 d\u1eef li\u1ec7u t\u01b0\u01a1ng \u1ee9ng.<\/span><\/li>\n\n\n\n<li><b>CROSS JOIN<\/b><span style=\"font-weight: 400;\">: Tr\u1ea3 v\u1ec1 t\u00edch Descartes c\u1ee7a hai b\u1ea3ng (k\u1ebft h\u1ee3p t\u1ea5t c\u1ea3 c\u00e1c h\u00e0ng c\u1ee7a b\u1ea3ng n\u00e0y v\u1edbi t\u1ea5t c\u1ea3 c\u00e1c h\u00e0ng c\u1ee7a b\u1ea3ng kia).<\/span><\/li>\n\n\n\n<li><b>SELF JOIN<\/b><span style=\"font-weight: 400;\">: L\u00e0 m\u1ed9t JOIN \u0111\u1eb7c bi\u1ec7t, trong \u0111\u00f3 m\u1ed9t b\u1ea3ng \u0111\u01b0\u1ee3c k\u1ebft h\u1ee3p v\u1edbi ch\u00ednh n\u00f3 nh\u01b0 th\u1ec3 \u0111\u00f3 l\u00e0 hai b\u1ea3ng kh\u00e1c nhau.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ebf-nao-la-subquery-truy-v\u1ea5n-con-cho-vi-d\u1ee5\"><b>Th\u1ebf n\u00e0o l\u00e0 Subquery (truy v\u1ea5n con)? Cho v\u00ed d\u1ee5.<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Subquery, hay c\u00f2n g\u1ecdi l\u00e0 truy v\u1ea5n con, l\u00e0 m\u1ed9t truy v\u1ea5n SQL \u0111\u01b0\u1ee3c l\u1ed3ng b\u00ean trong m\u1ed9t truy v\u1ea5n SQL kh\u00e1c. Subquery th\u01b0\u1eddng \u0111\u01b0\u1ee3c \u0111\u1eb7t trong m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">FROM<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c c\u00e1c m\u1ec7nh \u0111\u1ec1 kh\u00e1c c\u1ee7a truy v\u1ea5n ch\u00ednh. M\u1ee5c \u0111\u00edch c\u1ee7a subquery l\u00e0 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 t\u1ea1m th\u1eddi \u0111\u1ec3 cung c\u1ea5p d\u1eef li\u1ec7u cho truy v\u1ea5n b\u00ean ngo\u00e0i (truy v\u1ea5n ch\u00ednh).<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-d\u1eb7c-di\u1ec3m-c\u1ee7a-subquery\"><b>\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a Subquery<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Lu\u00f4n \u0111\u01b0\u1ee3c \u0111\u1eb7t trong c\u1eb7p d\u1ea5u ngo\u1eb7c \u0111\u01a1n <\/span><span style=\"font-weight: 400;\">()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ph\u00e2n bi\u1ec7t v\u1edbi truy v\u1ea5n ch\u00ednh.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb (scalar), m\u1ed9t c\u1ed9t, ho\u1eb7c m\u1ed9t b\u1ea3ng k\u1ebft qu\u1ea3.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Truy v\u1ea5n con s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi tr\u01b0\u1edbc truy v\u1ea5n ch\u00ednh.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed ta c\u00f3 b\u1ea3ng employees:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Name<\/b><\/td><td><b>Salary<\/b><\/td><td><b>Department<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">An<\/span><\/td><td><span style=\"font-weight: 400;\">4000<\/span><\/td><td><span style=\"font-weight: 400;\">K\u1ef9 thu\u1eadt<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">B\u00ecnh<\/span><\/td><td><span style=\"font-weight: 400;\">7000<\/span><\/td><td><span style=\"font-weight: 400;\">K\u1ef9 thu\u1eadt<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ch\u00e2u<\/span><\/td><td><span style=\"font-weight: 400;\">3000<\/span><\/td><td><span style=\"font-weight: 400;\">Kinh doanh<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">D\u0169ng<\/span><\/td><td><span style=\"font-weight: 400;\">5000<\/span><\/td><td><span style=\"font-weight: 400;\">Kinh doanh<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm c\u00e1c nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng cao h\u01a1n l\u01b0\u01a1ng trung b\u00ecnh c\u1ee7a t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">WHERE Salary &gt; (SELECT AVG(Salary) FROM Employees);<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Subquery:<\/b> <span style=\"font-weight: 400;\">(SELECT AVG(Salary) FROM Employees)<\/span><span style=\"font-weight: 400;\"> t\u00ednh l\u01b0\u01a1ng trung b\u00ecnh c\u1ee7a t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean (k\u1ebft qu\u1ea3 l\u00e0 4750).<\/span><\/li>\n\n\n\n<li><b>Truy v\u1ea5n ch\u00ednh:<\/b><span style=\"font-weight: 400;\"> L\u1ecdc c\u00e1c nh\u00e2n vi\u00ean c\u00f3 <\/span><span style=\"font-weight: 400;\">Salary<\/span><span style=\"font-weight: 400;\"> l\u1edbn h\u01a1n 4750.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-sql-danh-cho-trung-c\u1ea5p\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_SQL_danh_cho_Trung_cap\"><\/span><b>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL d\u00e0nh cho Trung c\u1ea5p<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-co-nh\u1eefng-lo\u1ea1i-index-nao\"><b>C\u00f3 nh\u1eefng lo\u1ea1i index n\u00e0o?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Index trong SQL l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u0103ng t\u1ed1c \u0111\u1ed9 truy v\u1ea5n d\u1eef li\u1ec7u t\u1eeb c\u00e1c b\u1ea3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. Index gi\u00fap t\u00ecm ki\u1ebfm v\u00e0 truy xu\u1ea5t d\u1eef li\u1ec7u nhanh h\u01a1n b\u1eb1ng c\u00e1ch t\u1ed5 ch\u1ee9c d\u1eef li\u1ec7u theo c\u00e1ch d\u1ec5 truy c\u1eadp. Tuy nhi\u00ean, c\u1ea7n l\u01b0u \u00fd r\u1eb1ng c\u00fa ph\u00e1p t\u1ea1o index c\u00f3 th\u1ec3 kh\u00e1c nhau t\u00f9y v\u00e0o h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (RDBMS) m\u00e0 b\u1ea1n \u0111ang s\u1eed d\u1ee5ng, ch\u1eb3ng h\u1ea1n nh\u01b0 MySQL, SQL Server, PostgreSQL ho\u1eb7c Oracle. C\u00e1c v\u00ed d\u1ee5 sau \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong MySQL.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e1c lo\u1ea1i Index ph\u1ed5 bi\u1ebfn trong SQL g\u1ed3m:<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-primary-index-clustered-index\"><b>Primary Index (Clustered Index)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Primary Index, hay c\u00f2n g\u1ecdi l\u00e0 Clustered Index, l\u00e0 lo\u1ea1i index m\u1eb7c \u0111\u1ecbnh \u0111\u01b0\u1ee3c t\u1ea1o ra khi b\u1ea1n khai b\u00e1o m\u1ed9t Primary Key trong b\u1ea3ng. N\u00f3 s\u1eafp x\u1ebfp d\u1eef li\u1ec7u trong b\u1ea3ng theo th\u1ee9 t\u1ef1 v\u1eadt l\u00fd d\u1ef1a tr\u00ean c\u1ed9t \u0111\u01b0\u1ee3c \u0111\u00e1nh index, c\u00f3 ngh\u0129a l\u00e0 d\u1eef li\u1ec7u trong b\u1ea3ng th\u1ef1c s\u1ef1 \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef theo th\u1ee9 t\u1ef1 c\u1ee7a Clustered Index, gi\u00fap vi\u1ec7c truy v\u1ea5n d\u1eef li\u1ec7u theo c\u1ed9t \u0111\u00f3 nhanh ch\u00f3ng h\u01a1n. M\u1ed9t b\u1ea3ng ch\u1ec9 c\u00f3 th\u1ec3 c\u00f3 m\u1ed9t Clustered Index duy nh\u1ea5t v\u00ec n\u00f3 quy\u1ebft \u0111\u1ecbnh th\u1ee9 t\u1ef1 l\u01b0u tr\u1eef v\u1eadt l\u00fd c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 trong MySQL \u0111\u1ec3 t\u1ea1o m\u1ed9t Clustered index gi\u00fap s\u1eafp x\u1ebfp d\u1eef li\u1ec7u trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> theo th\u1ee9 t\u1ef1 c\u1ee7a c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">EmployeeID<\/span><\/i><span style=\"font-weight: 400;\">, gi\u00fap truy v\u1ea5n theo <\/span><i><span style=\"font-weight: 400;\">EmployeeID<\/span><\/i><span style=\"font-weight: 400;\"> nhanh h\u01a1n, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">ALTER TABLE employees<\/span>\n\n<span style=\"font-weight: 400;\">ADD PRIMARY KEY (EmployeeID);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-secondary-index-non-clustered-index\"><b>Secondary Index (Non-Clustered Index):<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Secondary Index, hay Non-Clustered Index, l\u00e0 lo\u1ea1i index \u0111\u01b0\u1ee3c t\u1ea1o ra \u0111\u1ec3 t\u0103ng t\u1ed1c \u0111\u1ed9 truy v\u1ea5n m\u00e0 kh\u00f4ng thay \u0111\u1ed5i th\u1ee9 t\u1ef1 v\u1eadt l\u00fd c\u1ee7a d\u1eef li\u1ec7u trong b\u1ea3ng. Thay v\u00ec s\u1eafp x\u1ebfp d\u1eef li\u1ec7u, Non-Clustered Index t\u1ea1o ra m\u1ed9t b\u1ea3ng ri\u00eang bi\u1ec7t ch\u1ee9a c\u00e1c gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c \u0111\u00e1nh index v\u00e0 c\u00e1c con tr\u1ecf tr\u1ecf \u0111\u1ebfn v\u1ecb tr\u00ed d\u1eef li\u1ec7u th\u1ef1c s\u1ef1 trong b\u1ea3ng ch\u00ednh. V\u00ec v\u1eady, m\u1ed9t b\u1ea3ng c\u00f3 nhi\u1ec1u Non-Clustered Index, h\u1ed7 tr\u1ee3 t\u1ed1i \u01b0u h\u00f3a nhi\u1ec1u truy v\u1ea5n kh\u00e1c nhau.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 trong MySQL \u0111\u1ec3 t\u1ea1o m\u1ed9t non-clustered index gi\u00fap h\u1ed7 tr\u1ee3 t\u0103ng t\u1ed1c c\u00e1c truy v\u1ea5n t\u00ecm ki\u1ebfm theo t\u00ean (<\/span><i><span style=\"font-weight: 400;\">Name<\/span><\/i><span style=\"font-weight: 400;\">) trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\">, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE INDEX idx_employee_name ON employees(Name);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-unique-index\"><b>Unique Index:<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Unique Index l\u00e0 lo\u1ea1i index \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o r\u1eb1ng t\u1ea5t c\u1ea3 c\u00e1c gi\u00e1 tr\u1ecb trong c\u1ed9t \u0111\u01b0\u1ee3c \u0111\u00e1nh index l\u00e0 duy nh\u1ea5t, c\u00f3 ngh\u0129a l\u00e0 kh\u00f4ng th\u1ec3 ch\u00e8n hai h\u00e0ng d\u1eef li\u1ec7u c\u00f3 c\u00f9ng m\u1ed9t gi\u00e1 tr\u1ecb trong c\u1ed9t \u0111\u01b0\u1ee3c \u0111\u00e1nh index. Unique Index th\u01b0\u1eddng \u0111\u01b0\u1ee3c t\u1ea1o t\u1ef1 \u0111\u1ed9ng khi b\u1ea1n khai b\u00e1o Unique Constraint ho\u1eb7c Primary Key ho\u1eb7c c\u0169ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c t\u1ea1o th\u1ee7 c\u00f4ng, gi\u00fap b\u1ea3o v\u1ec7 t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u v\u00e0 t\u0103ng t\u1ed1c \u0111\u1ed9 truy v\u1ea5n d\u1ef1a tr\u00ean c\u1ed9t \u0111\u00f3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 trong MySQL \u0111\u1ec3 t\u1ea1o m\u1ed9t Unique Index gi\u00fap \u0111\u1ea3m b\u1ea3o r\u1eb1ng kh\u00f4ng c\u00f3 hai nh\u00e2n vi\u00ean n\u00e0o trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> c\u00f3 c\u00f9ng \u0111\u1ecba ch\u1ec9 email (<\/span><i><span style=\"font-weight: 400;\">Email<\/span><\/i><span style=\"font-weight: 400;\">, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE UNIQUE INDEX idx_unique_email ON employees(Email);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-composite-index\"><b>Composite Index:<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Composite Index l\u00e0 m\u1ed9t lo\u1ea1i index \u0111\u01b0\u1ee3c t\u1ea1o ra tr\u00ean nhi\u1ec1u c\u1ed9t trong b\u1ea3ng. M\u1ee5c \u0111\u00edch c\u1ee7a Composite Index l\u00e0 t\u1ed1i \u01b0u h\u00f3a c\u00e1c truy v\u1ea5n ph\u1ee9c t\u1ea1p li\u00ean quan \u0111\u1ebfn nhi\u1ec1u c\u1ed9t b\u1eb1ng c\u00e1ch k\u1ebft h\u1ee3p ch\u00fang trong m\u1ed9t index duy nh\u1ea5t. Lo\u1ea1i index n\u00e0y \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch khi truy v\u1ea5n d\u1eef li\u1ec7u th\u01b0\u1eddng xuy\u00ean s\u1eed d\u1ee5ng c\u00e1c \u0111i\u1ec1u ki\u1ec7n tr\u00ean nhi\u1ec1u c\u1ed9t, gi\u00fap gi\u1ea3m th\u1eddi gian x\u1eed l\u00fd d\u1eef li\u1ec7u so v\u1edbi vi\u1ec7c s\u1eed d\u1ee5ng index ri\u00eang l\u1ebb cho t\u1eebng c\u1ed9t.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 trong MySQL \u0111\u1ec3 t\u1ea1o m\u1ed9t Composite Index n\u00e0y gi\u00fap t\u1ed1i \u01b0u h\u00f3a c\u00e1c truy v\u1ea5n s\u1eed d\u1ee5ng c\u1ea3 hai c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">Name<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0 <\/span><i><span style=\"font-weight: 400;\">Department<\/span><\/i><span style=\"font-weight: 400;\"> c\u00f9ng m\u1ed9t l\u00fac, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE INDEX idx_composite_name_department ON employees(Name, Department);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-full-text-index\"><b>Full-Text Index:<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Full-Text Index \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf d\u00e0nh ri\u00eang cho vi\u1ec7c t\u00ecm ki\u1ebfm v\u0103n b\u1ea3n trong c\u00e1c c\u1ed9t ki\u1ec3u chu\u1ed7i, ch\u1eb3ng h\u1ea1n nh\u01b0 <\/span><i><span style=\"font-weight: 400;\">CHAR<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">VARCHAR<\/span><\/i><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><i><span style=\"font-weight: 400;\">TEXT<\/span><\/i><span style=\"font-weight: 400;\">. Lo\u1ea1i index n\u00e0y gi\u00fap t\u0103ng t\u1ed1c c\u00e1c truy v\u1ea5n t\u00ecm ki\u1ebfm ph\u1ee9c t\u1ea1p li\u00ean quan \u0111\u1ebfn t\u1eeb kh\u00f3a, c\u1ee5m t\u1eeb, ho\u1eb7c m\u1eabu v\u0103n b\u1ea3n trong m\u1ed9t b\u1ea3ng l\u1edbn. Full-Text Index th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c \u1ee9ng d\u1ee5ng c\u1ea7n t\u00ecm ki\u1ebfm v\u0103n b\u1ea3n nh\u01b0 c\u00f4ng c\u1ee5 t\u00ecm ki\u1ebfm, trang web ho\u1eb7c c\u00e1c h\u1ec7 th\u1ed1ng qu\u1ea3n l\u00fd n\u1ed9i dung.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u01b0u \u00fd:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Full-Text Index kh\u00f4ng h\u1ed7 tr\u1ee3 tr\u00ean t\u1ea5t c\u1ea3 c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u v\u00e0 c\u00fa ph\u00e1p c\u00f3 th\u1ec3 kh\u00e1c nhau.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Trong MySQL, Full-Text Index ch\u1ec9 h\u1ed7 tr\u1ee3 tr\u00ean c\u00e1c c\u1ed9t ki\u1ec3u <\/span><span style=\"font-weight: 400;\">CHAR<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">VARCHAR<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">TEXT<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t Full-Text Index gi\u00fap t\u0103ng t\u1ed1c c\u00e1c truy v\u1ea5n t\u00ecm ki\u1ebfm t\u1eeb kh\u00f3a ho\u1eb7c c\u1ee5m t\u1eeb trong n\u1ed9i dung (<\/span><i><span style=\"font-weight: 400;\">Content<\/span><\/i><span style=\"font-weight: 400;\">) c\u1ee7a b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Articles<\/span><\/i><span style=\"font-weight: 400;\">, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE FULLTEXT INDEX idx_fulltext_content ON Articles(Content);<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-sao-d\u1ec3-t\u1ed1i-\u01b0u-hoa-truy-v\u1ea5n-sql\"><b>L\u00e0m sao \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a truy v\u1ea5n SQL?<\/b><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>S\u1eed d\u1ee5ng Index hi\u1ec7u qu\u1ea3: <\/b><b>Index<\/b><span style=\"font-weight: 400;\"> gi\u00fap truy v\u1ea5n d\u1eef li\u1ec7u nhanh h\u01a1n b\u1eb1ng c\u00e1ch t\u1ea1o c\u1ea5u tr\u00fac t\u00ecm ki\u1ebfm. H\u00e3y t\u1ea1o c\u00e1c index tr\u00ean c\u00e1c c\u1ed9t \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng th\u01b0\u1eddng xuy\u00ean trong m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">JOIN<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">ORDER BY. <\/span><span style=\"font-weight: 400;\">Tuy nhi\u00ean, c\u1ea7n tr\u00e1nh s\u1eed d\u1ee5ng qu\u00e1 nhi\u1ec1u index, v\u00ec ch\u00fang c\u00f3 th\u1ec3 l\u00e0m ch\u1eadm c\u00e1c thao t\u00e1c ch\u00e8n, c\u1eadp nh\u1eadt, ho\u1eb7c x\u00f3a.<\/span><\/li>\n\n\n\n<li><b>Ch\u1ec9 truy v\u1ea5n c\u00e1c c\u1ed9t c\u1ea7n thi\u1ebft: <\/b><span style=\"font-weight: 400;\">Tr\u00e1nh s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">SELECT *<\/span><span style=\"font-weight: 400;\"> v\u00ec n\u00f3 truy v\u1ea5n to\u00e0n b\u1ed9 c\u00e1c c\u1ed9t, g\u00e2y l\u00e3ng ph\u00ed t\u00e0i nguy\u00ean. Thay v\u00e0o \u0111\u00f3, ch\u1ec9 ch\u1ecdn c\u00e1c c\u1ed9t c\u1ea7n thi\u1ebft.<\/span><\/li>\n\n\n\n<li><b>\u01afu ti\u00ean <\/b><span style=\"font-weight: 400;\">JOIN<\/span><b> thay v\u00ec Subquery: <\/b><span style=\"font-weight: 400;\">JOIN<\/span><span style=\"font-weight: 400;\"> th\u01b0\u1eddng nhanh h\u01a1n Subquery v\u00ec t\u1ed1i \u01b0u h\u00f3a c\u00e1ch x\u1eed l\u00fd d\u1eef li\u1ec7u<\/span><\/li>\n\n\n\n<li><b>Ki\u1ec3m tra k\u1ebf ho\u1ea1ch th\u1ef1c thi (Execution Plan): <\/b><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">EXPLAIN<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 xem c\u01a1 s\u1edf d\u1eef li\u1ec7u x\u1eed l\u00fd truy v\u1ea5n th\u1ebf n\u00e0o, t\u1eeb \u0111\u00f3 \u0111i\u1ec1u ch\u1ec9nh c\u00e2u l\u1ec7nh.<\/span><\/li>\n\n\n\n<li><b>Tr\u00e1nh s\u1eed d\u1ee5ng h\u00e0m trong \u0111i\u1ec1u ki\u1ec7n WHERE<\/b><span style=\"font-weight: 400;\">: Khi s\u1eed d\u1ee5ng c\u00e1c h\u00e0m (nh\u01b0 <\/span><span style=\"font-weight: 400;\">LOWER()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPPER()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">DATE()<\/span><span style=\"font-weight: 400;\">,&#8230;) trong \u0111i\u1ec1u ki\u1ec7n <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, c\u01a1 s\u1edf d\u1eef li\u1ec7u s\u1ebd kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng Index \u0111\u1ec3 t\u0103ng t\u1ed1c truy v\u1ea5n. Thay v\u00e0o \u0111\u00f3, h\u00e3y s\u1eed d\u1ee5ng c\u00e1c \u0111i\u1ec1u ki\u1ec7n so s\u00e1nh tr\u1ef1c ti\u1ebfp n\u1ebfu c\u00f3 th\u1ec3.<\/span><\/li>\n\n\n\n<li><b>S\u1eed d\u1ee5ng <\/b><span style=\"font-weight: 400;\">LIMIT<\/span><b> \u0111\u1ec3 gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi tr\u1ea3 v\u1ec1<\/b><span style=\"font-weight: 400;\">: N\u1ebfu kh\u00f4ng c\u1ea7n t\u1ea5t c\u1ea3 d\u1eef li\u1ec7u, h\u00e3y s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">LIMIT<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi tr\u1ea3 v\u1ec1, gi\u1ea3m t\u1ea3i cho c\u01a1 s\u1edf d\u1eef li\u1ec7u v\u00e0 t\u0103ng t\u1ed1c \u0111\u1ed9 ph\u1ea3n h\u1ed3i.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-sql-va-pl-sql\"><b>Ph\u00e2n bi\u1ec7t SQL v\u00e0 PL\/SQL<\/b><\/h3>\n\n\n\n<p><b>PL\/SQL<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh m\u1edf r\u1ed9ng c\u1ee7a SQL, \u0111\u01b0\u1ee3c Oracle ph\u00e1t tri\u1ec3n. N\u00f3 t\u00edch h\u1ee3p c\u00e1c c\u1ea5u tr\u00fac l\u1eadp tr\u00ecnh nh\u01b0 v\u00f2ng l\u1eb7p, \u0111i\u1ec1u ki\u1ec7n, v\u00e0 h\u00e0m. PL\/SQL \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 x\u1eed l\u00fd logic ph\u1ee9c t\u1ea1p tr\u1ef1c ti\u1ebfp tr\u00ean m\u00e1y ch\u1ee7 c\u01a1 s\u1edf d\u1eef li\u1ec7u Oracle, gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t v\u00e0 gi\u1ea3m t\u01b0\u01a1ng t\u00e1c gi\u1eefa \u1ee9ng d\u1ee5ng v\u00e0 c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><b>Ch\u1ee9c n\u0103ng ch\u00ednh:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c l\u1eadp tr\u00ecnh ph\u1ee9c t\u1ea1p b\u1eb1ng c\u00e1ch k\u1ebft h\u1ee3p SQL v\u1edbi logic l\u1eadp tr\u00ecnh.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 kh\u1ed1i l\u1ec7nh, h\u00e0m, th\u1ee7 t\u1ee5c, v\u00e0 trigger \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a v\u00e0 t\u1ed1i \u01b0u h\u00f3a t\u00e1c v\u1ee5.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa SQL v\u00e0 PL\/SQL g\u1ed3m:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>SQL<\/b><\/td><td><b>PL\/SQL<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch<\/span><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 truy v\u1ea5n v\u00e0 thao t\u00e1c d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 l\u1eadp tr\u00ecnh logic ph\u1ee9c t\u1ea1p k\u1ebft h\u1ee3p SQL v\u00e0 c\u00e1c c\u1ea5u tr\u00fac l\u1eadp tr\u00ecnh.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Lo\u1ea1i ng\u00f4n ng\u1eef<\/span><\/td><td><span style=\"font-weight: 400;\">Khai b\u00e1o (Declarative): M\u00f4 t\u1ea3 d\u1eef li\u1ec7u c\u1ea7n l\u1ea5y ho\u1eb7c thao t\u00e1c.<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u1ee7 t\u1ee5c (Procedural): M\u00f4 t\u1ea3 t\u1eebng b\u01b0\u1edbc x\u1eed l\u00fd logic.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Kh\u1ea3 n\u0103ng x\u1eed l\u00fd<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u1ef1c thi t\u1eebng c\u00e2u l\u1ec7nh m\u1ed9t c\u00e1ch \u0111\u1ed9c l\u1eadp.<\/span><\/td><td><span style=\"font-weight: 400;\">X\u1eed l\u00fd nhi\u1ec1u c\u00e2u l\u1ec7nh trong m\u1ed9t kh\u1ed1i m\u00e3 ho\u1eb7c ch\u01b0\u01a1ng tr\u00ecnh.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">\u1ee8ng d\u1ee5ng<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u00eam, s\u1eeda, x\u00f3a, truy v\u1ea5n d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng h\u00f3a, l\u1eadp tr\u00ecnh trigger, t\u1ea1o h\u00e0m v\u00e0 th\u1ee7 t\u1ee5c trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p h\u1ed7 tr\u1ee3<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 h\u1ed7 tr\u1ee3 c\u00e1c c\u00e2u l\u1ec7nh SQL \u0111\u01a1n gi\u1ea3n, c\u00f9ng v\u1edbi c\u00e1c h\u00e0m t\u00edch h\u1ee3p nh\u01b0 SUM, AVG, COUNT m\u00e0 kh\u00f4ng c\u00f3 v\u00f2ng l\u1eb7p ho\u1eb7c \u0111i\u1ec1u ki\u1ec7n.<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 c\u1ea5u tr\u00fac l\u1eadp tr\u00ecnh nh\u01b0 v\u00f2ng l\u1eb7p, \u0111i\u1ec1u ki\u1ec7n, h\u00e0m, th\u1ee7 t\u1ee5c.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">T\u00ednh n\u0103ng b\u1ed5 sung<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng h\u1ed7 tr\u1ee3 logic l\u1eadp tr\u00ecnh.<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 x\u1eed l\u00fd logic ph\u1ee9c t\u1ea1p v\u00e0 t\u00edch h\u1ee3p h\u00e0m ho\u1eb7c trigger.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ph\u1ea1m vi h\u1ed7 tr\u1ee3<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 b\u1edfi h\u1ea7u h\u1ebft c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (MySQL, SQL Server, PostgreSQL&#8230;).<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 trong Oracle Database.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 logic l\u1eadp tr\u00ecnh<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng h\u1ed7 tr\u1ee3.<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 h\u1ed7 tr\u1ee3 v\u1edbi c\u00e1c kh\u1ed1i m\u00e3 l\u1eadp tr\u00ecnh logic.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-stored-procedure-va-function-khac-nhau-nh\u01b0-th\u1ebf-nao\"><b>Stored Procedure v\u00e0 Function kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n\n\n\n<p><b>Th\u1ee7 t\u1ee5c (Stored Procedure)<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh SQL v\u00e0 logic l\u1eadp tr\u00ecnh \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong c\u01a1 s\u1edf d\u1eef li\u1ec7u \u0111\u1ec3 th\u1ef1c thi m\u1ed9t t\u00e1c v\u1ee5 c\u1ee5 th\u1ec3. Th\u1ee7 t\u1ee5c c\u00f3 th\u1ec3 ch\u1ee9a nhi\u1ec1u c\u00e2u l\u1ec7nh v\u00e0 cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c ph\u1ee9c t\u1ea1p.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a th\u1ee7 t\u1ee5c:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 nhi\u1ec1u gi\u00e1 tr\u1ecb th\u00f4ng qua c\u00e1c tham s\u1ed1 \u0111\u1ea7u ra (<\/span><span style=\"font-weight: 400;\">OUT<\/span><span style=\"font-weight: 400;\"> parameters).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 c\u00e1c c\u00e2u l\u1ec7nh <\/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;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ho\u1eb7c quy tr\u00ecnh.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Kh\u00f4ng b\u1eaft bu\u1ed9c tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>Function<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t kh\u1ed1i m\u00e3 l\u1ec7nh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t t\u00e1c v\u1ee5 c\u1ee5 th\u1ec3 v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t. Functions th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c bi\u1ec3u th\u1ee9c v\u00e0 c\u00e2u l\u1ec7nh SQL.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a Function:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">B\u1eaft bu\u1ed9c ph\u1ea3i tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb (s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">RETURN<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c \u0111\u1ecdc d\u1eef li\u1ec7u (kh\u00f4ng h\u1ed7 tr\u1ee3 <\/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;\">).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c ph\u00e9p t\u00ednh ho\u1eb7c bi\u1ec3u th\u1ee9c.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-s\u1ef1-khac-bi\u1ec7t-chinh-gi\u1eefa-stored-procedure-va-function\"><b>S\u1ef1 kh\u00e1c bi\u1ec7t ch\u00ednh gi\u1eefa Stored Procedure v\u00e0 Function:<\/b><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>Stored Procedure<\/b><\/td><td><b>Function<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p, c\u00f3 th\u1ec3 bao g\u1ed3m thay \u0111\u1ed5i d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">T\u00ednh to\u00e1n ho\u1eb7c tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t d\u1ef1a tr\u00ean \u0111\u1ea7u v\u00e0o.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng b\u1eaft bu\u1ed9c gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1, c\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 nhi\u1ec1u gi\u00e1 tr\u1ecb th\u00f4ng qua tham s\u1ed1 <\/span><span style=\"font-weight: 400;\">OUT<\/span><span style=\"font-weight: 400;\">.<\/span><\/td><td><span style=\"font-weight: 400;\">B\u1eaft bu\u1ed9c tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 DML<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 c\u00e1c thao t\u00e1c d\u1eef li\u1ec7u nh\u01b0 INSERT, <\/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;\">, <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">.<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 h\u1ed7 tr\u1ee3 \u0111\u1ecdc d\u1eef li\u1ec7u (<\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng trong SQL<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng trong c\u00e1c c\u00e2u l\u1ec7nh SQL nh\u01b0 SELECT, WHERE.<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 s\u1eed d\u1ee5ng tr\u1ef1c ti\u1ebfp trong c\u00e1c c\u00e2u l\u1ec7nh SQL.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Tham s\u1ed1<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 c\u1ea3 tham s\u1ed1 <\/span><span style=\"font-weight: 400;\">IN<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">OUT<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">INOUT<\/span><span style=\"font-weight: 400;\">.<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 h\u1ed7 tr\u1ee3 tham s\u1ed1 \u0111\u1ea7u v\u00e0o (<\/span><span style=\"font-weight: 400;\">IN<\/span><span style=\"font-weight: 400;\">).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ph\u1ea1m vi s\u1eed d\u1ee5ng<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u1eed l\u00fd logic v\u00e0 t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p.<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng ch\u1ee7 y\u1ebfu cho c\u00e1c ph\u00e9p to\u00e1n ho\u1eb7c truy v\u1ea5n \u0111\u01a1n gi\u1ea3n.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cte-common-table-expression-la-gi-khi-nao-nen-dung\"><b>CTE (Common Table Expression) l\u00e0 g\u00ec? Khi n\u00e0o n\u00ean d\u00f9ng?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">CTE (Common Table Expression) l\u00e0 m\u1ed9t bi\u1ec3u th\u1ee9c t\u1ea1m th\u1eddi trong SQL, cho ph\u00e9p \u0111\u1ecbnh ngh\u0129a m\u1ed9t t\u1eadp h\u1ee3p k\u1ebft qu\u1ea3 truy v\u1ea5n c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c tham chi\u1ebfu nhi\u1ec1u l\u1ea7n trong m\u1ed9t c\u00e2u l\u1ec7nh SQL. CTE \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a b\u1eb1ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">WITH<\/span><span style=\"font-weight: 400;\"> v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u00e0m cho truy v\u1ea5n ph\u1ee9c t\u1ea1p tr\u1edf n\u00ean d\u1ec5 \u0111\u1ecdc v\u00e0 d\u1ec5 duy tr\u00ec h\u01a1n. CTE kh\u00f4ng l\u01b0u tr\u1eef d\u1eef li\u1ec7u v\u1eadt l\u00fd, m\u00e0 ch\u1ec9 t\u1ed3n t\u1ea1i trong ph\u1ea1m vi c\u1ee7a c\u00e2u l\u1ec7nh SQL m\u00e0 n\u00f3 \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">WITH CTE_Name (Column1, Column2, ...)<\/span>\n\n<span style=\"font-weight: 400;\">AS (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;SELECT Column1, Column2, ...<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;FROM Table_Name<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;WHERE Condition<\/span>\n\n<span style=\"font-weight: 400;\">)<\/span>\n\n<span style=\"font-weight: 400;\">SELECT *<\/span>\n\n<span style=\"font-weight: 400;\">FROM CTE_Name<\/span>\n\n<span style=\"font-weight: 400;\">WHERE Another_Condition;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">WITH CTE_Name<\/span><span style=\"font-weight: 400;\">: \u0110\u1ecbnh ngh\u0129a t\u00ean c\u1ee7a CTE v\u00e0 danh s\u00e1ch c\u1ed9t (t\u00f9y ch\u1ecdn).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">AS<\/span><span style=\"font-weight: 400;\">: M\u1edf \u0111\u1ea7u kh\u1ed1i truy v\u1ea5n cho CTE.<\/span><\/li>\n\n\n\n<li><b>Truy v\u1ea5n b\u00ean trong<\/b><span style=\"font-weight: 400;\">: Truy v\u1ea5n tr\u1ea3 v\u1ec1 t\u1eadp k\u1ebft qu\u1ea3 t\u1ea1m th\u1eddi.<\/span><\/li>\n\n\n\n<li><b>Truy v\u1ea5n b\u00ean ngo\u00e0i<\/b><span style=\"font-weight: 400;\">: S\u1eed d\u1ee5ng CTE nh\u01b0 m\u1ed9t b\u1ea3ng t\u1ea1m trong c\u00e1c truy v\u1ea5n ti\u1ebfp theo.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 gi\u1ea3 s\u1eed b\u1ea1n mu\u1ed1n t\u00ecm ra c\u00e1c ph\u00f2ng ban c\u00f3 t\u1ed5ng l\u01b0\u01a1ng l\u1edbn h\u01a1n 50,000 v\u00e0 hi\u1ec3n th\u1ecb danh s\u00e1ch nh\u00e2n vi\u00ean trong c\u00e1c ph\u00f2ng ban \u0111\u00f3, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">WITH DepartmentSalary AS (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;SELECT DepartmentID, SUM(Salary) AS TotalSalary<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;GROUP BY DepartmentID<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;HAVING SUM(Salary) &gt; 50000<\/span>\n\n<span style=\"font-weight: 400;\">)<\/span>\n\n<span style=\"font-weight: 400;\">SELECT E.Name, E.Salary, D.TotalSalary<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees E<\/span>\n\n<span style=\"font-weight: 400;\">INNER JOIN DepartmentSalary D<\/span>\n\n<span style=\"font-weight: 400;\">ON E.DepartmentID = D.DepartmentID;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">DepartmentSalary<\/span><span style=\"font-weight: 400;\"> l\u00e0 CTE t\u00ednh t\u1ed5ng l\u01b0\u01a1ng theo t\u1eebng ph\u00f2ng ban.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Truy v\u1ea5n b\u00ean ngo\u00e0i s\u1eed d\u1ee5ng CTE \u0111\u1ec3 t\u00ecm nh\u00e2n vi\u00ean thu\u1ed9c c\u00e1c ph\u00f2ng ban c\u00f3 t\u1ed5ng l\u01b0\u01a1ng l\u1edbn h\u01a1n 50,000.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-khi-nao-nen-dung-cte\"><b>Khi n\u00e0o n\u00ean d\u00f9ng CTE?<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Khi c\u1ea7n l\u00e0m r\u00f5 truy v\u1ea5n ph\u1ee9c t\u1ea1p<\/b><span style=\"font-weight: 400;\">: N\u1ebfu truy v\u1ea5n c\u1ee7a b\u1ea1n qu\u00e1 ph\u1ee9c t\u1ea1p v\u1edbi nhi\u1ec1u ph\u00e9p n\u1ed1i (<\/span><span style=\"font-weight: 400;\">JOIN<\/span><span style=\"font-weight: 400;\">), nh\u00f3m (<\/span><span style=\"font-weight: 400;\">GROUP<\/span><span style=\"font-weight: 400;\"> BY), ho\u1eb7c \u0111i\u1ec1u ki\u1ec7n, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng CTE \u0111\u1ec3 chia nh\u1ecf t\u1eebng b\u01b0\u1edbc.<\/span><\/li>\n\n\n\n<li><b>Khi c\u1ea7n s\u1eed d\u1ee5ng l\u1ea1i k\u1ebft qu\u1ea3 t\u1ea1m th\u1eddi<\/b><span style=\"font-weight: 400;\">: N\u1ebfu m\u1ed9t t\u1eadp k\u1ebft qu\u1ea3 c\u1ea7n \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nhi\u1ec1u l\u1ea7n trong c\u00f9ng m\u1ed9t truy v\u1ea5n, CTE gi\u00fap b\u1ea1n t\u00e1i s\u1eed d\u1ee5ng k\u1ebft qu\u1ea3 t\u1ea1m th\u1eddi m\u00e0 kh\u00f4ng ph\u1ea3i l\u1eb7p l\u1ea1i c\u00f9ng m\u1ed9t m\u00e3 truy v\u1ea5n.<\/span><\/li>\n\n\n\n<li><b>Khi c\u1ea7n truy v\u1ea5n \u0111\u1ec7 quy<\/b><span style=\"font-weight: 400;\">: CTE h\u1ed7 tr\u1ee3 truy v\u1ea5n \u0111\u1ec7 quy, th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u c\u00f3 c\u1ea5u tr\u00fac c\u00e2y (hierarchical data), nh\u01b0 c\u00e2y th\u01b0 m\u1ee5c ho\u1eb7c m\u1ed1i quan h\u1ec7 cha-con.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-khi-nao-nen-tranh-dung-cte\"><b>Khi n\u00e0o n\u00ean tr\u00e1nh d\u00f9ng CTE?<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Khi truy v\u1ea5n \u0111\u01a1n gi\u1ea3n v\u00e0 kh\u00f4ng c\u1ea7n chia nh\u1ecf.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">V\u1edbi c\u00e1c b\u1ea3ng l\u1edbn, n\u1ebfu hi\u1ec7u su\u1ea5t l\u00e0 \u01b0u ti\u00ean, h\u00e3y ki\u1ec3m tra v\u00ec CTE kh\u00f4ng lu\u00f4n t\u1ed1i \u01b0u so v\u1edbi vi\u1ec7c d\u00f9ng Temporary Table ho\u1eb7c Subquery.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-explain-plan-query-execution-plan-la-gi\"><b>Explain Plan\/Query Execution Plan l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Explain Plan ho\u1eb7c Query Execution Plan l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 trong SQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 ph\u00e2n t\u00edch v\u00e0 hi\u1ec3u c\u00e1ch c\u01a1 s\u1edf d\u1eef li\u1ec7u th\u1ef1c thi m\u1ed9t truy v\u1ea5n SQL. N\u00f3 cung c\u1ea5p th\u00f4ng tin chi ti\u1ebft v\u1ec1 c\u00e1c b\u01b0\u1edbc m\u00e0 h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (DBMS) s\u1ebd th\u1ef1c hi\u1ec7n \u0111\u1ec3 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3, gi\u00fap ng\u01b0\u1eddi d\u00f9ng t\u1ed1i \u01b0u h\u00f3a truy v\u1ea5n v\u00e0 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cach-ho\u1ea1t-d\u1ed9ng-c\u1ee7a-explain-plan\"><b>C\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a Explain Plan<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Khi m\u1ed9t truy v\u1ea5n \u0111\u01b0\u1ee3c g\u1eedi \u0111\u1ebfn c\u01a1 s\u1edf d\u1eef li\u1ec7u, DBMS s\u1ebd t\u1ea1o ra m\u1ed9t &#8220;k\u1ebf ho\u1ea1ch th\u1ef1c thi&#8221; (Execution Plan). K\u1ebf ho\u1ea1ch n\u00e0y bao g\u1ed3m:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Th\u1ee9 t\u1ef1 c\u00e1c b\u01b0\u1edbc th\u1ef1c hi\u1ec7n truy v\u1ea5n.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng ph\u00e1p truy xu\u1ea5t d\u1eef li\u1ec7u (qu\u00e9t b\u1ea3ng, s\u1eed d\u1ee5ng index ho\u1eb7c join).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">\u01af\u1edbc t\u00ednh v\u1ec1 s\u1ed1 h\u00e0ng \u0111\u01b0\u1ee3c x\u1eed l\u00fd \u1edf m\u1ed7i b\u01b0\u1edbc.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Chi ph\u00ed (cost) c\u1ee7a t\u1eebng b\u01b0\u1edbc, bi\u1ec3u th\u1ecb t\u00e0i nguy\u00ean s\u1eed d\u1ee5ng (CPU, b\u1ed9 nh\u1edb, I\/O).<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-thong-tin-chinh-trong-explain-plan-g\u1ed3m\"><b>Th\u00f4ng tin ch\u00ednh trong Explain Plan g\u1ed3m<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Operation\/Step:<\/b><span style=\"font-weight: 400;\"> C\u00e1c thao t\u00e1c m\u00e0 DBMS s\u1ebd th\u1ef1c hi\u1ec7n (qu\u00e9t b\u1ea3ng, s\u1eed d\u1ee5ng index, hash join&#8230;).<\/span><\/li>\n\n\n\n<li><b>Cost:<\/b><span style=\"font-weight: 400;\"> Chi ph\u00ed \u01b0\u1edbc t\u00ednh c\u1ee7a m\u1ed7i b\u01b0\u1edbc, gi\u00fap x\u00e1c \u0111\u1ecbnh b\u01b0\u1edbc n\u00e0o t\u1ed1n nhi\u1ec1u t\u00e0i nguy\u00ean nh\u1ea5t.<\/span><\/li>\n\n\n\n<li><b>Rows:<\/b><span style=\"font-weight: 400;\"> S\u1ed1 h\u00e0ng d\u1ef1 ki\u1ebfn s\u1ebd \u0111\u01b0\u1ee3c x\u1eed l\u00fd trong m\u1ed7i b\u01b0\u1edbc.<\/span><\/li>\n\n\n\n<li><b>Key\/Index Used:<\/b><span style=\"font-weight: 400;\"> Ch\u1ec9 ra c\u00f3 s\u1eed d\u1ee5ng index kh\u00f4ng v\u00e0 index n\u00e0o \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><b>Filter:<\/b><span style=\"font-weight: 400;\"> \u0110i\u1ec1u ki\u1ec7n l\u1ecdc d\u1eef li\u1ec7u trong t\u1eebng b\u01b0\u1edbc.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-l\u1ec7nh-d\u1ec3-xem-k\u1ebf-ho\u1ea1ch-th\u1ef1c-thi\"><b>L\u1ec7nh \u0111\u1ec3 xem k\u1ebf ho\u1ea1ch th\u1ef1c thi:<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong MySQL:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">EXPLAIN SELECT * FROM Employees WHERE Salary &gt; 5000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong PostgreSQL:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">EXPLAIN SELECT * FROM Employees WHERE Salary &gt; 5000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong <\/span><b>SQL Server<\/b><span style=\"font-weight: 400;\">: S\u1eed d\u1ee5ng &#8220;Display Estimated Execution Plan&#8221; trong giao di\u1ec7n.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 trong MySQL ta c\u00f3 truy v\u1ea5n m\u1eabu sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">EXPLAIN SELECT Name, Salary&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">WHERE DepartmentID = 2 AND Salary &gt; 5000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 s\u1ebd tr\u1ea3 v\u1ec1:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>ID<\/b><\/td><td><b>Select Type<\/b><\/td><td><b>Table<\/b><\/td><td><b>Type<\/b><\/td><td><b>Possible Keys<\/b><\/td><td><b>Key<\/b><\/td><td><b>Rows<\/b><\/td><td><b>Extra<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">SIMPLE<\/span><\/td><td><span style=\"font-weight: 400;\">Employees<\/span><\/td><td><span style=\"font-weight: 400;\">range<\/span><\/td><td><span style=\"font-weight: 400;\">DepartmentID<\/span><\/td><td><span style=\"font-weight: 400;\">DepartmentID<\/span><\/td><td><span style=\"font-weight: 400;\">100<\/span><\/td><td><span style=\"font-weight: 400;\">Using where<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Type = range:<\/b><span style=\"font-weight: 400;\"> S\u1eed d\u1ee5ng index tr\u00ean c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">DepartmentID<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u00ecm c\u00e1c h\u00e0ng ph\u00f9 h\u1ee3p.<\/span><\/li>\n\n\n\n<li><b>Rows = 100:<\/b><span style=\"font-weight: 400;\"> \u01af\u1edbc t\u00ednh s\u1ebd x\u1eed l\u00fd 100 h\u00e0ng.<\/span><\/li>\n\n\n\n<li><b>Extra = Using where:<\/b><span style=\"font-weight: 400;\"> \u0110i\u1ec1u ki\u1ec7n l\u1ecdc <\/span><span style=\"font-weight: 400;\">Salary > 5000<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ebf-nao-la-view-trong-sql-l\u1ee3i-ich-va-h\u1ea1n-ch\u1ebf-c\u1ee7a-view\"><b>Th\u1ebf n\u00e0o l\u00e0 View trong SQL? L\u1ee3i \u00edch v\u00e0 h\u1ea1n ch\u1ebf c\u1ee7a View.<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">View trong SQL l\u00e0 m\u1ed9t b\u1ea3ng \u1ea3o (virtual table) \u0111\u01b0\u1ee3c t\u1ea1o d\u1ef1a tr\u00ean k\u1ebft qu\u1ea3 c\u1ee7a m\u1ed9t truy v\u1ea5n SQL. View kh\u00f4ng l\u01b0u tr\u1eef d\u1eef li\u1ec7u th\u1ef1c t\u1ebf, m\u00e0 ch\u1ec9 l\u01b0u tr\u1eef c\u00e2u l\u1ec7nh truy v\u1ea5n SQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u1ea1o n\u00f3. Khi truy v\u1ea5n View, c\u01a1 s\u1edf d\u1eef li\u1ec7u s\u1ebd th\u1ef1c thi c\u00e2u l\u1ec7nh SQL g\u1ed1c \u0111\u1ec3 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3. M\u1ed9t s\u1ed1 h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (nh\u01b0 SQL Server) h\u1ed7 tr\u1ee3 Indexed View ho\u1eb7c Materialized View, n\u01a1i View c\u00f3 th\u1ec3 l\u01b0u tr\u1eef d\u1eef li\u1ec7u th\u1ef1c t\u1ebf \u0111\u1ec3 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE VIEW ViewName AS<\/span>\n\n<span style=\"font-weight: 400;\">SELECT column1, column2, ...<\/span>\n\n<span style=\"font-weight: 400;\">FROM table_name<\/span>\n\n<span style=\"font-weight: 400;\">WHERE condition;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t View \u0111\u1ec3 hi\u1ec3n th\u1ecb c\u00e1c nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng cao h\u01a1n 5000, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE VIEW HighSalaryEmployees AS<\/span>\n\n<span style=\"font-weight: 400;\">SELECT Name, Salary<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">WHERE Salary &gt; 5000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 truy v\u1ea5n t\u1ea5t c\u1ea3 c\u00e1c nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng cao h\u01a1n 5000 t\u1eeb View <\/span><i><span style=\"font-weight: 400;\">HighSalaryEmployees<\/span><\/i><span style=\"font-weight: 400;\"> ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT *&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM HighSalaryEmployees;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 truy v\u1ea5n ch\u1ec9 t\u00ean c\u1ee7a c\u00e1c nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng cao h\u01a1n 7000:&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">FROM HighSalaryEmployees<\/span>\n\n<span style=\"font-weight: 400;\">WHERE Salary &gt; 7000;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: Gi\u1ea3 s\u1eed b\u1ea1n c\u00f3 th\u00eam b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Departments<\/span><\/i><span style=\"font-weight: 400;\"> l\u01b0u th\u00f4ng tin ph\u00f2ng ban, b\u1ea1n c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p View <\/span><i><span style=\"font-weight: 400;\">HighSalaryEmployees<\/span><\/i><span style=\"font-weight: 400;\"> v\u1edbi b\u1ea3ng n\u00e0y \u0111\u1ec3 l\u1ea5y th\u00f4ng tin ph\u00f2ng ban c\u1ee7a c\u00e1c nh\u00e2n vi\u00ean l\u01b0\u01a1ng cao v\u1edbi c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT e.Name, e.Salary, d.DepartmentName<\/span>\n\n<span style=\"font-weight: 400;\">FROM HighSalaryEmployees e<\/span>\n\n<span style=\"font-weight: 400;\">JOIN Departments d ON e.DepartmentID = d.ID;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: \u0110\u1ec3 t\u00ednh t\u1ed5ng l\u01b0\u01a1ng c\u1ee7a t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng cao h\u01a1n 5000 (d\u1ef1a tr\u00ean View <\/span><i><span style=\"font-weight: 400;\">HighSalaryEmployees<\/span><\/i><span style=\"font-weight: 400;\">) ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT SUM(Salary) AS TotalHighSalary<\/span>\n\n<span style=\"font-weight: 400;\">FROM HighSalaryEmployees;<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-l\u1ee3i-ich-c\u1ee7a-view\"><b>L\u1ee3i \u00edch c\u1ee7a View<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>\u0110\u01a1n gi\u1ea3n h\u00f3a truy v\u1ea5n ph\u1ee9c t\u1ea1p: <\/b><span style=\"font-weight: 400;\">View gi\u00fap l\u01b0u tr\u1eef c\u00e1c truy v\u1ea5n SQL ph\u1ee9c t\u1ea1p, gi\u00fap ng\u01b0\u1eddi d\u00f9ng kh\u00f4ng c\u1ea7n vi\u1ebft l\u1ea1i c\u00e1c c\u00e2u l\u1ec7nh d\u00e0i d\u00f2ng khi c\u1ea7n s\u1eed d\u1ee5ng l\u1ea1i.<\/span><\/li>\n\n\n\n<li><b>T\u0103ng t\u00ednh b\u1ea3o m\u1eadt d\u1eef li\u1ec7u: <\/b><span style=\"font-weight: 400;\">View cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng truy c\u1eadp v\u00e0o c\u00e1c d\u1eef li\u1ec7u c\u1ee5 th\u1ec3 m\u00e0 kh\u00f4ng c\u1ea7n quy\u1ec1n truy c\u1eadp tr\u1ef1c ti\u1ebfp v\u00e0o b\u1ea3ng g\u1ed1c. V\u00ed d\u1ee5, ch\u1ec9 hi\u1ec3n th\u1ecb m\u1ed9t s\u1ed1 c\u1ed9t trong b\u1ea3ng m\u00e0 kh\u00f4ng \u0111\u1ec3 l\u1ed9 t\u1ea5t c\u1ea3 d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>T\u00ednh t\u00e1i s\u1eed d\u1ee5ng cao: <\/b><span style=\"font-weight: 400;\">View c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nhi\u1ec1u l\u1ea7n trong c\u00e1c truy v\u1ea5n kh\u00e1c, ti\u1ebft ki\u1ec7m th\u1eddi gian v\u00e0 gi\u1ea3m l\u1ed7i khi l\u1eb7p l\u1ea1i c\u00f9ng m\u1ed9t logic truy v\u1ea5n.<\/span><\/li>\n\n\n\n<li><b>T\u00ednh \u0111\u1ed9c l\u1eadp v\u1edbi d\u1eef li\u1ec7u: <\/b><span style=\"font-weight: 400;\">N\u1ebfu c\u1ea5u tr\u00fac b\u1ea3ng g\u1ed1c thay \u0111\u1ed5i, ch\u1ec9 c\u1ea7n c\u1eadp nh\u1eadt c\u00e2u l\u1ec7nh truy v\u1ea5n trong View m\u00e0 kh\u00f4ng c\u1ea7n s\u1eeda \u0111\u1ed5i c\u00e1c truy v\u1ea5n li\u00ean quan kh\u00e1c.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-h\u1ea1n-ch\u1ebf-c\u1ee7a-view\"><b>H\u1ea1n ch\u1ebf c\u1ee7a view<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Hi\u1ec7u su\u1ea5t c\u00f3 th\u1ec3 b\u1ecb \u1ea3nh h\u01b0\u1edfng: <\/b><span style=\"font-weight: 400;\">V\u00ec View kh\u00f4ng l\u01b0u tr\u1eef d\u1eef li\u1ec7u th\u1ef1c t\u1ebf, m\u1ed7i khi truy v\u1ea5n View, c\u01a1 s\u1edf d\u1eef li\u1ec7u ph\u1ea3i th\u1ef1c thi l\u1ea1i c\u00e2u l\u1ec7nh g\u1ed1c, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 l\u00e0m ch\u1eadm hi\u1ec7u su\u1ea5t v\u1edbi c\u00e1c View ph\u1ee9c t\u1ea1p ho\u1eb7c khi truy v\u1ea5n tr\u00ean t\u1eadp d\u1eef li\u1ec7u l\u1edbn.<\/span><\/li>\n\n\n\n<li><b>Kh\u00f4ng th\u1ec3 s\u1eeda \u0111\u1ed5i d\u1eef li\u1ec7u tr\u1ef1c ti\u1ebfp trong m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p: <\/b><span style=\"font-weight: 400;\">N\u1ebfu View \u0111\u01b0\u1ee3c t\u1ea1o t\u1eeb nhi\u1ec1u b\u1ea3ng ho\u1eb7c s\u1eed d\u1ee5ng c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p (<\/span><span style=\"font-weight: 400;\">SUM<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">AVG<\/span><span style=\"font-weight: 400;\">, &#8230;), vi\u1ec7c ch\u00e8n, x\u00f3a ho\u1eb7c c\u1eadp nh\u1eadt d\u1eef li\u1ec7u qua View s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3.<\/span><\/li>\n\n\n\n<li><b>Ph\u1ee5 thu\u1ed9c v\u00e0o b\u1ea3ng g\u1ed1c: <\/b><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea3ng g\u1ed1c b\u1ecb thay \u0111\u1ed5i ho\u1eb7c x\u00f3a, View c\u0169ng s\u1ebd kh\u00f4ng ho\u1ea1t \u0111\u1ed9ng \u0111\u00fang ho\u1eb7c b\u1ecb m\u1ea5t.<\/span><\/li>\n\n\n\n<li><b>H\u1ea1n ch\u1ebf trong vi\u1ec7c \u00e1p d\u1ee5ng Index: <\/b><span style=\"font-weight: 400;\">View kh\u00f4ng h\u1ed7 tr\u1ee3 tr\u1ef1c ti\u1ebfp Index, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 l\u00e0m gi\u1ea3m hi\u1ec7u su\u1ea5t n\u1ebfu c\u1ea7n truy c\u1eadp nhanh v\u00e0o d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>Kh\u00f4ng ph\u00f9 h\u1ee3p v\u1edbi d\u1eef li\u1ec7u \u0111\u1ed9ng<\/b><span style=\"font-weight: 400;\">: View kh\u00f4ng t\u1ef1 \u0111\u1ed9ng ph\u1ea3n \u00e1nh c\u00e1c thay \u0111\u1ed5i trong logic ho\u1eb7c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u m\u1edbi. N\u1ebfu b\u1ea3ng g\u1ed1c thay \u0111\u1ed5i (v\u00ed d\u1ee5: th\u00eam ho\u1eb7c x\u00f3a c\u1ed9t), View s\u1ebd kh\u00f4ng t\u1ef1 c\u1eadp nh\u1eadt \u0111\u1ec3 ph\u1ea3n \u00e1nh nh\u1eefng thay \u0111\u1ed5i n\u00e0y. Khi \u0111\u00f3, c\u1ea7n ch\u1ec9nh s\u1eeda ho\u1eb7c t\u1ea1o l\u1ea1i View \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o ho\u1ea1t \u0111\u1ed9ng \u0111\u00fang.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-normalize-trong-sql-la-gi\"><b>Gi\u1ea3i th\u00edch Normalize trong SQL l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Normalize (chu\u1ea9n h\u00f3a) trong SQL l\u00e0 m\u1ed9t quy tr\u00ecnh t\u1ed5 ch\u1ee9c d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u \u0111\u1ec3 gi\u1ea3m thi\u1ec3u s\u1ef1 d\u01b0 th\u1eeba v\u00e0 tr\u00e1nh c\u00e1c b\u1ea5t th\u01b0\u1eddng (anomalies) trong vi\u1ec7c th\u00eam, x\u00f3a ho\u1eb7c c\u1eadp nh\u1eadt d\u1eef li\u1ec7u. Chu\u1ea9n h\u00f3a chia d\u1eef li\u1ec7u th\u00e0nh c\u00e1c b\u1ea3ng nh\u1ecf h\u01a1n v\u00e0 thi\u1ebft l\u1eadp c\u00e1c m\u1ed1i quan h\u1ec7 gi\u1eefa ch\u00fang \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n v\u00e0 hi\u1ec7u qu\u1ea3.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-l\u1ee3i-ich-c\u1ee7a-normalize-nbsp\"><b>L\u1ee3i \u00edch c\u1ee7a Normalize:&nbsp;<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Lo\u1ea1i b\u1ecf s\u1ef1 d\u01b0 th\u1eeba d\u1eef li\u1ec7u<\/b><span style=\"font-weight: 400;\">: \u0110\u1ea3m b\u1ea3o r\u1eb1ng m\u1ed7i d\u1eef li\u1ec7u ch\u1ec9 \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef \u1edf m\u1ed9t n\u01a1i duy nh\u1ea5t.<\/span><\/li>\n\n\n\n<li><b>\u0110\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u:<\/b><span style=\"font-weight: 400;\"> Tr\u00e1nh t\u00ecnh tr\u1ea1ng d\u1eef li\u1ec7u kh\u00f4ng nh\u1ea5t qu\u00e1n khi thay \u0111\u1ed5i.<\/span><\/li>\n\n\n\n<li><b>T\u0103ng c\u01b0\u1eddng hi\u1ec7u qu\u1ea3 l\u01b0u tr\u1eef:<\/b><span style=\"font-weight: 400;\"> T\u1ed1i \u01b0u h\u00f3a vi\u1ec7c s\u1eed d\u1ee5ng b\u1ed9 nh\u1edb b\u1eb1ng c\u00e1ch lo\u1ea1i b\u1ecf c\u00e1c d\u1eef li\u1ec7u th\u1eeba.<\/span><\/li>\n\n\n\n<li><b>T\u0103ng kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng<\/b><span style=\"font-weight: 400;\">: D\u1ec5 d\u00e0ng th\u00eam ho\u1eb7c thay \u0111\u1ed5i d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng c\u1ea7n ch\u1ec9nh s\u1eeda nhi\u1ec1u b\u1ea3ng.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cac-d\u1ea1ng-chu\u1ea9n-hoa\"><b>C\u00e1c d\u1ea1ng chu\u1ea9n h\u00f3a<\/b><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>D\u1ea1ng chu\u1ea9n ho\u00e1<\/b><\/td><td><b>M\u00f4 t\u1ea3<\/b><\/td><td><b>\u0110i\u1ec1u ki\u1ec7n c\u1ea7n thi\u1ebft<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">1NF (First Normal Form)<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o m\u1ed7i \u00f4 ch\u1ec9 ch\u1ee9a m\u1ed9t gi\u00e1 tr\u1ecb (atomic values) v\u00e0 kh\u00f4ng c\u00f3 nh\u00f3m gi\u00e1 tr\u1ecb l\u1eb7p (repeating groups).<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211; M\u1ed7i c\u1ed9t ch\u1ee9a d\u1eef li\u1ec7u d\u1ea1ng nguy\u00ean t\u1eed.&nbsp;<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; Kh\u00f4ng c\u00f3 h\u00e0ng ho\u1eb7c nh\u00f3m d\u1eef li\u1ec7u l\u1eb7p l\u1ea1i.<\/span><\/p>\n<\/td><\/tr><tr><td><span style=\"font-weight: 400;\">2NF (Second Normal Form)<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o b\u1ea3ng \u1edf d\u1ea1ng 1NF v\u00e0 lo\u1ea1i b\u1ecf c\u00e1c ph\u1ee5 thu\u1ed9c kh\u00f4ng \u0111\u1ea7y \u0111\u1ee7 (partial dependency).<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211; D\u1eef li\u1ec7u \u0111\u00e3 \u1edf d\u1ea1ng 1NF.&nbsp;<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; C\u00e1c c\u1ed9t kh\u00f4ng ph\u1ea3i kh\u00f3a c\u1ea7n ph\u1ea3i ph\u1ee5 thu\u1ed9c ho\u00e0n to\u00e0n v\u00e0o to\u00e0n b\u1ed9 kh\u00f3a ch\u00ednh.<\/span><\/p>\n<\/td><\/tr><tr><td><span style=\"font-weight: 400;\">3NF (Third Normal Form)<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o b\u1ea3ng \u1edf d\u1ea1ng 2NF v\u00e0 lo\u1ea1i b\u1ecf c\u00e1c ph\u1ee5 thu\u1ed9c b\u1eafc c\u1ea7u (transitive dependency).<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211; D\u1eef li\u1ec7u \u0111\u00e3 \u1edf d\u1ea1ng 2NF.&nbsp;<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; Kh\u00f4ng c\u00f3 c\u1ed9t kh\u00f4ng ph\u1ea3i kh\u00f3a ph\u1ee5 thu\u1ed9c v\u00e0o m\u1ed9t c\u1ed9t kh\u00f4ng ph\u1ea3i kh\u00f3a kh\u00e1c.<\/span><\/p>\n<\/td><\/tr><tr><td><span style=\"font-weight: 400;\">BCNF (Boyce-Codd Normal Form)<\/span><\/td><td><span style=\"font-weight: 400;\">L\u00e0 m\u1ed9t d\u1ea1ng chu\u1ea9n h\u00f3a n\u00e2ng cao c\u1ee7a 3NF, \u0111\u1ea3m b\u1ea3o r\u1eb1ng m\u1ed7i ph\u1ee5 thu\u1ed9c h\u00e0m \u0111\u1ec1u d\u1ef1a v\u00e0o m\u1ed9t si\u00eau kh\u00f3a.<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211; D\u1eef li\u1ec7u \u0111\u00e3 \u1edf d\u1ea1ng 3NF.&nbsp;<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; M\u1ed7i ph\u1ee5 thu\u1ed9c h\u00e0m ph\u1ea3i li\u00ean quan \u0111\u1ebfn m\u1ed9t si\u00eau kh\u00f3a c\u1ee7a b\u1ea3ng.<\/span><\/p>\n<\/td><\/tr><tr><td><span style=\"font-weight: 400;\">4NF (Fourth Normal Form)<\/span><\/td><td><span style=\"font-weight: 400;\">Lo\u1ea1i b\u1ecf c\u00e1c ph\u1ee5 thu\u1ed9c \u0111a tr\u1ecb (multi-valued dependency).<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211; D\u1eef li\u1ec7u \u0111\u00e3 \u1edf d\u1ea1ng BCNF.&nbsp;<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; Kh\u00f4ng c\u00f3 ph\u1ee5 thu\u1ed9c \u0111a tr\u1ecb trong b\u1ea3ng.<\/span><\/p>\n<\/td><\/tr><tr><td><span style=\"font-weight: 400;\">5NF (Fifth Normal Form)<\/span><\/td><td><span style=\"font-weight: 400;\">Lo\u1ea1i b\u1ecf c\u00e1c ph\u1ee5 thu\u1ed9c k\u1ebft h\u1ee3p (join dependency), \u0111\u1ea3m b\u1ea3o r\u1eb1ng d\u1eef li\u1ec7u kh\u00f4ng b\u1ecb ph\u00e2n m\u1ea3nh kh\u00f4ng c\u1ea7n thi\u1ebft.<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211; D\u1eef li\u1ec7u \u0111\u00e3 \u1edf d\u1ea1ng 4NF.&nbsp;<\/span>\n<p><span style=\"font-weight: 400;\">&#8211; M\u1ed7i ph\u1ee5 thu\u1ed9c k\u1ebft h\u1ee3p \u0111\u1ec1u c\u00f3 th\u1ec3 ph\u00e2n r\u00e3 m\u00e0 kh\u00f4ng l\u00e0m m\u1ea5t d\u1eef li\u1ec7u.<\/span><\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-denormalize-trong-sql-la-gi\"><b>Gi\u1ea3i th\u00edch Denormalize trong SQL l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Denormalize (phi chu\u1ea9n h\u00f3a) trong SQL l\u00e0 qu\u00e1 tr\u00ecnh k\u1ebft h\u1ee3p l\u1ea1i c\u00e1c b\u1ea3ng \u0111\u00e3 \u0111\u01b0\u1ee3c chu\u1ea9n h\u00f3a \u0111\u1ec3 gi\u1ea3m thi\u1ec3u c\u00e1c ph\u00e9p n\u1ed1i (JOIN) trong truy v\u1ea5n v\u00e0 t\u0103ng t\u1ed1c \u0111\u1ed9 \u0111\u1ecdc d\u1eef li\u1ec7u. Qu\u00e1 tr\u00ecnh n\u00e0y th\u01b0\u1eddng b\u1ed5 sung th\u00eam s\u1ef1 d\u01b0 th\u1eeba c\u00f3 ki\u1ec3m so\u00e1t b\u1eb1ng c\u00e1ch sao ch\u00e9p ho\u1eb7c t\u1ed5ng h\u1ee3p d\u1eef li\u1ec7u t\u1eeb c\u00e1c b\u1ea3ng kh\u00e1c nhau v\u00e0o m\u1ed9t b\u1ea3ng duy nh\u1ea5t.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-m\u1ee5c-tieu-c\u1ee7a-denormalize\"><b>M\u1ee5c ti\u00eau c\u1ee7a Denormalize<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>T\u0103ng hi\u1ec7u su\u1ea5t truy v\u1ea5n: <\/b><span style=\"font-weight: 400;\">C\u00e1c ph\u00e9p n\u1ed1i (<\/span><span style=\"font-weight: 400;\">JOIN<\/span><span style=\"font-weight: 400;\">) gi\u1eefa nhi\u1ec1u b\u1ea3ng chu\u1ea9n h\u00f3a c\u00f3 th\u1ec3 l\u00e0m gi\u1ea3m t\u1ed1c \u0111\u1ed9 truy v\u1ea5n, \u0111\u1eb7c bi\u1ec7t tr\u00ean t\u1eadp d\u1eef li\u1ec7u l\u1edbn. Denormalize gi\u00fap gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng ph\u00e9p n\u1ed1i.<\/span><\/li>\n\n\n\n<li><b>\u0110\u01a1n gi\u1ea3n h\u00f3a truy v\u1ea5n: <\/b><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef d\u01b0\u1edbi d\u1ea1ng phi chu\u1ea9n h\u00f3a d\u1ec5 d\u00e0ng truy v\u1ea5n h\u01a1n v\u00ec m\u1ecdi th\u00f4ng tin li\u00ean quan \u0111\u1ec1u n\u1eb1m trong m\u1ed9t b\u1ea3ng duy nh\u1ea5t.<\/span><\/li>\n\n\n\n<li><b>T\u1ed1i \u01b0u h\u00f3a cho \u0111\u1ecdc d\u1eef li\u1ec7u: <\/b><span style=\"font-weight: 400;\">Denormalize \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch trong c\u00e1c h\u1ec7 th\u1ed1ng t\u1eadp trung v\u00e0o \u0111\u1ecdc d\u1eef li\u1ec7u, ch\u1eb3ng h\u1ea1n nh\u01b0 h\u1ec7 th\u1ed1ng b\u00e1o c\u00e1o ho\u1eb7c kho d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-h\u1ea1n-ch\u1ebf-c\u1ee7a-denormalize\"><b>H\u1ea1n ch\u1ebf c\u1ee7a Denormalize<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>S\u1ef1 d\u01b0 th\u1eeba d\u1eef li\u1ec7u<\/b><span style=\"font-weight: 400;\">: Denormalize c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn s\u1ef1 tr\u00f9ng l\u1eb7p d\u1eef li\u1ec7u, l\u00e0m t\u0103ng k\u00edch th\u01b0\u1edbc l\u01b0u tr\u1eef.<\/span><\/li>\n\n\n\n<li><b>T\u1ed1n k\u00e9m khi c\u1eadp nh\u1eadt: <\/b><span style=\"font-weight: 400;\">V\u00ec d\u1eef li\u1ec7u b\u1ecb tr\u00f9ng l\u1eb7p, c\u1eadp nh\u1eadt m\u1ed9t th\u00f4ng tin c\u00f3 th\u1ec3 c\u1ea7n th\u1ef1c hi\u1ec7n tr\u00ean nhi\u1ec1u b\u1ea3n sao.<\/span><\/li>\n\n\n\n<li><b>M\u1ea5t t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u: <\/b><span style=\"font-weight: 400;\">D\u1ec5 x\u1ea3y ra l\u1ed7i kh\u00f4ng nh\u1ea5t qu\u00e1n n\u1ebfu d\u1eef li\u1ec7u d\u01b0 th\u1eeba kh\u00f4ng \u0111\u01b0\u1ee3c \u0111\u1ed3ng b\u1ed9 h\u00f3a.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-user-defined-function-la-gi\"><b>User-defined function l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">User-defined Function (UDF) trong SQL l\u00e0 m\u1ed9t h\u00e0m do ng\u01b0\u1eddi d\u00f9ng \u0111\u1ecbnh ngh\u0129a \u0111\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t t\u00e1c v\u1ee5 c\u1ee5 th\u1ec3 v\u00e0 c\u00f3 th\u1ec3 t\u00e1i s\u1eed d\u1ee5ng trong c\u00e1c truy v\u1ea5n. UDF \u0111\u01b0\u1ee3c t\u1ea1o ra \u0111\u1ec3 m\u1edf r\u1ed9ng kh\u1ea3 n\u0103ng c\u1ee7a SQL, gi\u00fap th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c t\u00ednh to\u00e1n ho\u1eb7c x\u1eed l\u00fd ph\u1ee9c t\u1ea1p m\u00e0 c\u00e1c h\u00e0m t\u00edch h\u1ee3p s\u1eb5n (built-in functions) kh\u00f4ng \u0111\u00e1p \u1ee9ng \u0111\u01b0\u1ee3c.<\/span><\/p>\n\n\n\n<p><b>C\u00e1c lo\u1ea1i User-defined Function:<\/b><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-scalar-function-ham-tr\u1ea3-v\u1ec1-gia-tr\u1ecb-d\u01a1n\"><b>Scalar Function (H\u00e0m tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb \u0111\u01a1n)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Scalar Function l\u00e0 lo\u1ea1i UDF tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t, c\u00f3 th\u1ec3 l\u00e0 s\u1ed1, chu\u1ed7i, ng\u00e0y gi\u1edd, ho\u1eb7c b\u1ea5t k\u1ef3 ki\u1ec3u d\u1eef li\u1ec7u n\u00e0o kh\u00e1c \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 trong SQL. Scalar Function th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 t\u00ednh to\u00e1n ho\u1eb7c x\u1eed l\u00fd d\u1eef li\u1ec7u \u0111\u01a1n gi\u1ea3n m\u00e0 kh\u00f4ng y\u00eau c\u1ea7u truy v\u1ea5n nhi\u1ec1u h\u00e0ng d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">H\u00e0m n\u00e0y c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">ORDER BY<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c b\u1ea5t k\u1ef3 n\u01a1i n\u00e0o c\u1ea7n m\u1ed9t gi\u00e1 tr\u1ecb \u0111\u01a1n l\u1ebb.&nbsp;<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-table-valued-function-ham-tr\u1ea3-v\u1ec1-b\u1ea3ng\"><b>Table-Valued Function (H\u00e0m tr\u1ea3 v\u1ec1 b\u1ea3ng)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Table-Valued Function l\u00e0 lo\u1ea1i UDF tr\u1ea3 v\u1ec1 m\u1ed9t t\u1eadp h\u1ee3p k\u1ebft qu\u1ea3 d\u01b0\u1edbi d\u1ea1ng m\u1ed9t b\u1ea3ng, t\u01b0\u01a1ng t\u1ef1 nh\u01b0 khi b\u1ea1n th\u1ef1c hi\u1ec7n m\u1ed9t truy v\u1ea5n <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">. H\u00e0m n\u00e0y \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi b\u1ea1n c\u1ea7n t\u00e1i s\u1eed d\u1ee5ng m\u1ed9t truy v\u1ea5n ph\u1ee9c t\u1ea1p nhi\u1ec1u l\u1ea7n ho\u1eb7c khi c\u1ea7n x\u1eed l\u00fd d\u1eef li\u1ec7u trong b\u1ea3ng m\u1ed9t c\u00e1ch linh ho\u1ea1t.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Table-Valued Function \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch trong vi\u1ec7c t\u1ed5 ch\u1ee9c truy v\u1ea5n, l\u00e0m cho m\u00e3 SQL tr\u1edf n\u00ean d\u1ec5 \u0111\u1ecdc h\u01a1n v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec. C\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Table-Valued Function nh\u01b0 m\u1ed9t b\u1ea3ng t\u1ea1m th\u1eddi trong truy v\u1ea5n SQL, gi\u00fap t\u1ed1i \u01b0u h\u00f3a c\u00e1c t\u00e1c v\u1ee5 nh\u01b0 l\u1ecdc d\u1eef li\u1ec7u, nh\u00f3m d\u1eef li\u1ec7u ho\u1eb7c th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n tr\u00ean nhi\u1ec1u h\u00e0ng d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-multi-statement-table-valued-function\"><b>Multi-Statement Table-Valued Function<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 m\u1ed9t d\u1ea1ng m\u1edf r\u1ed9ng c\u1ee7a Table-Valued Function, cho ph\u00e9p th\u1ef1c hi\u1ec7n nhi\u1ec1u c\u00e2u l\u1ec7nh SQL b\u00ean trong h\u00e0m tr\u01b0\u1edbc khi tr\u1ea3 v\u1ec1 b\u1ea3ng k\u1ebft qu\u1ea3. Kh\u00e1c v\u1edbi Table-Valued Function th\u00f4ng th\u01b0\u1eddng, lo\u1ea1i UDF n\u00e0y th\u01b0\u1eddng y\u00eau c\u1ea7u khai b\u00e1o m\u1ed9t bi\u1ebfn b\u1ea3ng (<\/span><span style=\"font-weight: 400;\">@TableVariable<\/span><span style=\"font-weight: 400;\">) \u0111\u1ec3 l\u01b0u tr\u1eef t\u1ea1m d\u1eef li\u1ec7u trong qu\u00e1 tr\u00ecnh x\u1eed l\u00fd. Sau \u0111\u00f3, d\u1eef li\u1ec7u n\u00e0y \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 d\u01b0\u1edbi d\u1ea1ng b\u1ea3ng.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Lo\u1ea1i h\u00e0m n\u00e0y h\u1eefu \u00edch khi c\u1ea7n th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p nh\u01b0 k\u1ebft h\u1ee3p nhi\u1ec1u ngu\u1ed3n d\u1eef li\u1ec7u, t\u00ednh to\u00e1n gi\u00e1 tr\u1ecb trung gian ho\u1eb7c \u00e1p d\u1ee5ng logic ph\u1ee9c t\u1ea1p tr\u01b0\u1edbc khi tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 cu\u1ed1i c\u00f9ng. Tuy nhi\u00ean, Multi-Statement Table-Valued Function c\u00f3 th\u1ec3 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn hi\u1ec7u su\u1ea5t v\u00ec n\u00f3 th\u1ef1c hi\u1ec7n nhi\u1ec1u thao t\u00e1c h\u01a1n so v\u1edbi Table-Valued Function th\u00f4ng th\u01b0\u1eddng.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p t\u1ed5ng qu\u00e1t trong SQL Server:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE FUNCTION FunctionName (@Parameter DataType)<\/span>\n\n<span style=\"font-weight: 400;\">RETURNS DataType<\/span>\n\n<span style=\"font-weight: 400;\">AS<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;-- Logic c\u1ee7a h\u00e0m<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;RETURN Value;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><b>V\u00ed d\u1ee5 1 v\u1edbi UDF \u0111\u01a1n gi\u1ea3n (Tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb)<\/b><span style=\"font-weight: 400;\">: T\u1ea1o m\u1ed9t h\u00e0m \u0111\u1ec3 t\u00ednh l\u01b0\u01a1ng sau thu\u1ebf v\u1edbi thu\u1ebf su\u1ea5t c\u1ed1 \u0111\u1ecbnh 10%:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE FUNCTION CalculateNetSalary (@GrossSalary DECIMAL(10,2))<\/span>\n\n<span style=\"font-weight: 400;\">RETURNS DECIMAL(10,2)<\/span>\n\n<span style=\"font-weight: 400;\">AS<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;RETURN @GrossSalary * 0.9; -- Tr\u1eeb 10% thu\u1ebf<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng UDF tr\u00ean trong truy v\u1ea5n:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT Name, Salary, dbo.CalculateNetSalary(Salary) AS NetSalary<\/span>\n\n<span style=\"font-weight: 400;\">FROM Employees;<\/span><\/code><\/pre>\n\n\n\n<p><b>V\u00ed d\u1ee5 2 v\u1edbi UDF ph\u1ee9c t\u1ea1p h\u01a1n (Tr\u1ea3 v\u1ec1 b\u1ea3ng): <\/b><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed9t h\u00e0m tr\u1ea3 v\u1ec1 danh s\u00e1ch nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng cao h\u01a1n m\u1ed9t gi\u00e1 tr\u1ecb nh\u1ea5t \u0111\u1ecbnh:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE FUNCTION GetHighSalaryEmployees (@MinSalary DECIMAL(10,2))<\/span>\n\n<span style=\"font-weight: 400;\">RETURNS TABLE<\/span>\n\n<span style=\"font-weight: 400;\">AS<\/span>\n\n<span style=\"font-weight: 400;\">RETURN<\/span>\n\n<span style=\"font-weight: 400;\">(<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;SELECT Name, Salary<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;FROM Employees<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;WHERE Salary &gt; @MinSalary<\/span>\n\n<span style=\"font-weight: 400;\">);<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e1ch th\u1ef1c thi truy v\u1ea5n tr\u00ean:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT * FROM dbo.GetHighSalaryEmployees(5000);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-d\u1eb7c-di\u1ec3m-c\u1ee7a-user-defined-function\"><b>\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a User-defined Function<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3:<\/b><span style=\"font-weight: 400;\"> UDF lu\u00f4n tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t ho\u1eb7c m\u1ed9t t\u1eadp h\u1ee3p k\u1ebft qu\u1ea3 (tu\u1ef3 thu\u1ed9c v\u00e0o lo\u1ea1i UDF).<\/span><\/li>\n\n\n\n<li><b>\u0110\u01b0\u1ee3c t\u00e1i s\u1eed d\u1ee5ng:<\/b><span style=\"font-weight: 400;\"> M\u1ed9t khi \u0111\u01b0\u1ee3c t\u1ea1o, UDF c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi trong nhi\u1ec1u truy v\u1ea5n kh\u00e1c nhau, gi\u1ea3m thi\u1ec3u vi\u1ec7c l\u1eb7p l\u1ea1i m\u00e3 l\u1ec7nh.<\/span><\/li>\n\n\n\n<li><b>D\u00f9ng trong c\u00e1c c\u00e2u l\u1ec7nh SQL:<\/b><span style=\"font-weight: 400;\"> UDF c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> GROUP BY<\/span><span style=\"font-weight: 400;\">, v\u00e0 nhi\u1ec1u m\u1ec7nh \u0111\u1ec1 kh\u00e1c.<\/span><\/li>\n\n\n\n<li><b>H\u1ea1n ch\u1ebf thao t\u00e1c d\u1eef li\u1ec7u:<\/b><span style=\"font-weight: 400;\"> UDF ch\u1ec9 c\u00f3 th\u1ec3 \u0111\u1ecdc d\u1eef li\u1ec7u (qua truy v\u1ea5n <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">), kh\u00f4ng th\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c thay \u0111\u1ed5i d\u1eef li\u1ec7u (<\/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;\">). Tuy nhi\u00ean m\u1ed9t s\u1ed1 h\u1ec7 qu\u1ea3n tr\u1ecb (nh\u01b0 PostgreSQL) cho ph\u00e9p UDF th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c n\u00e0y th\u00f4ng qua Stored Procedures ho\u1eb7c Functions v\u1edbi quy\u1ec1n \u0111\u1eb7c bi\u1ec7t.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-h\u1ea1n-ch\u1ebf-c\u1ee7a-udf\"><b>H\u1ea1n ch\u1ebf c\u1ee7a UDF<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Gi\u1edbi h\u1ea1n thao t\u00e1c:<\/b><span style=\"font-weight: 400;\"> Kh\u00f4ng th\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c thay \u0111\u1ed5i 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;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n\n\n\n<li><b>Gi\u1ea3m hi\u1ec7u su\u1ea5t:<\/b><span style=\"font-weight: 400;\"> Trong m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p, UDF c\u00f3 th\u1ec3 ch\u1eadm h\u01a1n khi \u00e1p d\u1ee5ng tr\u00ean t\u1eadp d\u1eef li\u1ec7u l\u1edbn v\u00ec m\u1ed7i l\u1ea7n g\u1ecdi h\u00e0m c\u00f3 th\u1ec3 g\u00e2y th\u00eam chi ph\u00ed x\u1eed l\u00fd.<\/span><\/li>\n\n\n\n<li><b>Kh\u1ea3 n\u0103ng t\u01b0\u01a1ng th\u00edch:<\/b><span style=\"font-weight: 400;\"> UDF \u0111\u01b0\u1ee3c tri\u1ec3n khai kh\u00e1c nhau trong c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (MySQL, SQL Server, PostgreSQL&#8230;).<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-t-sql-la-gi\"><b>T-SQL l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">T-SQL (Transact-SQL) l\u00e0 m\u1ed9t ph\u1ea7n m\u1edf r\u1ed9ng c\u1ee7a ng\u00f4n ng\u1eef SQL ti\u00eau chu\u1ea9n, \u0111\u01b0\u1ee3c ph\u00e1t tri\u1ec3n b\u1edfi Microsoft. T-SQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong Microsoft SQL Server v\u00e0 Azure SQL Database \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 li\u00ean quan \u0111\u1ebfn c\u01a1 s\u1edf d\u1eef li\u1ec7u, bao g\u1ed3m qu\u1ea3n l\u00fd, thao t\u00e1c d\u1eef li\u1ec7u, v\u00e0 l\u1eadp tr\u00ecnh.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">T-SQL b\u1ed5 sung c\u00e1c t\u00ednh n\u0103ng m\u00e0 SQL ti\u00eau chu\u1ea9n kh\u00f4ng c\u00f3, nh\u01b0:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>C\u00e1c c\u1ea5u tr\u00fac l\u1eadp tr\u00ecnh:<\/b><span style=\"font-weight: 400;\"> H\u1ed7 tr\u1ee3 c\u00e1c kh\u1ed1i m\u00e3 l\u1ec7nh nh\u01b0 v\u00f2ng l\u1eb7p, \u0111i\u1ec1u ki\u1ec7n, v\u00e0 bi\u1ebfn.<\/span><\/li>\n\n\n\n<li><b>T\u00edch h\u1ee3p m\u1ea1nh m\u1ebd v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u:<\/b><span style=\"font-weight: 400;\"> Cho ph\u00e9p t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd th\u1ee7 t\u1ee5c l\u01b0u tr\u1eef (stored procedures), h\u00e0m (functions) v\u00e0 trigger.<\/span><\/li>\n\n\n\n<li><b>Qu\u1ea3n l\u00fd giao d\u1ecbch:<\/b><span style=\"font-weight: 400;\"> Cung c\u1ea5p c\u00e1c l\u1ec7nh \u0111\u1ec3 ki\u1ec3m so\u00e1t giao d\u1ecbch (<\/span><span style=\"font-weight: 400;\">BEGIN<\/span> <span style=\"font-weight: 400;\">TRANSACTION<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">COMMIT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">ROLLBACK<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">T\u00ednh n\u0103ng x\u1eed l\u00fd d\u1eef li\u1ec7u n\u00e2ng cao nh\u01b0 CROSS APPLY, MERGE v\u00e0 TRY&#8230;CATCH \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 c\u00e1c h\u00e0m ph\u00e2n t\u00edch (analytic functions) nh\u01b0 ROW_NUMBER, RANK, v\u00e0 PARTITION BY.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-\u1ee9ng-d\u1ee5ng-c\u1ee7a-t-sql\"><b>\u1ee8ng d\u1ee5ng c\u1ee7a T-SQL<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>X\u1eed l\u00fd d\u1eef li\u1ec7u<\/b><span style=\"font-weight: 400;\">: Truy v\u1ea5n, l\u1ecdc v\u00e0 ph\u00e2n t\u00edch d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>L\u1eadp tr\u00ecnh logic:<\/b><span style=\"font-weight: 400;\"> X\u00e2y d\u1ef1ng c\u00e1c h\u1ec7 th\u1ed1ng t\u1ef1 \u0111\u1ed9ng h\u00f3a nh\u01b0 b\u00e1o c\u00e1o, h\u1ec7 th\u1ed1ng k\u1ebf to\u00e1n ho\u1eb7c qu\u1ea3n l\u00fd kho h\u00e0ng.<\/span><\/li>\n\n\n\n<li><b>T\u1ed1i \u01b0u h\u00f3a:<\/b><span style=\"font-weight: 400;\"> T-SQL cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c truy v\u1ea5n ph\u1ee9c t\u1ea1p v\u1edbi hi\u1ec7u su\u1ea5t cao.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-sao-d\u1ec3-h\u1ea1n-ch\u1ebf-quy\u1ec1n-truy-c\u1eadp-cho-user-trong-sql\"><b>L\u00e0m sao \u0111\u1ec3 h\u1ea1n ch\u1ebf quy\u1ec1n truy c\u1eadp cho user trong SQL?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 h\u1ea1n ch\u1ebf quy\u1ec1n truy c\u1eadp cho user trong SQL, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c c\u01a1 ch\u1ebf qu\u1ea3n l\u00fd quy\u1ec1n (permissions) v\u00e0 vai tr\u00f2 ng\u01b0\u1eddi d\u00f9ng (roles) \u0111\u01b0\u1ee3c cung c\u1ea5p b\u1edfi h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u. Qu\u1ea3n l\u00fd quy\u1ec1n truy c\u1eadp l\u00e0 m\u1ed9t ph\u1ea7n quan tr\u1ecdng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o b\u1ea3o m\u1eadt d\u1eef li\u1ec7u v\u00e0 ch\u1ec9 cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng th\u1ef1c hi\u1ec7n c\u00e1c h\u00e0nh \u0111\u1ed9ng c\u1ea7n thi\u1ebft. T\u00f9y thu\u1ed9c v\u00e0o h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 MySQL, SQL Server, ho\u1eb7c PostgreSQL, c\u00e1ch \u00e1p d\u1ee5ng c\u1ee5 th\u1ec3 c\u00f3 th\u1ec3 kh\u00e1c nhau.<\/span><\/p>\n\n\n\n<p><b>C\u00e1c c\u00e1ch h\u1ea1n ch\u1ebf quy\u1ec1n truy c\u1eadp cho user<\/b><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-t\u1ea1o-user-v\u1edbi-quy\u1ec1n-h\u1ea1n-c\u1ee5-th\u1ec3\"><b>T\u1ea1o user v\u1edbi quy\u1ec1n h\u1ea1n c\u1ee5 th\u1ec3<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Khi th\u00eam m\u1ed9t user m\u1edbi, b\u1ea1n c\u00f3 th\u1ec3 ch\u1ec9 \u0111\u1ecbnh quy\u1ec1n c\u1ee5 th\u1ec3 m\u00e0 user \u0111\u01b0\u1ee3c ph\u00e9p th\u1ef1c hi\u1ec7n, ch\u1eb3ng h\u1ea1n nh\u01b0 ch\u1ec9 \u0111\u1ecdc d\u1eef li\u1ec7u (<\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">) ho\u1eb7c ch\u1ec9 ghi d\u1eef li\u1ec7u (<\/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\u00ed d\u1ee5 (MySQL):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE USER 'read_user'@'localhost' IDENTIFIED BY 'password123';<\/span>\n\n<span style=\"font-weight: 400;\">GRANT SELECT ON database_name.* TO 'read_user'@'localhost';<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-s\u1eed-d\u1ee5ng-l\u1ec7nh-grant-va-revoke\"><strong>S\u1eed d\u1ee5ng l\u1ec7nh GRANT v\u00e0 REVOKE<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">GRANT<\/span><b>:<\/b><span style=\"font-weight: 400;\"> C\u1ea5p quy\u1ec1n cho user ho\u1eb7c c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00e1c nh\u01b0 th\u1ee7 t\u1ee5c (procedures) v\u00e0 h\u00e0m (functions) th\u1ef1c hi\u1ec7n c\u00e1c h\u00e0nh \u0111\u1ed9ng c\u1ee5 th\u1ec3.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">REVOKE<\/span><b>:<\/b><span style=\"font-weight: 400;\"> Thu h\u1ed3i quy\u1ec1n c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng n\u1ebfu kh\u00f4ng c\u00f2n c\u1ea7n thi\u1ebft.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">-- C\u1ea5p quy\u1ec1n ch\u1ec9 \u0111\u1ecdc<\/span>\n\n<span style=\"font-weight: 400;\">GRANT SELECT ON Employees TO 'user1';<\/span>\n\n<span style=\"font-weight: 400;\">-- Thu h\u1ed3i quy\u1ec1n s\u1eeda d\u1eef li\u1ec7u<\/span>\n\n<span style=\"font-weight: 400;\">REVOKE UPDATE ON Employees FROM 'user1';<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-s\u1eed-d\u1ee5ng-vai-tro-roles\"><b>S\u1eed d\u1ee5ng vai tr\u00f2 (Roles)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">T\u1ea1o vai tr\u00f2 (role) v\u1edbi m\u1ed9t t\u1eadp h\u1ee3p quy\u1ec1n c\u1ee5 th\u1ec3, sau \u0111\u00f3 g\u00e1n vai tr\u00f2 n\u00e0y cho user, gi\u00fap qu\u1ea3n l\u00fd quy\u1ec1n d\u1ec5 d\u00e0ng h\u01a1n khi c\u00f3 nhi\u1ec1u user. V\u00ed d\u1ee5 (SQL Server):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE ROLE ReadOnlyRole;<\/span>\n\n<span style=\"font-weight: 400;\">GRANT SELECT ON Employees TO ReadOnlyRole;<\/span>\n\n<span style=\"font-weight: 400;\">EXEC sp_addrolemember 'ReadOnlyRole', 'user1';<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-gi\u1edbi-h\u1ea1n-quy\u1ec1n-truy-c\u1eadp-vao-cac-c\u1ed9t-ho\u1eb7c-hang-c\u1ee5-th\u1ec3\"><b>Gi\u1edbi h\u1ea1n quy\u1ec1n truy c\u1eadp v\u00e0o c\u00e1c c\u1ed9t ho\u1eb7c h\u00e0ng c\u1ee5 th\u1ec3<\/b><\/h4>\n\n\n\n<p><b>Gi\u1edbi h\u1ea1n quy\u1ec1n truy c\u1eadp c\u1ed9t:<\/b><span style=\"font-weight: 400;\"> Cho ph\u00e9p user ch\u1ec9 xem ho\u1eb7c thao t\u00e1c tr\u00ean m\u1ed9t s\u1ed1 c\u1ed9t nh\u1ea5t \u0111\u1ecbnh. V\u00ed d\u1ee5 (MySQL):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">GRANT SELECT (Name, Salary) ON Employees TO 'user1';<\/span><\/code><\/pre>\n\n\n\n<p><b>Gi\u1edbi h\u1ea1n quy\u1ec1n truy c\u1eadp h\u00e0ng:<\/b><span style=\"font-weight: 400;\"> S\u1eed d\u1ee5ng View \u0111\u1ec3 t\u1ea1o l\u1edbp d\u1eef li\u1ec7u \u1ea3o ch\u1ee9a c\u00e1c h\u00e0ng \u0111\u01b0\u1ee3c ph\u00e9p truy c\u1eadp. V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE VIEW LimitedView AS<\/span>\n\n<span style=\"font-weight: 400;\">SELECT Name, Salary FROM Employees WHERE Department = 'IT';<\/span>\n\n<span style=\"font-weight: 400;\">GRANT SELECT ON LimitedView TO 'user1';<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-h\u1ea1n-ch\u1ebf-quy\u1ec1n-truy-c\u1eadp-qua-m\u1ea1ng\"><b>H\u1ea1n ch\u1ebf quy\u1ec1n truy c\u1eadp qua m\u1ea1ng<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Quy \u0111\u1ecbnh c\u1ee5 th\u1ec3 user ch\u1ec9 \u0111\u01b0\u1ee3c truy c\u1eadp t\u1eeb m\u1ed9t \u0111\u1ecba ch\u1ec9 IP ho\u1eb7c m\u00e1y ch\u1ee7 nh\u1ea5t \u0111\u1ecbnh. V\u00ed d\u1ee5 (MySQL):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE USER 'user1'@'192.168.1.10' IDENTIFIED BY 'password123';<\/span>\n\n<span style=\"font-weight: 400;\">GRANT SELECT ON database_name.* TO 'user1'@'192.168.1.10';<\/span><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tham kh\u1ea3o <strong><a href=\"https:\/\/itviec.com\/it-jobs\/sql?utm_medium=anchor_text_mid&amp;utm_source=blog&amp;utm_campaign=vec_lam_sql&amp;utm_content=cau_hoi_phong_van_sql\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m SQL<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean to\u00e0n qu\u1ed1c<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-sql-danh-cho-senior\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_SQL_danh_cho_Senior\"><\/span><b>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL d\u00e0nh cho Senior<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cach-th\u1ef1c-hi\u1ec7n-giao-d\u1ecbch-transaction-trong-sql-cac-thu\u1ed9c-tinh-c\u1ee7a-acid-la-gi\"><b>C\u00e1ch th\u1ef1c hi\u1ec7n giao d\u1ecbch (Transaction) trong SQL? C\u00e1c thu\u1ed9c t\u00ednh c\u1ee7a ACID l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Transaction (Giao d\u1ecbch) trong SQL l\u00e0 m\u1ed9t chu\u1ed7i c\u00e1c c\u00e2u l\u1ec7nh SQL \u0111\u01b0\u1ee3c th\u1ef1c thi nh\u01b0 m\u1ed9t \u0111\u01a1n v\u1ecb duy nh\u1ea5t. N\u1ebfu m\u1ed9t trong c\u00e1c c\u00e2u l\u1ec7nh kh\u00f4ng th\u00e0nh c\u00f4ng, to\u00e0n b\u1ed9 giao d\u1ecbch s\u1ebd b\u1ecb h\u1ee7y b\u1ecf, \u0111\u1ea3m b\u1ea3o r\u1eb1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00f4ng b\u1ecb r\u01a1i v\u00e0o tr\u1ea1ng th\u00e1i kh\u00f4ng nh\u1ea5t qu\u00e1n.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cac-b\u01b0\u1edbc-th\u1ef1c-hi\u1ec7n-giao-d\u1ecbch-trong-sql\"><b>C\u00e1c b\u01b0\u1edbc th\u1ef1c hi\u1ec7n giao d\u1ecbch trong SQL<\/b><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><b>B\u1eaft \u0111\u1ea7u giao d\u1ecbch: <\/b><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">BEGIN TRANSACTION<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c c\u00fa ph\u00e1p t\u01b0\u01a1ng t\u1ef1 (ph\u1ee5 thu\u1ed9c v\u00e0o h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u).<\/span><\/li>\n\n\n\n<li><b>Th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c SQL: <\/b><span style=\"font-weight: 400;\">Th\u00eam (<\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">), c\u1eadp nh\u1eadt (<\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">), ho\u1eb7c x\u00f3a (<\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\">) d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>X\u00e1c nh\u1eadn giao d\u1ecbch: <\/b><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">COMMIT<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 l\u01b0u v\u0129nh vi\u1ec5n c\u00e1c thay \u0111\u1ed5i v\u00e0o c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>H\u1ee7y b\u1ecf giao d\u1ecbch (n\u1ebfu c\u1ea7n): <\/b><span style=\"font-weight: 400;\">N\u1ebfu x\u1ea3y ra l\u1ed7i ho\u1eb7c c\u1ea7n ho\u00e0n t\u00e1c c\u00e1c thay \u0111\u1ed5i, s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">ROLLBACK<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 kh\u00f4i ph\u1ee5c tr\u1ea1ng th\u00e1i ban \u0111\u1ea7u c\u1ee7a c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ol>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: V\u1edbi qu\u00e1 tr\u00ecnh chuy\u1ec3n ti\u1ec1n gi\u1eefa hai t\u00e0i kho\u1ea3n. N\u1ebfu b\u1ea5t k\u1ef3 b\u01b0\u1edbc n\u00e0o th\u1ea5t b\u1ea1i, to\u00e0n b\u1ed9 giao d\u1ecbch s\u1ebd b\u1ecb ho\u00e0n t\u00e1c v\u1edbi c\u00fa ph\u00e1p nh\u01b0 sau (trong SQL Server):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">BEGIN TRANSACTION;<\/span>\n\n<span style=\"font-weight: 400;\">-- Tr\u1eeb ti\u1ec1n t\u1eeb t\u00e0i kho\u1ea3n ng\u01b0\u1eddi g\u1eedi<\/span>\n\n<span style=\"font-weight: 400;\">UPDATE Accounts<\/span>\n\n<span style=\"font-weight: 400;\">SET Balance = Balance - 1000<\/span>\n\n<span style=\"font-weight: 400;\">WHERE AccountID = 1;<\/span>\n\n<span style=\"font-weight: 400;\">-- C\u1ed9ng ti\u1ec1n v\u00e0o t\u00e0i kho\u1ea3n ng\u01b0\u1eddi nh\u1eadn<\/span>\n\n<span style=\"font-weight: 400;\">UPDATE Accounts<\/span>\n\n<span style=\"font-weight: 400;\">SET Balance = Balance + 1000<\/span>\n\n<span style=\"font-weight: 400;\">WHERE AccountID = 2;<\/span>\n\n<span style=\"font-weight: 400;\">-- X\u00e1c nh\u1eadn giao d\u1ecbch n\u1ebfu kh\u00f4ng c\u00f3 l\u1ed7i<\/span>\n\n<span style=\"font-weight: 400;\">IF @@ERROR = 0<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;COMMIT;<\/span>\n\n<span style=\"font-weight: 400;\">ELSE<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;ROLLBACK;<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cac-thu\u1ed9c-tinh-c\u1ee7a-acid-trong-giao-d\u1ecbch-sql\"><b>C\u00e1c thu\u1ed9c t\u00ednh c\u1ee7a ACID trong giao d\u1ecbch SQL<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">ACID l\u00e0 c\u00e1c thu\u1ed9c t\u00ednh c\u1ed1t l\u00f5i \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u trong m\u1ed9t giao d\u1ecbch.<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Atomicity (T\u00ednh nguy\u00ean t\u1eed): <\/b><span style=\"font-weight: 400;\">Giao d\u1ecbch \u0111\u01b0\u1ee3c th\u1ef1c thi nh\u01b0 m\u1ed9t \u0111\u01a1n v\u1ecb duy nh\u1ea5t. N\u1ebfu b\u1ea5t k\u1ef3 ph\u1ea7n n\u00e0o c\u1ee7a giao d\u1ecbch th\u1ea5t b\u1ea1i, to\u00e0n b\u1ed9 giao d\u1ecbch s\u1ebd b\u1ecb h\u1ee7y b\u1ecf. V\u00ed d\u1ee5: Trong v\u00ed d\u1ee5 chuy\u1ec3n ti\u1ec1n, n\u1ebfu kh\u00f4ng th\u1ec3 tr\u1eeb ti\u1ec1n t\u1eeb t\u00e0i kho\u1ea3n ng\u01b0\u1eddi g\u1eedi, giao d\u1ecbch s\u1ebd kh\u00f4ng th\u1ef1c hi\u1ec7n c\u1ed9ng ti\u1ec1n cho t\u00e0i kho\u1ea3n ng\u01b0\u1eddi nh\u1eadn.<\/span><\/li>\n\n\n\n<li><b>Consistency (T\u00ednh nh\u1ea5t qu\u00e1n): <\/b><span style=\"font-weight: 400;\">Sau khi giao d\u1ecbch ho\u00e0n t\u1ea5t, c\u01a1 s\u1edf d\u1eef li\u1ec7u s\u1ebd \u1edf tr\u1ea1ng th\u00e1i h\u1ee3p l\u1ec7, tu\u00e2n th\u1ee7 c\u00e1c r\u00e0ng bu\u1ed9c v\u00e0 quy t\u1eafc d\u1eef li\u1ec7u. V\u00ed d\u1ee5: S\u1ed1 ti\u1ec1n t\u1ed5ng trong c\u1ea3 hai t\u00e0i kho\u1ea3n ph\u1ea3i kh\u00f4ng \u0111\u1ed5i tr\u01b0\u1edbc v\u00e0 sau giao d\u1ecbch.<\/span><\/li>\n\n\n\n<li><b>Isolation (T\u00ednh c\u00f4 l\u1eadp): <\/b><span style=\"font-weight: 400;\">C\u00e1c giao d\u1ecbch \u0111\u1ed3ng th\u1eddi kh\u00f4ng \u1ea3nh h\u01b0\u1edfng l\u1eabn nhau. M\u1ed7i giao d\u1ecbch \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n nh\u01b0 th\u1ec3 n\u00f3 l\u00e0 duy nh\u1ea5t. V\u00ed d\u1ee5: N\u1ebfu hai giao d\u1ecbch c\u00f9ng thay \u0111\u1ed5i s\u1ed1 d\u01b0 t\u00e0i kho\u1ea3n, k\u1ebft qu\u1ea3 s\u1ebd gi\u1ed1ng nh\u01b0 t\u1eebng giao d\u1ecbch \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n l\u1ea7n l\u01b0\u1ee3t.<\/span><\/li>\n\n\n\n<li><b>Durability (T\u00ednh b\u1ec1n v\u1eefng): <\/b><span style=\"font-weight: 400;\">Sau khi giao d\u1ecbch \u0111\u01b0\u1ee3c x\u00e1c nh\u1eadn (<\/span><span style=\"font-weight: 400;\">COMMIT<\/span><span style=\"font-weight: 400;\">), c\u00e1c thay \u0111\u1ed5i s\u1ebd \u0111\u01b0\u1ee3c l\u01b0u v\u0129nh vi\u1ec5n, ngay c\u1ea3 khi x\u1ea3y ra l\u1ed7i h\u1ec7 th\u1ed1ng. V\u00ed d\u1ee5: Sau khi giao d\u1ecbch chuy\u1ec3n ti\u1ec1n ho\u00e0n t\u1ea5t, s\u1ed1 d\u01b0 m\u1edbi c\u1ee7a c\u00e1c t\u00e0i kho\u1ea3n s\u1ebd \u0111\u01b0\u1ee3c ghi l\u1ea1i v\u0129nh vi\u1ec5n.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-olap-va-oltp\"><b>Ph\u00e2n bi\u1ec7t OLAP v\u00e0 OLTP<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">OLAP (Online Analytical Processing) v\u00e0 OLTP (Online Transaction Processing) l\u00e0 hai lo\u1ea1i h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u ph\u1ee5c v\u1ee5 c\u00e1c m\u1ee5c \u0111\u00edch kh\u00e1c nhau trong qu\u1ea3n l\u00fd v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>OLAP (Online Analytical Processing)<\/b><\/td><td><b>OLTP (Online Transaction Processing)<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 ph\u00e2n t\u00edch d\u1eef li\u1ec7u v\u00e0 ra quy\u1ebft \u0111\u1ecbnh d\u1ef1a tr\u00ean d\u1eef li\u1ec7u l\u1ecbch s\u1eed.<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 x\u1eed l\u00fd giao d\u1ecbch tr\u1ef1c tuy\u1ebfn theo th\u1eddi gian th\u1ef1c.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">T\u00ednh ch\u1ea5t d\u1eef li\u1ec7u<\/span><\/td><td><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u \u0111\u01b0\u1ee3c t\u1ed5ng h\u1ee3p t\u1eeb nhi\u1ec1u ngu\u1ed3n v\u00e0 th\u01b0\u1eddng mang t\u00ednh l\u1ecbch s\u1eed.<\/span><\/td><td><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u chi ti\u1ebft, th\u1eddi gian th\u1ef1c, t\u1eadp trung v\u00e0o c\u00e1c giao d\u1ecbch h\u00e0ng ng\u00e0y.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ki\u1ec3u truy v\u1ea5n<\/span><\/td><td><span style=\"font-weight: 400;\">Truy v\u1ea5n ph\u1ee9c t\u1ea1p, th\u01b0\u1eddng l\u00e0 c\u00e1c ph\u00e9p t\u00ednh t\u1ed5ng h\u1ee3p, nh\u00f3m, v\u00e0 ph\u00e2n t\u00edch.<\/span><\/td><td><span style=\"font-weight: 400;\">Truy v\u1ea5n \u0111\u01a1n gi\u1ea3n, t\u1eadp trung v\u00e0o th\u00eam, s\u1eeda, x\u00f3a, ho\u1eb7c truy v\u1ea5n nhanh.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Thi\u1ebft k\u1ebf c\u01a1 s\u1edf d\u1eef li\u1ec7u<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a cho truy v\u1ea5n ph\u00e2n t\u00edch v\u1edbi c\u00e1c c\u1ea5u tr\u00fac nh\u01b0 kho d\u1eef li\u1ec7u ho\u1eb7c schema d\u1ea1ng sao.<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a cho x\u1eed l\u00fd giao d\u1ecbch v\u1edbi thi\u1ebft k\u1ebf chu\u1ea9n h\u00f3a (Normalization).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ng\u01b0\u1eddi s\u1eed d\u1ee5ng ch\u00ednh<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00e1c nh\u00e0 ph\u00e2n t\u00edch d\u1eef li\u1ec7u, qu\u1ea3n l\u00fd c\u1ea5p cao ho\u1eb7c chuy\u00ean gia ra quy\u1ebft \u0111\u1ecbnh.<\/span><\/td><td><span style=\"font-weight: 400;\">Ng\u01b0\u1eddi d\u00f9ng cu\u1ed1i, nh\u00e2n vi\u00ean v\u1eadn h\u00e0nh ho\u1eb7c c\u00e1c \u1ee9ng d\u1ee5ng giao d\u1ecbch.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Kh\u1ed1i l\u01b0\u1ee3ng d\u1eef li\u1ec7u<\/span><\/td><td><span style=\"font-weight: 400;\">X\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn t\u1eeb nhi\u1ec1u n\u0103m ho\u1eb7c nhi\u1ec1u ngu\u1ed3n.<\/span><\/td><td><span style=\"font-weight: 400;\">X\u1eed l\u00fd d\u1eef li\u1ec7u nh\u1ecf h\u01a1n, t\u1eadp trung v\u00e0o giao d\u1ecbch hi\u1ec7n t\u1ea1i.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">\u1ee8ng d\u1ee5ng<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ec7 th\u1ed1ng b\u00e1o c\u00e1o, kho d\u1eef li\u1ec7u, ph\u00e2n t\u00edch t\u00e0i ch\u00ednh.<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ec7 th\u1ed1ng qu\u1ea3n l\u00fd b\u00e1n h\u00e0ng, ng\u00e2n h\u00e0ng, ho\u1eb7c qu\u1ea3n l\u00fd t\u1ed3n kho.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-partitioning-trong-sql-la-gi\"><b>Partitioning trong SQL l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Partitioning trong SQL l\u00e0 k\u1ef9 thu\u1eadt chia nh\u1ecf m\u1ed9t b\u1ea3ng l\u1edbn th\u00e0nh c\u00e1c ph\u1ea7n nh\u1ecf h\u01a1n (g\u1ecdi l\u00e0 ph\u00e2n v\u00f9ng) d\u1ef1a tr\u00ean m\u1ed9t s\u1ed1 \u0111i\u1ec1u ki\u1ec7n ho\u1eb7c c\u1ed9t c\u1ee5 th\u1ec3. M\u1ed7i ph\u00e2n v\u00f9ng ch\u1ee9a m\u1ed9t t\u1eadp h\u1ee3p con d\u1eef li\u1ec7u c\u1ee7a b\u1ea3ng g\u1ed1c, nh\u01b0ng to\u00e0n b\u1ed9 c\u00e1c ph\u00e2n v\u00f9ng c\u00f9ng nhau v\u1eabn \u0111\u1ea1i di\u1ec7n cho d\u1eef li\u1ec7u \u0111\u1ea7y \u0111\u1ee7 c\u1ee7a b\u1ea3ng.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Partitioning \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 b\u1edfi m\u1ed9t s\u1ed1 h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 MySQL, PostgreSQL v\u00e0 SQL Server, v\u1edbi c\u00fa ph\u00e1p v\u00e0 kh\u1ea3 n\u0103ng kh\u00e1c nhau. Partitioning gi\u00fap c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t truy v\u1ea5n, qu\u1ea3n l\u00fd d\u1eef li\u1ec7u d\u1ec5 d\u00e0ng h\u01a1n v\u00e0 t\u1ed1i \u01b0u h\u00f3a vi\u1ec7c l\u01b0u tr\u1eef khi l\u00e0m vi\u1ec7c v\u1edbi l\u01b0\u1ee3ng d\u1eef li\u1ec7u l\u1edbn.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-l\u1ee3i-ich-c\u1ee7a-partitioning\"><b>L\u1ee3i \u00edch c\u1ee7a Partitioning<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>C\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t truy v\u1ea5n: <\/b><span style=\"font-weight: 400;\">Truy v\u1ea5n ch\u1ec9 c\u1ea7n qu\u00e9t m\u1ed9t ph\u00e2n v\u00f9ng c\u1ee5 th\u1ec3 thay v\u00ec to\u00e0n b\u1ed9 b\u1ea3ng, gi\u1ea3m th\u1eddi gian x\u1eed l\u00fd. Ph\u00f9 h\u1ee3p v\u1edbi d\u1eef li\u1ec7u l\u1edbn, nh\u01b0 kho d\u1eef li\u1ec7u ho\u1eb7c h\u1ec7 th\u1ed1ng b\u00e1o c\u00e1o.<\/span><\/li>\n\n\n\n<li><b>Qu\u1ea3n l\u00fd d\u1eef li\u1ec7u d\u1ec5 d\u00e0ng: <\/b><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 th\u00eam, x\u00f3a ho\u1eb7c l\u01b0u tr\u1eef d\u1eef li\u1ec7u ri\u00eang l\u1ebb \u1edf c\u1ea5p ph\u00e2n v\u00f9ng m\u00e0 kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn to\u00e0n b\u1ed9 b\u1ea3ng.<\/span><\/li>\n\n\n\n<li><b>T\u1ed1i \u01b0u h\u00f3a l\u01b0u tr\u1eef: <\/b><span style=\"font-weight: 400;\">C\u00e1c ph\u00e2n v\u00f9ng kh\u00e1c nhau c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c l\u01b0u tr\u00ean c\u00e1c \u1ed5 \u0111\u0129a kh\u00e1c nhau \u0111\u1ec3 c\u00e2n b\u1eb1ng t\u1ea3i.<\/span><\/li>\n\n\n\n<li><b>T\u0103ng t\u00ednh kh\u1ea3 d\u1ee5ng: <\/b><span style=\"font-weight: 400;\">N\u1ebfu m\u1ed9t ph\u00e2n v\u00f9ng b\u1ecb l\u1ed7i, c\u00e1c ph\u00e2n v\u00f9ng kh\u00e1c v\u1eabn ho\u1ea1t \u0111\u1ed9ng b\u00ecnh th\u01b0\u1eddng (ch\u1ee7 y\u1ebfu \u00e1p d\u1ee5ng trong c\u00e1c h\u1ec7 th\u1ed1ng ph\u00e2n t\u00e1n ho\u1eb7c l\u01b0u tr\u1eef ph\u00e2n v\u00f9ng tr\u00ean nhi\u1ec1u \u1ed5 \u0111\u0129a).<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-h\u1ea1n-ch\u1ebf-c\u1ee7a-partitioning\"><b>H\u1ea1n ch\u1ebf c\u1ee7a Partitioning<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>\u0110\u1ed9 ph\u1ee9c t\u1ea1p cao h\u01a1n: <\/b><span style=\"font-weight: 400;\">Thi\u1ebft k\u1ebf v\u00e0 qu\u1ea3n l\u00fd b\u1ea3ng ph\u00e2n v\u00f9ng \u0111\u00f2i h\u1ecfi ki\u1ebfn th\u1ee9c k\u1ef9 thu\u1eadt cao h\u01a1n so v\u1edbi b\u1ea3ng th\u01b0\u1eddng.<\/span><\/li>\n\n\n\n<li><b>Kh\u00f4ng ph\u00f9 h\u1ee3p v\u1edbi b\u1ea3ng nh\u1ecf: <\/b><span style=\"font-weight: 400;\">Partitioning th\u01b0\u1eddng kh\u00f4ng c\u1ea7n thi\u1ebft cho c\u00e1c b\u1ea3ng c\u00f3 k\u00edch th\u01b0\u1edbc nh\u1ecf.<\/span><\/li>\n\n\n\n<li><b>H\u1ea1n ch\u1ebf trong m\u1ed9t s\u1ed1 h\u1ec7 qu\u1ea3n tr\u1ecb: <\/b><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00f4ng h\u1ed7 tr\u1ee3 partitioning ho\u1eb7c ch\u1ec9 h\u1ed7 tr\u1ee3 \u1edf m\u1ee9c c\u01a1 b\u1ea3n.<\/span><\/li>\n\n\n\n<li><b>Hi\u1ec7u su\u1ea5t k\u00e9m n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf ph\u00f9 h\u1ee3p<\/b><span style=\"font-weight: 400;\">: N\u1ebfu c\u00e1c ph\u00e2n v\u00f9ng kh\u00f4ng \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf t\u1ed1t (ch\u1eb3ng h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng ph\u00e2n v\u00f9ng qu\u00e1 l\u1edbn ho\u1eb7c \u0111i\u1ec1u ki\u1ec7n ph\u00e2n v\u00f9ng kh\u00f4ng t\u1ed1i \u01b0u), c\u00e1c truy v\u1ea5n c\u00f3 th\u1ec3 ph\u1ea3i qu\u00e9t qua nhi\u1ec1u ph\u00e2n v\u00f9ng, d\u1eabn \u0111\u1ebfn hi\u1ec7u su\u1ea5t k\u00e9m h\u01a1n so v\u1edbi b\u1ea3ng kh\u00f4ng ph\u00e2n v\u00f9ng.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cac-lo\u1ea1i-partitioning\"><b>C\u00e1c lo\u1ea1i Partitioning<\/b><\/h4>\n\n\n\n<p><b>Range Partitioning (Ph\u00e2n v\u00f9ng theo kho\u1ea3ng gi\u00e1 tr\u1ecb)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Range Partitioning ph\u00e2n chia d\u1eef li\u1ec7u th\u00e0nh c\u00e1c ph\u00e2n v\u00f9ng d\u1ef1a tr\u00ean kho\u1ea3ng gi\u00e1 tr\u1ecb trong m\u1ed9t c\u1ed9t c\u1ee5 th\u1ec3. Lo\u1ea1i n\u00e0y th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi d\u1eef li\u1ec7u c\u00f3 s\u1ef1 ph\u00e2n b\u1ed5 theo th\u1eddi gian, s\u1ed1 l\u01b0\u1ee3ng, ho\u1eb7c c\u00e1c kho\u1ea3ng gi\u00e1 tr\u1ecb r\u00f5 r\u00e0ng.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 chia b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\"> th\u00e0nh c\u00e1c ph\u00e2n v\u00f9ng theo n\u0103m c\u1ee7a c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">OrderDate<\/span><\/i><span style=\"font-weight: 400;\">, ta l\u00e0m nh\u01b0 sau (MySQL):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE TABLE Orders (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;OrderID INT,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;OrderDate DATE,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;TotalAmount DECIMAL<\/span>\n\n<span style=\"font-weight: 400;\">) PARTITION BY RANGE (YEAR(OrderDate)) (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;PARTITION p_2020 VALUES LESS THAN (2021),<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;PARTITION p_2021 VALUES LESS THAN (2022),<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;PARTITION p_2022 VALUES LESS THAN (2023)<\/span>\n\n<span style=\"font-weight: 400;\">);<\/span><\/code><\/pre>\n\n\n\n<p><b>List Partitioning (Ph\u00e2n v\u00f9ng theo danh s\u00e1ch gi\u00e1 tr\u1ecb)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">List Partitioning ph\u00e2n chia d\u1eef li\u1ec7u d\u1ef1a tr\u00ean danh s\u00e1ch c\u00e1c gi\u00e1 tr\u1ecb c\u1ee5 th\u1ec3 trong m\u1ed9t c\u1ed9t. Lo\u1ea1i n\u00e0y ph\u00f9 h\u1ee3p v\u1edbi d\u1eef li\u1ec7u ph\u00e2n lo\u1ea1i r\u00f5 r\u00e0ng nh\u01b0 khu v\u1ef1c, lo\u1ea1i s\u1ea3n ph\u1ea9m ho\u1eb7c tr\u1ea1ng th\u00e1i.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 chia b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Employees<\/span><\/i><span style=\"font-weight: 400;\"> th\u00e0nh c\u00e1c ph\u00e2n v\u00f9ng theo khu v\u1ef1c (<\/span><i><span style=\"font-weight: 400;\">Region<\/span><\/i><span style=\"font-weight: 400;\">), ta l\u00e0m nh\u01b0 sau (MySQL):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE TABLE Employees (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;EmployeeID INT,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Name VARCHAR(50),<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Region VARCHAR(20)<\/span>\n\n<span style=\"font-weight: 400;\">) PARTITION BY LIST (Region) (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;PARTITION p_north VALUES IN ('North', 'Northeast'),<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;PARTITION p_south VALUES IN ('South', 'Southeast')<\/span>\n\n<span style=\"font-weight: 400;\">);<\/span><\/code><\/pre>\n\n\n\n<p><b>Hash Partitioning (Ph\u00e2n v\u00f9ng b\u0103m)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Hash Partitioning s\u1eed d\u1ee5ng h\u00e0m b\u0103m \u0111\u1ec3 ph\u00e2n chia d\u1eef li\u1ec7u d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb c\u1ee7a m\u1ed9t c\u1ed9t, th\u01b0\u1eddng \u00e1p d\u1ee5ng khi d\u1eef li\u1ec7u kh\u00f4ng c\u00f3 quy lu\u1eadt r\u00f5 r\u00e0ng ho\u1eb7c c\u1ea7n ph\u00e2n b\u1ed5 \u0111\u1ec1u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 chia b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Customers<\/span><\/i><span style=\"font-weight: 400;\"> th\u00e0nh 4 ph\u00e2n v\u00f9ng d\u1ef1a tr\u00ean c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">CustomerID<\/span><\/i><span style=\"font-weight: 400;\">, ta l\u00e0m nh\u01b0 sau (MySQL):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE TABLE Customers (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;CustomerID INT,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Name VARCHAR(50)<\/span>\n\n<span style=\"font-weight: 400;\">) PARTITION BY HASH (CustomerID) PARTITIONS 4;<\/span><\/code><\/pre>\n\n\n\n<p><b>Composite Partitioning (Ph\u00e2n v\u00f9ng k\u1ebft h\u1ee3p)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Composite Partitioning l\u00e0 s\u1ef1 k\u1ebft h\u1ee3p c\u1ee7a nhi\u1ec1u lo\u1ea1i ph\u00e2n v\u00f9ng, nh\u01b0 Range + Hash ho\u1eb7c List + Hash, \u0111\u1ec3 t\u1eadn d\u1ee5ng l\u1ee3i th\u1ebf c\u1ee7a c\u1ea3 hai c\u00e1ch ph\u00e2n v\u00f9ng (<\/span><b>MySQL<\/b><span style=\"font-weight: 400;\"> kh\u00f4ng h\u1ed7 tr\u1ee3 composite partitioning tr\u1ef1c ti\u1ebfp).<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 chia b\u1ea3ng Sales th\u00e0nh ph\u00e2n v\u00f9ng theo n\u0103m (<\/span><i><span style=\"font-weight: 400;\">Range<\/span><\/i><span style=\"font-weight: 400;\">) v\u00e0 ti\u1ebfp t\u1ee5c ph\u00e2n chia trong t\u1eebng n\u0103m d\u1ef1a tr\u00ean <\/span><i><span style=\"font-weight: 400;\">Region<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><i><span style=\"font-weight: 400;\">Hash<\/span><\/i><span style=\"font-weight: 400;\">), ta l\u00e0m nh\u01b0 sau (MySQL):<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE TABLE Sales (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;SaleID INT,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;SaleDate DATE,<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Region VARCHAR(20),<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;Amount DECIMAL<\/span>\n\n<span style=\"font-weight: 400;\">) PARTITION BY RANGE (YEAR(SaleDate)) SUBPARTITION BY HASH (Region) (<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;PARTITION p_2020 VALUES LESS THAN (2021),<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;PARTITION p_2021 VALUES LESS THAN (2022)<\/span>\n\n<span style=\"font-weight: 400;\">) SUBPARTITIONS 4;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-etl-trong-sql-la-gi\"><b>ETL trong SQL l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">ETL (Extract, Transform, Load) l\u00e0 m\u1ed9t quy tr\u00ecnh quan tr\u1ecdng trong qu\u1ea3n l\u00fd d\u1eef li\u1ec7u, s\u1eed d\u1ee5ng \u0111\u1ec3 tr\u00edch xu\u1ea5t (Extract) d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u ngu\u1ed3n, bi\u1ebfn \u0111\u1ed5i (Transform) d\u1eef li\u1ec7u theo c\u00e1c quy t\u1eafc kinh doanh ho\u1eb7c y\u00eau c\u1ea7u c\u1ee5 th\u1ec3 v\u00e0 t\u1ea3i (Load) d\u1eef li\u1ec7u v\u00e0o m\u1ed9t c\u01a1 s\u1edf d\u1eef li\u1ec7u ho\u1eb7c kho d\u1eef li\u1ec7u (Data Warehouse).<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">ETL th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c h\u1ec7 th\u1ed1ng ph\u00e2n t\u00edch v\u00e0 b\u00e1o c\u00e1o d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cac-b\u01b0\u1edbc-chinh-trong-etl\"><b>C\u00e1c b\u01b0\u1edbc ch\u00ednh trong ETL<\/b><\/h4>\n\n\n\n<p><b>1. Extract (Tr\u00edch xu\u1ea5t d\u1eef li\u1ec7u)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Giai \u0111o\u1ea1n n\u00e0y tr\u00edch xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb c\u00e1c ngu\u1ed3n kh\u00e1c nhau, c\u00f3 th\u1ec3 l\u00e0 c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7, t\u1ec7p CSV, XML, API, ho\u1eb7c h\u1ec7 th\u1ed1ng ERP. D\u1eef li\u1ec7u thu th\u1eadp c\u00f3 th\u1ec3 kh\u00f4ng \u0111\u1ed3ng nh\u1ea5t v\u1ec1 \u0111\u1ecbnh d\u1ea1ng ho\u1eb7c c\u1ea5u tr\u00fac, tuy nhi\u00ean c\u1ea7n \u0111\u1ea3m b\u1ea3o kh\u00f4ng m\u1ea5t m\u00e1t d\u1eef li\u1ec7u v\u00e0 gi\u1eef \u0111\u01b0\u1ee3c t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><b>2. Transform (Bi\u1ebfn \u0111\u1ed5i d\u1eef li\u1ec7u)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong b\u01b0\u1edbc n\u00e0y, d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c l\u00e0m s\u1ea1ch, chu\u1ea9n h\u00f3a ho\u1eb7c bi\u1ebfn \u0111\u1ed5i \u0111\u1ec3 \u0111\u00e1p \u1ee9ng y\u00eau c\u1ea7u kinh doanh. C\u00e1c thao t\u00e1c ph\u1ed5 bi\u1ebfn bao g\u1ed3m \u0111\u1ed5i \u0111\u1ecbnh d\u1ea1ng, t\u00ednh to\u00e1n gi\u00e1 tr\u1ecb m\u1edbi, ho\u1eb7c h\u1ee3p nh\u1ea5t d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ednh to\u00e1n gi\u00e1 tr\u1ecb m\u1edbi &#8220;T\u1ed5ng ti\u1ec1n \u0111\u00e3 \u0111i\u1ec1u ch\u1ec9nh&#8221; (v\u1edbi m\u1ee9c t\u0103ng 10%) v\u00e0 chuy\u1ec3n \u0111\u1ed5i \u0111\u1ecbnh d\u1ea1ng ng\u00e0y trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\">, ta l\u00e0m nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">SELECT&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;OrderID,&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;CustomerID,&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;CONVERT(DATE, OrderDate, 101) AS OrderDate,&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;TotalAmount * 1.1 AS AdjustedTotalAmount<\/span>\n\n<span style=\"font-weight: 400;\">FROM StagingDatabase.Orders;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">C\u1ed9t <\/span><i><span style=\"font-weight: 400;\">OrderDate<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c chuy\u1ec3n sang \u0111\u1ecbnh d\u1ea1ng <\/span><i><span style=\"font-weight: 400;\">DATE<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u1ec3 \u0111\u1ed3ng nh\u1ea5t.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u1ed9t <\/span><i><span style=\"font-weight: 400;\">TotalAmount<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c nh\u00e2n v\u1edbi 1.1 \u0111\u1ec3 t\u00ednh &#8220;T\u1ed5ng ti\u1ec1n \u0111\u00e3 \u0111i\u1ec1u ch\u1ec9nh&#8221;.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u n\u00e0y s\u1ebd \u0111\u01b0\u1ee3c l\u01b0u t\u1ea1m th\u1eddi trong giai \u0111o\u1ea1n staging \u0111\u1ec3 chu\u1ea9n b\u1ecb t\u1ea3i v\u00e0o kho d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>3. Load (T\u1ea3i d\u1eef li\u1ec7u)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 giai \u0111o\u1ea1n cu\u1ed1i c\u00f9ng, n\u01a1i d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c x\u1eed l\u00fd s\u1ebd \u0111\u01b0\u1ee3c \u0111\u01b0a v\u00e0o h\u1ec7 th\u1ed1ng \u0111\u00edch, nh\u01b0 kho d\u1eef li\u1ec7u (Data Warehouse).<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nested-trigger-la-gi\"><b>Nested trigger l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><b>Nested Trigger<\/b><span style=\"font-weight: 400;\"> trong SQL l\u00e0 c\u00e1c trigger (k\u00edch ho\u1ea1t) \u0111\u01b0\u1ee3c th\u1ef1c thi l\u1ed3ng nhau, t\u1ee9c l\u00e0 m\u1ed9t trigger c\u00f3 th\u1ec3 k\u00edch ho\u1ea1t m\u1ed9t ho\u1eb7c nhi\u1ec1u trigger kh\u00e1c. Qu\u00e1 tr\u00ecnh n\u00e0y x\u1ea3y ra khi m\u1ed9t trigger th\u1ef1c hi\u1ec7n m\u1ed9t thao t\u00e1c 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;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\">, v\u00e0 thao t\u00e1c n\u00e0y ti\u1ebfp t\u1ee5c k\u00edch ho\u1ea1t c\u00e1c trigger kh\u00e1c tr\u00ean b\u1ea3ng li\u00ean quan.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cach-ho\u1ea1t-d\u1ed9ng-c\u1ee7a-nested-trigger\"><b>C\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a Nested Trigger<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Trigger ch\u00ednh (Primary Trigger)<\/b><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c k\u00edch ho\u1ea1t b\u1edfi h\u00e0nh \u0111\u1ed9ng tr\u00ean b\u1ea3ng, ch\u1eb3ng h\u1ea1n 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;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n\n\n\n<li><b>Trigger ph\u1ee5 (Nested Trigger):<\/b><span style=\"font-weight: 400;\"> K\u00edch ho\u1ea1t do c\u00e1c thao t\u00e1c trong trigger ch\u00ednh. Trigger ph\u1ee5 c\u00f3 th\u1ec3 ti\u1ebfp t\u1ee5c k\u00edch ho\u1ea1t c\u00e1c trigger kh\u00e1c, d\u1eabn \u0111\u1ebfn chu\u1ed7i c\u00e1c trigger l\u1ed3ng nhau.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-vi-d\u1ee5\"><b>V\u00ed d\u1ee5<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed c\u00f3 hai b\u1ea3ng:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\">: L\u01b0u th\u00f4ng tin \u0111\u01a1n h\u00e0ng.<\/span><\/li>\n\n\n\n<li><i><span style=\"font-weight: 400;\">Inventory<\/span><\/i><span style=\"font-weight: 400;\">: L\u01b0u th\u00f4ng tin t\u1ed3n kho s\u1ea3n ph\u1ea9m.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>Y\u00eau c\u1ea7u:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Khi th\u00eam m\u1ed9t \u0111\u01a1n h\u00e0ng v\u00e0o b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\">, trigger s\u1ebd gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng s\u1ea3n ph\u1ea9m t\u1ed3n kho trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Inventory<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">N\u1ebfu s\u1ed1 l\u01b0\u1ee3ng t\u1ed3n kho d\u01b0\u1edbi m\u1ee9c t\u1ed1i thi\u1ec3u, m\u1ed9t trigger kh\u00e1c s\u1ebd ghi th\u00f4ng b\u00e1o c\u1ea3nh b\u00e1o v\u00e0o b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Alerts<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>C\u00e1ch th\u1ef1c hi\u1ec7n:<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u01b0\u1edbc 1: T\u1ea1o trigger ch\u00ednh tr\u00ean b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\">. Trigger n\u00e0y gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng t\u1ed3n kho s\u1ea3n ph\u1ea9m khi th\u00eam \u0111\u01a1n h\u00e0ng:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE TRIGGER trg_UpdateInventory<\/span>\n\n<span style=\"font-weight: 400;\">ON Orders<\/span>\n\n<span style=\"font-weight: 400;\">AFTER INSERT<\/span>\n\n<span style=\"font-weight: 400;\">AS<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;UPDATE Inventory<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;SET Quantity = Quantity - i.Quantity<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;FROM Inventory inv<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;INNER JOIN Inserted i ON inv.ProductID = i.ProductID;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u01b0\u1edbc 2: T\u1ea1o trigger ph\u1ee5 tr\u00ean b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Inventory<\/span><\/i><span style=\"font-weight: 400;\">. Trigger n\u00e0y ghi th\u00f4ng b\u00e1o c\u1ea3nh b\u00e1o n\u1ebfu t\u1ed3n kho d\u01b0\u1edbi m\u1ee9c t\u1ed1i thi\u1ec3u:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE TRIGGER trg_InventoryAlert<\/span>\n\n<span style=\"font-weight: 400;\">ON Inventory<\/span>\n\n<span style=\"font-weight: 400;\">AFTER UPDATE<\/span>\n\n<span style=\"font-weight: 400;\">AS<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;INSERT INTO Alerts (ProductID, AlertMessage, AlertDate)<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;SELECT ProductID, 'Low stock alert', GETDATE()<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;FROM Inserted<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;WHERE Quantity &lt; 10;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Gi\u1ea3i th\u00edch:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Khi th\u00eam m\u1ed9t \u0111\u01a1n h\u00e0ng v\u00e0o <\/span><i><span style=\"font-weight: 400;\">Orders<\/span><\/i><span style=\"font-weight: 400;\">, trigger <\/span><i><span style=\"font-weight: 400;\">trg_UpdateInventory<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng t\u1ed3n kho trong <\/span><i><span style=\"font-weight: 400;\">Inventory<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">N\u1ebfu t\u1ed3n kho d\u01b0\u1edbi 10, trigger <\/span><i><span style=\"font-weight: 400;\">trg_InventoryAlert<\/span><\/i><span style=\"font-weight: 400;\"> s\u1ebd th\u00eam th\u00f4ng b\u00e1o v\u00e0o b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Alerts<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-tranh-vong-l\u1eb7p-vo-h\u1ea1n-do-nested-trigger\"><b>Tr\u00e1nh v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n do Nested Trigger<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">N\u1ebfu m\u1ed9t trigger th\u1ef1c hi\u1ec7n h\u00e0nh \u0111\u1ed9ng k\u00edch ho\u1ea1t l\u1ea1i ch\u00ednh n\u00f3 (ho\u1eb7c m\u1ed9t trigger kh\u00e1c c\u00f3 quan h\u1ec7 tu\u1ea7n ho\u00e0n), v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n s\u1ebd x\u1ea3y ra, d\u1eabn \u0111\u1ebfn l\u1ed7i h\u1ec7 th\u1ed1ng ho\u1eb7c hi\u1ec7u su\u1ea5t suy gi\u1ea3m nghi\u00eam tr\u1ecdng.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e1ch tr\u00e1nh v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>S\u1eed d\u1ee5ng<\/b> <span style=\"font-weight: 400;\">NESTED_TRIGGERS<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">NESTED_TRIGGERS<\/span><span style=\"font-weight: 400;\"> l\u00e0 t\u00f9y ch\u1ecdn trong SQL Server gi\u00fap b\u1eadt\/t\u1eaft vi\u1ec7c k\u00edch ho\u1ea1t trigger l\u1ed3ng nhau. T\u00f9y ch\u1ecdn n\u00e0y ng\u0103n trigger ph\u1ee5 \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t b\u1edfi trigger ch\u00ednh, gi\u00fap lo\u1ea1i b\u1ecf v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n. V\u00ed d\u1ee5, n\u1ebfu trigger A c\u1eadp nh\u1eadt b\u1ea3ng B, t\u1eeb \u0111\u00f3 k\u00edch ho\u1ea1t l\u1ea1i A, t\u00f9y ch\u1ecdn n\u00e0y s\u1ebd ch\u1eb7n trigger A th\u1ef1c thi l\u1ea7n hai. Tuy nhi\u00ean, nh\u01b0\u1ee3c \u0111i\u1ec3m l\u00e0 b\u1ea1n kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c trigger l\u1ed3ng nhau h\u1eefu \u00edch trong m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p c\u1ea7n x\u1eed l\u00fd ph\u1ee9c t\u1ea1p.<\/span><\/li>\n\n\n\n<li><b>Th\u00eam \u0111i\u1ec1u ki\u1ec7n ki\u1ec3m tra: <\/b><span style=\"font-weight: 400;\">Th\u00eam \u0111i\u1ec1u ki\u1ec7n ki\u1ec3m tra trong trigger, v\u00ed d\u1ee5 s\u1eed d\u1ee5ng c\u1edd tr\u1ea1ng th\u00e1i (flag) ho\u1eb7c ki\u1ec3m tra d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c x\u1eed l\u00fd, gi\u00fap ng\u0103n trigger t\u1ef1 k\u00edch ho\u1ea1t l\u1ea1i tr\u00ean d\u1eef li\u1ec7u c\u0169 v\u00e0 \u0111\u1ea3m b\u1ea3o trigger ch\u1ec9 ch\u1ea1y tr\u00ean d\u1eef li\u1ec7u m\u1edbi ho\u1eb7c ch\u01b0a \u0111\u01b0\u1ee3c x\u1eed l\u00fd. Tuy nhi\u00ean, c\u00e1ch n\u00e0y \u0111\u00f2i h\u1ecfi b\u1ed5 sung logic v\u00e0 \u0111\u00f4i khi thay \u0111\u1ed5i c\u1ea5u tr\u00fac b\u1ea3ng, l\u00e0m t\u0103ng \u0111\u1ed9 ph\u1ee9c t\u1ea1p.<\/span><\/li>\n\n\n\n<li><b>H\u1ea1n ch\u1ebf thao t\u00e1c k\u00edch ho\u1ea1t: <\/b><span style=\"font-weight: 400;\">Thi\u1ebft k\u1ebf trigger sao cho kh\u00f4ng th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c c\u00f3 th\u1ec3 k\u00edch ho\u1ea1t l\u1ea1i ch\u00ednh n\u00f3, v\u00ed d\u1ee5 di chuy\u1ec3n logic x\u1eed l\u00fd sang Stored Procedure ho\u1eb7c \u1ee9ng d\u1ee5ng b\u00ean ngo\u00e0i s\u1ebd gi\u00fap gi\u1ea3m nguy c\u01a1 v\u00f2ng l\u1eb7p nh\u01b0ng l\u00e0m t\u0103ng s\u1ef1 ph\u1ee5 thu\u1ed9c v\u00e0o logic b\u00ean ngo\u00e0i, l\u00e0m gi\u1ea3m t\u00ednh t\u1ef1 \u0111\u1ed9ng h\u00f3a.<\/span><\/li>\n\n\n\n<li><b>S\u1eed d\u1ee5ng bi\u1ebfn ho\u1eb7c b\u1ea3ng t\u1ea1m: <\/b><span style=\"font-weight: 400;\">D\u00f9ng bi\u1ebfn ho\u1eb7c b\u1ea3ng t\u1ea1m \u0111\u1ec3 ghi nh\u1eadn tr\u1ea1ng th\u00e1i trigger, gi\u00fap ki\u1ec3m so\u00e1t vi\u1ec7c k\u00edch ho\u1ea1t l\u1eb7p l\u1ea1i trong m\u1ed9t giao d\u1ecbch. Bi\u1ebfn tr\u1ea1ng th\u00e1i \u0111\u1ea3m b\u1ea3o trigger kh\u00f4ng ch\u1ea1y l\u1ea1i tr\u00ean c\u00f9ng d\u1eef li\u1ec7u, nh\u01b0ng c\u00e1ch n\u00e0y c\u00f3 th\u1ec3 ph\u1ee9c t\u1ea1p v\u00e0 \u1ea3nh h\u01b0\u1edfng hi\u1ec7u su\u1ea5t n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-cac-m\u1ee9c-d\u1ed9-co-l\u1eadp-isolation-level\"><b>Gi\u1ea3i th\u00edch c\u00e1c m\u1ee9c \u0111\u1ed9 c\u00f4 l\u1eadp (Isolation level)<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">M\u1ee9c \u0111\u1ed9 c\u00f4 l\u1eadp (Isolation Level) l\u00e0 m\u1ed9t kh\u00eda c\u1ea1nh quan tr\u1ecdng trong qu\u1ea3n l\u00fd giao d\u1ecbch trong SQL. N\u00f3 x\u00e1c \u0111\u1ecbnh m\u1ee9c \u0111\u1ed9 m\u00e0 m\u1ed9t giao d\u1ecbch b\u1ecb c\u00e1ch ly kh\u1ecfi c\u00e1c giao d\u1ecbch kh\u00e1c \u0111ang di\u1ec5n ra, gi\u00fap \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u khi c\u00f3 nhi\u1ec1u giao d\u1ecbch \u0111\u1ed3ng th\u1eddi.<\/span><\/p>\n\n\n\n<p><b>C\u00e1c m\u1ee9c \u0111\u1ed9 c\u00f4 l\u1eadp (Isolation Levels):<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">SQL chu\u1ea9n \u0111\u1ecbnh ngh\u0129a 4 m\u1ee9c \u0111\u1ed9 c\u00f4 l\u1eadp ch\u00ednh, m\u1ed7i m\u1ee9c \u0111\u1ed9 c\u00e2n b\u1eb1ng gi\u1eefa hi\u1ec7u su\u1ea5t v\u00e0 t\u00ednh nh\u1ea5t qu\u00e1n d\u1eef li\u1ec7u:<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-read-uncommitted-d\u1ecdc-d\u1eef-li\u1ec7u-ch\u01b0a-cam-k\u1ebft\"><b>Read Uncommitted (\u0110\u1ecdc d\u1eef li\u1ec7u ch\u01b0a cam k\u1ebft)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u00e0 m\u1ee9c \u0111\u1ed9 c\u00f4 l\u1eadp th\u1ea5p nh\u1ea5t, cho ph\u00e9p m\u1ed9t giao d\u1ecbch \u0111\u1ecdc d\u1eef li\u1ec7u ch\u01b0a \u0111\u01b0\u1ee3c cam k\u1ebft t\u1eeb giao d\u1ecbch kh\u00e1c. \u0110i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn r\u1ee7i ro khi d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c \u0111\u1ecdc c\u00f3 th\u1ec3 b\u1ecb thay \u0111\u1ed5i ho\u1eb7c ho\u00e0n t\u00e1c.<\/span><\/p>\n\n\n\n<p><b>H\u1ea1n ch\u1ebf: <\/b><span style=\"font-weight: 400;\">c\u00f3 th\u1ec3 g\u00e2y ra Dirty Read<\/span><b>, <\/b><span style=\"font-weight: 400;\">c\u00f3 ngh\u0129a l\u00e0 m\u1ed9t giao d\u1ecbch \u0111\u1ecdc d\u1eef li\u1ec7u ch\u01b0a \u0111\u01b0\u1ee3c cam k\u1ebft t\u1eeb giao d\u1ecbch kh\u00e1c, n\u1ebfu giao d\u1ecbch kia b\u1ecb ho\u00e0n t\u00e1c, d\u1eef li\u1ec7u \u0111\u1ecdc \u0111\u01b0\u1ee3c s\u1ebd kh\u00f4ng h\u1ee3p l\u1ec7.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5:<\/b><span style=\"font-weight: 400;\"> Gi\u1ea3 s\u1eed Giao d\u1ecbch A c\u1eadp nh\u1eadt gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">Salary = 10000<\/span><span style=\"font-weight: 400;\"> cho <\/span><span style=\"font-weight: 400;\">EmployeeID = 1<\/span><span style=\"font-weight: 400;\"> nh\u01b0ng ch\u01b0a cam k\u1ebft. Trong khi \u0111\u00f3, Giao d\u1ecbch B \u0111\u1ecdc gi\u00e1 tr\u1ecb n\u00e0y. N\u1ebfu Giao d\u1ecbch A h\u1ee7y (<\/span><span style=\"font-weight: 400;\">ROLLBACK<\/span><span style=\"font-weight: 400;\">), d\u1eef li\u1ec7u \u0111\u1ecdc c\u1ee7a Giao d\u1ecbch B s\u1ebd sai.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-read-committed-d\u1ecdc-d\u1eef-li\u1ec7u-da-cam-k\u1ebft\"><b>Read Committed (\u0110\u1ecdc d\u1eef li\u1ec7u \u0111\u00e3 cam k\u1ebft)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Giao d\u1ecbch ch\u1ec9 \u0111\u1ecdc d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c cam k\u1ebft. \u0110\u00e2y l\u00e0 m\u1ee9c \u0111\u1ed9 c\u00f4 l\u1eadp m\u1eb7c \u0111\u1ecbnh trong nhi\u1ec1u h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u1ea3m nguy c\u01a1 Dirty Read.<\/span><\/p>\n\n\n\n<p><b>H\u1ea1n ch\u1ebf<\/b><span style=\"font-weight: 400;\">: c\u00f3 th\u1ec3 g\u00e2y ra Non-Repeatable Read, c\u00f3 ngh\u0129a l\u00e0 n\u1ebfu m\u1ed9t giao d\u1ecbch \u0111\u1ecdc d\u1eef li\u1ec7u nhi\u1ec1u l\u1ea7n, gi\u00e1 tr\u1ecb c\u00f3 th\u1ec3 thay \u0111\u1ed5i gi\u1eefa c\u00e1c l\u1ea7n \u0111\u1ecdc do giao d\u1ecbch kh\u00e1c cam k\u1ebft thay \u0111\u1ed5i d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5<\/b><span style=\"font-weight: 400;\">: Giao d\u1ecbch A \u0111\u1ecdc gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">Salary = 5000<\/span><span style=\"font-weight: 400;\"> t\u1eeb <\/span><span style=\"font-weight: 400;\">EmployeeID = 1<\/span><span style=\"font-weight: 400;\">. Trong l\u00fac \u0111\u00f3, Giao d\u1ecbch B c\u1eadp nh\u1eadt <\/span><span style=\"font-weight: 400;\">Salary = 6000<\/span><span style=\"font-weight: 400;\"> v\u00e0 cam k\u1ebft. Khi Giao d\u1ecbch A \u0111\u1ecdc l\u1ea1i, gi\u00e1 tr\u1ecb thay \u0111\u1ed5i th\u00e0nh 6000.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-repeatable-read-d\u1ecdc-co-th\u1ec3-l\u1eb7p-l\u1ea1i\"><b>Repeatable Read (\u0110\u1ecdc c\u00f3 th\u1ec3 l\u1eb7p l\u1ea1i)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Giao d\u1ecbch \u0111\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c gi\u00e1 tr\u1ecb \u0111\u1ecdc kh\u00f4ng thay \u0111\u1ed5i trong su\u1ed1t qu\u00e1 tr\u00ecnh giao d\u1ecbch, b\u1ea5t k\u1ec3 giao d\u1ecbch kh\u00e1c c\u1eadp nh\u1eadt d\u1eef li\u1ec7u. Tuy nhi\u00ean, d\u1eef li\u1ec7u m\u1edbi c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c th\u00eam v\u00e0o b\u1ea3ng v\u00e0 kh\u00f4ng b\u1ecb kh\u00f3a.<\/span><\/p>\n\n\n\n<p><b>H\u1ea1n ch\u1ebf<\/b><span style=\"font-weight: 400;\">: c\u00f3 th\u1ec3 g\u00e2y ra Phantom Read, ngh\u0129a l\u00e0 n\u1ebfu m\u1ed9t giao d\u1ecbch \u0111\u1ecdc m\u1ed9t t\u1eadp h\u1ee3p h\u00e0ng, giao d\u1ecbch kh\u00e1c c\u00f3 th\u1ec3 th\u00eam h\u00e0ng m\u1edbi v\u00e0o t\u1eadp h\u1ee3p \u0111\u00f3, d\u1eabn \u0111\u1ebfn k\u1ebft qu\u1ea3 kh\u00e1c khi truy v\u1ea5n l\u1ea1i.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5<\/b><span style=\"font-weight: 400;\">: Giao d\u1ecbch A \u0111\u1ecdc t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean trong ph\u00f2ng IT. Trong l\u00fac \u0111\u00f3, Giao d\u1ecbch B th\u00eam m\u1ed9t nh\u00e2n vi\u00ean m\u1edbi v\u00e0o ph\u00f2ng IT. Khi Giao d\u1ecbch A truy v\u1ea5n l\u1ea1i, nh\u00e2n vi\u00ean m\u1edbi xu\u1ea5t hi\u1ec7n.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-serializable-tu\u1ea7n-t\u1ef1-hoa\"><b>Serializable (Tu\u1ea7n t\u1ef1 h\u00f3a)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">M\u1ee9c \u0111\u1ed9 c\u00f4 l\u1eadp cao nh\u1ea5t, \u0111\u1ea3m b\u1ea3o to\u00e0n b\u1ed9 giao d\u1ecbch \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n nh\u01b0 th\u1ec3 ch\u00fang tu\u1ea7n t\u1ef1 h\u00f3a (kh\u00f4ng \u0111\u1ed3ng th\u1eddi). Ng\u0103n ch\u1eb7n m\u1ecdi v\u1ea5n \u0111\u1ec1, bao g\u1ed3m Dirty Read, Non-Repeatable Read v\u00e0 Phantom Read.<\/span><\/p>\n\n\n\n<p><b>H\u1ea1n ch\u1ebf<\/b><span style=\"font-weight: 400;\">: c\u00f3 th\u1ec3 g\u00e2y ra hi\u1ec7u su\u1ea5t th\u1ea5p do s\u1eed d\u1ee5ng kh\u00f3a to\u00e0n di\u1ec7n tr\u00ean b\u1ea3ng ho\u1eb7c ph\u1ea1m vi d\u1eef li\u1ec7u, ng\u0103n ch\u1eb7n giao d\u1ecbch kh\u00e1c truy c\u1eadp c\u00f9ng d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5<\/b><span style=\"font-weight: 400;\">: Giao d\u1ecbch A \u0111\u1ecdc t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean trong ph\u00f2ng HR. Trong l\u00fac \u0111\u00f3, Giao d\u1ecbch B kh\u00f4ng th\u1ec3 th\u00eam, x\u00f3a, ho\u1eb7c c\u1eadp nh\u1eadt nh\u00e2n vi\u00ean n\u00e0o trong ph\u00f2ng HR cho \u0111\u1ebfn khi Giao d\u1ecbch A ho\u00e0n t\u1ea5t.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-th\u1ef1c-hi\u1ec7n-ki\u1ec3m-soat-d\u1ed3ng-th\u1eddi-concurrency-control\"><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 th\u1ef1c hi\u1ec7n ki\u1ec3m so\u00e1t \u0111\u1ed3ng th\u1eddi (Concurrency Control)?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Ki\u1ec3m so\u00e1t \u0111\u1ed3ng th\u1eddi (Concurrency Control) trong SQL l\u00e0 k\u1ef9 thu\u1eadt \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 qu\u1ea3n l\u00fd vi\u1ec7c th\u1ef1c thi \u0111\u1ed3ng th\u1eddi nhi\u1ec1u giao d\u1ecbch trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, nh\u1eb1m \u0111\u1ea3m b\u1ea3o t\u00ednh nh\u1ea5t qu\u00e1n d\u1eef li\u1ec7u v\u00e0 t\u00ednh to\u00e0n v\u1eb9n khi c\u00f3 nhi\u1ec1u ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c \u1ee9ng d\u1ee5ng truy c\u1eadp c\u00f9ng m\u1ed9t l\u00fac.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00f4ng d\u1ee5ng c\u1ee7a Concurrency Control:&nbsp;<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">\u200b\u200b<\/span><b>Tr\u00e1nh xung \u0111\u1ed9t d\u1eef li\u1ec7u:<\/b><span style=\"font-weight: 400;\"> \u0110\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c giao d\u1ecbch kh\u00f4ng g\u00e2y l\u1ed7i ho\u1eb7c l\u00e0m sai l\u1ec7ch d\u1eef li\u1ec7u khi th\u1ef1c thi \u0111\u1ed3ng th\u1eddi.<\/span><\/li>\n\n\n\n<li><b>\u0110\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n: <\/b><span style=\"font-weight: 400;\">C\u00e1c k\u1ef9 thu\u1eadt ki\u1ec3m so\u00e1t \u0111\u1ed3ng th\u1eddi nh\u01b0 kh\u00f3a (Locking), timestamp ordering ho\u1eb7c multiversion concurrency control (MVCC) \u0111\u1ea3m b\u1ea3o r\u1eb1ng d\u1eef li\u1ec7u v\u1eabn \u0111\u00fang v\u00e0 h\u1ee3p l\u1ec7 ngay c\u1ea3 khi c\u00f3 nhi\u1ec1u giao d\u1ecbch c\u00f9ng th\u1ef1c thi. M\u1eb7c d\u00f9 c\u00f3 th\u1ec3 gi\u1ea3m hi\u1ec7u su\u1ea5t trong m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p, \u0111\u1eb7c bi\u1ec7t v\u1edbi c\u00e1c ph\u01b0\u01a1ng ph\u00e1p nh\u01b0 Serializable Isolation Level, t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u v\u1eabn lu\u00f4n \u0111\u01b0\u1ee3c \u01b0u ti\u00ean.<\/span><\/li>\n\n\n\n<li><b>\u0110\u1ea3m b\u1ea3o ACID:<\/b><span style=\"font-weight: 400;\"> Gi\u1eef v\u1eefng c\u00e1c thu\u1ed9c t\u00ednh <\/span><b>Atomicity<\/b><span style=\"font-weight: 400;\">, <\/span><b>Consistency<\/b><span style=\"font-weight: 400;\">, <\/span><b>Isolation<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Durability<\/b><span style=\"font-weight: 400;\"> c\u1ee7a giao d\u1ecbch.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>C\u00e1c ph\u01b0\u01a1ng ph\u00e1p ki\u1ec3m so\u00e1t \u0111\u1ed3ng th\u1eddi<\/b><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-khoa-locking\"><b>Kh\u00f3a (Locking)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng kh\u00f3a \u0111\u1ec3 ng\u0103n ch\u1eb7n giao d\u1ecbch kh\u00e1c truy c\u1eadp d\u1eef li\u1ec7u \u0111ang \u0111\u01b0\u1ee3c thao t\u00e1c. Kh\u00f3a c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng \u1edf c\u1ea5p \u0111\u1ed9 b\u1ea3ng, d\u00f2ng, ho\u1eb7c trang, t\u00f9y thu\u1ed9c v\u00e0o h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u v\u00e0 y\u00eau c\u1ea7u c\u1ee5 th\u1ec3. C\u00f3 hai lo\u1ea1i kh\u00f3a ph\u1ed5 bi\u1ebfn:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Shared Lock (Kh\u00f3a chia s\u1ebb):<\/b><span style=\"font-weight: 400;\"> Cho ph\u00e9p nhi\u1ec1u giao d\u1ecbch \u0111\u1ecdc c\u00f9ng l\u00fac, nh\u01b0ng kh\u00f4ng cho ph\u00e9p ghi.<\/span><\/li>\n\n\n\n<li><b>Exclusive Lock (Kh\u00f3a \u0111\u1ed9c quy\u1ec1n):<\/b><span style=\"font-weight: 400;\"> Ch\u1ec9 cho ph\u00e9p m\u1ed9t giao d\u1ecbch truy c\u1eadp d\u1eef li\u1ec7u \u0111\u1ec3 \u0111\u1ecdc ho\u1eb7c ghi.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-t-imestamp-ordering-x\u1ebfp-th\u1ee9-t\u1ef1-theo-d\u1ea5u-th\u1eddi-gian\"><b>T\u200b\u200bimestamp Ordering (X\u1ebfp th\u1ee9 t\u1ef1 theo d\u1ea5u th\u1eddi gian)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Giao d\u1ecbch \u0111\u01b0\u1ee3c th\u1ef1c thi theo th\u1ee9 t\u1ef1 th\u1eddi gian b\u1eaft \u0111\u1ea7u. Giao d\u1ecbch sau kh\u00f4ng th\u1ec3 ghi \u0111\u00e8 l\u00ean k\u1ebft qu\u1ea3 c\u1ee7a giao d\u1ecbch tr\u01b0\u1edbc. V\u00ed d\u1ee5 giao d\u1ecbch A b\u1eaft \u0111\u1ea7u tr\u01b0\u1edbc giao d\u1ecbch B, n\u1ebfu B mu\u1ed1n ghi d\u1eef li\u1ec7u m\u00e0 A \u0111ang \u0111\u1ecdc, B ph\u1ea3i ch\u1edd cho \u0111\u1ebfn khi A ho\u00e0n th\u00e0nh.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-ki\u1ec3m-soat-phien-b\u1ea3n-mvcc-multi-version-concurrency-control\"><b>Ki\u1ec3m so\u00e1t phi\u00ean b\u1ea3n (MVCC &#8211; Multi -Version Concurrency Control)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">MVCC cho ph\u00e9p c\u00e1c giao d\u1ecbch \u0111\u1ed3ng th\u1eddi b\u1eb1ng c\u00e1ch gi\u1eef nhi\u1ec1u phi\u00ean b\u1ea3n d\u1eef li\u1ec7u. Giao d\u1ecbch \u0111\u1ecdc s\u1ebd lu\u00f4n th\u1ea5y d\u1eef li\u1ec7u t\u1ea1i th\u1eddi \u0111i\u1ec3m b\u1eaft \u0111\u1ea7u giao d\u1ecbch, b\u1ea5t k\u1ec3 c\u00e1c giao d\u1ecbch kh\u00e1c \u0111\u00e3 thay \u0111\u1ed5i d\u1eef li\u1ec7u. Tuy nhi\u00ean, MVCC c\u00f3 chi ph\u00ed l\u01b0u tr\u1eef cao h\u01a1n do ph\u1ea3i l\u01b0u nhi\u1ec1u phi\u00ean b\u1ea3n d\u1eef li\u1ec7u. \u0110i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 \u1ea3nh h\u01b0\u1edfng hi\u1ec7u su\u1ea5t trong tr\u01b0\u1eddng h\u1ee3p b\u1ea3ng l\u1edbn v\u00e0 giao d\u1ecbch d\u00e0i.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: PostgreSQL v\u00e0 MySQL (InnoDB) s\u1eed d\u1ee5ng MVCC \u0111\u1ec3 cho ph\u00e9p c\u00e1c giao d\u1ecbch \u0111\u1ed3ng th\u1eddi m\u00e0 kh\u00f4ng c\u1ea7n kh\u00f3a.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-serializable-isolation-level\"><b>Serializable Isolation Level<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">\u00c1p d\u1ee5ng m\u1ee9c c\u00f4 l\u1eadp cao nh\u1ea5t \u0111\u1ec3 tu\u1ea7n t\u1ef1 h\u00f3a c\u00e1c giao d\u1ecbch, \u0111\u1ea3m b\u1ea3o kh\u00f4ng c\u00f3 xung \u0111\u1ed9t. Tuy nhi\u00ean, ph\u01b0\u01a1ng ph\u00e1p n\u00e0y l\u00e0m gi\u1ea3m hi\u1ec7u su\u1ea5t do s\u1eed d\u1ee5ng kh\u00f3a to\u00e0n di\u1ec7n.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-optimistic-concurrency-control-ki\u1ec3m-soat-d\u1ed3ng-th\u1eddi-l\u1ea1c-quan\"><b>Optimistic Concurrency Control (Ki\u1ec3m so\u00e1t \u0111\u1ed3ng th\u1eddi l\u1ea1c quan)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Cho ph\u00e9p c\u00e1c giao d\u1ecbch th\u1ef1c thi m\u00e0 kh\u00f4ng c\u1ea7n kh\u00f3a. Tr\u01b0\u1edbc khi ghi d\u1eef li\u1ec7u, h\u1ec7 th\u1ed1ng ki\u1ec3m tra n\u1ebfu d\u1eef li\u1ec7u kh\u00f4ng thay \u0111\u1ed5i trong qu\u00e1 tr\u00ecnh th\u1ef1c hi\u1ec7n giao d\u1ecbch. N\u1ebfu c\u00f3 thay \u0111\u1ed5i, giao d\u1ecbch b\u1ecb h\u1ee7y (ROLLBACK).<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-s\u1ef1-khac-bi\u1ec7t-gi\u1eefa-authentication-va-authorization-trong-sql\"><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa Authentication v\u00e0 Authorization trong SQL<\/b><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>Authentication<\/b><\/td><td><b>Authorization<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch<\/span><\/td><td><span style=\"font-weight: 400;\">X\u00e1c minh danh t\u00ednh ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c \u1ee9ng d\u1ee5ng truy c\u1eadp c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">X\u00e1c \u0111\u1ecbnh quy\u1ec1n m\u00e0 ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c \u1ee9ng d\u1ee5ng \u0111\u01b0\u1ee3c ph\u00e9p th\u1ef1c hi\u1ec7n.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n khi n\u00e0o<\/span><\/td><td><span style=\"font-weight: 400;\">Di\u1ec5n ra tr\u01b0\u1edbc khi truy c\u1eadp v\u00e0o c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">Di\u1ec5n ra sau khi ng\u01b0\u1eddi d\u00f9ng \u0111\u00e3 \u0111\u01b0\u1ee3c x\u00e1c th\u1ef1c.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3<\/span><\/td><td><span style=\"font-weight: 400;\">Cho ph\u00e9p ho\u1eb7c t\u1eeb ch\u1ed1i truy c\u1eadp v\u00e0o h\u1ec7 th\u1ed1ng.<\/span><\/td><td><span style=\"font-weight: 400;\">Cho ph\u00e9p ho\u1eb7c t\u1eeb ch\u1ed1i th\u1ef1c hi\u1ec7n h\u00e0nh \u0111\u1ed9ng c\u1ee5 th\u1ec3 (nh\u01b0 \u0111\u1ecdc, ghi).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">C\u00f4ng c\u1ee5 ch\u00ednh<\/span><\/td><td><span style=\"font-weight: 400;\">Username, password, MFA, token, ch\u1ee9ng ch\u1ec9, ho\u1eb7c t\u00edch h\u1ee3p h\u1ec7 \u0111i\u1ec1u h\u00e0nh.<\/span><\/td><td><span style=\"font-weight: 400;\">Quy\u1ec1n (Permissions) v\u00e0 vai tr\u00f2 (Roles) trong h\u1ec7 th\u1ed1ng, ch\u00ednh s\u00e1ch b\u1ea3o m\u1eadt (Security Policies), ho\u1eb7c r\u00e0ng bu\u1ed9c \u0111i\u1ec1u ki\u1ec7n (Row-Level Security).<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">C\u1ea5p \u0111\u1ed9<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ec7 th\u1ed1ng x\u00e1c th\u1ef1c (H\u1ec7 \u0111i\u1ec1u h\u00e0nh ho\u1eb7c c\u01a1 s\u1edf d\u1eef li\u1ec7u).<\/span><\/td><td><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u ho\u1eb7c thao t\u00e1c trong h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u, bao g\u1ed3m c\u1ea5p b\u1ea3ng, c\u1ed9t v\u00e0 h\u00e0ng.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">V\u00ed d\u1ee5<\/span><\/td><td><span style=\"font-weight: 400;\">Ng\u01b0\u1eddi d\u00f9ng \u0111\u0103ng nh\u1eadp v\u00e0o SQL Server b\u1eb1ng t\u00e0i kho\u1ea3n admin.<\/span><\/td><td><span style=\"font-weight: 400;\">C\u1ea5p quy\u1ec1n <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> tr\u00ean b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Employees<\/span><\/i><span style=\"font-weight: 400;\"> cho ng\u01b0\u1eddi d\u00f9ng <\/span><i><span style=\"font-weight: 400;\">User1<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-full-text-search-trong-sql-la-gi\"><b>Full-text search trong SQL l\u00e0 g\u00ec?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Full-Text Search (T\u00ecm ki\u1ebfm to\u00e0n v\u0103n) l\u00e0 m\u1ed9t t\u00ednh n\u0103ng trong SQL gi\u00fap th\u1ef1c hi\u1ec7n c\u00e1c truy v\u1ea5n t\u00ecm ki\u1ebfm tr\u00ean d\u1eef li\u1ec7u v\u0103n b\u1ea3n l\u1edbn (nh\u01b0 c\u00e1c c\u1ed9t ch\u1ee9a \u0111o\u1ea1n v\u0103n, t\u00e0i li\u1ec7u, ho\u1eb7c v\u0103n b\u1ea3n m\u00f4 t\u1ea3).<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Full-Text Search kh\u00f4ng ch\u1ec9 t\u00ecm ki\u1ebfm ch\u00ednh x\u00e1c t\u1eeb kh\u00f3a m\u00e0 c\u00f2n h\u1ed7 tr\u1ee3 c\u00e1c ki\u1ec3u t\u00ecm ki\u1ebfm n\u00e2ng cao nh\u01b0 t\u00ecm ki\u1ebfm g\u1ea7n \u0111\u00fang, t\u00ecm ki\u1ebfm theo c\u1ee5m t\u1eeb, ho\u1eb7c t\u00ecm ki\u1ebfm theo th\u1ee9 t\u1ef1 \u01b0u ti\u00ean.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cach-ho\u1ea1t-d\u1ed9ng-c\u1ee7a-full-text-search\"><b>C\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a Full-text search<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Full-Text Search ho\u1ea1t \u0111\u1ed9ng d\u1ef1a tr\u00ean vi\u1ec7c t\u1ea1o v\u00e0 s\u1eed d\u1ee5ng ch\u1ec9 m\u1ee5c to\u00e0n v\u0103n (Full-Text Index) \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a qu\u00e1 tr\u00ecnh t\u00ecm ki\u1ebfm trong c\u00e1c c\u1ed9t v\u0103n b\u1ea3n (Full-Text Index kh\u00f4ng l\u01b0u tr\u1eef to\u00e0n b\u1ed9 n\u1ed9i dung v\u0103n b\u1ea3n, m\u00e0 l\u01b0u tr\u1eef danh s\u00e1ch t\u1eeb kh\u00f3a, c\u1ee5m t\u1eeb, c\u00f9ng v\u1edbi v\u1ecb tr\u00ed v\u00e0 th\u00f4ng tin metadata c\u1ea7n thi\u1ebft \u0111\u1ec3 t\u00ecm ki\u1ebfm). Qu\u00e1 tr\u00ecnh n\u00e0y bao g\u1ed3m hai b\u01b0\u1edbc ch\u00ednh:<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 1: T\u1ea1o ch\u1ec9 m\u1ee5c to\u00e0n v\u0103n (Full-Text Index)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Ch\u1ec9 m\u1ee5c to\u00e0n v\u0103n l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac \u0111\u1eb7c bi\u1ec7t \u0111\u01b0\u1ee3c t\u1ea1o tr\u00ean c\u00e1c c\u1ed9t ch\u1ee9a v\u0103n b\u1ea3n. N\u00f3 kh\u00f4ng l\u01b0u tr\u1eef to\u00e0n b\u1ed9 n\u1ed9i dung m\u00e0 ph\u00e2n t\u00e1ch v\u0103n b\u1ea3n th\u00e0nh c\u00e1c t\u1eeb ho\u1eb7c c\u1ee5m t\u1eeb, sau \u0111\u00f3 l\u01b0u tr\u1eef th\u00f4ng tin v\u1ec1 ch\u00fang, bao g\u1ed3m:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">T\u1eeb xu\u1ea5t hi\u1ec7n trong v\u0103n b\u1ea3n.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">V\u1ecb tr\u00ed c\u1ee7a t\u1eeb trong v\u0103n b\u1ea3n.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: M\u1ed9t c\u1ed9t <\/span><i><span style=\"font-weight: 400;\">Content<\/span><\/i><span style=\"font-weight: 400;\"> ch\u1ee9a v\u0103n b\u1ea3n:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><span style=\"font-weight: 400;\">&#8220;SQL is a powerful database language.&#8221;<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Ch\u1ec9 m\u1ee5c to\u00e0n v\u0103n s\u1ebd l\u01b0u tr\u1eef c\u00e1c t\u1eeb:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>SQL, is, a, powerful, database, language<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><b>B\u01b0\u1edbc 2: S\u1eed d\u1ee5ng c\u00f4ng c\u1ee5 t\u00ecm ki\u1ebfm (Search Engine)<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">H\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u s\u1ebd s\u1eed d\u1ee5ng c\u00e1c h\u00e0m nh\u01b0 <\/span><span style=\"font-weight: 400;\">CONTAINS<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">FREETEXT<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u00ecm ki\u1ebfm trong ch\u1ec9 m\u1ee5c to\u00e0n v\u0103n thay v\u00ec qu\u00e9t tr\u1ef1c ti\u1ebfp b\u1ea3ng d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">CONTAINS<\/span><span style=\"font-weight: 400;\">: T\u00ecm ki\u1ebfm t\u1eeb kh\u00f3a c\u1ee5 th\u1ec3 trong v\u0103n b\u1ea3n. V\u00ed d\u1ee5 n\u1ebfu b\u1ea1n t\u00ecm ki\u1ebfm &#8220;database&#8221;, ch\u1ec9 m\u1ee5c s\u1ebd x\u00e1c \u0111\u1ecbnh t\u1ea5t c\u1ea3 c\u00e1c b\u1ea3n ghi ch\u1ee9a t\u1eeb &#8220;database&#8221; v\u00e0 tr\u1ea3 k\u1ebft qu\u1ea3 nhanh h\u01a1n so v\u1edbi vi\u1ec7c qu\u00e9t to\u00e0n b\u1ed9 d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">FREETEXT<\/span><span style=\"font-weight: 400;\">: T\u00ecm ki\u1ebfm linh ho\u1ea1t h\u01a1n b\u1eb1ng c\u00e1ch t\u00ecm c\u00e1c t\u1eeb ho\u1eb7c c\u1ee5m t\u1eeb c\u00f3 \u00fd ngh\u0129a t\u01b0\u01a1ng t\u1ef1. V\u00ed d\u1ee5 n\u1ebfu b\u1ea1n t\u00ecm ki\u1ebfm &#8220;database&#8221;, k\u1ebft qu\u1ea3 c\u00f3 th\u1ec3 bao g\u1ed3m c\u1ea3 &#8220;databases&#8221;, &#8220;relational database&#8221; ho\u1eb7c &#8220;SQL database&#8221;.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-h\u1ea1n-ch\u1ebf-c\u1ee7a-full-text-search\"><b>H\u1ea1n ch\u1ebf c\u1ee7a Full-text search<\/b><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Hi\u1ec7u su\u1ea5t k\u00e9m tr\u00ean d\u1eef li\u1ec7u l\u1edbn: <\/b><span style=\"font-weight: 400;\">Khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u ch\u1ee9a l\u01b0\u1ee3ng l\u1edbn d\u1eef li\u1ec7u v\u0103n b\u1ea3n, vi\u1ec7c t\u00ecm ki\u1ebfm c\u00f3 th\u1ec3 tr\u1edf n\u00ean ch\u1eadm n\u1ebfu ch\u1ec9 m\u1ee5c full-text kh\u00f4ng \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a. Ngo\u00e0i ra, vi\u1ec7c duy tr\u00ec ch\u1ec9 m\u1ee5c full-text (\u0111\u1eb7c bi\u1ec7t khi d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c th\u00eam, s\u1eeda ho\u1eb7c x\u00f3a th\u01b0\u1eddng xuy\u00ean) c\u00f3 th\u1ec3 ti\u00eau t\u1ed1n t\u00e0i nguy\u00ean v\u00e0 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn hi\u1ec7u su\u1ea5t chung c\u1ee7a h\u1ec7 th\u1ed1ng.<\/span><\/li>\n\n\n\n<li><b>Kh\u00f4ng ph\u00f9 h\u1ee3p cho t\u00ecm ki\u1ebfm real-time: <\/b><span style=\"font-weight: 400;\">Ch\u1ec9 m\u1ee5c full-text kh\u00f4ng c\u1eadp nh\u1eadt ngay l\u1eadp t\u1ee9c khi d\u1eef li\u1ec7u trong b\u1ea3ng thay \u0111\u1ed5i. Vi\u1ec7c \u0111\u1ed3ng b\u1ed9 h\u00f3a ch\u1ec9 m\u1ee5c th\u01b0\u1eddng x\u1ea3y ra \u0111\u1ecbnh k\u1ef3 ho\u1eb7c th\u1ee7 c\u00f4ng, c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn t\u00ecnh tr\u1ea1ng k\u1ebft qu\u1ea3 t\u00ecm ki\u1ebfm kh\u00f4ng ph\u1ea3n \u00e1nh ch\u00ednh x\u00e1c tr\u1ea1ng th\u00e1i hi\u1ec7n t\u1ea1i c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>H\u1ea1n ch\u1ebf trong ng\u00f4n ng\u1eef ph\u1ee9c t\u1ea1p: <\/b><span style=\"font-weight: 400;\">Full-Text Search kh\u00f4ng h\u1ed7 tr\u1ee3 t\u1ed1t cho c\u00e1c ng\u00f4n ng\u1eef ph\u1ee9c t\u1ea1p nh\u01b0 ti\u1ebfng Trung, Nh\u1eadt ho\u1eb7c H\u00e0n khi c\u00e1c k\u00fd t\u1ef1 th\u01b0\u1eddng kh\u00f4ng c\u00f3 d\u1ea5u ph\u00e2n c\u00e1ch r\u00f5 r\u00e0ng, d\u1eabn \u0111\u1ebfn vi\u1ec7c t\u00ecm ki\u1ebfm kh\u00f4ng ch\u00ednh x\u00e1c ho\u1eb7c b\u1ecf s\u00f3t d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>\u0110\u1ed9 ch\u00ednh x\u00e1c th\u1ea5p v\u1edbi c\u00e1c tr\u01b0\u1eddng h\u1ee3p ng\u1eef ngh\u0129a: <\/b><span style=\"font-weight: 400;\">Full-Text Search ch\u1ec9 t\u00ecm ki\u1ebfm d\u1ef1a tr\u00ean t\u1eeb kh\u00f3a v\u00e0 c\u1ee5m t\u1eeb trong ch\u1ec9 m\u1ee5c, kh\u00f4ng x\u1eed l\u00fd t\u1ed1t ng\u1eef ngh\u0129a ho\u1eb7c c\u00e1c tr\u01b0\u1eddng h\u1ee3p \u0111\u1ed3ng ngh\u0129a, t\u1eeb vi\u1ebft t\u1eaft ho\u1eb7c ng\u1eef c\u1ea3nh ph\u1ee9c t\u1ea1p. V\u00ed d\u1ee5: n\u1ebfu t\u00ecm ki\u1ebfm t\u1eeb &#8220;car&#8221;, Full-Text Search c\u00f3 th\u1ec3 kh\u00f4ng tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 li\u00ean quan \u0111\u1ebfn &#8220;vehicle&#8221; ho\u1eb7c &#8220;automobile.&#8221;<\/span><\/li>\n\n\n\n<li><b>Kh\u00f4ng h\u1ed7 tr\u1ee3 t\u1ed1t d\u1eef li\u1ec7u phi v\u0103n b\u1ea3n: <\/b><span style=\"font-weight: 400;\">Full-Text Search ch\u1ec9 ho\u1ea1t \u0111\u1ed9ng t\u1ed1t v\u1edbi d\u1eef li\u1ec7u d\u1ea1ng v\u0103n b\u1ea3n (<\/span><span style=\"font-weight: 400;\">CHAR<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">VARCHAR<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">TEXT<\/span><span style=\"font-weight: 400;\">) v\u00e0 kh\u00f4ng \u00e1p d\u1ee5ng \u0111\u01b0\u1ee3c v\u1edbi d\u1eef li\u1ec7u d\u1ea1ng s\u1ed1, ng\u00e0y th\u00e1ng ho\u1eb7c c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u phi v\u0103n b\u1ea3n kh\u00e1c.<\/span><\/li>\n\n\n\n<li><b>Ph\u1ee5 thu\u1ed9c v\u00e0o RDBMS: <\/b><span style=\"font-weight: 400;\">Kh\u00f4ng ph\u1ea3i t\u1ea5t c\u1ea3 c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u \u0111\u1ec1u h\u1ed7 tr\u1ee3 Full-Text Search, v\u00e0 c\u00e1c t\u00ednh n\u0103ng n\u00e0y th\u01b0\u1eddng kh\u00e1c nhau gi\u1eefa c\u00e1c RDBMS.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-k\u1ebft\"><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><b>T\u1ed5ng k\u1ebft<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">SQL kh\u00f4ng ch\u1ec9 d\u1eebng l\u1ea1i \u1edf vi\u1ec7c thao t\u00e1c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u m\u00e0 c\u00f2n l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap b\u1ea1n gi\u1ea3i quy\u1ebft c\u00e1c b\u00e0i to\u00e1n kinh doanh ph\u1ee9c t\u1ea1p. Vi\u1ec7c chu\u1ea9n b\u1ecb cho c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n kh\u00f4ng n\u00ean ch\u1ec9 v\u1ec1 ki\u1ebfn th\u1ee9c l\u00fd thuy\u1ebft m\u00e0 c\u00f2n kinh nghi\u1ec7m v\u00e0 kh\u1ea3 n\u0103ng \u1ee9ng d\u1ee5ng linh ho\u1ea1t v\u00e0o c\u00e1c b\u00e0i to\u00e1n th\u1ef1c t\u1ebf s\u1ebd gi\u00fap b\u1ea1n ghi \u0111i\u1ec3m trong m\u1eaft nh\u00e0 tuy\u1ec3n d\u1ee5ng h\u01a1n.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">ITviec hy v\u1ecdng b\u00e0i vi\u1ebft tr\u00ean s\u1ebd tr\u1edf th\u00e0nh ngu\u1ed3n t\u00e0i li\u1ec7u h\u1eefu \u00edch, gi\u00fap b\u1ea1n t\u1ef1 tin h\u01a1n trong qu\u00e1 tr\u00ecnh chu\u1ea9n b\u1ecb v\u00e0 s\u1eb5n s\u00e0ng chinh ph\u1ee5c m\u1ecdi th\u1eed th\u00e1ch trong bu\u1ed5i ph\u1ecfng v\u1ea5n SQL.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tham kh\u1ea3o <strong><a href=\"https:\/\/itviec.com\/it-jobs\/sql?utm_medium=anchor_text_low&amp;utm_source=blog&amp;utm_campaign=vec_lam_sql&amp;utm_content=cau_hoi_phong_van_sql\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m SQL<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean to\u00e0n qu\u1ed1c<\/em><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>SQL l\u00e0 m\u1ed9t k\u1ef9 n\u0103ng quan tr\u1ecdng trong ng\u00e0nh c\u00f4ng ngh\u1ec7 th\u00f4ng tin, \u0111\u1eb7c bi\u1ec7t khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u. Vi\u1ec7c chu\u1ea9n b\u1ecb c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL l\u00e0 y\u1ebfu t\u1ed1 then ch\u1ed1t gi\u00fap b\u1ea1n th\u00e0nh c\u00f4ng trong c\u00e1c bu\u1ed5i ph\u1ecfng v\u1ea5n. Trong b\u00e0i vi\u1ebft n\u00e0y, ITviec s\u1ebd gi\u1edbi thi\u1ec7u c\u00e1c [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":83991,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109,105],"tags":[],"class_list":["post-83983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it","category-phong-van-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>Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t - ITviec Blog<\/title>\n<meta name=\"description\" content=\"L\u01b0u ngay c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t, t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao, k\u00e8m v\u00ed d\u1ee5 minh h\u1ecda v\u00e0 gi\u1ea3i th\u00edch chi ti\u1ebft gi\u00fap b\u1ea1n chu\u1ea9n b\u1ecb k\u1ef9 h\u01a1n.\" \/>\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-hoi-phong-van-sql\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t\" \/>\n<meta property=\"og:description\" content=\"SQL l\u00e0 m\u1ed9t k\u1ef9 n\u0103ng quan tr\u1ecdng trong ng\u00e0nh c\u00f4ng ngh\u1ec7 th\u00f4ng tin, \u0111\u1eb7c bi\u1ec7t khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u. Vi\u1ec7c chu\u1ea9n b\u1ecb c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL l\u00e0 y\u1ebfu t\u1ed1\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-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-10T07:52:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-28T15:50:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-sql-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"950\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\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=\"67 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t - ITviec Blog","description":"L\u01b0u ngay c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t, t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao, k\u00e8m v\u00ed d\u1ee5 minh h\u1ecda v\u00e0 gi\u1ea3i th\u00edch chi ti\u1ebft gi\u00fap b\u1ea1n chu\u1ea9n b\u1ecb k\u1ef9 h\u01a1n.","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-hoi-phong-van-sql\/","og_locale":"vi_VN","og_type":"article","og_title":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t","og_description":"SQL l\u00e0 m\u1ed9t k\u1ef9 n\u0103ng quan tr\u1ecdng trong ng\u00e0nh c\u00f4ng ngh\u1ec7 th\u00f4ng tin, \u0111\u1eb7c bi\u1ec7t khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u. Vi\u1ec7c chu\u1ea9n b\u1ecb c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL l\u00e0 y\u1ebfu t\u1ed1","og_url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-01-10T07:52:58+00:00","article_modified_time":"2025-09-28T15:50:37+00:00","og_image":[{"width":950,"height":500,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-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":"67 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t","datePublished":"2025-01-10T07:52:58+00:00","dateModified":"2025-09-28T15:50:37+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/"},"wordCount":18031,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-sql-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT","Ph\u1ecfng v\u1ea5n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/","url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/","name":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-sql-vippro.jpg","datePublished":"2025-01-10T07:52:58+00:00","dateModified":"2025-09-28T15:50:37+00:00","description":"L\u01b0u ngay c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t, t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao, k\u00e8m v\u00ed d\u1ee5 minh h\u1ecda v\u00e0 gi\u1ea3i th\u00edch chi ti\u1ebft gi\u00fap b\u1ea1n chu\u1ea9n b\u1ecb k\u1ef9 h\u01a1n.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-sql-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/01\/cau-hoi-phong-van-sql-vippro.jpg","width":950,"height":500,"caption":"c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n sql - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-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":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t"}]},{"@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\/83983","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=83983"}],"version-history":[{"count":1,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/83983\/revisions"}],"predecessor-version":[{"id":91806,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/83983\/revisions\/91806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/83991"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=83983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=83983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=83983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}