Docs

Export to SQL (DDL)

DrawSQL can generate DDL scripts from your diagram that you can run/import to create your schema in your actual database.

To do so:

  1. From the diagram editor, click on 'File' in the menubar, then "Export"

  2. Select the SQL export type that corresponds to your diagram DBMS (eg: MySQL, PgSQL, or SQL Server)

  3. Click on the "Start export" button.

  4. Once the export process is complete, the .sql file will automatically be downloaded.

Sample export output

 CREATE TABLE `regions`(
    `region_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `region_name` VARCHAR(25) NULL
);
CREATE TABLE `employees`(
    `employee_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `first_name` VARCHAR(20) NULL,
    `last_name` VARCHAR(25) NOT NULL,
    `email` VARCHAR(100) NOT NULL,
    `phone_number` VARCHAR(20) NULL,
    `hire_date` DATE NOT NULL,
    `job_id` INT NOT NULL AUTO_INCREMENT,
    `salary` DECIMAL(8, 2) NOT NULL,
    `manager_id` INT NULL AUTO_INCREMENT,
    `department_id` INT NULL AUTO_INCREMENT
);
CREATE TABLE `jobs`(
    `job_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `job_title` VARCHAR(35) NOT NULL,
    `min_salary` DECIMAL(8, 2) NULL,
    `max_salary` DECIMAL(8, 2) NULL
);
CREATE TABLE `dependents`(
    `dependent_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `first_name` VARCHAR(50) NOT NULL,
    `last_name` VARCHAR(50) NOT NULL,
    `relationship` VARCHAR(25) NOT NULL,
    `employee_id` INT NOT NULL AUTO_INCREMENT
);
CREATE TABLE `locations`(
    `location_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `street_address` VARCHAR(40) NULL,
    `postal_code` VARCHAR(12) NULL,
    `city` VARCHAR(30) NOT NULL,
    `state_province` VARCHAR(25) NULL,
    `country_id` CHAR(2) NOT NULL
);
CREATE TABLE `countries`(
    `country_id` CHAR(2) NOT NULL,
    `country_name` VARCHAR(40) NULL,
    `region_id` INT NOT NULL AUTO_INCREMENT
);
ALTER TABLE
    `countries` ADD PRIMARY KEY(`country_id`);
CREATE TABLE `departments`(
    `department_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `department_name` VARCHAR(30) NOT NULL,
    `location_id` INT NULL AUTO_INCREMENT
);
ALTER TABLE
    `dependents` ADD CONSTRAINT `dependents_employee_id_foreign` FOREIGN KEY(`employee_id`) REFERENCES `employees`(`employee_id`);
ALTER TABLE
    `countries` ADD CONSTRAINT `countries_region_id_foreign` FOREIGN KEY(`region_id`) REFERENCES `regions`(`region_id`);
ALTER TABLE
    `employees` ADD CONSTRAINT `employees_manager_id_foreign` FOREIGN KEY(`manager_id`) REFERENCES `employees`(`employee_id`);
ALTER TABLE
    `employees` ADD CONSTRAINT `employees_job_id_foreign` FOREIGN KEY(`job_id`) REFERENCES `jobs`(`job_id`);
ALTER TABLE
    `employees` ADD CONSTRAINT `employees_department_id_foreign` FOREIGN KEY(`department_id`) REFERENCES `departments`(`department_id`);
ALTER TABLE
    `locations` ADD CONSTRAINT `locations_country_id_foreign` FOREIGN KEY(`country_id`) REFERENCES `countries`(`country_id`);
ALTER TABLE
    `departments` ADD CONSTRAINT `departments_location_id_foreign` FOREIGN KEY(`location_id`) REFERENCES `locations`(`location_id`);

Still have a question? Contact us via chat or email.