如何在SQLite/MySQL中限制列值 [英] How to restrict a column value in SQLite / MySQL
问题描述
我想限制SQL表中的列值.例如,列值只能是"car"或"bike"或"van".我的问题是如何在SQL中实现此目标?在DB端执行此操作是一个好主意,还是让应用程序限制输入?
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 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".
我正在使用的数据库类型是SQLite和MySQL.
The type of Databases I am using are SQLite and MySQL.
推荐答案
添加包含这些传输方式的新表,并将您的列作为该表的外键.将来可以将新的传输方式添加到表中,并且列定义保持不变.
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.
这篇关于如何在SQLite/MySQL中限制列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!