Logical Design

Your team hasn't chosen between PostgreSQL and SQL Server yet, but it needs to agree on the data now. A logical design in DbSchema holds the entities, their attributes and the relations between them. Its data types are general, such as INT or VARCHAR, and its names are written in plain words, such as customer id. When the design is ready, DbSchema generates the physical model for the database that you choose. Logical design needs DbSchema Architect.

The shop schema as a logical design in DbSchema: the entities countries, addresses, customers, orders, order items, payments, shipments, products and the rest, with attribute names written with spaces and dashed relation lines

A logical model describes the data independently of any database system. It doesn't connect to a database. A physical model is a design for one database system, such as PostgreSQL, with that system's data types. DbSchema calls the database system the DBMS, as in Target DBMS.

A logical design uses its own words for the parts of a schema:

Physical modelLogical design
SchemaSubject Area
TableEntity
ColumnAttribute
Foreign keyRelation

An entity is a thing that you store data about, such as a customer or an order. An attribute is one piece of that data, such as the customer's name.

The changes on this page affect only the model file. A database changes only when you generate the physical model and create its schema in the database. Database Terms explains model, schema, cardinality and the other words on this page.

Start a logical model

  1. Choose File → New Model. On the Welcome Screen, choose Start New.
  2. Choose Design without Database.
  3. Select Logical Design and press Start Design.
  4. In the Model Properties dialog, type a name in Project/SubjectArea Name and press OK.
The New Design Model dialog on Design without Database, with the Logical Design tile selected above the database tiles and the Start Design button

The empty logical model opens in a new window. A logical model has no connection, so its Database menu holds only JDBC Drivers Manager, and its Query menu is empty.

To start from a database that you already have, convert its model, as Turn a physical model into a logical one shows.

Create an entity

  1. Click an empty spot on the diagram, where the entity should go.
  2. Choose Table → Create Entity in the toolbar, or press Ctrl+T. You can also right-click the diagram and choose Create Entity.
  3. Type the name in Table Name.
  4. In the Attributes tab, press Add for each attribute. In the attribute dialog, type a Name and choose a Data Type. Tick Primary Key for the key, and Mandatory ( Not Null ) for an attribute that always has a value.
  5. Press OK.
The New Entity dialog for suppliers, with the attributes supplier id, INT not null, and name, VARCHAR(80) not null, in the Attributes tab

The logical data types are general: BIGINT, BOOLEAN, CHAR, DATE, DATETIME, DECIMAL, DOUBLE, ENUM, INT, MONEY, NUMERIC, NVARCHAR, TEXT, UUID and VARCHAR. Each one becomes a data type of the target database when you generate the physical model.

Create a relation

A relation links two entities, the way a foreign key links two tables. The child entity refers to the parent entity.

  1. Drag an attribute from the child entity onto the key attribute of the parent entity. For example, drag order id from shipments onto order id in orders.
  2. The New Relationship dialog opens, with the attributes filled in. Choose the type and the cardinality, as Set the cardinality of a relation shows.
  3. Press OK.
The New Relationship dialog: Entity shop.shipments refers to orders through order id, with Non-Identifying, Mandatory and Zero or More selected

You can also open the same dialog with Table → Create Relation in the toolbar. To edit a relation, double-click its line.

A relation must point at attributes whose values are unique. If the parent attributes have no primary key or unique key, DbSchema adds a unique index on them when you press OK. The index is named like unq_categories_name.

Set the cardinality of a relation

Cardinality says how many children a parent can have. In a logical design, you set it yourself in the relation dialog, together with two other choices:

GroupOptionWhat it sets
TypeIdentifyingThe child's primary key includes the attributes that refer to the parent
TypeNon-IdentifyingThe child keeps a primary key of its own
TypeMany to ManyMany rows on each side, such as suppliers and products
Is a parent required?MandatoryA child always has a parent. The referring attributes become Not Null
Is a parent required?OptionalA child may have no parent. The line is dashed
Number of childrenZero or More, One or More, Zero or One, OneHow many children a parent has
Number of childrenRangeThe minimum and maximum number of children, each from 0 to 255

Choosing Identifying also selects Mandatory.

The picture shows three relations, set like this:

RelationTypeMandatory or OptionalCardinality
orders to customersNon-IdentifyingOptionalZero or More
order items to ordersIdentifyingMandatoryOne or More
shipments to ordersNon-IdentifyingMandatoryOne
Relations around orders on a logical diagram: a dashed line from orders to customers with a circle and crow's foot, a solid line from order items to orders with a bar and crow's foot, and a solid line from shipments to orders with a single bar

The symbols at the child end of a line show its cardinality. A crow's foot, the three short lines, means many. By default, an arrow points to the parent entity. View → Relations → Relation Notation offers the same notations as for foreign keys.

Many-to-many relations

A database can't store a many-to-many relation directly. It needs an extra table, with a foreign key to each of the two tables. When you generate the physical model, DbSchema creates that table and names it after both entities, such as suppliers_products.

To create the entity between the two in the logical design itself, right-click the relation line and choose Resolve Many-To-Many. DbSchema replaces the relation with the new entity.

In both cases, the new table or entity is added to the model but not to the diagram. Find it in the tree on the left, under Entities or Tables.

Use inheritance between entities

An entity can inherit the attributes of another entity. For example, employee and customer can both inherit the attributes of person. person is then the super-entity, and the other two are its sub-entities.

  1. Open the dialog of the sub-entity.
  2. In Super Table, below the comment, choose the super-entity.
  3. Press OK.

A discriminator is an attribute of the super-entity that says which sub-entity a row belongs to. To mark it, open the dialog of that attribute in the super-entity and tick Discriminator. The SingleTable inheritance model below needs a discriminator to tell the rows apart.

When you generate the physical model, you choose an Inheritance Model. It decides which tables DbSchema creates for the super-entity and its sub-entities:

Inheritance ModelTables for a super-entity and its sub-entities
SingleTableOne table for the super-entity, with the attributes of all its sub-entities
ClassTableOne table per entity, each with its own attributes
ConcreteTableOne table per sub-entity, with the super-entity's attributes added. The super-entity gets no table

Set how names and types convert

Two dictionaries control how DbSchema converts names, data types and default values to the physical model. Both are under Model → Dictionaries, and in the Convert menu of the toolbar.

Convert entity and attribute names

The Naming Dictionary turns logical names into physical names. Under Separators, choose in Logical and Physical how the words of a name are separated. The default is Space for logical names and Underscore for physical names. Convert Cases changes the names to the uppercase or lowercase letters that the target database prefers. In the table below these settings, you can add your own pairs, such as a long word and its short form.

The Naming Dictionary with the separators Space for Logical and Underscore for Physical, and Convert Cases ticked

To see the names that the physical model will get before you generate it, tick View → Diagram Display → Show Physical Name. The diagram then shows customer_id in place of customer id.

Convert data types and default values

The Conversion Dictionary opens the Model Conversion Rules dialog. It defines how DbSchema converts each data type and default value of the model for the target database. To change a rule, double-click its right-hand cell.

The Model Conversion Rules from LogicalDesign to PostgreSQL: BIGINT to bigint, DATETIME to timestamp, INT to integer, VARCHAR to varchar, and the default values below them

The rules from PostgreSQL to the logical design convert integer to BIGINT. So if you convert a PostgreSQL model to a logical model and then back to PostgreSQL, integer becomes bigint. To keep integer, change that rule to INT in the physical model's Conversion Dictionary before you convert it.

Generate the physical model

  1. Choose Model → Generate Physical Model, or Convert → Generate Physical Model in the toolbar.
  2. Set Target DBMS, such as PostgreSQL.
  3. Choose the Inheritance Model, as Use inheritance between entities shows.
  4. Press OK.
The Convert Model Dialog of a logical model, with Target DBMS PostgreSQL and Inheritance Model SingleTable

The physical model opens in a new window, and the logical design doesn't change. If a data type has no rule for the target database, the Model Conversion Rules dialog opens first, as in Turn a physical model into a logical one.

The physical model doesn't keep the cardinality that you selected. For example, a relation set to One doesn't get a unique key in the physical model. In the physical model, DbSchema reads the cardinality from the columns again, as Foreign Keys shows.

The new physical model is offline. Connect it to a database and create the schema there. The same steps convert a physical model to another database, such as MySQL to PostgreSQL. Triggers, functions and procedures use code that only one database understands. Check them after converting the model.

Turn a physical model into a logical one

You can also create a logical design from the model of an existing database. The conversion makes a new model, and the original model doesn't change.

  1. Open the physical model.
  2. Choose Model → Convert Model.
  3. Set Target DBMS to LogicalDesign and press OK.
The Convert Model Dialog with Target DBMS set to LogicalDesign

The logical model opens in a new window. Attribute names are written with spaces instead of underscores, so customer_id becomes customer id. DbSchema sets each converted relation to Non-Identifying and Optional, even where the column is NOT NULL. If the column was NOT NULL, DbSchema sets the cardinality to One or More. If the column allowed NULL, such as categories.parent_id, DbSchema sets it to Zero or More. Check each relation afterwards, and set Mandatory where a child always has a parent.

DbSchema converts each data type by a rule, such as PostgreSQL integer to the logical BIGINT. If a data type has no conversion rule yet, DbSchema opens the Model Conversion Rules dialog first. The missing rule shows ???. In the shop model, that type is smallint, the type of reviews.rating:

The Model Conversion Rules dialog from PostgreSQL to LogicalDesign: integer becomes BIGINT, timestamp becomes DATETIME, and smallint shows ??? because it has no rule yet

Pressing Convert now shows the error "Not all LogicalDesign data types are specified. Please edit." Add a conversion rule for the data type first:

  1. Double-click the ??? cell. The Convert Data Type dialog opens.
  2. Select the LogicalDesign row and press Edit.
  3. In Edit Data Type Replacement, choose a Data Type, such as INT, and press OK.
  4. Press OK in Convert Data Type, then press Convert.

DbSchema keeps the new rule in its own settings. The next conversion from the same database system doesn't ask again, in any model.

Common problems

I can't find Generate Physical Model

Generate Physical Model is in the Model menu of a logical model. In a physical model, the same item reads Model → Convert Model.

How do I go from a physical model back to a logical one?

Convert it with Target DBMS set to LogicalDesign, as Turn a physical model into a logical one shows.

I created the relation the wrong way round

DbSchema can't swap the parent and the child of a relation. Right-click the line, choose Drop, and drag again from the child attribute onto the parent attribute.

Where did this unique index come from?

When you create a relation to attributes that have no primary key or unique key, DbSchema adds a unique index. See Create a relation. If you created the relation by mistake, drop the index in the Primary Key, Indexes tab of the parent entity.

The cardinality I set is gone in the physical model

A physical model has no setting for cardinality: DbSchema reads it from NOT NULL and the unique keys of the child columns. Change those in the physical model, as Change the cardinality shows.

Next steps