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.
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
- Start DbSchema. On the Welcome Screen, choose Start New in the sidebar.
- Choose Design without Database at the top.
- Click the database that you design for, such as PostgreSQL.
- Press Start Design.
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.
- 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. - Press OK.
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.
- 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....
- 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_demoin Schema and press OK.
- In the New Table dialog, type
customersin Table Name. - In the Columns tab, press Add. The New Column dialog opens.
- Type
customer_idin Name. DbSchema suggestsintegeras the data type. - Tick Primary Key. DbSchema also ticks Mandatory ( Not Null ), because a primary key can't contain NULL.
- Press OK.
- 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 typevarchar, Length100, with Mandatory ( Not Null ) ticked;email, data typevarchar, Length255, with Mandatory ( Not Null ) left empty.
- Press OK in the New Table dialog.
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.
| Name | Data type | Length | Decimal | Primary Key | Mandatory ( Not Null ) |
|---|---|---|---|---|---|
order_id | integer | yes | yes | ||
customer_id | integer | no | yes | ||
ordered_at | date | no | yes | ||
total | numeric | 10 | 2 | no | no |
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.
Link the tables 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.
- On the diagram, drag the name
customer_idin theorderstable onto the namecustomer_idin thecustomerstable. - The New Foreign Key dialog opens with the fields already filled in. Here, it names the foreign key
fk_orders_customers. - Press OK.
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.
- Press Save in the toolbar, or choose File → Save Model to File (Ctrl+S).
- Choose a folder and a file name, such as
docs_demo.dbs, and save.
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.
- Choose Database → Add Connection. If you saved a connection before, choose it in the Database menu instead, and go on at step 4.
- 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.
- Press Connect.
Fill in the connection dialog explains every field. Connecting only reads from the database.
- 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.
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.
- In Commit Changes into the Database, keep the schema
docs_demoticked, with all its tables. Press OK.
- The Execute Script In Database Dialog shows the first statement, under Current SQL. Read it.
- Press Execute to run it. The Execution Message says Done, and DbSchema shows the next statement.
- Repeat until the message says No further statements left to execute. Then press Close.
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.