{"id":5617,"date":"2024-11-14T09:14:32","date_gmt":"2024-11-14T01:14:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5617\/"},"modified":"2024-11-14T09:14:32","modified_gmt":"2024-11-14T01:14:32","slug":"sql%e5%a4%96%e9%94%ae%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5617\/","title":{"rendered":"sql\u5916\u952e\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5916\u952e\u662f\u4e00\u79cd\u7ea6\u675f\uff0c\u5c06\u4e00\u4e2a\u8868\u7684\u5217\u94fe\u63a5\u5230\u53e6\u4e00\u4e2a\u8868\u7684\u200b\u200b\u4e3b\u952e\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u548c\u5f15\u7528\u5b8c\u6574\u6027\u3002\u521b\u5efa\u5916\u952e\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u4f7f\u7528 alter table \u8bed\u53e5\u6216 create table \u8bed\u53e5\u3002\u5916\u952e\u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u9644\u52a0\u9009\u9879\uff0c\u4f8b\u5982 on delete cascade \u548c on update cascade\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u914d\u7f6e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019061646836.jpg\" class=\"aligncenter\" title=\"sql\u5916\u952e\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"sql\u5916\u952e\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5916\u952e\u306e\u4f5c\u6210\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u5916\u952e\u3068\u306f<\/strong><\/p>\n<p>\u5916\u952e\u306f\u3001\u3042\u308b\u30c6\u30fc\u30d6\u30eb\u306e\u5217\u306b\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u4e3b\u30ad\u30fc\u3092\u53c2\u7167\u3059\u308b\u5236\u7d04\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3068\u53c2\u7167\u6574\u5408\u6027\u304c\u78ba\u4fdd\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><strong>\u5916\u952e\u306e\u4f5c\u6210\u624b\u9806<\/strong><\/p>\n<p>\u5916\u952e\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u624b\u9806\u306b\u5f93\u3044\u307e\u3059\u3002<\/p>\n<ol>\n<li>\n<p><strong>ALTER TABLE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b<\/strong><\/p>\n<pre>ALTER TABLE &lt;\u30c6\u30fc\u30d6\u30eb\u540d&gt; ADD FOREIGN KEY (&lt;\u5217\u540d&gt;) REFERENCES &lt;\u53c2\u7167\u30c6\u30fc\u30d6\u30eb\u540d&gt;(&lt;\u4e3b\u30ad\u30fc\u540d&gt;);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>CREATE TABLE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b<\/strong><\/p>\n<pre>CREATE TABLE &lt;\u30c6\u30fc\u30d6\u30eb\u540d&gt; (\n  &lt;\u5217\u540d&gt; &lt;\u30c7\u30fc\u30bf\u578b&gt;,\n  FOREIGN KEY (&lt;\u5217\u540d&gt;) REFERENCES &lt;\u53c2\u7167\u30c6\u30fc\u30d6\u30eb\u540d&gt;(&lt;\u4e3b\u30ad\u30fc\u540d&gt;)\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ol>\n<p><strong>\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u306f\u3001orders \u30c6\u30fc\u30d6\u30eb\u306e product_id \u5217\u306b products \u30c6\u30fc\u30d6\u30eb\u306e id \u5217\u3092\u53c2\u7167\u3059\u308b\u5916\u952e\u3092\u4f5c\u6210\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n<pre>ALTER TABLE orders ADD FOREIGN KEY (product_id) REFERENCES products(id);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8ffd\u52a0\u30aa\u30d7\u30b7\u30e7\u30f3<\/strong><\/p>\n<p>\u5916\u952e\u3092\u4f5c\u6210\u3059\u308b\u969b\u306b\u306f\u3001\u8ffd\u52a0\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li> <strong>ON DELETE CASCADE:<\/strong> \u53c2\u7167\u3055\u308c\u308b\u30ec\u30b3\u30fc\u30c9\u304c\u524a\u9664\u3055\u308c\u308b\u3068\u3001\u53c2\u7167\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u3082\u81ea\u52d5\u7684\u306b\u524a\u9664\u3055\u308c\u307e\u3059\u3002<\/li>\n<li> <strong>ON DELETE SET NULL:<\/strong> \u53c2\u7167\u3055\u308c\u308b\u30ec\u30b3\u30fc\u30c9\u304c\u524a\u9664\u3055\u308c\u308b\u3068\u3001\u53c2\u7167\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u5024\u306f NULL \u306b\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u3002<\/li>\n<li> <strong>ON UPDATE CASCADE:<\/strong> \u53c2\u7167\u3055\u308c\u308b\u30ec\u30b3\u30fc\u30c9\u304c\u66f4\u65b0\u3055\u308c\u308b\u3068\u3001\u53c2\u7167\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u3082\u81ea\u52d5\u7684\u306b\u66f4\u65b0\u3055\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\n<p><strong>\u30d2\u30f3\u30c8<\/strong><\/p>\n<ul>\n<li>\u5916\u952e\u3092\u4f5c\u6210\u3059\u308b\u524d\u306b\u3001\u53c2\u7167\u6574\u5408\u6027\u304c\u3042\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n<li>\u5916\u952e\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u95a2\u9023\u3059\u308b\u30c7\u30fc\u30bf\u9593\u306e\u6574\u5408\u6027\u304c\u7dad\u6301\u3055\u308c\u307e\u3059\u3002<\/li>\n<li>\u5916\u952e\u306f\u6697\u9ed9\u7684\u306a\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f5c\u6210\u3057\u3001\u53c2\u7167\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u3078\u306e\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5916\u952e\u600e\u4e48\u5199\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>\u5916\u952e\u662f\u4e00\u79cd\u7ea6\u675f\uff0c\u5c06\u4e00\u4e2a\u8868\u7684\u5217\u94fe\u63a5\u5230\u53e6\u4e00\u4e2a\u8868\u7684\u200b\u200b\u4e3b\u952e\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u548c\u5f15\u7528\u5b8c\u6574\u6027\u3002\u521b\u5efa\u5916\u952e\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u4f7f\u7528 alter table \u8bed\u53e5\u6216 create table \u8bed\u53e5\u3002\u5916\u952e\u63d0\u4f9b\u4e86\u53ef\u9009\u7684\u9644\u52a0\u9009\u9879\uff0c\u4f8b\u5982 on delete cascade \u548c on update cascade\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u914d\u7f6e\u3002 SQL \u5916\u952e\u306e\u4f5c\u6210\u65b9\u6cd5 \u5916\u952e\u3068\u306f \u5916\u952e\u306f\u3001\u3042\u308b\u30c6\u30fc\u30d6\u30eb\u306e\u5217\u306b\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u4e3b\u30ad\u30fc\u3092\u53c2\u7167\u3059\u308b\u5236\u7d04\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3068\u53c2\u7167\u6574\u5408\u6027\u304c\u78ba\u4fdd\u3055\u308c\u307e\u3059\u3002 \u5916\u952e\u306e\u4f5c\u6210\u624b\u9806 \u5916\u952e\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u624b\u9806\u306b\u5f93\u3044\u307e\u3059\u3002 ALTER TABLE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b ALTER TABLE &lt;\u30c6\u30fc\u30d6\u30eb\u540d&gt; ADD FOREIGN KEY (&lt;\u5217\u540d&gt;) REFERENCES &lt;\u53c2\u7167\u30c6\u30fc\u30d6\u30eb\u540d&gt;(&lt;\u4e3b\u30ad\u30fc\u540d&gt;); \u767b\u5f55\u540e\u590d\u5236 CREATE TABLE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b CREATE TABLE &lt;\u30c6\u30fc\u30d6\u30eb\u540d&gt; ( &lt;\u5217\u540d&gt; &lt;\u30c7\u30fc\u30bf\u578b&gt;, FOREIGN KEY (&lt;\u5217\u540d&gt;) REFERENCES &lt;\u53c2\u7167\u30c6\u30fc\u30d6\u30eb\u540d&gt;(&lt;\u4e3b\u30ad\u30fc\u540d&gt;) ); \u767b\u5f55\u540e\u590d\u5236 \u4f8b \u4ee5\u4e0b\u306f\u3001orders \u30c6\u30fc\u30d6\u30eb\u306e product_id \u5217\u306b products [&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-5617","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5617","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=5617"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5617\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}