{"id":6627,"date":"2024-11-14T12:02:03","date_gmt":"2024-11-14T04:02:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6627\/"},"modified":"2024-11-14T12:02:03","modified_gmt":"2024-11-14T04:02:03","slug":"sql-server%e4%b8%ad%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%8a%a8%e7%bc%96%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6627\/","title":{"rendered":"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7"},"content":{"rendered":"<blockquote><p>\n  sql server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a1. \u6807\u8bc6\u6570\u636e\u7c7b\u578b\u81ea\u52a8\u751f\u6210\u552f\u4e00\u9012\u589e\u503c\uff1b2. \u5e8f\u5217\u5bf9\u8c61\u751f\u6210\u552f\u4e00\u9012\u589e\u6574\u6570\u5e8f\u5217\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9700\u6c42\uff1a\u6807\u8bc6\u7b80\u5355\u6613\u7528\uff0c\u4f46\u622a\u65ad\u6216\u91cd\u5efa\u8868\u65f6\u91cd\u7f6e\u5e8f\u5217\uff1b\u5e8f\u5217\u63d0\u4f9b\u66f4\u7cbe\u7ec6\u63a7\u5236\uff0c\u4f46\u521b\u5efa\u548c\u7ba1\u7406\u66f4\u590d\u6742\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/19\/2024081916460085217.jpg\" class=\"aligncenter\" title=\"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u63d2\u56fe\" alt=\"sql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL Server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7<\/strong><\/p>\n<p><strong>\u81ea\u52a8\u7f16\u53f7<\/strong>\u662f\u4e00\u79cd\u6570\u636e\u5e93\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u6570\u636e\u5e93\u81ea\u52a8\u751f\u6210\u552f\u4e00\u7684\u6570\u5b57\u5e8f\u5217\u3002\u5728 SQL Server \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\uff1a<\/p>\n<p><strong>1. \u6807\u8bc6\uff08Identity\uff09<\/strong><\/p>\n<ul>\n<li>\u6807\u8bc6\u6570\u636e\u7c7b\u578b\u4f1a\u5728\u63d2\u5165\u65b0\u884c\u65f6\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u503c\u3002<\/li>\n<li>\u8be5\u503c\u662f\u9012\u589e\u7684\uff08\u4ece 1 \u5f00\u59cb\uff09\u4e14\u4e0d\u91cd\u590d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>CREATE TABLE Customers (\n  CustomerID int IDENTITY(1, 1) PRIMARY KEY,\n  FirstName varchar(50) NOT NULL,\n  LastName varchar(50) NOT NULL\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5f53\u63d2\u5165\u65b0\u884c\u65f6\uff0cCustomerID \u5217\u7684\u503c\u5c06\u81ea\u52a8\u586b\u5145\u4e3a 1\u30012\u30013 \u7b49\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/p>\n<p><strong>2. \u5e8f\u5217\uff08Sequence\uff09<\/strong><\/p>\n<ul>\n<li>\u5e8f\u5217\u662f\u4e00\u79cd\u5bf9\u8c61\uff0c\u7528\u4e8e\u751f\u6210\u552f\u4e00\u4e14\u9012\u589e\u7684\u6574\u6570\u5e8f\u5217\u3002<\/li>\n<li>\u5e8f\u5217\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u548c\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>CREATE SEQUENCE CustomerSequence START WITH 1 INCREMENT BY 1;\n\nCREATE TABLE Customers (\n  CustomerID int PRIMARY KEY,\n  FirstName varchar(50) NOT NULL,\n  LastName varchar(50) NOT NULL\n);\n\nALTER TABLE Customers ADD CONSTRAINT DF_Customer_CustomerID DEFAULT (NEXT VALUE FOR CustomerSequence);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5f53\u63d2\u5165\u65b0\u884c\u65f6\uff0cCustomerID \u5217\u7684\u503c\u5c06\u81ea\u52a8\u586b\u5145\u4e3a\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u503c\uff08\u4f8b\u5982 1\u30012\u30013 \u7b49\uff09\u3002<\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u6807\u8bc6\u548c\u5e8f\u5217\u90fd\u53ef\u7528\u4e8e\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\uff0c\u4f46\u5b83\u4eec\u5404\u6709\u4f18\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>\u6807\u8bc6\uff1a<\/strong>\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5982\u679c\u8868\u88ab\u622a\u65ad\u6216\u91cd\u65b0\u5efa\u7acb\uff0c\u5219\u5e8f\u5217\u5c06\u91cd\u7f6e\u4e3a 1\u3002<\/li>\n<li> <strong>\u5e8f\u5217\uff1a<\/strong>\u5141\u8bb8\u66f4\u597d\u7684\u63a7\u5236\u5e8f\u5217\u503c\uff0c\u4f46\u521b\u5efa\u548c\u7ba1\u7406\u8d77\u6765\u66f4\u590d\u6742\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u6807\u8bc6\u901a\u5e38\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u9700\u8981\u66f4\u9ad8\u7ea7\u63a7\u5236\u7684\u60c5\u51b5\uff0c\u5e8f\u5217\u66f4\u5408\u9002\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql server\u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\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\u4e24\u79cd\u65b9\u6cd5\uff1a1. \u6807\u8bc6\u6570\u636e\u7c7b\u578b\u81ea\u52a8\u751f\u6210\u552f\u4e00\u9012\u589e\u503c\uff1b2. \u5e8f\u5217\u5bf9\u8c61\u751f\u6210\u552f\u4e00\u9012\u589e\u6574\u6570\u5e8f\u5217\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9700\u6c42\uff1a\u6807\u8bc6\u7b80\u5355\u6613\u7528\uff0c\u4f46\u622a\u65ad\u6216\u91cd\u5efa\u8868\u65f6\u91cd\u7f6e\u5e8f\u5217\uff1b\u5e8f\u5217\u63d0\u4f9b\u66f4\u7cbe\u7ec6\u63a7\u5236\uff0c\u4f46\u521b\u5efa\u548c\u7ba1\u7406\u66f4\u590d\u6742\u3002 SQL Server \u4e2d\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7 \u81ea\u52a8\u7f16\u53f7\u662f\u4e00\u79cd\u6570\u636e\u5e93\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u6570\u636e\u5e93\u81ea\u52a8\u751f\u6210\u552f\u4e00\u7684\u6570\u5b57\u5e8f\u5217\u3002\u5728 SQL Server \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u5b9e\u73b0\u81ea\u52a8\u7f16\u53f7\uff1a 1. \u6807\u8bc6\uff08Identity\uff09 \u6807\u8bc6\u6570\u636e\u7c7b\u578b\u4f1a\u5728\u63d2\u5165\u65b0\u884c\u65f6\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u503c\u3002 \u8be5\u503c\u662f\u9012\u589e\u7684\uff08\u4ece 1 \u5f00\u59cb\uff09\u4e14\u4e0d\u91cd\u590d\u3002 \u793a\u4f8b\uff1a CREATE TABLE Customers ( CustomerID int IDENTITY(1, 1) PRIMARY KEY, FirstName varchar(50) NOT NULL, LastName varchar(50) NOT NULL ); \u767b\u5f55\u540e\u590d\u5236 \u5f53\u63d2\u5165\u65b0\u884c\u65f6\uff0cCustomerID \u5217\u7684\u503c\u5c06\u81ea\u52a8\u586b\u5145\u4e3a 1\u30012\u30013 \u7b49\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002 2. \u5e8f\u5217\uff08Sequence\uff09 \u5e8f\u5217\u662f\u4e00\u79cd\u5bf9\u8c61\uff0c\u7528\u4e8e\u751f\u6210\u552f\u4e00\u4e14\u9012\u589e\u7684\u6574\u6570\u5e8f\u5217\u3002 \u5e8f\u5217\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u548c\u7ba1\u7406\u3002 \u793a\u4f8b\uff1a CREATE SEQUENCE CustomerSequence START WITH 1 INCREMENT [&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-6627","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6627","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=6627"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6627\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}