{"id":5898,"date":"2024-11-14T12:52:12","date_gmt":"2024-11-14T04:52:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5898\/"},"modified":"2024-11-14T12:52:12","modified_gmt":"2024-11-14T04:52:12","slug":"sql%e5%80%99%e9%80%89%e9%94%ae%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5898\/","title":{"rendered":"sql\u5019\u9009\u952e\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u8bbe\u7f6e\u5019\u9009\u952e\u7684\u65b9\u6cd5\uff1a\u786e\u5b9a\u552f\u4e00\u6807\u8bc6\u5217\uff1b\u4f7f\u7528 primary key \u7ea6\u675f\u521b\u5efa\u4e3b\u952e\uff1b\u4f7f\u7528 unique \u7ea6\u675f\u6dfb\u52a0\u552f\u4e00\u7ea6\u675f\uff1b\u521b\u5efa\u552f\u4e00\u7d22\u5f15\u3002\u8bbe\u7f6e\u5019\u9009\u952e\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3001\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u548c\u9632\u6b62\u6570\u636e\u91cd\u590d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321572260634.jpg\" class=\"aligncenter\" title=\"sql\u5019\u9009\u952e\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"sql\u5019\u9009\u952e\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5019\u9009\u952e\u8bbe\u7f6e<\/strong><\/p>\n<p>\u5019\u9009\u952e\u662f\u5728\u8868\u4e2d\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u6216\u5217\u7ec4\u5408\u3002\u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u5019\u9009\u952e\uff1a<\/p>\n<p><strong>1. \u786e\u5b9a\u552f\u4e00\u6807\u8bc6\u5217<\/strong><\/p>\n<ul>\n<li>\u5206\u6790\u8868\u4e2d\u7684\u6570\u636e\uff0c\u627e\u51fa\u53ef\u4ee5\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u6216\u5217\u7ec4\u5408\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 CREATE TABLE \u8bed\u53e5\u521b\u5efa\u4e3b\u952e\u7ea6\u675f<\/strong><\/p>\n<ul>\n<li>\u5728 CREATE TABLE \u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528 PRIMARY KEY \u7ea6\u675f\u6307\u5b9a\u4e3b\u952e\u5217\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>CREATE TABLE customers (\n  customer_id INT PRIMARY KEY,\n  name VARCHAR(255) NOT NULL\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u552f\u4e00\u7ea6\u675f<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u8868\u5df2\u7ecf\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u552f\u4e00\u7ea6\u675f\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>ALTER TABLE orders\nADD CONSTRAINT UK_order_number UNIQUE (order_number);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4f7f\u7528 CREATE UNIQUE INDEX \u8bed\u53e5\u521b\u5efa\u552f\u4e00\u7d22\u5f15<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u552f\u4e00\u7d22\u5f15\u4e5f\u53ef\u4ee5\u5f3a\u5236\u5b9e\u65bd\u5019\u9009\u952e\u7ea6\u675f\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>CREATE UNIQUE INDEX idx_customer_name ON customers (name);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u8868\u4e2d\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002<\/li>\n<li>\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\uff0c\u901a\u8fc7\u5019\u9009\u952e\u5feb\u901f\u67e5\u627e\u6570\u636e\u3002<\/li>\n<li>\u9632\u6b62\u6570\u636e\u91cd\u590d\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e00\u4e2a\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u5019\u9009\u952e\u3002<\/li>\n<li>\u5019\u9009\u952e\u4e0d\u4e00\u5b9a\u662f\u552f\u4e00\u952e\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u540d\u79f0\u76f8\u540c\u4f46\u5176\u4ed6\u5c5e\u6027\u4e0d\u540c\u7684\u5ba2\u6237\u3002<\/li>\n<li>\u5019\u9009\u952e\u7684\u5217\u4e0d\u80fd\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5019\u9009\u952e\u600e\u4e48\u8bbe\u7f6e\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>sql \u4e2d\u8bbe\u7f6e\u5019\u9009\u952e\u7684\u65b9\u6cd5\uff1a\u786e\u5b9a\u552f\u4e00\u6807\u8bc6\u5217\uff1b\u4f7f\u7528 primary key \u7ea6\u675f\u521b\u5efa\u4e3b\u952e\uff1b\u4f7f\u7528 unique \u7ea6\u675f\u6dfb\u52a0\u552f\u4e00\u7ea6\u675f\uff1b\u521b\u5efa\u552f\u4e00\u7d22\u5f15\u3002\u8bbe\u7f6e\u5019\u9009\u952e\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3001\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u548c\u9632\u6b62\u6570\u636e\u91cd\u590d\u3002 SQL \u5019\u9009\u952e\u8bbe\u7f6e \u5019\u9009\u952e\u662f\u5728\u8868\u4e2d\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u6216\u5217\u7ec4\u5408\u3002\u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u5019\u9009\u952e\uff1a 1. \u786e\u5b9a\u552f\u4e00\u6807\u8bc6\u5217 \u5206\u6790\u8868\u4e2d\u7684\u6570\u636e\uff0c\u627e\u51fa\u53ef\u4ee5\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u6216\u5217\u7ec4\u5408\u3002 2. \u4f7f\u7528 CREATE TABLE \u8bed\u53e5\u521b\u5efa\u4e3b\u952e\u7ea6\u675f \u5728 CREATE TABLE \u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528 PRIMARY KEY \u7ea6\u675f\u6307\u5b9a\u4e3b\u952e\u5217\u3002\u4f8b\u5982\uff1a CREATE TABLE customers ( customer_id INT PRIMARY KEY, name VARCHAR(255) NOT NULL ); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u552f\u4e00\u7ea6\u675f \u5982\u679c\u8868\u5df2\u7ecf\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6dfb\u52a0\u552f\u4e00\u7ea6\u675f\u3002\u4f8b\u5982\uff1a ALTER TABLE orders ADD CONSTRAINT [&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-5898","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5898","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=5898"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5898\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}