How to restrict a column value in SQLite / MySQL

后端 未结 5 1987
日久生厌
日久生厌 2020-12-14 18:03

I would like to restrict a column value in a SQL table. For example, the column values can only be \"car\" or \"bike\" or \"van\". My question is how do you achieve this in

5条回答
  •  别那么骄傲
    2020-12-14 18:37

    If you want to go with DB-side validation, you can use triggers. See this for SQLite, and this detailed how-to for MySQL.

    So the question is really whether you should use Database validation or not. If you have multiple clients -- whether they are different programs, or multiple users (with possibly different versions of the program) -- then going the database route is definitely best. The database is (hopefully) centralized, so you can decouple some of the details of validation. In your particular case, you can verify that the value being inserted into the column is contained in a separate table that simply lists valid values.

    On the other hand, if you have little experience with databases, plan to target several different databases, and don't have the time to develop expertise, perhaps simple application level validation is the most expedient choice.

提交回复
热议问题