{"id":5615,"date":"2024-11-14T09:54:23","date_gmt":"2024-11-14T01:54:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5615\/"},"modified":"2024-11-14T09:54:23","modified_gmt":"2024-11-14T01:54:23","slug":"sql%e6%80%8e%e4%b9%88%e8%ae%be%e4%b8%bb%e9%94%ae%e5%a4%96%e9%94%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5615\/","title":{"rendered":"sql\u600e\u4e48\u8bbe\u4e3b\u952e\u5916\u952e"},"content":{"rendered":"<blockquote><p>\n  \u4e3b\u952e\u7528\u4e8e\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u6bcf\u4e00\u884c\uff0c\u4e0d\u5141\u8bb8\u91cd\u590d\u6216\u4e3a null\uff0c\u800c\u5916\u952e\u5219\u7528\u4e8e\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6307\u5411\u53e6\u4e00\u8868\u7684\u4e3b\u952e\uff0c\u5e76\u5728\u4e3b\u952e\u884c\u88ab\u5220\u9664\u65f6\u6839\u636e\u6307\u5b9a\u7684\u9009\u9879\u66f4\u65b0\u6216\u5220\u9664\u5305\u542b\u5916\u952e\u7684\u884c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019033511740.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u8bbe\u4e3b\u952e\u5916\u952e\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u8bbe\u4e3b\u952e\u5916\u952e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 SQL \u8bbe\u7f6e\u4e3b\u952e\u548c\u5916\u952e<\/strong><\/p>\n<p><strong>\u4e3b\u952e<\/strong><\/p>\n<ul>\n<li>\u4e3b\u952e\u662f\u8868\u4e2d\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u3002<\/li>\n<li>\u4e3b\u952e\u7684\u503c\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5e76\u4e14\u4e0d\u80fd\u4e3a null\u3002<\/li>\n<li>\u6bcf\u4e2a\u8868\u53ea\u80fd\u6709\u4e00\u4e2a\u4e3b\u952e\u3002<\/li>\n<\/ul>\n<p><strong>\u8bbe\u7f6e\u4e3b\u952e<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 PRIMARY KEY \u7ea6\u675f\u6765\u8bbe\u7f6e\u4e3b\u952e\uff1a<\/p>\n<pre>ALTER TABLE table_name ADD PRIMARY KEY (column_name);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5728 customers \u8868\u4e2d\u8bbe\u7f6e customer_id \u5217\u4e3a\u4e3b\u952e\uff1a<\/p>\n<pre>ALTER TABLE customers ADD PRIMARY KEY (customer_id);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5916\u952e<\/strong><\/p>\n<ul>\n<li>\u5916\u952e\u662f\u8868\u4e2d\u5f15\u7528\u53e6\u4e00\u8868\u4e3b\u952e\u7684\u5217\u3002<\/li>\n<li>\u5916\u952e\u7684\u503c\u5fc5\u987b\u4e0e\u88ab\u5f15\u7528\u8868\u7684\u76f8\u5e94\u4e3b\u952e\u503c\u5339\u914d\u3002<\/li>\n<li>\u5916\u952e\u53ef\u4ee5\u7528\u4e8e\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<p><strong>\u8bbe\u7f6e\u5916\u952e<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u6765\u8bbe\u7f6e\u5916\u952e\uff1a<\/p>\n<pre>ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES other_table_name (other_column_name);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5728 orders \u8868\u4e2d\u8bbe\u7f6e customer_id \u5217\u4e3a\u5916\u952e\uff0c\u5f15\u7528 customers \u8868\u7684 customer_id \u4e3b\u952e\uff1a<\/p>\n<pre>ALTER TABLE orders ADD FOREIGN KEY (customer_id) REFERENCES customers (customer_id);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5916\u952e\u7ea6\u675f\u9009\u9879<\/strong><\/p>\n<ul>\n<li> <strong>ON DELETE CASCADE\uff1a<\/strong>\u5f53\u88ab\u5f15\u7528\u8868\u4e2d\u7684\u4e3b\u952e\u884c\u88ab\u5220\u9664\u65f6\uff0c\u4e5f\u4f1a\u5220\u9664\u5305\u542b\u5916\u952e\u884c\u7684\u6240\u6709\u884c\u3002<\/li>\n<li> <strong>ON DELETE SET NULL\uff1a<\/strong>\u5f53\u88ab\u5f15\u7528\u8868\u4e2d\u7684\u4e3b\u952e\u884c\u88ab\u5220\u9664\u65f6\uff0c\u5305\u542b\u5916\u952e\u7684\u884c\u7684\u5916\u952e\u503c\u5c06\u88ab\u8bbe\u7f6e\u4e3a null\u3002<\/li>\n<li> <strong>ON DELETE RESTRICT\uff1a<\/strong>\u5982\u679c\u5916\u952e\u503c\u4e0e\u88ab\u5f15\u7528\u8868\u4e2d\u5df2\u5b58\u5728\u7684\u884c\u4e0d\u5339\u914d\uff0c\u5219\u65e0\u6cd5\u5220\u9664\u88ab\u5f15\u7528\u8868\u4e2d\u7684\u4e3b\u952e\u884c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u8bbe\u4e3b\u952e\u5916\u952e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u952e\u7528\u4e8e\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u6bcf\u4e00\u884c\uff0c\u4e0d\u5141\u8bb8\u91cd\u590d\u6216\u4e3a null\uff0c\u800c\u5916\u952e\u5219\u7528\u4e8e\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6307\u5411\u53e6\u4e00\u8868\u7684\u4e3b\u952e\uff0c\u5e76\u5728\u4e3b\u952e\u884c\u88ab\u5220\u9664\u65f6\u6839\u636e\u6307\u5b9a\u7684\u9009\u9879\u66f4\u65b0\u6216\u5220\u9664\u5305\u542b\u5916\u952e\u7684\u884c\u3002 \u5982\u4f55\u4f7f\u7528 SQL \u8bbe\u7f6e\u4e3b\u952e\u548c\u5916\u952e \u4e3b\u952e \u4e3b\u952e\u662f\u8868\u4e2d\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u3002 \u4e3b\u952e\u7684\u503c\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5e76\u4e14\u4e0d\u80fd\u4e3a null\u3002 \u6bcf\u4e2a\u8868\u53ea\u80fd\u6709\u4e00\u4e2a\u4e3b\u952e\u3002 \u8bbe\u7f6e\u4e3b\u952e \u53ef\u4ee5\u4f7f\u7528 PRIMARY KEY \u7ea6\u675f\u6765\u8bbe\u7f6e\u4e3b\u952e\uff1a ALTER TABLE table_name ADD PRIMARY KEY (column_name); \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u5728 customers \u8868\u4e2d\u8bbe\u7f6e customer_id \u5217\u4e3a\u4e3b\u952e\uff1a ALTER TABLE customers ADD PRIMARY KEY (customer_id); \u767b\u5f55\u540e\u590d\u5236 \u5916\u952e \u5916\u952e\u662f\u8868\u4e2d\u5f15\u7528\u53e6\u4e00\u8868\u4e3b\u952e\u7684\u5217\u3002 \u5916\u952e\u7684\u503c\u5fc5\u987b\u4e0e\u88ab\u5f15\u7528\u8868\u7684\u76f8\u5e94\u4e3b\u952e\u503c\u5339\u914d\u3002 \u5916\u952e\u53ef\u4ee5\u7528\u4e8e\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 \u8bbe\u7f6e\u5916\u952e \u53ef\u4ee5\u4f7f\u7528 FOREIGN KEY \u7ea6\u675f\u6765\u8bbe\u7f6e\u5916\u952e\uff1a ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES other_table_name (other_column_name); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5615","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5615"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5615\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}