mysqldump -h localhost -u root -p --no-data --compact some_db
mysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)"
How do I use mysqldump to export only the CREATE TABLE commands
Question
I want All CREATE statements of MySql Tables in 1 query result.
For example, INFORMATION_SCHEMA contains all table names,comments etc. but where are the CREATE statements are stored in MySql ? can it be retrieved in one query for all tables ?
Currently i am retrieving TABLE ddl as below for 1 table. I have 100's of tables so i can repeat the same everytime which is time taking process
show create table row_format;
Solution
mysqldump -h localhost -u root -p --no-data --compact some_db
mysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)"
How do I use mysqldump to export only the CREATE TABLE commands
OTHER TIPS
I needed this today, and the mysqldump
answer generated me a .sql file that I could not import: it declared full CREATE TABLE
statements with foreign keys pointing to tables it had not declared yet.
Instead, I used Liquibase:
Download MySQL Connector/J (pick Platform Independent): https://dev.mysql.com/downloads/connector/j/
From the directory where you downloaded the connector jar, create a liquibase.properties
file and edit it like so:
outputDefaultSchema=true
includeSchema=true
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/replace_with_your_db
username=replace with your MySQL user
password=replace with your MySQL password
classpath=full path of the MySQL connector, for instance C:\mysql-connector-java-8.0.21.jar
liquibase --changeLogFile=schema.mysql.sql generateChangeLog
This will generate SQL commands for your schema, all in the right order.
For instance:
CREATE TABLE address (id BIGINT AUTO_INCREMENT NOT NULL, line VARCHAR(128) NULL, owner_id BIGINT NOT NULL);
CREATE TABLE person (id BIGINT AUTO_INCREMENT NOT NULL, name VARCHAR(64) NOT NULL);
ALTER TABLE address ADD CONSTRAINT fk_address_person FOREIGN KEY (owner_id) REFERENCES person (id) ON UPDATE RESTRICT ON DELETE RESTRICT;
Someone just showed me an easier way.