From the MySQL console, what command displays the schema of any given table?
            Asked
            
        
        
            Active
            
        
            Viewed 6e+01k times
        
    5 Answers
648
            For formatted output:
describe [db_name.]table_name;
For an SQL statement that can be used to create a table:
show create table [db_name.]table_name;
 
    
    
        Omry Yadan
        
- 31,280
- 18
- 64
- 87
- 
                    4This is not the answer. The OP is looking to figure out the "db_name" part, and this solution assumes you already know that – Antonio Ortells Dec 20 '16 at 21:36
- 
                    60the OP accepted that answer 7 years ago. I think he was happy with that answer. – Omry Yadan Dec 21 '16 at 22:36
- 
                    
- 
                    4
- 
                    2In Antionio's defense, some systems refer to their collections of related tables as schemas and not databases. Not sure which or why. – dlamblin Mar 15 '18 at 04:15
- 
                    1
- 
                    Syntax and how to use from official docs: https://dev.mysql.com/doc/refman/8.0/en/explain.html – Enrique René Mar 20 '19 at 11:49
- 
                    
23
            
            
        You can also use shorthand for describe as desc for table description.
desc [db_name.]table_name;
or
use db_name;
desc table_name;
You can also use explain for table description.
explain [db_name.]table_name;
See official doc
Will give output like:
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(10)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| age      | int(10)     | YES  |     | NULL    |       |
| sex      | varchar(10) | YES  |     | NULL    |       |
| sal      | int(10)     | YES  |     | NULL    |       |
| location | varchar(20) | YES  |     | Pune    |       |
+----------+-------------+------+-----+---------+-------+
 
    
    
        Somnath Muluk
        
- 55,015
- 38
- 216
- 226
18
            
            
        Perhaps the question needs to be slightly more precise here about what is required because it can be read it two different ways. i.e.
- How do I get the structure/definition for a table in mysql?
- How do I get the name of the schema/database this table resides in?
Given the accepted answer, the OP clearly intended it to be interpreted the first way. For anybody reading the question the other way try
SELECT `table_schema` 
FROM `information_schema`.`tables` 
WHERE `table_name` = 'whatever';
 
    
    
        Paul Campbell
        
- 1,906
- 2
- 12
- 19
- 
                    Your answer for the second point helped me for what I'm looking. My editor thrown the error saying "Invalid Object" on the table I tried to use and I have no clue about the DB or Schema under which the table falls in as the table is already created by someone else already, until I got this information – Suresh Nov 24 '17 at 16:13
 
     
     
    