1. 程式人生 > >mysql查看錶結構資訊

mysql查看錶結構資訊

需求背景是給一個表名然後給出相應的表結構資訊及索引資訊
常用的命令有如下:
1. desc tableName; desc employees.employees;
2. show columns from tableName; show COLUMNS from employees.employees;
3. describe tableName; DESCRIBE employees.employees;
這三個顯示的結果都是一樣的,顯示錶中filed,type,null,key,default及extra。
4. show create table tableName; show CREATE TABLE employees.employees;
這個語句會顯示這個表的建表語句。
5. select * from columns where table_name='表名';select * from information_schema.COLUMNS where TABLE_SCHEMA='employees' and TABLE_NAME='employees';
這個顯示的結果就比較全了。
接下來,來點更全的sql,這個是用來同步mysql和orac資料字典的所有sql。
mysql部分:

 1 ## 檢視所有的庫
 2 SELECT
 3     lower(schema_name) schema_name
 4 FROM
 5     information_schema.schemata
 6 WHERE
 7     schema_name NOT IN (
 8         'mysql',
 9         'information_schema',
10         'test',
11         'search',
12         'tbsearch',
13         'sbtest',
14         'dev_ddl'
15     )
16  
17 ## 產看某一個庫中的所有表
18 SELECT
19     table_name,
20     create_time updated_at,
21     table_type,
22     ENGINE,
23     table_rows num_rows,
24     table_comment,
25     ceil(data_length / 1024 / 1024) store_capacity
26 FROM
27     information_schema.TABLES
28 WHERE
29     table_schema = 'employees'
30 AND table_name NOT LIKE 'tmp#_%' ESCAPE '#'
31  
32 ##檢視某一個庫下某一個表的所有欄位
33 SELECT
34     lower(column_name) column_name,
35     ordinal_position position,
36     column_default dafault_value,
37     substring(is_nullable, 1, 1) nullable,
38     column_type data_type,
39     column_comment,
40     character_maximum_length data_length,
41     numeric_precision data_precision,
42     numeric_scale data_scale
43 FROM
44     information_schema.COLUMNS
45 WHERE
46     table_schema = 'employees'
47 AND table_name = 'employees';
48  
49  
50 ## 檢視某一個庫下某一張表的索引
51  
52 SELECT DISTINCT
53     lower(index_name) index_name,
54     lower(index_type) type
55 FROM
56     information_schema.statistics
57 WHERE
58     table_schema = 'employees'
59 AND table_name = 'employees';
60  
61 ## 檢視某一個庫下某一張表的某一個索引
62  
63 SELECT
64     lower(column_name) column_name,
65     seq_in_index column_position
66 FROM
67     information_schema.statistics
68 WHERE
69     table_schema = 'employees'
70 AND table_name = 'employees'
71 AND index_name = 'primary';
72  
73 ## 檢視某一個庫下某一個表的註釋
74 SELECT
75     table_comment comments
76 FROM
77     information_schema.TABLES
78 WHERE
79     table_schema = 'employees'
80 AND table_name = 'employees';
81  
82 ## 檢視某一個庫下某一個表的列的註釋
83 SELECT
84     lower(column_name) column_name,
85     column_comment comments
86 FROM
87     COLUMNS
88 WHERE
89     table_schema = 'employees'
90 AND table_name = 'employees';