{"id":505,"date":"2020-08-21T23:28:45","date_gmt":"2020-08-21T15:28:45","guid":{"rendered":"https:\/\/www.notown.club\/?p=505"},"modified":"2020-08-21T23:28:45","modified_gmt":"2020-08-21T15:28:45","slug":"mysql%e5%9f%ba%e6%9c%ac%e5%91%bd%e4%bb%a4%e7%94%a8%e6%b3%95%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=505","title":{"rendered":"Mysql\u57fa\u672c\u547d\u4ee4\u7528\u6cd5\uff08\u4e00\uff09"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>\u4e00\u3001\u57fa\u672c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">show databases;        --\u9648\u5217\u6570\u636e\u5e93\ncreate database \u65b0\u5e93\u540d --\u521b\u5efa\u6570\u636e\u5e93\ndrop database \u6570\u636e\u5e93\u540d  --\u5220\u9664\u6570\u636e\u5e93\nuse \u6570\u636e\u5e93\u540d;          --\u4f7f\u7528\u6570\u636e\u5e93\nshow tables;          --\u9648\u5217\u6570\u636e\u8868\ncreate table \u65b0\u8868\u540d(\u5217\u540d \u6570\u636e\u7c7b\u578b, \u5217\u540d \u6570\u636e\u7c7b\u578b...);   --\u521b\u5efa\u6570\u636e\u8868\ndrop table \u8868\u540d       --\u5220\u9664\u8868 \nselect * from \u8868\u540d    --\u5217\u51fa\u8868\u4e2d\u6240\u6709\u6570\u636e<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e8c\u3001\u589e\u5220\u6539\u67e5<\/strong><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4ee5\u5efa\u4e00\u4e2a\u901a\u8baf\u5f55\u4e3a\u4f8b\uff0c\u5efa\u8868\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create table phone_book( ID int, Name varchar(20), Sex varchar(10), Number varchar(20), Birth datetime);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u8868\u7684\u6027\u8d28\u5982\u4e0b\uff08\u53ef\u901a\u8fc7describe \u8868\u540d\u5f97\uff09\n+--------+-------------+------+-----+---------+-------+\n | Field  | Type        | Null | Key | Default | Extra |\n +--------+-------------+------+-----+---------+-------+\n | ID     | int(11)     | YES  |     | NULL    |       |\n | Name   | varchar(20) | YES  |     | NULL    |       |\n | Sex    | varchar(10) | YES  |     | NULL    |       |\n | Number | varchar(20) | YES  |     | NULL    |       |\n | Birth  | datetime    | YES  |     | NULL    |       |\n +--------+-------------+------+-----+---------+-------+<\/pre>\n\n\n\n<p><strong>1.\u589e <\/strong><\/p>\n\n\n\n<p> insert into \u8868\u540d values( \u6570\u636e1,\u6570\u636e2,\u6570\u636e3,\u6570\u636e4 ); <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">insert into phone_book values(1,'Tom','male','13534556889','1998-05-02');\ninsert into phone_book values(2,'Jenny','female','13534556889','1998-11-12');\ninsert into phone_book values(3,'Bob','male','13954556889','1997-01-12');\n-- \u63d2\u5165\u4e09\u6761\u8bb0\u5f55\nselect * from phone_book;\n +------+-------+--------+-------------+---------------------+\n | ID   | Name  | Sex    | Number      | Birth               |\n +------+-------+--------+-------------+---------------------+\n |    1 | Tom   | male   | 13534556889 | 1998-05-02 00:00:00 |\n |    2 | Jenny | female | 13534556889 | 1998-11-12 00:00:00 |\n |    3 | Bob   | male   | 13954556889 | 1997-01-12 00:00:00 |\n +------+-------+--------+-------------+---------------------+\n 3 rows in set (0.00 sec)<\/pre>\n\n\n\n<p><strong>2.\u5220<\/strong><\/p>\n\n\n\n<p>delete from \u8868\u540d where \u6761\u4ef6;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">delete from phone_book where ID=1;  --\u5220\u6389ID\u4e3a1\u7684\u8bb0\u5f55\nselect * from phone_book;\n +------+-------+--------+-------------+---------------------+\n | ID   | Name  | Sex    | Number      | Birth               |\n +------+-------+--------+-------------+---------------------+\n |    2 | Jenny | female | 13534556889 | 1998-11-12 00:00:00 |\n |    3 | Bob   | male   | 13954556889 | 1997-01-12 00:00:00 |\n +------+-------+--------+-------------+---------------------+\n 2 rows in set (0.00 sec)<\/pre>\n\n\n\n<p><strong>3.\u6539<\/strong><\/p>\n\n\n\n<p>update \u8868\u540d set \u5217x\u8868\u5934 = \u8981\u6539\u7684x\u5217\u6570\u636e \u6761\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">update phone_book set Number = '10086' where ID =2; --\u5c062\u53f7\u7684\u7535\u8bdd\u53f7\u7801\u6539\u621010086\nselect * from phone_book;\n +------+-------+--------+-------------+---------------------+\n | ID   | Name  | Sex    | Number      | Birth               |\n +------+-------+--------+-------------+---------------------+\n |    2 | Jenny | female | 10086       | 1998-11-12 00:00:00 |\n |    3 | Bob   | male   | 13954556889 | 1997-01-12 00:00:00 |\n +------+-------+--------+-------------+---------------------+\n 2 rows in set (0.00 sec)<\/pre>\n\n\n\n<p><strong>4.\u67e5<\/strong><\/p>\n\n\n\n<p>select * from \u8868\u540d \u6761\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select * from phone_book where Sex = 'female'; --\u627e\u51fa\u6027\u522b\u4e3a\u5973\u7684\u8bb0\u5f55\n +------+-------+--------+--------+---------------------+\n | ID   | Name  | Sex    | Number | Birth               |\n +------+-------+--------+--------+---------------------+\n |    2 | Jenny | female | 10086  | 1998-11-12 00:00:00 |\n +------+-------+--------+--------+---------------------+\n 1 row in set (0.00 sec)<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e09\u3001\u5efa\u8868\u7ea6\u675f<\/strong><\/p>\n\n\n\n<p><strong>1.\u4e3b\u952e\u7ea6\u675f<\/strong><br>\u786e\u4fdd\u4e00\u5f20\u8868\u4e2d\u67d0\u5217\u5143\u7d20\u4e0d\u91cd\u590d\u4e14\u4e0d\u4e3a\u7a7a\uff08\u63d2\u5165\u91cd\u590d\u503c\u6216\u7a7a\u503c\u4f1a\u51fa\u9519\uff09<br>\u5173\u952e\u5b57\uff1aprimary key<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5b9e\u4f8b1\uff08\u5173\u952e\u5b57\u52a0\u5728\u5217\u540d\u7684\u6570\u636e\u7c7b\u578b\u540e\u9762\uff09\uff1a\ncreate table Test1(ID int primary key, Name varchar(20));\ndescribe Test1;\n +-------+-------------+------+-----+---------+-------+\n | Field | Type        | Null | Key | Default | Extra |\n +-------+-------------+------+-----+---------+-------+\n | ID    | int(11)     | NO   | PRI | NULL    |       |\n | Name  | varchar(20) | YES  |     | NULL    |       |\n +-------+-------------+------+-----+---------+-------+\n 2 rows in set (0.00 sec)\n\u5b9e\u4f8b2\uff08\u5199\u5b8c\u5217\u548c\u6570\u636e\u7c7b\u578b\u540e\uff0c\u4ee5\u5173\u952e\u5b57\u52a0\u5706\u62ec\u53f7\u65b9\u5f0f\u8868\u660e\u7ea6\u675f\uff09\ncreate table Test2(ID int,Name varchar(20), primary key(ID));\n\uff08\u5982\u679c\u8981\u5728\u5706\u62ec\u53f7\u91cc\uff0c\u4ee5\u9017\u53f7\u9694\u5f00\u5199\u4e0aName\uff0c\u5c31\u662f\u8054\u5408\u4e3b\u952e\uff0c\u76f8\u5f53\u4e8e\u53ea\u6709\u4e00\u4e2a\u91cd\u590d\u503c\u65f6\u6ca1\u95ee\u9898\uff0c\u4f46\u4e24\u4e2a\u540c\u65f6\u91cd\u590d\u5c31\u4e0d\u80fd\u63d2\u5165\uff09<\/pre>\n\n\n\n<p><strong>2.\u81ea\u589e\u7ea6\u675f<\/strong><br>\u5f53\u8be5\u5217\u5143\u7d20\u63d2\u5165\u503c\u4e3a\u7a7a\u65f6\uff0c\u4f1a\u81ea\u589e\u3002<br>\u5173\u952e\u5b57\uff1aprimary key auto_increment<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create table Test3(ID int primary key auto_increment, Name varchar(20));\ninsert into Test3(Name) values('Jack');\ninsert into Test3(Name) values('Bob');\nselect * from Test3;\n +----+------+\n | ID | Name |\n +----+------+\n |  1 | Jack |\n |  2 | Bob  |\n +----+------+\n 2 rows in set (0.00 sec)<\/pre>\n\n\n\n<p><strong>3.\u5916\u952e\u7ea6\u675f<\/strong><br>\u5f53\u526f\u8868\u4e2d\u67d0\u5217\u7684\u5143\u7d20\u90fd\u662f\u6765\u81ea\u4e3b\u8868\u4e2d\u7684\u67d0\u5217\u65f6\uff0c\u53ef\u6dfb\u52a0\u6b64\u7ea6\u675f\uff0c\u6dfb\u52a0\u7ea6\u675f\u540e\uff0c\u8be5\u5217\u7684\u503c\u53ea\u80fd\u6765\u81ea\u4e3b\u8868\uff0c\u4e0d\u80fd\u63d2\u5165\u4e3b\u8868\u6ca1\u6709\u7684\u503c\u3002\u4ee5\u4e0b\u4ee5\u73ed\u7ea7\u8868\u548c\u5b66\u751f\u8868\u4e3a\u4f8b<br>\u5173\u952e\u5b57\uff1aforeign key<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create table Classes(ID int primary key auto_increment,Name varchar(20));\n\u63d2\u5165\u56db\u6761\u8bb0\u5f55\u540e\uff0c\u73ed\u7ea7\u8868\u5982\u4e0b\uff0c\u6709\u56db\u4e2a\u73edMagic\u3001Design\u3001PhotoShop\u3001Actor\n +----+-----------+\n | ID | Name      |\n +----+-----------+\n |  1 | Magic     |\n |  2 | Design    |\n |  3 | PhotoShop |\n |  4 | Actor     |\n +----+-----------+\n\u5b66\u751f\u8868\u5efa\u7acb\u65f6\uff0c\u73ed\u522b\u8fd9\u4e00\u5217\u4f7f\u7528\u5916\u952e\u7ea6\u675f\ncreate table Students(Stu_ID varchar(20) primary key,Name varchar(20),Class_ID int,foreign key(Class_ID) references Classes(ID));\n+----------+-------------+------+-----+---------+-------+\n | Field    | Type        | Null | Key | Default | Extra |\n +----------+-------------+------+-----+---------+-------+\n | Stu_ID   | varchar(20) | NO   | PRI | NULL    |       |\n | Name     | varchar(20) | YES  |     | NULL    |       |\n | Class_ID | int(11)     | YES  | MUL | NULL    |       |\n +----------+-------------+------+-----+---------+-------+\n\u6b64\u65f6\uff0c\u53ef\u4ee5\u63d2\u5165\ninsert into Students values('2017052432','Jim',1);\n\u4f46\u4e0d\u53ef\u4ee5\ninsert into Students values('2017052450','Jenny',10);\n\u56e0\u4e3a\u4e3b\u8868\uff08\u73ed\u7ea7\u6ca1\u670910\uff09<\/pre>\n\n\n\n<p><strong>4.\u552f\u4e00\u7ea6\u675f<\/strong><br>\u7ea6\u675f\u4fee\u9970\u7684\u5b57\u6bb5\u7684\u503c\u4e0d\u53ef\u91cd\u590d\uff08\u53ef\u4e3a\u7a7a\uff09<br>\u5173\u952e\u5b57\uff1aunique<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create table Test4(ID int, name varchar(20),unique(ID));\ndesc Test4;\n +-------+-------------+------+-----+---------+-------+\n | Field | Type        | Null | Key | Default | Extra |\n +-------+-------------+------+-----+---------+-------+\n | ID    | int(11)     | YES  | UNI | NULL    |       |\n | name  | varchar(20) | YES  |     | NULL    |       |\n +-------+-------------+------+-----+---------+-------+<\/pre>\n\n\n\n<p><strong>5.\u975e\u7a7a\u7ea6\u675f<\/strong><br>\u63d2\u5165\u503c\u4e0d\u80fd\u4e3a\u7a7a<br>\u5173\u952e\u5b57\uff1anot null<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create table Test5(ID int not null, name varchar(20));\ndesc Test5;\n +-------+-------------+------+-----+---------+-------+\n | Field | Type        | Null | Key | Default | Extra |\n +-------+-------------+------+-----+---------+-------+\n | ID    | int(11)     | NO   |     | NULL    |       |\n | name  | varchar(20) | YES  |     | NULL    |       |\n +-------+-------------+------+-----+---------+-------+<\/pre>\n\n\n\n<p><strong>6.\u9ed8\u8ba4\u7ea6\u675f<\/strong><br>\u5982\u679c\u63d2\u5165\u65f6\u6ca1\u4f20\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c<br>\u5173\u952e\u5b57\uff1adefault<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create table Test6( ID int, Name varchar(20), Age int default 20);\ninsert into Test6 values(2,'Ben',22);\ninsert into Test6(ID,Name) values(1,'Tony');\nselect * from Test6;\n +------+------+------+\n | ID   | Name | Age  |\n +------+------+------+\n |    2 | Ben  |   22 |\n |    1 | Tony |   20 |\n +------+------+------+\n 2 rows in set (0.00 sec)<\/pre>\n\n\n\n<p><strong>7.\u6dfb\u52a0\u7ea6\u675f\u3001\u5220\u9664\u7ea6\u675f<\/strong><\/p>\n\n\n\n<p>\u6dfb\u52a0\u7ea6\u675f\u4e09\u79cd\u65b9\u6cd5\uff1a<br>\u2460\u5efa\u8868\u65f6\u6dfb\u52a0<br>\u2461alter table \u8868\u540d modify \u5217\u540d \u6570\u636e\u7c7b\u578b \u7ea6\u675f<br>\u2462alter table \u8868\u540d add \u7ea6\u675f(\u5217\u540d)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u2460 \u5982\u524d\u6587\u6240\u8ff0\n\u2461 \n\u5148\u5efa\u8868\uff0c\u518d\u6dfb\u52a0\u7ea6\u675f\uff1a\ncreate table Test7(ID int,Name varchar(20));\nalter table Test7 modify ID int primary key;\n\u2462\nalter table Test7 add unique(Name);<\/pre>\n\n\n\n<p>\u5220\u9664\u7ea6\u675f\uff1a<br> alter table \u8868\u540d drop index \u5217\u540d <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">alter table Test7 drop index Name;<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u56db\u3001\u6570\u636e\u8868\u8bbe\u8ba1<\/strong><\/p>\n\n\n\n<p>\u7b2c\u4e00\u8303\u5f0f\uff1a \u5b57\u6bb5\u62c6\u5206\u5230\u4e0d\u53ef\u62c6\u5206 <\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4e09\u8303\u5f0f\uff1a\u5728\u6ee1\u8db3\u7b2c\u4e00\u8303\u5f0f\u7684\u524d\u63d0\u4e0b\uff0c\u9664\u4e3b\u952e\u5916\u7684\u6bcf\u4e00\u5217\u90fd\u5fc5\u987b\u4f9d\u8d56\u5168\u90e8\u4e3b\u952e\uff0c\u65e0\u4f20\u9012\u4f9d\u8d56\uff08\u611f\u89c9\u5c31\u662f\u4e00\u5f20\u8868\u91cc\u6bcf\u5217\u7684\u4fe1\u606f\u5e94\u8be5\u4e0d\u80fd\u63a8\u65ad\u51fa\u5176\u5b83\u5217\u7684\u4fe1\u606f\uff0c\u5426\u5219\u5c31\u62c6\u5206\u6210\u591a\u5f20\u8868\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u57fa\u672c show databases; &#8211;\u9648\u5217\u6570\u636e\u5e93 create database \u65b0\u5e93\u540d &#8211;\u521b\u5efa\u6570\u636e&hellip;<a href=\"https:\/\/www.notown.top\/?p=505\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Mysql\u57fa\u672c\u547d\u4ee4\u7528\u6cd5\uff08\u4e00\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[15],"tags":[],"class_list":["post-505","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=505"}],"version-history":[{"count":3,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/505\/revisions"}],"predecessor-version":[{"id":508,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/505\/revisions\/508"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}