{"id":186172,"date":"2024-11-11T03:42:16","date_gmt":"2024-11-10T19:42:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/186172\/"},"modified":"2024-11-11T03:42:17","modified_gmt":"2024-11-10T19:42:17","slug":"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8%e7%9a%84%e5%88%a4%e6%96%b7%e6%96%b9%e6%b3%95%e4%bb%8b%e7%b4%b9%ef%bc%88%e5%9c%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/186172\/","title":{"rendered":"\u6578\u64da\u5eab\u8868\u662f\u5426\u5b58\u5728\u7684\u5224\u65b7\u65b9\u6cd5\u4ecb\u7d39\uff08\u5728\u6578\u64da\u5eab\u4e2d\u8868\u662f\u5426\u5b58\u5728\u55ce\uff09"},"content":{"rendered":"<h1 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8%e7%9a%84%e5%88%a4%e6%96%b7%e6%96%b9%e6%b3%95%e4%bb%8b%e7%b4%b9-RrYYBRWtuY\">\u6578\u64da\u5eab\u8868\u662f\u5426\u5b58\u5728\u7684\u5224\u65b7\u65b9\u6cd5\u4ecb\u7d39<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0c\u78ba\u8a8d\u67d0\u500b\u8868\u662f\u5426\u5b58\u5728\u662f\u975e\u5e38\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u907f\u514d\u5728\u67e5\u8a62\u6216\u64cd\u4f5c\u6578\u64da\u6642\u51fa\u73fe\u932f\u8aa4\uff0c\u9084\u80fd\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5e7e\u7a2e\u5e38\u898b\u7684\u5224\u65b7\u6578\u64da\u5eab\u8868\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8-sql-%e6%9f%a5%e8%a9%a2-RrYYBRWtuY\">1. \u4f7f\u7528 SQL \u67e5\u8a62<\/h2>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 SQL \u67e5\u8a62\u4f86\u6aa2\u67e5\u8868\u7684\u5b58\u5728\u6027\u3002\u4e0d\u540c\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u6709\u4e0d\u540c\u7684\u8a9e\u6cd5\uff0c\u4ee5\u4e0b\u662f\u5e7e\u500b\u5e38\u898b\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"1-1-mysql-RrYYBRWtuY\">1.1 MySQL<\/h3>\n<pre><code>SELECT COUNT(*)\nFROM information_schema.tables \nWHERE table_schema = 'your_database_name' \nAND table_name = 'your_table_name';<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c`information_schema.tables` \u662f\u4e00\u500b\u7cfb\u7d71\u8868\uff0c\u5305\u542b\u4e86\u6240\u6709\u6578\u64da\u5eab\u7684\u8868\u4fe1\u606f\u3002\u901a\u904e\u67e5\u8a62\u9019\u500b\u8868\uff0c\u6211\u5011\u53ef\u4ee5\u78ba\u8a8d\u6307\u5b9a\u7684\u8868\u662f\u5426\u5b58\u5728\u3002<\/p>\n<h3 id=\"1-2-postgresql-RrYYBRWtuY\">1.2 PostgreSQL<\/h3>\n<pre><code>SELECT EXISTS (\n  SELECT 1 \n  FROM information_schema.tables \n  WHERE table_schema = 'public' \n  AND table_name = 'your_table_name'\n);<\/code><\/pre>\n<p>PostgreSQL \u63d0\u4f9b\u4e86 `EXISTS` \u51fd\u6578\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u544a\u8a34\u6211\u5011\u8868\u662f\u5426\u5b58\u5728\u3002<\/p>\n<h3 id=\"1-3-sql-server-RrYYBRWtuY\">1.3 SQL Server<\/h3>\n<pre><code>IF OBJECT_ID('your_database_name.dbo.your_table_name', 'U') IS NOT NULL\nBEGIN\n    PRINT 'Table exists.'\nEND\nELSE\nBEGIN\n    PRINT 'Table does not exist.'\nEND;<\/code><\/pre>\n<p>\u5728 SQL Server \u4e2d\uff0c`OBJECT_ID` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u5c0d\u8c61\u7684\u5b58\u5728\u6027\uff0c\u9019\u88e1\u7684 &#8216;U&#8217; \u4ee3\u8868\u7528\u6236\u8868\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8-orm-%e6%a1%86%e6%9e%b6-RrYYBRWtuY\">2. \u4f7f\u7528 ORM \u6846\u67b6<\/h2>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\uff0c\u5982 Hibernate \u6216 Entity Framework\uff0c\u9019\u4e9b\u6846\u67b6\u901a\u5e38\u63d0\u4f9b\u4e86\u65b9\u6cd5\u4f86\u6aa2\u67e5\u8868\u7684\u5b58\u5728\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<h3 id=\"2-1-hibernate-RrYYBRWtuY\">2.1 Hibernate<\/h3>\n<pre><code>Session session = sessionFactory.openSession();\nboolean exists = session.createSQLQuery(\"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'your_table_name'\").uniqueResult() &gt; 0;<\/code><\/pre>\n<h3 id=\"2-2-entity-framework-RrYYBRWtuY\">2.2 Entity Framework<\/h3>\n<pre><code>using (var context = new YourDbContext())\n{\n    bool exists = context.Database.SqlQuery&lt;int&gt;(\"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'your_table_name'\").Single() &gt; 0;\n}<\/code><\/pre>\n<p>\u9019\u4e9b\u65b9\u6cd5\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u76f4\u63a5\u7de8\u5beb SQL \u8a9e\u53e5\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e7%ae%a1%e7%90%86%e5%b7%a5%e5%85%b7-RrYYBRWtuY\">3. \u4f7f\u7528\u6578\u64da\u5eab\u7ba1\u7406\u5de5\u5177<\/h2>\n<p>\u8a31\u591a\u6578\u64da\u5eab\u7ba1\u7406\u5de5\u5177\uff08\u5982 phpMyAdmin\u3001SQL Server Management Studio \u7b49\uff09\u4e5f\u63d0\u4f9b\u4e86\u53ef\u8996\u5316\u7684\u754c\u9762\u4f86\u6aa2\u67e5\u8868\u7684\u5b58\u5728\u6027\u3002\u7528\u6236\u53ef\u4ee5\u901a\u904e\u9019\u4e9b\u5de5\u5177\u8f15\u9b06\u5730\u67e5\u770b\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709\u8868\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"4-%e5%b0%8f%e7%b5%90-RrYYBRWtuY\">4. \u5c0f\u7d50<\/h2>\n<p>\u78ba\u8a8d\u6578\u64da\u5eab\u8868\u662f\u5426\u5b58\u5728\u662f\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\u7684\u4e00\u500b\u57fa\u672c\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u901a\u904e SQL \u67e5\u8a62\u3001ORM \u6846\u67b6\u9084\u662f\u6578\u64da\u5eab\u7ba1\u7406\u5de5\u5177\uff0c\u958b\u767c\u8005\u90fd\u53ef\u4ee5\u6839\u64da\u81ea\u5df1\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u9084\u80fd\u6e1b\u5c11\u56e0\u8868\u4e0d\u5b58\u5728\u800c\u5c0e\u81f4\u7684\u932f\u8aa4\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7d39\u5982\u4f55\u5224\u65b7\u6578\u64da\u5eab\u4e2d\u8868\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u6709\u6548\u7ba1\u7406\u6578\u64da\u5eab\u7d50\u69cb\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-186172","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186172","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=186172"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186172\/revisions"}],"predecessor-version":[{"id":186173,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/186172\/revisions\/186173"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=186172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=186172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=186172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}