Should data validation be done at the database level?

后端 未结 12 1623
半阙折子戏
半阙折子戏 2021-02-07 02:45

I am writing some stored procedures to create tables and add data. One of the fields is a column that indicates percentage. The value there should be 0-100. I started thinkin

12条回答
  •  萌比男神i
    2021-02-07 03:27

    It would depend on how you are interacting with the database, IMO. For example, if the only way to the database is through your application, then just do the validation there.

    If you are going to allow other applications to update the database, then you may want to put the validation in the database, so that no matter how the data gets in there it gets validated at the lowest level.

    But, validation should go on at various levels, to give the user the quickest opportunity possible to know that there is a problem.

    You didn't mention which version of SQL Server, but you can look at user defined datatypes and see if that would help you out, as you can just centralize the validation.

提交回复
热议问题