mysql数据字典代码

平时做开发时,查看mysql的表是用了navicat,虽然查看单个表的各个字段时还算方便,但是要一次查看整个数据库各个表的各个字段的详情还是不那么方便,于是就在网上找了一段代码,把数据库的所有表的字段都显示出来,这样查看结构会比较清晰。

显示效果

 1 <?php
 2 /**
 3  * 生成mysql数据字典
 4  */
 5 header ( "Content-type: text/html; charset=utf-8" );
 6  
 7 // 配置数据库
 8 $dbserver = "localhost";//数据库IP地址
 9 $dbusername = "root";//数据库用户名
10 $dbpassword = "";//数据库密码
11 $database =  "test";//数据库名称
12  
13 // 其他配置
14 $title = '数据字典';
15  
16 $mysql_conn = @mysql_connect ( "$dbserver", "$dbusername", "$dbpassword" ) or die ( "Mysql connect is error." );
17 mysql_select_db ( $database, $mysql_conn );
18 mysql_query ( 'SET NAMES utf8', $mysql_conn );
19 $table_result = mysql_query ( 'show tables', $mysql_conn );
20 // 取得所有的表名
21 while ( $row = mysql_fetch_array ( $table_result ) ) {
22     $tables [] ['TABLE_NAME'] = $row [0];
23 }
24  
25 // 循环取得所有表的备注及表中列消息
26 foreach ( $tables as $k => $v ) {
27     $sql = 'SELECT * FROM ';
28     $sql .= 'INFORMATION_SCHEMA.TABLES ';
29     $sql .= 'WHERE ';
30     $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";
31     $table_result = mysql_query ( $sql, $mysql_conn );
32     while ( $t = mysql_fetch_array ( $table_result ) ) {
33         $tables [$k] ['TABLE_COMMENT'] = $t ['TABLE_COMMENT'];
34     }
35      
36     $sql = 'SELECT * FROM ';
37     $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
38     $sql .= 'WHERE ';
39     $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
40      
41     $fields = array ();
42     $field_result = mysql_query ( $sql, $mysql_conn );
43     while ( $t = mysql_fetch_array ( $field_result ) ) {
44         $fields [] = $t;
45     }
46     $tables [$k] ['COLUMN'] = $fields;
47 }
48 mysql_close ( $mysql_conn );
49  
50 $html = '';
51 // 循环所有表
52 foreach ( $tables as $k => $v ) {
53     // $html .= '<p><h2>'. $v['TABLE_COMMENT'] . '&nbsp;</h2>';
54     $html .= '<table  border="1" cellspacing="0" cellpadding="0" align="center">';
55     $html .= '<caption>' . $v ['TABLE_NAME'] . '  ' . $v ['TABLE_COMMENT'] . '</caption>';
56     $html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
57     <th>允许非空</th>
58     <th>自动递增</th><th>备注</th></tr>';
59     $html .= '';
60      
61     foreach ( $v ['COLUMN'] as $f ) {
62         $html .= '<tr><td class="c1">' . $f ['COLUMN_NAME'] . '</td>';
63         $html .= '<td class="c2">' . $f ['COLUMN_TYPE'] . '</td>';
64         $html .= '<td class="c3">&nbsp;' . $f ['COLUMN_DEFAULT'] . '</td>';
65         $html .= '<td class="c4">&nbsp;' . $f ['IS_NULLABLE'] . '</td>';
66         $html .= '<td class="c5">' . ($f ['EXTRA'] == 'auto_increment' ? '是' : '&nbsp;') . '</td>';
67         $html .= '<td class="c6">&nbsp;' . $f ['COLUMN_COMMENT'] . '</td>';
68         $html .= '</tr>';
69     }
70     $html .= '</tbody></table></p>';
71 }
72  
73 // 输出
74 echo '<html>
75 <head>
76 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
77 <title>' . $title . '</title>
78 <style>
79 body,td,th {font-family:"宋体"; font-size:12px;}
80 table{border-collapse:collapse;border:1px solid #CCC;background:#6089D4;}
81 table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
82 table th{text-align:left; font-weight:bold;height:26px; line-height:25px; font-size:16px; border:3px solid #fff; color:#ffffff; padding:5px;}
83 table td{height:25px; font-size:12px; border:3px solid #fff; background-color:#f0f0f0; padding:5px;}
84 .c1{ width: 150px;}
85 .c2{ width: 130px;}
86 .c3{ width: 70px;}
87 .c4{ width: 80px;}
88 .c5{ width: 80px;}
89 .c6{ width: 300px;}
90 </style>
91 </head>
92 <body>';
93 echo '<h1 style="text-align:center;">' . $title . '</h1>';
94 echo $html;
95 echo '</body></html>';
96  
97 ?>

 

转载于:https://my.oschina.net/zhaoxiuliang/blog/1523277

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用Mysql提供的信息模式(Information Schema)来获取数据库数据字典信息。Information Schema是Mysql提供的一个元数据数据库,包含了所有数据库的元数据信息,如表、列、索引、触发器等。 以下是使用Java代码获取Mysql数据库数据字典信息的示例: ```java import java.sql.*; public class MysqlDictionary { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"; String username = "root"; String password = "root"; Connection conn = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(url, username, password); DatabaseMetaData metaData = conn.getMetaData(); rs = metaData.getColumns(null, null, "test_table", null); while (rs.next()) { String columnName = rs.getString("COLUMN_NAME"); String columnType = rs.getString("TYPE_NAME"); int columnSize = rs.getInt("COLUMN_SIZE"); System.out.println("列名:" + columnName + ", 类型:" + columnType + ", 大小:" + columnSize); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 上述示例中,我们使用了Mysql的getColumns方法来获取test数据库中test_table表的所有列信息,并输出列名、类型和大小。其他的元数据信息,如索引、触发器等,可以通过类似的方式获取。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值