Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 August PostgreSQL: CREATE DOMAIN to Abstract Data Type and Enforce Business Rules

PostgreSQL: CREATE DOMAIN to Abstract Data Type and Enforce Business Rules

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

PostgreSQL has one of the very good data type to store all the optional constraints and business rule of the application.

The DOMAIN is a type of DATA TYPE to abstract the common constraint and other business rule of Database system.

For example, we have several tables with Email column and we require to apply CHECK CONSTRAINT for validation of those Email columns.

For this kind of requirement, we can create one DOMAIN DATA TYPE, Instead of creating different CHECK CONSTRAINT for all the columns.
The DOMAIN DATA TYPE is also very easy to manage at one single place, without modifying each individual CHECK CONSTRAINT.

Below is a small demonstration of this:

CREATE one DOMAIN data type to validate simple email expression:
Doesn’t allow numbers in the domain name and doesn’t allow for top level domains that are less than 2 or more than 3 letters.

1
2
3
4
CREATE DOMAIN domain_email AS TEXT
CHECK(
VALUE ~ '^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$'
);

CREATE a Sample table using domain_email data type:

1
2
3
4
5
CREATE TABLE tbl_TestEmail
(
ID INT
,Email domain_email
);

Insert record with valid email address:

1
2
3
INSERT INTO tbl_TestEmail VALUES (1,'anvesh@gmail.com');
INSERT INTO tbl_TestEmail VALUES (2,'anvesh_08@dbrnd.org');
INSERT INTO tbl_TestEmail VALUES (3,'dba@aol.in');

Try to insert invalid email and test the CHECK CONSTRAINT of DOMAIN DATA TYPE:

1
2
3
INSERT INTO tbl_TestEmail VALUES (4,'anvesh@dbrnd.co.in');
INSERT INTO tbl_TestEmail VALUES (5,'anvesh_08@08dbrnd.org');
INSERT INTO tbl_TestEmail VALUES (6,'dba@aol.info');

Aug 12, 2016Anvesh Patel
PostgreSQL 9.4: Using FILTER CLAUSE, multiple COUNT(*) in one SELECT Query for Different GroupsPostgreSQL 9.5: How to Concatenate and Overwrite JSON Document
Anvesh Patel
Anvesh Patel

Database Engineer

August 12, 2016 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, DOMAIN DATA TYPE, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....