mysqlsqliterestrictiontablecolumn

How to restrict a column value in SQLite / MySQL


I would like to restrict a column value in a table. For example, the column values can only be car or bike, or van. My question is how do you achieve this in SQL, and is it a good idea to do this on the DB side or should I let the application restrict the input?

I also have the intention to add or remove more values in the future, for example, truck.

The types of Databases I am using are SQLite and MySQL.


Solution

  • Add a new table containing these means of transport, and make your column a foreign key to that table. New means of transport can be added to the table in future, and your column definition remains the same.

    With this construction, I would definitively choose to regulate this at the DB level, rather than that of the application.