{"id":166839,"date":"2024-11-05T20:03:26","date_gmt":"2024-11-05T12:03:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166839\/"},"modified":"2024-11-05T20:03:27","modified_gmt":"2024-11-05T12:03:27","slug":"%e4%ba%86%e8%a7%a3sql%ef%bc%8c%e6%8e%8c%e6%8f%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8a%80%e8%83%bd-sql%e5%bf%85%e7%9f%a5%e5%bf%85%e6%9c%83-%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166839\/","title":{"rendered":"\u4e86\u89e3SQL\uff0c\u638c\u63e1\u6578\u64da\u5eab\u6280\u80fd (sql\u5fc5\u77e5\u5fc5\u6703 \u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e4%ba%86%e8%a7%a3sql%ef%bc%8c%e6%8e%8c%e6%8f%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8a%80%e8%83%bd-sql%e5%bf%85%e7%9f%a5%e5%bf%85%e6%9c%83-%e6%95%b8%e6%93%9a%e5%ba%ab-fgYpMWMoZY\">\u4e86\u89e3SQL\uff0c\u638c\u63e1\u6578\u64da\u5eab\u6280\u80fd (SQL\u5fc5\u77e5\u5fc5\u6703 \u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff08DBMS\uff09\u626e\u6f14\u8457\u81f3\u95dc\u91cd\u8981\u7684\u89d2\u8272\u3002SQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u7684\u4e3b\u8981\u8a9e\u8a00\uff0c\u7121\u8ad6\u662f\u7528\u65bc\u6578\u64da\u6aa2\u7d22\u3001\u66f4\u65b0\u9084\u662f\u7ba1\u7406\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eSQL\u7684\u57fa\u672c\u6982\u5ff5\u3001\u5e38\u7528\u8a9e\u6cd5\u53ca\u5176\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afsql%ef%bc%9f-fgYpMWMoZY\">\u4ec0\u9ebc\u662fSQL\uff1f<\/h2>\n<p>SQL\uff0c\u5373\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff0c\u662f\u4e00\u7a2e\u5c08\u9580\u7528\u65bc\u7ba1\u7406\u548c\u64cd\u4f5c\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u7de8\u7a0b\u8a9e\u8a00\u3002\u5b83\u5141\u8a31\u7528\u6236\u5275\u5efa\u3001\u8b80\u53d6\u3001\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\uff08\u901a\u5e38\u7c21\u7a31\u70baCRUD\u64cd\u4f5c\uff09\u3002SQL\u7684\u6a19\u6e96\u5316\u4f7f\u5f97\u4e0d\u540c\u7684\u6578\u64da\u5eab\u7cfb\u7d71\uff08\u5982MySQL\u3001PostgreSQL\u3001Oracle\u7b49\uff09\u90fd\u80fd\u5920\u4f7f\u7528\u76f8\u4f3c\u7684\u8a9e\u6cd5\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"sql%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-fgYpMWMoZY\">SQL\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684SQL\u8a9e\u6cd5\u793a\u4f8b\uff0c\u5e6b\u52a9\u60a8\u5feb\u901f\u4e0a\u624b\uff1a<\/p>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab-fgYpMWMoZY\">1. \u5275\u5efa\u6578\u64da\u5eab<\/h3>\n<pre><code>CREATE DATABASE my_database;<\/code><\/pre>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba%e8%a1%a8-fgYpMWMoZY\">2. \u5275\u5efa\u8868<\/h3>\n<pre><code>CREATE TABLE users (\n    id INT PRIMARY KEY AUTO_INCREMENT,\n    username VARCHAR(50) NOT NULL,\n    email VARCHAR(100) NOT NULL\n);<\/code><\/pre>\n<h3 id=\"3-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-fgYpMWMoZY\">3. \u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com');<\/code><\/pre>\n<h3 id=\"4-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-fgYpMWMoZY\">4. \u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code>SELECT * FROM users;<\/code><\/pre>\n<h3 id=\"5-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-fgYpMWMoZY\">5. \u66f4\u65b0\u6578\u64da<\/h3>\n<pre><code>UPDATE users SET email = 'john.doe@example.com' WHERE username = 'john_doe';<\/code><\/pre>\n<h3 id=\"6-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-fgYpMWMoZY\">6. \u522a\u9664\u6578\u64da<\/h3>\n<pre><code>DELETE FROM users WHERE username = 'john_doe';<\/code><\/pre>\n<h2 id=\"sql%e7%9a%84%e9%80%b2%e9%9a%8e%e5%8a%9f%e8%83%bd-fgYpMWMoZY\">SQL\u7684\u9032\u968e\u529f\u80fd<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684CRUD\u64cd\u4f5c\uff0cSQL\u9084\u63d0\u4f9b\u4e86\u8a31\u591a\u9032\u968e\u529f\u80fd\uff0c\u5e6b\u52a9\u7528\u6236\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002<\/p>\n<h3 id=\"1-%e8%81%af%e6%8e%a5%ef%bc%88join%ef%bc%89-fgYpMWMoZY\">1. \u806f\u63a5\uff08JOIN\uff09<\/h3>\n<p>\u806f\u63a5\u662fSQL\u4e2d\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u5141\u8a31\u7528\u6236\u5f9e\u591a\u500b\u8868\u4e2d\u6aa2\u7d22\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u5167\u90e8\u806f\u63a5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>SELECT users.username, orders.amount\nFROM users\nJOIN orders ON users.id = orders.user_id;<\/code><\/pre>\n<h3 id=\"2-%e8%81%9a%e5%90%88%e5%87%bd%e6%95%b8-fgYpMWMoZY\">2. \u805a\u5408\u51fd\u6578<\/h3>\n<p>\u805a\u5408\u51fd\u6578\u5982COUNT\u3001SUM\u3001AVG\u7b49\u53ef\u4ee5\u7528\u4f86\u5c0d\u6578\u64da\u9032\u884c\u7d71\u8a08\u5206\u6790\u3002\u4f8b\u5982\uff0c\u8a08\u7b97\u7528\u6236\u6578\u91cf\uff1a<\/p>\n<pre><code>SELECT COUNT(*) FROM users;<\/code><\/pre>\n<h3 id=\"3-%e5%ad%90%e6%9f%a5%e8%a9%a2-fgYpMWMoZY\">3. \u5b50\u67e5\u8a62<\/h3>\n<p>\u5b50\u67e5\u8a62\u662f\u5d4c\u5957\u5728\u5176\u4ed6\u67e5\u8a62\u4e2d\u7684\u67e5\u8a62\uff0c\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u66f4\u8907\u96dc\u7684\u6578\u64da\u6aa2\u7d22\uff1a<\/p>\n<pre><code>SELECT username FROM users\nWHERE id IN (SELECT user_id FROM orders WHERE amount &gt; 100);<\/code><\/pre>\n<h2 id=\"%e5%ad%b8%e7%bf%92sql%e7%9a%84%e8%b3%87%e6%ba%90-fgYpMWMoZY\">\u5b78\u7fd2SQL\u7684\u8cc7\u6e90<\/h2>\n<p>\u5b78\u7fd2SQL\u7684\u8cc7\u6e90\u975e\u5e38\u8c50\u5bcc\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u85a6\u7684\u5b78\u7fd2\u9014\u5f91\uff1a<\/p>\n<ul>\n<li>\u5728\u7dda\u8ab2\u7a0b\uff1a\u5982Coursera\u3001Udemy\u7b49\u5e73\u53f0\u63d0\u4f9b\u7684SQL\u8ab2\u7a0b\u3002<\/li>\n<li>\u66f8\u7c4d\uff1a\u5982\u300aSQL Fundamentals\u300b\u548c\u300aLearning SQL\u300b\u7b49\u66f8\u7c4d\u3002<\/li>\n<li>\u5be6\u8e10\uff1a\u4f7f\u7528MySQL\u6216PostgreSQL\u7b49\u958b\u6e90\u6578\u64da\u5eab\u9032\u884c\u5be6\u8e10\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-fgYpMWMoZY\">\u7e3d\u7d50<\/h2>\n<p>\u638c\u63e1SQL\u4e0d\u50c5\u80fd\u63d0\u5347\u60a8\u7684\u6578\u64da\u5eab\u7ba1\u7406\u6280\u80fd\uff0c\u9084\u80fd\u5e6b\u52a9\u60a8\u5728\u6578\u64da\u5206\u6790\u548c\u61c9\u7528\u958b\u767c\u4e2d\u66f4\u6709\u6548\u5730\u8655\u7406\u6578\u64da\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u5c0d\u65bc\u6578\u64da\u5eab\u6280\u80fd\u7684\u9700\u6c42\u4e5f\u5728\u4e0d\u65b7\u4e0a\u5347\u3002\u7121\u8ad6\u60a8\u662f\u521d\u5b78\u8005\u9084\u662f\u6709\u7d93\u9a57\u7684\u958b\u767c\u8005\uff0c\u5b78\u7fd2SQL\u90fd\u662f\u4e00\u9805\u503c\u5f97\u6295\u8cc7\u7684\u6280\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1SQL\u6280\u80fd\uff0c\u6df1\u5165\u4e86\u89e3\u6578\u64da\u5eab\u7ba1\u7406\uff0c\u63d0\u5347\u6578\u64da\u5206\u6790\u80fd\u529b\uff0c\u52a9\u4f60\u5728\u8077\u5834\u4e2d\u812b\u7a4e\u800c\u51fa\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-166839","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=166839"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166839\/revisions"}],"predecessor-version":[{"id":166840,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166839\/revisions\/166840"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}