Design a Database from Scratch

First, you design the tables of a new database in DbSchema. Later, you create them in a real database. By the end of this page, you have two tables, customers and orders, linked by a foreign key. They exist in your design file and in a PostgreSQL database.

The tables customers and orders on a DbSchema diagram, with the foreign key line from orders.customer_id to customers.customer_id

Two words are used in every step. The design, also called the model, is your DbSchema project: the tables, columns and diagrams that you see in DbSchema. The database is the real database on the server, such as PostgreSQL. Until you create the tables in the database, all your changes stay in the design. Database Terms explains table, column, primary key, foreign key and the other words on this page.

To design without a database, you need DbSchema Pro. A new installation starts with the Pro Trial, which includes it. With the free Community Edition, connect to a database instead, as the Tutorial shows. There, every table that you create goes into the database at once.

For the last two steps, you need a database where you may create tables, such as a PostgreSQL server on your computer. You can design the tables without one, and connect when it is ready.

Start an empty design

  1. Start DbSchema. On the Welcome Screen, choose Start New in the sidebar.
  2. Choose Design without Database at the top.
  3. Click the database that you design for, such as PostgreSQL.
  4. Press Start Design.
The Start New page of the DbSchema Welcome Screen with Design without Database chosen, the PostgreSQL tile selected and the Start Design button

The database type determines which data types you can choose for the columns, such as integer and varchar in PostgreSQL. If you haven't decided which database to use, click Logical Design. Logical Design needs DbSchema Architect.

  1. In the Model Properties dialog, type a name in Project/Schema Name, such as docs_demo. This name is the name of the model. A comment is optional.
  2. Press OK.
The Model Properties dialog of DbSchema with docs_demo as the Project/Schema Name and a comment

DbSchema opens an empty diagram. If a model is already open, choose File → New Model instead. The New Design Model dialog offers the same choices.

Create the first table

A table stores information about one kind of item, such as customers. Each column stores one piece of that information, such as the customer's name. The primary key is the column whose value identifies each row, such as customer_id.

  1. Right-click an empty place on the diagram and choose Create Table. You can also press Ctrl+T, or choose Create Table in the submenu Model → Create Table, View....
  2. The model has no schema yet. DbSchema asks which schema the table goes into. A schema is a named group of tables in the database. Type docs_demo in Schema and press OK.
The Choose Schema dialog of DbSchema, which says that the model has no schema yet, with docs_demo typed in Schema
  1. In the New Table dialog, type customers in Table Name.
  2. In the Columns tab, press Add. The New Column dialog opens.
  3. Type customer_id in Name. DbSchema suggests integer as the data type.
  4. Tick Primary Key. DbSchema also ticks Mandatory ( Not Null ), because a primary key can't contain NULL.
  5. Press OK.
The New Column dialog of DbSchema for docs_demo.customers, with the name customer_id, the data type integer, and Primary Key and Mandatory ticked
  1. Add two more columns the same way, but leave Primary Key unticked. Choose the data type in the list, and type the length in Length:
    • name, data type varchar, Length 100, with Mandatory ( Not Null ) ticked;
    • email, data type varchar, Length 255, with Mandatory ( Not Null ) left empty.
  2. Press OK in the New Table dialog.
The New Table dialog of DbSchema for customers, with the columns customer_id integer not null, name varchar(100) not null and email varchar(255)

The table shows on the diagram. The key icon marks customer_id as the primary key. varchar(100) holds text of up to 100 characters. name must have a value in every row. email may be NULL: a row may have no email.

Create the second table

Create the table orders the same way, with these columns. DbSchema puts it into the schema docs_demo without asking again.

NameData typeLengthDecimalPrimary KeyMandatory ( Not Null )
order_idinteger yesyes
customer_idinteger noyes
ordered_atdate noyes
totalnumeric102nono

numeric(10,2) holds numbers with up to 10 digits, 2 of them after the decimal point, such as 1234.50. customer_id in orders will hold the ID of the customer who placed the order. In the next step, you define this link with a foreign key.

A foreign key is a column that points to a row in another table. Here, orders.customer_id points to customers.customer_id. The database then rejects an order whose customer doesn't exist.

  1. On the diagram, drag the name customer_id in the orders table onto the name customer_id in the customers table.
  2. The New Foreign Key dialog opens with the fields already filled in. Here, it names the foreign key fk_orders_customers.
  3. Press OK.
The New Foreign Key dialog of DbSchema with the name fk_orders_customers, from docs_demo.orders to customers, with customer_id as the referring and the referred column

A line now joins orders and customers, as in the first picture on this page. To tidy the diagram, choose Diagram → Auto Arrange Tables, then Arrange Only. Foreign Keys explains the marks at the ends of the line.

Save the design file

The design exists only in DbSchema until you save it. DbSchema saves it as a .dbs file. Saving needs DbSchema Pro.

  1. Press Save in the toolbar, or choose File → Save Model to File (Ctrl+S).
  2. Choose a folder and a file name, such as docs_demo.dbs, and save.
The left end of the DbSchema toolbar, with the Model, Reopen and Save buttons

Saving updates only the design file. It does not change the database. Save again after each change. You can keep the file in Git with your application code, as Git Collaboration shows.

Connect to a database

Now choose the database where you want to create the tables. Use a test database, or a new one: the next step creates tables in it.

  1. Choose Database → Add Connection. If you saved a connection before, choose it in the Database menu instead, and go on at step 4.
  2. In the connection dialog, keep Standard as the Method. Choose where the database runs, and fill in the Database User, the Password and the Database.
  3. Press Connect.
The Connect to PostgreSQL dialog of DbSchema with the Standard method, This computer, default port, the user postgres and the database shopdb

Fill in the connection dialog explains every field. Connecting only reads from the database.

  1. In the Connected to PostgreSQL dialog, DbSchema asks what to do with the design and the database. Click Write the model into the database. This choice opens the dialogs described below. It changes nothing yet.
The Connected to the Database dialog of DbSchema with the two choices Compare, then decide difference by difference, and Write the model into the database

Create the tables in the database

DbSchema generates the SQL that creates your tables in the database. You review each statement before it runs. Creating the tables needs DbSchema Pro.

If you pressed Skip in the Connected to PostgreSQL dialog, choose Database → Create or Upgrade Model in the Database. It opens the same dialogs.

  1. In Commit Changes into the Database, keep the schema docs_demo ticked, with all its tables. Press OK.
The Commit Changes into the Database dialog of DbSchema with the schema docs_demo and its Tables ticked
  1. The Execute Script In Database Dialog shows the first statement, under Current SQL. Read it.
  2. Press Execute to run it. The Execution Message says Done, and DbSchema shows the next statement.
  3. Repeat until the message says No further statements left to execute. Then press Close.
The Execute Script In Database Dialog of DbSchema showing statement 2 of 4, CREATE TABLE docs_demo.customers, with Done as the Execution Message

Skip moves on to the next statement without running the one shown. Execute All asks you once, then runs all the remaining statements.

You don't need to type any of this SQL. For the design on this page, DbSchema ran these four statements in PostgreSQL:

CREATE SCHEMA IF NOT EXISTS docs_demo;

CREATE TABLE docs_demo.customers (
    customer_id integer NOT NULL,
    name        varchar(100) NOT NULL,
    email       varchar(255),
    CONSTRAINT pk_customers PRIMARY KEY ( customer_id )
);

CREATE TABLE docs_demo.orders (
    order_id    integer NOT NULL,
    customer_id integer NOT NULL,
    ordered_at  date NOT NULL,
    total       numeric(10,2),
    CONSTRAINT pk_orders PRIMARY KEY ( order_id )
);

ALTER TABLE docs_demo.orders ADD CONSTRAINT fk_orders_customers
    FOREIGN KEY ( customer_id ) REFERENCES docs_demo.customers( customer_id );

The tables now exist in the database, and the History pane at the bottom left lists the statements. Your design is now connected to the database. When you press OK in a table dialog, DbSchema changes both the design and the database. To change only the design again, choose Database → Offline (connect later). To change the design first and the database later, see Schema Synchronization.

Common problems

Design without Database asks me to upgrade

Designing without a database needs DbSchema Pro, and Logical Design needs DbSchema Architect. With the free Community Edition, connect to a database and create the tables there. See Licensing.

My tables went into the schema public

The Choose Schema dialog suggests public, the default schema of PostgreSQL. Type the name of your own schema there before you press OK.

Next steps