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.
In the preceding exercise, you created a table to record each item in the orders placed by Stories on CD customers.
The Line Item table, illustrated below, recorded the order number, the item ordered, the quantity,
the item's normal selling price, any applicable discount, the subtotal for the line or "LineCost", and the shipping status of the item.
Line Item |
OrderNo |
CDNo |
Quantity |
SellingPrice |
Discount |
LineCost |
Shipped |
LINE ITEM table records the 1) Order number, 2) CDNo, 3) Quantity, 4) Selling Price, 5) Discount, 6) LineCost, and 7) whether the item was shipped
Describe column domains in the context of Entities
In the context of entities of a relational database, column domains are a fundamental concept that define the set of allowable values for a particular column in a table. Here’s a detailed description of column domains:
- Definition of Column Domains:
- Column Domain: A domain specifies the permissible set of values that a column can hold in a relational database. It acts as a constraint on the type of data that can be entered into a column.
- Type: Each column in a table is associated with a data type, which is part of its domain definition. Examples include INTEGER, VARCHAR, DATE, BOOLEAN, etc.
- Purpose of Column Domains:
- Data Integrity: Domains help maintain data integrity by ensuring that only valid data types and values are entered into a column. This prevents data anomalies and inconsistencies.
- Consistency: By defining domains, you ensure that the data across different rows and tables remains consistent and reliable.
- Validation: Domains provide a way to validate data before it is stored in the database, reducing errors and improving data quality.
- Components of Column Domains:
- Data Type: Specifies the kind of data (e.g., integer, string, date) that can be stored in the column.
- Constraints: Additional rules that further restrict the values. These can include:
- NOT NULL: Ensures that the column cannot have NULL values.
- CHECK: Defines a condition that each row must satisfy.
- UNIQUE: Ensures that all values in the column are unique across the table.
- DEFAULT: Provides a default value for the column if none is specified.
- Range of Values: Sometimes, a domain can also specify a range or a specific set of allowable values. For example, a column for days of the week might restrict values to "Monday", "Tuesday", etc.
- Implementation in SQL:
- Creating a Table with Domains: When creating a table, you define the domain for each column using data types and constraints.
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
);
Domain Constraints Example:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
Price DECIMAL(10, 2) CHECK (Price >= 0),
Stock INT CHECK (Stock >= 0)
);
- Benefits of Using Column Domains:
- Enhanced Data Quality: By restricting the values that can be entered, domains help ensure high-quality data.
- Simplified Data Management: Domains make it easier to manage data by providing clear rules for what constitutes valid data.
- Improved Query Accuracy: Queries yield more accurate results when data integrity is maintained through strict domain enforcement.
- Better Documentation: Domains serve as documentation for the expected data format and constraints, making the database schema easier to understand and maintain.
In summary, column domains in the context of entities of a relational database are crucial for defining the types of data that can be stored in each column, ensuring data integrity, consistency, and quality throughout the database.
Column Domains used in Data Modeling
Question: How is the concept of column domains used in data modeling?
In data modeling, a column domain is a set of possible values that can be assigned to a particular column in a database table. The concept of column domains is used to ensure data consistency and accuracy by defining the range of valid values for each column.
Column domains help to ensure that data entered into a database is consistent and accurate by enforcing constraints on the values that can be assigned to a column. For example, a column domain for a column that stores dates might specify that only dates between a certain range are valid, or that the date format must be in a particular format such as "YYYY-MM-DD".
By using column domains, data modeling can ensure that data stored in a database is consistent and accurate, which can help to prevent errors and improve data quality. Column domains also help to make it easier to update and modify the database schema, since changes to the domain can be made in a single location and applied to all columns that use the domain.
In addition to enforcing constraints on values, column domains can also be used to specify default values, specify data types, and specify other attributes that are associated with a particular column. Overall, the use of column domains in data modeling is an important tool for ensuring data accuracy and consistency, and can help to improve the overall quality of data stored in a database.
Use Data Types for Fields
Available Domains
The data domains available in an SQL-based RDBMS include the following:
- CHAR: A fixed-length text string of up to 256 characters
- VARCHAR: A variable-length text string of up to 256 characters
- INT: An integer (whole number); the available size depends on the operating system, though a range of -32,768 to 32,767 (no decimals) is common.
- DECIMAL: Numbers with decimal values allowed; the database designer can define the number of allowable places to the left and right of the decimal point.
- DATE: A calendar date, usually of the form MM/DD/YY (two-digit month, two-digit day, two-digit year)
- TIME: A time (default format varies)
- DATETIME: Combined date and time
- BOOLEAN: Logical true or false, usually represented 1 or 0
Be sure to read your database management system's manual carefully some RDBMSs have domains that you can use to store larger integer values. In Microsoft Access, for example, the long domain can store integer values from
- 2,147,483,648 to 2,147,483,647. The next lesson describes column domains that support calculations.
[1]data type: The category of data stored in a database. Each field in a database must have a data type assigned to it, such as Integer, Text, or Date/Time.