Here's how I would do it:
CREATE TABLE twoletters ( code CHAR(2) NOT NULL , CONSTRAINT twoletters CHECK ( CHARACTER_LENGTH(code) = 2 AND SUBSTRING(code FROM 1 FOR 1) BETWEEN 'A' AND 'Z' AND SUBSTRING(code FROM 2 FOR 1) BETWEEN 'A' AND 'Z' ) );
Notice that I chose CHAR(2). This would allow the CHARACTER_LENGTH condition to be omitted, but I left it in if you cannot change your column from VARCHAR.
You may need to change the standard SQL functions (CHARACTER_LENGTH and SUBSTRING) to match your specific database system. For instance, in SQL Server you would use
Dig deeper on Oracle development languages
Related Q&A from Rudy Limeback, SQL Consultant, r937.com
Read about the Mimer Validator, a tool used to verify your SQL code, in this tip from SQL expert Rudy Limeback.continue reading
Read SQL expert Rudy Limeback's advice for counting combinations in a table with SQL's GROUP BY clausecontinue reading
Read an example of an SQL case expression from our SQL expert Rudy Limeback.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.