{"id":5868,"date":"2024-11-14T12:36:48","date_gmt":"2024-11-14T04:36:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5868\/"},"modified":"2024-11-14T12:36:48","modified_gmt":"2024-11-14T04:36:48","slug":"sql%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5868\/","title":{"rendered":"sql\u5b57\u7b26\u4e32\u600e\u4e48\u8bbe\u7f6e\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  sql \u5b57\u7b26\u4e32\u957f\u5ea6\u9650\u5236\u53d6\u51b3\u4e8e\u6570\u636e\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528 char(n)\u3001varchar(n) \u548c text \u8bbe\u7f6e\u56fa\u5b9a\u957f\u5ea6\u3001\u53ef\u53d8\u957f\u5ea6\u548c\u975e\u5e38\u957f\u7684\u6587\u672c\u3002\u8bbe\u7f6e\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5728 create table \u8bed\u53e5\u4e2d\u8bbe\u7f6e\uff0c\u5728 alter table \u8bed\u53e5\u4e2d\u4fee\u6539\uff0c\u4ee5\u53ca\u5728 insert \u6216 update \u8bed\u53e5\u4e2d\u6307\u5b9a\u3002\u8bbe\u7f6e\u957f\u5ea6\u9650\u5236\u6709\u52a9\u4e8e\u4f18\u5316\u6027\u80fd\u548c\u9632\u6b62\u6570\u636e\u6ea2\u51fa\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060321153686974.jpg\" class=\"aligncenter\" title=\"sql\u5b57\u7b26\u4e32\u600e\u4e48\u8bbe\u7f6e\u957f\u5ea6\u63d2\u56fe\" alt=\"sql\u5b57\u7b26\u4e32\u600e\u4e48\u8bbe\u7f6e\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e SQL \u5b57\u7b26\u4e32\u957f\u5ea6<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u957f\u5ea6\u9650\u5236\u51b3\u5b9a\u4e86\u5b57\u7b26\u4e32\u53ef\u4ee5\u5b58\u50a8\u7684\u6700\u5927\u5b57\u7b26\u6570\u3002\u6839\u636e\u6570\u636e\u5e93\u7cfb\u7edf\u548c\u6570\u636e\u7c7b\u578b\uff0c\u8bbe\u7f6e\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u65b9\u6cd5\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<p><strong>\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<ul>\n<li> <strong>CHAR(n)<\/strong>\uff1a\u56fa\u5b9a\u957f\u5ea6\u5b57\u7b26\u4e32\uff0cn \u6307\u5b9a\u6700\u5927\u5b57\u7b26\u6570\u3002<\/li>\n<li> <strong>VARCHAR(n)<\/strong>\uff1a\u53ef\u53d8\u957f\u5ea6\u5b57\u7b26\u4e32\uff0cn \u6307\u5b9a\u6700\u5927\u5b57\u7b26\u6570\u3002<\/li>\n<li> <strong>TEXT<\/strong>\uff1a\u53ef\u5b58\u50a8\u5927\u91cf\u6587\u672c\u6570\u636e\u7684\u7279\u6b8a\u6570\u636e\u7c7b\u578b\uff0c\u957f\u5ea6\u9650\u5236\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/li>\n<li> <strong>CLOB<\/strong>\uff1a\u5b57\u7b26\u5927\u5bf9\u8c61\uff0c\u53ef\u5b58\u50a8\u975e\u5e38\u5927\u7684\u6587\u672c\u6570\u636e\uff0c\u957f\u5ea6\u9650\u5236\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<p><strong>\u8bbe\u7f6e\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u65b9\u6cd5<\/strong><\/p>\n<p><strong>1. \u5728 CREATE TABLE \u8bed\u53e5\u4e2d\u8bbe\u7f6e\u957f\u5ea6<\/strong><\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>CREATE TABLE users (\n  name VARCHAR(255) NOT NULL,\n  description TEXT\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u8bed\u53e5\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a &#8220;users&#8221; \u7684\u8868\uff0c\u5176\u4e2d name \u5217\u4e3a\u53ef\u53d8\u957f\u5ea6\u5b57\u7b26\u4e32\uff0c\u6700\u5927\u957f\u5ea6\u4e3a 255 \u4e2a\u5b57\u7b26\uff0cdescription \u5217\u4e3a\u6587\u672c\u7c7b\u578b\uff0c\u957f\u5ea6\u9650\u5236\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/p>\n<p><strong>2. \u5728 ALTER TABLE \u8bed\u53e5\u4e2d\u4fee\u6539\u957f\u5ea6<\/strong><\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>ALTER TABLE users ALTER COLUMN name VARCHAR(500);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u8bed\u53e5\u5c06\u4fee\u6539 &#8220;users&#8221; \u8868\u4e2d name \u5217\u7684\u957f\u5ea6\uff0c\u5c06\u5176\u589e\u52a0\u5230 500 \u4e2a\u5b57\u7b26\u3002<\/p>\n<p><strong>3. \u5728 INSERT \u6216 UPDATE \u8bed\u53e5\u4e2d\u6307\u5b9a\u957f\u5ea6<\/strong><\/p>\n<p>\u5982\u679c\u5728 CREATE TABLE \u8bed\u53e5\u4e2d\u672a\u6307\u5b9a\u957f\u5ea6\uff0c\u5219\u53ef\u4ee5\u5728 INSERT \u6216 UPDATE \u8bed\u53e5\u4e2d\u6307\u5b9a\uff1a<\/p>\n<pre>INSERT INTO users (name) VALUES ('John Doe' COLLATE utf8mb4_500_ci);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u8bed\u53e5\u5728 &#8220;users&#8221; \u8868\u4e2d\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff0c\u5e76\u5c06 name \u5217\u7684\u503c\u8bbe\u7f6e\u4e3a John Doe\uff0c\u5e76\u6307\u5b9a\u5b57\u7b26\u96c6\u548c\u957f\u5ea6\u4e3a 500 \u7684\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8bbe\u7f6e\u5b57\u7b26\u4e32\u957f\u5ea6\u9650\u5236\u6709\u52a9\u4e8e\u4f18\u5316\u67e5\u8be2\u6027\u80fd\u548c\u9632\u6b62\u6570\u636e\u6ea2\u51fa\u3002<\/li>\n<li>\u5bf9\u4e8e\u4e0d\u9700\u8981\u4e25\u683c\u957f\u5ea6\u9650\u5236\u7684\u6587\u672c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 TEXT \u6216 CLOB \u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>\u4e0d\u540c\u6570\u636e\u5e93\u7cfb\u7edf\u5bf9\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64\u8bf7\u67e5\u9605\u7279\u5b9a\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u6587\u6863\u4ee5\u83b7\u53d6\u786e\u5207\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5b57\u7b26\u4e32\u600e\u4e48\u8bbe\u7f6e\u957f\u5ea6\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 \u5b57\u7b26\u4e32\u957f\u5ea6\u9650\u5236\u53d6\u51b3\u4e8e\u6570\u636e\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528 char(n)\u3001varchar(n) \u548c text \u8bbe\u7f6e\u56fa\u5b9a\u957f\u5ea6\u3001\u53ef\u53d8\u957f\u5ea6\u548c\u975e\u5e38\u957f\u7684\u6587\u672c\u3002\u8bbe\u7f6e\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5728 create table \u8bed\u53e5\u4e2d\u8bbe\u7f6e\uff0c\u5728 alter table \u8bed\u53e5\u4e2d\u4fee\u6539\uff0c\u4ee5\u53ca\u5728 insert \u6216 update \u8bed\u53e5\u4e2d\u6307\u5b9a\u3002\u8bbe\u7f6e\u957f\u5ea6\u9650\u5236\u6709\u52a9\u4e8e\u4f18\u5316\u6027\u80fd\u548c\u9632\u6b62\u6570\u636e\u6ea2\u51fa\u3002 \u5982\u4f55\u8bbe\u7f6e SQL \u5b57\u7b26\u4e32\u957f\u5ea6 \u5728 SQL \u4e2d\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u957f\u5ea6\u9650\u5236\u51b3\u5b9a\u4e86\u5b57\u7b26\u4e32\u53ef\u4ee5\u5b58\u50a8\u7684\u6700\u5927\u5b57\u7b26\u6570\u3002\u6839\u636e\u6570\u636e\u5e93\u7cfb\u7edf\u548c\u6570\u636e\u7c7b\u578b\uff0c\u8bbe\u7f6e\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u65b9\u6cd5\u6709\u6240\u4e0d\u540c\u3002 \u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b CHAR(n)\uff1a\u56fa\u5b9a\u957f\u5ea6\u5b57\u7b26\u4e32\uff0cn \u6307\u5b9a\u6700\u5927\u5b57\u7b26\u6570\u3002 VARCHAR(n)\uff1a\u53ef\u53d8\u957f\u5ea6\u5b57\u7b26\u4e32\uff0cn \u6307\u5b9a\u6700\u5927\u5b57\u7b26\u6570\u3002 TEXT\uff1a\u53ef\u5b58\u50a8\u5927\u91cf\u6587\u672c\u6570\u636e\u7684\u7279\u6b8a\u6570\u636e\u7c7b\u578b\uff0c\u957f\u5ea6\u9650\u5236\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u3002 CLOB\uff1a\u5b57\u7b26\u5927\u5bf9\u8c61\uff0c\u53ef\u5b58\u50a8\u975e\u5e38\u5927\u7684\u6587\u672c\u6570\u636e\uff0c\u957f\u5ea6\u9650\u5236\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u3002 \u8bbe\u7f6e\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u65b9\u6cd5 1. \u5728 CREATE TABLE \u8bed\u53e5\u4e2d\u8bbe\u7f6e\u957f\u5ea6 \u4f8b\u5982\uff1a CREATE TABLE users ( name VARCHAR(255) NOT NULL, description TEXT ); \u767b\u5f55\u540e\u590d\u5236 \u6b64\u8bed\u53e5\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a &#8220;users&#8221; \u7684\u8868\uff0c\u5176\u4e2d name \u5217\u4e3a\u53ef\u53d8\u957f\u5ea6\u5b57\u7b26\u4e32\uff0c\u6700\u5927\u957f\u5ea6\u4e3a 255 \u4e2a\u5b57\u7b26\uff0cdescription [&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-5868","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5868","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=5868"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5868\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}