| Lesson 7 | Column Domains |
| Objective | Describe column domains in the context of Entities. |
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
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:
DayOfWeek column, for example, might restrict values to "Monday",
"Tuesday", "Wednesday", and so on.Column domains serve three purposes:
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.
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.
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.
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.
The standard data domains available in an SQL-based RDBMS include:
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.