{"id":47,"date":"2022-07-06T10:20:27","date_gmt":"2022-07-06T02:20:27","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=47"},"modified":"2023-05-25T14:25:18","modified_gmt":"2023-05-25T06:25:18","slug":"mysql%e5%9f%ba%e7%a1%80%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/mysql\/mysql%e5%9f%ba%e7%a1%80%e8%af%ad%e5%8f%a5\/","title":{"rendered":"MySQL\u57fa\u7840\u8bed\u53e5"},"content":{"rendered":"\n<p><strong>1\u3001\u521b\u5efa\u6570\u636e\u5e93<\/strong> create database database-name<\/p>\n\n\n\n<p><strong>2\u3001\u5220\u9664\u6570\u636e\u5e93 <\/strong>drop database  database-name <\/p>\n\n\n\n<p><strong>3\u3001\u4fee\u6539\u6570\u636e\u5e93\u7684\u540d\u79f0<\/strong>   sp_renamedb 'old_name', 'new_name' <\/p>\n\n\n\n<p><strong>4\u3001\u5bf9\u6570\u636e\u5e93\u64cd\u4f5c<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> <strong>\u5206\u79bb\u6570\u636e\u5e93<\/strong>\uff1a<strong>&nbsp;sp_detach_db;<\/strong> <\/li><li> <strong>\u9644\u52a0\u6570\u636e\u5e93<\/strong>\uff1a<strong>sp_attach_db&nbsp;<\/strong>\u540e\u63a5\u8868\u660e\uff0c\u9644\u52a0\u9700\u8981\u5b8c\u6574\u7684\u8def\u5f84\u540d <\/li><\/ul>\n\n\n\n<p><strong>5\u3001\u5907\u4efdsql server<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong> \u521b\u5efa \u5907\u4efd\u6570\u636e\u7684&nbsp;device<\/strong><ul><li>use master<\/li><li>exec sp_addumpdevice 'disk', 'testBack', 'c:\\mssql7backup\\MyNwind_1.dat'<\/li><\/ul><\/li><li><strong>\u5f00\u59cb \u5907\u4efd<\/strong><ul><li>backup database pubs to testBack&nbsp; <\/li><\/ul><\/li><\/ul>\n\n\n\n<p><strong>6\u3001\u521b\u5efa\u65b0\u8868<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> create table &lt;\u8868\u540d&gt; (col1 type1 [not null] [primary key],col2 type2 [not null],..)<\/li><li><strong>\u6839\u636e\u5df2\u6709\u7684\u8868\u521b\u5efa\u65b0\u8868\uff1a&nbsp;<\/strong><ul><li><strong>A\uff1a<\/strong>create table tab_new like tab_old (\u4f7f\u7528\u65e7\u8868\u521b\u5efa\u65b0\u8868)<\/li><li><strong>B\uff1a<\/strong>create table tab_new as select col1,col2\u2026 from tab_old definition only <\/li><\/ul><\/li><\/ul>\n\n\n\n<p><strong>7\u3001\u6e05\u7a7a\u6570\u636e\u5e93\u8868<\/strong> truncate table &lt;\u8868\u540d&gt; <\/p>\n\n\n\n<p><strong>8\u3001\u5220\u9664\u65b0\u8868<\/strong> drop table &lt;\u8868\u540d&gt;<\/p>\n\n\n\n<p><strong>9\u3001\u589e\u52a0\u4e00\u4e2a\u5217<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> Alter table &lt;\u8868\u540d&gt; add column col type  <\/li><li><strong>\u6ce8<\/strong>\uff1a\u5217\u589e\u52a0\u540e\u5c06\u4e0d\u80fd\u5220\u9664\u3002DB2\u4e2d\u5217\u52a0\u4e0a\u540e\u6570\u636e\u7c7b\u578b\u4e5f\u4e0d\u80fd\u6539\u53d8\uff0c\u552f\u4e00\u80fd\u6539\u53d8\u7684\u662f\u589e\u52a0varchar\u7c7b\u578b\u7684\u957f\u5ea6\u3002 <\/li><\/ul>\n\n\n\n<p><strong>10\u3001\u6dfb\u52a0\u4e3b\u952e\/\u5220\u9664\u4e3b\u952e<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u65b0\u589e\u4e3b\u952e\uff1a<ul><li>Alter table tabname add primary key(col)&nbsp;<\/li><\/ul><\/li><li>\u5220\u9664\u4e3b\u952e\uff1a<ul><li>Alter table tabname drop primary key(col) <\/li><\/ul><\/li><\/ul>\n\n\n\n<p><strong>11\u3001\u521b\u5efa\u7d22\u5f15\/\u5220\u9664\u7d22\u5f15<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u521b\u5efa\u7d22\u5f15<ul><li> create [unique] index idxname on tabname(col\u2026.)&nbsp; <\/li><\/ul><\/li><li>\u5220\u9664\u7d22\u5f15<ul><li> drop index idxname <\/li><\/ul><\/li><li> <strong>\u6ce8\uff1a\u7d22\u5f15\u662f\u4e0d\u53ef\u66f4\u6539\u7684\uff0c\u60f3\u66f4\u6539\u5fc5\u987b\u5220\u9664\u91cd\u65b0\u5efa <\/strong><\/li><\/ul>\n\n\n\n<p><strong>12\u3001\u521b\u5efa\u89c6\u56fe\/\u5220\u9664\u89c6\u56fe<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u521b\u5efa\u89c6\u56fe\uff1a<ul><li> create view viewname as select statement  <\/li><\/ul><\/li><li>\u5220\u9664\u89c6\u56fe\uff1a<ul><li> drop view viewname <\/li><\/ul><\/li><\/ul>\n\n\n\n<p> <strong>13\u3001\u51e0\u4e2a\u7b80\u5355\u7684\u57fa\u672c\u7684SQL\u8bed\u53e5<\/strong> <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u9009\u62e9\uff1a<\/strong>select * from tableName where&nbsp;\u8303\u56f4<\/li><li><strong>\u63d2\u5165\uff1a<\/strong>insert into  tableName  (field1,field2) values(value1,value2)<\/li><li><strong>\u5220\u9664\uff1a<\/strong>delete from  tableName  where&nbsp;\u8303\u56f4<\/li><li><strong>\u66f4\u65b0<\/strong>\uff1aupdate  tableName  set field1=value1 where \u8303\u56f4<\/li><li><strong>\u6a21\u7cca\u67e5\u627e<\/strong>\uff1aselect * from  tableName  where field1 like \u2019%value1%\u2019<\/li><li><strong>\u6392\u5e8f<\/strong>\uff1aselect * from  tableName  order by field1,field2 [desc]<\/li><li><strong>\u603b\u6570<\/strong>\uff1aselect count as totalcount from  tableName  <\/li><li><strong>\u6c42\u548c<\/strong>\uff1aselect sum(field1) as sumvalue from  tableName  <\/li><li><strong>\u5e73\u5747<\/strong>\uff1aselect avg(field1) as avgvalue from  tableName  <\/li><li><strong>\u6700\u5927<\/strong>\uff1aselect max(field1) as maxvalue from  tableName  <\/li><li><strong>\u6700\u5c0f<\/strong>\uff1aselect min(field1) as minvalue from  tableName  <\/li><\/ul>\n\n\n\n<p><strong>14\u3001\u9ad8\u7ea7\u67e5\u8be2\u8fd0\u7b97\u7b26\uff08<\/strong> <strong>UNION\/EXCEPT\/INTERSECT<\/strong> <strong>\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> <strong>A\uff1aUNION \u8fd0\u7b97\u7b26<\/strong>&nbsp;<ul><li>UNION \u8fd0\u7b97\u7b26\u901a\u8fc7\u7ec4\u5408\u5176\u4ed6\u4e24\u4e2a\u7ed3\u679c\u8868\uff08\u4f8b\u5982 TABLE1 \u548c TABLE2\uff09\u5e76\u6d88\u53bb\u8868\u4e2d\u4efb\u4f55\u91cd\u590d\u884c\u800c\u6d3e\u751f\u51fa\u4e00\u4e2a\u7ed3\u679c\u8868\u3002\u5f53 ALL \u968f UNION \u4e00\u8d77\u4f7f\u7528\u65f6\uff08\u5373 UNION ALL\uff09\uff0c\u4e0d\u6d88\u9664\u91cd\u590d\u884c\u3002\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u6d3e\u751f\u8868\u7684\u6bcf\u4e00\u884c\u4e0d\u662f\u6765\u81ea TABLE1 \u5c31\u662f\u6765\u81ea TABLE2\u3002&nbsp;<\/li><\/ul><\/li><li> <strong>B\uff1aEXCEPT \u8fd0\u7b97\u7b26&nbsp;<\/strong>   <ul><li>  <strong>EXCEPT<\/strong>\u8fd0\u7b97\u7b26\u901a\u8fc7\u5305\u62ec\u6240\u6709\u5728 TABLE1 \u4e2d\u4f46\u4e0d\u5728 TABLE2 \u4e2d\u7684\u884c\u5e76\u6d88\u9664\u6240\u6709\u91cd\u590d\u884c\u800c\u6d3e\u751f\u51fa\u4e00\u4e2a\u7ed3\u679c\u8868\u3002\u5f53 ALL \u968f EXCEPT \u4e00\u8d77\u4f7f\u7528\u65f6&nbsp;(EXCEPT ALL)\uff0c\u4e0d\u6d88\u9664\u91cd\u590d\u884c\u3002  <\/li><\/ul><\/li><li> <strong>C\uff1aINTERSECT \u8fd0\u7b97\u7b26INTERSECT<\/strong><ul><li>\u8fd0\u7b97\u7b26\u901a\u8fc7\u53ea\u5305\u62ec TABLE1 \u548c TABLE2 \u4e2d\u90fd\u6709\u7684\u884c\u5e76\u6d88\u9664\u6240\u6709\u91cd\u590d\u884c\u800c\u6d3e\u751f\u51fa\u4e00\u4e2a\u7ed3\u679c\u8868\u3002\u5f53&nbsp;<strong>ALL<\/strong>\u968f INTERSECT \u4e00\u8d77\u4f7f\u7528\u65f6&nbsp;(INTERSECT ALL)\uff0c\u4e0d\u6d88\u9664\u91cd\u590d\u884c\u3002&nbsp;<\/li><li><strong>\u6ce8\uff1a<\/strong>\u4f7f\u7528\u8fd0\u7b97\u8bcd\u7684\u51e0\u4e2a\u67e5\u8be2\u7ed3\u679c\u884c\u5fc5\u987b\u662f\u4e00\u81f4\u7684\u3002 <\/li><\/ul><\/li><\/ul>\n\n\n\n<p><strong>15\u3001\u51e0\u79cd\u5916\u8fde\u63a5\uff08LEFT\/RIGHT\/INNER\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5de6\u5916\u8fde\u63a5\uff08 left&nbsp;\uff08outer\uff09&nbsp;join \uff09<ul><li> \u7ed3\u679c\u96c6\u51e0\u5305\u62ec\u8fde\u63a5\u8868\u7684\u5339\u914d\u884c\uff0c\u4e5f\u5305\u62ec\u5de6\u8fde\u63a5\u8868\u7684\u6240\u6709\u884c\u3002&nbsp;<\/li><li>SQL\u793a\u4f8b: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c <\/li><\/ul><\/li><li>\u53f3\u5916\u8fde\u63a5\uff08 right \uff08outer\uff09 join \uff09<ul><li> \u7ed3\u679c\u96c6\u65e2\u5305\u62ec\u8fde\u63a5\u8868\u7684\u5339\u914d\u8fde\u63a5\u884c\uff0c\u4e5f\u5305\u62ec\u53f3\u8fde\u63a5\u8868\u7684\u6240\u6709\u884c <\/li><\/ul><\/li><li>\u5168\u5916\u8fde\u63a5\uff08 full\/cross \uff08outer\uff09 join \uff09<ul><li> \u4e0d\u4ec5\u5305\u62ec\u7b26\u53f7\u8fde\u63a5\u8868\u7684\u5339\u914d\u884c\uff0c\u8fd8\u5305\u62ec\u4e24\u4e2a\u8fde\u63a5\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55 <\/li><\/ul><\/li><\/ul>\n\n\n\n<p><strong>16\u3001\u5206\u7ec4\uff08GTROUP BY\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> \u4e00\u5f20\u8868\uff0c\u4e00\u65e6\u5206\u7ec4\u5b8c\u6210\u540e\uff0c\u67e5\u8be2\u540e\u53ea\u80fd\u5f97\u5230\u7ec4\u76f8\u5173\u7684\u4fe1\u606f\u3002<\/li><li><strong>\u7ec4\u76f8\u5173\u7684\u4fe1\u606f\uff1a<\/strong>\uff08\u7edf\u8ba1\u4fe1\u606f\uff09count,sum,max,min,avg&nbsp;<strong>\u4e3a\u5206\u7ec4\u7684\u6807\u51c6<\/strong><\/li><li><strong>\u5728SQL Server\u4e2d\u5206\u7ec4\u65f6\uff1a\u4e0d\u80fd\u4ee5text,ntext,image\u7c7b\u578b\u7684\u5b57\u6bb5\u4f5c\u4e3a\u5206\u7ec4\u4f9d\u636e\u5728selecte\u7edf\u8ba1\u51fd\u6570\u4e2d\u7684\u5b57\u6bb5\uff0c\u4e0d\u80fd\u548c\u666e\u901a\u7684\u5b57\u6bb5\u653e\u5728\u4e00\u8d77\uff1b<\/strong> <\/li><\/ul>\n\n\n\n<p><\/p>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>1\u3001\u521b\u5efa\u6570\u636e\u5e93 create database database-name 2\u3001\u5220\u9664\u6570\u636e\u5e93 drop database datab &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-mysql"],"post_thumbnail_image":"","content_first_image":null,"post_medium_image_300":"","post_thumbnail_image_624":"","post_frist_image":null,"post_medium_image":"","post_large_image":"","post_full_image":"","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"MySQL","post_date":"2022-07-06","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":355,"next_post_id":61,"next_post_title":"MySQL\u8fdb\u9636","previous_post_id":null,"previous_post_title":null,"_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}