SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | CREATE DOMAIN

The CREATE DOMAIN statement is used in some relational database management systems (RDBMS) to create a user-defined data type with specific validation. Essentially, a domain is a named data type with optional constraints, allowing developers to use this custom-defined type in table definitions.

The primary advantage of using domains is the ability to centralize rules for specific data attributes. For instance, if several tables have a column representing a valid email address or a phone number, you can define a domain for such types and use it across these tables.

Syntax:

CREATE DOMAIN domain_name AS data_type
[DEFAULT default_value]
[CHECK (constraint_expression)];

Example:

  1. Creating a domain for email addresses:

    CREATE DOMAIN email_type AS VARCHAR(100)
    CHECK (VALUE LIKE '%@%.%');
    

    Here, the domain email_type is defined as a VARCHAR(100), and it ensures that any value stored within a column of this type looks like an email address.

  2. Creating a domain for age values:

    CREATE DOMAIN age_type AS INT
    CHECK (VALUE BETWEEN 0 AND 150);
    

    This domain ensures that age values are integers between 0 and 150.

Using the Domain:

Once a domain has been created, you can use it in table definitions just like any built-in data type:

CREATE TABLE person (
    person_id INT PRIMARY KEY,
    name VARCHAR(255),
    email email_type,
    age age_type
);

Notes:

  • Portability: Not all RDBMS support the CREATE DOMAIN statement. For instance, while PostgreSQL and some editions of SQL Server support it, MySQL does not. Always refer to your specific RDBMS documentation.

  • Modifying Domains: Some RDBMS systems allow you to modify existing domains using the ALTER DOMAIN statement.

  • Dropping Domains: If you want to remove a domain, you can typically use the DROP DOMAIN statement. However, you'll need to ensure that there are no tables currently using that domain.

Using domains can greatly improve the consistency of data in your database by centralizing validation rules. However, always be aware of potential pitfalls related to portability if you plan to switch RDBMS or share your database design with others.

  1. How to Use CREATE DOMAIN in SQL: The CREATE DOMAIN statement is used to define a user-defined data type in SQL.

    CREATE DOMAIN your_domain AS INT;
    
  2. Defining Custom Data Types with CREATE DOMAIN:

    CREATE DOMAIN phone_number AS VARCHAR(15);
    
  3. Specifying Constraints with CREATE DOMAIN:

    CREATE DOMAIN positive_integer AS INT CHECK (VALUE > 0);
    
  4. ALTER DOMAIN Statement in SQL: You can use ALTER DOMAIN to modify an existing domain.

    ALTER DOMAIN your_domain SET DEFAULT 0;
    
  5. Using Domains for Data Consistency in SQL:

    CREATE TABLE your_table (
       column1 positive_integer,
       column2 phone_number
    );
    
  6. Creating Domains with Default Values in SQL:

    CREATE DOMAIN temperature AS DECIMAL(5, 2) DEFAULT 0.0;
    
  7. Checking Existing Domains in a Database:

    SELECT *
    FROM INFORMATION_SCHEMA.DOMAINS;
    
  8. Creating Domains with CHECK Constraints in SQL:

    CREATE DOMAIN even_number AS INT CHECK (VALUE % 2 = 0);
    
  9. Using Domains with Tables and Columns in SQL:

    CREATE TABLE your_table (
       column1 even_number,
       column2 phone_number
    );
    
  10. Examples of CREATE DOMAIN in Different Database Systems:

    • PostgreSQL:
      CREATE DOMAIN email AS VARCHAR(255) CHECK (VALUE ~* '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,4}$');
      
    • SQL Server:
      CREATE DOMAIN email AS VARCHAR(255) CHECK (VALUE LIKE '%_@__%.__%');