Hello guys I had this question on my SQL exam and I do not know what the answer should be. Can you help me?
The table Employees, among others, contains a column called "Country" with a length of 3 characters and a column called "State" with a maximum length of 30 characters. If the value ‘USA’ is present in the "Country" column, then the "State" column should only contain two characters. For other values in the "Country" column, the "State" column can contain 0, 1, 2, or a maximum of 30 characters. Which of the following check constraints will ensure that the entered or modified data comply with the given requirements?
A)
Country = ‘USA’ AND LEN(State) =2
B)
Country = ‘USA’ OR LEN(State) =2
C)
Country <> ‘USA’ AND LEN(State) = 2
D)
Country <> ‘USA’ OR LEN(State) = 2
My teacher is saying that the right answer is D), but I just can’t wrap my head around that answer and How it would make sense. I thought that A) is the answer since both ‘USA’ and length should return true in order to be inserted. I think that D) answer would allow inserting values like ‘USA’ and ‘FILA’ since or will return true if one of ether is true.
Thanks in advance!
2
Answers
D would be the right answer, if a country is not equal to USA, the check constraint will not block, if USA is the country will not block only when the the state has only 2 letters, else it reports an error see sample
fiddle
As a preface, in SQL, and in computer-programming in general,
OR
is a logical disjunction – whereas in spoken English language "or" is generally used as a logical conjunction (i.e. "and" or "both").When looking at an expression involving
AND
orOR
, remember that it means you need to evaluate each operand (left-side and right-side) of theOR
expression independently, and the entire expression is eitherTRUE
orFALSE
(orUNKNOWN
if you have SQLNULL
s) according to this table (NULL
/UNKNOWN
omitted for brevity):OR
TRUE
TRUE
TRUE
OR
TRUE
FALSE
TRUE
OR
FALSE
TRUE
TRUE
OR
FALSE
FALSE
FALSE
AND
TRUE
TRUE
TRUE
AND
TRUE
FALSE
FALSE
AND
FALSE
TRUE
FALSE
AND
FALSE
FALSE
FALSE
To save time, I’ll just post some example data for each possible interpretation:
Country
State
'USA' = 'USA'
– and alsoLEN('WA') = 2
LEN('Foobar') = 6
not2
'Canada' <> 'USA'
'Canada' <> 'USA'
– and also:LEN('Foobar') = 6
not2
Country
State
'USA' = 'USA'
– and alsoLEN('WA') = 2
'USA' = 'USA'
– theLEN
term is ignoredLEN('NB') = 2
– the'Canada' <> 'USA'
term is ignored)'Canada' <> 'USA'
– and alsoLEN('Foobar') = 6
not2
Country
State
'USA' <> 'USA'
is false – even though theLEN(State)
part of the check passes usingAND
means both sides of theAND
must succeed for a pass.'USA' <> 'USA'
is false – andLEN('Foobar') = 6
not2
– so both sides of theAND
fail.'Canada' <> 'USA'
and alsoLEN('NB') = 2
'Canada' <> 'USA'
, butLEN('NB') = 6
Country
State
'USA' <> 'USA'
is false, but as this is anOR
condition, theLEN('WA') = 2
part passes.'USA' <> 'USA'
is false, so this can still pass ifLEN(State) = 2
, but in this case it’sLEN('Foobar') = 6
so it fails'Canada' <> 'USA'
is true, we can ignore theLEN(State)
clause, but even if we did, that would also pass asLEN('NB') = 2
.'Canada' <> 'USA'
is true, we can ignore theLEN(State)
clause