查询mysql数据库中所有表名

查询mysql数据库中所有表名,第1张

查找所有表的语句

select table_name

from information_schematables

where table_schema='当前数据库'

mysql>  use mysql

Database changed

mysql> show tables;

+---------------------------+

| Tables_in_mysql           |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_category             |

| help_keyword              |

| help_relation             |

| help_topic                |

| innodb_index_stats        |

| innodb_table_stats        |

| ndb_binlog_index          |

| plugin                    |

| proc                      |

| procs_priv                |

| proxies_priv              |

| servers                   |

| slave_master_info         |

| slave_relay_log_info      |

| slave_worker_info         |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

28 rows in set (005 sec)

show tables即为显示当前数据库中所有的表。

根据具体问题类型,进行步骤拆解/原因原理分析/内容拓展等。

具体步骤如下:/导致这种情况的原因主要是……

TABLE 语句

具体语法:TABLE table_name [ORDER BY column_name] [LIMIT number [OFFSET number]]

其实从语法上看,可以排序,也可以过滤记录集,不过比较简单,没有 SELECT 那么强大。

示例 1

简单的建一张很小的表 y1,记录数为 10 条。表 t1,插入 10 条记录

mysql-(ytt/3305)->create table t1 (r1 int,r2 int);

Query OK, 0 rows affected (002 sec)

mysql-(ytt/3305)->insert into t1

with recursive aa(a,b) as (

select 1,1

union all

select a+1,ceil(rand()20) from aa where a < 10

) select from aa;

Query OK, 10 rows affected (000 sec)

Records: 10  Duplicates: 0  Warnings: 0

简单全表扫描mysql-(ytt/3305)->select from t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (000 sec)

TABLE 结果mysql-(ytt/3305)->table t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (000 sec)

看下 table 的执行计划mysql-(ytt/3305)->explain table t1 order by r1 limit 2\G 1 row           id: 1  select_type: SIMPLE        table: t1   partitions: NULL         type: ALLpossible_keys: NULL          key: NULL      key_len: NULL          ref: NULL         rows: 10     filtered: 10000        Extra: Using filesort1 row in set, 1 warning (000 sec)

其实可以看到 TABLE 内部被 MySQL 转换为 SELECT 了。mysql-(ytt/3305)->show warnings\G 1 row  Level: Note   Code: 1003Message: / select#1 / select `ytt``t1``r1` AS `r1`,`ytt``t1``r2` AS `r2` from `ytt``t1` order by `ytt``t1``r1` limit 21 row in set (000 sec)

那其实从上面简单的例子可以看到 TABLE 在内部被转成了普通的 SELECT 来处理。示例 2应用于子查询里的子表。这里要注意,内表的字段数量必须和外表过滤的字段数量一致。克隆表 t1 结构mysql-(ytt/3305)->create table t2 like t1;Query OK, 0 rows affected (002 sec)

克隆表 t1 数据mysql-(ytt/3305)->insert into t2 table t1;Query OK, 10 rows affected (000 sec)Records: 10  Duplicates: 0  Warnings: 0

table t1 被当做内表,表 t1 有两个字段,必须同时满足 t2 检索时过滤的字段也是两个。mysql-(ytt/3305)->select from t2 where (r1,r2) in (table t1);+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (000 sec)

注意:这里如果过滤的字段数量和子表数量不一致,则会报错。

mysql里面提供了很多方法来获取表结构和表列:如下方法

获得某表所有列的信息:

String sql = select from tname;//tname为某一表名

Connection conn = ;

Statement st = conncreateStatement();

ResultSet rs = strs = stexecuteQuery(sql);

ResultSetMetaData rsmd = rsgetMetaData();

int colcount = rsmdgetColumnCount();//取得全部列数

for(int i=0;i<colcount;i++){

String colname = rsmdgetColumnName(i);//取得全部列名

}

以上为某表字段具体查询,如果是查询表的信息,如在mysql服务器上那样的查询结果的话,可以用一下代码:

ResultSetexecuteQuery("show tables")可以的到所有的表信息。

ResultSetexecuteQuery("describe tname")可以得到表的字段信息。//tname为表名

首先进入bbs数据库中

use bbs

然后执行命令

show tables

这样会列出bbs数据库中的所有表

知道表了以后

select from 表名;

这样就可以查询bbs数据库中所有表的数据了

<php

    $link = mysql_connect('你的主机名','用户名','密码');

    mysql_select_db('数据库名');

    

    $sql = "select  from 你的表名";

    $res = mysql_query('$sql')

    $row = mysql_fetch_assoc($res);

    

    echo $row['字段名'];

>

以上就是关于查询mysql数据库中所有表名全部的内容,包括:查询mysql数据库中所有表名、怎样用SQL语句查询一个数据库中的所有表、java 获取mysql 某个数据库中所有表及表的列的信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10157691.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存