{"id":200120,"date":"2025-05-06T08:24:23","date_gmt":"2025-05-06T00:24:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200120\/"},"modified":"2025-05-06T08:24:23","modified_gmt":"2025-05-06T00:24:23","slug":"%e5%9c%a8-postgresql-%e5%92%8c-mysql-%e4%b8%ad%e7%9a%84%e6%89%80%e6%9c%89%e8%a1%a8%e4%b8%ad%e6%9f%a5%e6%89%be%e7%89%b9%e5%ae%9a%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200120\/","title":{"rendered":"\u5728 PostgreSQL \u548c MySQL \u4e2d\u7684\u6240\u6709\u8868\u4e2d\u67e5\u627e\u7279\u5b9a\u6570\u636e"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728 PostgreSQL \u548c MySQL \u4e2d\u7684\u6240\u6709\u8868\u4e2d\u67e5\u627e\u7279\u5b9a\u6570\u636e<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5728 PostgreSQL \u548c MySQL \u4e2d\u7684\u6240\u6709\u8868\u4e2d\u67e5\u627e\u7279\u5b9a\u6570\u636e\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240726\/172198911566a377fb9389d.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4f7f\u7528\u5927\u578b\u6570\u636e\u5e93\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u53d1\u73b0\u81ea\u5df1\u9700\u8981\u5728\u591a\u4e2a\u8868\u548c\u5217\u4e2d\u67e5\u627e\u7279\u5b9a\u503c\u3002\u8fd9\u53ef\u80fd\u662f\u4e00\u9879\u5177\u6709\u6311\u6218\u6027\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5f53\u60a8\u4e0d\u77e5\u9053\u5230\u5e95\u8be5\u53bb\u54ea\u91cc\u67e5\u770b\u65f6\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5728 postgresql \u548c mysql \u4e2d\u81ea\u52a8\u6267\u884c\u6b64\u641c\u7d22\u3002<\/p>\n<h3> \u5728 postgresql \u4e2d\u641c\u7d22\u6570\u636e <\/h3>\n<p>postgresql \u5141\u8bb8\u9ad8\u7ea7\u8fc7\u7a0b\u8bed\u8a00\u529f\u80fd\uff0c\u8fd9\u5728\u6b64\u7c7b\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a pl\/pgsql \u5757\uff0c\u7528\u4e8e\u5728 postgresql \u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u8868\u548c\u5217\u4e2d\u641c\u7d22\u7279\u5b9a\u503c\u3002<\/p>\n<h4> \u5206\u6b65\u6307\u5357\uff1a <\/h4>\n<ol>\n<li><strong>\u521b\u5efa pl\/pgsql \u5757\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u4ee5\u4e0b pl\/pgsql \u5757\u5c06\u5728\u516c\u5171\u6a21\u5f0f\u5185\u6240\u6709\u7c7b\u578b\u4e3a\u5b57\u7b26\u53d8\u5316\u3001\u6587\u672c\u6216 uuid \u7684\u5217\u4e2d\u641c\u7d22\u503c\u201cdcea8891-b4e1-45f8-8cb9-c8a164cb98ff\u201d\u3002<\/p>\n<pre>   do $$ \n   declare\n       rec record;\n       search_text text := 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff';\n       query text;\n   begin\n       for rec in \n           select table_schema, table_name, column_name \n           from information_schema.columns \n           where table_schema = 'public' and data_type in ('character varying', 'text', 'uuid') \n       loop\n           query := 'select ''' || rec.table_schema || '.' || rec.table_name || '.' || rec.column_name || ''' as location, ' || \n                    rec.column_name || ' \n                    from ' || rec.table_schema || '.' || rec.table_name || ' \n                    where ' || rec.column_name || '::text = $1';\n\n           execute query using search_text into rec;\n\n           if rec is not null then\n               raise notice 'found in %', rec.location;\n           end if;\n       end loop;\n   end $$;\n<\/pre>\n<ol>\n<li><strong>\u8bf4\u660e\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li> <strong>\u58f0\u660e\u53d8\u91cf\uff1a<\/strong>\u6211\u4eec\u58f0\u660e\u53d8\u91cf\u6765\u4fdd\u5b58\u6211\u4eec\u7684\u641c\u7d22\u6587\u672c\u548c\u52a8\u6001\u67e5\u8be2\u3002<\/li>\n<li> <strong>\u5faa\u73af\u5217\uff1a<\/strong> \u6211\u4eec\u5faa\u73af\u904d\u5386\u516c\u5171\u6a21\u5f0f\u4e2d\u7c7b\u578b\u4e3a\u5b57\u7b26\u53d8\u5316\u3001\u6587\u672c\u6216 uuid \u7684\u6bcf\u4e00\u5217\u3002<\/li>\n<li> <strong>\u6784\u9020\u5e76\u6267\u884c\u67e5\u8be2\uff1a<\/strong> \u5bf9\u4e8e\u6bcf\u4e00\u5217\uff0c\u6211\u4eec\u6784\u9020\u4e00\u4e2a\u52a8\u6001 sql \u67e5\u8be2\u6765\u68c0\u67e5\u8be5\u5217\u662f\u5426\u5305\u542b\u641c\u7d22\u6587\u672c\u3002<\/li>\n<li> <strong>\u53d1\u51fa\u901a\u77e5\uff1a<\/strong> \u5982\u679c\u627e\u5230\u641c\u7d22\u6587\u672c\uff0c\u5219\u4f1a\u53d1\u51fa\u5e26\u6709\u8be5\u5217\u4f4d\u7f6e\u7684\u901a\u77e5\u3002<\/li>\n<\/ul>\n<ol>\n<li><strong>\u8fd0\u884c\u533a\u5757\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u5728 postgresql \u67e5\u8be2\u5de5\u5177\uff08\u4f8b\u5982 pgadmin\u3001psql\uff09\u4e2d\u6267\u884c\u4e0a\u8ff0\u5757\u3002\u8fd9\u5c06\u6253\u5370\u51fa\u627e\u5230\u6307\u5b9a\u503c\u7684\u4f4d\u7f6e\u3002<\/p>\n<h3> \u5728 mysql \u4e2d\u641c\u7d22\u6570\u636e <\/h3>\n<p>mysql \u4e0d\u652f\u6301 pl\/pgsql \u98ce\u683c\u7684\u8fc7\u7a0b\u8bed\u8a00\u5757\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u751f\u6210\u5e76\u8fd0\u884c\u5fc5\u8981\u7684\u67e5\u8be2\u6216\u4f7f\u7528 shell \u811a\u672c\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<h4> \u5206\u6b65\u6307\u5357\uff1a <\/h4>\n<ol>\n<li><strong>\u751f\u6210\u641c\u7d22\u67e5\u8be2\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u4ee5\u4e0b sql \u67e5\u8be2\u5c06\u751f\u6210\u4e00\u4e2a\u67e5\u8be2\u5217\u8868\uff0c\u7528\u4e8e\u5728\u6307\u5b9a\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u7c7b\u578b\u4e3a varchar\u3001text \u6216 char \u7684\u5217\u4e2d\u641c\u7d22\u503c\u201cdcea8891-b4e1-45f8-8cb9-c8a164cb98ff\u201d\u3002<\/p>\n<pre>   select \n       concat('select ''', table_schema, '.', table_name, '.', column_name, \n              ''' as location, ', column_name, \n              ' from ', table_schema, '.', table_name, \n              ' where ', column_name, ' = ''', 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff', ''';') as search_query\n   from \n       information_schema.columns\n   where \n       table_schema = 'your_database_name' \n       and data_type in ('varchar', 'text', 'char');\n<\/pre>\n<p>\u5c06 your_database_name \u66ff\u6362\u4e3a\u60a8\u6570\u636e\u5e93\u7684\u5b9e\u9645\u540d\u79f0\u3002<\/p>\n<ol>\n<li><strong>\u624b\u52a8\u8fd0\u884c\u751f\u6210\u7684\u67e5\u8be2\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u590d\u5236\u4e0a\u8ff0\u67e5\u8be2\u7684\u8f93\u51fa\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>   select 'your_database_name.table1.column1' as location, column1 \n   from your_database_name.table1 \n   where column1 = 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff';\n\n   select 'your_database_name.table2.column2' as location, column2 \n   from your_database_name.table2 \n   where column2 = 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff';\n<\/pre>\n<p>\u5728 mysql \u5ba2\u6237\u7aef\u4e2d\u624b\u52a8\u6267\u884c\u6bcf\u4e2a\u67e5\u8be2\u3002<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 shell \u811a\u672c\u5b9e\u73b0\u81ea\u52a8\u5316\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u5982\u679c\u60a8\u6709\u6743\u8bbf\u95ee\u7c7b unix shell\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 shell \u811a\u672c\u81ea\u52a8\u5316\u8be5\u8fc7\u7a0b\uff1a<\/p>\n<pre>   #!\/bin\/bash\n\n   SEARCH_TEXT='dcea8891-b4e1-45f8-8cb9-c8a164cb98ff'\n   DATABASE='your_database_name'\n   USERNAME='your_username'\n   PASSWORD='your_password'\n\n   # Generate the search queries\n   QUERIES=$(mysql -u $USERNAME -p$PASSWORD -D $DATABASE -N -e \"\n     SELECT CONCAT('SELECT ''', table_schema, '.', table_name, '.', column_name, \n                   ''' AS location, ', column_name, \n                   ' FROM ', table_schema, '.', table_name, \n                   ' WHERE ', column_name, ' = ''', '$SEARCH_TEXT', ''';')\n     FROM information_schema.columns\n     WHERE table_schema = '$DATABASE' AND data_type IN ('varchar', 'text', 'char');\")\n\n   # Execute each query\n   while read -r QUERY; do\n     mysql -u $USERNAME -p$PASSWORD -D $DATABASE -e \"$QUERY\"\n   done &lt;&lt;&lt; \"$QUERIES\"\n<\/pre>\n<p>\u5c06 your_username\u3001your_password \u548c your_database_name \u66ff\u6362\u4e3a\u60a8\u7684\u5b9e\u9645 mysql \u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u79f0\u3002\u8be5\u811a\u672c\u5c06\u751f\u6210\u5fc5\u8981\u7684\u641c\u7d22\u67e5\u8be2\uff0c\u7136\u540e\u6267\u884c\u6bcf\u4e2a\u67e5\u8be2\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<h3> \u7ed3\u8bba <\/h3>\n<p>\u65e0\u8bba\u60a8\u4f7f\u7528 postgresql \u8fd8\u662f mysql\uff0c\u60a8\u90fd\u53ef\u4ee5\u901a\u8fc7\u5229\u7528 postgresql \u4e2d\u7684\u8fc7\u7a0b\u8bed\u8a00\u5757\u6216\u5728 mysql \u4e2d\u751f\u6210\u548c\u6267\u884c\u52a8\u6001\u67e5\u8be2\uff0c\u5728\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u8868\u548c\u5217\u4e2d\u9ad8\u6548\u641c\u7d22\u7279\u5b9a\u503c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5728\u7ba1\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u4e3a\u60a8\u8282\u7701\u5927\u91cf\u65f6\u95f4\u548c\u7cbe\u529b\u3002<\/p>\n<p>\u5bf9\u4e8e postgresql\uff0cpl\/pgsql \u5757\u4e3a\u81ea\u52a8\u641c\u7d22\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e mysql\uff0c\u867d\u7136\u8fc7\u7a0b\u8bed\u8a00\u5757\u4e0d\u53ef\u7528\uff0c\u4f46\u624b\u52a8\u6216\u901a\u8fc7 shell \u811a\u672c\u751f\u6210\u548c\u8fd0\u884c\u52a8\u6001\u67e5\u8be2\u53ef\u4ee5\u8fbe\u5230\u76f8\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u901a\u8fc7\u5229\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u60a8\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5e93\u7ba1\u7406\u4efb\u52a1\u5e76\u5feb\u901f\u627e\u5230\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5728 PostgreSQL \u548c MySQL \u4e2d\u7684\u6240\u6709\u8868\u4e2d\u67e5\u627e\u7279\u5b9a\u6570\u636e\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6570\u636e\u5e93\u77e5\u8bc6\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664 <\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u548c M&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200120","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200120"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200120\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}