{"id":319372,"date":"2024-01-20T20:00:55","date_gmt":"2024-01-20T12:00:55","guid":{"rendered":"https:\/\/www.idc.net\/help\/319372\/"},"modified":"2024-01-20T20:00:55","modified_gmt":"2024-01-20T12:00:55","slug":"db2%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e8%87%aa%e5%8a%a8%e5%a2%9e%e9%95%bf%e4%b8%bb%e9%94%ae%e7%9a%84%e5%ae%9e%e9%99%85%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/319372\/","title":{"rendered":"DB2\u6570\u636e\u5e93\u4e2d\u81ea\u52a8\u589e\u957f\u4e3b\u952e\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u6cd5"},"content":{"rendered":"<p>\u4ee5\u4e0b\u7684\u6587\u7ae0\u4e3b\u8981\u5411\u5927\u5bb6\u8bb2\u8ff0\u7684\u662fDB2\u6570\u636e\u5e93\u4e2d\u81ea\u52a8\u589e\u957f\u4e3b\u952e\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2dDB2\u6570\u636e\u5e93\u53ef\u7528generated always as identity \u5c06\u67d0\u4e00\u4e2a\u76f8\u5173\u5b57\u6bb5\u6307\u5b9a\u4e3a\u81ea\u589e\u957f\u7684\u5b57\u6bb5\uff0c\u6bd4\u5982\uff1a<\/p>\n<p>\u8fd9\u8868\u793aid\u81ea\u52a8\u662f\u4e00\u4e2a\u81ea\u589e\u957f\u7684\u5b57\u6bb5\uff0c\u5b83\u4ece1\u5f00\u59cb\u589e\u52a0\u6bcf\u6b21\u589e\u52a01\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7generated \u5c06\u5b57\u6bb5\u7684\u503c\u901a\u8fc7\u5176\u4ed6\u5b57\u6bb5\u7684\u8ba1\u7b97\u7684\u6765\uff0c\u6bd4\u5982;<\/p>\n<p> &nbsp;<\/p>\n<pre>\n \n \n <ol>\n  \n  \n  <li><span><span>create&nbsp;table&nbsp;user(name&nbsp;varchar(30),&nbsp; &nbsp;<\/span><\/span><\/li>\n  \n  \n  <li><span>n1&nbsp;integer,&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>n2&nbsp;integer&nbsp;,&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>id&nbsp;integer&nbsp;generated&nbsp;always&nbsp;as&nbsp;(n1+n2))&nbsp;&nbsp;<\/span><\/li>\n \n \n <\/ol><\/pre>\n<p> \u5982\uff1a<\/p>\n<p> &nbsp;<\/p>\n<pre>\n \n \n <ol>\n  \n  \n  <li><span><span>create&nbsp;table&nbsp;strategy&nbsp; &nbsp;<\/span><\/span><\/li>\n  \n  \n  <li><span>(&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>strategy_id&nbsp;decimal(17)&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>GENERATED&nbsp;ALWAYS&nbsp;AS&nbsp;IDENTITY&nbsp;(START&nbsp;WITH&nbsp;1,&nbsp;INCREMENT&nbsp;BY&nbsp;1&nbsp;)&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>primary&nbsp;key&nbsp;not&nbsp;null,&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>strategy_name&nbsp;varchar(200),&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>area_code&nbsp;decimal(6,0)&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>);&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>GENERATED&nbsp;ALWAYS&nbsp;AS&nbsp;IDENTITY&nbsp;(START&nbsp;WITH&nbsp;1,&nbsp;INCREMENT&nbsp;BY&nbsp;1&nbsp;)&nbsp;&nbsp;<\/span><\/li>\n \n \n <\/ol><\/pre>\n<p> \u4ece1\u5f00\u59cb\u81ea\u589e\uff0c\u6bcf\u6b21\u52a01.<\/p>\n<p> DB2 Identity\u5b57\u6bb5 \u4e0e sequence\uff1a<\/p>\n<p> Identity\u5b57\u6bb5\u4e0esequence\u7684\u6700\u5927\u4e0d\u540c\u5728\u4e8e\uff1aidentity\u53ea\u80fd\u5728\u4e00\u4e2a\u8868\u4e2d\u4f7f\u7528\uff0c\u591a\u4e2a\u8868\u4e0d\u53ef\u4ee5\u5171\u4eabidentity;\u800csequence\u53ef\u4ee5\u901a\u8fc7\u547d\u540d\u65b9\u5f0f\u5728\u540c\u4e00\u4e2aDB2\u6570\u636e\u5e93\u5185\u90e8\u7684\u591a\u4e2a\u8868\u4e2d\u5171\u4eab\u5e8f\u5217\u53f7\u53d1\u751f\u5668<\/p>\n<p>\u751f\u6210\u6570\u5b57\u5e8f\u5217\u7684\u4e24\u79cd\u65b9\u6cd5<\/p>\n<p>\u5728 DB2 \u4e2d\u53ef\u4ee5\u7528\u4e24\u79cd\u65b9\u6cd5\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u6570\u5b57\u5e8f\u5217\uff1a<\/p>\n<p>\u5b9a\u4e49\u5e26\u6709 IDENTITY \u5c5e\u6027\u7684\u5217\u3002<\/p>\n<p>\u521b\u5efa SEQUENCE \u5bf9\u8c61\u3002<\/p>\n<p>IDENTITY \u5217<\/p>\n<p>\u5f53\u7528 IDENTITY \u5c5e\u6027\u5b9a\u4e49\u8868\u7684\u5217\u65f6\uff0c\u6bcf\u5f53\u5c06\u4e00\u884c\u63d2\u5165\u8868\u65f6\uff0c\u5c31\u4f1a\u81ea\u52a8\u4e3a\u8be5\u5217\u751f\u6210\u4e00\u4e2a\u6570\u503c\u3002<\/p>\n<p>SEQUENCE \u5bf9\u8c61<\/p>\n<p>\u8ba9 DB2 \u81ea\u52a8\u751f\u6210\u6570\u5b57\u5e8f\u5217\u7684\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u521b\u5efa SEQUENCE \u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528 \u5e8f\u5217\u8868\u8fbe\u5f0f(sequence expression)\u6765\u5f15\u7528\u5e8f\u5217\u5bf9\u8c61\u3002\u5e8f\u5217\u8868\u8fbe\u5f0f\u53ef\u4ee5\u51fa\u73b0\u5728\u8868\u8fbe\u5f0f\u80fd\u591f\u51fa\u73b0\u7684\u5927\u591a\u6570\u4f4d\u7f6e\u3002\u5e8f\u5217\u8868\u8fbe\u5f0f\u53ef\u4ee5\u6307\u5b9a\u8981\u8fd4\u56de\u7684\u503c\u662f\u65b0\u751f\u6210\u7684\u503c\u8fd8\u662f\u4ee5\u524d\u751f\u6210\u7684\u503c\u3002<\/p>\n<p>\u5982\u679c\u5e8f\u5217\u5f15\u7528\u662f\u9488\u5bf9 \u4e0b\u4e00\u503c(next value)\uff0c\u5c31\u4f1a\u4e3a\u5e8f\u5217\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u6570\u503c\uff0c\u5e76\u5c06\u8be5\u6570\u503c\u4f5c\u4e3a\u5e8f\u5217\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u8fd4\u56de\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5047\u8bbe\u5df2\u7ecf\u521b\u5efa\u4e86\u540d\u4e3a orders_seq \u7684\u5e8f\u5217\uff0c\u8be5\u5e8f\u5217\u8868\u8fbe\u5f0f\u5c31\u8fd4\u56de\u4e3a\u5e8f\u5217\u751f\u6210\u7684\u4e0b\u4e00\u4e2a\u503c\uff1a<\/p>\n<p> &nbsp;<\/p>\n<pre>\n \n \n <ol>\n  \n  \n  <li><span><span>NEXT&nbsp;VALUE&nbsp;FOR&nbsp;orders_seq&nbsp;&nbsp;<\/span><\/span><\/li>\n \n \n <\/ol><\/pre>\n<p> \u5982\u679c\u5e8f\u5217\u5f15\u7528\u662f\u9488\u5bf9 \u524d\u4e00\u503c(previous value)\uff0c\u90a3\u4e48\u4f1a\u5c06\u524d\u4e00\u4e2a SQL \u8bed\u53e5\u4e2d\u4e3a\u5e8f\u5217\u751f\u6210\u7684\u6570\u503c\u4f5c\u4e3a\u5e8f\u5217\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u8fd4\u56de\u3002\u8be5\u5e8f\u5217\u8868\u8fbe\u5f0f\u8fd4\u56de\u7531\u5e8f\u5217\u751f\u6210\u7684\u524d\u4e00\u4e2a\u503c\uff1a<\/p>\n<p> &nbsp;<\/p>\n<pre>\n \n \n <ol>\n  \n  \n  <li><span><span>PREVIOUS&nbsp;VALUE&nbsp;FOR&nbsp;orders_seq&nbsp;<\/span><\/span><\/li>\n \n \n <\/ol><\/pre>\n<p> \u6ce8\uff1a\u5f53 DB2 UDB \u5f15\u5165\u5e8f\u5217\u65f6\uff0c\u66fe\u652f\u6301\u975e SQL \u6807\u51c6\u8bed\u6cd5 NEXTVAL \u4ee3\u66ff NEXT VALUE \uff0c\u4ee5\u53ca PREVVAL \u4ee3\u66ff PREVIOUS VALUE \u3002\u8fd9\u4e9b\u53d8\u4f53\u7ee7\u7eed\u53d7\u5230\u652f\u6301\u3002<\/p>\n<p>\u5728\u6807\u8bc6\u548c\u5e8f\u5217\u4e4b\u95f4\u505a\u51fa\u9009\u62e9<\/p>\n<p>\u867d\u7136\u6807\u8bc6\u548c\u5e8f\u5217\u90fd\u7528\u4e8e\u751f\u6210\u6570\u503c\uff0c\u4f46\u60a8\u4e5f\u8bb8\u8981\u6839\u636e\u7279\u6b8a\u60c5\u51b5\u9009\u62e9\u4f7f\u7528\u4e00\u79cd\u6765\u4ee3\u66ff\u53e6\u4e00\u79cd\u3002<\/p>\n<p>\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\uff0c\u6807\u8bc6\u5217\u5f88\u6709\u7528\uff1a<\/p>\n<p>\u8868\u4e2d\u53ea\u6709\u4e00\u5217\u9700\u8981\u81ea\u52a8\u751f\u6210\u7684\u503c<\/p>\n<p>\u6bcf\u4e00\u884c\u90fd\u9700\u8981\u72ec\u7acb\u7684\u503c<\/p>\n<p>\u7528\u81ea\u52a8\u751f\u6210\u5668\u6765\u4ea7\u751f\u8868\u7684\u4e3b\u952e<\/p>\n<p>\u751f\u6210\u65b0\u503c\u7684\u8fdb\u7a0b\u4e0e\u5bf9\u8868\u7684\u63d2\u5165\u64cd\u4f5c\u7d27\u5bc6\u8054\u7cfb\uff0c\u65e0\u8bba\u63d2\u5165\u64cd\u4f5c\u662f\u5982\u4f55\u53d1\u751f\u7684<\/p>\n<p>\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\uff0c\u5e8f\u5217\u5bf9\u8c61\u5f88\u6709\u7528\uff1a<\/p>\n<p>\u8981\u5c06\u4ece\u4e00\u4e2a\u5e8f\u5217\u751f\u6210\u7684\u503c\u5b58\u50a8\u5230\u591a\u4e2a\u8868\u4e2d<\/p>\n<p>\u6bcf\u4e2a\u8868\u6709\u591a\u4e2a\u5217\u9700\u8981\u81ea\u52a8\u751f\u6210\u7684\u503c(\u53ef\u80fd\u901a\u8fc7\u4f7f\u7528\u540c\u4e00\u4e2a\u5e8f\u5217\u6216\u591a\u4e2a\u5e8f\u5217\u4e3a\u6bcf\u4e00\u884c\u751f\u6210\u591a\u4e2a\u503c)<\/p>\n<p>\u751f\u6210\u65b0\u503c\u7684\u8fdb\u7a0b\u4e0e\u4efb\u4f55\u5bf9\u8868\u7684\u5f15\u7528\u65e0\u5173<\/p>\n<p>\u4e0e\u5e8f\u5217\u5bf9\u8c61\u4e0d\u540c\uff0c\u6807\u8bc6\u5217\u662f\u5728\u8868\u4e0a\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u9700\u8981\u67d0\u4e9b\u9650\u5236\u3002\u6bcf\u4e2a\u8868\u6700\u591a\u53ea\u80fd\u6709\u4e00\u4e2a\u6807\u8bc6\u5217\u3002\u5f53\u521b\u5efa\u4e00\u4e2a\u5217\u4f5c\u4e3a\u6807\u8bc6\u5217\u65f6\uff0c\u5bf9\u4e8e\u8be5\u5217\u5fc5\u987b\u4f7f\u7528\u786e\u5207\u7684\u6570\u5b57\u6570\u636e\u7c7b\u578b\u3002\u56e0\u4e3a\u6807\u8bc6\u5c5e\u6027\u4e3a\u5217\u751f\u6210\u4e00\u4e2a\u503c\uff0c\u8fd9\u7c7b\u4f3c\u4e8e DEFAULT \u5b50\u53e5\u6240\u505a\u7684\uff0c\u6240\u4ee5\u5728\u5b9a\u4e49\u6807\u8bc6\u5217\u65f6\u4e0d\u80fd\u6307\u5b9a DEFAULT \u5b50\u53e5\u3002\u6807\u8bc6\u5217\u88ab\u9690\u5f0f\u5730\u5b9a\u4e49\u6210 NOT NULL \u3002\u4ee5\u4e0a\u7684\u76f8\u5173\u5185\u5bb9\u5c31\u662f\u5bf9DB2\u6570\u636e\u5e93\u4e2d\u81ea\u52a8\u589e\u957f\u4e3b\u952e\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u6cd5\u7684\u4ecb\u7ecd\uff0c\u671b\u4f60\u80fd\u6709\u6240\u6536\u83b7\u3002<\/p>\n<p>\u3010\u7f16\u8f91\u63a8\u8350\u3011<\/p>\n<ol>\n<li>\u5bf9\u6574\u7406DB2\u6570\u636e\u96c6\u8303\u56f4\u7684\u7b80\u4ecb<\/li>\n<li>DB2 LOAD\u547d\u4ee4\u6240\u63d0\u4f9b\u7684\u9009\u9879\u53ca\u6ce8\u518c\u8868\u53d8\u91cf\u5927\u5168<\/li>\n<li>DB2\u5feb\u7167\u51fd\u6570\u5168\u89e3\u6790\u7ecf\u5178\u7248<\/li>\n<li>DB2 \u4f18\u5316\u5668\u4e2d\u9488\u5bf9 JOIN \u8bed\u53e5\u7684\u7ed3\u679c\u96c6\u4f30\u8ba1<\/li>\n<li>DB2\uff1a\u4fe1\u606f\u7ba1\u7406 \u5c3d\u5728\u638c\u63e1<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u7684\u6587\u7ae0\u4e3b\u8981\u5411\u5927\u5bb6\u8bb2\u8ff0\u7684\u662fDB2\u6570\u636e\u5e93\u4e2d\u81ea\u52a8\u589e\u957f\u4e3b\u952e\u7684\u5b9e\u9645\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2dDB2\u6570\u636e\u5e93\u53ef\u7528genera [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-319372","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/319372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=319372"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/319372\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=319372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=319372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=319372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}