Defining a Database Domain

Data domains enforce data-entry standards on specific database fields

A simple definition of a database domain is the data type used by a column in a database. This data type can be a built-in type (such as an integer or a string) or a custom type that defines constraints on the data.

Data Entry and Domains

When you enter data into an online form of any kind, whether it's your name and email or a job application, a database stores your input behind the scenes. That database evaluates your entries based on a set of criteria.

For example, if you enter a ZIP code, the database expects to find five numbers (or five numbers followed by a hyphen then four numbers for a complete U.S. ZIP code). If you enter your name into a zip code field, the database gives you an error.

That's because the database tests your entry against the domain defined for the zip code field. A domain is basically a data type that can include optional restrictions.

Every type of database provides a way to define a set of restrictions and rules that govern allowable data, even if it does not call it a domain. See your database's documentation for details.

Computer network database
alexsl / Getty Images

Understanding a Database Domain

To understand a database domain, let's consider a few other aspects of a database:

  • A database schema defines a set of attributes, also called columns or fields. A table called "Contact Information" may include attributes for FirstName, LastName, JobTitle, StreetAddress, City, State, ZipCode, PhoneNumber, and Email.
  • Each attribute incorporates a domain that defines allowable values, potentially including its data type, length, values, and other details.

For example, the domain for an attribute ZipCode might specify a numeric data type, such as an integer, usually called an INT or an INTEGER, depending on the database. Or, a database designer might choose to define it instead as a character, usually called a CHAR. The attribute can be further defined to require a specific length, or whether an empty or unknown value is allowed.

When you gather all the elements that define a domain, you end up with a customized data type, also called a "user-defined data type" or a UDT.

What Is Domain Integrity?

The allowed values of an attribute establish domain integrity, which ensures that all data in a field contains valid values. 

Domain integrity is defined by:

  • The data type, such as integer, character, or decimal.
  • The allowed length of the data.
  • The range, defining the upper and lower boundaries.
  • Any constraints, or limitations on allowable values. For example, a U.S. ZIP code field might enforce a complete ZIP+4 code or a full nine-digit code.
  • The type of NULL support (whether an attribute can have an unknown or NULL value).
  • The default value, if any.
  • The date format painter, if applicable (for instance, dd/mm/yy or mm/dd/yyyy).

Creating a Domain

For databases that use Structured Query Language or a flavor of SQL, use the CREATE DOMAIN SQL command.

For example, the execution statement creates a ZipCode attribute of data type CHAR with five characters. A NULL, or unknown value, is not allowed. The range of the data must fall between 00000 and 99999. That creates a ZipCode attribute of data type CHAR with five characters. A NULL, or unknown value, is not allowed.

CREATE DOMAIN ZipCode CHAR(5) NOT NULL CHECK (VALUE >= '00000' AND VALUE <= '99999')

These database constraints push an error to an application that serves as the front-end to your database when the constraint is violated, so program an error-capture subroutine into your program to sanity-check before the program thinks it properly added information to the database.

Was this page helpful?