Query mysql table structure related sql

  1. /**View table structure**/  
  2. desc  yourtablename  
  3. /**View the create table statement**/  
  4. show createtable yourtablename   
  5. /**View all column information**/  
  6. use information_schema;  
  7. select * from columns where table_name='yourtablename';  
  8. /**View information about all column names**/  
  9. use information_schema;  
  10. select column_name from columns where table_name='yourtablename';  
  11. /** Concatenate column names to predefined sql**/  
  12. select concat('insert into yourtablename values(',r.column_name) from   
  13. (select group_concat(column_name) column_name from columns where table_name='yourtablename') r;  
  14. /**Query all table names and comments in mysql that contain the specified column**/  
  15. useinformation_schema;  
  16. selectdistinctc.table_name,t.TABLE_COMMENTfromcolumnscleftjointablest  
  17. onc.table_name=t.TABLE_NAME  
  18. wherec.TABLE_SCHEMA= 'database' /**Database name*/  
  19. andc.COLUMN_NAME= 'password' /**column name*/  
  20. andc.DATA_TYPE= 'int' /**Data column type*/  

 

 

View to get the field comments in the table:
> show full columns from tablename;
or 
show full fields from tablename;
or, in the metadata table, see
Select COLUMN_NAME column name, DATA_TYPE field type, COLUMN_COMMENT field comment
from INFORMATION_SCHEMA.COLUMNS
Where table_name = 'companies'##Table name
AND table_schema = 'testhuicard'##Database nameAND
column_name LIKE 'c_name'##Field name


2-1 How to view table comments:
> show create table tablename;

2-2 Get the entire database All table information (including table name, table comment, table type, etc.):
> SELECT table_name, table_type, engine
-> FROM information_schema.tables
-> WHERE table_schema = 'db5' //table_schema is the database name
-> ORDER BY table_name DESC ;
//This statement requests to list all tables in database db5 in reverse alphabetical order, but only displays three kinds of information: table name, table type, and table engine.
INFORMATION_SCHEMA is an information database that holds information about all other databases maintained by the MySQL server.

> SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sh_goods' AND TABLE_SCHEMA = 'sh_shop';//Get the comments of the sh_goods table in the sh_shop database .

2-3 Get the table comment or
Or use: show table status;
Comment is the table comment.

Extension:
Modify table comments:
alter table test1 comment 'Modified table comments';

Modify field comments:
alter table test1 modify column field_name int comment 'Modified field comments';  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326262822&siteId=291194637
Recommended