简体   繁体   English

如何创建始终在 MySQL 中具有默认值的自动生成列?

[英]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.

相关问题 如何在连接auto_increment值列和具有默认值的列时创建触发器 - how to create a trigger in concatenating a auto_increment value column and a column that has a default value 我如何创建第二个自动增量列mysql,其默认值每次插入新用户时都会增加 - how do i create a second auto increment column mysql with a default value that increases each time a new user is inserted 如何在MySQL中获取空表的列的默认值? - How can I get the default value of a column for an empty table in MySQL? 我可以设置在MySQL上自动递增的ID的默认整数值吗? - Can I set a default integer value of an id that is auto incremented on MySQL? mysql,使用默认值创建列 - mysql, Create Column with a default value 我如何在使用MySQL自动递增的MYSQL中创建id列? - How can I make an id column in MYSQL which has auto increment using python? 如何在第3列中显示2列mysql复合主键值作为默认值? - How can I show a 2 column mysql composite primary key value as a default value in a 3rd column? 如何在已经存在且有数据的表中创建 AUTO_INCREMENT 列? - How can I create an AUTO_INCREMENT column in a table that already exists and has data? 如何阻止 Gephi 在导入时擦除 MySQL 节点表中的“ID”列并用自动增量列代替 - How can I stop Gephi from erasing the `ID` column in a MySQL nodes table on import and substituting an auto-increment column in its place 在mysql中使用具有`|字符作为其值的列进行搜索 - Searching with a column that has `|` character as its value in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM