No matter how I try it, I can't getting the book's answer for this puzzle to work. Does anyone
know of a solution that will work for this problem.
The Problem is:
"How do you ensure that a column will have a single alphabetic character string in it? (That means
no spaces, no numbers, and no special characters.)"
The book's solution is as follows:
CREATE TABLE Foobar
(alpha_only VARCHAR(6) CHECK ((UPPER(TRIM(alpha_only)) || 'AAAAA') BETWEEN 'AAAAAA' AND
'ZZZZZZ')
);
However, this check constraint only prevents numerics beginning with 'A'.
So the constraint works by preventing following strings that begin with these kinds of characters:
!....,
4....,
A!...,
A4...
But the constraint fails to prevent non-alphabetic characters when the string starts with a
character > A. Thus B thru Z can be follow on not alphabetic characters.
I compared the result from PostgreSQL with sqlite and access. They returned the same result.
According to the text, this solution "could" have been generalized to work with more complicated
strings as well. For example string "masks" could be used to enforce a kind of tagging conventionlike 'AA44444',
'BB55555'.
Has anyone seen or done anything like this before?
I am interested to hear what kind of solutions there are.
Regards,
Richard Broersma Jr.