{"id":6629,"date":"2024-11-14T11:57:22","date_gmt":"2024-11-14T03:57:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6629\/"},"modified":"2024-11-14T11:57:22","modified_gmt":"2024-11-14T03:57:22","slug":"sql-server%e4%b8%ad%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%8a%a8%e7%bc%96%e5%8f%b7%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6629\/","title":{"rendered":"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  \u5728 sql server \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u7f16\u53f7\u53ef\u4f7f\u7528 identity \u5c5e\u6027\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u8868\u65f6\u8bbe\u7f6e identity(1, 1) \u5c5e\u6027\uff1b\u63d2\u5165\u6570\u636e\u65f6\u81ea\u52a8\u8bbe\u7f6e id \u5217\uff1b\u4f7f\u7528 @@identity \u67e5\u8be2\u63d2\u5165\u7684\u81ea\u52a8\u7f16\u53f7\u503c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u8bbe\u7f6e\u8d77\u59cb\u503c\u3001\u6b65\u957f\u3001\u79cd\u5b50\u548c\u5faa\u73af\u81ea\u52a8\u7f16\u53f7\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/19\/2024081916465257552.jpg\" class=\"aligncenter\" title=\"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL Server \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u7f16\u53f7<\/strong><\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>\u5728 SQL Server \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u7f16\u53f7\u53ef\u4ee5\u4f7f\u7528 IDENTITY \u5c5e\u6027\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li><strong>\u521b\u5efa\u8868\uff1a<\/strong><\/li>\n<\/ol>\n<pre>CREATE TABLE YourTable (\n    ID INT IDENTITY(1, 1) NOT NULL,\n    Name VARCHAR(50)\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>ID \u662f\u81ea\u52a8\u7f16\u53f7\u5217\u7684\u540d\u79f0\u3002<\/li>\n<li>IDENTITY(1, 1) \u6307\u5b9a\u8d77\u59cb\u503c\u548c\u6b65\u957f\u30021 \u8868\u793a\u8d77\u59cb\u503c\u4e3a 1\uff0c1 \u8868\u793a\u6b65\u957f\u4e3a 1\u3002<\/li>\n<\/ul>\n<ol>\n<li><strong>\u63d2\u5165\u6570\u636e\uff1a<\/strong><\/li>\n<\/ol>\n<pre>INSERT INTO YourTable (Name) VALUES ('John');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u65f6\uff0cID \u5217\u5c06\u81ea\u52a8\u8bbe\u7f6e\u4e3a 1\u3002<\/p>\n<ol>\n<li><strong>\u68c0\u7d22\u81ea\u52a8\u7f16\u53f7\u503c\uff1a<\/strong><\/li>\n<\/ol>\n<pre>SELECT @@IDENTITY;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8be5\u67e5\u8be2\u5c06\u8fd4\u56de\u5f53\u524d\u63d2\u5165\u8bb0\u5f55\u7684\u81ea\u52a8\u7f16\u53f7\u503c\uff0c\u5373 1\u3002<\/p>\n<p><strong>\u5176\u4ed6\u9009\u9879\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u8d77\u59cb\u503c\uff1a<\/strong>\u4f7f\u7528 IDENTITY(  ,  ) \u8bbe\u7f6e\u8d77\u59cb\u503c\u3002   <\/li>\n<li> <strong>\u6b65\u957f\uff1a<\/strong>\u4f7f\u7528 IDENTITY(  ,  ) \u8bbe\u7f6e\u6b65\u957f\u3002   <\/li>\n<li> <strong>\u79cd\u5b50\uff1a<\/strong>\u4f7f\u7528 DBCC CHECKIDENT(&#8216;  &#8216;, RESEED,  ) \u91cd\u65b0\u8bbe\u7f6e\u81ea\u52a8\u7f16\u53f7\u79cd\u5b50\u3002   <\/li>\n<li> <strong>\u5faa\u73af\uff1a<\/strong>\u4f7f\u7528 IDENTITY(  ,  ,  ) RESEED \u8bbe\u7f6e\u5faa\u73af\u81ea\u52a8\u7f16\u53f7\u3002\u5f53\u8fbe\u5230 max_value \u65f6\uff0c\u81ea\u52a8\u7f16\u53f7\u5c06\u91cd\u7f6e\u4e3a start_value    <\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\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>\u5728 sql server \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u7f16\u53f7\u53ef\u4f7f\u7528 identity \u5c5e\u6027\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u8868\u65f6\u8bbe\u7f6e identity(1, 1) \u5c5e\u6027\uff1b\u63d2\u5165\u6570\u636e\u65f6\u81ea\u52a8\u8bbe\u7f6e id \u5217\uff1b\u4f7f\u7528 @@identity \u67e5\u8be2\u63d2\u5165\u7684\u81ea\u52a8\u7f16\u53f7\u503c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u8bbe\u7f6e\u8d77\u59cb\u503c\u3001\u6b65\u957f\u3001\u79cd\u5b50\u548c\u5faa\u73af\u81ea\u52a8\u7f16\u53f7\u3002 SQL Server \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u7f16\u53f7 \u7b54\u6848\uff1a \u5728 SQL Server \u4e2d\u8bbe\u7f6e\u81ea\u52a8\u7f16\u53f7\u53ef\u4ee5\u4f7f\u7528 IDENTITY \u5c5e\u6027\u3002 \u8be6\u7ec6\u6b65\u9aa4\uff1a \u521b\u5efa\u8868\uff1a CREATE TABLE YourTable ( ID INT IDENTITY(1, 1) NOT NULL, Name VARCHAR(50) ); \u767b\u5f55\u540e\u590d\u5236 ID \u662f\u81ea\u52a8\u7f16\u53f7\u5217\u7684\u540d\u79f0\u3002 IDENTITY(1, 1) \u6307\u5b9a\u8d77\u59cb\u503c\u548c\u6b65\u957f\u30021 \u8868\u793a\u8d77\u59cb\u503c\u4e3a 1\uff0c1 \u8868\u793a\u6b65\u957f\u4e3a 1\u3002 \u63d2\u5165\u6570\u636e\uff1a INSERT INTO YourTable (Name) VALUES (&#8216;John&#8217;); [&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-6629","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6629","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=6629"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6629\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}