How to eRD to SQL
- The SQL panel opens on a sample schema so you can see the output straight away. Close it to edit the diagram, or start from a template or blank canvas from the File menu.
- Add tables and columns, mark keys, and drag a key column onto another table to create a foreign key. Set ON DELETE and ON UPDATE rules on each relationship.
- Open SQL → Generate SQL, pick PostgreSQL, MySQL, SQL Server or SQLite, choose whether to include DROP TABLE statements and comments, then Copy SQL or Download .sql.
What does this tool do?
The generator writes tables in dependency order, so each foreign key points at a table that already exists. If two tables reference each other, those constraints are added afterwards with ALTER TABLE (SQLite allows them inline). Primary keys, composite keys, UNIQUE and NOT NULL constraints, defaults, named composite indexes and single-column indexes are all included, and PostgreSQL, SQLite and SQL Server get an index on every foreign key that doesn't already have one.
Types and syntax are translated per database: auto-increment becomes GENERATED BY DEFAULT AS IDENTITY in PostgreSQL, AUTO_INCREMENT in MySQL, IDENTITY(1,1) in SQL Server and INTEGER PRIMARY KEY AUTOINCREMENT in SQLite. BOOLEAN becomes BIT on SQL Server, UUID becomes CHAR(36) on MySQL, and timestamps map to DATETIME2 or DATETIME where needed. Names that are reserved words or contain capitals are quoted the way each database expects. Run Validate Schema first — it catches missing primary keys, type mismatches between foreign keys and the keys they reference, and SET NULL on NOT NULL columns before the database does.
Privacy
Everything you type is processed in your browser. Nothing you enter is sent to our servers or stored by us. There's no account to create and nothing to install.
Frequently asked questions
Is the generated SQL ready to run?
It's standard DDL for the chosen database and runs as-is for typical schemas. Review defaults and custom types (like PostgreSQL enums) before running it against production.
How are many-to-many relationships handled?
A many-to-many line has no foreign key, so the SQL notes it with a comment. Select the line and choose Create junction table to add a linking table with two foreign keys; then the SQL includes it.
Can I switch databases later?
Yes. The diagram doesn't belong to one database — change the dialect in the SQL panel and the script is regenerated with that database's types and syntax.
Can I export DBML instead?
Yes. Choose SQL → Export DBML to copy or download the schema in DBML for dbdiagram.io and similar tools.
Last reviewed by the M2Toolkit team.