How to get database structure in MySQL via query

前端 未结 10 1285
旧时难觅i
旧时难觅i 2020-11-27 11:37

Is it possible to somehow get structure of MySQL database, or just some table with simple query?

Or is there another way, how can I do it?

相关标签:
10条回答
  • A variation of the first answer that I found useful

    Open your command prompt and enter (you dont have to be logged into your mysql server)

    mysqldump -hlocalhost -u<root> -p<password>  <dbname>  --compact --no-data > </path_to_mydump/>mysql.dmp
    
    0 讨论(0)
  • 2020-11-27 11:39

    That's the SHOW CREATE TABLE query. You can query the SCHEMA TABLES, too.

    SHOW CREATE TABLE YourTableName;
    
    0 讨论(0)
  • 2020-11-27 11:41
    SELECT *
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME ='products'; 
    

    where Table_schema is database name

    0 讨论(0)
  • 2020-11-27 11:42

    I think that what you're after is DESCRIBE

    DESCRIBE table;
    

    You can also use SHOW TABLES

    SHOW TABLES;
    

    to get a list of the tables in your database.

    0 讨论(0)
  • 2020-11-27 11:44

    In the following example,

    playground is the database name and equipment is the table name

    Another way is using SHOW-COLUMNS:5.5 (available also for 5.5>)

    $ mysql -uroot -p<password> -h<host> -P<port> -e \
        "SHOW COLUMNS FROM playground.equipment"
    

    And the output:

    mysql: [Warning] Using a password on the command line interface can be insecure.
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
    | type  | varchar(50) | YES  |     | NULL    |                |
    | quant | int(11)     | YES  |     | NULL    |                |
    | color | varchar(25) | YES  |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    

    One can also use mysqlshow-client (also available for 5.5>) like following:

    $ mysqlshow -uroot -p<password> -h<host> -P<port> \
        playground equipment
    

    And the output:

    mysqlshow: [Warning] Using a password on the command line interface can be insecure.
    Database: playground  Table: equipment
    +-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
    | Field | Type        | Collation         | Null | Key | Default | Extra          | Privileges                      | Comment |
    +-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
    | id    | int(11)     |                   | NO   | PRI |         | auto_increment | select,insert,update,references |         |
    | type  | varchar(50) | latin1_swedish_ci | YES  |     |         |                | select,insert,update,references |         |
    | quant | int(11)     |                   | YES  |     |         |                | select,insert,update,references |         |
    | color | varchar(25) | latin1_swedish_ci | YES  |     |         |                | select,insert,update,references |         |
    +-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
    
    0 讨论(0)
  • 2020-11-27 11:45

    SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='bodb' AND TABLE_NAME='abc';

    works for getting all column names

    0 讨论(0)
提交回复
热议问题