{"id":6628,"date":"2024-11-14T12:23:44","date_gmt":"2024-11-14T04:23:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6628\/"},"modified":"2024-11-14T12:23:44","modified_gmt":"2024-11-14T04:23:44","slug":"sql-server%e4%b8%ad%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%8a%a8%e7%bc%96%e5%8f%b7%e7%9a%84%e4%b8%89%e7%a7%8d%e5%8a%9e%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6628\/","title":{"rendered":"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u7684\u4e09\u79cd\u529e\u6cd5"},"content":{"rendered":"<blockquote><p>\n  sql server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u6709 3 \u79cd\u65b9\u6cd5\uff1a1. identity \u5217\uff08\u552f\u4e00\u9012\u589e\u503c\uff09\uff1b2. sequence\uff08\u8de8\u8868\u4f7f\u7528\u503c\u5e8f\u5217\uff09\uff1b3. newsequentialid() \u51fd\u6570\uff08\u65e0\u5e8f\u552f\u4e00\u503c\uff09\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u9700\u6c42\uff0c\u5982\u9012\u589e\u6807\u8bc6\u7b26\u4f7f\u7528 identity \u5217\uff0c\u8de8\u8868\u4f7f\u7528\u503c\u4f7f\u7528 sequence\uff0c\u4e0d\u53ef\u9884\u6d4b\u503c\u4f7f\u7528 newsequentialid() \u51fd\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/19\/2024081916462846187.jpg\" class=\"aligncenter\" title=\"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u7684\u4e09\u79cd\u529e\u6cd5\u63d2\u56fe\" alt=\"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u7684\u4e09\u79cd\u529e\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL Server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u7684 3 \u79cd\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 SQL Server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. IDENTITY \u5217<\/strong><\/p>\n<p>IDENTITY \u5217\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5217\uff0c\u5b83\u5728\u63d2\u5165\u65b0\u884c\u65f6\u81ea\u52a8\u751f\u6210\u552f\u4e00\u4e14\u9012\u589e\u7684\u503c\u3002IDENTITY \u503c\u4ece 1 \u5f00\u59cb\uff0c\u5e76\u4e14\u6bcf\u6b21\u63d2\u5165\u65b0\u884c\u65f6\u52a0 1\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CREATE TABLE [TableName] (\n    [ColumnName] INT IDENTITY(1, 1) PRIMARY KEY\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. SEQUENCE<\/strong><\/p>\n<p>SEQUENCE \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u751f\u6210\u552f\u4e00\u4e14\u9012\u589e\u7684\u503c\u5e8f\u5217\u3002\u4e0e IDENTITY \u5217\u4e0d\u540c\uff0cSEQUENCE \u503c\u53ef\u4ee5\u8de8\u591a\u4e2a\u8868\u4f7f\u7528\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CREATE SEQUENCE [SequenceName]\nSTART WITH [StartValue]\nINCREMENT BY [IncrementValue]\nMAXVALUE [MaxValue]\nCACHE [CacheSize];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8981\u5728\u8868\u4e2d\u4f7f\u7528 SEQUENCE\uff0c\u9700\u8981\u521b\u5efa\u5916\u952e\u7ea6\u675f\uff1a<\/p>\n<pre>ALTER TABLE [TableName]\nADD FOREIGN KEY ([ColumnName]) REFERENCES [SequenceName];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. NEWSEQUENTIALID() \u51fd\u6570<\/strong><\/p>\n<p>NEWSEQUENTIALID() \u51fd\u6570\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684 16 \u5b57\u8282\u4e8c\u8fdb\u5236\u503c\uff0c\u8be5\u503c\u57fa\u4e8e GUID (\u5168\u5c40\u552f\u4e00\u6807\u8bc6\u7b26)\u3002\u53ef\u4ee5\u5c06\u6b64\u503c\u7528\u4f5c\u81ea\u52a8\u7f16\u53f7\u5217\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>UPDATE [TableName]\nSET [ColumnName] = NEWSEQUENTIALID();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u81ea\u52a8\u7f16\u53f7\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u5177\u4f53\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6307\u5bfc\u539f\u5219\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u9700\u8981\u81ea\u52a8\u9012\u589e\u6807\u8bc6\u7b26\u7684\u8868\uff0cIDENTITY \u5217\u662f\u6700\u7b80\u5355\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u8de8\u591a\u4e2a\u8868\u4f7f\u7528\u81ea\u52a8\u7f16\u53f7\u503c\uff0c\u5219 SEQUENCE \u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u751f\u6210\u975e\u9012\u589e\u6216\u4e0d\u53ef\u9884\u6d4b\u7684\u503c\uff0c\u5219 NEWSEQUENTIALID() \u51fd\u6570\u662f\u5408\u9002\u7684\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u7684\u4e09\u79cd\u529e\u6cd5\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 server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u6709 3 \u79cd\u65b9\u6cd5\uff1a1. identity \u5217\uff08\u552f\u4e00\u9012\u589e\u503c\uff09\uff1b2. sequence\uff08\u8de8\u8868\u4f7f\u7528\u503c\u5e8f\u5217\uff09\uff1b3. newsequentialid() \u51fd\u6570\uff08\u65e0\u5e8f\u552f\u4e00\u503c\uff09\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u9700\u6c42\uff0c\u5982\u9012\u589e\u6807\u8bc6\u7b26\u4f7f\u7528 identity \u5217\uff0c\u8de8\u8868\u4f7f\u7528\u503c\u4f7f\u7528 sequence\uff0c\u4e0d\u53ef\u9884\u6d4b\u503c\u4f7f\u7528 newsequentialid() \u51fd\u6570\u3002 SQL Server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u7684 3 \u79cd\u65b9\u6cd5 \u5728 SQL Server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a 1. IDENTITY \u5217 IDENTITY \u5217\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5217\uff0c\u5b83\u5728\u63d2\u5165\u65b0\u884c\u65f6\u81ea\u52a8\u751f\u6210\u552f\u4e00\u4e14\u9012\u589e\u7684\u503c\u3002IDENTITY \u503c\u4ece 1 \u5f00\u59cb\uff0c\u5e76\u4e14\u6bcf\u6b21\u63d2\u5165\u65b0\u884c\u65f6\u52a0 1\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a CREATE TABLE [TableName] ( [ColumnName] INT IDENTITY(1, 1) PRIMARY KEY ); \u767b\u5f55\u540e\u590d\u5236 2. SEQUENCE SEQUENCE \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u751f\u6210\u552f\u4e00\u4e14\u9012\u589e\u7684\u503c\u5e8f\u5217\u3002\u4e0e IDENTITY \u5217\u4e0d\u540c\uff0cSEQUENCE \u503c\u53ef\u4ee5\u8de8\u591a\u4e2a\u8868\u4f7f\u7528\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a CREATE SEQUENCE [SequenceName] [&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-6628","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6628","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=6628"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6628\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}