How to get database structure in MySQL via query

前端 未结 10 1286
旧时难觅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条回答
  • 2020-11-27 11:54

    To get the whole database structure as a set of CREATE TABLE statements, use mysqldump:

    mysqldump database_name --compact --no-data
    

    For single tables, add the table name after db name in mysqldump. You get the same results with SQL and SHOW CREATE TABLE:

    SHOW CREATE TABLE table;
    

    Or DESCRIBE if you prefer a column listing:

    DESCRIBE table;
    
    0 讨论(0)
  • 2020-11-27 11:57

    using this:

    SHOW CREATE TABLE `users`;
    

    will give you the DDL for that table

    DESCRIBE `users`
    

    will list the columns in that table

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

    Take a look at the INFORMATION_SCHEMA.TABLES table. It contains metadata about all your tables.

    Example:

    SELECT * FROM `INFORMATION_SCHEMA`.`TABLES`
    WHERE TABLE_NAME LIKE 'table1'
    

    The advantage of this over other methods is that you can easily use queries like the one above as subqueries in your other queries.

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

    Nowadays, people use DESC instead of DESCRIPTION. For example:- DESC users;

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