[英]How can I create an auto generated column that always has its default value in MySQL?
I want to create a MySQL column that always uses its default value and won't allow the user to insert values in it.我想创建一个始终使用其默认值并且不允许用户在其中插入值的 MySQL 列。
It's a creation_date column and I've set its default value to now(), but the user can insert any date value in this column if he wants to.这是一个 creation_date 列,我已将其默认值设置为 now(),但用户可以根据需要在此列中插入任何日期值。 The now() is only used if the user don't provide any values for that column.
now() 仅在用户未为该列提供任何值时使用。
I can imagine a few ways to accomplish my goal using triggers, but, is this the right way?我可以想象几种使用触发器实现目标的方法,但是,这是正确的方法吗?
Isn't there a "protected" flag to set on the column or something like that?是否没有在列上设置“受保护”标志或类似的东西?
There is no "protected" column concept, no.没有“受保护”列的概念,没有。 You would need to use triggers.
您将需要使用触发器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.