{"id":200094,"date":"2025-05-06T08:56:16","date_gmt":"2025-05-06T00:56:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200094\/"},"modified":"2025-05-06T08:56:16","modified_gmt":"2025-05-06T00:56:16","slug":"mysql%e5%a4%96%e9%94%ae%e4%bb%8b%e7%bb%8d%e5%8f%8a%e5%ba%94%e7%94%a8%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200094\/","title":{"rendered":"MySQL\u5916\u952e\u4ecb\u7ecd\u53ca\u5e94\u7528\u793a\u4f8b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>MySQL\u5916\u952e\u4ecb\u7ecd\u53ca\u5e94\u7528\u793a\u4f8b<\/h1>\n<p>\u5916\u952e\u662f MySQL \u6570\u636e\u5e93\u4e2d\u7684\u4e00\u79cd\u7ea6\u675f\u673a\u5236\uff0c\u7528\u4e8e\u7ef4\u62a4\u8868\u4e0e\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u5916\u952e\u662f\u6307\u5728\u4e00\u4e2a\u8868\u4e2d\u67d0\u4e2a\u5217\u7684\u503c\u5fc5\u987b\u5728\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u76f8\u5e94\u5217\u4e2d\u5b58\u5728\u5bf9\u5e94\u503c\u3002\u901a\u8fc7\u8bbe\u7f6e\u5916\u952e\u7ea6\u675f\uff0c\u53ef\u4ee5\u9632\u6b62\u5728\u8868\u4e2d\u63d2\u5165\u6216\u66f4\u65b0\u4e0d\u7b26\u5408\u903b\u8f91\u5173\u7cfb\u7684\u6570\u636e\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p>MySQL\u4e2d\u5916\u952e\u7684\u6982\u5ff5\u53ca\u5b9e\u9645\u5e94\u7528<\/p>\n<p><strong>\u4e00\u3001\u5916\u952e\u7684\u6982\u5ff5<\/strong><\/p>\n<p>\u5728\u6570\u636e\u5e93\u8bbe\u8ba1\u4e2d\uff0c\u5916\u952e\u662f\u7528\u6765\u63cf\u8ff0\u8868\u4e0e\u8868\u4e4b\u95f4\u5173\u7cfb\u7684\u4e00\u79cd\u91cd\u8981\u7ea6\u675f\u3002\u5916\u952e\u7528\u6765\u786e\u4fdd\u5728\u4e00\u4e2a\u8868\u4e2d\u7684\u67d0\u4e9b\u5217\u7684\u503c\u5fc5\u987b\u5728\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u76f8\u5e94\u5217\u4e2d\u6709\u5bf9\u5e94\u7684\u503c\u3002\u5916\u952e\u7684\u5b58\u5728\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\uff0c\u907f\u514d\u4e86\u4e0d\u7b26\u5408\u903b\u8f91\u5173\u7cfb\u7684\u6570\u636e\u63d2\u5165\u6216\u66f4\u65b0\u3002<\/p>\n<p><strong>\u4e8c\u3001\u5916\u952e\u7684\u5b9e\u9645\u5e94\u7528<\/strong><\/p>\n<p>\u5728\u5b9e\u9645\u7684\u6570\u636e\u5e93\u8bbe\u8ba1\u4e2d\uff0c\u5916\u952e\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u3002\u4e0b\u9762\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660eMySQL\u4e2d\u5916\u952e\u7684\u4f7f\u7528\u3002<\/p>\n<p><strong>1. \u521b\u5efa\u4e24\u4e2a\u76f8\u5173\u8868<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e24\u4e2a\u76f8\u5173\u7684\u8868\uff0c\u4e00\u4e2a\u662f\u8ba2\u5355\u8868\uff08orders\uff09\uff0c\u53e6\u4e00\u4e2a\u662f\u5ba2\u6237\u8868\uff08customers\uff09\u3002\u8ba2\u5355\u8868\u4e2d\u5c06\u5305\u542b\u4e00\u4e2a\u6307\u5411\u5ba2\u6237\u8868\u7684\u5916\u952e\uff0c\u4ee5\u5efa\u7acb\u8ba2\u5355\u4e0e\u5ba2\u6237\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<pre>CREATE TABLE customers (\n    customer_id INT PRIMARY KEY,\n    customer_name VARCHAR(50)\n);\n\nCREATE TABLE orders (\n    order_id INT PRIMARY KEY,\n    order_date DATE,\n    customer_id INT,\n    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)\n);<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u8868\uff0c\u5206\u522b\u662fcustomers\u8868\u548corders\u8868\u3002orders\u8868\u4e2d\u7684customer_id\u5217\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5916\u952e\uff0c\u6307\u5411customers\u8868\u7684customer_id\u5217\u3002<\/p>\n<p><strong>2. \u63d2\u5165\u6570\u636e<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5411customers\u8868\u548corders\u8868\u4e2d\u63d2\u5165\u4e00\u4e9b\u6570\u636e\u3002\u9996\u5148\u63d2\u5165customers\u8868\u7684\u6570\u636e:<\/p>\n<pre>INSERT INTO customers (customer_id, customer_name) VALUES (1, 'Alice');\nINSERT INTO customers (customer_id, customer_name) VALUES (2, 'Bob');<\/pre>\n<p>\u7136\u540e\u63d2\u5165orders\u8868\u7684\u6570\u636e\uff0c\u6ce8\u610f\u8981\u4fdd\u8bc1\u63d2\u5165\u7684customer_id\u5728customers\u8868\u4e2d\u5b58\u5728\uff1a<\/p>\n<pre>INSERT INTO orders (order_id, order_date, customer_id) VALUES (1, '2022-01-01', 1);\nINSERT INTO orders (order_id, order_date, customer_id) VALUES (2, '2022-01-02', 2);<\/pre>\n<p><strong>3. \u6d4b\u8bd5\u5916\u952e\u7ea6\u675f<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u6f14\u793a\u4e00\u4e0b\u5916\u952e\u7684\u7ea6\u675f\u4f5c\u7528\uff0c\u5728\u5c1d\u8bd5\u63d2\u5165\u4e00\u4e2a\u65e0\u6548\u7684customer_id\u65f6\uff0c\u4f1a\u53d7\u5230\u5916\u952e\u7ea6\u675f\u7684\u9650\u5236\uff1a<\/p>\n<pre>INSERT INTO orders (order_id, order_date, customer_id) VALUES (3, '2022-01-05', 3);<\/pre>\n<p>\u6b64\u65f6\u4f1a\u63d0\u793a\u9519\u8bef\uff0c\u56e0\u4e3a\u6ca1\u6709customer_id\u4e3a3\u7684\u8bb0\u5f55\u5b58\u5728\u4e8ecustomers\u8868\u4e2d\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u63d2\u5165\u4e0d\u5408\u9002\u7684\u8ba2\u5355\u6570\u636e\u3002<\/p>\n<p><strong>4. \u5916\u952e\u7684\u64cd\u4f5c\u89c4\u5219<\/strong><\/p>\n<p>\u5728MySQL\u4e2d\uff0c\u5916\u952e\u7ea6\u675f\u6709\u51e0\u79cd\u64cd\u4f5c\u89c4\u5219\uff0c\u4e00\u822c\u5305\u62ecCASCADE\u3001SET NULL\u3001RESTRICT\u7b49\u3002\u4ee5CASCADE\u4e3a\u4f8b\uff0c\u5f53\u4ececustomers\u8868\u4e2d\u5220\u9664\u4e00\u4e2a\u5ba2\u6237\u65f6\uff0c\u5bf9\u5e94\u7684\u8ba2\u5355\u6570\u636e\u5c06\u88ab\u81ea\u52a8\u5220\u9664\uff0c\u907f\u514d\u4e86\u6570\u636e\u7684\u5b64\u7acb\u60c5\u51b5\u3002<\/p>\n<pre>CREATE TABLE orders (\n    order_id INT PRIMARY KEY,\n    order_date DATE,\n    customer_id INT,\n    FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE\n);<\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5916\u952e\u7684\u91cd\u8981\u6027\u53ca\u5b9e\u9645\u5e94\u7528\u3002\u5728\u6570\u636e\u5e93\u8bbe\u8ba1\u4e2d\u5408\u7406\u5730\u8fd0\u7528\u5916\u952e\u7ea6\u675f\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\uff0c\u907f\u514d\u6570\u636e\u63d2\u5165\u66f4\u65b0\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\u3002MySQL\u4e2d\u5916\u952e\u7684\u6982\u5ff5\u4e0d\u4ec5\u9650\u4e8e\u4e0a\u8ff0\u793a\u4f8b\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u8fd8\u6709\u66f4\u591a\u590d\u6742\u7684\u573a\u666f\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u6765\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aMySQL\u5916\u952e\u4ecb\u7ecd\u53ca\u5e94\u7528\u793a\u4f8b\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u5916\u952e\u4ecb\u7ecd\u53ca\u5e94\u7528\u793a\u4f8b \u5916&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200094","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200094"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200094\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}