Bird
0
0

You want to create a domain hex_color that stores a 7-character string starting with '#' followed by exactly 6 hexadecimal digits (0-9, A-F, a-f). Which domain definition is correct?

hard📝 Application Q9 of 15
PostgreSQL - Advanced Features
You want to create a domain hex_color that stores a 7-character string starting with '#' followed by exactly 6 hexadecimal digits (0-9, A-F, a-f). Which domain definition is correct?
ACREATE DOMAIN hex_color AS char(6) CHECK (VALUE ~ '^#[0-9A-Fa-f]{6}$');
BCREATE DOMAIN hex_color AS varchar(7) CHECK (VALUE LIKE '#______');
CCREATE DOMAIN hex_color AS varchar(7) CHECK (VALUE ~ '^#[0-9A-Fa-f]{6}$');
DCREATE DOMAIN hex_color AS varchar(7) CHECK (VALUE ~ '^#[0-9]{6}$');
Step-by-Step Solution
Solution:
  1. Step 1: Check base type and length

    The string is 7 characters: '#' plus 6 hex digits, so varchar(7) is correct.
  2. Step 2: Verify regex pattern

    The regex '^#[0-9A-Fa-f]{6}$' matches '#' followed by exactly 6 hex digits (case insensitive).
  3. Final Answer:

    CREATE DOMAIN hex_color AS varchar(7) CHECK (VALUE ~ '^#[0-9A-Fa-f]{6}$'); -> Option C
  4. Quick Check:

    Regex matches '#' plus 6 hex digits, length matches [OK]
Quick Trick: Use regex to validate hex color format precisely [OK]
Common Mistakes:
  • Wrong length in char type
  • Using LIKE instead of regex
  • Ignoring case in hex digits

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes