Relational Constructs   «Prev  Next»

Lesson 7 Column Domains
Objective Describe column domains in the context of Entities.

Columns Represent Entity Attributes

Whenever you create a table to represent an entity, you create columns to represent that entity's attributes. Each column corresponds to one property of the entity - one piece of information the database needs to store about every instance of that entity.

In the preceding exercise, you created a table to record each line item in the orders placed by Stories on CD customers. The Line Item table records seven attributes of the Line Item entity: the order number, the CD ordered, the quantity, the normal selling price, any applicable discount, the calculated line cost, and the shipping status.

Line Item
OrderNo CDNo Quantity SellingPrice Discount LineCost Shipped
The LINE ITEM table records: 1) Order number, 2) CDNo, 3) Quantity, 4) Selling Price, 5) Discount, 6) LineCost, and 7) whether the item was Shipped.
Database Design and Theory

What Is a Column Domain?

A column domain specifies the permissible set of values that a column can hold. It acts as a constraint on the type of data that can be entered into a column, ensuring that every value stored in that column is valid, meaningful, and consistent with the attribute it represents.

A column domain is more than just a data type. It is the combination of:

Column domains serve three purposes:

  1. Data integrity - domains ensure that only valid data types and values are accepted, preventing anomalies and inconsistencies from entering the database.
  2. Consistency - by defining the domain of each column, you guarantee that data across all rows in a table - and across related tables - remains consistent and reliable.
  3. Validation - domains validate data at the point of entry, before it is stored, reducing errors and improving overall data quality.

How ER Diagram Attributes Map to Column Domains

The connection between ER diagram attributes and column domains is the bridge from conceptual database design to physical implementation. At the conceptual level, an ER diagram defines entities and their attributes. Each attribute has a domain - the set of all legal values that attribute can take. Domains are documented as part of the conceptual model but are not usually shown visually on the ER diagram itself (only attribute names appear in ovals).

When the ER diagram is translated into a relational schema, each entity becomes a table, each attribute becomes a column, and the attribute's domain becomes the column's domain - enforced through the data type and constraint definitions in the CREATE TABLE statement. The following table summarizes this mapping:

ER Diagram Concept Relational Implementation How the Domain Is Represented
Entity Table Table name
Attribute Column Column name + data type
Attribute Domain Column Domain Data type + constraints (CHECK, NOT NULL, length, precision)
Key Attribute Primary Key column UNIQUE + NOT NULL (often with auto-increment)
Relationship Attribute Foreign Key column Domain matching the referenced primary key's domain

The CD entity from the Stories on CD course project illustrates this mapping concretely. In the ER diagram, the CD entity has attributes including CDNo (positive integers), CDTitle (text strings), RetailPrice (positive decimal numbers), and AgeGroup (a specific set of strings). The corresponding relational table enforces each of those domains through data types and constraints:


CREATE TABLE CD (
    CDNo        INT            PRIMARY KEY,
    CDTitle     VARCHAR(200)   NOT NULL,
    DistID      INT            NOT NULL,
    RetailPrice DECIMAL(10,2)  CHECK (RetailPrice > 0),
    AgeGroup    VARCHAR(20)    CHECK (AgeGroup IN ('Child', 'Teen', 'Adult', 'All')),
    Description TEXT,
    FOREIGN KEY (DistID) REFERENCES DISTRIBUTOR(DistID)
);

Each column's domain directly implements the attribute's domain from the ER diagram. The RetailPrice CHECK constraint enforces the "positive decimal numbers" domain. The AgeGroup CHECK constraint enforces the enumerated set of valid age group values. The NOT NULL on CDTitle enforces that every CD must have a title. The foreign key on DistID enforces that every CD must reference a valid distributor.

Column Domains in Data Modeling

In data modeling, column domains play a role beyond individual table design. They provide a vocabulary for expressing business rules in database terms and a mechanism for applying consistent constraints across the schema.

Consider a date column that stores order dates. The domain for this column might specify that only dates on or after the company's founding date are valid, and that the date must be stored in ISO 8601 format (YYYY-MM-DD). A CHECK constraint enforces the date range; the DATE data type enforces the format. Together they implement the domain.

One of the practical advantages of thinking in terms of domains during data modeling is maintainability. When the same kind of attribute appears in multiple tables - for example, a price attribute in both a Products table and an OrderItems table - both columns should share the same domain definition (DECIMAL(10,2) with a CHECK constraint requiring a non-negative value). If the business rule changes - for example, prices may now be zero for free items - the change is made consistently across all columns that share the domain.

Column domains also serve as documentation. A column defined as VARCHAR(20) CHECK (AgeGroup IN ('Child', 'Teen', 'Adult', 'All')) communicates its valid values directly in the schema definition. Anyone examining the schema can see immediately what values are permitted without consulting external documentation.

SQL Implementation Examples

The following examples show how column domains are implemented in SQL through data types and constraints. Each column definition expresses a specific domain that the RDBMS will enforce on every INSERT and UPDATE operation:


CREATE TABLE Employees (
    EmployeeID INT            NOT NULL,
    FirstName  VARCHAR(50)    NOT NULL,
    LastName   VARCHAR(50)    NOT NULL,
    BirthDate  DATE,
    Salary     DECIMAL(10, 2) CHECK (Salary > 0),
    Email      VARCHAR(100)   UNIQUE
);

In this table: EmployeeID, FirstName, and LastName are NOT NULL - these attributes must always have values. Salary has a CHECK constraint requiring a positive value. Email has a UNIQUE constraint preventing duplicate email addresses. BirthDate uses the DATE type, preventing storage of non-date values.


CREATE TABLE Products (
    ProductID   INT            PRIMARY KEY,
    ProductName VARCHAR(100)   NOT NULL,
    Price       DECIMAL(10, 2) CHECK (Price >= 0),
    Stock       INT            CHECK (Stock >= 0)
);

Here, Price and Stock have CHECK constraints requiring non-negative values. A product price cannot be negative, and a stock level cannot be negative - these are business rules expressed as column domain constraints in the schema.

Why Data Types Matter: Use the Right Domain for Each Field

You can store the contents of any field as plain text, but doing so sacrifices the domain enforcement that data types provide. An RDBMS that does not know a column holds numbers cannot prevent the storage of text in that column, cannot perform arithmetic on its values, and cannot sort it numerically. An RDBMS that does not know a column holds dates cannot validate date formats, cannot perform date arithmetic, and cannot enforce date range constraints.

All RDBMSs based on the Structured Query Language (SQL) provide a standard selection of data domains - also referred to as data types[1] - that characterize a column's contents. Assigning the appropriate data type to each column is the first step in defining its domain.

Available Data Domains in an SQL-Based RDBMS

The standard data domains available in an SQL-based RDBMS include:

  1. CHAR(n) - a fixed-length text string of exactly n characters. If the stored value is shorter than n, the column is padded with spaces to fill the defined length.
  2. VARCHAR(n) - a variable-length text string of up to n characters. Storage is proportional to the actual length of the value, making VARCHAR more storage-efficient than CHAR for columns where value lengths vary significantly.
  3. INT - an integer (whole number with no decimal component). The available range depends on the operating system and RDBMS implementation; a range of -32,768 to 32,767 is common for standard INT. Many systems also provide BIGINT for larger ranges.
  4. DECIMAL(p, s) - numbers with decimal values, where p is the total number of digits and s is the number of digits to the right of the decimal point. DECIMAL(10, 2) stores values up to 99,999,999.99 with exactly two decimal places - appropriate for monetary values.
  5. DATE - a calendar date. The storage format and display format may differ; ISO 8601 format (YYYY-MM-DD) is the recommended standard for portability across RDBMS platforms.
  6. TIME - a time of day. Default format varies by RDBMS; most store hours, minutes, and seconds.
  7. DATETIME (or TIMESTAMP) - a combined date and time value. TIMESTAMP is the ISO standard term; DATETIME is used by some platforms including SQL Server and MySQL.
  8. BOOLEAN - a logical true or false value, usually stored internally as 1 (true) or 0 (false). Not all RDBMS platforms support BOOLEAN natively; some use TINYINT(1) or a CHECK constraint to achieve the same effect.

Read your RDBMS documentation carefully when selecting data types. Different platforms extend the standard in different ways. Many platforms support a BIGINT type that stores integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 - far beyond the range of standard INT. For very large text values, platforms provide TEXT or CLOB types. For binary data, BLOB types are available. Choosing the right domain for each column is a foundational decision in physical database design.

The next lesson describes column domains that support calculations.

[1] Data type: The category of data stored in a database. Each column in a database table must have a data type assigned to it, such as Integer, Text, or Date/Time. The data type is the technical foundation of the column's domain.

SEMrush Software 7 SEMrush Banner 7