{"id":200129,"date":"2025-05-06T10:15:41","date_gmt":"2025-05-06T02:15:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200129\/"},"modified":"2025-05-06T10:15:41","modified_gmt":"2025-05-06T02:15:41","slug":"%e5%9f%ba%e6%9c%ac-mysql-%e6%9f%a5%e8%af%a2%ef%bc%9a%e7%bb%bc%e5%90%88%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200129\/","title":{"rendered":"\u57fa\u672c MySQL \u67e5\u8be2\uff1a\u7efc\u5408\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u57fa\u672c MySQL \u67e5\u8be2\uff1a\u7efc\u5408\u6307\u5357<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6570\u636e\u5e93\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u57fa\u672c MySQL \u67e5\u8be2\uff1a\u7efc\u5408\u6307\u5357\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240824\/172446968966c951b9e12a2.jpg\" class=\"aligncenter\"><\/p>\n<h2> <em><em>\u4f5c\u8005\uff1anirmalya mondal<\/em><\/em> <\/h2>\n<h3> \u4ecb\u7ecd <\/h3>\n<p>mysql \u662f\u7528\u4e8e web \u5e94\u7528\u7a0b\u5e8f\u548c\u5176\u4ed6\u6570\u636e\u9a71\u52a8\u5e94\u7528\u7a0b\u5e8f\u7684\u6700\u6d41\u884c\u7684\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf (rdbms) \u4e4b\u4e00\u3002\u65e0\u8bba\u60a8\u662f\u521d\u5b66\u8005\u8fd8\u662f\u60f3\u8981\u63d0\u9ad8 mysql \u6280\u80fd\u7684\u4eba\uff0c\u4e86\u89e3\u57fa\u672c\u67e5\u8be2\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u672c\u535a\u5ba2\u5c06\u5f15\u5bfc\u60a8\u5b8c\u6210\u4e00\u4e9b\u57fa\u672c\u7684 mysql \u67e5\u8be2\uff0c\u53ef\u7528\u4e8e\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u8868\u64cd\u4f5c\u548c\u6570\u636e\u7ba1\u7406\u3002<\/p>\n<h3> 1. \u6570\u636e\u5e93\u64cd\u4f5c <\/h3>\n<h4> <strong>\u521b\u5efa\u6570\u636e\u5e93<\/strong> <\/h4>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u4e00\u4e2a\u6570\u636e\u5e93\u6765\u5b58\u50a8\u8868\u548c\u6570\u636e\u3002\u521b\u5efa\u6570\u636e\u5e93\u5f88\u7b80\u5355\uff1a<\/p>\n<pre>create database my_database;\n<\/pre>\n<h4> <strong>\u9009\u62e9\u6570\u636e\u5e93<\/strong> <\/h4>\n<p>\u521b\u5efa\u6570\u636e\u5e93\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\u6765\u9009\u62e9\u5b83\uff1a<\/p>\n<pre>use my_database;\n<\/pre>\n<h4> <strong>\u5220\u9664\u6570\u636e\u5e93<\/strong> <\/h4>\n<p>\u5982\u679c\u9700\u8981\u5220\u9664\u6570\u636e\u5e93\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>drop database my_database;\n<\/pre>\n<h3> 2. \u8868\u64cd\u4f5c <\/h3>\n<h4> <strong>\u521b\u5efa\u8868<\/strong> <\/h4>\n<p>\u8868\u662f\u5b58\u50a8\u6570\u636e\u7684\u5730\u65b9\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u5305\u542b\u7279\u5b9a\u5217\u7684\u8868\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>create table users (\n    id int auto_increment primary key,\n    name varchar(100),\n    email varchar(100),\n    age int\n);\n<\/pre>\n<h4> <strong>\u663e\u793a\u8868\u683c<\/strong> <\/h4>\n<p>\u8981\u67e5\u770b\u6240\u9009\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u8868\uff1a<\/p>\n<pre>show tables;\n<\/pre>\n<h4> <strong>\u63cf\u8ff0\u8868\u7ed3\u6784<\/strong> <\/h4>\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u8868\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u63cf\u8ff0\u4e00\u4e0b\uff1a<\/p>\n<pre>describe users;\n<\/pre>\n<h4> <strong>\u66f4\u6539\u8868<\/strong> <\/h4>\n<p>\u5982\u679c\u60a8\u9700\u8981\u901a\u8fc7\u6dfb\u52a0\u6216\u66f4\u6539\u5217\u6765\u4fee\u6539\u8868\u683c\uff1a<\/p>\n<ul>\n<li> <strong>\u6dfb\u52a0\u4e13\u680f<\/strong> <\/li>\n<\/ul>\n<pre>  alter table users add phone varchar(15);\n<\/pre>\n<ul>\n<li> <strong>\u4fee\u6539\u5217<\/strong> <\/li>\n<\/ul>\n<pre>  alter table users modify age tinyint;\n<\/pre>\n<h4> <strong>\u6389\u843d\u684c\u5b50<\/strong> <\/h4>\n<p>\u5220\u9664\u8868\uff1a<\/p>\n<pre>drop table users;\n<\/pre>\n<h3> 3. \u6570\u636e\u64cd\u4f5c <\/h3>\n<h4> <strong>\u63d2\u5165\u6570\u636e<\/strong> <\/h4>\n<p>\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u8868\u4e2d\uff1a<\/p>\n<pre>insert into users (name, email, age) values ('john doe', 'john@example.com', 25);\n<\/pre>\n<h4> <strong>\u9009\u62e9\u6570\u636e<\/strong> <\/h4>\n<p>\u4ece\u8868\u4e2d\u68c0\u7d22\u6570\u636e\uff1a<\/p>\n<pre>select name, email from users where age &gt; 20;\n<\/pre>\n<h4> <strong>\u9009\u62e9\u6240\u6709\u6570\u636e<\/strong> <\/h4>\n<p>\u8981\u68c0\u7d22\u8868\u4e2d\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n<pre>select * from users;\n<\/pre>\n<h4> <strong>\u66f4\u65b0\u6570\u636e<\/strong> <\/h4>\n<p>\u66f4\u65b0\u8868\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n<pre>update users set age = 26 where name = 'john doe';\n<\/pre>\n<h4> <strong>\u5220\u9664\u6570\u636e<\/strong> <\/h4>\n<p>\u8981\u4ece\u8868\u4e2d\u5220\u9664\u6570\u636e\uff1a<\/p>\n<pre>delete from users where name = 'john doe';\n<\/pre>\n<h3> 4. \u6761\u4ef6\u67e5\u8be2 <\/h3>\n<h4> <strong>where \u5b50\u53e5<\/strong> <\/h4>\n<p>\u4f7f\u7528where\u5b50\u53e5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fc7\u6ee4\u8bb0\u5f55\uff1a<\/p>\n<pre>select * from users where age &gt; 20;\n<\/pre>\n<h4> <strong>\u548c\/\u6216\u6761\u4ef6<\/strong> <\/h4>\n<p>\u4f7f\u7528 and \u6216 or \u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\uff1a<\/p>\n<pre>select * from users where age &gt; 20 and name = 'john doe';\n<\/pre>\n<h4> <strong>in \u5b50\u53e5<\/strong> <\/h4>\n<p>\u6839\u636e\u503c\u5217\u8868\u9009\u62e9\u6570\u636e\uff1a<\/p>\n<pre>select * from users where age in (20, 25, 30);\n<\/pre>\n<h4> <strong>between \u5b50\u53e5<\/strong> <\/h4>\n<p>\u8fc7\u6ee4\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6570\u636e\uff1a<\/p>\n<pre>select * from users where age between 20 and 30;\n<\/pre>\n<h4> <strong>like\u6761\u6b3e<\/strong> <\/h4>\n<p>\u4f7f\u7528 like \u5b50\u53e5\u641c\u7d22\u6a21\u5f0f\uff1a<\/p>\n<pre>select * from users where name like 'j%';\n<\/pre>\n<h4> <strong>is null \/ is not null<\/strong> <\/h4>\n<p>\u8fc7\u6ee4\u5177\u6709 null \u6216 not null \u503c\u7684\u8bb0\u5f55\uff1a<\/p>\n<pre>select * from users where email is null;\n<\/pre>\n<h3> 5.\u805a\u5408\u51fd\u6570 <\/h3>\n<h4> <strong>count <\/strong> <\/h4>\n<p>\u8ba1\u7b97\u884c\u6570\uff1a<\/p>\n<pre>select count(*) from users;\n<\/pre>\n<h4> <strong>\u603b\u548c<\/strong> <\/h4>\n<p>\u8ba1\u7b97\u5217\u7684\u603b\u548c\uff1a<\/p>\n<pre>select sum(age) from users;\n<\/pre>\n<h4> <strong>avg<\/strong> <\/h4>\n<p>\u6c42\u4e00\u5217\u7684\u5e73\u5747\u503c\uff1a<\/p>\n<pre>select avg(age) from users;\n<\/pre>\n<h4> <strong>\u6700\u5927\u548c\u6700\u5c0f<\/strong> <\/h4>\n<p>\u67e5\u627e\u4e00\u5217\u7684\u6700\u5927\u503c\u6216\u6700\u5c0f\u503c\uff1a<\/p>\n<pre>select max(age) from users;\n<\/pre>\n<pre>select min(age) from users;\n<\/pre>\n<h3> 6. \u5206\u7ec4\u548c\u6392\u5e8f <\/h3>\n<h4> <strong>\u5206\u7ec4\u4f9d\u636e<\/strong> <\/h4>\n<p>\u6839\u636e\u4e00\u5217\u6216\u591a\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<pre>select age, count(*) from users group by age;\n<\/pre>\n<h4> <strong>\u62e5\u6709<\/strong> <\/h4>\n<p>\u8fc7\u6ee4\u5206\u7ec4\u6570\u636e\uff1a<\/p>\n<pre>select age, count(*) from users group by age having count(*) &gt; 1;\n<\/pre>\n<h4> <strong>\u8ba2\u8d2d\u4f9d\u636e<\/strong> <\/h4>\n<p>\u6309\u5347\u5e8f\u6216\u964d\u5e8f\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>select * from users order by age desc;\n<\/pre>\n<h3> 7. \u52a0\u5165\u64cd\u4f5c <\/h3>\n<h4> <strong>\u5185\u8fde\u63a5<\/strong> <\/h4>\n<p>\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u540c\u65f6\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\uff1a<\/p>\n<pre>select users.name, orders.order_date from users\ninner join orders on users.id = orders.user_id;\n<\/pre>\n<h4> <strong>\u5de6\u52a0\u5165<\/strong> <\/h4>\n<p>\u4ece\u5de6\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u4ece\u53f3\u8868\u4e2d\u83b7\u53d6\u5339\u914d\u7684\u884c\uff1a<\/p>\n<pre>select users.name, orders.order_date from users\nleft join orders on users.id = orders.user_id;\n<\/pre>\n<h4> <strong>\u53f3\u52a0\u5165<\/strong> <\/h4>\n<p>\u4ece\u53f3\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u4ece\u5de6\u8868\u4e2d\u83b7\u53d6\u5339\u914d\u7684\u884c\uff1a<\/p>\n<pre>select users.name, orders.order_date from users\nright join orders on users.id = orders.user_id;\n<\/pre>\n<h3> 8. \u5b50\u67e5\u8be2 <\/h3>\n<h4> <strong>where \u4e2d\u7684\u5b50\u67e5\u8be2<\/strong> <\/h4>\n<p>\u4f7f\u7528\u5b50\u67e5\u8be2\u6765\u8fc7\u6ee4\u7ed3\u679c\uff1a<\/p>\n<pre>select name from users where id = (select user_id from orders where order_id = 1);\n<\/pre>\n<h4> <strong>select \u4e2d\u7684\u5b50\u67e5\u8be2<\/strong> <\/h4>\n<p>\u4f7f\u7528\u5b50\u67e5\u8be2\u6765\u8ba1\u7b97\u503c\uff1a<\/p>\n<pre>select name, (select count(*) from orders where users.id = orders.user_id) as order_count\nfrom users;\n<\/pre>\n<h3> 9. \u610f\u89c1 <\/h3>\n<h4> <strong>\u521b\u5efa\u89c6\u56fe<\/strong> <\/h4>\n<p>\u6839\u636e\u67e5\u8be2\u521b\u5efa\u865a\u62df\u8868\uff1a<\/p>\n<pre>create view user_orders as\nselect users.name, orders.order_date from users\ninner join orders on users.id = orders.user_id;\n<\/pre>\n<h4> <strong>\u4e0b\u62c9\u89c6\u56fe<\/strong> <\/h4>\n<p>\u5220\u9664\u89c6\u56fe\uff1a<\/p>\n<pre>drop view user_orders;\n<\/pre>\n<h3> 10. \u7d22\u5f15 <\/h3>\n<h4> <strong>\u521b\u5efa\u7d22\u5f15<\/strong> <\/h4>\n<p>\u901a\u8fc7\u521b\u5efa\u7d22\u5f15\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\uff1a<\/p>\n<pre>create index idx_name on users (name);\n<\/pre>\n<h4> <strong>\u6389\u843d\u6307\u6570<\/strong> <\/h4>\n<p>\u5220\u9664\u7d22\u5f15\uff1a<\/p>\n<pre>DROP INDEX idx_name ON users;\n<\/pre>\n<h3> \u7ed3\u8bba <\/h3>\n<p>\u7406\u89e3\u8fd9\u4e9b\u57fa\u672c\u7684 mysql \u67e5\u8be2\u5bf9\u4e8e\u4efb\u4f55\u4f7f\u7528\u5173\u7cfb\u6570\u636e\u5e93\u7684\u4eba\u6765\u8bf4\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u65e0\u8bba\u60a8\u662f\u7ba1\u7406\u6570\u636e\u3001\u4f18\u5316\u67e5\u8be2\u8fd8\u662f\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff0c\u8fd9\u4e9b\u547d\u4ee4\u90fd\u6784\u6210\u4e86\u60a8\u7684 mysql \u6280\u80fd\u7684\u57fa\u7840\u3002\u901a\u8fc7\u638c\u63e1\u5b83\u4eec\uff0c\u60a8\u5c06\u80fd\u591f\u8f7b\u677e\u5904\u7406\u5927\u591a\u6570\u4e0e\u6570\u636e\u5e93\u76f8\u5173\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u57fa\u672c MySQL \u67e5\u8be2\uff1a\u7efc\u5408\u6307\u5357\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664 <\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c MySQL \u67e5\u8be2\uff1a\u7efc\u5408\u6307\u5357&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200129","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200129","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200129"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200129\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}