1

My question is not formuled from a database point of view, but advantages from a business logic point of view.

Using as example table CreditCard and column CreditCardNumber. I can assign a unique index to CreditCardNumber, because there's no possible to have two identical CreditCardNumber. But, isn't this a business rule? So should I allow similar CreditCardNumber in database, and check myself the uniqueness in the business logic layer?

Am I correct? Or which advantages exists about using unique indexes in this case? Or both cases are correct and I can choose anyone?

Thanks.

1 Answers1

0

Enforcing a unique credit card number (or any other similar rule) should be done at both the application layer as well as the database layer.

You test for uniqueness at the application layer so you can provide feedback to the user. For instance, a clerk is typing in a new credit card number. You want your application to check if the number is already in the database and if so, alert the user so they can correct the error.

You want to enforce having a unique number at the database level to make absolutely sure that no duplicate entries are made for a column that must be unique.

Benny Hill
  • 6,191
  • 4
  • 39
  • 59