Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 412
Chris Klepeis Avatar asked Jul 09 '26 00:07

Chris Klepeis


1 Answers

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.

like image 69
cjk Avatar answered Jul 11 '26 17:07

cjk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!