当前位置:网站首页>Summary of three methods for MySQL to view table structure

Summary of three methods for MySQL to view table structure

2022-07-06 23:19:00 1024 questions

Catalog

mysql View table structure

1. show create table

2. desc

3. information_schema.COLUMNS

Get all the table structures and comments

Export all table information according to the library name

Export all table names and table comments according to the library name

mysql Get all tables of the entire library , And table structure

mysql View table structure 1. show create tableshow create table The name of the table

This sentence comes up instinctively every time , Semantic memorization , You can directly view the table creation statement , But it is not convenient for code processing .

2. descdesc The name of the table

This statement is easy to use , Can be obtained COLUMNS The more important fields in : name 、 type 、 Is it empty 、 key 、 The default value is 、 Additional information . But cannot get the comment of the field .

3. information_schema.COLUMNSselect * from information_schema.COLUMNS where TABLE_SCHEMA = ' Library name ' and TABLE_NAME = ' The name of the table '

relative show create table for , Relatively simple , Comprehensive information , You can query the name of the field 、 type 、 key 、 jurisdiction 、 Notes and other information . To get the field name , So I use this as a method to get the table structure in my script .

attach :information_schema.COLUMNS Fields and meanings

Field name and meaning

TABLE_CATALOG Table type ( I didn't understand what to do ?)

TABLE_SCHEMA Library name

TABLE_NAME The name of the table

COLUMN_NAME Field name

ORDINAL_POSITION Location number

COLUMN_DEFAULT The default value is

IS_NULLABLE Can it be empty

DATA_TYPE data type

CHARACTER_MAXIMUM_LENGTH Maximum string length ( The value type is empty )

CHARACTER_OCTET_LENGTH Maximum storage length of string ( It is generally the same as the previous field )

NUMERIC_PRECISION Numerical accuracy ( Non numeric type is empty )

NUMERIC_SCALE Number of decimal places ( Non numeric type is empty )

DATETIME_PRECISION Date precision

CHARACTER_SET_NAME Encoding mode

COLLATION_NAME sort order

COLUMN_TYPE Field type

COLUMN_KEY Fields involved key( Primary key 、 Unique key, etc )

EXTRA other ( Such as auto_increment)

PRIVILEGES jurisdiction

COLUMN_COMMENT Annotation fields

GENERATION_EXPRESSION Represents the expression ( I don't understand ,mysql Table inheritance ?)

Get all the table structures and comments Export all table information according to the library name SELECT    *FROM    information_schema.`TABLES`WHERE    TABLE_SCHEMA = 'db_name' Export all table names and table comments according to the library name SELECT    TABLE_NAME,    TABLE_COMMENTFROM    information_schema.`TABLES`WHERE    TABLE_SCHEMA = 'db_name';mysql Get all tables of the entire library , And table structure SELECT    TABLE_SCHEMA AS ' Library name ',    TABLE_NAME AS ' Table name ',    COLUMN_NAME AS ' Name ',    ORDINAL_POSITION AS ' Order of columns ',    COLUMN_DEFAULT AS ' The default value is ',    IS_NULLABLE AS ' Is it empty ',    DATA_TYPE AS ' data type ',    CHARACTER_MAXIMUM_LENGTH AS ' Maximum character length ',    NUMERIC_PRECISION AS ' Numerical accuracy ( Maximum number )',    NUMERIC_SCALE AS ' Decimal precision ',    COLUMN_TYPE AS ' Column type ',    COLUMN_KEY 'KEY',    EXTRA AS ' Additional instructions ',    COLUMN_COMMENT AS ' notes 'FROM    information_schema.`COLUMNS`WHERE    TABLE_SCHEMA = 'db_name'ORDER BY    TABLE_NAME,    ORDINAL_POSITION;

The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .


原网站

版权声明
本文为[1024 questions]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061505471427.html

随机推荐