{"id":944,"date":"2022-03-20T13:08:19","date_gmt":"2022-03-20T05:08:19","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=944"},"modified":"2022-03-20T13:27:16","modified_gmt":"2022-03-20T05:27:16","slug":"7-sql%e8%af%ad%e5%8f%a5%e4%b9%8b%e5%85%83%e6%95%b0%e6%8d%ae%e8%8e%b7%e5%8f%96%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=944","title":{"rendered":"7.SQL\u8bed\u53e5\u4e4b\u5143\u6570\u636e\u83b7\u53d6\u6848\u4f8b"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.show\u8bed\u53e5\u83b7\u53d6<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>show databases\uff1b\r\nshow tables;\r\nshow privileges;\r\nshow grants for;\r\nshow create database\/table\r\nshow charset\r\nshow collation\r\nshow processlist\r\nshow variables\r\nshow status\r\nshow index from\r\nshow engines\r\nshow binary logs\r\nshow binlog events in\r\nshow master status\r\nshow slave status\r\nshow slave hosts\r\nshow plugins\r\nshow engine innodb status\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.information_schema<\/h2>\n\n\n\n<p><strong><em>1.\u4ecb\u7ecd<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u89c6\u56fe.\u67e5\u8be2\u5143\u6570\u636e\u7684\u89c6\u56fe\u53ca\u65b9\u6cd5,\u53ef\u4ee5\u770b\u6210\u7ed9\u8bed\u53e5\u8bbe\u7f6e\u4e2a\u522b\u540d\nmysql> select  student.sid, student.sname,avg(num),count(course.cname),group_concat(cname) from student join score  join course on student.sid=score.student_id where score.course_id=course.cid group by student.sid,student.sname having avg(num)>60 ;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"212\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-78.png\" alt=\"\" class=\"wp-image-945\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-78.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-78-300x74.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-78-768x188.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><figcaption>\u8fd9\u4e2a\u8bed\u53e5\u4e5f\u592a\u957f\u4e86\u5427<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u7ed9\u4e0a\u9762\u7684\u8bed\u53e5\u521b\u5efa\u4e2a\u89c6\u56fe<\/strong>\nmysql> create view sfy as  (select  student.sid, student.sname,avg(num),count(course.cname),group_concat(cname) from student join score  join courrse on student.sid=score.student_id where score.course_id=course.cid group by student.sid,student.sname having avg(num)>60) ;\n<strong>\u7136\u540e\u6267\u884c\u5982\u4e0b\u547d\u4ee4###\u53ef\u4ee5\u770b\u5230\u67e5\u8be2\u7ed3\u679c\u4e0e\u4e0a\u9762\u7684\u4e00\u81f4<\/strong>\r\nmysql> select * from sfy;\r<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"253\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-79.png\" alt=\"\" class=\"wp-image-946\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-79.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-79-300x88.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-79-768x225.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><strong><em>2.tables\u89c6\u56fe\u5e94\u7528<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql> use information_schema;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"789\" height=\"312\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-80.png\" alt=\"\" class=\"wp-image-947\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-80.png 789w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-80-300x119.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-80-768x304.png 768w\" sizes=\"(max-width: 789px) 100vw, 789px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>TABLE_SCHEMA\uff08\u8868\u6240\u5728\u5e93\uff09\r\nTABLE_NAME\uff08\u8868\u540d\uff09\r\nENGINE\uff08\u5b58\u50a8\u5f15\u64ce\uff09\r\nTABLE_ROWS\uff08\u8868\u7684\u884c\u6570\uff08\u7c97\u7565\u7edf\u8ba1\uff09\uff09\r\nAVG_ROW_LENGTH\uff08\u5e73\u5747\u884c\u957f\u5ea6\uff08\u7c97\u7565\u7edf\u8ba1\uff09\uff09\r\nINDEX_LENGTH\uff08\u7d22\u5f15\u957f\u5ea6\uff08\u7c97\u7565\u7edf\u8ba1\uff09\uff09\r\nDATA_FREE(\u788e\u7247\u91cf)\r\nTABLE_COMMENT\u8868\u6ce8\u91ca\r<\/code><\/pre>\n\n\n\n<p><strong>\u4f8b\u5b501.<\/strong>\u7edf\u8ba1\u6240\u6709\u5e93\uff0c\u5bf9\u5e94\u7684\u8868\u4e2a\u6570\u548c\u540d\u5b57\u5217\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5e93\u540d  \u8868\u4e2a\u6570   \u8868\u540d\r\nworld  3       city\uff0ca,b\r\nmysql> select TABLE_SCHEMA as '\u5e93\u540d',count(TABLE_NAME) as '\u8868\u4e2a\u6570',group_concat(TABLE_NAME) as '\u8868\u540d' from tables group by TABLE_SCHEMA having  \u8868\u4e2a\u6570&lt;10 ;\r<\/code><\/pre>\n\n\n\n<p><strong>\u4f8b\u5b502<\/strong>.\u7edf\u8ba1\u6bcf\u4e2a\u5e93\u7684\u6570\u636e\u91cf\u5927\u5c0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql> select TABLE_SCHEMA as '\u5e93\u540d',sum(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH) as '\u6570\u636e\u5927\u5c0f' from tables group by TABLE_SCHEMA ;<\/code><\/pre>\n\n\n\n<p><strong>\u4f8b\u5b503.<\/strong>\u62fc\u63a5\u67e5\u8be2\uff0c\u67e5\u8be2\u4e00\u4e0b\u4e1a\u52a1\u6570\u636e\u5e93\u4e2d\uff0c\u975eInnodb\u7684\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql> create table test.t2 (id int)engine=myisam;\r\nmysql> select TABLE_SCHEMA,TABLE_NAME,ENGINE from information_schema.tables where TABLE_SCHEMA not in ('information_schema','performance_schema','sys','mysql') and (ENGINE!='InnoDB' or ENGINE is null);\n\r<\/code><\/pre>\n\n\n\n<p><strong>\u4f8b\u5b504.<\/strong>\u5c06\u4f8b3\u6267\u884c\u7ed3\u679c\u8fdb\u884c\u62fc\u63a5\uff0c\u5c06\u975eInnodb\u7684\u8868\u66ff\u6362\u4e3aInnodb\u7684\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql> select concat(\"alter table \",TABLE_SCHEMA,\".\",TABLE_NAME,\" ENGINE=InnoDB\") from information_schema.tables where TABLE_SCHEMA not in ('information_schema','performance_schema','sys','mysql','school') and (ENGINE!='InnoDB' or ENGINE is null);\n<strong>\u53ef\u4ee5\u5c06\u5176\u8f93\u51fa\u5230\u6587\u4ef6\u91cc\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u91cc\u6dfb\u52a0\u53c2\u6570secure-file-priv=\/tmp<\/strong>\nselect concat(\"alter table \",TABLE_SCHEMA,\".\",TABLE_NAME,\" ENGINE=InnoDB\") from information_schema.tables where TABLE_SCHEMA not in ('information_schema','performance_schema','sys','mysql','school') and (ENGINE!='InnoDB' or ENGINE is null) into outfile '\/tmp\/sql.sql';<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"31\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-82.png\" alt=\"\" class=\"wp-image-954\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-82.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-82-300x11.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-82-768x28.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><figcaption>\u5982\u679c\u4e0d\u6dfb\u52a0\u914d\u7f6e\u6587\u4ef6\u53c2\u6570\uff0c\u4f1a\u62a5\u5982\u4e0a\u9519\u8bef<\/figcaption><\/figure>\n\n\n\n<p>\u5bfc\u51fa\u62fc\u63a5\u7684\u6587\u4ef6\uff0c\u7136\u540esource\u6587\u4ef6\u6279\u91cf\u4fee\u6539\u8868\u7684\u5b58\u50a8\u5f15\u64ce\u4e3aInnoDB<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"260\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-83.png\" alt=\"\" class=\"wp-image-955\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-83.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-83-300x90.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-83-768x231.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"264\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-84.png\" alt=\"\" class=\"wp-image-956\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-84.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-84-300x92.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-84-768x234.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><strong>\u4f8b\u5b504.<\/strong>\u5c06\u6267\u884c\u7ed3\u679c\u8fdb\u884c\u62fc\u63a5\uff0c\u5b9e\u73b0\u6240\u6709\u5e93\u7684\u5355\u8868\u5907\u4efd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql> select concat(\"mysqldump \",TABLE_SCHEMA,\" \",TABLE_NAME,\" > \/tmp\/\",\"\",TABLE_SCHEMA,\"_\",TABLE_NAME,\".sql\") from information_schema.tables where TABLE_SCHEMA not in ('information_schema','performance_schema','sys','mysql')  into outfile '\/tmp\/alter_test.sh' ;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"436\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-85.png\" alt=\"\" class=\"wp-image-957\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-85.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-85-300x151.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-85-768x387.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><strong><em>3.COLUMNS\u89c6\u56fe<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TABLE_SCHEMA\r\nTABLE_NAME\r\nCOLUMN_NAME\r\nDATA_TYPE\r\nCOLUMN_KEY\r\nCOLUMN_COMMENT\r<\/code><\/pre>\n\n\n\n<p><strong>\u4f8b\u5b50.<\/strong>\u67e5\u8be2\u4e00\u4e0b\u6bcf\u4e2a\u5e93\u6bcf\u5f20\u8868\u90fd\u6709\u54ea\u4e9b\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql> select TABLE_SCHEMA,TABLE_NAME,group_concat(COLUMN_NAME) from COLUMNS where TABLE_SCHEMA not in ('information_schema','performance_schema','sys','mysql') group by TABLE_SCHEMA,TABLE_NAME;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"230\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-86.png\" alt=\"\" class=\"wp-image-960\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-86.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-86-300x80.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-86-768x204.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/944"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=944"}],"version-history":[{"count":5,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/944\/revisions"}],"predecessor-version":[{"id":961,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/944\/revisions\/961"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}