2

I need to generate CSV files of DESC TableName for few hundred tables in a database.

Could you please help me with it guys? I am sure there is a way using Bash script. I tried using mysqldump but it is for data dump and sql dump of structure :(

Thank you!

1 Answer 1

2

On MySQL 5.0 on onwards, you can use the information_schema (http://dev.mysql.com/doc/refman/5.0/en/information-schema.html)

Example:

SELECT      CASE ordinal_position 
                WHEN 1 THEN CONCAT('Table ', table_schema, '.', table_name, '\n')
                ELSE ''
            END
,           column_name
,           column_type
,           is_nullable
,           column_key
,           column_default
,           extra
FROM        information_schema.columns
WHERE       table_schema = SCHEMA()
ORDER BY    table_schema
,           table_name
,           ordinal_position

The output here should be equivalent (but not identical) to what you get from DESC

Note that in this case, the query only reports the tables from the current database. Tweak the WHERE clause to fit your requirements.

Also a word of warning: these queries can be very slow, esp. if they run the first time.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow! Simply superb! An awesome solution, I wish I knew more about Info. Schema before. Thanks a lot Roland. You just saved me tonnes of work and I hate doing manual tasks! Thanks again :)
Table_name fix : CONCAT(table_schema, '.', table_name) Roland's solution rocks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.