Generate CREATE statements for all MySql Tables
mysqldump -h localhost -u root -p --no-data --compact some_dbmysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)"
How do I use mysqldump to export only the CREATE TABLE commands
Someone just showed me an easier way.
- Highlight all the tables
- Right click.
- Select 'Copy to Clipboard' and then 'Create statements'.
- Go to a text editor and paste and everything is there.
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=trueincludeSchema=truedriver=com.mysql.cj.jdbc.Driverurl=jdbc:mysql://localhost:3306/replace_with_your_dbusername=replace with your MySQL userpassword=replace with your MySQL passwordclasspath=full path of the MySQL connector, for instance C:\mysql-connector-java-8.0.21.jar
- Install Liquibase then run:
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;