{"id":141467,"date":"2024-10-31T21:47:05","date_gmt":"2024-10-31T13:47:05","guid":{"rendered":"https:\/\/server.hk\/cnblog\/141467\/"},"modified":"2024-10-31T21:47:06","modified_gmt":"2024-10-31T13:47:06","slug":"sql-server-%e4%b8%ad%e5%88%aa%e9%99%a4%e4%b8%bb%e9%8d%b5%e5%92%8c%e9%87%8d%e5%bb%ba%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/141467\/","title":{"rendered":"SQL Server \u4e2d\u522a\u9664\u4e3b\u9375\u548c\u91cd\u5efa\u7684\u65b9\u6cd5"},"content":{"rendered":"<h1 id=\"sql-server-%e4%b8%ad%e5%88%aa%e9%99%a4%e4%b8%bb%e9%8d%b5%e5%92%8c%e9%87%8d%e5%bb%ba%e7%9a%84%e6%96%b9%e6%b3%95-kneuIaygVH\">SQL Server \u4e2d\u522a\u9664\u4e3b\u9375\u548c\u91cd\u5efa\u7684\u65b9\u6cd5<\/h1>\n<p>\u5728 SQL Server \u4e2d\uff0c\u4e3b\u9375\u662f\u7528\u4f86\u552f\u4e00\u6a19\u8b58\u8868\u4e2d\u6bcf\u4e00\u884c\u6578\u64da\u7684\u91cd\u8981\u7d04\u675f\u3002\u7576\u9700\u8981\u5c0d\u8868\u7d50\u69cb\u9032\u884c\u8abf\u6574\u6642\uff0c\u53ef\u80fd\u6703\u9700\u8981\u522a\u9664\u73fe\u6709\u7684\u4e3b\u9375\u4e26\u91cd\u65b0\u5efa\u7acb\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728 SQL Server \u4e2d\u522a\u9664\u4e3b\u9375\u548c\u91cd\u5efa\u4e3b\u9375\u7684\u6b65\u9a5f\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u7bc4\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e5%88%aa%e9%99%a4%e4%b8%bb%e9%8d%b5-kneuIaygVH\">\u522a\u9664\u4e3b\u9375<\/h2>\n<p>\u8981\u522a\u9664 SQL Server \u4e2d\u7684\u4e3b\u9375\uff0c\u9996\u5148\u9700\u8981\u78ba\u5b9a\u4e3b\u9375\u7684\u540d\u7a31\u3002\u901a\u5e38\uff0c\u4e3b\u9375\u7684\u540d\u7a31\u662f\u5728\u5275\u5efa\u8868\u6642\u6307\u5b9a\u7684\u3002\u5982\u679c\u672a\u6307\u5b9a\uff0cSQL Server \u6703\u81ea\u52d5\u751f\u6210\u4e00\u500b\u540d\u7a31\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u67e5\u627e\u8868\u4e2d\u4e3b\u9375\u7684\u540d\u7a31\uff1a<\/p>\n<pre><code>SELECT \n    kcu.TABLE_NAME, \n    kc.CONSTRAINT_NAME \nFROM \n    INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS kc \nJOIN \n    INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu \nON \n    kc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME \nWHERE \n    kc.CONSTRAINT_TYPE = 'PRIMARY KEY' \n    AND kcu.TABLE_NAME = '\u4f60\u7684\u8868\u540d';<\/code><\/pre>\n<p>\u4e00\u65e6\u78ba\u5b9a\u4e86\u4e3b\u9375\u7684\u540d\u7a31\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u8a9e\u53e5\u4f86\u522a\u9664\u4e3b\u9375\uff1a<\/p>\n<pre><code>ALTER TABLE \u4f60\u7684\u8868\u540d \nDROP CONSTRAINT \u4e3b\u9375\u540d\u7a31;<\/code><\/pre>\n<h2 id=\"%e9%87%8d%e5%bb%ba%e4%b8%bb%e9%8d%b5-kneuIaygVH\">\u91cd\u5efa\u4e3b\u9375<\/h2>\n<p>\u522a\u9664\u4e3b\u9375\u5f8c\uff0c\u82e5\u9700\u8981\u91cd\u65b0\u5efa\u7acb\u4e3b\u9375\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u8a9e\u53e5\u3002\u9996\u5148\uff0c\u78ba\u4fdd\u9078\u64c7\u7684\u5217\u662f\u552f\u4e00\u7684\uff0c\u4e26\u4e14\u4e0d\u5305\u542b NULL \u503c\uff1a<\/p>\n<pre><code>ALTER TABLE \u4f60\u7684\u8868\u540d \nADD CONSTRAINT \u65b0\u4e3b\u9375\u540d\u7a31 PRIMARY KEY (\u5217\u540d1, \u5217\u540d2);<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c`\u5217\u540d1` \u548c `\u5217\u540d2` \u662f\u4f60\u5e0c\u671b\u4f5c\u70ba\u4e3b\u9375\u7684\u5217\u3002\u53ef\u4ee5\u6839\u64da\u9700\u8981\u9078\u64c7\u4e00\u500b\u6216\u591a\u500b\u5217\u4f5c\u70ba\u4e3b\u9375\u3002<\/p>\n<h2 id=\"%e7%af%84%e4%be%8b-kneuIaygVH\">\u7bc4\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba `Employees` \u7684\u8868\uff0c\u4e26\u4e14\u5b83\u7684\u4e3b\u9375\u662f `EmployeeID`\u3002\u5982\u679c\u6211\u5011\u60f3\u8981\u522a\u9664\u9019\u500b\u4e3b\u9375\u4e26\u91cd\u65b0\u5efa\u7acb\u5b83\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\uff1a<\/p>\n<pre><code>-- \u67e5\u627e\u4e3b\u9375\u540d\u7a31\nSELECT \n    kcu.TABLE_NAME, \n    kc.CONSTRAINT_NAME \nFROM \n    INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS kc \nJOIN \n    INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu \nON \n    kc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME \nWHERE \n    kc.CONSTRAINT_TYPE = 'PRIMARY KEY' \n    AND kcu.TABLE_NAME = 'Employees';\n\n-- \u522a\u9664\u4e3b\u9375\nALTER TABLE Employees \nDROP CONSTRAINT PK_Employees;  -- \u5047\u8a2d\u4e3b\u9375\u540d\u7a31\u70ba PK_Employees\n\n-- \u91cd\u5efa\u4e3b\u9375\nALTER TABLE Employees \nADD CONSTRAINT PK_Employees PRIMARY KEY (EmployeeID);<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-kneuIaygVH\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u5728\u522a\u9664\u4e3b\u9375\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u6c92\u6709\u5176\u4ed6\u4f9d\u8cf4\u65bc\u8a72\u4e3b\u9375\u7684\u5916\u9375\u7d04\u675f\u3002<\/li>\n<li>\u91cd\u5efa\u4e3b\u9375\u6642\uff0c\u9078\u64c7\u7684\u5217\u5fc5\u9808\u662f\u552f\u4e00\u7684\uff0c\u4e26\u4e14\u4e0d\u61c9\u5305\u542b NULL \u503c\u3002<\/li>\n<li>\u5728\u9032\u884c\u7d50\u69cb\u8b8a\u66f4\u6642\uff0c\u5efa\u8b70\u5148\u5099\u4efd\u6578\u64da\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-kneuIaygVH\">\u7e3d\u7d50<\/h2>\n<p>\u5728 SQL Server \u4e2d\uff0c\u522a\u9664\u548c\u91cd\u5efa\u4e3b\u9375\u662f\u4e00\u500b\u76f8\u5c0d\u7c21\u55ae\u7684\u904e\u7a0b\uff0c\u4f46\u9700\u8981\u8b39\u614e\u64cd\u4f5c\u4ee5\u907f\u514d\u6578\u64da\u5b8c\u6574\u6027\u554f\u984c\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u8868\u7684\u4e3b\u9375\u7d04\u675f\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5728 SQL Server \u4e2d\u522a\u9664\u4e3b\u9375\u548c\u91cd\u5efa\u4e3b\u9375\u7684\u6b65\u9a5f\u8207\u65b9\u6cd5\uff0c\u78ba\u4fdd\u6578\u64da\u5b8c\u6574\u6027\u8207\u6027\u80fd\u512a\u5316\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-141467","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/141467","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=141467"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/141467\/revisions"}],"predecessor-version":[{"id":141468,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/141467\/revisions\/141468"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=141467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=141467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=141467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}