Chris Klepeis
Chris Klepeis

Reputation: 9973

SQL Server Check Constraint Inquiry

I have a table which stores payments and want to ensure if the payment method is a credit card then the card type field should be IN ('Visa', 'MasterCard', 'Discover', 'American Express'), however if the payment method is not a credit card then the card type field should be NULL.

Will a check constraint allow me to use a statement like this:

(method = 'CC' AND cardType IN ('Visa', 'MasterCard', 'Discover', 'American Express'))
OR
(method != 'CC' AND cardType = NULL)

I might be totally off base with this one since constraints should maybe only be used to check 1 field (not sure).

Also if such a statement is allowed would there possibly be an adverse performance hit?

Edit: I plan on eventually creating a cardType field and have cardType in our payments table as a foreign key... this is just something I'm thinking of doing in the meantime

Upvotes: 3

Views: 802

Answers (1)

cjk
cjk

Reputation: 46415

Why not just foreign key to another table and allow nulls?

Even better would be to ahve a card type code in another table, and store a key to that.

Upvotes: 3

Related Questions