【问题标题】:MySQL query to check whether a table is in the database or notMySQL查询以检查表是否在数据库中
【发布时间】:2014-03-02 09:32:29
【问题描述】:

我想检查一个表是否在数据库中。那么什么是 MySQL 查询来检查一个表是否在数据库中?

【问题讨论】:

标签: mysql


【解决方案1】:

您也应该尝试使用 database_name 和 table_name..

SELECT count(*)
FROM information_schema.tables
WHERE table_schema = 'db_name'
AND table_name = 'table_name'

如果存在则返回 1,否则返回 0

【讨论】:

  • 这是一个更好的主意。我也有一个疑问。什么是“information_schema.tables”?
  • information_schema 是我自己创建的 mysql 数据库,用于保存有关其他数据库和表的信息。 tablesinformation_schema 中的一个表,它保存有关表的信息。..
  • 我使用这样的代码:preparedStatement = connect.prepareStatement("select count(*) from information_schema.tables where table_schema=mysql and table_name=addstudent");返回值将存储在哪里?
  • 它的语句..它不是查询的执行..你只是准备了语句..接下来你需要执行查询..然后只有它会返回值..它会正常运行表结果..
  • 您必须在 Java 中获取 ResultSet 的结果.. atully .executeQuery() 将结果集返回给您。 ResultSet rs = PreparedStatement.executeQuery(); 然后尝试 rs.next();int exists = rs.getInt(1);现在如果存在=1 表示表存在
【解决方案2】:
SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'MyDataBase'

【讨论】:

  • 您能解释一下“information_schema.TABLES”和“TABLE SCHEMA”是什么吗?
  • infomation_schema 是主数据库,tables 是包含所有表的列表的表。 table_schema 是您感兴趣的数据库。即:您自己的数据库。
【解决方案3】:
SELECT count(*)  FROM information_schema.TABLES where TABLE_SCHEMA='dbname' and table_name='tblname'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 2013-04-12
    • 2018-03-05
    相关资源
    最近更新 更多