User Data Types

A user data type is a data type that you define once and use for many columns. For example, a PostgreSQL domain named email_address uses varchar(120) and checks that the value contains an @. An enum type, such as order_status, allows only a fixed list of values.

The DbSchema tree of the shop schema with the folder Type or Domains open and the type email_address in it

DbSchema lists the user data types of each schema in a folder of the tree. The folder name depends on your database. For PostgreSQL, it is Type or Domains.

The design is your DbSchema project: the tables, columns and types that you see in DbSchema. The database is the real database on the server. Each task below says which of the two it changes. Database Terms explains data type, schema and the other words on this page.

Create a user data type

  1. Choose Model → Create Table, View... → Create User Data Type.
  2. Type the Name, such as email_address, and a Comment.
  3. In Definition, replace the template with the SQL statement that creates the type. Write ${nameWithSchemaName} in place of the type name. DbSchema replaces this placeholder with the schema and the name.
  4. Press OK.
The New Type or Domain dialog of DbSchema for email_address, with Java Type VARCHAR and the definition CREATE DOMAIN with a CHECK on VALUE

For PostgreSQL, a domain is defined with a CREATE DOMAIN statement, and an enum type with a CREATE TYPE statement:

CREATE DOMAIN ${nameWithSchemaName} AS varchar(120) CHECK ( VALUE LIKE '%@%' )
CREATE TYPE ${nameWithSchemaName} AS ENUM ( 'new', 'paid', 'shipped' )

The other fields of the dialog:

FieldWhat it sets
SpecificationWhich parameters follow the type name in a column of this type: No parameters required, Length or Precision - (x), Decimal - (x,y) or Enumeration - (a,b,c...)
Java TypeThe matching type of the JDBC standard (java.sql.Types), such as VARCHAR or INTEGER
VirtualKeeps the type in the design only

DbSchema never creates a virtual type in the database. When DbSchema generates SQL scripts, it replaces the virtual type's name with its definition.

If you are connected, OK creates the type in the database at once, unless you ticked Virtual. If you work offline, the type stays in the design until you synchronize.

Use the type for a column

  1. Double-click the column on the diagram.
  2. In the data type list, choose the type. The list shows the type with its schema name, such as shop.email_address.
  3. Press OK.
The Edit Column dialog of DbSchema for customers.email with the data type shop.email_address

If you are connected, DbSchema changes the column in the database at once, with ALTER TABLE.

Rename a type

Rename the type from the tree. Then all the columns that use this type show its new name. Don't rename it in its Edit dialog while you are connected. Common problems explains why.

  1. Right-click the type in the tree and choose Rename.
  2. Type the new name and press OK.
The Rename Editor dialog of DbSchema with the new name contact_email

Rename changes only the design, even when you are connected. Every column that uses the type shows the new name at once.

To apply the new name to the database, choose Database → Create or Upgrade Model in the Database. This needs DbSchema Pro. DbSchema shows each statement before it runs it, as Create the tables in the database shows. For PostgreSQL, DbSchema creates a type with the new name and changes each column to use it:

CREATE DOMAIN shop.contact_email AS varchar(120) CHECK ( VALUE LIKE '%@%' )
COMMENT ON DOMAIN shop.contact_email IS 'An email address. The check rejects values without @.'
ALTER TABLE shop.customers ALTER COLUMN email TYPE shop.contact_email USING email::text::shop.contact_email

The old type remains in the database. When nothing uses the old type any more, you can drop it in the SQL Editor. For example, run DROP DOMAIN shop.email_address. PostgreSQL refuses the drop while a column still uses the type.

Common problems

Renaming a type in its Edit dialog fails with "cannot drop type"

If you are connected, don't rename a type in its Edit dialog while columns use it. DbSchema then tries to create a type with the new name and drop the old one. It runs both statements in one transaction. The database refuses to drop a type that a column uses, so DbSchema shows an error such as:

ERROR: cannot drop type shop.contact_email because other objects depend on it
Detail: column email of table shop.customers depends on type shop.contact_email

The database rejects the entire transaction. The old type remains, and no new type is created. Press OK in the error message, then Cancel in the Edit dialog. Rename the type from the tree instead, as Rename a type shows.

Next steps