{"id":38,"date":"2020-04-22T21:00:27","date_gmt":"2020-04-22T13:00:27","guid":{"rendered":"http:\/\/iotch.xyz\/?p=38"},"modified":"2020-04-22T21:00:27","modified_gmt":"2020-04-22T13:00:27","slug":"mysql%e5%b8%b8%e7%94%a8%e5%9f%ba%e6%9c%acsql%e8%af%ad%e5%8f%a5%e6%80%bb%e7%bb%93-mysql","status":"publish","type":"post","link":"https:\/\/www.iotch.xyz\/?p=38","title":{"rendered":"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3&#8212;mysql"},"content":{"rendered":"\n<p>SQL\u5206\u7c7b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;www.cnblogs.com\/Mrwxs\/articles\/8613470.html<\/code><\/pre>\n\n\n\n<p>DDL-----\u6570\u636e\u5b9a\u4e49\u8bed\u8a00(CREATE--\u521b\u5efa,ALTER--\u4fee\u6539. DROP--\u5220\u9664\u8868,DECLARE--\u58f0\u660e)<\/p>\n\n\n\n<p>DML-----\u6570\u636e\u5b9a\u4e49\u8bed\u8a00(SELECT--\u67e5\u8be2,DELECT--\u5220\u9664\u6570\u636e,UPDATE--\u66f4\u65b0,INSERT--\u63d2\u5165)<\/p>\n\n\n\n<p>DC<\/p>\n\n\n\n<p>MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql - \u9b4f\u5148\u751f - \u535a\u5ba2\u56ed<\/p>\n\n\n\n<p><strong>\u4e00:mysql\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n\n\n\n<p><strong>1.1 \u6574\u6570\u7c7b\u578b-----\u6700\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n\n\n\n<p>&nbsp;\u4e25\u683c\u6570\u503c\u6570\u636e\u7c7b\u578b(<strong>INTEGER\u3001SMALLINT\u3001DECIMAL<\/strong>\u548c<strong>NUMERIC<\/strong>)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u7c7b\u578b<\/strong><\/td><td><strong>\u5927\u5c0f<\/strong><\/td><td><strong>\u8303\u56f4\uff08\u6709\u7b26\u53f7\uff09<\/strong><\/td><td><strong>\u8303\u56f4\uff08\u65e0\u7b26\u53f7\uff09<\/strong><\/td><td><strong>\u7528\u9014<\/strong><\/td><\/tr><tr><td>TINYINT<\/td><td>1 \u5b57\u8282<\/td><td>(-128\uff0c127)<\/td><td>(0\uff0c255)<\/td><td>\u5c0f\u6574\u6570\u503c<\/td><\/tr><tr><td>SMALLINT<\/td><td>2 \u5b57\u8282<\/td><td>(-32 768\uff0c32 767)<\/td><td>(0\uff0c65 535)<\/td><td>\u5927\u6574\u6570\u503c<\/td><\/tr><tr><td>MEDIUMINT<\/td><td>3 \u5b57\u8282<\/td><td>(-8 388 608\uff0c8 388 607)<\/td><td>(0\uff0c16 777 215)<\/td><td>\u5927\u6574\u6570\u503c<\/td><\/tr><tr><td>INT\u6216INTEGER<\/td><td>4 \u5b57\u8282<\/td><td>(-2 147 483 648\uff0c2 147 483 647)<\/td><td>(0\uff0c4 294 967 295)<\/td><td>\u5927\u6574\u6570\u503c<\/td><\/tr><tr><td>BIGINT<\/td><td>8 \u5b57\u8282<\/td><td>(-9 233 372 036 854 775 808\uff0c9 223 372 036 854 775 807)<\/td><td>(0\uff0c18 446 744 073 709 551 615)<\/td><td>\u6781\u5927\u6574\u6570\u503c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.2 \u6d6e\u70b9\u6570\u7c7b\u578b\u548c\u5b9a\u70b9\u6570\u7c7b\u578b<\/strong><\/p>\n\n\n\n<p>\u8fd1\u4f3c\u6570\u503c\u6570\u636e\u7c7b\u578b<strong>(FLOAT\u3001REAL<\/strong>\u548c<strong>DOUBLE PRECISION<\/strong>)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>FLOAT<\/td><td>4 \u5b57\u8282<\/td><td>(-3.402 823 466 E+38\uff0c-1.175 494 351 E-38)\uff0c0\uff0c(1.175 494 351 E-38\uff0c3.402 823 466 351 E+38)<\/td><td>0\uff0c(1.175 494 351 E-38\uff0c3.402 823 466 E+38)<\/td><td>\u5355\u7cbe\u5ea6<br>\u6d6e\u70b9\u6570\u503c<\/td><\/tr><tr><td>DOUBLE<\/td><td>8 \u5b57\u8282<\/td><td>(-1.797 693 134 862 315 7 E+308\uff0c-2.225 073 858 507 201 4 E-308)\uff0c0\uff0c(2.225 073 858 507 201 4 E-308\uff0c1.797 693 134 862 315 7 E+308)<\/td><td>0\uff0c(2.225 073 858 507 201 4 E-308\uff0c1.797 693 134 862 315 7 E+308)<\/td><td>\u53cc\u7cbe\u5ea6<br>\u6d6e\u70b9\u6570\u503c<\/td><\/tr><tr><td>DECIMAL &nbsp;&nbsp;<\/td><td>\u5bf9DECIMAL(M,D) \u5982\u679cM&gt;D\uff0c\u4e3aM+2\u5426\u5219\u4e3aD+2<\/td><td>\u4f9d\u8d56\u4e8eM\u548cD\u7684\u503c<\/td><td>\u4f9d\u8d56\u4e8eM\u548cD\u7684\u503c<\/td><td>\u5c0f\u6570\u503c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.3\u65e5\u671f\u4e0e\u65f6\u7c7b\u578b<\/strong><\/p>\n\n\n\n<p>\u8868\u793a\u65f6\u95f4\u503c\u7684\u65e5\u671f\u548c\u65f6\u95f4\u7c7b\u578b\u4e3a<strong>DATETIME\u3001DATE\u3001TIMESTAMP\u3001TIME<\/strong>\u548c<strong>YEAR<\/strong>\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u65f6\u95f4\u7c7b\u578b\u6709\u4e00\u4e2a\u6709\u6548\u503c\u8303\u56f4\u548c\u4e00\u4e2a\"\u96f6\"\u503c\uff0c\u5f53\u6307\u5b9a\u4e0d\u5408\u6cd5\u7684MySQL\u4e0d\u80fd\u8868\u793a\u7684\u503c\u65f6\u4f7f\u7528\"\u96f6\"\u503c\u3002<\/p>\n\n\n\n<p>TIMESTAMP\u7c7b\u578b\u6709\u4e13\u6709\u7684\u81ea\u52a8\u66f4\u65b0\u7279\u6027\uff0c\u5c06\u5728\u540e\u9762\u63cf\u8ff0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u7c7b\u578b<\/strong><\/td><td><strong>\u5927\u5c0f<\/strong><\/td><td><strong>\u8303\u56f4<\/strong><\/td><td><strong>\u683c\u5f0f<\/strong><\/td><td><strong>\u7528\u9014<\/strong><\/td><\/tr><tr><td>DATE<\/td><td>3\u5b57\u8282<\/td><td>1000-01-01\/9999-12-31<\/td><td>YYYY-MM-DD<\/td><td>\u65e5\u671f\u503c<\/td><\/tr><tr><td>TIME<\/td><td>3\u5b57\u8282<\/td><td>'-838:59:59'\/'838:59:59'<\/td><td>HH:MM:SS<\/td><td>\u65f6\u95f4\u503c\u6216\u6301\u7eed\u65f6\u95f4<\/td><\/tr><tr><td>YEAR<\/td><td>1\u5b57\u8282<\/td><td>1901\/2155<\/td><td>YYYY<\/td><td>\u5e74\u4efd\u503c<\/td><\/tr><tr><td>DATETIME<\/td><td>8\u5b57\u8282<\/td><td>1000-01-01 00:00:00\/9999-12-31 23:59:59<\/td><td>YYYY-MM-DD HH:MM:SS<\/td><td>\u6df7\u5408\u65e5\u671f\u548c\u65f6\u95f4\u503c<\/td><\/tr><tr><td>TIMESTAMP<\/td><td>4\u5b57\u8282<\/td><td>1970-01-01 00:00:00\/2038 &nbsp; \u7ed3\u675f\u65f6\u95f4\u662f\u7b2c&nbsp;<strong>2147483647<\/strong>&nbsp;\u79d2\uff0c\u5317\u4eac\u65f6\u95f4&nbsp;<strong>2038-1-19 11:14:07<\/strong>\uff0c\u683c\u6797\u5c3c\u6cbb\u65f6\u95f4 2038\u5e741\u670819\u65e5 \u51cc\u6668 03:14:07<\/td><td>YYYYMMDD HHMMSS<\/td><td>\u6df7\u5408\u65e5\u671f\u548c\u65f6\u95f4\u503c\uff0c\u65f6\u95f4\u6233<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.4 \u5b57\u7b26\u4e32\u7c7b\u578b<\/h2>\n\n\n\n<p>\u5b57\u7b26\u4e32\u7c7b\u578b\u6307<strong>CHAR\u3001VARCHAR\u3001BINARY\u3001VARBINARY\u3001BLOB\u3001TEXT\u3001ENUM<\/strong>\u548c<strong>SET<\/strong>\u3002\u8be5\u8282\u63cf\u8ff0\u4e86\u8fd9\u4e9b\u7c7b\u578b\u5982\u4f55\u5de5\u4f5c\u4ee5\u53ca\u5982\u4f55\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u578b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u7c7b\u578b<\/strong><\/td><td><strong>\u5927\u5c0f<\/strong><\/td><td><strong>\u7528\u9014<\/strong><\/td><\/tr><tr><td>CHAR<\/td><td>0-255\u5b57\u8282<\/td><td>\u5b9a\u957f\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>VARCHAR<\/td><td>0-65535 \u5b57\u8282<\/td><td>\u53d8\u957f\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>TINYBLOB<\/td><td>0-255\u5b57\u8282<\/td><td>\u4e0d\u8d85\u8fc7 255 \u4e2a\u5b57\u7b26\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>TINYTEXT<\/td><td>0-255\u5b57\u8282<\/td><td>\u77ed\u6587\u672c\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>BLOB<\/td><td>0-65 535\u5b57\u8282<\/td><td>\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u957f\u6587\u672c\u6570\u636e<\/td><\/tr><tr><td>TEXT<\/td><td>0-65 535\u5b57\u8282<\/td><td>\u957f\u6587\u672c\u6570\u636e<\/td><\/tr><tr><td>MEDIUMBLOB<\/td><td>0-16 777 215\u5b57\u8282<\/td><td>\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u4e2d\u7b49\u957f\u5ea6\u6587\u672c\u6570\u636e<\/td><\/tr><tr><td>MEDIUMTEXT<\/td><td>0-16 777 215\u5b57\u8282<\/td><td>\u4e2d\u7b49\u957f\u5ea6\u6587\u672c\u6570\u636e<\/td><\/tr><tr><td>LONGBLOB<\/td><td>0-4 294 967 295\u5b57\u8282<\/td><td>\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u6781\u5927\u6587\u672c\u6570\u636e<\/td><\/tr><tr><td>LONGTEXT<\/td><td>0-4 294 967 295\u5b57\u8282<\/td><td>\u6781\u5927\u6587\u672c\u6570\u636e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>CHAR\u548cVARCHAR\u7c7b\u578b\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u4fdd\u5b58\u548c\u68c0\u7d22\u7684\u65b9\u5f0f\u4e0d\u540c\u3002\u5b83\u4eec\u7684\u6700\u5927\u957f\u5ea6\u548c\u662f\u5426\u5c3e\u90e8\u7a7a\u683c\u88ab\u4fdd\u7559\u7b49\u65b9\u9762\u4e5f\u4e0d\u540c\u3002\u5728\u5b58\u50a8\u6216\u68c0\u7d22\u8fc7\u7a0b\u4e2d\u4e0d\u8fdb\u884c\u5927\u5c0f\u5199\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>BINARY\u548cVARBINARY\u7c7b\u7c7b\u4f3c\u4e8eCHAR\u548cVARCHAR\uff0c\u4e0d\u540c\u7684\u662f\u5b83\u4eec\u5305\u542b\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u800c\u4e0d\u8981\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u5305\u542b\u5b57\u8282\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u7b26\u5b57\u7b26\u4e32\u3002\u8fd9\u8bf4\u660e\u5b83\u4eec\u6ca1\u6709\u5b57\u7b26\u96c6\uff0c\u5e76\u4e14\u6392\u5e8f\u548c\u6bd4\u8f83\u57fa\u4e8e\u5217\u503c\u5b57\u8282\u7684\u6570\u503c\u503c\u3002<\/p>\n\n\n\n<p>BLOB\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u53ef\u53d8\u6570\u91cf\u7684\u6570\u636e\u3002\u67094\u79cdBLOB\u7c7b\u578b\uff1aTINYBLOB\u3001BLOB\u3001MEDIUMBLOB\u548cLONGBLOB\u3002\u5b83\u4eec\u53ea\u662f\u53ef\u5bb9\u7eb3\u503c\u7684\u6700\u5927\u957f\u5ea6\u4e0d\u540c\u3002<\/p>\n\n\n\n<p><strong>\u67094\u79cdTEXT\u7c7b\u578b\uff1aTINYTEXT\u3001TEXT\u3001MEDIUMTEXT\u548cLONGTEXT\u3002\u8fd9\u4e9b\u5bf9\u5e944\u79cdBLOB\u7c7b\u578b\uff0c\u6709\u76f8\u540c\u7684\u6700\u5927\u957f\u5ea6\u548c\u5b58\u50a8\u9700\u6c42\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u4e8c:mysql\u670d\u52a1\u7684\u542f\u52a8\u548c\u505c\u6b62<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>net stop mysql \u3000\u3000 --\u542f\u52a8 \nnet start mysql    --\u505c\u6b62 <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321190450159-469220644.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u4e09 \uff1a\u767b\u5f55mysql<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql -h localhost -u root -P <strong>3306<\/strong> -p<\/pre>\n\n\n\n<p><strong>password: *******<\/strong><\/p>\n\n\n\n<p><strong>-h mysql\u8fde\u63a5\u5730\u5740<\/strong><\/p>\n\n\n\n<p><strong>-u mysql\u767b\u5f55\u7528\u6237\u540d<\/strong><\/p>\n\n\n\n<p><strong>-P mysql\u8fde\u63a5\u7aef\u53e3(\u9ed8\u8ba4\u4e3a 3306)<\/strong><\/p>\n\n\n\n<p><strong>-p mysql\u767b\u5f55\u5bc6\u7801<\/strong>(\u4e0d\u5efa\u8bae\u76f4\u63a5\u5728\u540e\u9762\u5199\u5bc6\u7801[\u660e\u6587])<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321190707867-215524635.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u56db:\u5bc6\u7801\u76f4\u63a5\u767b\u5f55mysql<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql -h localhost -u root -p 123456 <\/pre>\n\n\n\n<p><strong>\u4e94:\u9000\u51famysql<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;exit;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321190735185-1871188508.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u516d\uff1a\u67e5\u770b\u6570\u636e\u5e93<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SHOW DATABASES; <\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321191451381-359964281.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u4e03\uff1a\u521b\u5efa\u548c\u5220\u9664\u6570\u636e\u5e93<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;CREATE { DATABASE | SCHEMA } [ IF NOT EXISTS ] db_name [DEFAULT] CHARACTER SET [ = ] charset_name; --\u521b\u5efa\nmysql&gt;DROP DATABASE db_name;\u3000\u3000--\u5220\u9664<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321191309381-1067879701.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321191436504-1621197750.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u516b:\u67e5\u770b\u6570\u636e\u5e93\u5b58\u50a8\u5f15\u64ce\u3001\u4f7f\u7528\u6570\u636e\u5e93\u3001\u67e5\u770b\u5f53\u524d\u4f7f\u7528\u7684\u6570\u636e\u5e93<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SHOW ENGINES;  --\u67e5\u770b\u5b58\u50a8\u5f15\u64ce\nmysql&gt;USE db_name;     --\u4f7f\u7528\u6570\u636e\u5e93\nmysql&gt;SELECT DATABASE(); --\u67e5\u770b\u5f53\u524d\u4f7f\u7528\u7684\u6570\u636e\u5e93<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321191602461-1707894714.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u6269\u5c55:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180320235304604-1544695834.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>8.1 &nbsp; \\g \u6216 &nbsp;\\G &nbsp;\u8ba9\u7ed3\u679c\u663e\u793a\u66f4\u663e\u7684\u7f8e\u89c2\u3002<\/p>\n\n\n\n<p>8.2 &nbsp; \u4f7f\u7528SHOW \u8bed\u53e5\u67e5\u8be2mysql\u652f\u6301\u7684\u5b58\u50a8\u5f15\u64ce\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SHOW VARIABLES LIKE 'have%';  --\u67e5\u8be2\u652f\u6301have\u5f00\u5934\u7684\u5b58\u50a8\u5f15\u64ce<\/pre>\n\n\n\n<p><strong>\u4e5d:\u663e\u793a\u5e93\u4e2d\u7684\u6570\u636e\u8868<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;USE db_name; \u3000\u3000--\u4f7f\u7528\u6570\u636e\u5e93\nmysql&gt;SHOW TABLES;\u3000\u3000 --\u663e\u793a\u6570\u636e\u8868<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321191721602-48890148.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5341\uff1a\u521b\u5efa\u548c\u5220\u9664\u6570\u636e\u8868<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;CREATE  TABLE [IF NOT EXISTS] table_name(column_name column_type...) --\u521b\u5efa\nmysql&gt;DROP TABLE table_name;    --\u5220\u9664<\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b10.1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; CREATE TABLE [IF NOT EXISTS] tb1(\n     &gt;id     INT  UNSIGNED PRIMARY KEY AUTO_INCREMENT,\n     &gt;age    INT  UNSIGNED  NOT NULL,\n     &gt;name VARCHAR(<strong>150<\/strong>) NOT NULL,\n     &gt;status   BOOLEAN NOT NULL DEFAULT <strong>0<\/strong>\n     &gt;)ENGINE=InnoDB DEFAULT CHARSET=utf8;  <\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5982\u679c\u4f60\u4e0d\u60f3\u5b57\u6bb5\u4e3a&nbsp;<strong>NULL<\/strong>&nbsp;\u53ef\u4ee5\u8bbe\u7f6e\u5b57\u6bb5\u7684\u5c5e\u6027\u4e3a&nbsp;<strong>NOT NULL<\/strong>\uff0c \u5728\u64cd\u4f5c\u6570\u636e\u5e93\u65f6\u5982\u679c\u8f93\u5165\u8be5\u5b57\u6bb5\u7684\u6570\u636e\u4e3a<strong>NULL<\/strong>&nbsp;\uff0c\u5c31\u4f1a\u62a5\u9519\u3002<\/li><li><strong>AUTO_INCREMENT<\/strong>\u5b9a\u4e49\u5217\u4e3a\u81ea\u589e\u7684\u5c5e\u6027\uff0c\u4e00\u822c\u7528\u4e8e\u4e3b\u952e\uff0c\u6570\u503c\u4f1a\u81ea\u52a8\u52a01\u3002<\/li><li><strong>PRIMARY KEY<\/strong>\u5173\u952e\u5b57\u7528\u4e8e\u5b9a\u4e49\u5217\u4e3a\u4e3b\u952e\u3002 \u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u5217\u6765\u5b9a\u4e49\u4e3b\u952e\uff0c\u5217\u95f4\u4ee5\u9017\u53f7\u5206\u9694\u3002<\/li><li><strong>UNSIGNED<\/strong>\u65e0\u7b26\u53f7\u3002<\/li><li><strong>DEFAULT<\/strong> \u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/li><li><strong>COMMENT&nbsp;<\/strong>\u5b57\u6bb5\u6216\u5217\u7684\u6ce8\u91ca\u662f\u7528\u5c5e\u6027comment\u6765\u6dfb\u52a0\u3002<\/li><li><strong>ENGINE<\/strong> \u8bbe\u7f6e\u5b58\u50a8\u5f15\u64ce\uff0c<strong>CHARSET<\/strong> \u8bbe\u7f6e\u7f16\u7801\u3002<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321192255976-278949079.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5341\u4e00:\u67e5\u770b\u6570\u636e\u8868\u7684\u7ed3\u6784&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;DESC table_name;\nmysql&gt;DESCRIBE table_name;\nmysql&gt;EXPLAIN  table_name;\nmysql&gt;SHOW COLUMNS FROM  table_name;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321192348676-98948838.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5341\u4e8c:\u63d2\u5165\u6570\u636e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;INSERT INTO table_name ( field1, field2,...fieldN )\n     &gt;VALUES( value1, value2,...valueN );<\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b12.1:&nbsp;INSERT INTO tb1(age,name,status) VALUES (23,'\u5f20\u4e09',1);<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;INSERT  INTO tb1(age,name,status) VALUES (<strong>23<\/strong>,'\u5f20\u4e09',<strong>1<\/strong>),(<strong>21<\/strong>,'\u674e\u56db',<strong>1<\/strong>),(<strong>24<\/strong>,'\u738b\u4e94',<strong>0<\/strong>),(<strong>21<\/strong>,'\u8d75\u516d',<strong>1<\/strong>),(<strong>22<\/strong>,'\u674e\u5cf0',<strong>1<\/strong>),(<strong>24<\/strong>,'\u8c22\u56db\u65f6',<strong>1<\/strong>),(<strong>20<\/strong>,'\u5f20\u91d1\u9f99',<strong>1<\/strong>);<\/pre>\n\n\n\n<p><strong>\u6ce8<\/strong>:\u5982\u679c\u6570\u636e\u662f\u5b57\u7b26\u578b\uff0c\u5fc5\u987b\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u8005\u53cc\u5f15\u53f7\uff0c\u5982\uff1a\"value\"\u3002<\/p>\n\n\n\n<p>\u3000<strong> set names gbk;&nbsp;<\/strong>\u786e\u4fdd\u8868\u4e2d\u8be5\u5b57\u6bb5\u7684\u5b57\u7b26\u96c6\u4e3a\u4e2d\u6587\u517c\u5bb9\u3002<\/p>\n\n\n\n<p>\u3000\u4f7f\u7528\u7bad\u5934\u6807\u8bb0<strong>-&gt;&nbsp;<\/strong>\u4e0d\u662f SQL \u8bed\u53e5\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4ec5\u4ec5\u8868\u793a\u4e00\u4e2a\u65b0\u884c\uff0c\u5982\u679c\u4e00\u6761SQL\u8bed\u53e5\u592a\u957f\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56de\u8f66\u952e\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u884c\u6765\u7f16\u5199 SQL \u8bed\u53e5\uff0cSQL \u8bed\u53e5\u7684\u547d\u4ee4\u7ed3\u675f\u7b26\u4e3a\u5206\u53f7&nbsp;;\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321192838566-662535902.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5728\u4ee5\u4e0a\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u63d0\u4f9b id \u7684\u6570\u636e\uff0c\u56e0\u4e3a\u8be5\u5b57\u6bb5\u6211\u4eec\u5728\u521b\u5efa\u8868\u7684\u65f6\u5019\u5df2\u7ecf\u8bbe\u7f6e\u5b83\u4e3a <strong>AUTO_INCREMENT<\/strong>(\u81ea\u52a8\u589e\u52a0) \u5c5e\u6027\u3002 \u6240\u4ee5\uff0c\u8be5\u5b57\u6bb5\u4f1a\u81ea\u52a8\u9012\u589e\u800c\u4e0d\u9700\u8981\u6211\u4eec\u53bb\u8bbe\u7f6e\u3002<\/li><li><strong>status<\/strong>\u7684\u6570\u636e\uff0c\u6211\u4eec\u5728\u521b\u5efa\u8868\u7684\u65f6\u5019\u5df2\u7ecf\u8bbe\u7f6e\u5b83\u4e3a<strong>DEFAULT<\/strong>(\u9ed8\u8ba4\u503c)\u719f\u5c5e\u6027\u3002\u6240\u4ee5\uff0c\u8be5\u5b57\u6bb5\u5728\u6ca1\u6709\u63d0\u4f9b\u6570\u636e\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u503c\u4e3a:0\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u5341\u4e09:\u67e5\u8be2\u6570\u636e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT (column_name,column_name,...)  FROM table_name [WHERE Clause] [LIMIT N][ OFFSET M];<\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b13.1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT * FROM tb1;\nmysql&gt;SELECT (id,age,name,status) FROM tb1;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321194802630-1416146888.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u67e5\u8be2\u8bed\u53e5\u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u8868\uff0c\u8868\u4e4b\u95f4\u4f7f\u7528\u9017\u53f7(,)\u5206\u5272\uff0c\u5e76\u4f7f\u7528<strong>WHERE<\/strong>\u8bed\u53e5\u6765\u8bbe\u5b9a\u67e5\u8be2\u6761\u4ef6\u3002<\/li><li><strong>SELECT<\/strong> \u547d\u4ee4\u53ef\u4ee5\u8bfb\u53d6\u4e00\u6761\u6216\u8005\u591a\u6761\u8bb0\u5f55\u3002<\/li><li>\u53ef\u4ee5\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u6765\u4ee3\u66ff\u5176\u4ed6\u5b57\u6bb5\uff0c<strong>SELECT<\/strong>\u8bed\u53e5\u4f1a\u8fd4\u56de\u8868\u7684\u6240\u6709\u5b57\u6bb5\u6570\u636e<\/li><li>\u53ef\u4ee5\u4f7f\u7528<strong> WHERE<\/strong> \u8bed\u53e5\u6765\u5305\u542b\u4efb\u4f55\u6761\u4ef6\u3002<\/li><li>\u53ef\u4ee5\u4f7f\u7528 <strong>LIMIT<\/strong> \u5c5e\u6027\u6765\u8bbe\u5b9a\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u3002<\/li><li>\u53ef\u4ee5\u901a\u8fc7<strong>OFFSET<\/strong>\u6307\u5b9a<strong>SELECT<\/strong>\u8bed\u53e5\u5f00\u59cb\u67e5\u8be2\u7684\u6570\u636e\u504f\u79fb\u91cf\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u504f\u79fb\u91cf\u4e3a0\u3002<\/li><\/ul>\n\n\n\n<p>\u5341\u56db:WHERE \u5b50\u53e5<\/p>\n\n\n\n<p><strong>\u6709\u6761\u4ef6\u5730\u4ece\u8868\u4e2d\u9009\u53d6\u6570\u636e\uff0c\u53ef\u5c06 WHERE \u5b50\u53e5\u6dfb\u52a0\u5230 SELECT \u8bed\u53e5\u4e2d\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT field1, field2,...fieldN FROM table_name1, table_name2...\n     &gt;[WHERE condition1 [AND [OR]] condition2.....<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u67e5\u8be2\u8bed\u53e5\u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u8868\uff0c\u8868\u4e4b\u95f4\u4f7f\u7528\u9017\u53f7,&nbsp;\u5206\u5272\uff0c\u5e76\u4f7f\u7528WHERE\u8bed\u53e5\u6765\u8bbe\u5b9a\u67e5\u8be2\u6761\u4ef6\u3002<\/li><li>\u53ef\u4ee5\u5728 <strong>WHERE<\/strong> \u5b50\u53e5\u4e2d\u6307\u5b9a\u4efb\u4f55\u6761\u4ef6\u3002<\/li><li>\u53ef\u4ee5\u4f7f\u7528<strong> AND<\/strong> \u6216\u8005<strong> OR<\/strong> \u6307\u5b9a\u4e00\u4e2a\u6216\u591a\u4e2a\u6761\u4ef6\u3002<\/li><li><strong>WHERE<\/strong> \u5b50\u53e5\u4e5f\u53ef\u4ee5\u8fd0\u7528\u4e8e SQL \u7684<strong> DELETE<\/strong> \u6216\u8005 <strong>UPDATE<\/strong> \u547d\u4ee4\u3002<\/li><li><strong>WHERE<\/strong> \u5b50\u53e5\u7c7b\u4f3c\u4e8e\u7a0b\u5e8f\u8bed\u8a00\u4e2d\u7684 <strong>if<\/strong> \u6761\u4ef6\uff0c\u6839\u636e MySQL \u8868\u4e2d\u7684\u5b57\u6bb5\u503c\u6765\u8bfb\u53d6\u6307\u5b9a\u7684\u6570\u636e\u3002<\/li><\/ul>\n\n\n\n<p>\u4ee5\u4e0b\u4e3a\u64cd\u4f5c\u7b26\u5217\u8868\uff0c\u53ef\u7528\u4e8e WHERE \u5b50\u53e5\u4e2d\u3002<\/p>\n\n\n\n<p>\u4e0b\u8868\u4e2d\u5b9e\u4f8b\u5047\u5b9a A \u4e3a 10, B \u4e3a 20<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u64cd\u4f5c\u7b26<\/strong><\/td><td><strong>\u63cf\u8ff0<\/strong><\/td><td><strong>\u5b9e\u4f8b<\/strong><\/td><\/tr><tr><td>=<\/td><td>\u7b49\u53f7\uff0c\u68c0\u6d4b\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\u8fd4\u56detrue<\/td><td>(A = B) \u8fd4\u56defalse\u3002<\/td><\/tr><tr><td>&lt;&gt;, !=<\/td><td>\u4e0d\u7b49\u4e8e\uff0c\u68c0\u6d4b\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\u8fd4\u56detrue<\/td><td>(A != B) \u8fd4\u56de true\u3002<\/td><\/tr><tr><td>&gt;<\/td><td>\u5927\u4e8e\u53f7\uff0c\u68c0\u6d4b\u5de6\u8fb9\u7684\u503c\u662f\u5426\u5927\u4e8e\u53f3\u8fb9\u7684\u503c, \u5982\u679c\u5de6\u8fb9\u7684\u503c\u5927\u4e8e\u53f3\u8fb9\u7684\u503c\u8fd4\u56detrue<\/td><td>(A &gt; B) \u8fd4\u56defalse\u3002<\/td><\/tr><tr><td>&lt;<\/td><td>\u5c0f\u4e8e\u53f7\uff0c\u68c0\u6d4b\u5de6\u8fb9\u7684\u503c\u662f\u5426\u5c0f\u4e8e\u53f3\u8fb9\u7684\u503c, \u5982\u679c\u5de6\u8fb9\u7684\u503c\u5c0f\u4e8e\u53f3\u8fb9\u7684\u503c\u8fd4\u56detrue<\/td><td>(A &lt; B) \u8fd4\u56de true\u3002<\/td><\/tr><tr><td>&gt;=<\/td><td>\u5927\u4e8e\u7b49\u4e8e\u53f7\uff0c\u68c0\u6d4b\u5de6\u8fb9\u7684\u503c\u662f\u5426\u5927\u4e8e\u6216\u7b49\u4e8e\u53f3\u8fb9\u7684\u503c, \u5982\u679c\u5de6\u8fb9\u7684\u503c\u5927\u4e8e\u6216\u7b49\u4e8e\u53f3\u8fb9\u7684\u503c\u8fd4\u56detrue<\/td><td>(A &gt;= B) \u8fd4\u56defalse\u3002<\/td><\/tr><tr><td>&lt;=<\/td><td>\u5c0f\u4e8e\u7b49\u4e8e\u53f7\uff0c\u68c0\u6d4b\u5de6\u8fb9\u7684\u503c\u662f\u5426\u5c0f\u4e8e\u4e8e\u6216\u7b49\u4e8e\u53f3\u8fb9\u7684\u503c, \u5982\u679c\u5de6\u8fb9\u7684\u503c\u5c0f\u4e8e\u6216\u7b49\u4e8e\u53f3\u8fb9\u7684\u503c\u8fd4\u56detrue<\/td><td>(A &lt;= B) \u8fd4\u56de true\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u60f3\u518d MySQL \u6570\u636e\u8868\u4e2d\u8bfb\u53d6\u6307\u5b9a\u7684\u6570\u636e\uff0cWHERE \u5b50\u53e5\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u4e3b\u952e\u6765\u4f5c\u4e3a WHERE \u5b50\u53e5\u7684\u6761\u4ef6\u67e5\u8be2\u662f\u975e\u5e38\u5feb\u901f\u7684\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u7ed9\u5b9a\u7684\u6761\u4ef6\u5728\u8868\u4e2d\u6ca1\u6709\u4efb\u4f55\u5339\u914d\u7684\u8bb0\u5f55\uff0c\u90a3\u4e48\u67e5\u8be2\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u6570\u636e\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b:14.4\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT  * FROM tb1 WHERE name='\u674e\u56db';<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321194859102-210880710.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a&nbsp;<\/strong>\u8bfb\u53d6 tb1\u8868\u4e2d name \u5b57\u6bb5\u503c\u4e3a \u674e\u56db \u7684\u6240\u6709\u8bb0\u5f55<\/p>\n\n\n\n<p><strong>\u6ce8:<\/strong>MySQL \u7684 WHERE \u5b50\u53e5\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u662f\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528 <strong>BINARY<\/strong> \u5173\u952e\u5b57\u6765\u8bbe\u5b9a <strong>WHERE \u5b50\u53e5<\/strong>\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u662f<strong>\u533a\u5206\u5927\u5c0f\u5199<\/strong>\u7684<\/p>\n\n\n\n<p>\u5341\u4e94:UPDATE \u67e5\u8be2<\/p>\n\n\n\n<p><strong>\u4fee\u6539\u6216\u66f4\u65b0 MySQL \u4e2d\u7684\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 SQL UPDATE \u547d\u4ee4\u6765\u64cd\u4f5c\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause];<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53ef\u4ee5\u540c\u65f6\u66f4\u65b0\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u6bb5\u3002<\/li><li>\u53ef\u4ee5\u5728 WHERE \u5b50\u53e5\u4e2d\u6307\u5b9a\u4efb\u4f55\u6761\u4ef6\u3002<\/li><li>\u53ef\u4ee5\u5728\u4e00\u4e2a\u5355\u72ec\u8868\u4e2d\u540c\u65f6\u66f4\u65b0\u6570\u636e\u3002<\/li><\/ul>\n\n\n\n<p>\u5f53\u4f60\u9700\u8981\u66f4\u65b0\u6570\u636e\u8868\u4e2d\u6307\u5b9a\u884c\u7684\u6570\u636e\u65f6 WHERE \u5b50\u53e5\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n\n\n\n<p><strong>\u6269\u5c55:<\/strong><\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u9700\u8981\u5c06\u5b57\u6bb5\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u4e32\u6279\u91cf\u4fee\u6539\u4e3a\u5176\u4ed6\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u5df2\u4f7f\u7528\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;UPDATE table_name SET field = REPLACE(field, 'old-value', 'new-value') [WHERE Clause] ;<\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b15.1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;UPDATE tb1 SET name = REPLACE (name,'\u5f20\u4e09','\u5f20\u4e09\u4e30')  WHERE id = <strong>1<\/strong>;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321195115650-1602908570.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a<\/strong>\u66f4\u65b0tb1\u8868&nbsp;id \u4e3a 3 \u7684name \u5b57\u6bb5\u503c \u5c06 \"\u5f20\u4e09\" \u66ff\u6362\u4e3a&nbsp;\"\u5f20\u4e09\u4e30\"\u3002<\/p>\n\n\n\n<p>UPDATE \u8bed\u53e5\u53ef\u7528\u6765\u4fee\u6539\u8868\u4e2d\u7684\u6570\u636e, \u7b80\u5355\u6765\u8bf4\u57fa\u672c\u7684\u4f7f\u7528\u5f62\u5f0f\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;UPDATE table_name  SET column_name = new-value [WHERE  Clause] ;<\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b15.2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;UPDATE  tb1  SET status = DEFAULT WHERE id = <strong>4<\/strong> ;<\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a<\/strong>\u66f4\u65b0tb1\u8868&nbsp;id \u4e3a 4 \u7684status\u5b57\u6bb5\u503c \u66f4\u65b0\u4e3a\u9ed8\u8ba4\u503c 0 \u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321195231794-542136899.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5341\u516d:DELETE \u8bed\u53e5<\/p>\n\n\n\n<p><strong>DELETE &nbsp;FROM \u547d\u4ee4\u6765\u5220\u9664 MySQL \u6570\u636e\u8868\u4e2d\u7684\u8bb0\u5f55\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;DELETE FROM table_name [WHERE Clause];<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5982\u679c<strong>\u6ca1\u6709\u6307\u5b9a WHERE \u5b50\u53e5<\/strong>\uff0cMySQL \u8868\u4e2d\u7684<strong>\u6240\u6709\u8bb0\u5f55\u5c06\u88ab\u5220\u9664&nbsp;<\/strong>[ \u5de5\u4f5c\u65f6 &nbsp;\u4e00\u5b9a\u8981\u8bb0\u5f97\u52a0 \u5220\u9664\u6761\u4ef6 &nbsp;\u4e0d\u7136\u7b49\u7740\u8dd1\u5e93\u5427\uff01] ---\u4e0d\u8fc7\u53ea\u6e05\u7a7a\u4e86\u8868\u4e2d\u6570\u636e\u3002\u8981\u4e0d\u4f60\u518d\u8bd5\u8bd5<strong> DROP TABLE<\/strong> &nbsp;table_name; &nbsp;---\u6e05\u7a7a\u6570\u636e\u548c\u5220\u9664\u8868\u7ed3\u6784\u3002<\/li><li>\u4f60\u53ef\u4ee5\u5728 WHERE \u5b50\u53e5\u4e2d\u6307\u5b9a\u4efb\u4f55\u6761\u4ef6<\/li><li>\u4f60\u53ef\u4ee5\u5728\u5355\u4e2a\u8868\u4e2d\u4e00\u6b21\u6027\u5220\u9664\u8bb0\u5f55 [<strong> \u614e\u7528<\/strong> ]\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u5b9e\u4f8b16.1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;DELETE FROM tb1 WHERE name = '\u738b\u4e94';<\/pre>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a<\/strong>\u5220\u9664tb1\u8868name \u4e3a \u738b\u4e94 \u7684\u6570\u636e\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321195452496-287104514.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5341\u4e03:LIKE \u5b50\u53e5<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; \u6a21\u7cca\u67e5\u8be2 &nbsp;------- &nbsp;% &nbsp;<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u5728 MySQL \u4e2d\u4f7f\u7528 SQL SELECT \u547d\u4ee4\u6765\u8bfb\u53d6\u6570\u636e\uff0c \u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u5728 SELECT \u8bed\u53e5\u4e2d\u4f7f\u7528 WHERE \u5b50\u53e5\u6765\u83b7\u53d6\u6307\u5b9a\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p>WHERE \u5b50\u53e5\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7b49\u53f7&nbsp;=&nbsp;\u6765\u8bbe\u5b9a\u83b7\u53d6\u6570\u636e\u7684\u6761\u4ef6\uff0c\u5982 \"name = '\u674e\u56db'\"\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u83b7\u53d6 name \u5b57\u6bb5\u542b\u6709 \"\u674e\" \u5b57\u7b26\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u8fd9\u65f6\u6211\u4eec\u5c31\u9700\u8981\u5728 WHERE \u5b50\u53e5\u4e2d\u4f7f\u7528 SQL LIKE \u5b50\u53e5\u3002<\/p>\n\n\n\n<p>SQL LIKE \u5b50\u53e5\u4e2d\u4f7f\u7528\u767e\u5206\u53f7&nbsp;%\u5b57\u7b26\u6765\u8868\u793a\u4efb\u610f\u5b57\u7b26\uff0c\u7c7b\u4f3c\u4e8eUNIX\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u661f\u53f7&nbsp;* \u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u4f7f\u7528\u767e\u5206\u53f7&nbsp;%, LIKE \u5b50\u53e5\u4e0e\u7b49\u53f7&nbsp;=&nbsp;\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT  field1, field2,... FROM  table_name WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53ef\u4ee5\u5728 WHERE \u5b50\u53e5\u4e2d\u6307\u5b9a\u4efb\u4f55\u6761\u4ef6\u3002<\/li><li>\u53ef\u4ee5\u5728 WHERE \u5b50\u53e5\u4e2d\u4f7f\u7528LIKE\u5b50\u53e5\u3002<\/li><li>\u53ef\u4ee5\u4f7f\u7528LIKE\u5b50\u53e5\u4ee3\u66ff\u7b49\u53f7&nbsp;=\u3002<\/li><li>LIKE \u901a\u5e38\u4e0e&nbsp;%&nbsp;\u4e00\u540c\u4f7f\u7528\uff0c\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u5143\u5b57\u7b26\u7684\u641c\u7d22\u3002<\/li><li>\u53ef\u4ee5\u4f7f\u7528 AND \u6216\u8005 OR \u6307\u5b9a\u4e00\u4e2a\u6216\u591a\u4e2a\u6761\u4ef6\u3002<\/li><li>\u53ef\u4ee5\u5728 DELETE \u6216 UPDATE \u547d\u4ee4\u4e2d\u4f7f\u7528 WHERE...LIKE \u5b50\u53e5\u6765\u6307\u5b9a\u6761\u4ef6\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u5b9e\u4f8b17.1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT * FROM tb1 WHERE  name LIKE '\u5f20%';<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321195612405-2083383807.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a<\/strong>\u6211\u4eec\u5c06 tb1\u8868\u4e2d\u83b7\u53d6name \u5b57\u6bb5\u4e2d\u4ee5&nbsp;\u5f20&nbsp;\u4e3a\u5f00\u5934\u7684\u7684\u6240\u6709\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b17.2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT * FROM tb1 WHERE  name LIKE '%\u4e30' AND  id = '1%';<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321200451728-1388314232.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790\uff1a<\/strong>\u5c06 tb1\u8868 \u4ee5name \u5b57\u6bb5\u4e2d \u4e30 \u7ed3\u675f \u5e76\u4e14 \u4ee5 1 \u5f00\u59cb \u7684\u6240\u6709\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p>\u5341\u516b:UNION<\/p>\n\n\n\n<p><strong>NION<\/strong> \u64cd\u4f5c\u7b26\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u4ee5\u4e0a\u7684 <strong>SELECT<\/strong> \u8bed\u53e5\u7684\u7ed3\u679c\u7ec4\u5408\u5230\u4e00\u4e2a\u7ed3\u679c\u96c6\u5408\u4e2d\u3002\u591a\u4e2a SELECT \u8bed\u53e5\u4f1a\u5220\u9664\u91cd\u590d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> mysql&gt;SELECT expression1, expression2, ... FROM table_name [WHERE conditions]  <br>\u3000\u3000   &gt;UNION [ ALL | DISTINCT ] <br>      &gt;SELECT expression1, expression2, ... FROM table_name [WHERE conditions];<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>expression1, expression2, ... <\/strong>: \u8981\u68c0\u7d22\u7684\u5217\u3002<\/li><li><strong>tables:&nbsp;<\/strong>\u8981\u68c0\u7d22\u7684\u6570\u636e\u8868\u3002<\/li><li><strong>WHERE conditions:&nbsp;<\/strong>\u53ef\u9009\uff0c \u68c0\u7d22\u6761\u4ef6\u3002<\/li><li><strong>DISTINCT:&nbsp;<\/strong>\u53ef\u9009\uff0c\u5220\u9664\u7ed3\u679c\u96c6\u4e2d\u91cd\u590d\u7684\u6570\u636e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b UNION \u64cd\u4f5c\u7b26\u5df2\u7ecf\u5220\u9664\u4e86\u91cd\u590d\u6570\u636e\uff0c\u6240\u4ee5 DISTINCT \u4fee\u9970\u7b26\u5bf9\u7ed3\u679c\u6ca1\u5565\u5f71\u54cd\u3002<\/li><li><strong>ALL:&nbsp;<\/strong>\u53ef\u9009\uff0c\u8fd4\u56de\u6240\u6709\u7ed3\u679c\u96c6\uff0c\u5305\u542b\u91cd\u590d\u6570\u636e\u3002<\/li><\/ul>\n\n\n\n<p><strong>&nbsp;18.1&nbsp;\u5b9e\u4f8b UNION<\/strong><\/p>\n\n\n\n<p><strong>\uff081\uff09\u6570\u636e\u8868 tb1\u6570\u636e<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321211930922-2079729543.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\uff082\uff09&nbsp;\u6570\u636e\u8868 tb2\u6570\u636e<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321211910529-582469621.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT status FROM tb1 \n\u3000\u3000  &gt;UNION<br>\u3000\u3000  &gt;SELECT dept_status FROM tb2 <br>\u3000\u3000  &gt;ORDER BY status ; <\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321213019870-1126813655.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790:<\/strong>\u4ecetb1\u548ctb2\u8868\u4e2d\u9009\u53d6\u4e0d\u540c\u7684status (\u53ea\u6709\u4e0d\u540c\u7684\u503c)<\/p>\n\n\n\n<p><strong>&nbsp;18.2&nbsp;\u5b9e\u4f8b UNION ALL&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT status FROM tb1 \n     &gt;UNION ALL\n     &gt;SELECT dept_status FROM tb2 \n     &gt;ORDER BY status ;  <\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321213850879-1967837885.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>&nbsp;<strong>\u5b9e\u4f8b\u89e3\u6790:<\/strong>\u4ecetb1\u548ctb2\u8868\u4e2d\u9009\u53d6\u6240\u6709\u7684status (\u4e5f\u6709\u91cd\u590d\u7684\u503c)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">18.3 \u5e26\u6709 WHERE \u7684 SQL UNION ALL<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT id, name,status  FROM tb1\n     &gt;WHERE status = <strong>0<\/strong>\n     &gt;UNION ALL\n     &gt;SELECT dept_id,  dept_name ,dept_status FROM  tb2\n     &gt;WHERE dept_status = <strong>0<\/strong> \n     &gt;ORDER BY id;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321215643261-1586311388.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790:<\/strong>\u4ecetb1\u548ctb2\u8868\u4e2d\u9009\u53d6\u6240\u6709\u7684status=0\u7684\u503c (\u4e5f\u6709\u91cd\u590d\u7684\u503c)<\/p>\n\n\n\n<p>\u5341\u4e5d:\u6392\u5e8f<\/p>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u4ece MySQL \u8868\u4e2d\u4f7f\u7528SELECT\u8bed\u53e5\u6765\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5bf9\u8bfb\u53d6\u7684\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528<strong>ORDER BY<\/strong>&nbsp;\u5b50\u53e5\u6765\u8bbe\u5b9a\u4f60\u60f3\u6309\u54ea\u4e2a\u5b57\u6bb5\u54ea\u79cd\u65b9\u5f0f\u6765\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u641c\u7d22\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT field1, field2,... table_name1, table_name2...\n     &gt;ORDER BY field1, [field2...] [ASC [DESC]]<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u5b57\u6bb5\u6765\u4f5c\u4e3a\u6392\u5e8f\u7684\u6761\u4ef6\uff0c\u4ece\u800c\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u67e5\u8be2\u7ed3\u679c\u3002<\/li><li>\u53ef\u4ee5\u8bbe\u5b9a\u591a\u4e2a\u5b57\u6bb5\u6765\u6392\u5e8f\u3002<\/li><li>\u53ef\u4ee5\u4f7f\u7528<strong> ASC<\/strong> \u6216 <strong>DESC<\/strong> \u5173\u952e\u5b57\u6765\u8bbe\u7f6e\u67e5\u8be2\u7ed3\u679c\u662f\u6309<strong>\u5347\u5e8f<\/strong>\u6216<strong>\u964d\u5e8f\u6392\u5217<\/strong>\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u662f\u6309\u5347\u5e8f\u6392\u5217\u3002<\/li><li>\u53ef\u4ee5\u6dfb\u52a0 <strong>WHERE...LIKE <\/strong>\u5b50\u53e5\u6765\u8bbe\u7f6e\u6761\u4ef6\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u5b9e\u4f8b:19.1&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT * FROM  tb1 ORDER BY id DESC;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321220437987-86458796.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790:<\/strong>\u8bfb\u53d6 tb1 \u8868\u4e2d\u6240\u6709\u6570\u636e\u5e76\u6309 id \u5b57\u6bb5\u7684\u964d\u5e8f\u6392\u5217\u3002<\/p>\n\n\n\n<p>\u4e8c\u5341:\u5206\u7ec4<\/p>\n\n\n\n<p>GROUP BY \u8bed\u53e5\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n\n\n\n<p>\u5728\u5206\u7ec4\u7684\u5217\u4e0a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <strong>COUNT, SUM, AVG<\/strong>,\u7b49<strong>\u51fd\u6570<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT column_name, function(column_name)\n     &gt;FROM table_name\n     &gt;WHERE column_name operator value\n     &gt;GROUP BY column_name;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><br><\/strong><strong>\u89e3\u6790:<\/strong>\u7ee7\u7eed\u5f80tb1\u8868\u6dfb\u52a0\u5b57\u6bb5\u53ca\u5176\u6570\u636e---(\u8ffd\u52a0\u5b57\u6bb5\u540e\u9762\u6211\u4eec\u4f1a\u5b66\u5230-- <strong>ALTER  ADD<\/strong>)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT name, COUNT(*) FROM  tb1 GROUP BY name;<\/pre>\n\n\n\n<p>&nbsp;<strong>20.1<\/strong><strong>\u5b9e\u4f8b:&nbsp;GROUP BY<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321223817065-914429762.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790:<\/strong>\u4f7f\u7528 GROUP BY \u8bed\u53e5 \u5c06\u6570\u636e\u8868\u6309\u540d\u5b57\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u4eba\u6709\u591a\u5c11\u6761\u8bb0\u5f55<\/p>\n\n\n\n<p><strong>&nbsp;20.2&nbsp;\u5b9e\u4f8b:&nbsp;<\/strong>\u4f7f\u7528 <strong>WITH ROLLUP<\/strong><\/p>\n\n\n\n<p>WITH ROLLUP \u53ef\u4ee5\u5b9e\u73b0\u5728\u5206\u7ec4\u7edf\u8ba1\u6570\u636e\u57fa\u7840\u4e0a\u518d\u8fdb\u884c\u76f8\u540c\u7684\u7edf\u8ba1\uff08SUM,AVG,COUNT\u2026\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT name, SUM(singin) as singin_count FROM  tb1 GROUP BY name WITH ROLLUP;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321224331440-1158837504.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u5b9e\u4f8b\u89e3\u6790:<\/strong>\u4ee5\u4e0a\u7684\u6570\u636e\u8868\u6309\u540d\u5b57\u8fdb\u884c\u5206\u7ec4\uff0c\u518d\u7edf\u8ba1\u6bcf\u4e2a\u4eba\u767b\u5f55\u7684\u6b21\u6570<\/p>\n\n\n\n<p><strong>\u62d3\u5c55:<\/strong>AS \u8bbe\u7f6e\u5b57\u6bb5\u522b\u540d\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u8bb0\u5f55 NULL \u8868\u793a\u6240\u6709\u4eba\u7684\u767b\u5f55\u6b21\u6570\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 coalesce \u6765\u8bbe\u7f6e\u4e00\u4e2a\u53ef\u4ee5\u53d6\u4ee3 NUll \u7684\u540d\u79f0\uff0ccoalesce \u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT COALESCE (a,b,c);<\/pre>\n\n\n\n<p>\u53c2\u6570\u8bf4\u660e\uff1a\u5982\u679ca==null,\u5219\u9009\u62e9b\uff1b\u5982\u679cb==null,\u5219\u9009\u62e9c\uff1b\u5982\u679ca!=null,\u5219\u9009\u62e9a\uff1b\u5982\u679ca b c \u90fd\u4e3anull \uff0c\u5219\u8fd4\u56de\u4e3anull\uff08\u6ca1\u610f\u4e49\uff09\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b20.3<\/strong> &nbsp; \u5982\u679c\u540d\u5b57\u4e3a\u7a7a\u6211\u4eec\u4f7f\u7528\u603b\u6570\u4ee3\u66ff<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt;SELECT coalesce(name, '\u603b\u6570'), SUM(singin) as singin_count FROM  tb1 GROUP BY name WITH ROLLUP;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2018.cnblogs.com\/blog\/1153970\/201803\/1153970-20180321224920614-1505647763.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4e8c\u5341\u4e00:\u8fde\u63a5\u7684\u4f7f\u7528<\/p>\n\n\n\n<p>\u6211\u4eec\u5df2\u7ecf\u5b66\u4f1a\u4e86\u5982\u4f55\u5728\u4e00\u5f20\u8868\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u8fd9\u662f\u76f8\u5bf9\u7b80\u5355\u7684\uff0c\u4f46\u662f\u5728\u771f\u6b63\u7684\u5e94\u7528\u4e2d\u7ecf\u5e38\u9700\u8981\u4ece\u591a\u4e2a\u6570\u636e\u8868\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u672c\u7ae0\u8282\u6211\u4eec\u5c06\u5411\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 MySQL \u7684 JOIN \u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u8868\u4e2d\u67e5\u8be2\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728 SELECT, UPDATE \u548c DELETE \u8bed\u53e5\u4e2d\u4f7f\u7528 Mysql \u7684 JOIN \u6765\u8054\u5408\u591a\u8868\u67e5\u8be2\u3002<\/p>\n\n\n\n<p>JOIN \u6309\u7167\u529f\u80fd\u5927\u81f4\u5206\u4e3a\u5982\u4e0b\u4e09\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>INNER JOIN\uff08\u5185\u8fde\u63a5,\u6216\u7b49\u503c\u8fde\u63a5\uff09<\/strong>\uff1a\u83b7\u53d6\u4e24\u4e2a\u8868\u4e2d\u5b57\u6bb5\u5339\u914d\u5173\u7cfb\u7684\u8bb0\u5f55\u3002<\/li><li><strong>LEFT JOIN\uff08\u5de6\u8fde\u63a5\uff09\uff1a<\/strong>\u83b7\u53d6\u5de6\u8868\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u53f3\u8868\u6ca1\u6709\u5bf9\u5e94\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li><li><strong>RIGHT JOIN\uff08\u53f3\u8fde\u63a5\uff09\uff1a<\/strong>&nbsp;\u4e0e LEFT JOIN \u76f8\u53cd\uff0c\u7528\u4e8e\u83b7\u53d6\u53f3\u8868\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u5de6\u8868\u6ca1\u6709\u5bf9\u5e94\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li><\/ul>\n\n\n\n<p>\u4e8c\u5341\u4e8c:NULL \u503c\u5904\u7406<\/p>\n\n\n\n<p>\u4e8c\u5341\u4e09:\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n\n\n\n<p>\u4e8c\u5341\u56db:\u4e8b\u52a1<\/p>\n\n\n\n<p>\u4e8c\u5341\u4e94:ALTER\u547d\u4ee4<\/p>\n\n\n\n<p>\u4e8c\u5341\u516d:\u7d22\u5f15<\/p>\n\n\n\n<p>\u4e8c\u5341\u4e03:\u4e34\u65f6\u8868<\/p>\n\n\n\n<p>\u4e8c\u5341\u516b:\u590d\u5236\u8868<\/p>\n\n\n\n<p>\u4e8c\u5341\u4e5d:\u5143\u6570\u636e<\/p>\n\n\n\n<p>\u4e09\u5341:\u5e8f\u5217\u4f7f\u7528<\/p>\n\n\n\n<p>MySQL\u5e8f\u5217\u662f\u4e00\u7ec4\u6574\u6570\uff1a1, 2, 3, ...\uff0c\u7531\u4e8e\u4e00\u5f20\u6570\u636e\u8868\u53ea\u80fd\u6709\u4e00\u4e2a\u5b57\u6bb5\u81ea\u589e\u4e3b\u952e\uff0c \u5982\u679c\u4f60\u60f3\u5b9e\u73b0\u5176\u4ed6\u5b57\u6bb5\u4e5f\u5b9e\u73b0\u81ea\u52a8\u589e\u52a0\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528MySQL\u5e8f\u5217\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u672c\u7ae0\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528MySQL\u7684\u5e8f\u5217\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">30.\u4f7f\u7528AUTO_INCREMENT<\/h2>\n\n\n\n<p>MySQL\u4e2d\u6700\u7b80\u5355\u4f7f\u7528\u5e8f\u5217\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528 MySQL AUTO_INCREMENT \u6765\u5b9a\u4e49\u5217\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u521b\u5efa\u4e86\u6570\u636e\u8868insect\uff0c insect\u4e2did\u65e0\u9700\u6307\u5b9a\u503c\u53ef\u5b9e\u73b0\u81ea\u52a8\u589e\u957f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; CREATE TABLE insect(-&gt; id INT UNSIGNED NOT NULL AUTO_INCREMENT,\n    -&gt; PRIMARY KEY (id),\n    -&gt; name VARCHAR(<strong>30<\/strong>) NOT NULL, # type of insect\n    -&gt; date DATE NOT NULL, # date collected\n    -&gt; origin VARCHAR(<strong>30<\/strong>) NOT NULL # where collected\n);\nmysql&gt; INSERT INTO insect (id,name,date,origin) VALUES\n    -&gt; (NULL,'housefly','2001-09-10','kitchen'),\n    -&gt; (NULL,'millipede','2001-09-10','driveway'),\n    -&gt; (NULL,'grasshopper','2001-09-10','front yard');\nmysql&gt; SELECT * FROM insect ORDER BY id;<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">30.1\u83b7\u53d6AUTO_INCREMENT\u503c<\/h2>\n\n\n\n<p>\u5728MySQL\u7684\u5ba2\u6237\u7aef\u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528 SQL\u4e2d\u7684LAST_INSERT_ID( ) \u51fd\u6570\u6765\u83b7\u53d6\u6700\u540e\u7684\u63d2\u5165\u8868\u4e2d\u7684\u81ea\u589e\u5217\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u5728PHP\u6216PERL\u811a\u672c\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u51fd\u6570\u6765\u83b7\u53d6\u6700\u540e\u7684\u63d2\u5165\u8868\u4e2d\u7684\u81ea\u589e\u5217\u7684\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PERL\u5b9e\u4f8b<\/h3>\n\n\n\n<p>\u4f7f\u7528 mysql_insertid \u5c5e\u6027\u6765\u83b7\u53d6 AUTO_INCREMENT \u7684\u503c\u3002 \u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$dbh-&gt;do (\"INSERT INTO insect (name,date,origin)\nVALUES('moth','2001-09-14','windowsill')\");\nmy $seq = $dbh-&gt;{mysql_insertid};<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">PHP\u5b9e\u4f8b<\/h3>\n\n\n\n<p>PHP \u901a\u8fc7 mysql_insert_id ()\u51fd\u6570\u6765\u83b7\u53d6\u6267\u884c\u7684\u63d2\u5165SQL\u8bed\u53e5\u4e2d AUTO_INCREMENT\u5217\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql_query (\"INSERT INTO insect (name,date,origin)\nVALUES('moth','2001-09-14','windowsill')\", $conn_id);\n$seq = mysql_insert_id ($conn_id);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">30.2\u91cd\u7f6e\u5e8f\u5217<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u5220\u9664\u4e86\u6570\u636e\u8868\u4e2d\u7684\u591a\u6761\u8bb0\u5f55\uff0c\u5e76\u5e0c\u671b\u5bf9\u5269\u4e0b\u6570\u636e\u7684AUTO_INCREMENT\u5217\u8fdb\u884c\u91cd\u65b0\u6392\u5217\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u901a\u8fc7\u5220\u9664\u81ea\u589e\u7684\u5217\uff0c\u7136\u540e\u91cd\u65b0\u6dfb\u52a0\u6765\u5b9e\u73b0\u3002 \u4e0d\u8fc7\u8be5\u64cd\u4f5c\u8981\u975e\u5e38\u5c0f\u5fc3\uff0c\u5982\u679c\u5728\u5220\u9664\u7684\u540c\u65f6\u53c8\u6709\u65b0\u8bb0\u5f55\u6dfb\u52a0\uff0c\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u6570\u636e\u6df7\u4e71\u3002\u64cd\u4f5c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; ALTER TABLE insect DROP id;\nmysql&gt; ALTER TABLE insect\n    -&gt; ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,\n    -&gt; ADD PRIMARY KEY (id);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">30.3\u8bbe\u7f6e\u5e8f\u5217\u7684\u5f00\u59cb\u503c<\/h2>\n\n\n\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\u5e8f\u5217\u7684\u5f00\u59cb\u503c\u4e3a1\uff0c\u4f46\u5982\u679c\u4f60\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u5f00\u59cb\u503c100\uff0c\u90a3\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u8bed\u53e5\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; CREATE TABLE insect(-&gt; id INT UNSIGNED NOT NULL AUTO_INCREMENT,\n    -&gt; PRIMARY KEY (id),\n    -&gt; name VARCHAR(<strong>30<\/strong>) NOT NULL, \n    -&gt; date DATE NOT NULL,\n    -&gt; origin VARCHAR(<strong>30<\/strong>) NOT NULL\n)engine=innodb auto_increment=<strong>100<\/strong> charset=utf8;<\/pre>\n\n\n\n<p>\u6216\u8005\u4f60\u4e5f\u53ef\u4ee5\u5728\u8868\u521b\u5efa\u6210\u529f\u540e\uff0c\u901a\u8fc7\u4ee5\u4e0b\u8bed\u53e5\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; ALTER TABLE t AUTO_INCREMENT = <strong>100<\/strong>;<\/pre>\n\n\n\n<p>\u4e09\u5341\u4e00:\u5904\u7406\u91cd\u590d\u6570\u636e<\/p>\n\n\n\n<p>\u6709\u4e9b MySQL \u6570\u636e\u8868\u4e2d\u53ef\u80fd\u5b58\u5728\u91cd\u590d\u7684\u8bb0\u5f55\uff0c\u6709\u4e9b\u60c5\u51b5\u6211\u4eec\u5141\u8bb8\u91cd\u590d\u6570\u636e\u7684\u5b58\u5728\uff0c\u4f46\u6709\u65f6\u5019\u6211\u4eec\u4e5f\u9700\u8981\u5220\u9664\u8fd9\u4e9b\u91cd\u590d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u672c\u7ae0\u8282\u6211\u4eec\u5c06\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u9632\u6b62\u6570\u636e\u8868\u51fa\u73b0\u91cd\u590d\u6570\u636e\u53ca\u5982\u4f55\u5220\u9664\u6570\u636e\u8868\u4e2d\u7684\u91cd\u590d\u6570\u636e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">31.1\u9632\u6b62\u8868\u4e2d\u51fa\u73b0\u91cd\u590d\u6570\u636e\u3002<\/h2>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728mysql\u6570\u636e\u8868\u4e2d\u8bbe\u7f6e\u6307\u5b9a\u7684\u5b57\u6bb5\u4e3aPRIMART KEY (\u4e3b\u952e)\u6216\u8005UNIQUE(\u552f\u4e00)\u7d22\u5f15\u6765\u4fdd\u8bc1\u6570\u636e\u7684\u552f\u4e00\u6027<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4e00\u4e2a\u5b9e\u4f8b\uff1a\u4e0b\u8868\u4e2d\u65e0\u7d22\u5f15\u53ca\u4e3b\u952e\uff0c\u6240\u4ee5\u8be5\u8868\u5141\u8bb8\u51fa\u73b0\u591a\u6761\u91cd\u590d\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE person_tbl\n(\n    first_name CHAR(<strong>20<\/strong>),\n    last_name CHAR(<strong>20<\/strong>),\n    sex CHAR(<strong>10<\/strong>)\n);<\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u8bbe\u7f6e\u8868\u4e2d\u5b57\u6bb5first_name\uff0clast_name\u6570\u636e\u4e0d\u80fd\u91cd\u590d\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u53cc\u4e3b\u952e\u6a21\u5f0f\u6765\u8bbe\u7f6e\u6570\u636e\u7684\u552f\u4e00\u6027\uff0c \u5982\u679c\u4f60\u8bbe\u7f6e\u4e86\u53cc\u4e3b\u952e\uff0c\u90a3\u4e48\u90a3\u4e2a\u952e\u7684\u9ed8\u8ba4\u503c\u4e0d\u80fd\u4e3aNULL\uff0c\u53ef\u8bbe\u7f6e\u4e3aNOT NULL\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE person_tbl\n(\n   first_name CHAR(<strong>20<\/strong>) NOT NULL,\n   last_name CHAR(<strong>20<\/strong>) NOT NULL,\n   sex CHAR(<strong>10<\/strong>),\n   PRIMARY KEY (last_name, first_name)\n);<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u8bbe\u7f6e\u4e86\u552f\u4e00\u7d22\u5f15\uff0c\u90a3\u4e48\u5728\u63d2\u5165\u91cd\u590d\u6570\u636e\u65f6\uff0cSQL\u8bed\u53e5\u5c06\u65e0\u6cd5\u6267\u884c\u6210\u529f,\u5e76\u629b\u51fa\u9519\u3002<\/p>\n\n\n\n<p>INSERT IGNORE INTO\u4e0eINSERT INTO\u7684\u533a\u522b\u5c31\u662fINSERT IGNORE\u4f1a\u5ffd\u7565\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u6570\u636e\uff0c\u5982\u679c\u6570\u636e\u5e93\u6ca1\u6709\u6570\u636e\uff0c\u5c31\u63d2\u5165\u65b0\u7684\u6570\u636e\uff0c\u5982\u679c\u6709\u6570\u636e\u7684\u8bdd\u5c31\u8df3\u8fc7\u8fd9\u6761\u6570\u636e\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u7559\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u5b58\u5728\u6570\u636e\uff0c\u8fbe\u5230\u5728\u95f4\u9699\u4e2d\u63d2\u5165\u6570\u636e\u7684\u76ee\u7684\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4f7f\u7528\u4e86INSERT IGNORE INTO\uff0c\u6267\u884c\u540e\u4e0d\u4f1a\u51fa\u9519\uff0c\u4e5f\u4e0d\u4f1a\u5411\u6570\u636e\u8868\u4e2d\u63d2\u5165\u91cd\u590d\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; INSERT IGNORE INTO person_tbl (last_name, first_name)\n    -&gt; VALUES( 'Jay', 'Thomas');\nmysql&gt; INSERT IGNORE INTO person_tbl (last_name, first_name)\n    -&gt; VALUES( 'Jay', 'Thomas');<\/pre>\n\n\n\n<p>INSERT IGNORE INTO\u5f53\u63d2\u5165\u6570\u636e\u65f6\uff0c\u5728\u8bbe\u7f6e\u4e86\u8bb0\u5f55\u7684\u552f\u4e00\u6027\u540e\uff0c\u5982\u679c\u63d2\u5165\u91cd\u590d\u6570\u636e\uff0c\u5c06\u4e0d\u8fd4\u56de\u9519\u8bef\uff0c\u53ea\u4ee5\u8b66\u544a\u5f62\u5f0f\u8fd4\u56de\u3002 \u800cREPLACE INTO into\u5982\u679c\u5b58\u5728primary \u6216 unique\u76f8\u540c\u7684\u8bb0\u5f55\uff0c\u5219\u5148\u5220\u9664\u6389\u3002\u518d\u63d2\u5165\u65b0\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u79cd\u8bbe\u7f6e\u6570\u636e\u7684\u552f\u4e00\u6027\u65b9\u6cd5\u662f\u6dfb\u52a0\u4e00\u4e2aUNIQUE\u7d22\u5f15\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE person_tbl\n(\n   first_name CHAR(<strong>20<\/strong>) NOT NULL,\n   last_name CHAR(<strong>20<\/strong>) NOT NULL,\n   sex CHAR(<strong>10<\/strong>)\n   UNIQUE (last_name, first_name)\n);<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">31.2\u7edf\u8ba1\u91cd\u590d\u6570\u636e<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u6211\u4eec\u5c06\u7edf\u8ba1\u8868\u4e2d first_name \u548c last_name\u7684\u91cd\u590d\u8bb0\u5f55\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT COUNT(*) as repetitions, last_name, first_name\n    -&gt; FROM person_tbl\n    -&gt; GROUP BY last_name, first_name\n    -&gt; HAVING repetitions &gt; <strong>1<\/strong>;<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u67e5\u8be2\u8bed\u53e5\u5c06\u8fd4\u56de person_tbl \u8868\u4e2d\u91cd\u590d\u7684\u8bb0\u5f55\u6570\u3002 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u67e5\u8be2\u91cd\u590d\u7684\u503c\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u786e\u5b9a\u54ea\u4e00\u5217\u5305\u542b\u7684\u503c\u53ef\u80fd\u4f1a\u91cd\u590d\u3002<\/li><li>\u5728\u5217\u9009\u62e9\u5217\u8868\u4f7f\u7528COUNT(*)\u5217\u51fa\u7684\u90a3\u4e9b\u5217\u3002<\/li><li>\u5728GROUP BY\u5b50\u53e5\u4e2d\u5217\u51fa\u7684\u5217\u3002<\/li><li>HAVING\u5b50\u53e5\u8bbe\u7f6e\u91cd\u590d\u6570\u5927\u4e8e1\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">31.3\u8fc7\u6ee4\u91cd\u590d\u6570\u636e<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u8bfb\u53d6\u4e0d\u91cd\u590d\u7684\u6570\u636e\u53ef\u4ee5\u5728 SELECT \u8bed\u53e5\u4e2d\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u6765\u8fc7\u6ee4\u91cd\u590d\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT DISTINCT last_name, first_name\n    -&gt; FROM person_tbl;<\/pre>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 GROUP BY \u6765\u8bfb\u53d6\u6570\u636e\u8868\u4e2d\u4e0d\u91cd\u590d\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT last_name, first_name\n    -&gt; FROM person_tbl\n    -&gt; GROUP BY (last_name, first_name);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">31.4\u5220\u9664\u91cd\u590d\u6570\u636e<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u5220\u9664\u6570\u636e\u8868\u4e2d\u7684\u91cd\u590d\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7684SQL\u8bed\u53e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; CREATE TABLE tmp SELECT last_name, first_name, sex FROM person_tbl  GROUP BY (last_name, first_name, sex);\nmysql&gt; DROP TABLE person_tbl;\nmysql&gt; ALTER TABLE tmp RENAME TO person_tbl;<\/pre>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u5728\u6570\u636e\u8868\u4e2d\u6dfb\u52a0 INDEX\uff08\u7d22\u5f15\uff09 \u548c PRIMAY KEY\uff08\u4e3b\u952e\uff09\u8fd9\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5220\u9664\u8868\u4e2d\u7684\u91cd\u590d\u8bb0\u5f55\u3002\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; ALTER IGNORE TABLE person_tbl\n    -&gt; ADD PRIMARY KEY (last_name, first_name);<\/pre>\n\n\n\n<p>\u4e09\u5341\u4e8c:MySQL \u53ca SQL \u6ce8\u5165<\/p>\n\n\n\n<p>\u5982\u679c\u60a8\u901a\u8fc7\u7f51\u9875\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u5e76\u5c06\u5176\u63d2\u5165\u4e00\u4e2aMySQL\u6570\u636e\u5e93\uff0c\u90a3\u4e48\u5c31\u6709\u53ef\u80fd\u53d1\u751fSQL\u6ce8\u5165\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u672c\u7ae0\u8282\u5c06\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u9632\u6b62SQL\u6ce8\u5165\uff0c\u5e76\u901a\u8fc7\u811a\u672c\u6765\u8fc7\u6ee4SQL\u4e2d\u6ce8\u5165\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>\u6240\u8c13SQL\u6ce8\u5165\uff0c\u5c31\u662f\u901a\u8fc7\u628aSQL\u547d\u4ee4\u63d2\u5165\u5230Web\u8868\u5355\u9012\u4ea4\u6216\u8f93\u5165\u57df\u540d\u6216\u9875\u9762\u8bf7\u6c42\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u6700\u7ec8\u8fbe\u5230\u6b3a\u9a97\u670d\u52a1\u5668\u6267\u884c\u6076\u610f\u7684SQL\u547d\u4ee4\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u7684\u8f93\u5165\uff0c\u6211\u4eec\u5fc5\u987b\u8ba4\u5b9a\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u90fd\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u6211\u4eec\u90fd\u9700\u8981\u5bf9\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\uff0c\u8f93\u5165\u7684\u7528\u6237\u540d\u5fc5\u987b\u4e3a\u5b57\u6bcd\u3001\u6570\u5b57\u53ca\u4e0b\u5212\u7ebf\u7684\u7ec4\u5408\uff0c\u4e14\u7528\u6237\u540d\u957f\u5ea6\u4e3a 8 \u5230 20 \u4e2a\u5b57\u7b26\u4e4b\u95f4\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">if (preg_match(\"\/^\\w{<strong>8<\/strong>,<strong>20<\/strong>}$\/\", $_GET['username'], $matches))\n{\n   $result = mysqli_query($conn, \"SELECT * FROM users \n                          WHERE username=$matches[0]\");\n}\n else \n{\n   echo \"username \u8f93\u5165\u5f02\u5e38\";\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<p>\u8ba9\u6211\u4eec\u770b\u4e0b\u5728\u6ca1\u6709\u8fc7\u6ee4\u7279\u6b8a\u5b57\u7b26\u65f6\uff0c\u51fa\u73b0\u7684SQL\u60c5\u51b5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u8bbe\u5b9a$name \u4e2d\u63d2\u5165\u4e86\u6211\u4eec\u4e0d\u9700\u8981\u7684SQL\u8bed\u53e5<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">$name = \"Qadir'; DELETE FROM users;\";\n mysqli_query($conn, \"SELECT * FROM users WHERE name='{$name}'\");<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u7684\u6ce8\u5165\u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9 <em>n<\/em><em>a<\/em><em>m<\/em><em>e<\/em>\u7684\u53d8\u91cf\u8fdb\u884c\u8fc7\u6ee4\uff0cname\u7684\u53d8\u91cf\u8fdb\u884c\u8fc7\u6ee4\uff0c name \u4e2d\u63d2\u5165\u4e86\u6211\u4eec\u4e0d\u9700\u8981\u7684SQL\u8bed\u53e5\uff0c\u5c06\u5220\u9664 users \u8868\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u5728PHP\u4e2d\u7684 mysqli_query() \u662f\u4e0d\u5141\u8bb8\u6267\u884c\u591a\u4e2a SQL \u8bed\u53e5\u7684\uff0c\u4f46\u662f\u5728 SQLite \u548c PostgreSQL \u662f\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u6761SQL\u8bed\u53e5\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5bf9\u8fd9\u4e9b\u7528\u6237\u7684\u6570\u636e\u9700\u8981\u8fdb\u884c\u4e25\u683c\u7684\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<p>\u9632\u6b62SQL\u6ce8\u5165\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u4e2a\u8981\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1.\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u7684\u8f93\u5165\u3002\u5bf9\u7528\u6237\u7684\u8f93\u5165\u8fdb\u884c\u6821\u9a8c\uff0c\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6216\u9650\u5236\u957f\u5ea6\uff1b\u5bf9\u5355\u5f15\u53f7\u548c \u53cc\"-\"\u8fdb\u884c\u8f6c\u6362\u7b49\u3002<\/li><li>2.\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528\u52a8\u6001\u62fc\u88c5sql\uff0c\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u5316\u7684sql\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\u8fdb\u884c\u6570\u636e\u67e5\u8be2\u5b58\u53d6\u3002<\/li><li>3.\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4e3a\u6bcf\u4e2a\u5e94\u7528\u4f7f\u7528\u5355\u72ec\u7684\u6743\u9650\u6709\u9650\u7684\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li><li>4.\u4e0d\u8981\u628a\u673a\u5bc6\u4fe1\u606f\u76f4\u63a5\u5b58\u653e\uff0c\u52a0\u5bc6\u6216\u8005hash\u6389\u5bc6\u7801\u548c\u654f\u611f\u7684\u4fe1\u606f\u3002<\/li><li>5.\u5e94\u7528\u7684\u5f02\u5e38\u4fe1\u606f\u5e94\u8be5\u7ed9\u51fa\u5c3d\u53ef\u80fd\u5c11\u7684\u63d0\u793a\uff0c\u6700\u597d\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u5bf9\u539f\u59cb\u9519\u8bef\u4fe1\u606f\u8fdb\u884c\u5305\u88c5<\/li><li>6.sql\u6ce8\u5165\u7684\u68c0\u6d4b\u65b9\u6cd5\u4e00\u822c\u91c7\u53d6\u8f85\u52a9\u8f6f\u4ef6\u6216\u7f51\u7ad9\u5e73\u53f0\u6765\u68c0\u6d4b\uff0c\u8f6f\u4ef6\u4e00\u822c\u91c7\u7528sql\u6ce8\u5165\u68c0\u6d4b\u5de5\u5177jsky\uff0c\u7f51\u7ad9\u5e73\u53f0\u5c31\u6709\u4ebf\u601d\u7f51\u7ad9\u5b89\u5168\u5e73\u53f0\u68c0\u6d4b\u5de5\u5177\u3002MDCSOFT SCAN\u7b49\u3002\u91c7\u7528MDCSOFT-IPS\u53ef\u4ee5\u6709\u6548\u7684\u9632\u5fa1SQL\u6ce8\u5165\uff0cXSS\u653b\u51fb\u7b49\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u9632\u6b62SQL\u6ce8\u5165<\/h2>\n\n\n\n<p>\u5728\u811a\u672c\u8bed\u8a00\uff0c\u5982Perl\u548cPHP\u4f60\u53ef\u4ee5\u5bf9\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u8f6c\u4e49\u4ece\u800c\u6765\u9632\u6b62SQL\u6ce8\u5165\u3002<\/p>\n\n\n\n<p>PHP\u7684MySQL\u6269\u5c55\u63d0\u4f9b\u4e86mysqli_real_escape_string()\u51fd\u6570\u6765\u8f6c\u4e49\u7279\u6b8a\u7684\u8f93\u5165\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (get_magic_quotes_gpc()) \n{\n  $name = stripslashes($name);\n}\n$name = mysqli_real_escape_string($conn, $name);\n mysqli_query($conn, \"SELECT * FROM users WHERE name='{$name}'\");<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Like\u8bed\u53e5\u4e2d\u7684\u6ce8\u5165<\/h2>\n\n\n\n<p>like\u67e5\u8be2\u65f6\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u503c\u6709\"_\"\u548c\"%\"\uff0c\u5219\u4f1a\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\uff1a\u7528\u6237\u672c\u6765\u53ea\u662f\u60f3\u67e5\u8be2\"abcd_\"\uff0c\u67e5\u8be2\u7ed3\u679c\u4e2d\u5374\u6709\"abcd_\"\u3001\"abcde\"\u3001\"abcdf\"\u7b49\u7b49\uff1b\u7528\u6237\u8981\u67e5\u8be2\"30%\"\uff08\u6ce8\uff1a\u767e\u5206\u4e4b\u4e09\u5341\uff09\u65f6\u4e5f\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u5728PHP\u811a\u672c\u4e2d\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528addcslashes()\u51fd\u6570\u6765\u5904\u7406\u4ee5\u4e0a\u60c5\u51b5\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$sub = addcslashes(mysqli_real_escape_string($conn, \"%something_\"), \"%_\");\n\/\/ $sub == \\%something\\_\n mysqli_query($conn, \"SELECT * FROM messages WHERE subject LIKE '{$sub}%'\");<\/pre>\n\n\n\n<p>addcslashes() \u51fd\u6570\u5728\u6307\u5b9a\u7684\u5b57\u7b26\u524d\u6dfb\u52a0\u53cd\u659c\u6760\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\u683c\u5f0f:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">addcslashes(string,characters)<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u53c2\u6570<\/strong><\/td><td><strong>\u63cf\u8ff0<\/strong><\/td><\/tr><tr><td>string<\/td><td>\u5fc5\u9700\u3002\u89c4\u5b9a\u8981\u68c0\u67e5\u7684\u5b57\u7b26\u4e32\u3002<\/td><\/tr><tr><td>characters<\/td><td>\u53ef\u9009\u3002\u89c4\u5b9a\u53d7 addcslashes() \u5f71\u54cd\u7684\u5b57\u7b26\u6216\u5b57\u7b26\u8303\u56f4\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e09\u5341\u4e09:\u5bfc\u51fa\u6570\u636e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">33.1\u4f7f\u7528 SELECT ... INTO OUTFILE \u8bed\u53e5\u5bfc\u51fa\u6570\u636e<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u6211\u4eec\u5c06\u6570\u636e\u8868tb1<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT * FROM tb1\n    -&gt; INTO OUTFILE '\/tmp\/tutorials.txt';<\/pre>\n\n\n\n<p>\u6570\u636e\u5bfc\u51fa\u5230 \/tmp\/tutorials.txt \u6587\u4ef6\u4e2d:<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u9009\u9879\u6765\u8bbe\u7f6e\u6570\u636e\u8f93\u51fa\u7684\u6307\u5b9a\u683c\u5f0f\uff0c\u4ee5\u4e0b\u5b9e\u4f8b\u4e3a\u5bfc\u51fa CSV \u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT * FROM passwd INTO OUTFILE '\/tmp\/tutorials.txt'\n    -&gt; FIELDS TERMINATED BY ',' ENCLOSED BY '\"'\n    -&gt; LINES TERMINATED BY '\\r\\n';<\/pre>\n\n\n\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u751f\u6210\u4e00\u4e2a\u6587\u4ef6\uff0c\u5404\u503c\u7528\u9017\u53f7\u9694\u5f00\u3002\u8fd9\u79cd\u683c\u5f0f\u53ef\u4ee5\u88ab\u8bb8\u591a\u7a0b\u5e8f\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT a,b,a+b INTO OUTFILE '\/tmp\/result.text'\nFIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'\nLINES TERMINATED BY '\\n'\nFROM test_table;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SELECT ... INTO OUTFILE \u8bed\u53e5\u6709\u4ee5\u4e0b\u5c5e\u6027:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>LOAD DATA INFILE\u662fSELECT ... INTO OUTFILE\u7684\u9006\u64cd\u4f5c\uff0cSELECT\u53e5\u6cd5\u3002\u4e3a\u4e86\u5c06\u4e00\u4e2a\u6570\u636e\u5e93\u7684\u6570\u636e\u5199\u5165\u4e00\u4e2a\u6587\u4ef6\uff0c\u4f7f\u7528SELECT ... INTO OUTFILE\uff0c\u4e3a\u4e86\u5c06\u6587\u4ef6\u8bfb\u56de\u6570\u636e\u5e93\uff0c\u4f7f\u7528LOAD DATA INFILE\u3002<\/li><li>SELECT...INTO OUTFILE 'file_name'\u5f62\u5f0f\u7684SELECT\u53ef\u4ee5\u628a\u88ab\u9009\u62e9\u7684\u884c\u5199\u5165\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002\u8be5\u6587\u4ef6\u88ab\u521b\u5efa\u5230\u670d\u52a1\u5668\u4e3b\u673a\u4e0a\uff0c\u56e0\u6b64\u60a8\u5fc5\u987b\u62e5\u6709FILE\u6743\u9650\uff0c\u624d\u80fd\u4f7f\u7528\u6b64\u8bed\u6cd5\u3002<\/li><li>\u8f93\u51fa\u4e0d\u80fd\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u3002\u9632\u6b62\u6587\u4ef6\u6570\u636e\u88ab\u7be1\u6539\u3002<\/li><li>\u4f60\u9700\u8981\u6709\u4e00\u4e2a\u767b\u9646\u670d\u52a1\u5668\u7684\u8d26\u53f7\u6765\u68c0\u7d22\u6587\u4ef6\u3002\u5426\u5219 SELECT ... INTO OUTFILE \u4e0d\u4f1a\u8d77\u4efb\u4f55\u4f5c\u7528\u3002<\/li><li>\u5728UNIX\u4e2d\uff0c\u8be5\u6587\u4ef6\u88ab\u521b\u5efa\u540e\u662f\u53ef\u8bfb\u7684\uff0c\u6743\u9650\u7531MySQL\u670d\u52a1\u5668\u6240\u62e5\u6709\u3002\u8fd9\u610f\u5473\u7740\uff0c\u867d\u7136\u4f60\u5c31\u53ef\u4ee5\u8bfb\u53d6\u8be5\u6587\u4ef6\uff0c\u4f46\u53ef\u80fd\u65e0\u6cd5\u5c06\u5176\u5220\u9664\u3002<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33.2\u5bfc\u51fa\u8868\u4f5c\u4e3a\u539f\u59cb\u6570\u636e<\/h2>\n\n\n\n<p>mysqldump\u662fmysql\u7528\u4e8e\u8f6c\u5b58\u50a8\u6570\u636e\u5e93\u7684\u5b9e\u7528\u7a0b\u5e8f\u3002\u5b83\u4e3b\u8981\u4ea7\u751f\u4e00\u4e2aSQL\u811a\u672c\uff0c\u5176\u4e2d\u5305\u542b\u4ece\u5934\u91cd\u65b0\u521b\u5efa\u6570\u636e\u5e93\u6240\u5fc5\u9700\u7684\u547d\u4ee4CREATE TABLE INSERT\u7b49\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528mysqldump\u5bfc\u51fa\u6570\u636e\u9700\u8981\u4f7f\u7528 --tab \u9009\u9879\u6765\u6307\u5b9a\u5bfc\u51fa\u6587\u4ef6\u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u8be5\u76ee\u6807\u5fc5\u987b\u662f\u53ef\u5199\u7684\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u5c06\u6570\u636e\u8868 tb1 \u5bfc\u51fa\u5230 \/tmp \u76ee\u5f55\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqldump -u root -p --no-create-info \\\n            --tab=\/tmp RUNOOB runoob_tbl\npassword ******<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33.3\u5bfc\u51faSQL\u683c\u5f0f\u7684\u6570\u636e<\/h2>\n\n\n\n<p>\u5bfc\u51faSQL\u683c\u5f0f\u7684\u6570\u636e\u5230\u6307\u5b9a\u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqldump -u root -p RUNOOB runoob_tbl &gt; dump.txt\npassword ******<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u547d\u4ee4\u521b\u5efa\u7684\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">-- MySQL dump 8.23\n--\n-- Host: localhost    Database: RUNOOB\n---------------------------------------------------------\n-- Server version       3.23.58\n--\n-- Table structure for table `runoob_tbl`\n--\nCREATE TABLE runoob_tbl (\n  runoob_id int(<strong>11<\/strong>) NOT NULL auto_increment,\n  runoob_title varchar(<strong>100<\/strong>) NOT NULL default '',\n  runoob_author varchar(<strong>40<\/strong>) NOT NULL default '',\n  submission_date date default NULL,\n  PRIMARY KEY  (runoob_id),\n  UNIQUE KEY AUTHOR_INDEX (runoob_author)\n) TYPE=MyISAM;\n\n--\n-- Dumping data for table `runoob_tbl`\n--\n\nINSERT INTO runoob_tbl \n       VALUES (<strong>1<\/strong>,'Learn PHP','John Poul','2007-05-24');\nINSERT INTO runoob_tbl \n       VALUES (<strong>2<\/strong>,'Learn MySQL','Abdul S','2007-05-24');\nINSERT INTO runoob_tbl \n       VALUES (<strong>3<\/strong>,'JAVA Tutorial','Sanjay','2007-05-06');<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"javascript:void(0);\" target=\"_blank\"  rel=\"nofollow\" ><img decoding=\"async\" src=\"\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" title=\"MYSQL\u5e38\u7528\u57fa\u672cSQL\u8bed\u53e5\u603b\u7ed3---mysql\" alt=\"\u590d\u5236\u4ee3\u7801\"\/><\/a><\/figure>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u5bfc\u51fa\u6574\u4e2a\u6570\u636e\u5e93\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqldump -u root -p RUNOOB &gt; database_dump.txt\npassword ******<\/pre>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u5907\u4efd\u6240\u6709\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqldump -u root -p --all-databases &gt; database_dump.txt\npassword ******<\/pre>\n\n\n\n<p>--all-databases \u9009\u9879\u5728 MySQL 3.23.12 \u53ca\u4ee5\u540e\u7248\u672c\u52a0\u5165\u3002<\/p>\n\n\n\n<p>\u8be5\u65b9\u6cd5\u53ef\u7528\u4e8e\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u5907\u4efd\u7b56\u7565\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">33.4\u5c06\u6570\u636e\u8868\u53ca\u6570\u636e\u5e93\u62f7\u8d1d\u81f3\u5176\u4ed6\u4e3b\u673a<\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u5c06\u6570\u636e\u62f7\u8d1d\u81f3\u5176\u4ed6\u7684 MySQL \u670d\u52a1\u5668\u4e0a, \u4f60\u53ef\u4ee5\u5728 mysqldump \u547d\u4ee4\u4e2d\u6307\u5b9a\u6570\u636e\u5e93\u540d\u53ca\u6570\u636e\u8868\u3002<\/p>\n\n\n\n<p>\u5728\u6e90\u4e3b\u673a\u4e0a\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5c06\u6570\u636e\u5907\u4efd\u81f3 dump.txt \u6587\u4ef6\u4e2d:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqldump -u root -p database_name table_name &gt; dump.txt\npassword *****<\/pre>\n\n\n\n<p>\u5982\u679c\u5b8c\u6574\u5907\u4efd\u6570\u636e\u5e93\uff0c\u5219\u65e0\u9700\u4f7f\u7528\u7279\u5b9a\u7684\u8868\u540d\u79f0\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u5c06\u5907\u4efd\u7684\u6570\u636e\u5e93\u5bfc\u5165\u5230MySQL\u670d\u52a1\u5668\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f60\u9700\u8981\u786e\u8ba4\u6570\u636e\u5e93\u5df2\u7ecf\u521b\u5efa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysql -u root -p database_name &lt; dump.txt\npassword *****<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5bfc\u51fa\u7684\u6570\u636e\u76f4\u63a5\u5bfc\u5165\u5230\u8fdc\u7a0b\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u4f46\u8bf7\u786e\u4fdd\u4e24\u53f0\u670d\u52a1\u5668\u662f\u76f8\u901a\u7684\uff0c\u662f\u53ef\u4ee5\u76f8\u4e92\u8bbf\u95ee\u7684\uff1a<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqldump -h other-host.com -P port -u root -p database_name &gt; dump.txt\npassword ****<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4e09\u5341\u56db:\u5bfc\u5165\u6570\u636e<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">34.1\u4f7f\u7528 LOAD DATA \u5bfc\u5165\u6570\u636e<\/h2>\n\n\n\n<p>MySQL \u4e2d\u63d0\u4f9b\u4e86LOAD DATA INFILE\u8bed\u53e5\u6765\u63d2\u5165\u6570\u636e\u3002 \u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u5c06\u4ece\u5f53\u524d\u76ee\u5f55\u4e2d\u8bfb\u53d6\u6587\u4ef6 dump.txt \uff0c\u5c06\u8be5\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u63d2\u5165\u5230\u5f53\u524d\u6570\u636e\u5e93\u7684 mytbl \u8868\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl;<\/pre>\n\n\n\n<p>\u3000\u5982\u679c\u6307\u5b9aLOCAL\u5173\u952e\u8bcd\uff0c\u5219\u8868\u660e\u4ece\u5ba2\u6237\u4e3b\u673a\u4e0a\u6309\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\uff0c\u5219\u6587\u4ef6\u5728\u670d\u52a1\u5668\u4e0a\u6309\u8def\u5f84\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u4f60\u80fd\u660e\u786e\u5730\u5728LOAD DATA\u8bed\u53e5\u4e2d\u6307\u51fa\u5217\u503c\u7684\u5206\u9694\u7b26\u548c\u884c\u5c3e\u6807\u8bb0\uff0c\u4f46\u662f\u9ed8\u8ba4\u6807\u8bb0\u662f\u5b9a\u4f4d\u7b26\u548c\u6362\u884c\u7b26\u3002<\/p>\n\n\n\n<p>\u4e24\u4e2a\u547d\u4ee4\u7684 FIELDS \u548c LINES \u5b50\u53e5\u7684\u8bed\u6cd5\u662f\u4e00\u6837\u7684\u3002\u4e24\u4e2a\u5b50\u53e5\u90fd\u662f\u53ef\u9009\u7684\uff0c\u4f46\u662f\u5982\u679c\u4e24\u4e2a\u540c\u65f6\u88ab\u6307\u5b9a\uff0cFIELDS \u5b50\u53e5\u5fc5\u987b\u51fa\u73b0\u5728 LINES \u5b50\u53e5\u4e4b\u524d\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u7528\u6237\u6307\u5b9a\u4e00\u4e2a FIELDS \u5b50\u53e5\uff0c\u5b83\u7684\u5b50\u53e5 \uff08TERMINATED BY\u3001[OPTIONALLY] ENCLOSED BY \u548c ESCAPED BY) \u4e5f\u662f\u53ef\u9009\u7684\uff0c\u4e0d\u8fc7\uff0c\u7528\u6237\u5fc5\u987b\u81f3\u5c11\u6307\u5b9a\u5b83\u4eec\u4e2d\u7684\u4e00\u4e2a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl\n  -&gt; FIELDS TERMINATED BY ':'\n  -&gt; LINES TERMINATED BY '\\r\\n';<\/pre>\n\n\n\n<p>LOAD DATA \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u6309\u7167\u6570\u636e\u6587\u4ef6\u4e2d\u5217\u7684\u987a\u5e8f\u63d2\u5165\u6570\u636e\u7684\uff0c\u5982\u679c\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u5217\u4e0e\u63d2\u5165\u8868\u4e2d\u7684\u5217\u4e0d\u4e00\u81f4\uff0c\u5219\u9700\u8981\u6307\u5b9a\u5217\u7684\u987a\u5e8f\u3002<\/p>\n\n\n\n<p>\u5982\uff0c\u5728\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u5217\u987a\u5e8f\u662f a,b,c\uff0c\u4f46\u5728\u63d2\u5165\u8868\u7684\u5217\u987a\u5e8f\u4e3ab,c,a\uff0c\u5219\u6570\u636e\u5bfc\u5165\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; LOAD DATA LOCAL INFILE 'dump.txt' \n    -&gt; INTO TABLE mytbl (b, c, a);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528 mysqlimport \u5bfc\u5165\u6570\u636e<\/h2>\n\n\n\n<p>mysqlimport\u5ba2\u6237\u7aef\u63d0\u4f9b\u4e86LOAD DATA INFILEQL\u8bed\u53e5\u7684\u4e00\u4e2a\u547d\u4ee4\u884c\u63a5\u53e3\u3002mysqlimport\u7684\u5927\u591a\u6570\u9009\u9879\u76f4\u63a5\u5bf9\u5e94LOAD DATA INFILE\u5b50\u53e5\u3002<\/p>\n\n\n\n<p>\u4ece\u6587\u4ef6 dump.txt \u4e2d\u5c06\u6570\u636e\u5bfc\u5165\u5230 mytbl \u6570\u636e\u8868\u4e2d, \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqlimport -u root -p --local database_name dump.txt\npassword *****<\/pre>\n\n\n\n<p>mysqlimport\u547d\u4ee4\u53ef\u4ee5\u6307\u5b9a\u9009\u9879\u6765\u8bbe\u7f6e\u6307\u5b9a\u683c\u5f0f,\u547d\u4ee4\u8bed\u53e5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqlimport -u root -p --local --fields-terminated-by=\":\" \\\n   --lines-terminated-by=\"\\r\\n\"  database_name dump.txt\npassword *****<\/pre>\n\n\n\n<p>mysqlimport \u8bed\u53e5\u4e2d\u4f7f\u7528 --columns \u9009\u9879\u6765\u8bbe\u7f6e\u5217\u7684\u987a\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mysqlimport -u root -p --local --columns=b,c,a \\\n    database_name dump.txt\npassword *****<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">mysqlimport\u7684\u5e38\u7528\u9009\u9879\u4ecb\u7ecd<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u9009\u9879<\/strong><\/td><td><strong>\u529f\u80fd<\/strong><\/td><\/tr><tr><td>-d or --delete<\/td><td>\u65b0\u6570\u636e\u5bfc\u5165\u6570\u636e\u8868\u4e2d\u4e4b\u524d\u5220\u9664\u6570\u636e\u6570\u636e\u8868\u4e2d\u7684\u6240\u6709\u4fe1\u606f<\/td><\/tr><tr><td>-f or --force<\/td><td>\u4e0d\u7ba1\u662f\u5426\u9047\u5230\u9519\u8bef\uff0cmysqlimport\u5c06\u5f3a\u5236\u7ee7\u7eed\u63d2\u5165\u6570\u636e<\/td><\/tr><tr><td>-i or --ignore<\/td><td>mysqlimport\u8df3\u8fc7\u6216\u8005\u5ffd\u7565\u90a3\u4e9b\u6709\u76f8\u540c\u552f\u4e00 \u5173\u952e\u5b57\u7684\u884c\uff0c \u5bfc\u5165\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u5c06\u88ab\u5ffd\u7565\u3002<\/td><\/tr><tr><td>-l or -lock-tables<\/td><td>\u6570\u636e\u88ab\u63d2\u5165\u4e4b\u524d\u9501\u4f4f\u8868\uff0c\u8fd9\u6837\u5c31\u9632\u6b62\u4e86\uff0c \u4f60\u5728\u66f4\u65b0\u6570\u636e\u5e93\u65f6\uff0c\u7528\u6237\u7684\u67e5\u8be2\u548c\u66f4\u65b0\u53d7\u5230\u5f71\u54cd\u3002<\/td><\/tr><tr><td>-r or -replace<\/td><td>\u8fd9\u4e2a\u9009\u9879\u4e0e\uff0di\u9009\u9879\u7684\u4f5c\u7528\u76f8\u53cd\uff1b\u6b64\u9009\u9879\u5c06\u66ff\u4ee3 \u8868\u4e2d\u6709\u76f8\u540c\u552f\u4e00\u5173\u952e\u5b57\u7684\u8bb0\u5f55\u3002<\/td><\/tr><tr><td>--fields-enclosed- by= char<\/td><td>\u6307\u5b9a\u6587\u672c\u6587\u4ef6\u4e2d\u6570\u636e\u7684\u8bb0\u5f55\u65f6\u4ee5\u4ec0\u4e48\u62ec\u8d77\u7684\uff0c \u5f88\u591a\u60c5\u51b5\u4e0b \u6570\u636e\u4ee5\u53cc\u5f15\u53f7\u62ec\u8d77\u3002 \u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\u6570\u636e\u662f\u6ca1\u6709\u88ab\u5b57\u7b26\u62ec\u8d77\u7684\u3002<\/td><\/tr><tr><td>--fields-terminated- by=char<\/td><td>\u6307\u5b9a\u5404\u4e2a\u6570\u636e\u7684\u503c\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c\u5728\u53e5\u53f7\u5206\u9694\u7684\u6587\u4ef6\u4e2d\uff0c \u5206\u9694\u7b26\u662f\u53e5\u53f7\u3002\u60a8\u53ef\u4ee5\u7528\u6b64\u9009\u9879\u6307\u5b9a\u6570\u636e\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002 \u9ed8\u8ba4\u7684\u5206\u9694\u7b26\u662f\u8df3\u683c\u7b26\uff08Tab\uff09<\/td><\/tr><tr><td>--lines-terminated- by=str<\/td><td>\u6b64\u9009\u9879\u6307\u5b9a\u6587\u672c\u6587\u4ef6\u4e2d\u884c\u4e0e\u884c\u4e4b\u95f4\u6570\u636e\u7684\u5206\u9694\u5b57\u7b26\u4e32 \u6216\u8005\u5b57\u7b26\u3002 \u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0bmysqlimport\u4ee5newline\u4e3a\u884c\u5206\u9694\u7b26\u3002 \u60a8\u53ef\u4ee5\u9009\u62e9\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u66ff\u4ee3\u4e00\u4e2a\u5355\u4e2a\u7684\u5b57\u7b26\uff1a \u4e00\u4e2a\u65b0\u884c\u6216\u8005\u4e00\u4e2a\u56de\u8f66\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>mysqlimport\u547d\u4ee4\u5e38\u7528\u7684\u9009\u9879\u8fd8\u6709-v \u663e\u793a\u7248\u672c\uff08version\uff09\uff0c -p \u63d0\u793a\u8f93\u5165\u5bc6\u7801\uff08password\uff09\u7b49\u3002Little strokes fell great oaks. Keep on going never give up.<\/p>\n\n\n\n<p><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"38\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>SQL\u5206\u7c7b: DDL&#8212;&#8211;\u6570\u636e\u5b9a\u4e49\u8bed\u8a00(CREATE&#8211;\u521b\u5efa,ALTER&#8211;\u4fee\u6539. DROP&#8211;\u5220\u9664\u8868,DECLARE&#8211;\u58f0\u660e &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38"}],"version-history":[{"count":0,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}